blob: c5039e7b430c7e050c9741feba2335442e8562fb [file] [log] [blame]
Donghwa Leed2a69822012-07-02 01:16:02 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 *
4 * Author: Donghwa Lee <dh09.lee@samsung.com>
5 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02006 * SPDX-License-Identifier: GPL-2.0+
Donghwa Leed2a69822012-07-02 01:16:02 +00007 */
8
9#include <config.h>
Simon Glassbb5930d2016-02-21 21:09:01 -070010#include <dm.h>
Donghwa Leed2a69822012-07-02 01:16:02 +000011#include <common.h>
Simon Glassbb5930d2016-02-21 21:09:01 -070012#include <display.h>
13#include <fdtdec.h>
14#include <libfdt.h>
Donghwa Leed2a69822012-07-02 01:16:02 +000015#include <malloc.h>
Simon Glassbb5930d2016-02-21 21:09:01 -070016#include <video_bridge.h>
Heiko Schocher0c06db52014-06-24 10:10:03 +020017#include <linux/compat.h>
Donghwa Leed2a69822012-07-02 01:16:02 +000018#include <linux/err.h>
19#include <asm/arch/clk.h>
20#include <asm/arch/cpu.h>
21#include <asm/arch/dp_info.h>
22#include <asm/arch/dp.h>
Simon Glassbb5930d2016-02-21 21:09:01 -070023#include <asm/arch/pinmux.h>
Simon Glass7eb860d2016-02-21 21:08:57 -070024#include <asm/arch/power.h>
Donghwa Leed2a69822012-07-02 01:16:02 +000025
26#include "exynos_dp_lowlevel.h"
27
Ajay Kumar9947d132013-02-21 23:53:06 +000028DECLARE_GLOBAL_DATA_PTR;
29
Donghwa Leed2a69822012-07-02 01:16:02 +000030static void exynos_dp_disp_info(struct edp_disp_info *disp_info)
31{
32 disp_info->h_total = disp_info->h_res + disp_info->h_sync_width +
33 disp_info->h_back_porch + disp_info->h_front_porch;
34 disp_info->v_total = disp_info->v_res + disp_info->v_sync_width +
35 disp_info->v_back_porch + disp_info->v_front_porch;
36
37 return;
38}
39
Simon Glass8b449a62016-02-21 21:09:00 -070040static int exynos_dp_init_dp(struct exynos_dp *regs)
Donghwa Leed2a69822012-07-02 01:16:02 +000041{
42 int ret;
Simon Glass8b449a62016-02-21 21:09:00 -070043 exynos_dp_reset(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +000044
45 /* SW defined function Normal operation */
Simon Glass8b449a62016-02-21 21:09:00 -070046 exynos_dp_enable_sw_func(regs, DP_ENABLE);
Donghwa Leed2a69822012-07-02 01:16:02 +000047
Simon Glass8b449a62016-02-21 21:09:00 -070048 ret = exynos_dp_init_analog_func(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +000049 if (ret != EXYNOS_DP_SUCCESS)
50 return ret;
51
Simon Glass8b449a62016-02-21 21:09:00 -070052 exynos_dp_init_hpd(regs);
53 exynos_dp_init_aux(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +000054
55 return ret;
56}
57
58static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
59{
60 int i;
61 unsigned char sum = 0;
62
63 for (i = 0; i < EDID_BLOCK_LENGTH; i++)
64 sum = sum + edid_data[i];
65
66 return sum;
67}
68
Simon Glass8b449a62016-02-21 21:09:00 -070069static unsigned int exynos_dp_read_edid(struct exynos_dp *regs)
Donghwa Leed2a69822012-07-02 01:16:02 +000070{
71 unsigned char edid[EDID_BLOCK_LENGTH * 2];
72 unsigned int extend_block = 0;
73 unsigned char sum;
74 unsigned char test_vector;
75 int retval;
76
77 /*
78 * EDID device address is 0x50.
79 * However, if necessary, you must have set upper address
80 * into E-EDID in I2C device, 0x30.
81 */
82
83 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
Simon Glass8b449a62016-02-21 21:09:00 -070084 exynos_dp_read_byte_from_i2c(regs, I2C_EDID_DEVICE_ADDR,
Simon Glass8c9b8dc2016-02-21 21:08:44 -070085 EDID_EXTENSION_FLAG, &extend_block);
Donghwa Leed2a69822012-07-02 01:16:02 +000086
87 if (extend_block > 0) {
88 printf("DP EDID data includes a single extension!\n");
89
90 /* Read EDID data */
Simon Glass8b449a62016-02-21 21:09:00 -070091 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -070092 I2C_EDID_DEVICE_ADDR,
Donghwa Leed2a69822012-07-02 01:16:02 +000093 EDID_HEADER_PATTERN,
94 EDID_BLOCK_LENGTH,
95 &edid[EDID_HEADER_PATTERN]);
96 if (retval != 0) {
97 printf("DP EDID Read failed!\n");
98 return -1;
99 }
100 sum = exynos_dp_calc_edid_check_sum(edid);
101 if (sum != 0) {
102 printf("DP EDID bad checksum!\n");
103 return -1;
104 }
105
106 /* Read additional EDID data */
Simon Glass8b449a62016-02-21 21:09:00 -0700107 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700108 I2C_EDID_DEVICE_ADDR,
Donghwa Leed2a69822012-07-02 01:16:02 +0000109 EDID_BLOCK_LENGTH,
110 EDID_BLOCK_LENGTH,
111 &edid[EDID_BLOCK_LENGTH]);
112 if (retval != 0) {
113 printf("DP EDID Read failed!\n");
114 return -1;
115 }
116 sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
117 if (sum != 0) {
118 printf("DP EDID bad checksum!\n");
119 return -1;
120 }
121
Simon Glass8b449a62016-02-21 21:09:00 -0700122 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700123 &test_vector);
Donghwa Leed2a69822012-07-02 01:16:02 +0000124 if (test_vector & DPCD_TEST_EDID_READ) {
Simon Glass8b449a62016-02-21 21:09:00 -0700125 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700126 DPCD_TEST_EDID_CHECKSUM,
Donghwa Leed2a69822012-07-02 01:16:02 +0000127 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
Simon Glass8b449a62016-02-21 21:09:00 -0700128 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700129 DPCD_TEST_RESPONSE,
Donghwa Leed2a69822012-07-02 01:16:02 +0000130 DPCD_TEST_EDID_CHECKSUM_WRITE);
131 }
132 } else {
133 debug("DP EDID data does not include any extensions.\n");
134
135 /* Read EDID data */
Simon Glass8b449a62016-02-21 21:09:00 -0700136 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700137 I2C_EDID_DEVICE_ADDR,
Donghwa Leed2a69822012-07-02 01:16:02 +0000138 EDID_HEADER_PATTERN,
139 EDID_BLOCK_LENGTH,
140 &edid[EDID_HEADER_PATTERN]);
141
142 if (retval != 0) {
143 printf("DP EDID Read failed!\n");
144 return -1;
145 }
146 sum = exynos_dp_calc_edid_check_sum(edid);
147 if (sum != 0) {
148 printf("DP EDID bad checksum!\n");
149 return -1;
150 }
151
Simon Glass8b449a62016-02-21 21:09:00 -0700152 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Donghwa Leed2a69822012-07-02 01:16:02 +0000153 &test_vector);
154 if (test_vector & DPCD_TEST_EDID_READ) {
Simon Glass8b449a62016-02-21 21:09:00 -0700155 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700156 DPCD_TEST_EDID_CHECKSUM, edid[EDID_CHECKSUM]);
Simon Glass8b449a62016-02-21 21:09:00 -0700157 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700158 DPCD_TEST_RESPONSE,
Donghwa Leed2a69822012-07-02 01:16:02 +0000159 DPCD_TEST_EDID_CHECKSUM_WRITE);
160 }
161 }
162
163 debug("DP EDID Read success!\n");
164
165 return 0;
166}
167
Simon Glass8b449a62016-02-21 21:09:00 -0700168static unsigned int exynos_dp_handle_edid(struct exynos_dp *regs,
169 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000170{
171 unsigned char buf[12];
172 unsigned int ret;
173 unsigned char temp;
174 unsigned char retry_cnt;
175 unsigned char dpcd_rev[16];
176 unsigned char lane_bw[16];
177 unsigned char lane_cnt[16];
178
179 memset(dpcd_rev, 0, 16);
180 memset(lane_bw, 0, 16);
181 memset(lane_cnt, 0, 16);
182 memset(buf, 0, 12);
183
184 retry_cnt = 5;
185 while (retry_cnt) {
186 /* Read DPCD 0x0000-0x000b */
Simon Glass8b449a62016-02-21 21:09:00 -0700187 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_DPCD_REV, 12,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700188 buf);
Donghwa Leed2a69822012-07-02 01:16:02 +0000189 if (ret != EXYNOS_DP_SUCCESS) {
190 if (retry_cnt == 0) {
191 printf("DP read_byte_from_dpcd() failed\n");
192 return ret;
193 }
194 retry_cnt--;
195 } else
196 break;
197 }
198
199 /* */
200 temp = buf[DPCD_DPCD_REV];
201 if (temp == DP_DPCD_REV_10 || temp == DP_DPCD_REV_11)
Simon Glass8b449a62016-02-21 21:09:00 -0700202 priv->dpcd_rev = temp;
Donghwa Leed2a69822012-07-02 01:16:02 +0000203 else {
204 printf("DP Wrong DPCD Rev : %x\n", temp);
205 return -ENODEV;
206 }
207
208 temp = buf[DPCD_MAX_LINK_RATE];
209 if (temp == DP_LANE_BW_1_62 || temp == DP_LANE_BW_2_70)
Simon Glass8b449a62016-02-21 21:09:00 -0700210 priv->lane_bw = temp;
Donghwa Leed2a69822012-07-02 01:16:02 +0000211 else {
212 printf("DP Wrong MAX LINK RATE : %x\n", temp);
213 return -EINVAL;
214 }
215
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500216 /* Refer VESA Display Port Standard Ver1.1a Page 120 */
Simon Glass8b449a62016-02-21 21:09:00 -0700217 if (priv->dpcd_rev == DP_DPCD_REV_11) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000218 temp = buf[DPCD_MAX_LANE_COUNT] & 0x1f;
219 if (buf[DPCD_MAX_LANE_COUNT] & 0x80)
Simon Glass8b449a62016-02-21 21:09:00 -0700220 priv->dpcd_efc = 1;
Donghwa Leed2a69822012-07-02 01:16:02 +0000221 else
Simon Glass8b449a62016-02-21 21:09:00 -0700222 priv->dpcd_efc = 0;
Donghwa Leed2a69822012-07-02 01:16:02 +0000223 } else {
224 temp = buf[DPCD_MAX_LANE_COUNT];
Simon Glass8b449a62016-02-21 21:09:00 -0700225 priv->dpcd_efc = 0;
Donghwa Leed2a69822012-07-02 01:16:02 +0000226 }
227
228 if (temp == DP_LANE_CNT_1 || temp == DP_LANE_CNT_2 ||
229 temp == DP_LANE_CNT_4) {
Simon Glass8b449a62016-02-21 21:09:00 -0700230 priv->lane_cnt = temp;
Donghwa Leed2a69822012-07-02 01:16:02 +0000231 } else {
232 printf("DP Wrong MAX LANE COUNT : %x\n", temp);
233 return -EINVAL;
234 }
235
Simon Glass8b449a62016-02-21 21:09:00 -0700236 ret = exynos_dp_read_edid(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000237 if (ret != EXYNOS_DP_SUCCESS) {
238 printf("DP exynos_dp_read_edid() failed\n");
239 return -EINVAL;
240 }
241
242 return ret;
243}
244
Simon Glass8b449a62016-02-21 21:09:00 -0700245static void exynos_dp_init_training(struct exynos_dp *regs)
Donghwa Leed2a69822012-07-02 01:16:02 +0000246{
247 /*
248 * MACRO_RST must be applied after the PLL_LOCK to avoid
249 * the DP inter pair skew issue for at least 10 us
250 */
Simon Glass8b449a62016-02-21 21:09:00 -0700251 exynos_dp_reset_macro(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000252
253 /* All DP analog module power up */
Simon Glass8b449a62016-02-21 21:09:00 -0700254 exynos_dp_set_analog_power_down(regs, POWER_ALL, 0);
Donghwa Leed2a69822012-07-02 01:16:02 +0000255}
256
Simon Glass8b449a62016-02-21 21:09:00 -0700257static unsigned int exynos_dp_link_start(struct exynos_dp *regs,
258 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000259{
260 unsigned char buf[5];
261 unsigned int ret = 0;
262
263 debug("DP: %s was called\n", __func__);
264
Simon Glass8b449a62016-02-21 21:09:00 -0700265 priv->lt_info.lt_status = DP_LT_CR;
266 priv->lt_info.ep_loop = 0;
267 priv->lt_info.cr_loop[0] = 0;
268 priv->lt_info.cr_loop[1] = 0;
269 priv->lt_info.cr_loop[2] = 0;
270 priv->lt_info.cr_loop[3] = 0;
Donghwa Leed2a69822012-07-02 01:16:02 +0000271
272 /* Set sink to D0 (Sink Not Ready) mode. */
Simon Glass8b449a62016-02-21 21:09:00 -0700273 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_SINK_POWER_STATE,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700274 DPCD_SET_POWER_STATE_D0);
Donghwa Leed2a69822012-07-02 01:16:02 +0000275 if (ret != EXYNOS_DP_SUCCESS) {
276 printf("DP write_dpcd_byte failed\n");
277 return ret;
278 }
279
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500280 /* Set link rate and count as you want to establish */
Simon Glass8b449a62016-02-21 21:09:00 -0700281 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
282 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leed2a69822012-07-02 01:16:02 +0000283
284 /* Setup RX configuration */
Simon Glass8b449a62016-02-21 21:09:00 -0700285 buf[0] = priv->lane_bw;
286 buf[1] = priv->lane_cnt;
Donghwa Leed2a69822012-07-02 01:16:02 +0000287
Simon Glass8b449a62016-02-21 21:09:00 -0700288 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_LINK_BW_SET, 2, buf);
Donghwa Leed2a69822012-07-02 01:16:02 +0000289 if (ret != EXYNOS_DP_SUCCESS) {
290 printf("DP write_dpcd_byte failed\n");
291 return ret;
292 }
293
Simon Glass8b449a62016-02-21 21:09:00 -0700294 exynos_dp_set_lane_pre_emphasis(regs, PRE_EMPHASIS_LEVEL_0,
295 priv->lane_cnt);
Donghwa Leed2a69822012-07-02 01:16:02 +0000296
297 /* Set training pattern 1 */
Simon Glass8b449a62016-02-21 21:09:00 -0700298 exynos_dp_set_training_pattern(regs, TRAINING_PTN1);
Donghwa Leed2a69822012-07-02 01:16:02 +0000299
300 /* Set RX training pattern */
301 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_1;
302
303 buf[1] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
304 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
305 buf[2] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
306 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
307 buf[3] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
308 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
309 buf[4] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
310 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
311
Simon Glass8b449a62016-02-21 21:09:00 -0700312 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700313 5, buf);
Donghwa Leed2a69822012-07-02 01:16:02 +0000314 if (ret != EXYNOS_DP_SUCCESS) {
315 printf("DP write_dpcd_byte failed\n");
316 return ret;
317 }
318
319 return ret;
320}
321
Simon Glass8b449a62016-02-21 21:09:00 -0700322static unsigned int exynos_dp_training_pattern_dis(struct exynos_dp *regs)
Donghwa Leed2a69822012-07-02 01:16:02 +0000323{
324 unsigned int ret = EXYNOS_DP_SUCCESS;
325
Simon Glass8b449a62016-02-21 21:09:00 -0700326 exynos_dp_set_training_pattern(regs, DP_NONE);
Donghwa Leed2a69822012-07-02 01:16:02 +0000327
Simon Glass8b449a62016-02-21 21:09:00 -0700328 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700329 DPCD_TRAINING_PATTERN_DISABLED);
Donghwa Leed2a69822012-07-02 01:16:02 +0000330 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500331 printf("DP request_link_training_req failed\n");
Donghwa Leed2a69822012-07-02 01:16:02 +0000332 return -EAGAIN;
333 }
334
335 return ret;
336}
337
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700338static unsigned int exynos_dp_enable_rx_to_enhanced_mode(
Simon Glass8b449a62016-02-21 21:09:00 -0700339 struct exynos_dp *regs, unsigned char enable)
Donghwa Leed2a69822012-07-02 01:16:02 +0000340{
341 unsigned char data;
342 unsigned int ret = EXYNOS_DP_SUCCESS;
343
Simon Glass8b449a62016-02-21 21:09:00 -0700344 ret = exynos_dp_read_byte_from_dpcd(regs, DPCD_LANE_COUNT_SET,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700345 &data);
Donghwa Leed2a69822012-07-02 01:16:02 +0000346 if (ret != EXYNOS_DP_SUCCESS) {
347 printf("DP read_from_dpcd failed\n");
348 return -EAGAIN;
349 }
350
351 if (enable)
352 data = DPCD_ENHANCED_FRAME_EN | DPCD_LN_COUNT_SET(data);
353 else
354 data = DPCD_LN_COUNT_SET(data);
355
Simon Glass8b449a62016-02-21 21:09:00 -0700356 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_LANE_COUNT_SET, data);
Donghwa Leed2a69822012-07-02 01:16:02 +0000357 if (ret != EXYNOS_DP_SUCCESS) {
358 printf("DP write_to_dpcd failed\n");
359 return -EAGAIN;
360
361 }
362
363 return ret;
364}
365
Simon Glass8b449a62016-02-21 21:09:00 -0700366static unsigned int exynos_dp_set_enhanced_mode(struct exynos_dp *regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700367 unsigned char enhance_mode)
Donghwa Leed2a69822012-07-02 01:16:02 +0000368{
369 unsigned int ret = EXYNOS_DP_SUCCESS;
370
Simon Glass8b449a62016-02-21 21:09:00 -0700371 ret = exynos_dp_enable_rx_to_enhanced_mode(regs, enhance_mode);
Donghwa Leed2a69822012-07-02 01:16:02 +0000372 if (ret != EXYNOS_DP_SUCCESS) {
373 printf("DP rx_enhance_mode failed\n");
374 return -EAGAIN;
375 }
376
Simon Glass8b449a62016-02-21 21:09:00 -0700377 exynos_dp_enable_enhanced_mode(regs, enhance_mode);
Donghwa Leed2a69822012-07-02 01:16:02 +0000378
379 return ret;
380}
381
Simon Glass8b449a62016-02-21 21:09:00 -0700382static int exynos_dp_read_dpcd_lane_stat(struct exynos_dp *regs,
383 struct exynos_dp_priv *priv,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700384 unsigned char *status)
Donghwa Leed2a69822012-07-02 01:16:02 +0000385{
386 unsigned int ret, i;
387 unsigned char buf[2];
388 unsigned char lane_stat[DP_LANE_CNT_4] = {0,};
389 unsigned char shift_val[DP_LANE_CNT_4] = {0,};
390
391 shift_val[0] = 0;
392 shift_val[1] = 4;
393 shift_val[2] = 0;
394 shift_val[3] = 4;
395
Simon Glass8b449a62016-02-21 21:09:00 -0700396 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_LANE0_1_STATUS, 2,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700397 buf);
Donghwa Leed2a69822012-07-02 01:16:02 +0000398 if (ret != EXYNOS_DP_SUCCESS) {
399 printf("DP read lane status failed\n");
400 return ret;
401 }
402
Simon Glass8b449a62016-02-21 21:09:00 -0700403 for (i = 0; i < priv->lane_cnt; i++) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000404 lane_stat[i] = (buf[(i / 2)] >> shift_val[i]) & 0x0f;
405 if (lane_stat[0] != lane_stat[i]) {
406 printf("Wrong lane status\n");
407 return -EINVAL;
408 }
409 }
410
411 *status = lane_stat[0];
412
413 return ret;
414}
415
Simon Glass8b449a62016-02-21 21:09:00 -0700416static unsigned int exynos_dp_read_dpcd_adj_req(struct exynos_dp *regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700417 unsigned char lane_num, unsigned char *sw, unsigned char *em)
Donghwa Leed2a69822012-07-02 01:16:02 +0000418{
419 unsigned int ret = EXYNOS_DP_SUCCESS;
420 unsigned char buf;
421 unsigned int dpcd_addr;
422 unsigned char shift_val[DP_LANE_CNT_4] = {0, 4, 0, 4};
423
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500424 /* lane_num value is used as array index, so this range 0 ~ 3 */
Donghwa Leed2a69822012-07-02 01:16:02 +0000425 dpcd_addr = DPCD_ADJUST_REQUEST_LANE0_1 + (lane_num / 2);
426
Simon Glass8b449a62016-02-21 21:09:00 -0700427 ret = exynos_dp_read_byte_from_dpcd(regs, dpcd_addr, &buf);
Donghwa Leed2a69822012-07-02 01:16:02 +0000428 if (ret != EXYNOS_DP_SUCCESS) {
429 printf("DP read adjust request failed\n");
430 return -EAGAIN;
431 }
432
433 *sw = ((buf >> shift_val[lane_num]) & 0x03);
434 *em = ((buf >> shift_val[lane_num]) & 0x0c) >> 2;
435
436 return ret;
437}
438
Simon Glass8b449a62016-02-21 21:09:00 -0700439static int exynos_dp_equalizer_err_link(struct exynos_dp *regs,
440 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000441{
442 int ret;
443
Simon Glass8b449a62016-02-21 21:09:00 -0700444 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000445 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500446 printf("DP training_pattern_disable() failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700447 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000448 }
449
Simon Glass8b449a62016-02-21 21:09:00 -0700450 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leed2a69822012-07-02 01:16:02 +0000451 if (ret != EXYNOS_DP_SUCCESS) {
452 printf("DP set_enhanced_mode() failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700453 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000454 }
455
456 return ret;
457}
458
Simon Glass8b449a62016-02-21 21:09:00 -0700459static int exynos_dp_reduce_link_rate(struct exynos_dp *regs,
460 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000461{
462 int ret;
463
Simon Glass8b449a62016-02-21 21:09:00 -0700464 if (priv->lane_bw == DP_LANE_BW_2_70) {
465 priv->lane_bw = DP_LANE_BW_1_62;
Donghwa Leed2a69822012-07-02 01:16:02 +0000466 printf("DP Change lane bw to 1.62Gbps\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700467 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leed2a69822012-07-02 01:16:02 +0000468 ret = EXYNOS_DP_SUCCESS;
469 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700470 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000471 if (ret != EXYNOS_DP_SUCCESS)
472 printf("DP training_patter_disable() failed\n");
473
Simon Glass8b449a62016-02-21 21:09:00 -0700474 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leed2a69822012-07-02 01:16:02 +0000475 if (ret != EXYNOS_DP_SUCCESS)
476 printf("DP set_enhanced_mode() failed\n");
477
Simon Glass8b449a62016-02-21 21:09:00 -0700478 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000479 }
480
481 return ret;
482}
483
Simon Glass8b449a62016-02-21 21:09:00 -0700484static unsigned int exynos_dp_process_clock_recovery(struct exynos_dp *regs,
485 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000486{
487 unsigned int ret = EXYNOS_DP_SUCCESS;
488 unsigned char lane_stat;
489 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0, };
490 unsigned int i;
491 unsigned char adj_req_sw;
492 unsigned char adj_req_em;
493 unsigned char buf[5];
494
495 debug("DP: %s was called\n", __func__);
496 mdelay(1);
497
Simon Glass8b449a62016-02-21 21:09:00 -0700498 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leed2a69822012-07-02 01:16:02 +0000499 if (ret != EXYNOS_DP_SUCCESS) {
500 printf("DP read lane status failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700501 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000502 return ret;
503 }
504
505 if (lane_stat & DP_LANE_STAT_CR_DONE) {
506 debug("DP clock Recovery training succeed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700507 exynos_dp_set_training_pattern(regs, TRAINING_PTN2);
Donghwa Leed2a69822012-07-02 01:16:02 +0000508
Simon Glass8b449a62016-02-21 21:09:00 -0700509 for (i = 0; i < priv->lane_cnt; i++) {
510 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700511 &adj_req_sw, &adj_req_em);
Donghwa Leed2a69822012-07-02 01:16:02 +0000512 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass8b449a62016-02-21 21:09:00 -0700513 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000514 return ret;
515 }
516
517 lt_ctl_val[i] = 0;
518 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
519
520 if ((adj_req_sw == VOLTAGE_LEVEL_3)
521 || (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
522 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
523 MAX_PRE_EMPHASIS_REACH_3;
524 }
Simon Glass8b449a62016-02-21 21:09:00 -0700525 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700526 lt_ctl_val[i], i);
Donghwa Leed2a69822012-07-02 01:16:02 +0000527 }
528
529 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_2;
530 buf[1] = lt_ctl_val[0];
531 buf[2] = lt_ctl_val[1];
532 buf[3] = lt_ctl_val[2];
533 buf[4] = lt_ctl_val[3];
534
Simon Glass8b449a62016-02-21 21:09:00 -0700535 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leed2a69822012-07-02 01:16:02 +0000536 DPCD_TRAINING_PATTERN_SET, 5, buf);
537 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500538 printf("DP write training pattern1 failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700539 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000540 return ret;
541 } else
Simon Glass8b449a62016-02-21 21:09:00 -0700542 priv->lt_info.lt_status = DP_LT_ET;
Donghwa Leed2a69822012-07-02 01:16:02 +0000543 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700544 for (i = 0; i < priv->lane_cnt; i++) {
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700545 lt_ctl_val[i] = exynos_dp_get_lanex_pre_emphasis(
Simon Glass8b449a62016-02-21 21:09:00 -0700546 regs, i);
547 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leed2a69822012-07-02 01:16:02 +0000548 &adj_req_sw, &adj_req_em);
549 if (ret != EXYNOS_DP_SUCCESS) {
550 printf("DP read adj req failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700551 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000552 return ret;
553 }
554
555 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
556 (adj_req_em == PRE_EMPHASIS_LEVEL_3))
Simon Glass8b449a62016-02-21 21:09:00 -0700557 ret = exynos_dp_reduce_link_rate(regs,
558 priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000559
560 if ((DRIVE_CURRENT_SET_0_GET(lt_ctl_val[i]) ==
561 adj_req_sw) &&
562 (PRE_EMPHASIS_SET_0_GET(lt_ctl_val[i]) ==
563 adj_req_em)) {
Simon Glass8b449a62016-02-21 21:09:00 -0700564 priv->lt_info.cr_loop[i]++;
565 if (priv->lt_info.cr_loop[i] == MAX_CR_LOOP)
Donghwa Leed2a69822012-07-02 01:16:02 +0000566 ret = exynos_dp_reduce_link_rate(
Simon Glass8b449a62016-02-21 21:09:00 -0700567 regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000568 }
569
570 lt_ctl_val[i] = 0;
571 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
572
573 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
574 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
575 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
576 MAX_PRE_EMPHASIS_REACH_3;
577 }
Simon Glass8b449a62016-02-21 21:09:00 -0700578 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700579 lt_ctl_val[i], i);
Donghwa Leed2a69822012-07-02 01:16:02 +0000580 }
581
Simon Glass8b449a62016-02-21 21:09:00 -0700582 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leed2a69822012-07-02 01:16:02 +0000583 DPCD_TRAINING_LANE0_SET, 4, lt_ctl_val);
584 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500585 printf("DP write training pattern2 failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700586 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000587 return ret;
588 }
589 }
590
591 return ret;
592}
593
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700594static unsigned int exynos_dp_process_equalizer_training(
Simon Glass8b449a62016-02-21 21:09:00 -0700595 struct exynos_dp *regs, struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000596{
597 unsigned int ret = EXYNOS_DP_SUCCESS;
598 unsigned char lane_stat, adj_req_sw, adj_req_em, i;
599 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0,};
600 unsigned char interlane_aligned = 0;
601 unsigned char f_bw;
602 unsigned char f_lane_cnt;
603 unsigned char sink_stat;
604
605 mdelay(1);
606
Simon Glass8b449a62016-02-21 21:09:00 -0700607 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leed2a69822012-07-02 01:16:02 +0000608 if (ret != EXYNOS_DP_SUCCESS) {
609 printf("DP read lane status failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700610 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000611 return ret;
612 }
613
614 debug("DP lane stat : %x\n", lane_stat);
615
616 if (lane_stat & DP_LANE_STAT_CR_DONE) {
Simon Glass8b449a62016-02-21 21:09:00 -0700617 ret = exynos_dp_read_byte_from_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700618 DPCD_LN_ALIGN_UPDATED,
619 &sink_stat);
Donghwa Leed2a69822012-07-02 01:16:02 +0000620 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass8b449a62016-02-21 21:09:00 -0700621 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000622
623 return ret;
624 }
625
626 interlane_aligned = (sink_stat & DPCD_INTERLANE_ALIGN_DONE);
627
Simon Glass8b449a62016-02-21 21:09:00 -0700628 for (i = 0; i < priv->lane_cnt; i++) {
629 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leed2a69822012-07-02 01:16:02 +0000630 &adj_req_sw, &adj_req_em);
631 if (ret != EXYNOS_DP_SUCCESS) {
632 printf("DP read adj req 1 failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700633 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leed2a69822012-07-02 01:16:02 +0000634
635 return ret;
636 }
637
638 lt_ctl_val[i] = 0;
639 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
640
641 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
642 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
643 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3;
644 lt_ctl_val[i] |= MAX_PRE_EMPHASIS_REACH_3;
645 }
646 }
647
648 if (((lane_stat&DP_LANE_STAT_CE_DONE) &&
649 (lane_stat&DP_LANE_STAT_SYM_LOCK))
650 && (interlane_aligned == DPCD_INTERLANE_ALIGN_DONE)) {
651 debug("DP Equalizer training succeed\n");
652
Simon Glass8b449a62016-02-21 21:09:00 -0700653 f_bw = exynos_dp_get_link_bandwidth(regs);
654 f_lane_cnt = exynos_dp_get_lane_count(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000655
656 debug("DP final BandWidth : %x\n", f_bw);
657 debug("DP final Lane Count : %x\n", f_lane_cnt);
658
Simon Glass8b449a62016-02-21 21:09:00 -0700659 priv->lt_info.lt_status = DP_LT_FINISHED;
Donghwa Leed2a69822012-07-02 01:16:02 +0000660
Simon Glass8b449a62016-02-21 21:09:00 -0700661 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000662
663 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700664 priv->lt_info.ep_loop++;
Donghwa Leed2a69822012-07-02 01:16:02 +0000665
Simon Glass8b449a62016-02-21 21:09:00 -0700666 if (priv->lt_info.ep_loop > MAX_EQ_LOOP) {
667 if (priv->lane_bw == DP_LANE_BW_2_70) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000668 ret = exynos_dp_reduce_link_rate(
Simon Glass8b449a62016-02-21 21:09:00 -0700669 regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000670 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700671 priv->lt_info.lt_status =
Donghwa Leed2a69822012-07-02 01:16:02 +0000672 DP_LT_FAIL;
Simon Glass8b449a62016-02-21 21:09:00 -0700673 exynos_dp_equalizer_err_link(regs,
674 priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000675 }
676 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700677 for (i = 0; i < priv->lane_cnt; i++)
Donghwa Leed2a69822012-07-02 01:16:02 +0000678 exynos_dp_set_lanex_pre_emphasis(
Simon Glass8b449a62016-02-21 21:09:00 -0700679 regs, lt_ctl_val[i], i);
Donghwa Leed2a69822012-07-02 01:16:02 +0000680
Simon Glass8b449a62016-02-21 21:09:00 -0700681 ret = exynos_dp_write_bytes_to_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700682 DPCD_TRAINING_LANE0_SET,
683 4, lt_ctl_val);
Donghwa Leed2a69822012-07-02 01:16:02 +0000684 if (ret != EXYNOS_DP_SUCCESS) {
685 printf("DP set lt pattern failed\n");
Simon Glass8b449a62016-02-21 21:09:00 -0700686 priv->lt_info.lt_status =
Donghwa Leed2a69822012-07-02 01:16:02 +0000687 DP_LT_FAIL;
Simon Glass8b449a62016-02-21 21:09:00 -0700688 exynos_dp_equalizer_err_link(regs,
689 priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000690 }
691 }
692 }
Simon Glass8b449a62016-02-21 21:09:00 -0700693 } else if (priv->lane_bw == DP_LANE_BW_2_70) {
694 ret = exynos_dp_reduce_link_rate(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000695 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700696 priv->lt_info.lt_status = DP_LT_FAIL;
697 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000698 }
699
700 return ret;
701}
702
Simon Glass8b449a62016-02-21 21:09:00 -0700703static unsigned int exynos_dp_sw_link_training(struct exynos_dp *regs,
704 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000705{
706 unsigned int ret = 0;
707 int training_finished;
708
709 /* Turn off unnecessary lane */
Simon Glass8b449a62016-02-21 21:09:00 -0700710 if (priv->lane_cnt == 1)
711 exynos_dp_set_analog_power_down(regs, CH1_BLOCK, 1);
Donghwa Leed2a69822012-07-02 01:16:02 +0000712
713 training_finished = 0;
714
Simon Glass8b449a62016-02-21 21:09:00 -0700715 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leed2a69822012-07-02 01:16:02 +0000716
717 /* Process here */
718 while (!training_finished) {
Simon Glass8b449a62016-02-21 21:09:00 -0700719 switch (priv->lt_info.lt_status) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000720 case DP_LT_START:
Simon Glass8b449a62016-02-21 21:09:00 -0700721 ret = exynos_dp_link_start(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000722 if (ret != EXYNOS_DP_SUCCESS) {
723 printf("DP LT:link start failed\n");
724 return ret;
725 }
726 break;
727 case DP_LT_CR:
Simon Glass8b449a62016-02-21 21:09:00 -0700728 ret = exynos_dp_process_clock_recovery(regs,
729 priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000730 if (ret != EXYNOS_DP_SUCCESS) {
731 printf("DP LT:clock recovery failed\n");
732 return ret;
733 }
734 break;
735 case DP_LT_ET:
Simon Glass8b449a62016-02-21 21:09:00 -0700736 ret = exynos_dp_process_equalizer_training(regs,
737 priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000738 if (ret != EXYNOS_DP_SUCCESS) {
739 printf("DP LT:equalizer training failed\n");
740 return ret;
741 }
742 break;
743 case DP_LT_FINISHED:
744 training_finished = 1;
745 break;
746 case DP_LT_FAIL:
747 return -1;
748 }
749 }
750
751 return ret;
752}
753
Simon Glass8b449a62016-02-21 21:09:00 -0700754static unsigned int exynos_dp_set_link_train(struct exynos_dp *regs,
755 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000756{
757 unsigned int ret;
758
Simon Glass8b449a62016-02-21 21:09:00 -0700759 exynos_dp_init_training(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000760
Simon Glass8b449a62016-02-21 21:09:00 -0700761 ret = exynos_dp_sw_link_training(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +0000762 if (ret != EXYNOS_DP_SUCCESS)
Robert P. J. Daya418f7e2015-12-16 11:31:23 -0500763 printf("DP dp_sw_link_training() failed\n");
Donghwa Leed2a69822012-07-02 01:16:02 +0000764
765 return ret;
766}
767
Simon Glass8b449a62016-02-21 21:09:00 -0700768static void exynos_dp_enable_scramble(struct exynos_dp *regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700769 unsigned int enable)
Donghwa Leed2a69822012-07-02 01:16:02 +0000770{
771 unsigned char data;
772
773 if (enable) {
Simon Glass8b449a62016-02-21 21:09:00 -0700774 exynos_dp_enable_scrambling(regs, DP_ENABLE);
Donghwa Leed2a69822012-07-02 01:16:02 +0000775
Simon Glass8b449a62016-02-21 21:09:00 -0700776 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700777 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass8b449a62016-02-21 21:09:00 -0700778 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leed2a69822012-07-02 01:16:02 +0000779 (u8)(data & ~DPCD_SCRAMBLING_DISABLED));
780 } else {
Simon Glass8b449a62016-02-21 21:09:00 -0700781 exynos_dp_enable_scrambling(regs, DP_DISABLE);
782 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700783 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass8b449a62016-02-21 21:09:00 -0700784 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leed2a69822012-07-02 01:16:02 +0000785 (u8)(data | DPCD_SCRAMBLING_DISABLED));
786 }
787}
788
Simon Glass8b449a62016-02-21 21:09:00 -0700789static unsigned int exynos_dp_config_video(struct exynos_dp *regs,
790 struct exynos_dp_priv *priv)
Donghwa Leed2a69822012-07-02 01:16:02 +0000791{
792 unsigned int ret = 0;
793 unsigned int retry_cnt;
794
795 mdelay(1);
796
Simon Glass8b449a62016-02-21 21:09:00 -0700797 if (priv->video_info.master_mode) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000798 printf("DP does not support master mode\n");
799 return -ENODEV;
800 } else {
801 /* debug slave */
Simon Glass8b449a62016-02-21 21:09:00 -0700802 exynos_dp_config_video_slave_mode(regs,
803 &priv->video_info);
Donghwa Leed2a69822012-07-02 01:16:02 +0000804 }
805
Simon Glass8b449a62016-02-21 21:09:00 -0700806 exynos_dp_set_video_color_format(regs, &priv->video_info);
Donghwa Leed2a69822012-07-02 01:16:02 +0000807
Simon Glass8b449a62016-02-21 21:09:00 -0700808 if (priv->video_info.bist_mode) {
809 if (exynos_dp_config_video_bist(regs, priv) != 0)
Donghwa Leed2a69822012-07-02 01:16:02 +0000810 return -1;
811 }
812
Simon Glass8b449a62016-02-21 21:09:00 -0700813 ret = exynos_dp_get_pll_lock_status(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000814 if (ret != PLL_LOCKED) {
815 printf("DP PLL is not locked yet\n");
816 return -EIO;
817 }
818
Simon Glass8b449a62016-02-21 21:09:00 -0700819 if (priv->video_info.master_mode == 0) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000820 retry_cnt = 10;
821 while (retry_cnt) {
Simon Glass8b449a62016-02-21 21:09:00 -0700822 ret = exynos_dp_is_slave_video_stream_clock_on(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000823 if (ret != EXYNOS_DP_SUCCESS) {
824 if (retry_cnt == 0) {
825 printf("DP stream_clock_on failed\n");
826 return ret;
827 }
828 retry_cnt--;
829 mdelay(1);
830 } else
831 break;
832 }
833 }
834
835 /* Set to use the register calculated M/N video */
Simon Glass8b449a62016-02-21 21:09:00 -0700836 exynos_dp_set_video_cr_mn(regs, CALCULATED_M, 0, 0);
Donghwa Leed2a69822012-07-02 01:16:02 +0000837
838 /* For video bist, Video timing must be generated by register */
Simon Glass8b449a62016-02-21 21:09:00 -0700839 exynos_dp_set_video_timing_mode(regs, VIDEO_TIMING_FROM_CAPTURE);
Donghwa Leed2a69822012-07-02 01:16:02 +0000840
841 /* Enable video bist */
Simon Glass8b449a62016-02-21 21:09:00 -0700842 if (priv->video_info.bist_pattern != COLOR_RAMP &&
843 priv->video_info.bist_pattern != BALCK_WHITE_V_LINES &&
844 priv->video_info.bist_pattern != COLOR_SQUARE)
845 exynos_dp_enable_video_bist(regs,
846 priv->video_info.bist_mode);
Donghwa Leed2a69822012-07-02 01:16:02 +0000847 else
Simon Glass8b449a62016-02-21 21:09:00 -0700848 exynos_dp_enable_video_bist(regs, DP_DISABLE);
Donghwa Leed2a69822012-07-02 01:16:02 +0000849
850 /* Disable video mute */
Simon Glass8b449a62016-02-21 21:09:00 -0700851 exynos_dp_enable_video_mute(regs, DP_DISABLE);
Donghwa Leed2a69822012-07-02 01:16:02 +0000852
853 /* Configure video Master or Slave mode */
Simon Glass8b449a62016-02-21 21:09:00 -0700854 exynos_dp_enable_video_master(regs,
855 priv->video_info.master_mode);
Donghwa Leed2a69822012-07-02 01:16:02 +0000856
857 /* Enable video */
Simon Glass8b449a62016-02-21 21:09:00 -0700858 exynos_dp_start_video(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000859
Simon Glass8b449a62016-02-21 21:09:00 -0700860 if (priv->video_info.master_mode == 0) {
Donghwa Leed2a69822012-07-02 01:16:02 +0000861 retry_cnt = 100;
862 while (retry_cnt) {
Simon Glass8b449a62016-02-21 21:09:00 -0700863 ret = exynos_dp_is_video_stream_on(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +0000864 if (ret != EXYNOS_DP_SUCCESS) {
865 if (retry_cnt == 0) {
866 printf("DP Timeout of video stream\n");
867 return ret;
868 }
869 retry_cnt--;
870 mdelay(5);
871 } else
872 break;
873 }
874 }
875
876 return ret;
877}
878
Simon Glassbb5930d2016-02-21 21:09:01 -0700879static int exynos_dp_ofdata_to_platdata(struct udevice *dev)
Ajay Kumar9947d132013-02-21 23:53:06 +0000880{
Simon Glassbb5930d2016-02-21 21:09:01 -0700881 struct exynos_dp_priv *priv = dev_get_priv(dev);
882 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700883 unsigned int node = dev_of_offset(dev);
Simon Glassbb5930d2016-02-21 21:09:01 -0700884 fdt_addr_t addr;
Ajay Kumar9947d132013-02-21 23:53:06 +0000885
Simon Glassbb5930d2016-02-21 21:09:01 -0700886 addr = dev_get_addr(dev);
887 if (addr == FDT_ADDR_T_NONE) {
888 debug("Can't get the DP base address\n");
889 return -EINVAL;
890 }
891 priv->regs = (struct exynos_dp *)addr;
Simon Glass8b449a62016-02-21 21:09:00 -0700892 priv->disp_info.h_res = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000893 "samsung,h-res", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700894 priv->disp_info.h_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000895 "samsung,h-sync-width", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700896 priv->disp_info.h_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000897 "samsung,h-back-porch", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700898 priv->disp_info.h_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000899 "samsung,h-front-porch", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700900 priv->disp_info.v_res = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000901 "samsung,v-res", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700902 priv->disp_info.v_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000903 "samsung,v-sync-width", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700904 priv->disp_info.v_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000905 "samsung,v-back-porch", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700906 priv->disp_info.v_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000907 "samsung,v-front-porch", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700908 priv->disp_info.v_sync_rate = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000909 "samsung,v-sync-rate", 0);
910
Simon Glass8b449a62016-02-21 21:09:00 -0700911 priv->lt_info.lt_status = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000912 "samsung,lt-status", 0);
913
Simon Glass8b449a62016-02-21 21:09:00 -0700914 priv->video_info.master_mode = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000915 "samsung,master-mode", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700916 priv->video_info.bist_mode = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000917 "samsung,bist-mode", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700918 priv->video_info.bist_pattern = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000919 "samsung,bist-pattern", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700920 priv->video_info.h_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000921 "samsung,h-sync-polarity", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700922 priv->video_info.v_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000923 "samsung,v-sync-polarity", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700924 priv->video_info.interlaced = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000925 "samsung,interlaced", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700926 priv->video_info.color_space = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000927 "samsung,color-space", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700928 priv->video_info.dynamic_range = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000929 "samsung,dynamic-range", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700930 priv->video_info.ycbcr_coeff = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000931 "samsung,ycbcr-coeff", 0);
Simon Glass8b449a62016-02-21 21:09:00 -0700932 priv->video_info.color_depth = fdtdec_get_int(blob, node,
Ajay Kumar9947d132013-02-21 23:53:06 +0000933 "samsung,color-depth", 0);
934 return 0;
935}
Ajay Kumar9947d132013-02-21 23:53:06 +0000936
Simon Glassbb5930d2016-02-21 21:09:01 -0700937static int exynos_dp_bridge_init(struct udevice *dev)
Donghwa Leed2a69822012-07-02 01:16:02 +0000938{
Simon Glassbb5930d2016-02-21 21:09:01 -0700939 const int max_tries = 10;
940 int num_tries;
941 int ret;
Donghwa Leed2a69822012-07-02 01:16:02 +0000942
Simon Glassbb5930d2016-02-21 21:09:01 -0700943 debug("%s\n", __func__);
944 ret = video_bridge_attach(dev);
945 if (ret) {
946 debug("video bridge init failed: %d\n", ret);
947 return ret;
Donghwa Leed2a69822012-07-02 01:16:02 +0000948 }
949
Simon Glassbb5930d2016-02-21 21:09:01 -0700950 /*
951 * We need to wait for 90ms after bringing up the bridge since there
952 * is a phantom "high" on the HPD chip during its bootup. The phantom
953 * high comes within 7ms of de-asserting PD and persists for at least
954 * 15ms. The real high comes roughly 50ms after PD is de-asserted. The
955 * phantom high makes it hard for us to know when the NXP chip is up.
956 */
957 mdelay(90);
Donghwa Leed2a69822012-07-02 01:16:02 +0000958
Simon Glassbb5930d2016-02-21 21:09:01 -0700959 for (num_tries = 0; num_tries < max_tries; num_tries++) {
960 /* Check HPD. If it's high, or we don't have it, all is well */
961 ret = video_bridge_check_attached(dev);
962 if (!ret || ret == -ENOENT)
963 return 0;
Simon Glass8c9b8dc2016-02-21 21:08:44 -0700964
Simon Glassbb5930d2016-02-21 21:09:01 -0700965 debug("%s: eDP bridge failed to come up; try %d of %d\n",
966 __func__, num_tries, max_tries);
967 }
Ajay Kumarbeded3d2013-02-21 23:53:04 +0000968
Simon Glassbb5930d2016-02-21 21:09:01 -0700969 /* Immediately go into bridge reset if the hp line is not high */
970 return -EIO;
971}
972
973static int exynos_dp_bridge_setup(const void *blob)
974{
975 const int max_tries = 2;
976 int num_tries;
977 struct udevice *dev;
978 int ret;
979
980 /* Configure I2C registers for Parade bridge */
981 ret = uclass_get_device(UCLASS_VIDEO_BRIDGE, 0, &dev);
982 if (ret) {
983 debug("video bridge init failed: %d\n", ret);
984 return ret;
985 }
986
987 if (strncmp(dev->driver->name, "parade", 6)) {
988 /* Mux HPHPD to the special hotplug detect mode */
989 exynos_pinmux_config(PERIPH_ID_DPHPD, 0);
990 }
991
992 for (num_tries = 0; num_tries < max_tries; num_tries++) {
993 ret = exynos_dp_bridge_init(dev);
994 if (!ret)
995 return 0;
996 if (num_tries == max_tries - 1)
997 break;
998
999 /*
1000 * If we're here, the bridge chip failed to initialise.
1001 * Power down the bridge in an attempt to reset.
1002 */
1003 video_bridge_set_active(dev, false);
1004
1005 /*
1006 * Arbitrarily wait 300ms here with DP_N low. Don't know for
1007 * sure how long we should wait, but we're being paranoid.
1008 */
1009 mdelay(300);
1010 }
1011
1012 return ret;
1013}
1014int exynos_dp_enable(struct udevice *dev, int panel_bpp,
1015 const struct display_timing *timing)
1016{
1017 struct exynos_dp_priv *priv = dev_get_priv(dev);
1018 struct exynos_dp *regs = priv->regs;
1019 unsigned int ret;
1020
1021 debug("%s: start\n", __func__);
Simon Glass8b449a62016-02-21 21:09:00 -07001022 exynos_dp_disp_info(&priv->disp_info);
Donghwa Leed2a69822012-07-02 01:16:02 +00001023
Simon Glassbb5930d2016-02-21 21:09:01 -07001024 ret = exynos_dp_bridge_setup(gd->fdt_blob);
1025 if (ret && ret != -ENODEV)
1026 printf("LCD bridge failed to enable: %d\n", ret);
1027
Simon Glass7eb860d2016-02-21 21:08:57 -07001028 exynos_dp_phy_ctrl(1);
Donghwa Leed2a69822012-07-02 01:16:02 +00001029
Simon Glass8b449a62016-02-21 21:09:00 -07001030 ret = exynos_dp_init_dp(regs);
Donghwa Leed2a69822012-07-02 01:16:02 +00001031 if (ret != EXYNOS_DP_SUCCESS) {
1032 printf("DP exynos_dp_init_dp() failed\n");
1033 return ret;
1034 }
1035
Simon Glass8b449a62016-02-21 21:09:00 -07001036 ret = exynos_dp_handle_edid(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +00001037 if (ret != EXYNOS_DP_SUCCESS) {
1038 printf("EDP handle_edid fail\n");
1039 return ret;
1040 }
1041
Simon Glass8b449a62016-02-21 21:09:00 -07001042 ret = exynos_dp_set_link_train(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +00001043 if (ret != EXYNOS_DP_SUCCESS) {
1044 printf("DP link training fail\n");
1045 return ret;
1046 }
1047
Simon Glass8b449a62016-02-21 21:09:00 -07001048 exynos_dp_enable_scramble(regs, DP_ENABLE);
1049 exynos_dp_enable_rx_to_enhanced_mode(regs, DP_ENABLE);
1050 exynos_dp_enable_enhanced_mode(regs, DP_ENABLE);
Donghwa Leed2a69822012-07-02 01:16:02 +00001051
Simon Glass8b449a62016-02-21 21:09:00 -07001052 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
1053 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leed2a69822012-07-02 01:16:02 +00001054
Simon Glass8b449a62016-02-21 21:09:00 -07001055 exynos_dp_init_video(regs);
1056 ret = exynos_dp_config_video(regs, priv);
Donghwa Leed2a69822012-07-02 01:16:02 +00001057 if (ret != EXYNOS_DP_SUCCESS) {
1058 printf("Exynos DP init failed\n");
1059 return ret;
1060 }
1061
Simon Glass129c9422015-07-02 18:16:14 -06001062 debug("Exynos DP init done\n");
Donghwa Leed2a69822012-07-02 01:16:02 +00001063
1064 return ret;
1065}
Simon Glassbb5930d2016-02-21 21:09:01 -07001066
1067
1068static const struct dm_display_ops exynos_dp_ops = {
1069 .enable = exynos_dp_enable,
1070};
1071
1072static const struct udevice_id exynos_dp_ids[] = {
1073 { .compatible = "samsung,exynos5-dp" },
1074 { }
1075};
1076
1077U_BOOT_DRIVER(exynos_dp) = {
1078 .name = "eexynos_dp",
1079 .id = UCLASS_DISPLAY,
1080 .of_match = exynos_dp_ids,
1081 .ops = &exynos_dp_ops,
1082 .ofdata_to_platdata = exynos_dp_ofdata_to_platdata,
1083 .priv_auto_alloc_size = sizeof(struct exynos_dp_priv),
1084};