blob: b95b14da77d0f4d552c214e3a3f9f241f1cd4361 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glassf15fe612015-04-14 21:03:41 -06002/*
3 * Copyright (c) 2011-2013, NVIDIA Corporation.
4 * Copyright 2014 Google Inc.
Simon Glassf15fe612015-04-14 21:03:41 -06005 */
6
Simon Glass7d3d7762016-01-21 19:45:00 -07007#include <display.h>
Simon Glassf15fe612015-04-14 21:03:41 -06008#include <dm.h>
9#include <div64.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060012#include <time.h>
Simon Glassfad72182016-01-30 16:37:50 -070013#include <video_bridge.h>
Simon Glassf15fe612015-04-14 21:03:41 -060014#include <asm/io.h>
15#include <asm/arch-tegra/dc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Simon Glass7d3d7762016-01-21 19:45:00 -070017#include "display.h"
Simon Glassf15fe612015-04-14 21:03:41 -060018#include "edid.h"
19#include "sor.h"
Simon Glass7d3d7762016-01-21 19:45:00 -070020#include "displayport.h"
Simon Glassf15fe612015-04-14 21:03:41 -060021
Simon Glass662f2aa2015-04-14 21:03:44 -060022#define DO_FAST_LINK_TRAINING 1
23
Simon Glassf15fe612015-04-14 21:03:41 -060024struct tegra_dp_plat {
25 ulong base;
26};
27
Simon Glassfad72182016-01-30 16:37:50 -070028/**
29 * struct tegra_dp_priv - private displayport driver info
30 *
31 * @dc_dev: Display controller device that is sending the video feed
32 */
Simon Glassf15fe612015-04-14 21:03:41 -060033struct tegra_dp_priv {
Simon Glassfad72182016-01-30 16:37:50 -070034 struct udevice *sor;
35 struct udevice *dc_dev;
Simon Glassf15fe612015-04-14 21:03:41 -060036 struct dpaux_ctlr *regs;
Simon Glassf15fe612015-04-14 21:03:41 -060037 u8 revision;
38 int enabled;
39};
40
41struct tegra_dp_priv dp_data;
42
43static inline u32 tegra_dpaux_readl(struct tegra_dp_priv *dp, u32 reg)
44{
45 return readl((u32 *)dp->regs + reg);
46}
47
48static inline void tegra_dpaux_writel(struct tegra_dp_priv *dp, u32 reg,
49 u32 val)
50{
51 writel(val, (u32 *)dp->regs + reg);
52}
53
54static inline u32 tegra_dc_dpaux_poll_register(struct tegra_dp_priv *dp,
55 u32 reg, u32 mask, u32 exp_val,
56 u32 poll_interval_us,
57 u32 timeout_us)
58{
59 u32 reg_val = 0;
60 u32 temp = timeout_us;
61
62 do {
63 udelay(poll_interval_us);
64 reg_val = tegra_dpaux_readl(dp, reg);
65 if (timeout_us > poll_interval_us)
66 timeout_us -= poll_interval_us;
67 else
68 break;
69 } while ((reg_val & mask) != exp_val);
70
71 if ((reg_val & mask) == exp_val)
72 return 0; /* success */
73 debug("dpaux_poll_register 0x%x: timeout: (reg_val)0x%08x & (mask)0x%08x != (exp_val)0x%08x\n",
74 reg, reg_val, mask, exp_val);
75 return temp;
76}
77
78static inline int tegra_dpaux_wait_transaction(struct tegra_dp_priv *dp)
79{
80 /* According to DP spec, each aux transaction needs to finish
81 within 40ms. */
82 if (tegra_dc_dpaux_poll_register(dp, DPAUX_DP_AUXCTL,
83 DPAUX_DP_AUXCTL_TRANSACTREQ_MASK,
84 DPAUX_DP_AUXCTL_TRANSACTREQ_DONE,
85 100, DP_AUX_TIMEOUT_MS * 1000) != 0) {
86 debug("dp: DPAUX transaction timeout\n");
87 return -1;
88 }
89 return 0;
90}
91
92static int tegra_dc_dpaux_write_chunk(struct tegra_dp_priv *dp, u32 cmd,
93 u32 addr, u8 *data, u32 *size,
94 u32 *aux_stat)
95{
96 int i;
97 u32 reg_val;
98 u32 timeout_retries = DP_AUX_TIMEOUT_MAX_TRIES;
99 u32 defer_retries = DP_AUX_DEFER_MAX_TRIES;
100 u32 temp_data;
101
102 if (*size > DP_AUX_MAX_BYTES)
103 return -1; /* only write one chunk of data */
104
105 /* Make sure the command is write command */
106 switch (cmd) {
107 case DPAUX_DP_AUXCTL_CMD_I2CWR:
108 case DPAUX_DP_AUXCTL_CMD_MOTWR:
109 case DPAUX_DP_AUXCTL_CMD_AUXWR:
110 break;
111 default:
112 debug("dp: aux write cmd 0x%x is invalid\n", cmd);
113 return -EINVAL;
114 }
115
116 tegra_dpaux_writel(dp, DPAUX_DP_AUXADDR, addr);
117 for (i = 0; i < DP_AUX_MAX_BYTES / 4; ++i) {
118 memcpy(&temp_data, data, 4);
119 tegra_dpaux_writel(dp, DPAUX_DP_AUXDATA_WRITE_W(i), temp_data);
120 data += 4;
121 }
122
123 reg_val = tegra_dpaux_readl(dp, DPAUX_DP_AUXCTL);
124 reg_val &= ~DPAUX_DP_AUXCTL_CMD_MASK;
125 reg_val |= cmd;
126 reg_val &= ~DPAUX_DP_AUXCTL_CMDLEN_FIELD;
127 reg_val |= ((*size - 1) << DPAUX_DP_AUXCTL_CMDLEN_SHIFT);
128
129 while ((timeout_retries > 0) && (defer_retries > 0)) {
130 if ((timeout_retries != DP_AUX_TIMEOUT_MAX_TRIES) ||
131 (defer_retries != DP_AUX_DEFER_MAX_TRIES))
132 udelay(1);
133
134 reg_val |= DPAUX_DP_AUXCTL_TRANSACTREQ_PENDING;
135 tegra_dpaux_writel(dp, DPAUX_DP_AUXCTL, reg_val);
136
137 if (tegra_dpaux_wait_transaction(dp))
138 debug("dp: aux write transaction timeout\n");
139
140 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
141
142 if ((*aux_stat & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR_PENDING) ||
143 (*aux_stat & DPAUX_DP_AUXSTAT_RX_ERROR_PENDING) ||
144 (*aux_stat & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR_PENDING) ||
145 (*aux_stat & DPAUX_DP_AUXSTAT_NO_STOP_ERROR_PENDING)) {
146 if (timeout_retries-- > 0) {
147 debug("dp: aux write retry (0x%x) -- %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 debug("dp: aux write got error (0x%x)\n",
155 *aux_stat);
156 return -ETIMEDOUT;
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 debug("dp: aux write defer (0x%x) -- %d\n",
164 *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 debug("dp: aux write defer exceeds max retries (0x%x)\n",
171 *aux_stat);
172 return -ETIMEDOUT;
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 debug("dp: aux write failed (0x%x)\n", *aux_stat);
182 return -EIO;
183 }
184 }
185 /* Should never come to here */
186 return -EIO;
187}
188
189static int tegra_dc_dpaux_read_chunk(struct tegra_dp_priv *dp, u32 cmd,
190 u32 addr, u8 *data, u32 *size,
191 u32 *aux_stat)
192{
193 u32 reg_val;
194 u32 timeout_retries = DP_AUX_TIMEOUT_MAX_TRIES;
195 u32 defer_retries = DP_AUX_DEFER_MAX_TRIES;
196
197 if (*size > DP_AUX_MAX_BYTES) {
198 debug("only read one chunk\n");
199 return -EIO; /* only read one chunk */
200 }
201
202 /* Check to make sure the command is read command */
203 switch (cmd) {
204 case DPAUX_DP_AUXCTL_CMD_I2CRD:
205 case DPAUX_DP_AUXCTL_CMD_I2CREQWSTAT:
206 case DPAUX_DP_AUXCTL_CMD_MOTRD:
207 case DPAUX_DP_AUXCTL_CMD_AUXRD:
208 break;
209 default:
210 debug("dp: aux read cmd 0x%x is invalid\n", cmd);
211 return -EIO;
212 }
213
214 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
215 if (!(*aux_stat & DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)) {
216 debug("dp: HPD is not detected\n");
217 return -EIO;
218 }
219
220 tegra_dpaux_writel(dp, DPAUX_DP_AUXADDR, addr);
221
222 reg_val = tegra_dpaux_readl(dp, DPAUX_DP_AUXCTL);
223 reg_val &= ~DPAUX_DP_AUXCTL_CMD_MASK;
224 reg_val |= cmd;
225 reg_val &= ~DPAUX_DP_AUXCTL_CMDLEN_FIELD;
226 reg_val |= ((*size - 1) << DPAUX_DP_AUXCTL_CMDLEN_SHIFT);
227 while ((timeout_retries > 0) && (defer_retries > 0)) {
228 if ((timeout_retries != DP_AUX_TIMEOUT_MAX_TRIES) ||
229 (defer_retries != DP_AUX_DEFER_MAX_TRIES))
230 udelay(DP_DPCP_RETRY_SLEEP_NS * 2);
231
232 reg_val |= DPAUX_DP_AUXCTL_TRANSACTREQ_PENDING;
233 tegra_dpaux_writel(dp, DPAUX_DP_AUXCTL, reg_val);
234
235 if (tegra_dpaux_wait_transaction(dp))
236 debug("dp: aux read transaction timeout\n");
237
238 *aux_stat = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
239
240 if ((*aux_stat & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR_PENDING) ||
241 (*aux_stat & DPAUX_DP_AUXSTAT_RX_ERROR_PENDING) ||
242 (*aux_stat & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR_PENDING) ||
243 (*aux_stat & DPAUX_DP_AUXSTAT_NO_STOP_ERROR_PENDING)) {
244 if (timeout_retries-- > 0) {
245 debug("dp: aux read retry (0x%x) -- %d\n",
246 *aux_stat, timeout_retries);
247 /* clear the error bits */
248 tegra_dpaux_writel(dp, DPAUX_DP_AUXSTAT,
249 *aux_stat);
250 continue; /* retry */
251 } else {
252 debug("dp: aux read got error (0x%x)\n",
253 *aux_stat);
254 return -ETIMEDOUT;
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 debug("dp: aux read defer (0x%x) -- %d\n",
262 *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 debug("dp: aux read defer exceeds max retries (0x%x)\n",
269 *aux_stat);
270 return -ETIMEDOUT;
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 debug("dp: aux read failed (0x%x\n", *aux_stat);
289 return -EIO;
290 }
291 }
292 /* Should never come to here */
293 debug("%s: can't\n", __func__);
294
295 return -EIO;
296}
297
298static int tegra_dc_dpaux_read(struct tegra_dp_priv *dp, u32 cmd, u32 addr,
299 u8 *data, u32 *size, u32 *aux_stat)
300{
301 u32 finished = 0;
302 u32 cur_size;
303 int ret = 0;
304
305 do {
306 cur_size = *size - finished;
307 if (cur_size > DP_AUX_MAX_BYTES)
308 cur_size = DP_AUX_MAX_BYTES;
309
310 ret = tegra_dc_dpaux_read_chunk(dp, cmd, addr,
311 data, &cur_size, aux_stat);
312 if (ret)
313 break;
314
315 /* cur_size should be the real size returned */
316 addr += cur_size;
317 data += cur_size;
318 finished += cur_size;
319
320 } while (*size > finished);
321 *size = finished;
322
323 return ret;
324}
325
326static int tegra_dc_dp_dpcd_read(struct tegra_dp_priv *dp, u32 cmd,
327 u8 *data_ptr)
328{
329 u32 size = 1;
330 u32 status = 0;
331 int ret;
332
333 ret = tegra_dc_dpaux_read_chunk(dp, DPAUX_DP_AUXCTL_CMD_AUXRD,
334 cmd, data_ptr, &size, &status);
335 if (ret) {
336 debug("dp: Failed to read DPCD data. CMD 0x%x, Status 0x%x\n",
337 cmd, status);
338 }
339
340 return ret;
341}
342
343static int tegra_dc_dp_dpcd_write(struct tegra_dp_priv *dp, u32 cmd,
344 u8 data)
345{
346 u32 size = 1;
347 u32 status = 0;
348 int ret;
349
350 ret = tegra_dc_dpaux_write_chunk(dp, DPAUX_DP_AUXCTL_CMD_AUXWR,
351 cmd, &data, &size, &status);
352 if (ret) {
353 debug("dp: Failed to write DPCD data. CMD 0x%x, Status 0x%x\n",
354 cmd, status);
355 }
356
357 return ret;
358}
359
360static int tegra_dc_i2c_aux_read(struct tegra_dp_priv *dp, u32 i2c_addr,
361 u8 addr, u8 *data, u32 size, u32 *aux_stat)
362{
363 u32 finished = 0;
364 int ret = 0;
365
366 do {
367 u32 cur_size = min((u32)DP_AUX_MAX_BYTES, size - finished);
368
369 u32 len = 1;
370 ret = tegra_dc_dpaux_write_chunk(
371 dp, DPAUX_DP_AUXCTL_CMD_MOTWR, i2c_addr,
372 &addr, &len, aux_stat);
373 if (ret) {
374 debug("%s: error sending address to read.\n",
375 __func__);
376 return ret;
377 }
378
379 ret = tegra_dc_dpaux_read_chunk(
380 dp, DPAUX_DP_AUXCTL_CMD_I2CRD, i2c_addr,
381 data, &cur_size, aux_stat);
382 if (ret) {
383 debug("%s: error reading data.\n", __func__);
384 return ret;
385 }
386
387 /* cur_size should be the real size returned */
388 addr += cur_size;
389 data += cur_size;
390 finished += cur_size;
391 } while (size > finished);
392
393 return finished;
394}
395
396static void tegra_dc_dpaux_enable(struct tegra_dp_priv *dp)
397{
398 /* clear interrupt */
399 tegra_dpaux_writel(dp, DPAUX_INTR_AUX, 0xffffffff);
400 /* do not enable interrupt for now. Enable them when Isr in place */
401 tegra_dpaux_writel(dp, DPAUX_INTR_EN_AUX, 0x0);
402
403 tegra_dpaux_writel(dp, DPAUX_HYBRID_PADCTL,
404 DPAUX_HYBRID_PADCTL_AUX_DRVZ_OHM_50 |
405 DPAUX_HYBRID_PADCTL_AUX_CMH_V0_70 |
406 0x18 << DPAUX_HYBRID_PADCTL_AUX_DRVI_SHIFT |
407 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV_ENABLE);
408
409 tegra_dpaux_writel(dp, DPAUX_HYBRID_SPARE,
410 DPAUX_HYBRID_SPARE_PAD_PWR_POWERUP);
411}
412
413#ifdef DEBUG
414static void tegra_dc_dp_dump_link_cfg(struct tegra_dp_priv *dp,
415 const struct tegra_dp_link_config *link_cfg)
416{
417 debug("DP config: cfg_name cfg_value\n");
418 debug(" Lane Count %d\n",
419 link_cfg->max_lane_count);
420 debug(" SupportEnhancedFraming %s\n",
421 link_cfg->support_enhanced_framing ? "Y" : "N");
422 debug(" Bandwidth %d\n",
423 link_cfg->max_link_bw);
424 debug(" bpp %d\n",
425 link_cfg->bits_per_pixel);
426 debug(" EnhancedFraming %s\n",
427 link_cfg->enhanced_framing ? "Y" : "N");
428 debug(" Scramble_enabled %s\n",
429 link_cfg->scramble_ena ? "Y" : "N");
430 debug(" LinkBW %d\n",
431 link_cfg->link_bw);
432 debug(" lane_count %d\n",
433 link_cfg->lane_count);
434 debug(" activespolarity %d\n",
435 link_cfg->activepolarity);
436 debug(" active_count %d\n",
437 link_cfg->active_count);
438 debug(" tu_size %d\n",
439 link_cfg->tu_size);
440 debug(" active_frac %d\n",
441 link_cfg->active_frac);
442 debug(" watermark %d\n",
443 link_cfg->watermark);
444 debug(" hblank_sym %d\n",
445 link_cfg->hblank_sym);
446 debug(" vblank_sym %d\n",
447 link_cfg->vblank_sym);
448}
449#endif
450
Simon Glass662f2aa2015-04-14 21:03:44 -0600451static int _tegra_dp_lower_link_config(struct tegra_dp_priv *dp,
452 struct tegra_dp_link_config *cfg)
453{
454 switch (cfg->link_bw) {
455 case SOR_LINK_SPEED_G1_62:
456 if (cfg->max_link_bw > SOR_LINK_SPEED_G1_62)
457 cfg->link_bw = SOR_LINK_SPEED_G2_7;
458 cfg->lane_count /= 2;
459 break;
460 case SOR_LINK_SPEED_G2_7:
461 cfg->link_bw = SOR_LINK_SPEED_G1_62;
462 break;
463 case SOR_LINK_SPEED_G5_4:
464 if (cfg->lane_count == 1) {
465 cfg->link_bw = SOR_LINK_SPEED_G2_7;
466 cfg->lane_count = cfg->max_lane_count;
467 } else {
468 cfg->lane_count /= 2;
469 }
470 break;
471 default:
472 debug("dp: Error link rate %d\n", cfg->link_bw);
473 return -ENOLINK;
474 }
475
476 return (cfg->lane_count > 0) ? 0 : -ENOLINK;
477}
478
Simon Glassf15fe612015-04-14 21:03:41 -0600479/*
480 * Calcuate if given cfg can meet the mode request.
481 * Return 0 if mode is possible, -1 otherwise
482 */
483static int tegra_dc_dp_calc_config(struct tegra_dp_priv *dp,
484 const struct display_timing *timing,
485 struct tegra_dp_link_config *link_cfg)
486{
487 const u32 link_rate = 27 * link_cfg->link_bw * 1000 * 1000;
488 const u64 f = 100000; /* precision factor */
489 u32 num_linkclk_line; /* Number of link clocks per line */
490 u64 ratio_f; /* Ratio of incoming to outgoing data rate */
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 int i;
507 int neg;
508
509 if (!link_rate || !link_cfg->lane_count || !timing->pixelclock.typ ||
510 !link_cfg->bits_per_pixel)
511 return -1;
512
513 if ((u64)timing->pixelclock.typ * link_cfg->bits_per_pixel >=
514 (u64)link_rate * 8 * link_cfg->lane_count)
515 return -1;
516
517 num_linkclk_line = (u32)(lldiv(link_rate * timing->hactive.typ,
518 timing->pixelclock.typ));
519
520 ratio_f = (u64)timing->pixelclock.typ * link_cfg->bits_per_pixel * f;
521 ratio_f /= 8;
522 do_div(ratio_f, link_rate * link_cfg->lane_count);
523
524 for (i = 64; i >= 32; --i) {
525 activesym_f = ratio_f * i;
526 activecount_f = lldiv(activesym_f, (u32)f) * f;
527 frac_f = activesym_f - activecount_f;
528 activecount = (u32)(lldiv(activecount_f, (u32)f));
529
530 if (frac_f < (lldiv(f, 2))) /* fraction < 0.5 */
531 activepolarity = 0;
532 else {
533 activepolarity = 1;
534 frac_f = f - frac_f;
535 }
536
537 if (frac_f != 0) {
538 /* warning: frac_f should be 64-bit */
539 frac_f = lldiv(f * f, frac_f); /* 1 / fraction */
540 if (frac_f > (15 * f))
541 activefrac = activepolarity ? 1 : 15;
542 else
543 activefrac = activepolarity ?
544 (u32)lldiv(frac_f, (u32)f) + 1 :
545 (u32)lldiv(frac_f, (u32)f);
546 }
547
548 if (activefrac == 1)
549 activepolarity = 0;
550
551 if (activepolarity == 1)
552 approx_value_f = activefrac ? lldiv(
553 (activecount_f + (activefrac * f - f) * f),
554 (activefrac * f)) :
555 activecount_f + f;
556 else
557 approx_value_f = activefrac ?
558 activecount_f + lldiv(f, activefrac) :
559 activecount_f;
560
561 if (activesym_f < approx_value_f) {
562 accumulated_error_f = num_linkclk_line *
563 lldiv(approx_value_f - activesym_f, i);
564 neg = 1;
565 } else {
566 accumulated_error_f = num_linkclk_line *
567 lldiv(activesym_f - approx_value_f, i);
568 neg = 0;
569 }
570
571 if ((neg && (lowest_neg_error_f > accumulated_error_f)) ||
572 (accumulated_error_f == 0)) {
573 lowest_neg_error_f = accumulated_error_f;
574 lowest_neg_tusize = i;
575 lowest_neg_activecount = activecount;
576 lowest_neg_activepolarity = activepolarity;
577 lowest_neg_activefrac = activefrac;
578
579 if (accumulated_error_f == 0)
580 break;
581 }
582 }
583
584 if (lowest_neg_activefrac == 0) {
585 link_cfg->activepolarity = 0;
586 link_cfg->active_count = lowest_neg_activepolarity ?
587 lowest_neg_activecount : lowest_neg_activecount - 1;
588 link_cfg->tu_size = lowest_neg_tusize;
589 link_cfg->active_frac = 1;
590 } else {
591 link_cfg->activepolarity = lowest_neg_activepolarity;
592 link_cfg->active_count = (u32)lowest_neg_activecount;
593 link_cfg->tu_size = lowest_neg_tusize;
594 link_cfg->active_frac = (u32)lowest_neg_activefrac;
595 }
596
597 watermark_f = lldiv(ratio_f * link_cfg->tu_size * (f - ratio_f), f);
598 link_cfg->watermark = (u32)(lldiv(watermark_f + lowest_neg_error_f,
599 f)) + link_cfg->bits_per_pixel / 4 - 1;
600 num_symbols_per_line = (timing->hactive.typ *
601 link_cfg->bits_per_pixel) /
602 (8 * link_cfg->lane_count);
603
604 if (link_cfg->watermark > 30) {
605 debug("dp: sor setting: unable to get a good tusize, force watermark to 30\n");
606 link_cfg->watermark = 30;
607 return -1;
608 } else if (link_cfg->watermark > num_symbols_per_line) {
609 debug("dp: sor setting: force watermark to the number of symbols in the line\n");
610 link_cfg->watermark = num_symbols_per_line;
611 return -1;
612 }
613
614 /*
615 * Refer to dev_disp.ref for more information.
616 * # symbols/hblank = ((SetRasterBlankEnd.X + SetRasterSize.Width -
617 * SetRasterBlankStart.X - 7) * link_clk / pclk)
618 * - 3 * enhanced_framing - Y
619 * where Y = (# lanes == 4) 3 : (# lanes == 2) ? 6 : 12
620 */
621 link_cfg->hblank_sym = (int)lldiv(((uint64_t)timing->hback_porch.typ +
622 timing->hfront_porch.typ + timing->hsync_len.typ - 7) *
623 link_rate, timing->pixelclock.typ) -
624 3 * link_cfg->enhanced_framing -
625 (12 / link_cfg->lane_count);
626
627 if (link_cfg->hblank_sym < 0)
628 link_cfg->hblank_sym = 0;
629
Simon Glassf15fe612015-04-14 21:03:41 -0600630 /*
631 * Refer to dev_disp.ref for more information.
632 * # symbols/vblank = ((SetRasterBlankStart.X -
633 * SetRasterBlankEen.X - 25) * link_clk / pclk)
634 * - Y - 1;
635 * where Y = (# lanes == 4) 12 : (# lanes == 2) ? 21 : 39
636 */
637 link_cfg->vblank_sym = (int)lldiv(((uint64_t)timing->hactive.typ - 25)
638 * link_rate, timing->pixelclock.typ) - (36 /
639 link_cfg->lane_count) - 4;
640
641 if (link_cfg->vblank_sym < 0)
642 link_cfg->vblank_sym = 0;
643
644 link_cfg->is_valid = 1;
645#ifdef DEBUG
646 tegra_dc_dp_dump_link_cfg(dp, link_cfg);
647#endif
648
649 return 0;
650}
651
652static int tegra_dc_dp_init_max_link_cfg(
653 const struct display_timing *timing,
654 struct tegra_dp_priv *dp,
655 struct tegra_dp_link_config *link_cfg)
656{
657 const int drive_current = 0x40404040;
658 const int preemphasis = 0x0f0f0f0f;
659 const int postcursor = 0;
660 u8 dpcd_data;
661 int ret;
662
663 ret = tegra_dc_dp_dpcd_read(dp, DP_MAX_LANE_COUNT, &dpcd_data);
664 if (ret)
665 return ret;
666 link_cfg->max_lane_count = dpcd_data & DP_MAX_LANE_COUNT_MASK;
Simon Glass662f2aa2015-04-14 21:03:44 -0600667 link_cfg->tps3_supported = (dpcd_data &
668 DP_MAX_LANE_COUNT_TPS3_SUPPORTED_YES) ? 1 : 0;
Simon Glassf15fe612015-04-14 21:03:41 -0600669
670 link_cfg->support_enhanced_framing =
671 (dpcd_data & DP_MAX_LANE_COUNT_ENHANCED_FRAMING_YES) ?
672 1 : 0;
673
674 ret = tegra_dc_dp_dpcd_read(dp, DP_MAX_DOWNSPREAD, &dpcd_data);
675 if (ret)
676 return ret;
677 link_cfg->downspread = (dpcd_data & DP_MAX_DOWNSPREAD_VAL_0_5_PCT) ?
678 1 : 0;
679
Simon Glass662f2aa2015-04-14 21:03:44 -0600680 ret = tegra_dc_dp_dpcd_read(dp, NV_DPCD_TRAINING_AUX_RD_INTERVAL,
681 &link_cfg->aux_rd_interval);
682 if (ret)
683 return ret;
Simon Glassf15fe612015-04-14 21:03:41 -0600684 ret = tegra_dc_dp_dpcd_read(dp, DP_MAX_LINK_RATE,
685 &link_cfg->max_link_bw);
686 if (ret)
687 return ret;
688
689 /*
690 * Set to a high value for link training and attach.
691 * Will be re-programmed when dp is enabled.
692 */
693 link_cfg->drive_current = drive_current;
694 link_cfg->preemphasis = preemphasis;
695 link_cfg->postcursor = postcursor;
696
697 ret = tegra_dc_dp_dpcd_read(dp, DP_EDP_CONFIGURATION_CAP, &dpcd_data);
698 if (ret)
699 return ret;
700
701 link_cfg->alt_scramber_reset_cap =
702 (dpcd_data & DP_EDP_CONFIGURATION_CAP_ASC_RESET_YES) ?
703 1 : 0;
704 link_cfg->only_enhanced_framing =
705 (dpcd_data & DP_EDP_CONFIGURATION_CAP_FRAMING_CHANGE_YES) ?
706 1 : 0;
707
708 link_cfg->lane_count = link_cfg->max_lane_count;
709 link_cfg->link_bw = link_cfg->max_link_bw;
710 link_cfg->enhanced_framing = link_cfg->support_enhanced_framing;
Simon Glass662f2aa2015-04-14 21:03:44 -0600711 link_cfg->frame_in_ms = (1000 / 60) + 1;
Simon Glassf15fe612015-04-14 21:03:41 -0600712
713 tegra_dc_dp_calc_config(dp, timing, link_cfg);
714 return 0;
715}
716
Simon Glassfad72182016-01-30 16:37:50 -0700717static int tegra_dc_dp_set_assr(struct tegra_dp_priv *priv,
718 struct udevice *sor, int ena)
Simon Glassf15fe612015-04-14 21:03:41 -0600719{
720 int ret;
721
722 u8 dpcd_data = ena ?
723 DP_MAIN_LINK_CHANNEL_CODING_SET_ASC_RESET_ENABLE :
724 DP_MAIN_LINK_CHANNEL_CODING_SET_ASC_RESET_DISABLE;
725
Simon Glassfad72182016-01-30 16:37:50 -0700726 ret = tegra_dc_dp_dpcd_write(priv, DP_EDP_CONFIGURATION_SET,
Simon Glassf15fe612015-04-14 21:03:41 -0600727 dpcd_data);
728 if (ret)
729 return ret;
730
731 /* Also reset the scrambler to 0xfffe */
732 tegra_dc_sor_set_internal_panel(sor, ena);
733 return 0;
734}
735
736static int tegra_dp_set_link_bandwidth(struct tegra_dp_priv *dp,
Simon Glassfad72182016-01-30 16:37:50 -0700737 struct udevice *sor,
Simon Glassf15fe612015-04-14 21:03:41 -0600738 u8 link_bw)
739{
740 tegra_dc_sor_set_link_bandwidth(sor, link_bw);
741
742 /* Sink side */
743 return tegra_dc_dp_dpcd_write(dp, DP_LINK_BW_SET, link_bw);
744}
745
746static int tegra_dp_set_lane_count(struct tegra_dp_priv *dp,
747 const struct tegra_dp_link_config *link_cfg,
Simon Glassfad72182016-01-30 16:37:50 -0700748 struct udevice *sor)
Simon Glassf15fe612015-04-14 21:03:41 -0600749{
750 u8 dpcd_data;
751 int ret;
752
753 /* check if panel support enhanched_framing */
754 dpcd_data = link_cfg->lane_count;
755 if (link_cfg->enhanced_framing)
756 dpcd_data |= DP_LANE_COUNT_SET_ENHANCEDFRAMING_T;
757 ret = tegra_dc_dp_dpcd_write(dp, DP_LANE_COUNT_SET, dpcd_data);
758 if (ret)
759 return ret;
760
761 tegra_dc_sor_set_lane_count(sor, link_cfg->lane_count);
762
763 /* Also power down lanes that will not be used */
764 return 0;
765}
766
767static int tegra_dc_dp_link_trained(struct tegra_dp_priv *dp,
768 const struct tegra_dp_link_config *cfg)
769{
770 u32 lane;
771 u8 mask;
772 u8 data;
773 int ret;
774
775 for (lane = 0; lane < cfg->lane_count; ++lane) {
776 ret = tegra_dc_dp_dpcd_read(dp, (lane / 2) ?
777 DP_LANE2_3_STATUS : DP_LANE0_1_STATUS,
778 &data);
779 if (ret)
780 return ret;
781 mask = (lane & 1) ?
782 NV_DPCD_STATUS_LANEXPLUS1_CR_DONE_YES |
783 NV_DPCD_STATUS_LANEXPLUS1_CHN_EQ_DONE_YES |
784 NV_DPCD_STATUS_LANEXPLUS1_SYMBOL_LOCKED_YES :
785 DP_LANE_CR_DONE |
786 DP_LANE_CHANNEL_EQ_DONE |
787 DP_LANE_SYMBOL_LOCKED;
788 if ((data & mask) != mask)
789 return -1;
790 }
Simon Glass662f2aa2015-04-14 21:03:44 -0600791 return 0;
792}
793
794static int tegra_dp_channel_eq_status(struct tegra_dp_priv *dp,
795 const struct tegra_dp_link_config *cfg)
796{
797 u32 cnt;
798 u32 n_lanes = cfg->lane_count;
799 u8 data;
800 u8 ce_done = 1;
801 int ret;
802
803 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
804 ret = tegra_dc_dp_dpcd_read(dp, DP_LANE0_1_STATUS + cnt, &data);
805 if (ret)
806 return ret;
807
808 if (n_lanes == 1) {
809 ce_done = (data & (0x1 <<
810 NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) &&
811 (data & (0x1 <<
812 NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT));
813 break;
814 } else if (!(data & (0x1 <<
815 NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) ||
816 !(data & (0x1 <<
817 NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT)) ||
818 !(data & (0x1 <<
819 NV_DPCD_STATUS_LANEXPLUS1_CHN_EQ_DONE_SHIFT)) ||
820 !(data & (0x1 <<
821 NV_DPCD_STATUS_LANEXPLUS1_SYMBOL_LOCKED_SHIFT)))
822 return -EIO;
823 }
824
825 if (ce_done) {
826 ret = tegra_dc_dp_dpcd_read(dp,
827 DP_LANE_ALIGN_STATUS_UPDATED,
828 &data);
829 if (ret)
830 return ret;
831 if (!(data & NV_DPCD_LANE_ALIGN_STATUS_UPDATED_DONE_YES))
832 ce_done = 0;
833 }
834
835 return ce_done ? 0 : -EIO;
836}
837
838static int tegra_dp_clock_recovery_status(struct tegra_dp_priv *dp,
839 const struct tegra_dp_link_config *cfg)
840{
841 u32 cnt;
842 u32 n_lanes = cfg->lane_count;
843 u8 data_ptr;
844 int ret;
845
846 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
847 ret = tegra_dc_dp_dpcd_read(dp, (DP_LANE0_1_STATUS + cnt),
848 &data_ptr);
849 if (ret)
850 return ret;
851
852 if (n_lanes == 1)
853 return (data_ptr & NV_DPCD_STATUS_LANEX_CR_DONE_YES) ?
854 1 : 0;
855 else if (!(data_ptr & NV_DPCD_STATUS_LANEX_CR_DONE_YES) ||
856 !(data_ptr & (NV_DPCD_STATUS_LANEXPLUS1_CR_DONE_YES)))
857 return 0;
858 }
859
860 return 1;
861}
862
863static int tegra_dp_lt_adjust(struct tegra_dp_priv *dp, u32 pe[4], u32 vs[4],
864 u32 pc[4], u8 pc_supported,
865 const struct tegra_dp_link_config *cfg)
866{
867 size_t cnt;
868 u8 data_ptr;
869 u32 n_lanes = cfg->lane_count;
870 int ret;
871
872 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
873 ret = tegra_dc_dp_dpcd_read(dp, DP_ADJUST_REQUEST_LANE0_1 + cnt,
874 &data_ptr);
875 if (ret)
876 return ret;
877 pe[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_PE_MASK) >>
878 NV_DPCD_ADJUST_REQ_LANEX_PE_SHIFT;
879 vs[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_DC_MASK) >>
880 NV_DPCD_ADJUST_REQ_LANEX_DC_SHIFT;
881 pe[1 + 2 * cnt] =
882 (data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_MASK) >>
883 NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_SHIFT;
884 vs[1 + 2 * cnt] =
885 (data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_MASK) >>
886 NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_SHIFT;
887 }
888 if (pc_supported) {
889 ret = tegra_dc_dp_dpcd_read(dp, NV_DPCD_ADJUST_REQ_POST_CURSOR2,
890 &data_ptr);
891 if (ret)
892 return ret;
893 for (cnt = 0; cnt < n_lanes; cnt++) {
894 pc[cnt] = (data_ptr >>
895 NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_SHIFT(cnt)) &
896 NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_MASK;
897 }
898 }
899
900 return 0;
901}
902
903static void tegra_dp_wait_aux_training(struct tegra_dp_priv *dp,
904 bool is_clk_recovery,
905 const struct tegra_dp_link_config *cfg)
906{
907 if (!cfg->aux_rd_interval)
908 udelay(is_clk_recovery ? 200 : 500);
909 else
910 mdelay(cfg->aux_rd_interval * 4);
911}
912
913static void tegra_dp_tpg(struct tegra_dp_priv *dp, u32 tp, u32 n_lanes,
914 const struct tegra_dp_link_config *cfg)
915{
916 u8 data = (tp == training_pattern_disabled)
917 ? (tp | NV_DPCD_TRAINING_PATTERN_SET_SC_DISABLED_F)
918 : (tp | NV_DPCD_TRAINING_PATTERN_SET_SC_DISABLED_T);
919
920 tegra_dc_sor_set_dp_linkctl(dp->sor, 1, tp, cfg);
921 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_PATTERN_SET, data);
922}
923
924static int tegra_dp_link_config(struct tegra_dp_priv *dp,
925 const struct tegra_dp_link_config *link_cfg)
926{
927 u8 dpcd_data;
928 u32 retry;
929 int ret;
930
931 if (link_cfg->lane_count == 0) {
932 debug("dp: error: lane count is 0. Can not set link config.\n");
933 return -ENOLINK;
934 }
935
936 /* Set power state if it is not in normal level */
937 ret = tegra_dc_dp_dpcd_read(dp, DP_SET_POWER, &dpcd_data);
938 if (ret)
939 return ret;
940
941 if (dpcd_data == DP_SET_POWER_D3) {
942 dpcd_data = DP_SET_POWER_D0;
943
944 /* DP spec requires 3 retries */
945 for (retry = 3; retry > 0; --retry) {
946 ret = tegra_dc_dp_dpcd_write(dp, DP_SET_POWER,
947 dpcd_data);
948 if (!ret)
949 break;
950 if (retry == 1) {
951 debug("dp: Failed to set DP panel power\n");
952 return ret;
953 }
954 }
955 }
956
957 /* Enable ASSR if possible */
958 if (link_cfg->alt_scramber_reset_cap) {
959 ret = tegra_dc_dp_set_assr(dp, dp->sor, 1);
960 if (ret)
961 return ret;
962 }
963
964 ret = tegra_dp_set_link_bandwidth(dp, dp->sor, link_cfg->link_bw);
965 if (ret) {
966 debug("dp: Failed to set link bandwidth\n");
967 return ret;
968 }
969 ret = tegra_dp_set_lane_count(dp, link_cfg, dp->sor);
970 if (ret) {
971 debug("dp: Failed to set lane count\n");
972 return ret;
973 }
974 tegra_dc_sor_set_dp_linkctl(dp->sor, 1, training_pattern_none,
975 link_cfg);
976
977 return 0;
978}
979
980static int tegra_dp_lower_link_config(struct tegra_dp_priv *dp,
981 const struct display_timing *timing,
982 struct tegra_dp_link_config *cfg)
983{
984 struct tegra_dp_link_config tmp_cfg;
985 int ret;
986
987 tmp_cfg = *cfg;
988 cfg->is_valid = 0;
989
990 ret = _tegra_dp_lower_link_config(dp, cfg);
991 if (!ret)
992 ret = tegra_dc_dp_calc_config(dp, timing, cfg);
993 if (!ret)
994 ret = tegra_dp_link_config(dp, cfg);
995 if (ret)
996 goto fail;
997
998 return 0;
999
1000fail:
1001 *cfg = tmp_cfg;
1002 tegra_dp_link_config(dp, &tmp_cfg);
1003 return ret;
1004}
1005
1006static int tegra_dp_lt_config(struct tegra_dp_priv *dp, u32 pe[4], u32 vs[4],
1007 u32 pc[4], const struct tegra_dp_link_config *cfg)
1008{
Simon Glassfad72182016-01-30 16:37:50 -07001009 struct udevice *sor = dp->sor;
Simon Glass662f2aa2015-04-14 21:03:44 -06001010 u32 n_lanes = cfg->lane_count;
1011 u8 pc_supported = cfg->tps3_supported;
1012 u32 cnt;
1013 u32 val;
1014
1015 for (cnt = 0; cnt < n_lanes; cnt++) {
1016 u32 mask = 0;
1017 u32 pe_reg, vs_reg, pc_reg;
1018 u32 shift = 0;
1019
1020 switch (cnt) {
1021 case 0:
1022 mask = PR_LANE2_DP_LANE0_MASK;
1023 shift = PR_LANE2_DP_LANE0_SHIFT;
1024 break;
1025 case 1:
1026 mask = PR_LANE1_DP_LANE1_MASK;
1027 shift = PR_LANE1_DP_LANE1_SHIFT;
1028 break;
1029 case 2:
1030 mask = PR_LANE0_DP_LANE2_MASK;
1031 shift = PR_LANE0_DP_LANE2_SHIFT;
1032 break;
1033 case 3:
1034 mask = PR_LANE3_DP_LANE3_MASK;
1035 shift = PR_LANE3_DP_LANE3_SHIFT;
1036 break;
1037 default:
1038 debug("dp: incorrect lane cnt\n");
1039 return -EINVAL;
1040 }
1041
1042 pe_reg = tegra_dp_pe_regs[pc[cnt]][vs[cnt]][pe[cnt]];
1043 vs_reg = tegra_dp_vs_regs[pc[cnt]][vs[cnt]][pe[cnt]];
1044 pc_reg = tegra_dp_pc_regs[pc[cnt]][vs[cnt]][pe[cnt]];
1045
1046 tegra_dp_set_pe_vs_pc(sor, mask, pe_reg << shift,
1047 vs_reg << shift, pc_reg << shift,
1048 pc_supported);
1049 }
1050
1051 tegra_dp_disable_tx_pu(dp->sor);
1052 udelay(20);
1053
1054 for (cnt = 0; cnt < n_lanes; cnt++) {
1055 u32 max_vs_flag = tegra_dp_is_max_vs(pe[cnt], vs[cnt]);
1056 u32 max_pe_flag = tegra_dp_is_max_pe(pe[cnt], vs[cnt]);
1057
1058 val = (vs[cnt] << NV_DPCD_TRAINING_LANEX_SET_DC_SHIFT) |
1059 (max_vs_flag ?
1060 NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_T :
1061 NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_F) |
1062 (pe[cnt] << NV_DPCD_TRAINING_LANEX_SET_PE_SHIFT) |
1063 (max_pe_flag ?
1064 NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_T :
1065 NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_F);
1066 tegra_dc_dp_dpcd_write(dp, (DP_TRAINING_LANE0_SET + cnt), val);
1067 }
1068
1069 if (pc_supported) {
1070 for (cnt = 0; cnt < n_lanes / 2; cnt++) {
1071 u32 max_pc_flag0 = tegra_dp_is_max_pc(pc[cnt]);
1072 u32 max_pc_flag1 = tegra_dp_is_max_pc(pc[cnt + 1]);
1073 val = (pc[cnt] << NV_DPCD_LANEX_SET2_PC2_SHIFT) |
1074 (max_pc_flag0 ?
1075 NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_T :
1076 NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_F) |
1077 (pc[cnt + 1] <<
1078 NV_DPCD_LANEXPLUS1_SET2_PC2_SHIFT) |
1079 (max_pc_flag1 ?
1080 NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_T :
1081 NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_F);
1082 tegra_dc_dp_dpcd_write(dp,
1083 NV_DPCD_TRAINING_LANE0_1_SET2 +
1084 cnt, val);
1085 }
1086 }
1087
1088 return 0;
1089}
1090
1091static int _tegra_dp_channel_eq(struct tegra_dp_priv *dp, u32 pe[4],
1092 u32 vs[4], u32 pc[4], u8 pc_supported,
1093 u32 n_lanes,
1094 const struct tegra_dp_link_config *cfg)
1095{
1096 u32 retry_cnt;
1097
1098 for (retry_cnt = 0; retry_cnt < 4; retry_cnt++) {
1099 int ret;
1100
1101 if (retry_cnt) {
1102 ret = tegra_dp_lt_adjust(dp, pe, vs, pc, pc_supported,
1103 cfg);
1104 if (ret)
1105 return ret;
1106 tegra_dp_lt_config(dp, pe, vs, pc, cfg);
1107 }
1108
1109 tegra_dp_wait_aux_training(dp, false, cfg);
1110
1111 if (!tegra_dp_clock_recovery_status(dp, cfg)) {
1112 debug("dp: CR failed in channel EQ sequence!\n");
1113 break;
1114 }
1115
1116 if (!tegra_dp_channel_eq_status(dp, cfg))
1117 return 0;
1118 }
1119
1120 return -EIO;
1121}
1122
1123static int tegra_dp_channel_eq(struct tegra_dp_priv *dp, u32 pe[4], u32 vs[4],
1124 u32 pc[4],
1125 const struct tegra_dp_link_config *cfg)
1126{
1127 u32 n_lanes = cfg->lane_count;
1128 u8 pc_supported = cfg->tps3_supported;
1129 int ret;
1130 u32 tp_src = training_pattern_2;
1131
1132 if (pc_supported)
1133 tp_src = training_pattern_3;
1134
1135 tegra_dp_tpg(dp, tp_src, n_lanes, cfg);
1136
1137 ret = _tegra_dp_channel_eq(dp, pe, vs, pc, pc_supported, n_lanes, cfg);
1138
1139 tegra_dp_tpg(dp, training_pattern_disabled, n_lanes, cfg);
1140
1141 return ret;
1142}
1143
1144static int _tegra_dp_clk_recovery(struct tegra_dp_priv *dp, u32 pe[4],
1145 u32 vs[4], u32 pc[4], u8 pc_supported,
1146 u32 n_lanes,
1147 const struct tegra_dp_link_config *cfg)
1148{
1149 u32 vs_temp[4];
1150 u32 retry_cnt = 0;
1151
1152 do {
1153 tegra_dp_lt_config(dp, pe, vs, pc, cfg);
1154 tegra_dp_wait_aux_training(dp, true, cfg);
1155
1156 if (tegra_dp_clock_recovery_status(dp, cfg))
1157 return 0;
1158
1159 memcpy(vs_temp, vs, sizeof(vs_temp));
1160 tegra_dp_lt_adjust(dp, pe, vs, pc, pc_supported, cfg);
1161
1162 if (memcmp(vs_temp, vs, sizeof(vs_temp)))
1163 retry_cnt = 0;
1164 else
1165 ++retry_cnt;
1166 } while (retry_cnt < 5);
1167
1168 return -EIO;
1169}
1170
1171static int tegra_dp_clk_recovery(struct tegra_dp_priv *dp, u32 pe[4],
1172 u32 vs[4], u32 pc[4],
1173 const struct tegra_dp_link_config *cfg)
1174{
1175 u32 n_lanes = cfg->lane_count;
1176 u8 pc_supported = cfg->tps3_supported;
1177 int err;
1178
1179 tegra_dp_tpg(dp, training_pattern_1, n_lanes, cfg);
1180
1181 err = _tegra_dp_clk_recovery(dp, pe, vs, pc, pc_supported, n_lanes,
1182 cfg);
1183 if (err < 0)
1184 tegra_dp_tpg(dp, training_pattern_disabled, n_lanes, cfg);
1185
1186 return err;
1187}
1188
1189static int tegra_dc_dp_full_link_training(struct tegra_dp_priv *dp,
1190 const struct display_timing *timing,
1191 struct tegra_dp_link_config *cfg)
1192{
Simon Glassfad72182016-01-30 16:37:50 -07001193 struct udevice *sor = dp->sor;
Simon Glass662f2aa2015-04-14 21:03:44 -06001194 int err;
1195 u32 pe[4], vs[4], pc[4];
1196
1197 tegra_sor_precharge_lanes(sor, cfg);
1198
1199retry_cr:
1200 memset(pe, PREEMPHASIS_DISABLED, sizeof(pe));
1201 memset(vs, DRIVECURRENT_LEVEL0, sizeof(vs));
1202 memset(pc, POSTCURSOR2_LEVEL0, sizeof(pc));
1203
1204 err = tegra_dp_clk_recovery(dp, pe, vs, pc, cfg);
1205 if (err) {
1206 if (!tegra_dp_lower_link_config(dp, timing, cfg))
1207 goto retry_cr;
1208
1209 debug("dp: clk recovery failed\n");
1210 goto fail;
1211 }
1212
1213 err = tegra_dp_channel_eq(dp, pe, vs, pc, cfg);
1214 if (err) {
1215 if (!tegra_dp_lower_link_config(dp, timing, cfg))
1216 goto retry_cr;
1217
1218 debug("dp: channel equalization failed\n");
1219 goto fail;
1220 }
1221#ifdef DEBUG
1222 tegra_dc_dp_dump_link_cfg(dp, cfg);
1223#endif
Simon Glassf15fe612015-04-14 21:03:41 -06001224 return 0;
Simon Glass662f2aa2015-04-14 21:03:44 -06001225
1226fail:
1227 return err;
Simon Glassf15fe612015-04-14 21:03:41 -06001228}
1229
1230/*
1231 * All link training functions are ported from kernel dc driver.
1232 * See more details at drivers/video/tegra/dc/dp.c
1233 */
1234static int tegra_dc_dp_fast_link_training(struct tegra_dp_priv *dp,
1235 const struct tegra_dp_link_config *link_cfg,
Simon Glassfad72182016-01-30 16:37:50 -07001236 struct udevice *sor)
Simon Glassf15fe612015-04-14 21:03:41 -06001237{
1238 u8 link_bw;
1239 u8 lane_count;
1240 u16 data16;
1241 u32 data32;
1242 u32 size;
1243 u32 status;
1244 int j;
1245 u32 mask = 0xffff >> ((4 - link_cfg->lane_count) * 4);
1246
1247 tegra_dc_sor_set_lane_parm(sor, link_cfg);
1248 tegra_dc_dp_dpcd_write(dp, DP_MAIN_LINK_CHANNEL_CODING_SET,
1249 DP_SET_ANSI_8B10B);
1250
1251 /* Send TP1 */
1252 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_1, link_cfg);
1253 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_PATTERN_SET,
1254 DP_TRAINING_PATTERN_1);
1255
1256 for (j = 0; j < link_cfg->lane_count; ++j)
1257 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_LANE0_SET + j, 0x24);
1258 udelay(520);
1259
1260 size = sizeof(data16);
1261 tegra_dc_dpaux_read(dp, DPAUX_DP_AUXCTL_CMD_AUXRD,
1262 DP_LANE0_1_STATUS, (u8 *)&data16, &size, &status);
1263 status = mask & 0x1111;
1264 if ((data16 & status) != status) {
1265 debug("dp: Link training error for TP1 (%#x, status %#x)\n",
1266 data16, status);
1267 return -EFAULT;
1268 }
1269
1270 /* enable ASSR */
1271 tegra_dc_dp_set_assr(dp, sor, link_cfg->scramble_ena);
1272 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_3, link_cfg);
1273
1274 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_PATTERN_SET,
1275 link_cfg->link_bw == 20 ? 0x23 : 0x22);
1276 for (j = 0; j < link_cfg->lane_count; ++j)
1277 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_LANE0_SET + j, 0x24);
1278 udelay(520);
1279
1280 size = sizeof(data32);
1281 tegra_dc_dpaux_read(dp, DPAUX_DP_AUXCTL_CMD_AUXRD, DP_LANE0_1_STATUS,
1282 (u8 *)&data32, &size, &status);
1283 if ((data32 & mask) != (0x7777 & mask)) {
1284 debug("dp: Link training error for TP2/3 (0x%x)\n", data32);
1285 return -EFAULT;
1286 }
1287
1288 tegra_dc_sor_set_dp_linkctl(sor, 1, training_pattern_disabled,
1289 link_cfg);
1290 tegra_dc_dp_dpcd_write(dp, DP_TRAINING_PATTERN_SET, 0);
1291
1292 if (tegra_dc_dp_link_trained(dp, link_cfg)) {
1293 tegra_dc_sor_read_link_config(sor, &link_bw, &lane_count);
1294 debug("Fast link training failed, link bw %d, lane # %d\n",
1295 link_bw, lane_count);
1296 return -EFAULT;
1297 }
1298
1299 debug("Fast link training succeeded, link bw %d, lane %d\n",
1300 link_cfg->link_bw, link_cfg->lane_count);
1301
1302 return 0;
1303}
1304
Simon Glass662f2aa2015-04-14 21:03:44 -06001305static int tegra_dp_do_link_training(struct tegra_dp_priv *dp,
1306 struct tegra_dp_link_config *link_cfg,
1307 const struct display_timing *timing,
Simon Glassfad72182016-01-30 16:37:50 -07001308 struct udevice *sor)
Simon Glassf15fe612015-04-14 21:03:41 -06001309{
Simon Glassf15fe612015-04-14 21:03:41 -06001310 u8 link_bw;
1311 u8 lane_count;
Simon Glassf15fe612015-04-14 21:03:41 -06001312 int ret;
1313
Simon Glass662f2aa2015-04-14 21:03:44 -06001314 if (DO_FAST_LINK_TRAINING) {
1315 ret = tegra_dc_dp_fast_link_training(dp, link_cfg, sor);
Simon Glassf15fe612015-04-14 21:03:41 -06001316 if (ret) {
Simon Glass662f2aa2015-04-14 21:03:44 -06001317 debug("dp: fast link training failed\n");
1318 } else {
1319 /*
1320 * set to a known-good drive setting if fast link
1321 * succeeded. Ignore any error.
1322 */
1323 ret = tegra_dc_sor_set_voltage_swing(dp->sor, link_cfg);
1324 if (ret)
1325 debug("Failed to set voltage swing\n");
Simon Glassf15fe612015-04-14 21:03:41 -06001326 }
Simon Glass662f2aa2015-04-14 21:03:44 -06001327 } else {
1328 ret = -ENOSYS;
Simon Glassf15fe612015-04-14 21:03:41 -06001329 }
Simon Glassf15fe612015-04-14 21:03:41 -06001330 if (ret) {
Simon Glass662f2aa2015-04-14 21:03:44 -06001331 /* Try full link training then */
1332 ret = tegra_dc_dp_full_link_training(dp, timing, link_cfg);
1333 if (ret) {
1334 debug("dp: full link training failed\n");
1335 return ret;
1336 }
Simon Glassf15fe612015-04-14 21:03:41 -06001337 }
1338
1339 /* Everything is good; double check the link config */
1340 tegra_dc_sor_read_link_config(sor, &link_bw, &lane_count);
1341
1342 if ((link_cfg->link_bw == link_bw) &&
1343 (link_cfg->lane_count == lane_count))
1344 return 0;
1345 else
1346 return -EFAULT;
1347}
1348
1349static int tegra_dc_dp_explore_link_cfg(struct tegra_dp_priv *dp,
1350 struct tegra_dp_link_config *link_cfg,
Simon Glassfad72182016-01-30 16:37:50 -07001351 struct udevice *sor,
Simon Glassf15fe612015-04-14 21:03:41 -06001352 const struct display_timing *timing)
1353{
1354 struct tegra_dp_link_config temp_cfg;
1355
1356 if (!timing->pixelclock.typ || !timing->hactive.typ ||
1357 !timing->vactive.typ) {
1358 debug("dp: error mode configuration");
1359 return -EINVAL;
1360 }
1361 if (!link_cfg->max_link_bw || !link_cfg->max_lane_count) {
1362 debug("dp: error link configuration");
1363 return -EINVAL;
1364 }
1365
1366 link_cfg->is_valid = 0;
1367
1368 memcpy(&temp_cfg, link_cfg, sizeof(temp_cfg));
1369
1370 temp_cfg.link_bw = temp_cfg.max_link_bw;
1371 temp_cfg.lane_count = temp_cfg.max_lane_count;
1372
1373 /*
1374 * set to max link config
1375 */
1376 if ((!tegra_dc_dp_calc_config(dp, timing, &temp_cfg)) &&
Simon Glass662f2aa2015-04-14 21:03:44 -06001377 (!tegra_dp_link_config(dp, &temp_cfg)) &&
1378 (!tegra_dp_do_link_training(dp, &temp_cfg, timing, sor)))
Simon Glassf15fe612015-04-14 21:03:41 -06001379 /* the max link cfg is doable */
1380 memcpy(link_cfg, &temp_cfg, sizeof(temp_cfg));
1381
1382 return link_cfg->is_valid ? 0 : -EFAULT;
1383}
1384
1385static int tegra_dp_hpd_plug(struct tegra_dp_priv *dp)
1386{
1387 const int vdd_to_hpd_delay_ms = 200;
1388 u32 val;
1389 ulong start;
1390
1391 start = get_timer(0);
1392 do {
1393 val = tegra_dpaux_readl(dp, DPAUX_DP_AUXSTAT);
1394 if (val & DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)
1395 return 0;
1396 udelay(100);
1397 } while (get_timer(start) < vdd_to_hpd_delay_ms);
1398
1399 return -EIO;
1400}
1401
Simon Glass662f2aa2015-04-14 21:03:44 -06001402static int tegra_dc_dp_sink_out_of_sync(struct tegra_dp_priv *dp, u32 delay_ms)
1403{
1404 u8 dpcd_data;
1405 int out_of_sync;
1406 int ret;
1407
1408 debug("%s: delay=%d\n", __func__, delay_ms);
1409 mdelay(delay_ms);
1410 ret = tegra_dc_dp_dpcd_read(dp, DP_SINK_STATUS, &dpcd_data);
1411 if (ret)
1412 return ret;
1413
1414 out_of_sync = !(dpcd_data & DP_SINK_STATUS_PORT0_IN_SYNC);
1415 if (out_of_sync)
1416 debug("SINK receive port 0 out of sync, data=%x\n", dpcd_data);
1417 else
1418 debug("SINK is in synchronization\n");
1419
1420 return out_of_sync;
1421}
1422
1423static int tegra_dc_dp_check_sink(struct tegra_dp_priv *dp,
1424 struct tegra_dp_link_config *link_cfg,
1425 const struct display_timing *timing)
1426{
1427 const int max_retry = 5;
1428 int delay_frame;
1429 int retries;
1430
1431 /*
1432 * DP TCON may skip some main stream frames, thus we need to wait
1433 * some delay before reading the DPCD SINK STATUS register, starting
1434 * from 5
1435 */
1436 delay_frame = 5;
1437
1438 retries = max_retry;
1439 do {
1440 int ret;
1441
1442 if (!tegra_dc_dp_sink_out_of_sync(dp, link_cfg->frame_in_ms *
1443 delay_frame))
1444 return 0;
1445
1446 debug("%s: retries left %d\n", __func__, retries);
1447 if (!retries--) {
1448 printf("DP: Out of sync after %d retries\n", max_retry);
1449 return -EIO;
1450 }
Simon Glassfad72182016-01-30 16:37:50 -07001451 ret = tegra_dc_sor_detach(dp->dc_dev, dp->sor);
Simon Glass662f2aa2015-04-14 21:03:44 -06001452 if (ret)
1453 return ret;
1454 if (tegra_dc_dp_explore_link_cfg(dp, link_cfg, dp->sor,
1455 timing)) {
1456 debug("dp: %s: error to configure link\n", __func__);
1457 continue;
1458 }
1459
1460 tegra_dc_sor_set_power_state(dp->sor, 1);
Simon Glassfad72182016-01-30 16:37:50 -07001461 tegra_dc_sor_attach(dp->dc_dev, dp->sor, link_cfg, timing);
Simon Glass662f2aa2015-04-14 21:03:44 -06001462
1463 /* Increase delay_frame for next try in case the sink is
1464 skipping more frames */
1465 delay_frame += 10;
1466 } while (1);
1467}
1468
Simon Glassf15fe612015-04-14 21:03:41 -06001469int tegra_dp_enable(struct udevice *dev, int panel_bpp,
1470 const struct display_timing *timing)
1471{
1472 struct tegra_dp_priv *priv = dev_get_priv(dev);
1473 struct tegra_dp_link_config slink_cfg, *link_cfg = &slink_cfg;
Simon Glassfad72182016-01-30 16:37:50 -07001474 struct udevice *sor;
Simon Glassf15fe612015-04-14 21:03:41 -06001475 int data;
1476 int retry;
1477 int ret;
1478
1479 memset(link_cfg, '\0', sizeof(*link_cfg));
1480 link_cfg->is_valid = 0;
1481 link_cfg->scramble_ena = 1;
1482
1483 tegra_dc_dpaux_enable(priv);
1484
1485 if (tegra_dp_hpd_plug(priv) < 0) {
1486 debug("dp: hpd plug failed\n");
1487 return -EIO;
1488 }
1489
1490 link_cfg->bits_per_pixel = panel_bpp;
1491 if (tegra_dc_dp_init_max_link_cfg(timing, priv, link_cfg)) {
1492 debug("dp: failed to init link configuration\n");
1493 return -ENOLINK;
1494 }
1495
Michal Suchanekac12a2f2022-10-12 21:57:59 +02001496 ret = uclass_first_device_err(UCLASS_VIDEO_BRIDGE, &sor);
1497 if (ret) {
Simon Glassfad72182016-01-30 16:37:50 -07001498 debug("dp: failed to find SOR device: ret=%d\n", ret);
Simon Glassf15fe612015-04-14 21:03:41 -06001499 return ret;
Simon Glassfad72182016-01-30 16:37:50 -07001500 }
Simon Glassf15fe612015-04-14 21:03:41 -06001501 priv->sor = sor;
1502 ret = tegra_dc_sor_enable_dp(sor, link_cfg);
1503 if (ret)
1504 return ret;
1505
1506 tegra_dc_sor_set_panel_power(sor, 1);
1507
1508 /* Write power on to DPCD */
1509 data = DP_SET_POWER_D0;
1510 retry = 0;
1511 do {
1512 ret = tegra_dc_dp_dpcd_write(priv, DP_SET_POWER, data);
1513 } while ((retry++ < DP_POWER_ON_MAX_TRIES) && ret);
1514
1515 if (ret || retry >= DP_POWER_ON_MAX_TRIES) {
1516 debug("dp: failed to power on panel (0x%x)\n", ret);
1517 return -ENETUNREACH;
1518 goto error_enable;
1519 }
1520
1521 /* Confirm DP plugging status */
1522 if (!(tegra_dpaux_readl(priv, DPAUX_DP_AUXSTAT) &
1523 DPAUX_DP_AUXSTAT_HPD_STATUS_PLUGGED)) {
1524 debug("dp: could not detect HPD\n");
1525 return -ENXIO;
1526 }
1527
1528 /* Check DP version */
1529 if (tegra_dc_dp_dpcd_read(priv, DP_DPCD_REV, &priv->revision)) {
1530 debug("dp: failed to read the revision number from sink\n");
1531 return -EIO;
1532 }
1533
1534 if (tegra_dc_dp_explore_link_cfg(priv, link_cfg, sor, timing)) {
1535 debug("dp: error configuring link\n");
1536 return -ENOMEDIUM;
1537 }
1538
1539 tegra_dc_sor_set_power_state(sor, 1);
Simon Glassfad72182016-01-30 16:37:50 -07001540 ret = tegra_dc_sor_attach(priv->dc_dev, sor, link_cfg, timing);
Simon Glassf15fe612015-04-14 21:03:41 -06001541 if (ret && ret != -EEXIST)
1542 return ret;
1543
Simon Glass662f2aa2015-04-14 21:03:44 -06001544 /*
1545 * This takes a long time, but can apparently resolve a failure to
1546 * bring up the display correctly.
1547 */
1548 if (0) {
1549 ret = tegra_dc_dp_check_sink(priv, link_cfg, timing);
1550 if (ret)
1551 return ret;
1552 }
1553
Simon Glassf15fe612015-04-14 21:03:41 -06001554 /* Power down the unused lanes to save power - a few hundred mW */
1555 tegra_dc_sor_power_down_unused_lanes(sor, link_cfg);
1556
Simon Glassfad72182016-01-30 16:37:50 -07001557 ret = video_bridge_set_backlight(sor, 80);
1558 if (ret) {
1559 debug("dp: failed to set backlight\n");
1560 return ret;
1561 }
1562
Simon Glassf15fe612015-04-14 21:03:41 -06001563 priv->enabled = true;
1564error_enable:
1565 return 0;
1566}
1567
Simon Glassaad29ae2020-12-03 16:55:21 -07001568static int tegra_dp_of_to_plat(struct udevice *dev)
Simon Glassf15fe612015-04-14 21:03:41 -06001569{
Simon Glassfa20e932020-12-03 16:55:20 -07001570 struct tegra_dp_plat *plat = dev_get_plat(dev);
Simon Glassf15fe612015-04-14 21:03:41 -06001571
Simon Glass5eb75402017-07-25 08:30:01 -06001572 plat->base = dev_read_addr(dev);
Simon Glassf15fe612015-04-14 21:03:41 -06001573
1574 return 0;
1575}
1576
1577static int tegra_dp_read_edid(struct udevice *dev, u8 *buf, int buf_size)
1578{
1579 struct tegra_dp_priv *priv = dev_get_priv(dev);
1580 const int tegra_edid_i2c_address = 0x50;
1581 u32 aux_stat = 0;
1582
1583 tegra_dc_dpaux_enable(priv);
1584
1585 return tegra_dc_i2c_aux_read(priv, tegra_edid_i2c_address, 0, buf,
1586 buf_size, &aux_stat);
1587}
1588
Simon Glass7d3d7762016-01-21 19:45:00 -07001589static const struct dm_display_ops dp_tegra_ops = {
Simon Glassf15fe612015-04-14 21:03:41 -06001590 .read_edid = tegra_dp_read_edid,
1591 .enable = tegra_dp_enable,
1592};
1593
1594static int dp_tegra_probe(struct udevice *dev)
1595{
Simon Glassfa20e932020-12-03 16:55:20 -07001596 struct tegra_dp_plat *plat = dev_get_plat(dev);
Simon Glassf15fe612015-04-14 21:03:41 -06001597 struct tegra_dp_priv *priv = dev_get_priv(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -07001598 struct display_plat *disp_uc_plat = dev_get_uclass_plat(dev);
Simon Glassf15fe612015-04-14 21:03:41 -06001599
1600 priv->regs = (struct dpaux_ctlr *)plat->base;
1601 priv->enabled = false;
1602
Simon Glassfad72182016-01-30 16:37:50 -07001603 /* Remember the display controller that is sending us video */
1604 priv->dc_dev = disp_uc_plat->src_dev;
1605
Simon Glassf15fe612015-04-14 21:03:41 -06001606 return 0;
1607}
1608
1609static const struct udevice_id tegra_dp_ids[] = {
1610 { .compatible = "nvidia,tegra124-dpaux" },
Peter Robinson85ccc372022-05-03 09:32:54 +01001611 { .compatible = "nvidia,tegra210-dpaux" },
Simon Glassf15fe612015-04-14 21:03:41 -06001612 { }
1613};
1614
1615U_BOOT_DRIVER(dp_tegra) = {
1616 .name = "dpaux_tegra",
Simon Glass7d3d7762016-01-21 19:45:00 -07001617 .id = UCLASS_DISPLAY,
Simon Glassf15fe612015-04-14 21:03:41 -06001618 .of_match = tegra_dp_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001619 .of_to_plat = tegra_dp_of_to_plat,
Simon Glassf15fe612015-04-14 21:03:41 -06001620 .probe = dp_tegra_probe,
1621 .ops = &dp_tegra_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001622 .priv_auto = sizeof(struct tegra_dp_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001623 .plat_auto = sizeof(struct tegra_dp_plat),
Simon Glassf15fe612015-04-14 21:03:41 -06001624};