blob: 2781f6f55ba1abf79e3f0a3bc4f809f1d4d38169 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donghwa Leeb73a88b2012-07-02 01:16:02 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 *
5 * Author: Donghwa Lee <dh09.lee@samsung.com>
Donghwa Leeb73a88b2012-07-02 01:16:02 +00006 */
7
Simon Glass51a3ec32017-05-17 17:18:07 -06008#include <common.h>
Simon Glassa1015ad2016-02-21 21:09:01 -07009#include <dm.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000010#include <common.h>
Simon Glassa1015ad2016-02-21 21:09:01 -070011#include <display.h>
12#include <fdtdec.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000014#include <malloc.h>
Simon Glassa1015ad2016-02-21 21:09:01 -070015#include <video_bridge.h>
Heiko Schocher4f7a9a32014-06-24 10:10:03 +020016#include <linux/compat.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000017#include <linux/err.h>
18#include <asm/arch/clk.h>
19#include <asm/arch/cpu.h>
20#include <asm/arch/dp_info.h>
21#include <asm/arch/dp.h>
Simon Glassa1015ad2016-02-21 21:09:01 -070022#include <asm/arch/pinmux.h>
Simon Glassbfd239c2016-02-21 21:08:57 -070023#include <asm/arch/power.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000024
25#include "exynos_dp_lowlevel.h"
26
Ajay Kumar65f50f92013-02-21 23:53:06 +000027DECLARE_GLOBAL_DATA_PTR;
28
Donghwa Leeb73a88b2012-07-02 01:16:02 +000029static void exynos_dp_disp_info(struct edp_disp_info *disp_info)
30{
31 disp_info->h_total = disp_info->h_res + disp_info->h_sync_width +
32 disp_info->h_back_porch + disp_info->h_front_porch;
33 disp_info->v_total = disp_info->v_res + disp_info->v_sync_width +
34 disp_info->v_back_porch + disp_info->v_front_porch;
35
36 return;
37}
38
Simon Glass305f5812016-02-21 21:09:00 -070039static int exynos_dp_init_dp(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +000040{
41 int ret;
Simon Glass305f5812016-02-21 21:09:00 -070042 exynos_dp_reset(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000043
44 /* SW defined function Normal operation */
Simon Glass305f5812016-02-21 21:09:00 -070045 exynos_dp_enable_sw_func(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000046
Simon Glass305f5812016-02-21 21:09:00 -070047 ret = exynos_dp_init_analog_func(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000048 if (ret != EXYNOS_DP_SUCCESS)
49 return ret;
50
Simon Glass305f5812016-02-21 21:09:00 -070051 exynos_dp_init_hpd(regs);
52 exynos_dp_init_aux(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000053
54 return ret;
55}
56
57static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
58{
59 int i;
60 unsigned char sum = 0;
61
62 for (i = 0; i < EDID_BLOCK_LENGTH; i++)
63 sum = sum + edid_data[i];
64
65 return sum;
66}
67
Simon Glass305f5812016-02-21 21:09:00 -070068static unsigned int exynos_dp_read_edid(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +000069{
70 unsigned char edid[EDID_BLOCK_LENGTH * 2];
71 unsigned int extend_block = 0;
72 unsigned char sum;
73 unsigned char test_vector;
74 int retval;
75
76 /*
77 * EDID device address is 0x50.
78 * However, if necessary, you must have set upper address
79 * into E-EDID in I2C device, 0x30.
80 */
81
82 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
Simon Glass305f5812016-02-21 21:09:00 -070083 exynos_dp_read_byte_from_i2c(regs, I2C_EDID_DEVICE_ADDR,
Simon Glass0de70892016-02-21 21:08:44 -070084 EDID_EXTENSION_FLAG, &extend_block);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000085
86 if (extend_block > 0) {
87 printf("DP EDID data includes a single extension!\n");
88
89 /* Read EDID data */
Simon Glass305f5812016-02-21 21:09:00 -070090 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -070091 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +000092 EDID_HEADER_PATTERN,
93 EDID_BLOCK_LENGTH,
94 &edid[EDID_HEADER_PATTERN]);
95 if (retval != 0) {
96 printf("DP EDID Read failed!\n");
97 return -1;
98 }
99 sum = exynos_dp_calc_edid_check_sum(edid);
100 if (sum != 0) {
101 printf("DP EDID bad checksum!\n");
102 return -1;
103 }
104
105 /* Read additional EDID data */
Simon Glass305f5812016-02-21 21:09:00 -0700106 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700107 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000108 EDID_BLOCK_LENGTH,
109 EDID_BLOCK_LENGTH,
110 &edid[EDID_BLOCK_LENGTH]);
111 if (retval != 0) {
112 printf("DP EDID Read failed!\n");
113 return -1;
114 }
115 sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
116 if (sum != 0) {
117 printf("DP EDID bad checksum!\n");
118 return -1;
119 }
120
Simon Glass305f5812016-02-21 21:09:00 -0700121 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Simon Glass0de70892016-02-21 21:08:44 -0700122 &test_vector);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000123 if (test_vector & DPCD_TEST_EDID_READ) {
Simon Glass305f5812016-02-21 21:09:00 -0700124 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700125 DPCD_TEST_EDID_CHECKSUM,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000126 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
Simon Glass305f5812016-02-21 21:09:00 -0700127 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700128 DPCD_TEST_RESPONSE,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000129 DPCD_TEST_EDID_CHECKSUM_WRITE);
130 }
131 } else {
132 debug("DP EDID data does not include any extensions.\n");
133
134 /* Read EDID data */
Simon Glass305f5812016-02-21 21:09:00 -0700135 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700136 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000137 EDID_HEADER_PATTERN,
138 EDID_BLOCK_LENGTH,
139 &edid[EDID_HEADER_PATTERN]);
140
141 if (retval != 0) {
142 printf("DP EDID Read failed!\n");
143 return -1;
144 }
145 sum = exynos_dp_calc_edid_check_sum(edid);
146 if (sum != 0) {
147 printf("DP EDID bad checksum!\n");
148 return -1;
149 }
150
Simon Glass305f5812016-02-21 21:09:00 -0700151 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000152 &test_vector);
153 if (test_vector & DPCD_TEST_EDID_READ) {
Simon Glass305f5812016-02-21 21:09:00 -0700154 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700155 DPCD_TEST_EDID_CHECKSUM, edid[EDID_CHECKSUM]);
Simon Glass305f5812016-02-21 21:09:00 -0700156 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700157 DPCD_TEST_RESPONSE,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000158 DPCD_TEST_EDID_CHECKSUM_WRITE);
159 }
160 }
161
162 debug("DP EDID Read success!\n");
163
164 return 0;
165}
166
Simon Glass305f5812016-02-21 21:09:00 -0700167static unsigned int exynos_dp_handle_edid(struct exynos_dp *regs,
168 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000169{
170 unsigned char buf[12];
171 unsigned int ret;
172 unsigned char temp;
173 unsigned char retry_cnt;
174 unsigned char dpcd_rev[16];
175 unsigned char lane_bw[16];
176 unsigned char lane_cnt[16];
177
178 memset(dpcd_rev, 0, 16);
179 memset(lane_bw, 0, 16);
180 memset(lane_cnt, 0, 16);
181 memset(buf, 0, 12);
182
183 retry_cnt = 5;
184 while (retry_cnt) {
185 /* Read DPCD 0x0000-0x000b */
Simon Glass305f5812016-02-21 21:09:00 -0700186 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_DPCD_REV, 12,
Simon Glass0de70892016-02-21 21:08:44 -0700187 buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000188 if (ret != EXYNOS_DP_SUCCESS) {
189 if (retry_cnt == 0) {
190 printf("DP read_byte_from_dpcd() failed\n");
191 return ret;
192 }
193 retry_cnt--;
194 } else
195 break;
196 }
197
198 /* */
199 temp = buf[DPCD_DPCD_REV];
200 if (temp == DP_DPCD_REV_10 || temp == DP_DPCD_REV_11)
Simon Glass305f5812016-02-21 21:09:00 -0700201 priv->dpcd_rev = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000202 else {
203 printf("DP Wrong DPCD Rev : %x\n", temp);
204 return -ENODEV;
205 }
206
207 temp = buf[DPCD_MAX_LINK_RATE];
208 if (temp == DP_LANE_BW_1_62 || temp == DP_LANE_BW_2_70)
Simon Glass305f5812016-02-21 21:09:00 -0700209 priv->lane_bw = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000210 else {
211 printf("DP Wrong MAX LINK RATE : %x\n", temp);
212 return -EINVAL;
213 }
214
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500215 /* Refer VESA Display Port Standard Ver1.1a Page 120 */
Simon Glass305f5812016-02-21 21:09:00 -0700216 if (priv->dpcd_rev == DP_DPCD_REV_11) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000217 temp = buf[DPCD_MAX_LANE_COUNT] & 0x1f;
218 if (buf[DPCD_MAX_LANE_COUNT] & 0x80)
Simon Glass305f5812016-02-21 21:09:00 -0700219 priv->dpcd_efc = 1;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000220 else
Simon Glass305f5812016-02-21 21:09:00 -0700221 priv->dpcd_efc = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000222 } else {
223 temp = buf[DPCD_MAX_LANE_COUNT];
Simon Glass305f5812016-02-21 21:09:00 -0700224 priv->dpcd_efc = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000225 }
226
227 if (temp == DP_LANE_CNT_1 || temp == DP_LANE_CNT_2 ||
228 temp == DP_LANE_CNT_4) {
Simon Glass305f5812016-02-21 21:09:00 -0700229 priv->lane_cnt = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000230 } else {
231 printf("DP Wrong MAX LANE COUNT : %x\n", temp);
232 return -EINVAL;
233 }
234
Simon Glass305f5812016-02-21 21:09:00 -0700235 ret = exynos_dp_read_edid(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000236 if (ret != EXYNOS_DP_SUCCESS) {
237 printf("DP exynos_dp_read_edid() failed\n");
238 return -EINVAL;
239 }
240
241 return ret;
242}
243
Simon Glass305f5812016-02-21 21:09:00 -0700244static void exynos_dp_init_training(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000245{
246 /*
247 * MACRO_RST must be applied after the PLL_LOCK to avoid
248 * the DP inter pair skew issue for at least 10 us
249 */
Simon Glass305f5812016-02-21 21:09:00 -0700250 exynos_dp_reset_macro(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000251
252 /* All DP analog module power up */
Simon Glass305f5812016-02-21 21:09:00 -0700253 exynos_dp_set_analog_power_down(regs, POWER_ALL, 0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000254}
255
Simon Glass305f5812016-02-21 21:09:00 -0700256static unsigned int exynos_dp_link_start(struct exynos_dp *regs,
257 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000258{
259 unsigned char buf[5];
260 unsigned int ret = 0;
261
262 debug("DP: %s was called\n", __func__);
263
Simon Glass305f5812016-02-21 21:09:00 -0700264 priv->lt_info.lt_status = DP_LT_CR;
265 priv->lt_info.ep_loop = 0;
266 priv->lt_info.cr_loop[0] = 0;
267 priv->lt_info.cr_loop[1] = 0;
268 priv->lt_info.cr_loop[2] = 0;
269 priv->lt_info.cr_loop[3] = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000270
271 /* Set sink to D0 (Sink Not Ready) mode. */
Simon Glass305f5812016-02-21 21:09:00 -0700272 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_SINK_POWER_STATE,
Simon Glass0de70892016-02-21 21:08:44 -0700273 DPCD_SET_POWER_STATE_D0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000274 if (ret != EXYNOS_DP_SUCCESS) {
275 printf("DP write_dpcd_byte failed\n");
276 return ret;
277 }
278
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500279 /* Set link rate and count as you want to establish */
Simon Glass305f5812016-02-21 21:09:00 -0700280 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
281 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000282
283 /* Setup RX configuration */
Simon Glass305f5812016-02-21 21:09:00 -0700284 buf[0] = priv->lane_bw;
285 buf[1] = priv->lane_cnt;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000286
Simon Glass305f5812016-02-21 21:09:00 -0700287 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_LINK_BW_SET, 2, buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000288 if (ret != EXYNOS_DP_SUCCESS) {
289 printf("DP write_dpcd_byte failed\n");
290 return ret;
291 }
292
Simon Glass305f5812016-02-21 21:09:00 -0700293 exynos_dp_set_lane_pre_emphasis(regs, PRE_EMPHASIS_LEVEL_0,
294 priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000295
296 /* Set training pattern 1 */
Simon Glass305f5812016-02-21 21:09:00 -0700297 exynos_dp_set_training_pattern(regs, TRAINING_PTN1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000298
299 /* Set RX training pattern */
300 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_1;
301
302 buf[1] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
303 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
304 buf[2] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
305 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
306 buf[3] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
307 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
308 buf[4] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
309 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
310
Simon Glass305f5812016-02-21 21:09:00 -0700311 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700312 5, buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000313 if (ret != EXYNOS_DP_SUCCESS) {
314 printf("DP write_dpcd_byte failed\n");
315 return ret;
316 }
317
318 return ret;
319}
320
Simon Glass305f5812016-02-21 21:09:00 -0700321static unsigned int exynos_dp_training_pattern_dis(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000322{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100323 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000324
Simon Glass305f5812016-02-21 21:09:00 -0700325 exynos_dp_set_training_pattern(regs, DP_NONE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000326
Simon Glass305f5812016-02-21 21:09:00 -0700327 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700328 DPCD_TRAINING_PATTERN_DISABLED);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000329 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500330 printf("DP request_link_training_req failed\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000331 return -EAGAIN;
332 }
333
334 return ret;
335}
336
Simon Glass0de70892016-02-21 21:08:44 -0700337static unsigned int exynos_dp_enable_rx_to_enhanced_mode(
Simon Glass305f5812016-02-21 21:09:00 -0700338 struct exynos_dp *regs, unsigned char enable)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000339{
340 unsigned char data;
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100341 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000342
Simon Glass305f5812016-02-21 21:09:00 -0700343 ret = exynos_dp_read_byte_from_dpcd(regs, DPCD_LANE_COUNT_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700344 &data);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000345 if (ret != EXYNOS_DP_SUCCESS) {
346 printf("DP read_from_dpcd failed\n");
347 return -EAGAIN;
348 }
349
350 if (enable)
351 data = DPCD_ENHANCED_FRAME_EN | DPCD_LN_COUNT_SET(data);
352 else
353 data = DPCD_LN_COUNT_SET(data);
354
Simon Glass305f5812016-02-21 21:09:00 -0700355 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_LANE_COUNT_SET, data);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000356 if (ret != EXYNOS_DP_SUCCESS) {
357 printf("DP write_to_dpcd failed\n");
358 return -EAGAIN;
359
360 }
361
362 return ret;
363}
364
Simon Glass305f5812016-02-21 21:09:00 -0700365static unsigned int exynos_dp_set_enhanced_mode(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700366 unsigned char enhance_mode)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000367{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100368 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000369
Simon Glass305f5812016-02-21 21:09:00 -0700370 ret = exynos_dp_enable_rx_to_enhanced_mode(regs, enhance_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000371 if (ret != EXYNOS_DP_SUCCESS) {
372 printf("DP rx_enhance_mode failed\n");
373 return -EAGAIN;
374 }
375
Simon Glass305f5812016-02-21 21:09:00 -0700376 exynos_dp_enable_enhanced_mode(regs, enhance_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000377
378 return ret;
379}
380
Simon Glass305f5812016-02-21 21:09:00 -0700381static int exynos_dp_read_dpcd_lane_stat(struct exynos_dp *regs,
382 struct exynos_dp_priv *priv,
Simon Glass0de70892016-02-21 21:08:44 -0700383 unsigned char *status)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000384{
385 unsigned int ret, i;
386 unsigned char buf[2];
387 unsigned char lane_stat[DP_LANE_CNT_4] = {0,};
388 unsigned char shift_val[DP_LANE_CNT_4] = {0,};
389
390 shift_val[0] = 0;
391 shift_val[1] = 4;
392 shift_val[2] = 0;
393 shift_val[3] = 4;
394
Simon Glass305f5812016-02-21 21:09:00 -0700395 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_LANE0_1_STATUS, 2,
Simon Glass0de70892016-02-21 21:08:44 -0700396 buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000397 if (ret != EXYNOS_DP_SUCCESS) {
398 printf("DP read lane status failed\n");
399 return ret;
400 }
401
Simon Glass305f5812016-02-21 21:09:00 -0700402 for (i = 0; i < priv->lane_cnt; i++) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000403 lane_stat[i] = (buf[(i / 2)] >> shift_val[i]) & 0x0f;
404 if (lane_stat[0] != lane_stat[i]) {
405 printf("Wrong lane status\n");
406 return -EINVAL;
407 }
408 }
409
410 *status = lane_stat[0];
411
412 return ret;
413}
414
Simon Glass305f5812016-02-21 21:09:00 -0700415static unsigned int exynos_dp_read_dpcd_adj_req(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700416 unsigned char lane_num, unsigned char *sw, unsigned char *em)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000417{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100418 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000419 unsigned char buf;
420 unsigned int dpcd_addr;
421 unsigned char shift_val[DP_LANE_CNT_4] = {0, 4, 0, 4};
422
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500423 /* lane_num value is used as array index, so this range 0 ~ 3 */
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000424 dpcd_addr = DPCD_ADJUST_REQUEST_LANE0_1 + (lane_num / 2);
425
Simon Glass305f5812016-02-21 21:09:00 -0700426 ret = exynos_dp_read_byte_from_dpcd(regs, dpcd_addr, &buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000427 if (ret != EXYNOS_DP_SUCCESS) {
428 printf("DP read adjust request failed\n");
429 return -EAGAIN;
430 }
431
432 *sw = ((buf >> shift_val[lane_num]) & 0x03);
433 *em = ((buf >> shift_val[lane_num]) & 0x0c) >> 2;
434
435 return ret;
436}
437
Simon Glass305f5812016-02-21 21:09:00 -0700438static int exynos_dp_equalizer_err_link(struct exynos_dp *regs,
439 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000440{
441 int ret;
442
Simon Glass305f5812016-02-21 21:09:00 -0700443 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000444 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500445 printf("DP training_pattern_disable() failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700446 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000447 }
448
Simon Glass305f5812016-02-21 21:09:00 -0700449 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000450 if (ret != EXYNOS_DP_SUCCESS) {
451 printf("DP set_enhanced_mode() failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700452 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000453 }
454
455 return ret;
456}
457
Simon Glass305f5812016-02-21 21:09:00 -0700458static int exynos_dp_reduce_link_rate(struct exynos_dp *regs,
459 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000460{
461 int ret;
462
Simon Glass305f5812016-02-21 21:09:00 -0700463 if (priv->lane_bw == DP_LANE_BW_2_70) {
464 priv->lane_bw = DP_LANE_BW_1_62;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000465 printf("DP Change lane bw to 1.62Gbps\n");
Simon Glass305f5812016-02-21 21:09:00 -0700466 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000467 ret = EXYNOS_DP_SUCCESS;
468 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700469 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000470 if (ret != EXYNOS_DP_SUCCESS)
471 printf("DP training_patter_disable() failed\n");
472
Simon Glass305f5812016-02-21 21:09:00 -0700473 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000474 if (ret != EXYNOS_DP_SUCCESS)
475 printf("DP set_enhanced_mode() failed\n");
476
Simon Glass305f5812016-02-21 21:09:00 -0700477 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000478 }
479
480 return ret;
481}
482
Simon Glass305f5812016-02-21 21:09:00 -0700483static unsigned int exynos_dp_process_clock_recovery(struct exynos_dp *regs,
484 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000485{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100486 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000487 unsigned char lane_stat;
488 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0, };
489 unsigned int i;
490 unsigned char adj_req_sw;
491 unsigned char adj_req_em;
492 unsigned char buf[5];
493
494 debug("DP: %s was called\n", __func__);
495 mdelay(1);
496
Simon Glass305f5812016-02-21 21:09:00 -0700497 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000498 if (ret != EXYNOS_DP_SUCCESS) {
499 printf("DP read lane status failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700500 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000501 return ret;
502 }
503
504 if (lane_stat & DP_LANE_STAT_CR_DONE) {
505 debug("DP clock Recovery training succeed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700506 exynos_dp_set_training_pattern(regs, TRAINING_PTN2);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000507
Simon Glass305f5812016-02-21 21:09:00 -0700508 for (i = 0; i < priv->lane_cnt; i++) {
509 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Simon Glass0de70892016-02-21 21:08:44 -0700510 &adj_req_sw, &adj_req_em);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000511 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass305f5812016-02-21 21:09:00 -0700512 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000513 return ret;
514 }
515
516 lt_ctl_val[i] = 0;
517 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
518
519 if ((adj_req_sw == VOLTAGE_LEVEL_3)
520 || (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
521 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
522 MAX_PRE_EMPHASIS_REACH_3;
523 }
Simon Glass305f5812016-02-21 21:09:00 -0700524 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700525 lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000526 }
527
528 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_2;
529 buf[1] = lt_ctl_val[0];
530 buf[2] = lt_ctl_val[1];
531 buf[3] = lt_ctl_val[2];
532 buf[4] = lt_ctl_val[3];
533
Simon Glass305f5812016-02-21 21:09:00 -0700534 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000535 DPCD_TRAINING_PATTERN_SET, 5, buf);
536 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500537 printf("DP write training pattern1 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700538 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000539 return ret;
540 } else
Simon Glass305f5812016-02-21 21:09:00 -0700541 priv->lt_info.lt_status = DP_LT_ET;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000542 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700543 for (i = 0; i < priv->lane_cnt; i++) {
Simon Glass0de70892016-02-21 21:08:44 -0700544 lt_ctl_val[i] = exynos_dp_get_lanex_pre_emphasis(
Simon Glass305f5812016-02-21 21:09:00 -0700545 regs, i);
546 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000547 &adj_req_sw, &adj_req_em);
548 if (ret != EXYNOS_DP_SUCCESS) {
549 printf("DP read adj req failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700550 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000551 return ret;
552 }
553
554 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
555 (adj_req_em == PRE_EMPHASIS_LEVEL_3))
Simon Glass305f5812016-02-21 21:09:00 -0700556 ret = exynos_dp_reduce_link_rate(regs,
557 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000558
559 if ((DRIVE_CURRENT_SET_0_GET(lt_ctl_val[i]) ==
560 adj_req_sw) &&
561 (PRE_EMPHASIS_SET_0_GET(lt_ctl_val[i]) ==
562 adj_req_em)) {
Simon Glass305f5812016-02-21 21:09:00 -0700563 priv->lt_info.cr_loop[i]++;
564 if (priv->lt_info.cr_loop[i] == MAX_CR_LOOP)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000565 ret = exynos_dp_reduce_link_rate(
Simon Glass305f5812016-02-21 21:09:00 -0700566 regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000567 }
568
569 lt_ctl_val[i] = 0;
570 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
571
572 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
573 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
574 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
575 MAX_PRE_EMPHASIS_REACH_3;
576 }
Simon Glass305f5812016-02-21 21:09:00 -0700577 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700578 lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000579 }
580
Simon Glass305f5812016-02-21 21:09:00 -0700581 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000582 DPCD_TRAINING_LANE0_SET, 4, lt_ctl_val);
583 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500584 printf("DP write training pattern2 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700585 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000586 return ret;
587 }
588 }
589
590 return ret;
591}
592
Simon Glass0de70892016-02-21 21:08:44 -0700593static unsigned int exynos_dp_process_equalizer_training(
Simon Glass305f5812016-02-21 21:09:00 -0700594 struct exynos_dp *regs, struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000595{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100596 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000597 unsigned char lane_stat, adj_req_sw, adj_req_em, i;
598 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0,};
599 unsigned char interlane_aligned = 0;
600 unsigned char f_bw;
601 unsigned char f_lane_cnt;
602 unsigned char sink_stat;
603
604 mdelay(1);
605
Simon Glass305f5812016-02-21 21:09:00 -0700606 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000607 if (ret != EXYNOS_DP_SUCCESS) {
608 printf("DP read lane status failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700609 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000610 return ret;
611 }
612
613 debug("DP lane stat : %x\n", lane_stat);
614
615 if (lane_stat & DP_LANE_STAT_CR_DONE) {
Simon Glass305f5812016-02-21 21:09:00 -0700616 ret = exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700617 DPCD_LN_ALIGN_UPDATED,
618 &sink_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000619 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass305f5812016-02-21 21:09:00 -0700620 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000621
622 return ret;
623 }
624
625 interlane_aligned = (sink_stat & DPCD_INTERLANE_ALIGN_DONE);
626
Simon Glass305f5812016-02-21 21:09:00 -0700627 for (i = 0; i < priv->lane_cnt; i++) {
628 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000629 &adj_req_sw, &adj_req_em);
630 if (ret != EXYNOS_DP_SUCCESS) {
631 printf("DP read adj req 1 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700632 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000633
634 return ret;
635 }
636
637 lt_ctl_val[i] = 0;
638 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
639
640 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
641 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
642 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3;
643 lt_ctl_val[i] |= MAX_PRE_EMPHASIS_REACH_3;
644 }
645 }
646
647 if (((lane_stat&DP_LANE_STAT_CE_DONE) &&
648 (lane_stat&DP_LANE_STAT_SYM_LOCK))
649 && (interlane_aligned == DPCD_INTERLANE_ALIGN_DONE)) {
650 debug("DP Equalizer training succeed\n");
651
Simon Glass305f5812016-02-21 21:09:00 -0700652 f_bw = exynos_dp_get_link_bandwidth(regs);
653 f_lane_cnt = exynos_dp_get_lane_count(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000654
655 debug("DP final BandWidth : %x\n", f_bw);
656 debug("DP final Lane Count : %x\n", f_lane_cnt);
657
Simon Glass305f5812016-02-21 21:09:00 -0700658 priv->lt_info.lt_status = DP_LT_FINISHED;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000659
Simon Glass305f5812016-02-21 21:09:00 -0700660 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000661
662 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700663 priv->lt_info.ep_loop++;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000664
Simon Glass305f5812016-02-21 21:09:00 -0700665 if (priv->lt_info.ep_loop > MAX_EQ_LOOP) {
666 if (priv->lane_bw == DP_LANE_BW_2_70) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000667 ret = exynos_dp_reduce_link_rate(
Simon Glass305f5812016-02-21 21:09:00 -0700668 regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000669 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700670 priv->lt_info.lt_status =
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000671 DP_LT_FAIL;
Simon Glass305f5812016-02-21 21:09:00 -0700672 exynos_dp_equalizer_err_link(regs,
673 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000674 }
675 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700676 for (i = 0; i < priv->lane_cnt; i++)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000677 exynos_dp_set_lanex_pre_emphasis(
Simon Glass305f5812016-02-21 21:09:00 -0700678 regs, lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000679
Simon Glass305f5812016-02-21 21:09:00 -0700680 ret = exynos_dp_write_bytes_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700681 DPCD_TRAINING_LANE0_SET,
682 4, lt_ctl_val);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000683 if (ret != EXYNOS_DP_SUCCESS) {
684 printf("DP set lt pattern failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700685 priv->lt_info.lt_status =
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000686 DP_LT_FAIL;
Simon Glass305f5812016-02-21 21:09:00 -0700687 exynos_dp_equalizer_err_link(regs,
688 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000689 }
690 }
691 }
Simon Glass305f5812016-02-21 21:09:00 -0700692 } else if (priv->lane_bw == DP_LANE_BW_2_70) {
693 ret = exynos_dp_reduce_link_rate(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000694 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700695 priv->lt_info.lt_status = DP_LT_FAIL;
696 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000697 }
698
699 return ret;
700}
701
Simon Glass305f5812016-02-21 21:09:00 -0700702static unsigned int exynos_dp_sw_link_training(struct exynos_dp *regs,
703 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000704{
705 unsigned int ret = 0;
706 int training_finished;
707
708 /* Turn off unnecessary lane */
Simon Glass305f5812016-02-21 21:09:00 -0700709 if (priv->lane_cnt == 1)
710 exynos_dp_set_analog_power_down(regs, CH1_BLOCK, 1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000711
712 training_finished = 0;
713
Simon Glass305f5812016-02-21 21:09:00 -0700714 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000715
716 /* Process here */
717 while (!training_finished) {
Simon Glass305f5812016-02-21 21:09:00 -0700718 switch (priv->lt_info.lt_status) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000719 case DP_LT_START:
Simon Glass305f5812016-02-21 21:09:00 -0700720 ret = exynos_dp_link_start(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000721 if (ret != EXYNOS_DP_SUCCESS) {
722 printf("DP LT:link start failed\n");
723 return ret;
724 }
725 break;
726 case DP_LT_CR:
Simon Glass305f5812016-02-21 21:09:00 -0700727 ret = exynos_dp_process_clock_recovery(regs,
728 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000729 if (ret != EXYNOS_DP_SUCCESS) {
730 printf("DP LT:clock recovery failed\n");
731 return ret;
732 }
733 break;
734 case DP_LT_ET:
Simon Glass305f5812016-02-21 21:09:00 -0700735 ret = exynos_dp_process_equalizer_training(regs,
736 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000737 if (ret != EXYNOS_DP_SUCCESS) {
738 printf("DP LT:equalizer training failed\n");
739 return ret;
740 }
741 break;
742 case DP_LT_FINISHED:
743 training_finished = 1;
744 break;
745 case DP_LT_FAIL:
746 return -1;
747 }
748 }
749
750 return ret;
751}
752
Simon Glass305f5812016-02-21 21:09:00 -0700753static unsigned int exynos_dp_set_link_train(struct exynos_dp *regs,
754 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000755{
756 unsigned int ret;
757
Simon Glass305f5812016-02-21 21:09:00 -0700758 exynos_dp_init_training(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000759
Simon Glass305f5812016-02-21 21:09:00 -0700760 ret = exynos_dp_sw_link_training(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000761 if (ret != EXYNOS_DP_SUCCESS)
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500762 printf("DP dp_sw_link_training() failed\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000763
764 return ret;
765}
766
Simon Glass305f5812016-02-21 21:09:00 -0700767static void exynos_dp_enable_scramble(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700768 unsigned int enable)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000769{
770 unsigned char data;
771
772 if (enable) {
Simon Glass305f5812016-02-21 21:09:00 -0700773 exynos_dp_enable_scrambling(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000774
Simon Glass305f5812016-02-21 21:09:00 -0700775 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700776 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass305f5812016-02-21 21:09:00 -0700777 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000778 (u8)(data & ~DPCD_SCRAMBLING_DISABLED));
779 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700780 exynos_dp_enable_scrambling(regs, DP_DISABLE);
781 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700782 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass305f5812016-02-21 21:09:00 -0700783 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000784 (u8)(data | DPCD_SCRAMBLING_DISABLED));
785 }
786}
787
Simon Glass305f5812016-02-21 21:09:00 -0700788static unsigned int exynos_dp_config_video(struct exynos_dp *regs,
789 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000790{
791 unsigned int ret = 0;
792 unsigned int retry_cnt;
793
794 mdelay(1);
795
Simon Glass305f5812016-02-21 21:09:00 -0700796 if (priv->video_info.master_mode) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000797 printf("DP does not support master mode\n");
798 return -ENODEV;
799 } else {
800 /* debug slave */
Simon Glass305f5812016-02-21 21:09:00 -0700801 exynos_dp_config_video_slave_mode(regs,
802 &priv->video_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000803 }
804
Simon Glass305f5812016-02-21 21:09:00 -0700805 exynos_dp_set_video_color_format(regs, &priv->video_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000806
Simon Glass305f5812016-02-21 21:09:00 -0700807 if (priv->video_info.bist_mode) {
808 if (exynos_dp_config_video_bist(regs, priv) != 0)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000809 return -1;
810 }
811
Simon Glass305f5812016-02-21 21:09:00 -0700812 ret = exynos_dp_get_pll_lock_status(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000813 if (ret != PLL_LOCKED) {
814 printf("DP PLL is not locked yet\n");
815 return -EIO;
816 }
817
Simon Glass305f5812016-02-21 21:09:00 -0700818 if (priv->video_info.master_mode == 0) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000819 retry_cnt = 10;
820 while (retry_cnt) {
Simon Glass305f5812016-02-21 21:09:00 -0700821 ret = exynos_dp_is_slave_video_stream_clock_on(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000822 if (ret != EXYNOS_DP_SUCCESS) {
823 if (retry_cnt == 0) {
824 printf("DP stream_clock_on failed\n");
825 return ret;
826 }
827 retry_cnt--;
828 mdelay(1);
829 } else
830 break;
831 }
832 }
833
834 /* Set to use the register calculated M/N video */
Simon Glass305f5812016-02-21 21:09:00 -0700835 exynos_dp_set_video_cr_mn(regs, CALCULATED_M, 0, 0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000836
837 /* For video bist, Video timing must be generated by register */
Simon Glass305f5812016-02-21 21:09:00 -0700838 exynos_dp_set_video_timing_mode(regs, VIDEO_TIMING_FROM_CAPTURE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000839
840 /* Enable video bist */
Simon Glass305f5812016-02-21 21:09:00 -0700841 if (priv->video_info.bist_pattern != COLOR_RAMP &&
842 priv->video_info.bist_pattern != BALCK_WHITE_V_LINES &&
843 priv->video_info.bist_pattern != COLOR_SQUARE)
844 exynos_dp_enable_video_bist(regs,
845 priv->video_info.bist_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000846 else
Simon Glass305f5812016-02-21 21:09:00 -0700847 exynos_dp_enable_video_bist(regs, DP_DISABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000848
849 /* Disable video mute */
Simon Glass305f5812016-02-21 21:09:00 -0700850 exynos_dp_enable_video_mute(regs, DP_DISABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000851
852 /* Configure video Master or Slave mode */
Simon Glass305f5812016-02-21 21:09:00 -0700853 exynos_dp_enable_video_master(regs,
854 priv->video_info.master_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000855
856 /* Enable video */
Simon Glass305f5812016-02-21 21:09:00 -0700857 exynos_dp_start_video(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000858
Simon Glass305f5812016-02-21 21:09:00 -0700859 if (priv->video_info.master_mode == 0) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000860 retry_cnt = 100;
861 while (retry_cnt) {
Simon Glass305f5812016-02-21 21:09:00 -0700862 ret = exynos_dp_is_video_stream_on(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000863 if (ret != EXYNOS_DP_SUCCESS) {
864 if (retry_cnt == 0) {
865 printf("DP Timeout of video stream\n");
866 return ret;
867 }
868 retry_cnt--;
869 mdelay(5);
870 } else
871 break;
872 }
873 }
874
875 return ret;
876}
Ajay Kumar65f50f92013-02-21 23:53:06 +0000877
Simon Glassa1015ad2016-02-21 21:09:01 -0700878static int exynos_dp_ofdata_to_platdata(struct udevice *dev)
Ajay Kumar65f50f92013-02-21 23:53:06 +0000879{
Simon Glassa1015ad2016-02-21 21:09:01 -0700880 struct exynos_dp_priv *priv = dev_get_priv(dev);
881 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700882 unsigned int node = dev_of_offset(dev);
Simon Glassa1015ad2016-02-21 21:09:01 -0700883 fdt_addr_t addr;
Ajay Kumar65f50f92013-02-21 23:53:06 +0000884
Simon Glassba1dea42017-05-17 17:18:05 -0600885 addr = devfdt_get_addr(dev);
Simon Glassa1015ad2016-02-21 21:09:01 -0700886 if (addr == FDT_ADDR_T_NONE) {
887 debug("Can't get the DP base address\n");
888 return -EINVAL;
889 }
890 priv->regs = (struct exynos_dp *)addr;
Simon Glass305f5812016-02-21 21:09:00 -0700891 priv->disp_info.h_res = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000892 "samsung,h-res", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700893 priv->disp_info.h_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000894 "samsung,h-sync-width", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700895 priv->disp_info.h_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000896 "samsung,h-back-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700897 priv->disp_info.h_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000898 "samsung,h-front-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700899 priv->disp_info.v_res = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000900 "samsung,v-res", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700901 priv->disp_info.v_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000902 "samsung,v-sync-width", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700903 priv->disp_info.v_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000904 "samsung,v-back-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700905 priv->disp_info.v_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000906 "samsung,v-front-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700907 priv->disp_info.v_sync_rate = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000908 "samsung,v-sync-rate", 0);
909
Simon Glass305f5812016-02-21 21:09:00 -0700910 priv->lt_info.lt_status = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000911 "samsung,lt-status", 0);
912
Simon Glass305f5812016-02-21 21:09:00 -0700913 priv->video_info.master_mode = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000914 "samsung,master-mode", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700915 priv->video_info.bist_mode = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000916 "samsung,bist-mode", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700917 priv->video_info.bist_pattern = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000918 "samsung,bist-pattern", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700919 priv->video_info.h_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000920 "samsung,h-sync-polarity", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700921 priv->video_info.v_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000922 "samsung,v-sync-polarity", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700923 priv->video_info.interlaced = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000924 "samsung,interlaced", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700925 priv->video_info.color_space = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000926 "samsung,color-space", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700927 priv->video_info.dynamic_range = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000928 "samsung,dynamic-range", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700929 priv->video_info.ycbcr_coeff = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000930 "samsung,ycbcr-coeff", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700931 priv->video_info.color_depth = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000932 "samsung,color-depth", 0);
933 return 0;
934}
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000935
Simon Glassa1015ad2016-02-21 21:09:01 -0700936static int exynos_dp_bridge_init(struct udevice *dev)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000937{
Simon Glassa1015ad2016-02-21 21:09:01 -0700938 const int max_tries = 10;
939 int num_tries;
940 int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000941
Simon Glassa1015ad2016-02-21 21:09:01 -0700942 debug("%s\n", __func__);
943 ret = video_bridge_attach(dev);
944 if (ret) {
945 debug("video bridge init failed: %d\n", ret);
946 return ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000947 }
948
Simon Glassa1015ad2016-02-21 21:09:01 -0700949 /*
950 * We need to wait for 90ms after bringing up the bridge since there
951 * is a phantom "high" on the HPD chip during its bootup. The phantom
952 * high comes within 7ms of de-asserting PD and persists for at least
953 * 15ms. The real high comes roughly 50ms after PD is de-asserted. The
954 * phantom high makes it hard for us to know when the NXP chip is up.
955 */
956 mdelay(90);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000957
Simon Glassa1015ad2016-02-21 21:09:01 -0700958 for (num_tries = 0; num_tries < max_tries; num_tries++) {
959 /* Check HPD. If it's high, or we don't have it, all is well */
960 ret = video_bridge_check_attached(dev);
961 if (!ret || ret == -ENOENT)
962 return 0;
Simon Glass0de70892016-02-21 21:08:44 -0700963
Simon Glassa1015ad2016-02-21 21:09:01 -0700964 debug("%s: eDP bridge failed to come up; try %d of %d\n",
965 __func__, num_tries, max_tries);
966 }
Ajay Kumarca7c3e12013-02-21 23:53:04 +0000967
Simon Glassa1015ad2016-02-21 21:09:01 -0700968 /* Immediately go into bridge reset if the hp line is not high */
969 return -EIO;
970}
971
972static int exynos_dp_bridge_setup(const void *blob)
973{
974 const int max_tries = 2;
975 int num_tries;
976 struct udevice *dev;
977 int ret;
978
979 /* Configure I2C registers for Parade bridge */
980 ret = uclass_get_device(UCLASS_VIDEO_BRIDGE, 0, &dev);
981 if (ret) {
982 debug("video bridge init failed: %d\n", ret);
983 return ret;
984 }
985
986 if (strncmp(dev->driver->name, "parade", 6)) {
987 /* Mux HPHPD to the special hotplug detect mode */
988 exynos_pinmux_config(PERIPH_ID_DPHPD, 0);
989 }
990
991 for (num_tries = 0; num_tries < max_tries; num_tries++) {
992 ret = exynos_dp_bridge_init(dev);
993 if (!ret)
994 return 0;
995 if (num_tries == max_tries - 1)
996 break;
997
998 /*
999 * If we're here, the bridge chip failed to initialise.
1000 * Power down the bridge in an attempt to reset.
1001 */
1002 video_bridge_set_active(dev, false);
1003
1004 /*
1005 * Arbitrarily wait 300ms here with DP_N low. Don't know for
1006 * sure how long we should wait, but we're being paranoid.
1007 */
1008 mdelay(300);
1009 }
1010
1011 return ret;
1012}
1013int exynos_dp_enable(struct udevice *dev, int panel_bpp,
1014 const struct display_timing *timing)
1015{
1016 struct exynos_dp_priv *priv = dev_get_priv(dev);
1017 struct exynos_dp *regs = priv->regs;
1018 unsigned int ret;
1019
1020 debug("%s: start\n", __func__);
Simon Glass305f5812016-02-21 21:09:00 -07001021 exynos_dp_disp_info(&priv->disp_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001022
Simon Glassa1015ad2016-02-21 21:09:01 -07001023 ret = exynos_dp_bridge_setup(gd->fdt_blob);
1024 if (ret && ret != -ENODEV)
1025 printf("LCD bridge failed to enable: %d\n", ret);
1026
Simon Glassbfd239c2016-02-21 21:08:57 -07001027 exynos_dp_phy_ctrl(1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001028
Simon Glass305f5812016-02-21 21:09:00 -07001029 ret = exynos_dp_init_dp(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001030 if (ret != EXYNOS_DP_SUCCESS) {
1031 printf("DP exynos_dp_init_dp() failed\n");
1032 return ret;
1033 }
1034
Simon Glass305f5812016-02-21 21:09:00 -07001035 ret = exynos_dp_handle_edid(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001036 if (ret != EXYNOS_DP_SUCCESS) {
1037 printf("EDP handle_edid fail\n");
1038 return ret;
1039 }
1040
Simon Glass305f5812016-02-21 21:09:00 -07001041 ret = exynos_dp_set_link_train(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001042 if (ret != EXYNOS_DP_SUCCESS) {
1043 printf("DP link training fail\n");
1044 return ret;
1045 }
1046
Simon Glass305f5812016-02-21 21:09:00 -07001047 exynos_dp_enable_scramble(regs, DP_ENABLE);
1048 exynos_dp_enable_rx_to_enhanced_mode(regs, DP_ENABLE);
1049 exynos_dp_enable_enhanced_mode(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001050
Simon Glass305f5812016-02-21 21:09:00 -07001051 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
1052 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001053
Simon Glass305f5812016-02-21 21:09:00 -07001054 exynos_dp_init_video(regs);
1055 ret = exynos_dp_config_video(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001056 if (ret != EXYNOS_DP_SUCCESS) {
1057 printf("Exynos DP init failed\n");
1058 return ret;
1059 }
1060
Simon Glass5e7ab072015-07-02 18:16:14 -06001061 debug("Exynos DP init done\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001062
1063 return ret;
1064}
Simon Glassa1015ad2016-02-21 21:09:01 -07001065
1066
1067static const struct dm_display_ops exynos_dp_ops = {
1068 .enable = exynos_dp_enable,
1069};
1070
1071static const struct udevice_id exynos_dp_ids[] = {
1072 { .compatible = "samsung,exynos5-dp" },
1073 { }
1074};
1075
1076U_BOOT_DRIVER(exynos_dp) = {
Dongjin Kime51b7772017-10-27 23:08:51 -04001077 .name = "exynos_dp",
Simon Glassa1015ad2016-02-21 21:09:01 -07001078 .id = UCLASS_DISPLAY,
1079 .of_match = exynos_dp_ids,
1080 .ops = &exynos_dp_ops,
1081 .ofdata_to_platdata = exynos_dp_ofdata_to_platdata,
1082 .priv_auto_alloc_size = sizeof(struct exynos_dp_priv),
1083};