Debian Codegraph @

src/soc/mediatek/common/dp/dptx_v1.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <assert.h>
4 #include <commonlib/bsd/helpers.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <device/mmio.h>
8 #include <edid.h>
9 #include <soc/addressmap.h>
10 #include <soc/dptx.h>
11 #include <soc/dptx_hal.h>
12 #include <soc/dptx_reg.h>
13 #include <soc/dp_intf.h>
14 #include <string.h>
15 #include <timer.h>
16
17 static void dptx_training_checkswingpre(struct mtk_dp *mtk_dp,
18 u8 target_lane_count,
19 const u8 *dpcp202_x, u8 *dpcp_buf)
20 {
21 u8 swing_value[DPTX_LANE_MAX] = {0}, preemphasis[DPTX_LANE_MAX] = {0};
22
23 assert(target_lane_count <= DPTX_LANE_MAX);
24
25 /* Lane 0 */
26 if (target_lane_count >= 0x1) {
27 swing_value[0] = (dpcp202_x[4] & 0x3);
28 preemphasis[0] = (dpcp202_x[4] & 0xc) >> 2;
29
30 /*
31 * Adjust the swing and pre-emphasis done,
32 * and notify sink side.
33 */
34 dpcp_buf[0] = swing_value[0] | (preemphasis[0] << 3);
35
36 /* Max swing reached. */
37 if (swing_value[0] == DPTX_SWING3)
38 dpcp_buf[0] |= BIT(2);
39
40 /* Max pre-emphasis reached. */
41 if (preemphasis[0] == DPTX_PREEMPHASIS3)
42 dpcp_buf[0] |= BIT(5);
43 }
44
45 /* Lane 1 */
46 if (target_lane_count >= 0x2) {
47 swing_value[1] = (dpcp202_x[4] & 0x30) >> 4;
48 preemphasis[1] = (dpcp202_x[4] & 0xc0) >> 6;
49
50 /*
51 * Adjust the swing and pre-emphasis done,
52 * and notify sink side.
53 */
54 dpcp_buf[1] = swing_value[1] | (preemphasis[1] << 3);
55
56 /* Max swing reached. */
57 if (swing_value[1] == DPTX_SWING3)
58 dpcp_buf[1] |= BIT(2);
59
60 /* Max pre-emphasis reached. */
61 if (preemphasis[1] == DPTX_PREEMPHASIS3)
62 dpcp_buf[1] |= BIT(5);
63 }
64
65 /* Lane 2 and Lane 3 */
66 if (target_lane_count >= 0x3) {
67 assert(target_lane_count == 0x4);
68
69 /* Lane 2 */
70 swing_value[2] = (dpcp202_x[5] & 0x3);
71 preemphasis[2] = (dpcp202_x[5] & 0x0c) >> 2;
72
73 /*
74 * Adjust the swing and pre-emphasis done,
75 * and notify sink side.
76 */
77 dpcp_buf[2] = swing_value[2] | (preemphasis[2] << 3);
78
79 /* Max swing reached. */
80 if (swing_value[2] == DPTX_SWING3)
81 dpcp_buf[2] |= BIT(2);
82
83 /* Max pre-emphasis reached. */
84 if (preemphasis[2] == DPTX_PREEMPHASIS3)
85 dpcp_buf[2] |= BIT(5);
86
87 /* Lane 3 */
88 swing_value[3] = (dpcp202_x[5] & 0x30) >> 4;
89 preemphasis[3] = (dpcp202_x[5] & 0xc0) >> 6;
90
91 /*
92 * Adjust the swing and pre-emphasis done,
93 * and notify sink side.
94 */
95 dpcp_buf[0x3] = swing_value[3] | (preemphasis[3] << 3);
96
97 /* Max swing reached. */
98 if (swing_value[3] == DPTX_SWING3)
99 dpcp_buf[3] |= BIT(2);
100
101 /* Max pre-emphasis reached. */
102 if (preemphasis[3] == DPTX_PREEMPHASIS3)
103 dpcp_buf[3] |= BIT(5);
104 }
105
106 /* Adjust the swing and pre-emphasis */
107 dptx_hal_set_swing_preemphasis(mtk_dp, target_lane_count,
108 swing_value, preemphasis);
109
110 /* Wait signal stable enough */
111 mdelay(2);
112 }
113
114 static int dptx_train_tps1(struct mtk_dp *mtk_dp, u8 target_lanecount,
115 int *status_ctrl, int *iteration, u8 *dpcp_buffer,
116 u8 *dpcd206)
117 {
118 u8 tmp_val[6];
119 u8 dpcd200c[3];
120
121 printk(BIOS_INFO, "CR Training START\n");
122 dptx_hal_setscramble(mtk_dp, false);
123
124 if (*status_ctrl == 0x0) {
125 dptx_hal_set_txtrainingpattern(mtk_dp, BIT(4));
126 *status_ctrl = 0x1;
127 tmp_val[0] = 0x21;
128 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
129 DPCD_00102, 0x1, tmp_val);
130 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
131 DPCD_00206, 0x2, tmp_val + 4);
132 *iteration = *iteration + 1;
133
134 dptx_training_checkswingpre(mtk_dp, target_lanecount,
135 tmp_val, dpcp_buffer);
136 }
137
138 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
139 DPCD_00103, target_lanecount, dpcp_buffer);
140
141 dptx_link_train_clock_recovery_delay(mtk_dp->rx_cap);
142
143 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
144 DPCD_00202, 0x6, tmp_val);
145
146 if (mtk_dp->train_info.sink_extcap_en) {
147 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
148 DPCD_0200C, 0x3, dpcd200c);
149 tmp_val[0] = dpcd200c[0];
150 tmp_val[1] = dpcd200c[1];
151 tmp_val[2] = dpcd200c[2];
152 }
153
154 if (dptx_clock_recovery_ok(tmp_val, target_lanecount)) {
155 printk(BIOS_INFO, "CR Training Success\n");
156
157 mtk_dp->train_info.cr_done = true;
158 *iteration = 0x1;
159
160 return TRAIN_STEP_SUCCESS;
161 }
162
163 printk(BIOS_INFO, "CR Training Fail\n");
164
165 /* Requested swing and emp is the same with last time. */
166 if (*dpcd206 == tmp_val[4]) {
167 *iteration = *iteration + 1;
168 if (*dpcd206 & 0x3)
169 return TRAIN_STEP_FAIL_BREAK;
170 } else {
171 *dpcd206 = tmp_val[4];
172 }
173
174 return TRAIN_STEP_FAIL_NOT_BREAK;
175 }
176
177 static int dptx_train_tps2_3(struct mtk_dp *mtk_dp, u8 target_lanecount,
178 int *status_ctrl, int *iteration, u8 *dpcp_buffer,
179 u8 *dpcd206)
180 {
181 u8 tmp_val[6];
182 u8 dpcd200c[3];
183
184 printk(BIOS_INFO, "EQ Training START\n");
185
186 if (*status_ctrl == 0x1) {
187 if (mtk_dp->train_info.tps4)
188 dptx_hal_set_txtrainingpattern(mtk_dp, BIT(7));
189 else if (mtk_dp->train_info.tps3)
190 dptx_hal_set_txtrainingpattern(mtk_dp, BIT(6));
191 else
192 dptx_hal_set_txtrainingpattern(mtk_dp, BIT(5));
193
194 if (mtk_dp->train_info.tps4) {
195 tmp_val[0] = 0x07;
196 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
197 DPCD_00102, 0x1, tmp_val);
198 } else if (mtk_dp->train_info.tps3) {
199 tmp_val[0] = 0x23;
200 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
201 DPCD_00102, 0x1, tmp_val);
202 } else {
203 tmp_val[0] = 0x22;
204 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
205 DPCD_00102, 0x1, tmp_val);
206 }
207
208 *status_ctrl = 0x2;
209 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
210 DPCD_00206, 0x2, tmp_val + 4);
211
212 *iteration = *iteration + 1;
213 dptx_training_checkswingpre(mtk_dp, target_lanecount,
214 tmp_val, dpcp_buffer);
215 }
216
217 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE, DPCD_00103,
218 target_lanecount, dpcp_buffer);
219 dptx_link_train_channel_eq_delay(mtk_dp->rx_cap);
220
221 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
222 DPCD_00202, 0x6, tmp_val);
223
224 if (mtk_dp->train_info.sink_extcap_en) {
225 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
226 DPCD_0200C, 0x3, dpcd200c);
227 tmp_val[0] |= dpcd200c[0];
228 tmp_val[1] |= dpcd200c[1];
229 tmp_val[2] |= dpcd200c[2];
230 }
231
232 if (!dptx_clock_recovery_ok(tmp_val, target_lanecount)) {
233 printk(BIOS_INFO, "EQ Training Fail\n");
234 mtk_dp->train_info.cr_done = false;
235 mtk_dp->train_info.eq_done = false;
236 return TRAIN_STEP_FAIL_BREAK;
237 }
238
239 if (dptx_channel_eq_ok(tmp_val, target_lanecount)) {
240 printk(BIOS_INFO, "EQ Training Success\n");
241 mtk_dp->train_info.eq_done = true;
242 return TRAIN_STEP_SUCCESS;
243 }
244 printk(BIOS_INFO, "EQ Training Fail\n");
245
246 if (*dpcd206 == tmp_val[4])
247 *iteration = *iteration + 1;
248 else
249 *dpcd206 = tmp_val[4];
250
251 return TRAIN_STEP_FAIL_NOT_BREAK;
252 }
253
254 static int dptx_trainingflow(struct mtk_dp *mtk_dp,
255 u8 lanerate, u8 lanecount)
256 {
257 u8 tmp_val[6];
258 u8 target_linkrate = lanerate;
259 u8 target_lanecount = lanecount;
260 u8 dpcp_buffer[4];
261 u8 dpcd206;
262 bool pass_tps1 = false;
263 bool pass_tps2_3 = false;
264 int train_retry, status_ctrl, iteration;
265
266 memset(tmp_val, 0, sizeof(tmp_val));
267 memset(dpcp_buffer, 0, sizeof(dpcp_buffer));
268
269 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
270 DPCD_00600, 0x1, tmp_val);
271 if (tmp_val[0] != 0x1) {
272 tmp_val[0] = 0x1;
273 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
274 DPCD_00600, 0x1, tmp_val);
275 mdelay(1);
276 }
277
278 tmp_val[0] = target_linkrate;
279 tmp_val[1] = target_lanecount | DPTX_AUX_SET_ENAHNCED_FRAME;
280 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
281 DPCD_00100, 0x2, tmp_val);
282
283 if (mtk_dp->train_info.sink_ssc_en) {
284 tmp_val[0x0] = 0x10;
285 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
286 DPCD_00107, 0x1, tmp_val);
287 }
288
289 train_retry = 0x0;
290 status_ctrl = 0x0;
291 iteration = 0x1;
292 dpcd206 = 0xff;
293
294 dptx_hal_set_txlane(mtk_dp, target_lanecount / 2);
295 dptx_hal_set_txrate(mtk_dp, target_linkrate);
296
297 do {
298 train_retry++;
299 if (!pass_tps1) {
300 int ret = dptx_train_tps1(mtk_dp, target_lanecount,
301 &status_ctrl, &iteration,
302 dpcp_buffer, &dpcd206);
303 if (ret == TRAIN_STEP_FAIL_BREAK)
304 break;
305 if (ret == TRAIN_STEP_SUCCESS) {
306 pass_tps1 = true;
307 train_retry = 0;
308 }
309 } else {
310 int ret = dptx_train_tps2_3(mtk_dp, target_lanecount,
311 &status_ctrl, &iteration,
312 dpcp_buffer, &dpcd206);
313 if (ret == TRAIN_STEP_FAIL_BREAK)
314 break;
315 if (ret == TRAIN_STEP_SUCCESS) {
316 pass_tps2_3 = true;
317 break;
318 }
319 }
320
321 dptx_training_checkswingpre(mtk_dp, target_lanecount,
322 tmp_val, dpcp_buffer);
323
324 } while (train_retry < DPTX_TRAIN_RETRY_LIMIT &&
325 iteration < DPTX_TRAIN_MAX_ITERATION);
326
327 tmp_val[0] = 0x0;
328 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
329 DPCD_00102, 0x1, tmp_val);
330 dptx_hal_set_txtrainingpattern(mtk_dp, 0);
331
332 if (!pass_tps2_3) {
333 printk(BIOS_ERR, "Link Training Fail\n");
334 return DPTX_TRANING_FAIL;
335 }
336
337 mtk_dp->train_info.linkrate = target_linkrate;
338 mtk_dp->train_info.linklane_count = target_lanecount;
339
340 dptx_hal_setscramble(mtk_dp, true);
341
342 tmp_val[0] = target_lanecount | DPTX_AUX_SET_ENAHNCED_FRAME;
343
344 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
345 DPCD_00101, 0x1, tmp_val);
346 dptx_hal_set_ef_mode(mtk_dp, ENABLE_DPTX_EF_MODE);
347
348 printk(BIOS_INFO, "Link Training Success\n");
349 return DPTX_PASS;
350 }
351
352 static void dptx_training_changemode(struct mtk_dp *mtk_dp)
353 {
354 dptx_hal_phyd_reset(mtk_dp);
355 dptx_hal_reset_swing_preemphasis(mtk_dp);
356 dptx_hal_ssc_en(mtk_dp, mtk_dp->train_info.sink_ssc_en);
357
358 mdelay(2);
359 }
360
361 int dptx_set_trainingstart(struct mtk_dp *mtk_dp)
362 {
363 u8 lanecount;
364 u8 linkrate;
365 u8 buffer;
366 u8 limit;
367 u8 max_linkrate;
368 struct stopwatch sw;
369
370 stopwatch_init(&sw);
371 buffer = 0x1;
372 dptx_auxwrite_dpcd(mtk_dp, DP_AUX_NATIVE_WRITE,
373 DPCD_00600, 0x1, &buffer);
374
375 linkrate = mtk_dp->rx_cap[1];
376 lanecount = mtk_dp->rx_cap[2] & 0x1f;
377
378 printk(BIOS_INFO, "RX support linkrate = %#x, lanecount = %#x\n",
379 linkrate, lanecount);
380
381 mtk_dp->train_info.linkrate =
382 (linkrate >= mtk_dp->train_info.sys_max_linkrate) ?
383 mtk_dp->train_info.sys_max_linkrate : linkrate;
384 mtk_dp->train_info.linklane_count = (lanecount >= MAX_LANECOUNT) ?
385 MAX_LANECOUNT : lanecount;
386
387 if (mtk_dp->train_info.sink_extcap_en)
388 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
389 DPCD_02002, 0x1, &buffer);
390 else
391 dptx_auxread_dpcd(mtk_dp, DP_AUX_NATIVE_READ,
392 DPCD_00200, 0x1, &buffer);
393
394 if ((buffer & 0xbf) != 0)
395 mtk_dp->train_info.sink_count_num = buffer & 0xbf;
396
397 linkrate = mtk_dp->train_info.linkrate;
398 lanecount = mtk_dp->train_info.linklane_count;
399
400 switch (linkrate) {
401 case DP_LINKRATE_RBR:
402 case DP_LINKRATE_HBR:
403 case DP_LINKRATE_HBR2:
404 case DP_LINKRATE_HBR25:
405 case DP_LINKRATE_HBR3:
406 break;
407 default:
408 mtk_dp->train_info.linkrate = DP_LINKRATE_HBR3;
409 break;
410 };
411
412 max_linkrate = linkrate;
413 limit = 0x6;
414
415 do {
416 mtk_dp->train_info.cr_done = false;
417 mtk_dp->train_info.eq_done = false;
418
419 dptx_training_changemode(mtk_dp);
420 dptx_trainingflow(mtk_dp, linkrate, lanecount);
421
422 if (!mtk_dp->train_info.cr_done) {
423 /* CR fail and reduce link capability. */
424 switch (linkrate) {
425 case DP_LINKRATE_RBR:
426 lanecount = lanecount / 2;
427 linkrate = max_linkrate;
428
429 if (lanecount == 0x0)
430 return DPTX_TRANING_FAIL;
431 break;
432 case DP_LINKRATE_HBR:
433 linkrate = DP_LINKRATE_RBR;
434 break;
435 case DP_LINKRATE_HBR2:
436 linkrate = DP_LINKRATE_HBR;
437 break;
438 case DP_LINKRATE_HBR3:
439 linkrate = DP_LINKRATE_HBR2;
440 break;
441 default:
442 return DPTX_TRANING_FAIL;
443 };
444 } else if (!mtk_dp->train_info.eq_done) {
445 /* EQ fail and reduce lane counts. */
446 if (lanecount == DP_LANECOUNT_4)
447 lanecount = DP_LANECOUNT_2;
448 else if (lanecount == DP_LANECOUNT_2)
449 lanecount = DP_LANECOUNT_1;
450 else
451 return DPTX_TRANING_FAIL;
452 } else {
453 printk(BIOS_INFO, "%s done after %lld msecs\n", __func__,
454 stopwatch_duration_msecs(&sw));
455 return DPTX_PASS;
456 }
457 } while (--limit > 0);
458
459 return DPTX_TRANING_FAIL;
460 }