Debian Codegraph @

src/soc/nvidia/tegra210/dp.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * drivers/video/tegra/dc/dp.c
5 */
6
7 #include <console/console.h>
8 #include <device/device.h>
9 #include <device/i2c_simple.h>
10 #include <edid.h>
11 #include <string.h>
12 #include <delay.h>
13 #include <soc/addressmap.h>
14 #include <soc/clock.h>
15 #include <soc/display.h>
16 #include <soc/nvidia/tegra/i2c.h>
17 #include <soc/nvidia/tegra/dc.h>
18 #include <soc/nvidia/tegra/types.h>
19 #include <soc/nvidia/tegra/pwm.h>
20 #include <soc/nvidia/tegra/displayport.h>
21 #include <soc/sor.h>
22 #include <types.h>
23
24 #include "chip.h"
25
26 #define DO_FAST_LINK_TRAINING 0
27
28 struct tegra_dc dc_data;
29
30 enum {
31 DP_LT_SUCCESS = 0,
32 DP_LT_FAILED = -1,
33 };
34
35 struct tegra_dc_dp_data dp_data;
36
37 static inline u32 tegra_dpaux_readl(struct tegra_dc_dp_data *dp, u32 reg)
38 {
39 void *addr = dp->aux_base + (u32)(reg << 2);
40 u32 reg_val = READL(addr);
41 return reg_val;
42 }
43
44 static inline void tegra_dpaux_writel(struct tegra_dc_dp_data *dp,
45 u32 reg, u32 val)
46 {
47 void *addr = dp->aux_base + (u32)(reg << 2);
48 WRITEL(val, addr);
49 }
50
51 static inline u32 tegra_dc_dpaux_poll_register(struct tegra_dc_dp_data *dp,
52 u32 reg, u32 mask, u32 exp_val,
53 u32 poll_interval_us,
54 u32 timeout_us)
55 {
56 u32 reg_val = 0;
57 u32 temp = timeout_us;
58
59 do {
60 udelay(poll_interval_us);
61 reg_val = tegra_dpaux_readl(dp, reg);
62 if (timeout_us > poll_interval_us)
63 timeout_us -= poll_interval_us;
64 else
65 break;
66 } while ((reg_val & mask) != exp_val);
67
68 if ((reg_val & mask) == exp_val)
69 return 0; /* success */
70 printk(BIOS_ERR,
71 "dpaux_poll_register 0x%x: timeout: "
72 "(reg_val)0x%08x & (mask)0x%08x != (exp_val)0x%08x\n",
73 reg, reg_val, mask, exp_val);
74 return temp;
75 }
76
77 static inline int tegra_dpaux_wait_transaction(struct tegra_dc_dp_data *dp)
78 {
79 /* According to DP spec, each aux transaction needs to finish
80 within 40ms. */
81 if (tegra_dc_dpaux_poll_register(dp, DPAUX_DP_AUXCTL,
82 DPAUX_DP_AUXCTL_TRANSACTREQ_MASK,
83 DPAUX_DP_AUXCTL_TRANSACTREQ_DONE,
84 100, DP_AUX_TIMEOUT_MS * 1000) != 0) {
85 printk(BIOS_INFO, "dp: DPAUX transaction timeout\n");
86 return -1;
87 }
88 return 0;
89 }
90
91 static int tegra_dc_dpaux_write_chunk(struct tegra_dc_dp_data *dp, u32 cmd,
92 u32 addr, u8 *data, u32 *size,
93 u32 *aux_stat)
94 {
95 int i;
96 u32 reg_val;
97 u32 timeout_retries = DP_AUX_TIMEOUT_MAX_TRIES;
98 u32 defer_retries = DP_AUX_DEFER_MAX_TRIES;
99 u32 temp_data;
100
101 if (*size > DP_AUX_MAX_BYTES)
102 return -1; /* only write one chunk of data */
103
104 /* Make sure the command is write command */
105 switch (cmd) {
106 case DPAUX_DP_AUXCTL_CMD_I2CWR:
107 case DPAUX_DP_AUXCTL_CMD_MOTWR:
108 case DPAUX_DP_AUXCTL_CMD_AUXWR:
109 break;
110 default:
111 printk(BIOS_ERR, "dp: aux write cmd 0x%x is invalid\n", cmd);
112 return -1;
113 }
114
115 tegra_dpaux_writel(dp, DPAUX_DP_AUXADDR, addr);
116 for (i = 0; i < DP_AUX_MAX_BYTES / 4; ++i) {
117 memcpy(&temp_data, data, 4);
118 tegra_dpaux_writel(dp, DPAUX_DP_AUXDATA_WRITE_W(i), temp_data);
119 data += 4;
120 }
121
122 reg_val = tegra_dpaux_readl(dp, DPAUX_DP_AUXCTL);
123 reg_val &= ~DPAUX_DP_AUXCTL_CMD_MASK;
124 reg_val |= cmd;
125 reg_val &= ~DPAUX_DP_AUXCTL_CMDLEN_FIELD;
126 reg_val |= ((*size - 1) << DPAUX_DP_AUXCTL_CMDLEN_SHIFT);
127
128 while ((timeout_retries > 0) && (defer_retries > 0)) {
129 if ((timeout_retries != DP_AUX_TIMEOUT_MAX_TRIES) ||
130 (defer_retries != DP_AUX_DEFER_MAX_TRIES))
131 udelay(1);
132
133 reg_val |= DPAUX_DP_AUXCTL_TRANSACTREQ_PENDING;
134 tegra_dpaux_writel(dp, DPAUX_DP_AUXCTL, reg_val);
135
136 if (tegra_dpaux_wait_transaction(dp))
137 printk(BIOS_ERR, "dp: aux write transaction timeout\n");
138
139 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
140
141 if ((*aux_stat & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR_PENDING) ||
142 (*aux_stat & DPAUX_DP_AUXSTAT_RX_ERROR_PENDING) ||
143 (*aux_stat & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR_PENDING) ||
144 (*aux_stat & DPAUX_DP_AUXSTAT_NO_STOP_ERROR_PENDING)) {
145 if (timeout_retries-- > 0) {
146 printk(BIOS_INFO, "dp: aux write retry (0x%x)"
147 " -- %d\n",
148 *aux_stat, timeout_retries);
149 /* clear the error bits */
150 tegra_dpaux_writel(dp, DPAUX_DP_AUXSTAT,
151 *aux_stat);
152 continue;
153 } else {
154 printk(BIOS_ERR, "dp: aux write got error"
155 " (0x%x)\n", *aux_stat);
156 return -1;
157 }
158 }
159
160 if ((*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_I2CDEFER) ||
161 (*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_DEFER)) {
162 if (defer_retries-- > 0) {
163 printk(BIOS_INFO, "dp: aux write defer (0x%x)"
164 " -- %d\n", *aux_stat, defer_retries);
165 /* clear the error bits */
166 tegra_dpaux_writel(dp, DPAUX_DP_AUXSTAT,
167 *aux_stat);
168 continue;
169 } else {
170 printk(BIOS_ERR, "dp: aux write defer exceeds"
171 " max retries (0x%x)\n", *aux_stat);
172 return -1;
173 }
174 }
175
176 if ((*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_MASK) ==
177 DPAUX_DP_AUXSTAT_REPLYTYPE_ACK) {
178 *size = ((*aux_stat) & DPAUX_DP_AUXSTAT_REPLY_M_MASK);
179 return 0;
180 } else {
181 printk(BIOS_ERR, "dp: aux write failed (0x%x)\n",
182 *aux_stat);
183 return -1;
184 }
185 }
186 /* Should never come to here */
187 return -1;
188 }
189
190 static int tegra_dc_dpaux_read_chunk(struct tegra_dc_dp_data *dp, u32 cmd,
191 u32 addr, u8 *data, u32 *size,
192 u32 *aux_stat)
193 {
194 u32 reg_val;
195 u32 timeout_retries = DP_AUX_TIMEOUT_MAX_TRIES;
196 u32 defer_retries = DP_AUX_DEFER_MAX_TRIES;
197
198 if (*size > DP_AUX_MAX_BYTES)
199 return -1; /* only read one chunk */
200
201 /* Check to make sure the command is read command */
202 switch (cmd) {
203 case DPAUX_DP_AUXCTL_CMD_I2CRD:
204 case DPAUX_DP_AUXCTL_CMD_I2CREQWSTAT:
205 case DPAUX_DP_AUXCTL_CMD_MOTRD:
206 case DPAUX_DP_AUXCTL_CMD_AUXRD:
207 break;
208 default:
209 printk(BIOS_ERR, "dp: aux read cmd 0x%x is invalid\n", cmd);
210 return -1;
211 }
212
213 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
214 if (!(*aux_stat & DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)) {
215 printk(BIOS_SPEW, "dp: HPD is not detected\n");
216 return -1;
217 }
218
219 tegra_dpaux_writel(dp, DPAUX_DP_AUXADDR, addr);
220
221 reg_val = tegra_dpaux_readl(dp, DPAUX_DP_AUXCTL);
222 reg_val &= ~DPAUX_DP_AUXCTL_CMD_MASK;
223 reg_val |= cmd;
224 reg_val &= ~DPAUX_DP_AUXCTL_CMDLEN_FIELD;
225 reg_val |= ((*size - 1) << DPAUX_DP_AUXCTL_CMDLEN_SHIFT);
226 while ((timeout_retries > 0) && (defer_retries > 0)) {
227 if ((timeout_retries != DP_AUX_TIMEOUT_MAX_TRIES) ||
228 (defer_retries != DP_AUX_DEFER_MAX_TRIES))
229 udelay(DP_DPCP_RETRY_SLEEP_NS * 2);
230
231 reg_val |= DPAUX_DP_AUXCTL_TRANSACTREQ_PENDING;
232 tegra_dpaux_writel(dp, DPAUX_DP_AUXCTL, reg_val);
233
234 if (tegra_dpaux_wait_transaction(dp))
235 printk(BIOS_INFO, "dp: aux read transaction timeout\n");
236
237 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
238
239 if ((*aux_stat & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR_PENDING) ||
240 (*aux_stat & DPAUX_DP_AUXSTAT_RX_ERROR_PENDING) ||
241 (*aux_stat & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR_PENDING) ||
242 (*aux_stat & DPAUX_DP_AUXSTAT_NO_STOP_ERROR_PENDING)) {
243 if (timeout_retries-- > 0) {
244 printk(BIOS_INFO, "dp: aux read retry (0x%x)"
245 " -- %d\n", *aux_stat,
246 timeout_retries);
247 /* clear the error bits */
248 tegra_dpaux_writel(dp, DPAUX_DP_AUXSTAT,
249 *aux_stat);
250 continue; /* retry */
251 } else {
252 printk(BIOS_ERR, "dp: aux read got error"
253 " (0x%x)\n", *aux_stat);
254 return -1;
255 }
256 }
257
258 if ((*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_I2CDEFER) ||
259 (*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_DEFER)) {
260 if (defer_retries-- > 0) {
261 printk(BIOS_INFO, "dp: aux read defer (0x%x)"
262 " -- %d\n", *aux_stat, defer_retries);
263 /* clear the error bits */
264 tegra_dpaux_writel(dp, DPAUX_DP_AUXSTAT,
265 *aux_stat);
266 continue;
267 } else {
268 printk(BIOS_INFO, "dp: aux read defer exceeds"
269 " max retries (0x%x)\n", *aux_stat);
270 return -1;
271 }
272 }
273
274 if ((*aux_stat & DPAUX_DP_AUXSTAT_REPLYTYPE_MASK) ==
275 DPAUX_DP_AUXSTAT_REPLYTYPE_ACK) {
276 int i;
277 u32 temp_data[4];
278
279 for (i = 0; i < DP_AUX_MAX_BYTES / 4; ++i)
280 temp_data[i] = tegra_dpaux_readl(dp,
281 DPAUX_DP_AUXDATA_READ_W(i));
282
283 *size = ((*aux_stat) & DPAUX_DP_AUXSTAT_REPLY_M_MASK);
284 memcpy(data, temp_data, *size);
285
286 return 0;
287 } else {
288 printk(BIOS_ERR, "dp: aux read failed (0x%x\n",
289 *aux_stat);
290 return -1;
291 }
292 }
293 /* Should never come to here */
294 printk(BIOS_ERR, "%s: can't\n", __func__);
295 return -1;
296 }
297
298 #if DO_FAST_LINK_TRAINING
299 static int tegra_dc_dpaux_read(struct tegra_dc_dp_data *dp, u32 cmd, u32 addr,
300 u8 *data, u32 *size, u32 *aux_stat)
301 {
302 u32 finished = 0;
303 u32 cur_size;
304 int ret = 0;
305
306 do {
307 cur_size = *size - finished;
308 if (cur_size > DP_AUX_MAX_BYTES)
309 cur_size = DP_AUX_MAX_BYTES;
310
311 ret = tegra_dc_dpaux_read_chunk(dp, cmd, addr,
312 data, &cur_size, aux_stat);
313 if (ret)
314 break;
315
316 /* cur_size should be the real size returned */
317 addr += cur_size;
318 data += cur_size;
319 finished += cur_size;
320
321 } while (*size > finished);
322
323 *size = finished;
324 return ret;
325 }
326 #endif /* DO_FAST_LINK_TRAINING */
327
328 static int tegra_dc_dp_dpcd_read(struct tegra_dc_dp_data *dp, u32 cmd,
329 u8 *data_ptr)
330 {
331 u32 size = 1;
332 u32 status = 0;
333 int ret;
334
335 ret = tegra_dc_dpaux_read_chunk(dp, DPAUX_DP_AUXCTL_CMD_AUXRD,
336 cmd, data_ptr, &size, &status);
337 if (ret)
338 printk(BIOS_ERR,
339 "dp: Failed to read DPCD data. CMD 0x%x, Status 0x%x\n",
340 cmd, status);
341
342 return ret;
343 }
344
345 static int tegra_dc_dp_dpcd_write(struct tegra_dc_dp_data *dp, u32 cmd,
346 u8 data)
347 {
348 u32 size = 1;
349 u32 status = 0;
350 int ret;
351
352 ret = tegra_dc_dpaux_write_chunk(dp, DPAUX_DP_AUXCTL_CMD_AUXWR,
353 cmd, &data, &size, &status);
354 if (ret)
355 printk(BIOS_ERR,
356 "dp: Failed to write DPCD data. CMD 0x%x, Status 0x%x\n",
357 cmd, status);
358 return ret;
359 }
360
361 static int tegra_dc_i2c_aux_read(struct tegra_dc_dp_data *dp, u32 i2c_addr,
362 u8 addr, u8 *data, u32 *size, u32 *aux_stat)
363 {
364 u32 finished = 0;
365 int ret = 0;
366
367 do {
368 u32 cur_size = MIN(DP_AUX_MAX_BYTES, *size - finished);
369
370 u32 len = 1;
371 ret = tegra_dc_dpaux_write_chunk(
372 dp, DPAUX_DP_AUXCTL_CMD_MOTWR, i2c_addr,
373 &addr, &len, aux_stat);
374 if (ret) {
375 printk(BIOS_ERR, "%s: error sending address to read.\n",
376 __func__);
377 break;
378 }
379
380 ret = tegra_dc_dpaux_read_chunk(
381 dp, DPAUX_DP_AUXCTL_CMD_I2CRD, i2c_addr,
382 data, &cur_size, aux_stat);
383 if (ret) {
384 printk(BIOS_ERR, "%s: error reading data.\n", __func__);
385 break;
386 }
387
388 /* cur_size should be the real size returned */
389 addr += cur_size;
390 data += cur_size;
391 finished += cur_size;
392 } while (*size > finished);
393
394 *size = finished;
395 return ret;
396 }
397
398 static void tegra_dc_dpaux_enable(struct tegra_dc_dp_data *dp)
399 {
400 /* clear interrupt */
401 tegra_dpaux_writel(dp, DPAUX_INTR_AUX, 0xffffffff);
402 /* do not enable interrupt for now. Enable them when Isr in place */
403 tegra_dpaux_writel(dp, DPAUX_INTR_EN_AUX, 0x0);
404
405 tegra_dpaux_writel(dp, DPAUX_HYBRID_PADCTL,
406 DPAUX_HYBRID_PADCTL_AUX_DRVZ_OHM_50 |
407 DPAUX_HYBRID_PADCTL_AUX_CMH_V0_70 |
408 0x18 << DPAUX_HYBRID_PADCTL_AUX_DRVI_SHIFT |
409 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV_ENABLE);
410
411 tegra_dpaux_writel(dp, DPAUX_HYBRID_SPARE,
412 DPAUX_HYBRID_SPARE_PAD_PWR_POWERUP);
413 }
414
415 static void tegra_dc_dp_dump_link_cfg(struct tegra_dc_dp_data *dp,
416 const struct tegra_dc_dp_link_config *link_cfg)
417 {
418 printk(BIOS_INFO, "DP config: cfg_name "
419 "cfg_value\n");
420 printk(BIOS_INFO, " Lane Count %d\n",
421 link_cfg->max_lane_count);
422 printk(BIOS_INFO, " SupportEnhancedFraming %s\n",
423 link_cfg->support_enhanced_framing ? "Y" : "N");
424 printk(BIOS_INFO, " Bandwidth %d\n",
425 link_cfg->max_link_bw);
426 printk(BIOS_INFO, " bpp %d\n",
427 link_cfg->bits_per_pixel);
428 printk(BIOS_INFO, " EnhancedFraming %s\n",
429 link_cfg->enhanced_framing ? "Y" : "N");
430 printk(BIOS_INFO, " Scramble_enabled %s\n",
431 link_cfg->scramble_ena ? "Y" : "N");
432 printk(BIOS_INFO, " LinkBW %d\n",
433 link_cfg->link_bw);
434 printk(BIOS_INFO, " lane_count %d\n",
435 link_cfg->lane_count);
436 printk(BIOS_INFO, " activespolarity %d\n",
437 link_cfg->activepolarity);
438 printk(BIOS_INFO, " active_count %d\n",
439 link_cfg->active_count);
440 printk(BIOS_INFO, " tu_size %d\n",
441 link_cfg->tu_size);
442 printk(BIOS_INFO, " active_frac %d\n",
443 link_cfg->active_frac);
444 printk(BIOS_INFO, " watermark %d\n",
445 link_cfg->watermark);
446 printk(BIOS_INFO, " hblank_sym %d\n",
447 link_cfg->hblank_sym);
448 printk(BIOS_INFO, " vblank_sym %d\n",
449 link_cfg->vblank_sym);
450 }
451
452 static int _tegra_dp_lower_link_config(struct tegra_dc_dp_data *dp,
453 struct tegra_dc_dp_link_config *link_cfg)
454 {
455 switch (link_cfg->link_bw) {
456 case SOR_LINK_SPEED_G1_62:
457 if (link_cfg->max_link_bw > SOR_LINK_SPEED_G1_62)
458 link_cfg->link_bw = SOR_LINK_SPEED_G2_7;
459 link_cfg->lane_count /= 2;
460 break;
461 case SOR_LINK_SPEED_G2_7:
462 link_cfg->link_bw = SOR_LINK_SPEED_G1_62;
463 break;
464 case SOR_LINK_SPEED_G5_4:
465 if (link_cfg->lane_count == 1) {
466 link_cfg->link_bw = SOR_LINK_SPEED_G2_7;
467 link_cfg->lane_count = link_cfg->max_lane_count;
468 } else
469 link_cfg->lane_count /= 2;
470 break;
471 default:
472 printk(BIOS_ERR, "dp: Error link rate %d\n", link_cfg->link_bw);
473 return DP_LT_FAILED;
474 }
475
476 return (link_cfg->lane_count > 0) ? DP_LT_SUCCESS : DP_LT_FAILED;
477 }
478
479 /* Calculate if given cfg can meet the mode request. */
480 /* Return true if mode is possible, false otherwise. */
481 static int tegra_dc_dp_calc_config(struct tegra_dc_dp_data *dp,
482 const struct soc_nvidia_tegra210_config *config,
483 struct tegra_dc_dp_link_config *link_cfg)
484 {
485 const u32 link_rate = 27 * link_cfg->link_bw * 1000 * 1000;
486 const u64 f = 100000; /* precision factor */
487
488 u32 num_linkclk_line; /* Number of link clocks per line */
489 u64 ratio_f; /* Ratio of incoming to outgoing data rate */
490
491 u64 frac_f;
492 u64 activesym_f; /* Activesym per TU */
493 u64 activecount_f;
494 u32 activecount;
495 u32 activepolarity;
496 u64 approx_value_f;
497 u32 activefrac = 0;
498 u64 accumulated_error_f = 0;
499 u32 lowest_neg_activecount = 0;
500 u32 lowest_neg_activepolarity = 0;
501 u32 lowest_neg_tusize = 64;
502 u32 num_symbols_per_line;
503 u64 lowest_neg_activefrac = 0;
504 u64 lowest_neg_error_f = 64 * f;
505 u64 watermark_f;
506
507 int i;
508 int neg;
509
510 printk(BIOS_INFO, "dp: %s\n", __func__);
511
512 if (!link_rate || !link_cfg->lane_count || !config->pixel_clock ||
513 !link_cfg->bits_per_pixel)
514 return -1;
515
516 if ((u64)config->pixel_clock * link_cfg->bits_per_pixel >=
517 (u64)link_rate * 8 * link_cfg->lane_count)
518 return -1;
519
520 num_linkclk_line = (u32)((u64)link_rate * (u64)config->xres /
521 config->pixel_clock);
522
523 ratio_f = (u64)config->pixel_clock * link_cfg->bits_per_pixel * f;
524 ratio_f /= 8;
525 ratio_f = (u64)(ratio_f / (link_rate * link_cfg->lane_count));
526
527 for (i = 64; i >= 32; --i) {
528 activesym_f = ratio_f * i;
529 activecount_f = (u64)(activesym_f / (u32)f) * f;
530 frac_f = activesym_f - activecount_f;
531 activecount = (u32)((u64)(activecount_f / (u32)f));
532
533 if (frac_f < (f / 2)) /* fraction < 0.5 */
534 activepolarity = 0;
535 else {
536 activepolarity = 1;
537 frac_f = f - frac_f;
538 }
539
540 if (frac_f != 0) {
541 frac_f = (u64)((f * f) / frac_f); /* 1/fraction */
542 if (frac_f > (15 * f))
543 activefrac = activepolarity ? 1 : 15;
544 else
545 activefrac = activepolarity ?
546 (u32)((u64)(frac_f / (u32)f)) + 1 :
547 (u32)((u64)(frac_f / (u32)f));
548 }
549
550 if (activefrac == 1)
551 activepolarity = 0;
552
553 if (activepolarity == 1)
554 approx_value_f = activefrac ? (u64)(
555 (activecount_f + (activefrac * f - f) * f) /
556 (activefrac * f)) :
557 activecount_f + f;
558 else
559 approx_value_f = activefrac ?
560 activecount_f + (u64)(f / activefrac) :
561 activecount_f;
562
563 if (activesym_f < approx_value_f) {
564 accumulated_error_f = num_linkclk_line *
565 (u64)((approx_value_f - activesym_f) / i);
566 neg = 1;
567 } else {
568 accumulated_error_f = num_linkclk_line *
569 (u64)((activesym_f - approx_value_f) / i);
570 neg = 0;
571 }
572
573 if ((neg && (lowest_neg_error_f > accumulated_error_f)) ||
574 (accumulated_error_f == 0)) {
575 lowest_neg_error_f = accumulated_error_f;
576 lowest_neg_tusize = i;
577 lowest_neg_activecount = activecount;
578 lowest_neg_activepolarity = activepolarity;
579 lowest_neg_activefrac = activefrac;
580
581 if (accumulated_error_f == 0)
582 break;
583 }
584 }
585
586 if (lowest_neg_activefrac == 0) {
587 link_cfg->activepolarity = 0;
588 link_cfg->active_count = lowest_neg_activepolarity ?
589 lowest_neg_activecount : lowest_neg_activecount - 1;
590 link_cfg->tu_size = lowest_neg_tusize;
591 link_cfg->active_frac = 1;
592 } else {
593 link_cfg->activepolarity = lowest_neg_activepolarity;
594 link_cfg->active_count = (u32)lowest_neg_activecount;
595 link_cfg->tu_size = lowest_neg_tusize;
596 link_cfg->active_frac = (u32)lowest_neg_activefrac;
597 }
598
599 watermark_f = (u64)((ratio_f * link_cfg->tu_size * (f - ratio_f)) / f);
600 link_cfg->watermark = (u32)((u64)((watermark_f + lowest_neg_error_f) /
601 f)) + link_cfg->bits_per_pixel / 4 - 1;
602 num_symbols_per_line = (config->xres * link_cfg->bits_per_pixel) /
603 (8 * link_cfg->lane_count);
604
605 if (link_cfg->watermark > 30) {
606 printk(BIOS_INFO,
607 "dp: sor setting: unable to get a good tusize, "
608 "force watermark to 30.\n");
609 link_cfg->watermark = 30;
610 return -1;
611 } else if (link_cfg->watermark > num_symbols_per_line) {
612 printk(BIOS_INFO,
613 "dp: sor setting: force watermark to the number "
614 "of symbols in the line.\n");
615 link_cfg->watermark = num_symbols_per_line;
616 return -1;
617 }
618
619 /* Refer to dev_disp.ref for more information. */
620 /* # symbols/hblank = ((SetRasterBlankEnd.X + SetRasterSize.Width - */
621 /* SetRasterBlankStart.X - 7) * link_clk / pclk) */
622 /* - 3 * enhanced_framing - Y */
623 /* where Y = (# lanes == 4) 3 : (# lanes == 2) ? 6 : 12 */
624 link_cfg->hblank_sym = (int)((u64)(((u64)(config->hback_porch +
625 config->hfront_porch + config->hsync_width - 7) *
626 link_rate) / config->pixel_clock)) -
627 3 * link_cfg->enhanced_framing -
628 (12 / link_cfg->lane_count);
629
630 if (link_cfg->hblank_sym < 0)
631 link_cfg->hblank_sym = 0;
632
633 /* Refer to dev_disp.ref for more information. */
634 /* # symbols/vblank = ((SetRasterBlankStart.X - */
635 /* SetRasterBlankEen.X - 25) * link_clk / pclk) */
636 /* - Y - 1; */
637 /* where Y = (# lanes == 4) 12 : (# lanes == 2) ? 21 : 39 */
638 link_cfg->vblank_sym = (int)((u64)((u64)(config->xres - 25)
639 * link_rate / config->pixel_clock)) - (36 /
640 link_cfg->lane_count) - 4;
641
642 if (link_cfg->vblank_sym < 0)
643 link_cfg->vblank_sym = 0;
644
645 link_cfg->is_valid = 1;
646 tegra_dc_dp_dump_link_cfg(dp, link_cfg);
647
648 return 0;
649 }
650
651 static int tegra_dc_dp_init_max_link_cfg(
652 struct soc_nvidia_tegra210_config *config,
653 struct tegra_dc_dp_data *dp,
654 struct tegra_dc_dp_link_config *link_cfg)
655 {
656 u8 dpcd_data;
657 int ret;
658
659 printk(BIOS_INFO, "dp: %s\n", __func__);
660
661 CHECK_RET(tegra_dc_dp_dpcd_read(dp, NV_DPCD_MAX_LANE_COUNT,
662 &dpcd_data));
663 link_cfg->max_lane_count = dpcd_data & NV_DPCD_MAX_LANE_COUNT_MASK;
664 link_cfg->tps3_supported = (dpcd_data &
665 NV_DPCD_MAX_LANE_COUNT_TPS3_SUPPORTED_YES) ? 1 : 0;
666
667 link_cfg->support_enhanced_framing =
668 (dpcd_data & NV_DPCD_MAX_LANE_COUNT_ENHANCED_FRAMING_YES) ?
669 1 : 0;
670
671 CHECK_RET(tegra_dc_dp_dpcd_read(dp, NV_DPCD_MAX_DOWNSPREAD,
672 &dpcd_data));
673 link_cfg->downspread = (dpcd_data & NV_DPCD_MAX_DOWNSPREAD_VAL_0_5_PCT)
674 ? 1 : 0;
675
676 CHECK_RET(tegra_dc_dp_dpcd_read(dp, NV_DPCD_TRAINING_AUX_RD_INTERVAL,
677 &link_cfg->aux_rd_interval));
678
679 CHECK_RET(tegra_dc_dp_dpcd_read(dp, NV_DPCD_MAX_LINK_BANDWIDTH,
680 &link_cfg->max_link_bw));
681
682 link_cfg->bits_per_pixel = config->panel_bits_per_pixel;
683
684 /*
685 * Set to a high value for link training and attach.
686 * Will be re-programmed when dp is enabled.
687 */
688 link_cfg->drive_current = config->dp.drive_current;
689 link_cfg->preemphasis = config->dp.preemphasis;
690 link_cfg->postcursor = config->dp.postcursor;
691
692 CHECK_RET(tegra_dc_dp_dpcd_read(dp, NV_DPCD_EDP_CONFIG_CAP,
693 &dpcd_data));
694 link_cfg->alt_scramber_reset_cap =
695 (dpcd_data & NV_DPCD_EDP_CONFIG_CAP_ASC_RESET_YES) ?
696 1 : 0;
697 link_cfg->only_enhanced_framing =
698 (dpcd_data & NV_DPCD_EDP_CONFIG_CAP_FRAMING_CHANGE_YES) ?
699 1 : 0;
700
701 link_cfg->lane_count = link_cfg->max_lane_count;
702 link_cfg->link_bw = link_cfg->max_link_bw;
703 link_cfg->enhanced_framing = link_cfg->support_enhanced_framing;
704
705 tegra_dc_dp_calc_config(dp, config, link_cfg);
706 return 0;
707 }
708
709 static int tegra_dc_dp_set_assr(struct tegra_dc_dp_data *dp, int ena)
710 {
711 int ret;
712
713 u8 dpcd_data = ena ?
714 NV_DPCD_EDP_CONFIG_SET_ASC_RESET_ENABLE :
715 NV_DPCD_EDP_CONFIG_SET_ASC_RESET_DISABLE;
716
717 CHECK_RET(tegra_dc_dp_dpcd_write(dp, NV_DPCD_EDP_CONFIG_SET,
718 dpcd_data));
719
720 /* Also reset the scrambler to 0xfffe */
721 tegra_dc_sor_set_internal_panel(&dp->sor, ena);
722 return 0;
723 }
724
725 static int tegra_dp_set_link_bandwidth(struct tegra_dc_dp_data *dp, u8 link_bw)
726 {
727 tegra_dc_sor_set_link_bandwidth(&dp->sor, link_bw);
728
729 /* Sink side */
730 return tegra_dc_dp_dpcd_write(dp, NV_DPCD_LINK_BANDWIDTH_SET, link_bw);
731 }
732
733 static int tegra_dp_set_lane_count(struct tegra_dc_dp_data *dp,
734 const struct tegra_dc_dp_link_config *link_cfg)
735 {
736 u8 dpcd_data;
737 int ret;
738
739 /* check if panel support enhanched_framing */
740 dpcd_data = link_cfg->lane_count;
741 if (link_cfg->enhanced_framing)
742 dpcd_data |= NV_DPCD_LANE_COUNT_SET_ENHANCEDFRAMING_T;
743 CHECK_RET(tegra_dc_dp_dpcd_write(dp, NV_DPCD_LANE_COUNT_SET,
744 dpcd_data));
745
746 tegra_dc_sor_set_lane_count(&dp->sor, link_cfg->lane_count);
747
748 /* Also power down lanes that will not be used */
749 return 0;
750 }
751
752 #if DO_FAST_LINK_TRAINING
753 static int tegra_dc_dp_link_trained(struct tegra_dc_dp_data *dp,
754 const struct tegra_dc_dp_link_config *link_cfg)
755 {
756 u32 lane;
757 u8 mask;
758 u8 data;
759 int ret;
760
761 for (lane = 0; lane < link_cfg->lane_count; ++lane) {
762 CHECK_RET(tegra_dc_dp_dpcd_read(dp, (lane/2) ?
763 NV_DPCD_LANE2_3_STATUS : NV_DPCD_LANE0_1_STATUS,
764 &data));
765 mask = (lane & 1) ?
766 NV_DPCD_STATUS_LANEXPLUS1_CR_DONE_YES |
767 NV_DPCD_STATUS_LANEXPLUS1_CHN_EQ_DONE_YES |
768 NV_DPCD_STATUS_LANEXPLUS1_SYMBOL_LOCKED_YES :
769 NV_DPCD_STATUS_LANEX_CR_DONE_YES |
770 NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_YES |
771 NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_YES;
772 if ((data & mask) != mask)
773 return -1;
774 }
775 return 0;
776 }
777 #endif /* DO_FAST_LINK_TRAINING */
778
779 static int tegra_dp_channel_eq_status(struct tegra_dc_dp_data *dp)
780 {
781 u32 cnt;
782 u32 n_lanes = dp->link_cfg.lane_count;
783 u8 data;
784 u8 ce_done = 1;
785
786 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
787 tegra_dc_dp_dpcd_read(dp, (NV_DPCD_LANE0_1_STATUS + cnt),
788 &data);
789
790 if (n_lanes == 1) {
791 ce_done =
792 (data & (0x1 << NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) &&
793 (data & (0x1 << NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT));
794 break;
795 } else
796 if (!(data & (0x1 << NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) ||
797 !(data & (0x1 << NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT)) ||
798 !(data & (0x1 << NV_DPCD_STATUS_LANEXPLUS1_CHN_EQ_DONE_SHIFT)) ||
799 !(data & (0x1 << NV_DPCD_STATUS_LANEXPLUS1_SYMBOL_LOCKED_SHIFT)))
800 return 0;
801 }
802
803 if (ce_done) {
804 tegra_dc_dp_dpcd_read(dp, NV_DPCD_LANE_ALIGN_STATUS_UPDATED,
805 &data);
806 if (!(data & NV_DPCD_LANE_ALIGN_STATUS_UPDATED_DONE_YES))
807 ce_done = 0;
808 }
809
810 return ce_done;
811 }
812
813 static u8 tegra_dp_clock_recovery_status(struct tegra_dc_dp_data *dp)
814 {
815 u32 cnt;
816 u32 n_lanes = dp->link_cfg.lane_count;
817 u8 data_ptr;
818
819 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
820 tegra_dc_dp_dpcd_read(dp,
821 (NV_DPCD_LANE0_1_STATUS + cnt), &data_ptr);
822
823 if (n_lanes == 1)
824 return (data_ptr & NV_DPCD_STATUS_LANEX_CR_DONE_YES) ? 1 : 0;
825 else if (!(data_ptr & NV_DPCD_STATUS_LANEX_CR_DONE_YES) ||
826 !(data_ptr & (NV_DPCD_STATUS_LANEXPLUS1_CR_DONE_YES)))
827 return 0;
828 }
829
830 return 1;
831 }
832
833 static void tegra_dp_lt_adjust(struct tegra_dc_dp_data *dp,
834 u32 pe[4], u32 vs[4], u32 pc[4],
835 u8 pc_supported)
836 {
837 size_t cnt;
838 u8 data_ptr;
839 u32 n_lanes = dp->link_cfg.lane_count;
840
841 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
842 tegra_dc_dp_dpcd_read(dp,
843 (NV_DPCD_LANE0_1_ADJUST_REQ + cnt), &data_ptr);
844 pe[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_PE_MASK) >>
845 NV_DPCD_ADJUST_REQ_LANEX_PE_SHIFT;
846 vs[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_DC_MASK) >>
847 NV_DPCD_ADJUST_REQ_LANEX_DC_SHIFT;
848 pe[1 + 2 * cnt] =
849 (data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_MASK) >>
850 NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_SHIFT;
851 vs[1 + 2 * cnt] =
852 (data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_MASK) >>
853 NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_SHIFT;
854 }
855 if (pc_supported) {
856 tegra_dc_dp_dpcd_read(dp,
857 NV_DPCD_ADJUST_REQ_POST_CURSOR2, &data_ptr);
858 for (cnt = 0; cnt < n_lanes; cnt++) {
859 pc[cnt] = (data_ptr >>
860 NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_SHIFT(cnt)) &
861 NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_MASK;
862 }
863 }
864 }
865
866 static inline u32 tegra_dp_wait_aux_training(struct tegra_dc_dp_data *dp,
867 u8 is_clk_recovery)
868 {
869 if (!dp->link_cfg.aux_rd_interval)
870 is_clk_recovery ? udelay(200) :
871 udelay(500);
872 else
873 mdelay(dp->link_cfg.aux_rd_interval * 4);
874
875 return dp->link_cfg.aux_rd_interval;
876 }
877
878 static void tegra_dp_tpg(struct tegra_dc_dp_data *dp, u32 tp, u32 n_lanes)
879 {
880 u8 data = (tp == training_pattern_disabled)
881 ? (tp | NV_DPCD_TRAINING_PATTERN_SET_SC_DISABLED_F)
882 : (tp | NV_DPCD_TRAINING_PATTERN_SET_SC_DISABLED_T);
883
884 tegra_dc_sor_set_dp_linkctl(&dp->sor, 1, tp, &dp->link_cfg);
885 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_PATTERN_SET, data);
886 }
887
888 static int tegra_dp_link_config(struct tegra_dc_dp_data *dp,
889 const struct tegra_dc_dp_link_config *link_cfg)
890 {
891 u8 dpcd_data;
892 u32 retry;
893
894 if (link_cfg->lane_count == 0) {
895 printk(BIOS_ERR, "dp: error: lane count is 0. "
896 "Can not set link config.\n");
897 return DP_LT_FAILED;
898 }
899
900 /* Set power state if it is not in normal level */
901 if (tegra_dc_dp_dpcd_read(dp, NV_DPCD_SET_POWER, &dpcd_data))
902 return DP_LT_FAILED;
903
904 if (dpcd_data == NV_DPCD_SET_POWER_VAL_D3_PWRDWN) {
905 dpcd_data = NV_DPCD_SET_POWER_VAL_D0_NORMAL;
906
907 /* DP spec requires 3 retries */
908 for (retry = 3; retry > 0; --retry) {
909 if (tegra_dc_dp_dpcd_write(dp, NV_DPCD_SET_POWER,
910 dpcd_data))
911 break;
912 if (retry == 1) {
913 printk(BIOS_ERR, "dp: Failed to set DP panel"
914 " power\n");
915 return DP_LT_FAILED;
916 }
917 }
918 }
919
920 /* Enable ASSR if possible */
921 if (link_cfg->alt_scramber_reset_cap)
922 if (tegra_dc_dp_set_assr(dp, 1))
923 return DP_LT_FAILED;
924
925 if (tegra_dp_set_link_bandwidth(dp, link_cfg->link_bw)) {
926 printk(BIOS_ERR, "dp: Failed to set link bandwidth\n");
927 return DP_LT_FAILED;
928 }
929 if (tegra_dp_set_lane_count(dp, link_cfg)) {
930 printk(BIOS_ERR, "dp: Failed to set lane count\n");
931 return DP_LT_FAILED;
932 }
933 tegra_dc_sor_set_dp_linkctl(&dp->sor, 1, training_pattern_none,
934 link_cfg);
935 return DP_LT_SUCCESS;
936 }
937
938 static int tegra_dp_lower_link_config(struct tegra_dc_dp_data *dp,
939 struct tegra_dc_dp_link_config *cfg)
940 {
941 struct tegra_dc_dp_link_config tmp_cfg;
942
943 tmp_cfg = dp->link_cfg;
944 cfg->is_valid = 0;
945
946 if (_tegra_dp_lower_link_config(dp, cfg))
947 goto fail;
948
949 if (tegra_dc_dp_calc_config(dp, dp->dc->config, cfg))
950 goto fail;
951 tegra_dp_link_config(dp, cfg);
952
953 return DP_LT_SUCCESS;
954 fail:
955 dp->link_cfg = tmp_cfg;
956 tegra_dp_link_config(dp, &tmp_cfg);
957 return DP_LT_FAILED;
958 }
959
960 static void tegra_dp_lt_config(struct tegra_dc_dp_data *dp,
961 u32 pe[4], u32 vs[4], u32 pc[4])
962 {
963 struct tegra_dc_sor_data *sor = &dp->sor;
964 u32 n_lanes = dp->link_cfg.lane_count;
965 u8 pc_supported = dp->link_cfg.tps3_supported;
966 u32 cnt;
967 u32 val;
968
969 for (cnt = 0; cnt < n_lanes; cnt++) {
970 u32 mask = 0;
971 u32 pe_reg, vs_reg, pc_reg;
972 u32 shift = 0;
973
974 switch (cnt) {
975 case 0:
976 mask = NV_SOR_PR_LANE2_DP_LANE0_MASK;
977 shift = NV_SOR_PR_LANE2_DP_LANE0_SHIFT;
978 break;
979 case 1:
980 mask = NV_SOR_PR_LANE1_DP_LANE1_MASK;
981 shift = NV_SOR_PR_LANE1_DP_LANE1_SHIFT;
982 break;
983 case 2:
984 mask = NV_SOR_PR_LANE0_DP_LANE2_MASK;
985 shift = NV_SOR_PR_LANE0_DP_LANE2_SHIFT;
986 break;
987 case 3:
988 mask = NV_SOR_PR_LANE3_DP_LANE3_MASK;
989 shift = NV_SOR_PR_LANE3_DP_LANE3_SHIFT;
990 break;
991 default:
992 printk(BIOS_ERR,
993 "dp: incorrect lane cnt\n");
994 }
995
996 pe_reg = tegra_dp_pe_regs[pc[cnt]][vs[cnt]][pe[cnt]];
997 vs_reg = tegra_dp_vs_regs[pc[cnt]][vs[cnt]][pe[cnt]];
998 pc_reg = tegra_dp_pc_regs[pc[cnt]][vs[cnt]][pe[cnt]];
999
1000 tegra_dp_set_pe_vs_pc(sor, mask, pe_reg << shift,
1001 vs_reg << shift, pc_reg << shift, pc_supported);
1002 }
1003
1004 tegra_dp_disable_tx_pu(&dp->sor);
1005 udelay(20);
1006
1007 for (cnt = 0; cnt < n_lanes; cnt++) {
1008 u32 max_vs_flag = tegra_dp_is_max_vs(pe[cnt], vs[cnt]);
1009 u32 max_pe_flag = tegra_dp_is_max_pe(pe[cnt], vs[cnt]);
1010
1011 val = (vs[cnt] << NV_DPCD_TRAINING_LANEX_SET_DC_SHIFT) |
1012 (max_vs_flag ?
1013 NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_T :
1014 NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_F) |
1015 (pe[cnt] << NV_DPCD_TRAINING_LANEX_SET_PE_SHIFT) |
1016 (max_pe_flag ?
1017 NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_T :
1018 NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_F);
1019 tegra_dc_dp_dpcd_write(dp,
1020 (NV_DPCD_TRAINING_LANE0_SET + cnt), val);
1021 }
1022
1023 if (pc_supported) {
1024 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
1025 u32 max_pc_flag0 = tegra_dp_is_max_pc(pc[cnt]);
1026 u32 max_pc_flag1 = tegra_dp_is_max_pc(pc[cnt + 1]);
1027 val = (pc[cnt] << NV_DPCD_LANEX_SET2_PC2_SHIFT) |
1028 (max_pc_flag0 ?
1029 NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_T :
1030 NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_F) |
1031 (pc[cnt + 1] <<
1032 NV_DPCD_LANEXPLUS1_SET2_PC2_SHIFT) |
1033 (max_pc_flag1 ?
1034 NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_T :
1035 NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_F);
1036 tegra_dc_dp_dpcd_write(dp,
1037 (NV_DPCD_TRAINING_LANE0_1_SET2 + cnt), val);
1038 }
1039 }
1040 }
1041
1042 static int _tegra_dp_channel_eq(struct tegra_dc_dp_data *dp, u32 pe[4],
1043 u32 vs[4], u32 pc[4], u8 pc_supported,
1044 u32 n_lanes)
1045 {
1046 u32 retry_cnt;
1047
1048 for (retry_cnt = 0; retry_cnt < 4; retry_cnt++) {
1049 if (retry_cnt) {
1050 tegra_dp_lt_adjust(dp, pe, vs, pc, pc_supported);
1051 tegra_dp_lt_config(dp, pe, vs, pc);
1052 }
1053
1054 tegra_dp_wait_aux_training(dp, 0);
1055
1056 if (!tegra_dp_clock_recovery_status(dp)) {
1057 printk(BIOS_ERR, "dp: CR failed in channel EQ"
1058 " sequence!\n");
1059 break;
1060 }
1061
1062 if (tegra_dp_channel_eq_status(dp))
1063 return DP_LT_SUCCESS;
1064 }
1065
1066 return DP_LT_FAILED;
1067 }
1068
1069 static int tegra_dp_channel_eq(struct tegra_dc_dp_data *dp,
1070 u32 pe[4], u32 vs[4], u32 pc[4])
1071 {
1072 u32 n_lanes = dp->link_cfg.lane_count;
1073 u8 pc_supported = dp->link_cfg.tps3_supported;
1074 int err;
1075 u32 tp_src = training_pattern_2;
1076
1077 if (pc_supported)
1078 tp_src = training_pattern_3;
1079
1080 tegra_dp_tpg(dp, tp_src, n_lanes);
1081
1082 err = _tegra_dp_channel_eq(dp, pe, vs, pc, pc_supported, n_lanes);
1083
1084 tegra_dp_tpg(dp, training_pattern_disabled, n_lanes);
1085
1086 return err;
1087 }
1088
1089 static int _tegra_dp_clk_recovery(struct tegra_dc_dp_data *dp, u32 pe[4],
1090 u32 vs[4], u32 pc[4], u8 pc_supported,
1091 u32 n_lanes)
1092 {
1093 u32 vs_temp[4];
1094 u32 retry_cnt = 0;
1095
1096 do {
1097 tegra_dp_lt_config(dp, pe, vs, pc);
1098 tegra_dp_wait_aux_training(dp, 1);
1099
1100 if (tegra_dp_clock_recovery_status(dp))
1101 return DP_LT_SUCCESS;
1102
1103 memcpy(vs_temp, vs, sizeof(vs_temp));
1104 tegra_dp_lt_adjust(dp, pe, vs, pc, pc_supported);
1105
1106 if (memcmp(vs_temp, vs, sizeof(vs_temp)))
1107 retry_cnt = 0;
1108 else
1109 ++retry_cnt;
1110 } while (retry_cnt < 5);
1111
1112 return DP_LT_FAILED;
1113 }
1114
1115 static int tegra_dp_clk_recovery(struct tegra_dc_dp_data *dp,
1116 u32 pe[4], u32 vs[4], u32 pc[4])
1117 {
1118 u32 n_lanes = dp->link_cfg.lane_count;
1119 u8 pc_supported = dp->link_cfg.tps3_supported;
1120 int err;
1121
1122 tegra_dp_tpg(dp, training_pattern_1, n_lanes);
1123
1124 err = _tegra_dp_clk_recovery(dp, pe, vs, pc, pc_supported, n_lanes);
1125 if (err < 0)
1126 tegra_dp_tpg(dp, training_pattern_disabled, n_lanes);
1127
1128 return err;
1129 }
1130
1131 static int tegra_dc_dp_full_link_training(struct tegra_dc_dp_data *dp)
1132 {
1133 struct tegra_dc_sor_data *sor = &dp->sor;
1134 int err;
1135 u32 pe[4], vs[4], pc[4];
1136
1137 printk(BIOS_INFO, "dp: %s\n", __func__);
1138 tegra_sor_precharge_lanes(sor);
1139
1140 retry_cr:
1141 memset(pe, preEmphasis_Disabled, sizeof(pe));
1142 memset(vs, driveCurrent_Level0, sizeof(vs));
1143 memset(pc, postCursor2_Level0, sizeof(pc));
1144
1145 err = tegra_dp_clk_recovery(dp, pe, vs, pc);
1146 if (err != DP_LT_SUCCESS) {
1147 if (!tegra_dp_lower_link_config(dp, &dp->link_cfg))
1148 goto retry_cr;
1149
1150 printk(BIOS_ERR, "dp: clk recovery failed\n");
1151 goto fail;
1152 }
1153
1154 err = tegra_dp_channel_eq(dp, pe, vs, pc);
1155 if (err != DP_LT_SUCCESS) {
1156 if (!tegra_dp_lower_link_config(dp, &dp->link_cfg))
1157 goto retry_cr;
1158
1159 printk(BIOS_ERR,
1160 "dp: channel equalization failed\n");
1161 goto fail;
1162 }
1163
1164 tegra_dc_dp_dump_link_cfg(dp, &dp->link_cfg);
1165
1166 return 0;
1167
1168 fail:
1169 return err;
1170 }
1171
1172 /*
1173 * All link training functions are ported from kernel dc driver.
1174 * See more details at drivers/video/tegra/dc/dp.c
1175 */
1176 #if DO_FAST_LINK_TRAINING
1177 static int tegra_dc_dp_fast_link_training(struct tegra_dc_dp_data *dp,
1178 const struct tegra_dc_dp_link_config *link_cfg)
1179 {
1180 struct tegra_dc_sor_data *sor = &dp->sor;
1181 u8 link_bw;
1182 u8 lane_count;
1183 u16 data16;
1184 u32 data32;
1185 u32 size;
1186 u32 status;
1187 int j;
1188 u32 mask = 0xffff >> ((4 - link_cfg->lane_count) * 4);
1189
1190 printk(BIOS_INFO, "dp: %s\n", __func__);
1191
1192 tegra_dc_sor_set_lane_parm(sor, link_cfg);
1193 tegra_dc_dp_dpcd_write(dp, NV_DPCD_MAIN_LINK_CHANNEL_CODING_SET,
1194 NV_DPCD_MAIN_LINK_CHANNEL_CODING_SET_ANSI_8B10B);
1195
1196 /* Send TP1 */
1197 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_1, link_cfg);
1198 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_PATTERN_SET,
1199 NV_DPCD_TRAINING_PATTERN_SET_TPS_TP1);
1200
1201 for (j = 0; j < link_cfg->lane_count; ++j)
1202 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_LANE0_SET + j,
1203 0x24);
1204 udelay(520);
1205
1206 size = sizeof(data16);
1207 tegra_dc_dpaux_read(dp, DPAUX_DP_AUXCTL_CMD_AUXRD,
1208 NV_DPCD_LANE0_1_STATUS, (u8 *)&data16, &size, &status);
1209 status = mask & 0x1111;
1210 if ((data16 & status) != status) {
1211 printk(BIOS_ERR,
1212 "dp: Link training error for TP1 (%#x)\n", data16);
1213 return -EFAULT;
1214 }
1215
1216 /* enable ASSR */
1217 tegra_dc_dp_set_assr(dp, link_cfg->scramble_ena);
1218 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_3, link_cfg);
1219
1220 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_PATTERN_SET,
1221 link_cfg->link_bw == 20 ? 0x23 : 0x22);
1222 for (j = 0; j < link_cfg->lane_count; ++j)
1223 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_LANE0_SET + j,
1224 0x24);
1225 udelay(520);
1226
1227 size = sizeof(data32);
1228 tegra_dc_dpaux_read(dp, DPAUX_DP_AUXCTL_CMD_AUXRD,
1229 NV_DPCD_LANE0_1_STATUS, (u8 *)&data32, &size, &status);
1230 if ((data32 & mask) != (0x7777 & mask)) {
1231 printk(BIOS_ERR,
1232 "dp: Link training error for TP2/3 (0x%x)\n", data32);
1233 return -EFAULT;
1234 }
1235
1236 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_disabled,
1237 link_cfg);
1238 tegra_dc_dp_dpcd_write(dp, NV_DPCD_TRAINING_PATTERN_SET, 0);
1239
1240 if (tegra_dc_dp_link_trained(dp, link_cfg)) {
1241 tegra_dc_sor_read_link_config(&dp->sor, &link_bw,
1242 &lane_count);
1243 printk(BIOS_ERR,
1244 "Fast link trainging failed, link bw %d, lane # %d\n",
1245 link_bw, lane_count);
1246 return -EFAULT;
1247 }
1248
1249 printk(BIOS_INFO,
1250 "Fast link trainging succeeded, link bw %d, lane %d\n",
1251 link_cfg->link_bw, link_cfg->lane_count);
1252
1253 return 0;
1254 }
1255 #endif /* DO_FAST_LINK_TRAINING */
1256
1257 static int tegra_dp_do_link_training(struct tegra_dc_dp_data *dp,
1258 const struct tegra_dc_dp_link_config *link_cfg)
1259 {
1260 u8 link_bw;
1261 u8 lane_count;
1262 #if DO_FAST_LINK_TRAINING
1263 int ret;
1264
1265 /* Now do the fast link training for eDP */
1266 ret = tegra_dc_dp_fast_link_training(dp, link_cfg);
1267 if (ret) {
1268 printk(BIOS_ERR, "dp: fast link training failed\n");
1269
1270 /* Try full link training then */
1271 if (tegra_dc_dp_full_link_training(dp)) {
1272 printk(BIOS_ERR, "dp: full link training failed\n");
1273 return ret;
1274 }
1275 } else {
1276 /* set to a known-good drive setting if fast link succeeded */
1277 tegra_dc_sor_set_voltage_swing(&dp->sor);
1278 }
1279 #else
1280 if (tegra_dc_dp_full_link_training(dp)) {
1281 printk(BIOS_ERR, "dp: full link training failed\n");
1282 return -EFAULT;
1283 }
1284 #endif
1285
1286 /* Everything goes well, double check the link config */
1287 /* TODO: record edc/c2 data for debugging */
1288 tegra_dc_sor_read_link_config(&dp->sor, &link_bw, &lane_count);
1289
1290 if ((link_cfg->link_bw == link_bw) &&
1291 (link_cfg->lane_count == lane_count))
1292 return 0;
1293 else
1294 return -EFAULT;
1295 }
1296
1297 static int tegra_dc_dp_explore_link_cfg(struct tegra_dc_dp_data *dp,
1298 struct tegra_dc_dp_link_config *link_cfg,
1299 const struct soc_nvidia_tegra210_config *config)
1300 {
1301 struct tegra_dc_dp_link_config temp_cfg;
1302
1303 if (!config->pixel_clock || !config->xres || !config->yres) {
1304 printk(BIOS_ERR,
1305 "dp: error mode configuration");
1306 return -EINVAL;
1307 }
1308 if (!link_cfg->max_link_bw || !link_cfg->max_lane_count) {
1309 printk(BIOS_ERR,
1310 "dp: error link configuration");
1311 return -EINVAL;
1312 }
1313
1314 link_cfg->is_valid = 0;
1315
1316 memcpy(&temp_cfg, link_cfg, sizeof(temp_cfg));
1317
1318 temp_cfg.link_bw = temp_cfg.max_link_bw;
1319 temp_cfg.lane_count = temp_cfg.max_lane_count;
1320
1321 /*
1322 * set to max link config
1323 */
1324 if ((!tegra_dc_dp_calc_config(dp, config, &temp_cfg)) &&
1325 (!tegra_dp_link_config(dp, &temp_cfg)) &&
1326 (!tegra_dp_do_link_training(dp, &temp_cfg)))
1327 /* the max link cfg is doable */
1328 memcpy(link_cfg, &temp_cfg, sizeof(temp_cfg));
1329
1330 return link_cfg->is_valid ? 0 : -EFAULT;
1331 }
1332
1333 static void tegra_dp_update_config(struct tegra_dc_dp_data *dp,
1334 struct soc_nvidia_tegra210_config *config)
1335 {
1336 struct edid edid;
1337 u8 buf[128] = {0};
1338 u32 size = sizeof(buf), aux_stat = 0;
1339
1340 printk(BIOS_ERR, "%s: enable r/w dump.\n",
1341 __func__);
1342
1343 tegra_dc_dpaux_enable(dp);
1344 if (tegra_dc_i2c_aux_read(dp, TEGRA_EDID_I2C_ADDRESS, 0, buf, &size,
1345 &aux_stat)) {
1346 printk(BIOS_ERR, "%s: Failed to read EDID. Use defaults.\n",
1347 __func__);
1348 return;
1349 }
1350
1351 if (decode_edid(buf, sizeof(buf), &edid) != EDID_CONFORMANT) {
1352 printk(BIOS_ERR, "%s: Failed to decode EDID. Use defaults.\n",
1353 __func__);
1354 return;
1355 }
1356
1357 config->xres = config->display_xres = edid.mode.ha;
1358 config->yres = config->display_yres = edid.mode.va;
1359
1360 config->pixel_clock = edid.mode.pixel_clock * 1000;
1361
1362 config->hfront_porch = edid.mode.hso;
1363 config->hsync_width = edid.mode.hspw;
1364 config->hback_porch = edid.mode.hbl - edid.mode.hso - edid.mode.hspw;
1365
1366 config->vfront_porch = edid.mode.vso;
1367 config->vsync_width = edid.mode.vspw;
1368 config->vback_porch = edid.mode.vbl - edid.mode.vso - edid.mode.vspw;
1369
1370 /**
1371 * Note edid->framebuffer_bits_per_pixel is currently hard-coded as 32,
1372 * so we should keep the default value in device config.
1373 *
1374 * EDID v1.3 panels may not have color depth info, so we need to check
1375 * if these values are zero before updating config.
1376 */
1377 if (edid.panel_bits_per_pixel)
1378 config->panel_bits_per_pixel = edid.panel_bits_per_pixel;
1379 if (edid.panel_bits_per_color)
1380 config->color_depth = edid.panel_bits_per_color;
1381 printk(BIOS_SPEW, "%s: configuration updated by EDID.\n", __func__);
1382 }
1383
1384 void dp_init(void *_config)
1385 {
1386 struct soc_nvidia_tegra210_config *config = (void *)_config;
1387 struct tegra_dc *dc = config->dc_data;
1388 struct tegra_dc_dp_data *dp = &dp_data;
1389
1390 /* set up links among config, dc, dp and sor */
1391 dp->dc = dc;
1392 dc->out = dp;
1393 dp->sor.dc = dc;
1394
1395 dp->sor.power_is_up = 0;
1396 dp->sor.base = (void *)TEGRA_ARM_SOR;
1397 dp->sor.pmc_base = (void *)TEGRA_PMC_BASE;
1398 dp->sor.portnum = 0;
1399 dp->sor.link_cfg = &dp->link_cfg;
1400 dp->aux_base = (void *)TEGRA_ARM_DPAUX;
1401 dp->link_cfg.is_valid = 0;
1402 dp->enabled = 0;
1403
1404 tegra_dp_update_config(dp, config);
1405 }
1406
1407 static void tegra_dp_hpd_config(struct tegra_dc_dp_data *dp,
1408 struct soc_nvidia_tegra210_config *config)
1409 {
1410 u32 val;
1411
1412 val = config->dp.hpd_plug_min_us |
1413 (config->dp.hpd_unplug_min_us <<
1414 DPAUX_HPD_CONFIG_UNPLUG_MIN_TIME_SHIFT);
1415 tegra_dpaux_writel(dp, DPAUX_HPD_CONFIG, val);
1416
1417 tegra_dpaux_writel(dp, DPAUX_HPD_IRQ_CONFIG, config->dp.hpd_irq_min_us);
1418 }
1419
1420 static int tegra_dp_hpd_plug(struct tegra_dc_dp_data *dp, int timeout_ms)
1421 {
1422 u32 val;
1423 u32 timeout = timeout_ms * 1000;
1424 do {
1425 val = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
1426 if (val & DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)
1427 return 0;
1428 udelay(100);
1429 timeout -= 100;
1430 } while (timeout > 0);
1431 return -1;
1432 }
1433
1434 static int tegra_dc_dp_sink_out_of_sync(struct tegra_dc_dp_data *dp,
1435 u32 delay_ms)
1436 {
1437 u8 dpcd_data;
1438 int out_of_sync;
1439
1440 mdelay(delay_ms);
1441 tegra_dc_dp_dpcd_read(dp, NV_DPCD_SINK_STATUS, &dpcd_data);
1442
1443 out_of_sync = ((dpcd_data & NV_DPCD_SINK_STATUS_PORT0_IN_SYNC) !=
1444 NV_DPCD_SINK_STATUS_PORT0_IN_SYNC);
1445
1446 if (out_of_sync)
1447 printk(BIOS_ERR,
1448 "SINK receive port 0 is out of synchronization\n");
1449 else
1450 printk(BIOS_INFO,
1451 "SINK is in synchronization\n");
1452
1453 return out_of_sync;
1454 }
1455
1456 static void tegra_dc_dp_check_sink(struct tegra_dc_dp_data *dp,
1457 struct soc_nvidia_tegra210_config *config)
1458 {
1459 u8 max_retry = 3;
1460 int delay_frame;
1461
1462 /* DP TCON may skip some main stream frames, thus we need to wait
1463 some delay before reading the DPCD SINK STATUS register, starting
1464 from 5 */
1465 delay_frame = 5;
1466
1467 while (tegra_dc_dp_sink_out_of_sync(dp, FRAME_IN_MS * delay_frame) &&
1468 max_retry--) {
1469 tegra_dc_detach(&dp->sor);
1470 if (tegra_dc_dp_explore_link_cfg(dp, &dp->link_cfg, config)) {
1471 printk(BIOS_ERR, "dp: %s: error to configure link\n",
1472 __func__);
1473 continue;
1474 }
1475
1476 tegra_dc_sor_set_power_state(&dp->sor, 1);
1477 tegra_dc_sor_attach(&dp->sor);
1478
1479 /* Increase delay_frame for next try in case the sink is
1480 skipping more frames */
1481 delay_frame += 10;
1482 }
1483 }
1484
1485 void dp_enable(void *_dp)
1486 {
1487 struct tegra_dc_dp_data *dp = _dp;
1488 struct tegra_dc *dc = dp->dc;
1489 struct soc_nvidia_tegra210_config *config = dc->config;
1490
1491 u8 data;
1492 u32 retry;
1493 int ret;
1494
1495 tegra_dc_dpaux_enable(dp);
1496
1497 tegra_dp_hpd_config(dp, config);
1498 if (tegra_dp_hpd_plug(dp, config->dp.vdd_to_hpd_delay_ms) < 0) {
1499 printk(BIOS_ERR, "dp: hpd plug failed\n");
1500 goto error_enable;
1501 }
1502
1503 if (tegra_dc_dp_init_max_link_cfg(config, dp, &dp->link_cfg)) {
1504 printk(BIOS_ERR, "dp: failed to init link configuration\n");
1505 goto error_enable;
1506 }
1507
1508 tegra_dc_sor_enable_dp(&dp->sor);
1509
1510 tegra_dc_sor_set_panel_power(&dp->sor, 1);
1511
1512 /* Write power on to DPCD */
1513 data = NV_DPCD_SET_POWER_VAL_D0_NORMAL;
1514 retry = 0;
1515 do {
1516 ret = tegra_dc_dp_dpcd_write(dp,
1517 NV_DPCD_SET_POWER, data);
1518 } while ((retry++ < DP_POWER_ON_MAX_TRIES) && ret);
1519
1520 if (ret || retry >= DP_POWER_ON_MAX_TRIES) {
1521 printk(BIOS_ERR,
1522 "dp: failed to power on panel (0x%x)\n", ret);
1523 goto error_enable;
1524 }
1525
1526 /* Confirm DP is plugging status */
1527 if (!(tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT) &
1528 DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)) {
1529 printk(BIOS_ERR, "dp: could not detect HPD\n");
1530 goto error_enable;
1531 }
1532
1533 /* Check DP version */
1534 if (tegra_dc_dp_dpcd_read(dp, NV_DPCD_REV, &dp->revision))
1535 printk(BIOS_ERR,
1536 "dp: failed to read the revision number from sink\n");
1537
1538 if (tegra_dc_dp_explore_link_cfg(dp, &dp->link_cfg, config)) {
1539 printk(BIOS_ERR, "dp: error to configure link\n");
1540 goto error_enable;
1541 }
1542
1543 tegra_dc_sor_set_power_state(&dp->sor, 1);
1544 tegra_dc_sor_attach(&dp->sor);
1545
1546 tegra_dc_dp_check_sink(dp, config);
1547
1548 /*
1549 * Power down the unused lanes to save power
1550 * (about hundreds milli-watts, varies from boards).
1551 */
1552 tegra_dc_sor_power_down_unused_lanes(&dp->sor);
1553
1554 dp->enabled = 1;
1555 error_enable:
1556 return;
1557 }
1558
1559 void dp_display_startup(struct device *dev)
1560 {
1561 struct soc_nvidia_tegra210_config *config = dev->chip_info;
1562 struct display_controller *disp_ctrl =
1563 (void *)config->display_controller;
1564
1565 u32 framebuffer_size_mb = config->framebuffer_size / MiB;
1566 u32 framebuffer_base_mb = config->framebuffer_base / MiB;
1567
1568 struct pwm_controller *pwm = (void *)TEGRA_PWM_BASE;
1569 struct tegra_dc *dc = &dc_data;
1570 u32 plld_rate;
1571
1572 printk(BIOS_INFO, "%s: entry: disp_ctrl: %p.\n",
1573 __func__, disp_ctrl);
1574
1575 if (disp_ctrl == NULL) {
1576 printk(BIOS_ERR, "No dc is assigned by dt.\n");
1577 return;
1578 }
1579
1580 dc->base = (void *)disp_ctrl;
1581 dc->config = config;
1582 config->dc_data = dc;
1583
1584 /* Note dp_init may read EDID and change some config values. */
1585 dp_init(config);
1586
1587 if (framebuffer_size_mb == 0) {
1588 framebuffer_size_mb = ALIGN_UP(config->display_xres *
1589 config->display_yres *
1590 (config->framebuffer_bits_per_pixel / 8), MiB)/MiB;
1591 }
1592
1593 config->framebuffer_size = framebuffer_size_mb * MiB;
1594 config->framebuffer_base = framebuffer_base_mb * MiB;
1595
1596 /* The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER
1597 * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the
1598 * update_display_mode() for detail.
1599 */
1600 plld_rate = clock_configure_plld(config->pixel_clock * 2);
1601 if (plld_rate == 0) {
1602 printk(BIOS_ERR, "dc: clock init failed\n");
1603 return;
1604 } else if (plld_rate != config->pixel_clock * 2) {
1605 printk(BIOS_WARNING, "dc: plld rounded to %u\n", plld_rate);
1606 config->pixel_clock = plld_rate / 2;
1607 }
1608
1609 /* set disp1's clock source to PLLD_OUT0 */
1610 clock_configure_source(disp1, PLLD, (plld_rate/KHz)/2);
1611
1612 /* Init dc */
1613 if (tegra_dc_init(disp_ctrl)) {
1614 printk(BIOS_ERR, "dc: init failed\n");
1615 return;
1616 }
1617
1618 /* Configure dc mode */
1619 if (update_display_mode(disp_ctrl, config)) {
1620 printk(BIOS_ERR, "dc: failed to configure display mode.\n");
1621 return;
1622 }
1623
1624 /* Enable dp */
1625 dp_enable(dc->out);
1626
1627 /* Set up Tegra PWM n (where n is specified in config->dp.pwm) to drive the
1628 * panel backlight.
1629 */
1630 printk(BIOS_SPEW, "%s: enable panel backlight pwm\n", __func__);
1631 WRITEL(((1 << NV_PWM_CSR_ENABLE_SHIFT) |
1632 (220 << NV_PWM_CSR_PULSE_WIDTH_SHIFT) | /* 220/256 */
1633 0x02e), /* frequency divider */
1634 &pwm->pwm[config->dp.pwm].csr);
1635
1636 /* Set up window */
1637 update_window(config);
1638 printk(BIOS_INFO, "%s: display init done.\n", __func__);
1639
1640 /* Save panel mode to cb tables */
1641 pass_mode_info_to_payload(config);
1642
1643 /*
1644 * After this point, it is payload's responsibility to allocate
1645 * framebuffer and sets the base address to dc's
1646 * WINBUF_START_ADDR register and enables window by setting dc's
1647 * DISP_DISP_WIN_OPTIONS register.
1648 */
1649 }