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