blob: a532d5ae1abbb844cacee7b3ec8efc8c95635834 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000017#include <malloc.h>
Simon Glassa1015ad2016-02-21 21:09:01 -070018#include <video_bridge.h>
Heiko Schocher4f7a9a32014-06-24 10:10:03 +020019#include <linux/compat.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000020#include <linux/err.h>
21#include <asm/arch/clk.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/dp_info.h>
24#include <asm/arch/dp.h>
Simon Glassa1015ad2016-02-21 21:09:01 -070025#include <asm/arch/pinmux.h>
Simon Glassbfd239c2016-02-21 21:08:57 -070026#include <asm/arch/power.h>
Donghwa Leeb73a88b2012-07-02 01:16:02 +000027
28#include "exynos_dp_lowlevel.h"
29
Ajay Kumar65f50f92013-02-21 23:53:06 +000030DECLARE_GLOBAL_DATA_PTR;
31
Donghwa Leeb73a88b2012-07-02 01:16:02 +000032static void exynos_dp_disp_info(struct edp_disp_info *disp_info)
33{
34 disp_info->h_total = disp_info->h_res + disp_info->h_sync_width +
35 disp_info->h_back_porch + disp_info->h_front_porch;
36 disp_info->v_total = disp_info->v_res + disp_info->v_sync_width +
37 disp_info->v_back_porch + disp_info->v_front_porch;
38
39 return;
40}
41
Simon Glass305f5812016-02-21 21:09:00 -070042static int exynos_dp_init_dp(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +000043{
44 int ret;
Simon Glass305f5812016-02-21 21:09:00 -070045 exynos_dp_reset(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000046
47 /* SW defined function Normal operation */
Simon Glass305f5812016-02-21 21:09:00 -070048 exynos_dp_enable_sw_func(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000049
Simon Glass305f5812016-02-21 21:09:00 -070050 ret = exynos_dp_init_analog_func(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000051 if (ret != EXYNOS_DP_SUCCESS)
52 return ret;
53
Simon Glass305f5812016-02-21 21:09:00 -070054 exynos_dp_init_hpd(regs);
55 exynos_dp_init_aux(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000056
57 return ret;
58}
59
60static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
61{
62 int i;
63 unsigned char sum = 0;
64
65 for (i = 0; i < EDID_BLOCK_LENGTH; i++)
66 sum = sum + edid_data[i];
67
68 return sum;
69}
70
Simon Glass305f5812016-02-21 21:09:00 -070071static unsigned int exynos_dp_read_edid(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +000072{
73 unsigned char edid[EDID_BLOCK_LENGTH * 2];
74 unsigned int extend_block = 0;
75 unsigned char sum;
76 unsigned char test_vector;
77 int retval;
78
79 /*
80 * EDID device address is 0x50.
81 * However, if necessary, you must have set upper address
82 * into E-EDID in I2C device, 0x30.
83 */
84
85 /* Read Extension Flag, Number of 128-byte EDID extension blocks */
Simon Glass305f5812016-02-21 21:09:00 -070086 exynos_dp_read_byte_from_i2c(regs, I2C_EDID_DEVICE_ADDR,
Simon Glass0de70892016-02-21 21:08:44 -070087 EDID_EXTENSION_FLAG, &extend_block);
Donghwa Leeb73a88b2012-07-02 01:16:02 +000088
89 if (extend_block > 0) {
90 printf("DP EDID data includes a single extension!\n");
91
92 /* Read EDID data */
Simon Glass305f5812016-02-21 21:09:00 -070093 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -070094 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +000095 EDID_HEADER_PATTERN,
96 EDID_BLOCK_LENGTH,
97 &edid[EDID_HEADER_PATTERN]);
98 if (retval != 0) {
99 printf("DP EDID Read failed!\n");
100 return -1;
101 }
102 sum = exynos_dp_calc_edid_check_sum(edid);
103 if (sum != 0) {
104 printf("DP EDID bad checksum!\n");
105 return -1;
106 }
107
108 /* Read additional EDID data */
Simon Glass305f5812016-02-21 21:09:00 -0700109 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700110 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000111 EDID_BLOCK_LENGTH,
112 EDID_BLOCK_LENGTH,
113 &edid[EDID_BLOCK_LENGTH]);
114 if (retval != 0) {
115 printf("DP EDID Read failed!\n");
116 return -1;
117 }
118 sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
119 if (sum != 0) {
120 printf("DP EDID bad checksum!\n");
121 return -1;
122 }
123
Simon Glass305f5812016-02-21 21:09:00 -0700124 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Simon Glass0de70892016-02-21 21:08:44 -0700125 &test_vector);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000126 if (test_vector & DPCD_TEST_EDID_READ) {
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_EDID_CHECKSUM,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000129 edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
Simon Glass305f5812016-02-21 21:09:00 -0700130 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700131 DPCD_TEST_RESPONSE,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000132 DPCD_TEST_EDID_CHECKSUM_WRITE);
133 }
134 } else {
135 debug("DP EDID data does not include any extensions.\n");
136
137 /* Read EDID data */
Simon Glass305f5812016-02-21 21:09:00 -0700138 retval = exynos_dp_read_bytes_from_i2c(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700139 I2C_EDID_DEVICE_ADDR,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000140 EDID_HEADER_PATTERN,
141 EDID_BLOCK_LENGTH,
142 &edid[EDID_HEADER_PATTERN]);
143
144 if (retval != 0) {
145 printf("DP EDID Read failed!\n");
146 return -1;
147 }
148 sum = exynos_dp_calc_edid_check_sum(edid);
149 if (sum != 0) {
150 printf("DP EDID bad checksum!\n");
151 return -1;
152 }
153
Simon Glass305f5812016-02-21 21:09:00 -0700154 exynos_dp_read_byte_from_dpcd(regs, DPCD_TEST_REQUEST,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000155 &test_vector);
156 if (test_vector & DPCD_TEST_EDID_READ) {
Simon Glass305f5812016-02-21 21:09:00 -0700157 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700158 DPCD_TEST_EDID_CHECKSUM, edid[EDID_CHECKSUM]);
Simon Glass305f5812016-02-21 21:09:00 -0700159 exynos_dp_write_byte_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700160 DPCD_TEST_RESPONSE,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000161 DPCD_TEST_EDID_CHECKSUM_WRITE);
162 }
163 }
164
165 debug("DP EDID Read success!\n");
166
167 return 0;
168}
169
Simon Glass305f5812016-02-21 21:09:00 -0700170static unsigned int exynos_dp_handle_edid(struct exynos_dp *regs,
171 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000172{
173 unsigned char buf[12];
174 unsigned int ret;
175 unsigned char temp;
176 unsigned char retry_cnt;
177 unsigned char dpcd_rev[16];
178 unsigned char lane_bw[16];
179 unsigned char lane_cnt[16];
180
181 memset(dpcd_rev, 0, 16);
182 memset(lane_bw, 0, 16);
183 memset(lane_cnt, 0, 16);
184 memset(buf, 0, 12);
185
186 retry_cnt = 5;
187 while (retry_cnt) {
188 /* Read DPCD 0x0000-0x000b */
Simon Glass305f5812016-02-21 21:09:00 -0700189 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_DPCD_REV, 12,
Simon Glass0de70892016-02-21 21:08:44 -0700190 buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000191 if (ret != EXYNOS_DP_SUCCESS) {
192 if (retry_cnt == 0) {
193 printf("DP read_byte_from_dpcd() failed\n");
194 return ret;
195 }
196 retry_cnt--;
197 } else
198 break;
199 }
200
201 /* */
202 temp = buf[DPCD_DPCD_REV];
203 if (temp == DP_DPCD_REV_10 || temp == DP_DPCD_REV_11)
Simon Glass305f5812016-02-21 21:09:00 -0700204 priv->dpcd_rev = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000205 else {
206 printf("DP Wrong DPCD Rev : %x\n", temp);
207 return -ENODEV;
208 }
209
210 temp = buf[DPCD_MAX_LINK_RATE];
211 if (temp == DP_LANE_BW_1_62 || temp == DP_LANE_BW_2_70)
Simon Glass305f5812016-02-21 21:09:00 -0700212 priv->lane_bw = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000213 else {
214 printf("DP Wrong MAX LINK RATE : %x\n", temp);
215 return -EINVAL;
216 }
217
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500218 /* Refer VESA Display Port Standard Ver1.1a Page 120 */
Simon Glass305f5812016-02-21 21:09:00 -0700219 if (priv->dpcd_rev == DP_DPCD_REV_11) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000220 temp = buf[DPCD_MAX_LANE_COUNT] & 0x1f;
221 if (buf[DPCD_MAX_LANE_COUNT] & 0x80)
Simon Glass305f5812016-02-21 21:09:00 -0700222 priv->dpcd_efc = 1;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000223 else
Simon Glass305f5812016-02-21 21:09:00 -0700224 priv->dpcd_efc = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000225 } else {
226 temp = buf[DPCD_MAX_LANE_COUNT];
Simon Glass305f5812016-02-21 21:09:00 -0700227 priv->dpcd_efc = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000228 }
229
230 if (temp == DP_LANE_CNT_1 || temp == DP_LANE_CNT_2 ||
231 temp == DP_LANE_CNT_4) {
Simon Glass305f5812016-02-21 21:09:00 -0700232 priv->lane_cnt = temp;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000233 } else {
234 printf("DP Wrong MAX LANE COUNT : %x\n", temp);
235 return -EINVAL;
236 }
237
Simon Glass305f5812016-02-21 21:09:00 -0700238 ret = exynos_dp_read_edid(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000239 if (ret != EXYNOS_DP_SUCCESS) {
240 printf("DP exynos_dp_read_edid() failed\n");
241 return -EINVAL;
242 }
243
244 return ret;
245}
246
Simon Glass305f5812016-02-21 21:09:00 -0700247static void exynos_dp_init_training(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000248{
249 /*
250 * MACRO_RST must be applied after the PLL_LOCK to avoid
251 * the DP inter pair skew issue for at least 10 us
252 */
Simon Glass305f5812016-02-21 21:09:00 -0700253 exynos_dp_reset_macro(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000254
255 /* All DP analog module power up */
Simon Glass305f5812016-02-21 21:09:00 -0700256 exynos_dp_set_analog_power_down(regs, POWER_ALL, 0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000257}
258
Simon Glass305f5812016-02-21 21:09:00 -0700259static unsigned int exynos_dp_link_start(struct exynos_dp *regs,
260 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000261{
262 unsigned char buf[5];
263 unsigned int ret = 0;
264
265 debug("DP: %s was called\n", __func__);
266
Simon Glass305f5812016-02-21 21:09:00 -0700267 priv->lt_info.lt_status = DP_LT_CR;
268 priv->lt_info.ep_loop = 0;
269 priv->lt_info.cr_loop[0] = 0;
270 priv->lt_info.cr_loop[1] = 0;
271 priv->lt_info.cr_loop[2] = 0;
272 priv->lt_info.cr_loop[3] = 0;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000273
274 /* Set sink to D0 (Sink Not Ready) mode. */
Simon Glass305f5812016-02-21 21:09:00 -0700275 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_SINK_POWER_STATE,
Simon Glass0de70892016-02-21 21:08:44 -0700276 DPCD_SET_POWER_STATE_D0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000277 if (ret != EXYNOS_DP_SUCCESS) {
278 printf("DP write_dpcd_byte failed\n");
279 return ret;
280 }
281
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500282 /* Set link rate and count as you want to establish */
Simon Glass305f5812016-02-21 21:09:00 -0700283 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
284 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000285
286 /* Setup RX configuration */
Simon Glass305f5812016-02-21 21:09:00 -0700287 buf[0] = priv->lane_bw;
288 buf[1] = priv->lane_cnt;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000289
Simon Glass305f5812016-02-21 21:09:00 -0700290 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_LINK_BW_SET, 2, buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000291 if (ret != EXYNOS_DP_SUCCESS) {
292 printf("DP write_dpcd_byte failed\n");
293 return ret;
294 }
295
Simon Glass305f5812016-02-21 21:09:00 -0700296 exynos_dp_set_lane_pre_emphasis(regs, PRE_EMPHASIS_LEVEL_0,
297 priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000298
299 /* Set training pattern 1 */
Simon Glass305f5812016-02-21 21:09:00 -0700300 exynos_dp_set_training_pattern(regs, TRAINING_PTN1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000301
302 /* Set RX training pattern */
303 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_1;
304
305 buf[1] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
306 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
307 buf[2] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
308 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
309 buf[3] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
310 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
311 buf[4] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
312 DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
313
Simon Glass305f5812016-02-21 21:09:00 -0700314 ret = exynos_dp_write_bytes_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700315 5, buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000316 if (ret != EXYNOS_DP_SUCCESS) {
317 printf("DP write_dpcd_byte failed\n");
318 return ret;
319 }
320
321 return ret;
322}
323
Simon Glass305f5812016-02-21 21:09:00 -0700324static unsigned int exynos_dp_training_pattern_dis(struct exynos_dp *regs)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000325{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100326 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000327
Simon Glass305f5812016-02-21 21:09:00 -0700328 exynos_dp_set_training_pattern(regs, DP_NONE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000329
Simon Glass305f5812016-02-21 21:09:00 -0700330 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700331 DPCD_TRAINING_PATTERN_DISABLED);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000332 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500333 printf("DP request_link_training_req failed\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000334 return -EAGAIN;
335 }
336
337 return ret;
338}
339
Simon Glass0de70892016-02-21 21:08:44 -0700340static unsigned int exynos_dp_enable_rx_to_enhanced_mode(
Simon Glass305f5812016-02-21 21:09:00 -0700341 struct exynos_dp *regs, unsigned char enable)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000342{
343 unsigned char data;
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100344 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000345
Simon Glass305f5812016-02-21 21:09:00 -0700346 ret = exynos_dp_read_byte_from_dpcd(regs, DPCD_LANE_COUNT_SET,
Simon Glass0de70892016-02-21 21:08:44 -0700347 &data);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000348 if (ret != EXYNOS_DP_SUCCESS) {
349 printf("DP read_from_dpcd failed\n");
350 return -EAGAIN;
351 }
352
353 if (enable)
354 data = DPCD_ENHANCED_FRAME_EN | DPCD_LN_COUNT_SET(data);
355 else
356 data = DPCD_LN_COUNT_SET(data);
357
Simon Glass305f5812016-02-21 21:09:00 -0700358 ret = exynos_dp_write_byte_to_dpcd(regs, DPCD_LANE_COUNT_SET, data);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000359 if (ret != EXYNOS_DP_SUCCESS) {
360 printf("DP write_to_dpcd failed\n");
361 return -EAGAIN;
362
363 }
364
365 return ret;
366}
367
Simon Glass305f5812016-02-21 21:09:00 -0700368static unsigned int exynos_dp_set_enhanced_mode(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700369 unsigned char enhance_mode)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000370{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100371 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000372
Simon Glass305f5812016-02-21 21:09:00 -0700373 ret = exynos_dp_enable_rx_to_enhanced_mode(regs, enhance_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000374 if (ret != EXYNOS_DP_SUCCESS) {
375 printf("DP rx_enhance_mode failed\n");
376 return -EAGAIN;
377 }
378
Simon Glass305f5812016-02-21 21:09:00 -0700379 exynos_dp_enable_enhanced_mode(regs, enhance_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000380
381 return ret;
382}
383
Simon Glass305f5812016-02-21 21:09:00 -0700384static int exynos_dp_read_dpcd_lane_stat(struct exynos_dp *regs,
385 struct exynos_dp_priv *priv,
Simon Glass0de70892016-02-21 21:08:44 -0700386 unsigned char *status)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000387{
388 unsigned int ret, i;
389 unsigned char buf[2];
390 unsigned char lane_stat[DP_LANE_CNT_4] = {0,};
391 unsigned char shift_val[DP_LANE_CNT_4] = {0,};
392
393 shift_val[0] = 0;
394 shift_val[1] = 4;
395 shift_val[2] = 0;
396 shift_val[3] = 4;
397
Simon Glass305f5812016-02-21 21:09:00 -0700398 ret = exynos_dp_read_bytes_from_dpcd(regs, DPCD_LANE0_1_STATUS, 2,
Simon Glass0de70892016-02-21 21:08:44 -0700399 buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000400 if (ret != EXYNOS_DP_SUCCESS) {
401 printf("DP read lane status failed\n");
402 return ret;
403 }
404
Simon Glass305f5812016-02-21 21:09:00 -0700405 for (i = 0; i < priv->lane_cnt; i++) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000406 lane_stat[i] = (buf[(i / 2)] >> shift_val[i]) & 0x0f;
407 if (lane_stat[0] != lane_stat[i]) {
408 printf("Wrong lane status\n");
409 return -EINVAL;
410 }
411 }
412
413 *status = lane_stat[0];
414
415 return ret;
416}
417
Simon Glass305f5812016-02-21 21:09:00 -0700418static unsigned int exynos_dp_read_dpcd_adj_req(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700419 unsigned char lane_num, unsigned char *sw, unsigned char *em)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000420{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100421 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000422 unsigned char buf;
423 unsigned int dpcd_addr;
424 unsigned char shift_val[DP_LANE_CNT_4] = {0, 4, 0, 4};
425
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500426 /* lane_num value is used as array index, so this range 0 ~ 3 */
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000427 dpcd_addr = DPCD_ADJUST_REQUEST_LANE0_1 + (lane_num / 2);
428
Simon Glass305f5812016-02-21 21:09:00 -0700429 ret = exynos_dp_read_byte_from_dpcd(regs, dpcd_addr, &buf);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000430 if (ret != EXYNOS_DP_SUCCESS) {
431 printf("DP read adjust request failed\n");
432 return -EAGAIN;
433 }
434
435 *sw = ((buf >> shift_val[lane_num]) & 0x03);
436 *em = ((buf >> shift_val[lane_num]) & 0x0c) >> 2;
437
438 return ret;
439}
440
Simon Glass305f5812016-02-21 21:09:00 -0700441static int exynos_dp_equalizer_err_link(struct exynos_dp *regs,
442 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000443{
444 int ret;
445
Simon Glass305f5812016-02-21 21:09:00 -0700446 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000447 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500448 printf("DP training_pattern_disable() failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700449 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000450 }
451
Simon Glass305f5812016-02-21 21:09:00 -0700452 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000453 if (ret != EXYNOS_DP_SUCCESS) {
454 printf("DP set_enhanced_mode() failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700455 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000456 }
457
458 return ret;
459}
460
Simon Glass305f5812016-02-21 21:09:00 -0700461static int exynos_dp_reduce_link_rate(struct exynos_dp *regs,
462 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000463{
464 int ret;
465
Simon Glass305f5812016-02-21 21:09:00 -0700466 if (priv->lane_bw == DP_LANE_BW_2_70) {
467 priv->lane_bw = DP_LANE_BW_1_62;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000468 printf("DP Change lane bw to 1.62Gbps\n");
Simon Glass305f5812016-02-21 21:09:00 -0700469 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000470 ret = EXYNOS_DP_SUCCESS;
471 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700472 ret = exynos_dp_training_pattern_dis(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000473 if (ret != EXYNOS_DP_SUCCESS)
474 printf("DP training_patter_disable() failed\n");
475
Simon Glass305f5812016-02-21 21:09:00 -0700476 ret = exynos_dp_set_enhanced_mode(regs, priv->dpcd_efc);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000477 if (ret != EXYNOS_DP_SUCCESS)
478 printf("DP set_enhanced_mode() failed\n");
479
Simon Glass305f5812016-02-21 21:09:00 -0700480 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000481 }
482
483 return ret;
484}
485
Simon Glass305f5812016-02-21 21:09:00 -0700486static unsigned int exynos_dp_process_clock_recovery(struct exynos_dp *regs,
487 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000488{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100489 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000490 unsigned char lane_stat;
491 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0, };
492 unsigned int i;
493 unsigned char adj_req_sw;
494 unsigned char adj_req_em;
495 unsigned char buf[5];
496
497 debug("DP: %s was called\n", __func__);
498 mdelay(1);
499
Simon Glass305f5812016-02-21 21:09:00 -0700500 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000501 if (ret != EXYNOS_DP_SUCCESS) {
502 printf("DP read lane status failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700503 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000504 return ret;
505 }
506
507 if (lane_stat & DP_LANE_STAT_CR_DONE) {
508 debug("DP clock Recovery training succeed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700509 exynos_dp_set_training_pattern(regs, TRAINING_PTN2);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000510
Simon Glass305f5812016-02-21 21:09:00 -0700511 for (i = 0; i < priv->lane_cnt; i++) {
512 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Simon Glass0de70892016-02-21 21:08:44 -0700513 &adj_req_sw, &adj_req_em);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000514 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass305f5812016-02-21 21:09:00 -0700515 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000516 return ret;
517 }
518
519 lt_ctl_val[i] = 0;
520 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
521
522 if ((adj_req_sw == VOLTAGE_LEVEL_3)
523 || (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
524 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
525 MAX_PRE_EMPHASIS_REACH_3;
526 }
Simon Glass305f5812016-02-21 21:09:00 -0700527 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700528 lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000529 }
530
531 buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_2;
532 buf[1] = lt_ctl_val[0];
533 buf[2] = lt_ctl_val[1];
534 buf[3] = lt_ctl_val[2];
535 buf[4] = lt_ctl_val[3];
536
Simon Glass305f5812016-02-21 21:09:00 -0700537 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000538 DPCD_TRAINING_PATTERN_SET, 5, buf);
539 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500540 printf("DP write training pattern1 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700541 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000542 return ret;
543 } else
Simon Glass305f5812016-02-21 21:09:00 -0700544 priv->lt_info.lt_status = DP_LT_ET;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000545 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700546 for (i = 0; i < priv->lane_cnt; i++) {
Simon Glass0de70892016-02-21 21:08:44 -0700547 lt_ctl_val[i] = exynos_dp_get_lanex_pre_emphasis(
Simon Glass305f5812016-02-21 21:09:00 -0700548 regs, i);
549 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000550 &adj_req_sw, &adj_req_em);
551 if (ret != EXYNOS_DP_SUCCESS) {
552 printf("DP read adj req failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700553 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000554 return ret;
555 }
556
557 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
558 (adj_req_em == PRE_EMPHASIS_LEVEL_3))
Simon Glass305f5812016-02-21 21:09:00 -0700559 ret = exynos_dp_reduce_link_rate(regs,
560 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000561
562 if ((DRIVE_CURRENT_SET_0_GET(lt_ctl_val[i]) ==
563 adj_req_sw) &&
564 (PRE_EMPHASIS_SET_0_GET(lt_ctl_val[i]) ==
565 adj_req_em)) {
Simon Glass305f5812016-02-21 21:09:00 -0700566 priv->lt_info.cr_loop[i]++;
567 if (priv->lt_info.cr_loop[i] == MAX_CR_LOOP)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000568 ret = exynos_dp_reduce_link_rate(
Simon Glass305f5812016-02-21 21:09:00 -0700569 regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000570 }
571
572 lt_ctl_val[i] = 0;
573 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
574
575 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
576 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
577 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
578 MAX_PRE_EMPHASIS_REACH_3;
579 }
Simon Glass305f5812016-02-21 21:09:00 -0700580 exynos_dp_set_lanex_pre_emphasis(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700581 lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000582 }
583
Simon Glass305f5812016-02-21 21:09:00 -0700584 ret = exynos_dp_write_bytes_to_dpcd(regs,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000585 DPCD_TRAINING_LANE0_SET, 4, lt_ctl_val);
586 if (ret != EXYNOS_DP_SUCCESS) {
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500587 printf("DP write training pattern2 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700588 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000589 return ret;
590 }
591 }
592
593 return ret;
594}
595
Simon Glass0de70892016-02-21 21:08:44 -0700596static unsigned int exynos_dp_process_equalizer_training(
Simon Glass305f5812016-02-21 21:09:00 -0700597 struct exynos_dp *regs, struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000598{
Heinrich Schuchardt67be5d72018-03-19 07:46:08 +0100599 unsigned int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000600 unsigned char lane_stat, adj_req_sw, adj_req_em, i;
601 unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0,};
602 unsigned char interlane_aligned = 0;
603 unsigned char f_bw;
604 unsigned char f_lane_cnt;
605 unsigned char sink_stat;
606
607 mdelay(1);
608
Simon Glass305f5812016-02-21 21:09:00 -0700609 ret = exynos_dp_read_dpcd_lane_stat(regs, priv, &lane_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000610 if (ret != EXYNOS_DP_SUCCESS) {
611 printf("DP read lane status failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700612 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000613 return ret;
614 }
615
616 debug("DP lane stat : %x\n", lane_stat);
617
618 if (lane_stat & DP_LANE_STAT_CR_DONE) {
Simon Glass305f5812016-02-21 21:09:00 -0700619 ret = exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700620 DPCD_LN_ALIGN_UPDATED,
621 &sink_stat);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000622 if (ret != EXYNOS_DP_SUCCESS) {
Simon Glass305f5812016-02-21 21:09:00 -0700623 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000624
625 return ret;
626 }
627
628 interlane_aligned = (sink_stat & DPCD_INTERLANE_ALIGN_DONE);
629
Simon Glass305f5812016-02-21 21:09:00 -0700630 for (i = 0; i < priv->lane_cnt; i++) {
631 ret = exynos_dp_read_dpcd_adj_req(regs, i,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000632 &adj_req_sw, &adj_req_em);
633 if (ret != EXYNOS_DP_SUCCESS) {
634 printf("DP read adj req 1 failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700635 priv->lt_info.lt_status = DP_LT_FAIL;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000636
637 return ret;
638 }
639
640 lt_ctl_val[i] = 0;
641 lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
642
643 if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
644 (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
645 lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3;
646 lt_ctl_val[i] |= MAX_PRE_EMPHASIS_REACH_3;
647 }
648 }
649
650 if (((lane_stat&DP_LANE_STAT_CE_DONE) &&
651 (lane_stat&DP_LANE_STAT_SYM_LOCK))
652 && (interlane_aligned == DPCD_INTERLANE_ALIGN_DONE)) {
653 debug("DP Equalizer training succeed\n");
654
Simon Glass305f5812016-02-21 21:09:00 -0700655 f_bw = exynos_dp_get_link_bandwidth(regs);
656 f_lane_cnt = exynos_dp_get_lane_count(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000657
658 debug("DP final BandWidth : %x\n", f_bw);
659 debug("DP final Lane Count : %x\n", f_lane_cnt);
660
Simon Glass305f5812016-02-21 21:09:00 -0700661 priv->lt_info.lt_status = DP_LT_FINISHED;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000662
Simon Glass305f5812016-02-21 21:09:00 -0700663 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000664
665 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700666 priv->lt_info.ep_loop++;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000667
Simon Glass305f5812016-02-21 21:09:00 -0700668 if (priv->lt_info.ep_loop > MAX_EQ_LOOP) {
669 if (priv->lane_bw == DP_LANE_BW_2_70) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000670 ret = exynos_dp_reduce_link_rate(
Simon Glass305f5812016-02-21 21:09:00 -0700671 regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000672 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700673 priv->lt_info.lt_status =
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000674 DP_LT_FAIL;
Simon Glass305f5812016-02-21 21:09:00 -0700675 exynos_dp_equalizer_err_link(regs,
676 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000677 }
678 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700679 for (i = 0; i < priv->lane_cnt; i++)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000680 exynos_dp_set_lanex_pre_emphasis(
Simon Glass305f5812016-02-21 21:09:00 -0700681 regs, lt_ctl_val[i], i);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000682
Simon Glass305f5812016-02-21 21:09:00 -0700683 ret = exynos_dp_write_bytes_to_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700684 DPCD_TRAINING_LANE0_SET,
685 4, lt_ctl_val);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000686 if (ret != EXYNOS_DP_SUCCESS) {
687 printf("DP set lt pattern failed\n");
Simon Glass305f5812016-02-21 21:09:00 -0700688 priv->lt_info.lt_status =
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000689 DP_LT_FAIL;
Simon Glass305f5812016-02-21 21:09:00 -0700690 exynos_dp_equalizer_err_link(regs,
691 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000692 }
693 }
694 }
Simon Glass305f5812016-02-21 21:09:00 -0700695 } else if (priv->lane_bw == DP_LANE_BW_2_70) {
696 ret = exynos_dp_reduce_link_rate(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000697 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700698 priv->lt_info.lt_status = DP_LT_FAIL;
699 exynos_dp_equalizer_err_link(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000700 }
701
702 return ret;
703}
704
Simon Glass305f5812016-02-21 21:09:00 -0700705static unsigned int exynos_dp_sw_link_training(struct exynos_dp *regs,
706 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000707{
708 unsigned int ret = 0;
709 int training_finished;
710
711 /* Turn off unnecessary lane */
Simon Glass305f5812016-02-21 21:09:00 -0700712 if (priv->lane_cnt == 1)
713 exynos_dp_set_analog_power_down(regs, CH1_BLOCK, 1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000714
715 training_finished = 0;
716
Simon Glass305f5812016-02-21 21:09:00 -0700717 priv->lt_info.lt_status = DP_LT_START;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000718
719 /* Process here */
720 while (!training_finished) {
Simon Glass305f5812016-02-21 21:09:00 -0700721 switch (priv->lt_info.lt_status) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000722 case DP_LT_START:
Simon Glass305f5812016-02-21 21:09:00 -0700723 ret = exynos_dp_link_start(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000724 if (ret != EXYNOS_DP_SUCCESS) {
725 printf("DP LT:link start failed\n");
726 return ret;
727 }
728 break;
729 case DP_LT_CR:
Simon Glass305f5812016-02-21 21:09:00 -0700730 ret = exynos_dp_process_clock_recovery(regs,
731 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000732 if (ret != EXYNOS_DP_SUCCESS) {
733 printf("DP LT:clock recovery failed\n");
734 return ret;
735 }
736 break;
737 case DP_LT_ET:
Simon Glass305f5812016-02-21 21:09:00 -0700738 ret = exynos_dp_process_equalizer_training(regs,
739 priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000740 if (ret != EXYNOS_DP_SUCCESS) {
741 printf("DP LT:equalizer training failed\n");
742 return ret;
743 }
744 break;
745 case DP_LT_FINISHED:
746 training_finished = 1;
747 break;
748 case DP_LT_FAIL:
749 return -1;
750 }
751 }
752
753 return ret;
754}
755
Simon Glass305f5812016-02-21 21:09:00 -0700756static unsigned int exynos_dp_set_link_train(struct exynos_dp *regs,
757 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000758{
759 unsigned int ret;
760
Simon Glass305f5812016-02-21 21:09:00 -0700761 exynos_dp_init_training(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000762
Simon Glass305f5812016-02-21 21:09:00 -0700763 ret = exynos_dp_sw_link_training(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000764 if (ret != EXYNOS_DP_SUCCESS)
Robert P. J. Day971abcd2015-12-16 11:31:23 -0500765 printf("DP dp_sw_link_training() failed\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000766
767 return ret;
768}
769
Simon Glass305f5812016-02-21 21:09:00 -0700770static void exynos_dp_enable_scramble(struct exynos_dp *regs,
Simon Glass0de70892016-02-21 21:08:44 -0700771 unsigned int enable)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000772{
773 unsigned char data;
774
775 if (enable) {
Simon Glass305f5812016-02-21 21:09:00 -0700776 exynos_dp_enable_scrambling(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000777
Simon Glass305f5812016-02-21 21:09:00 -0700778 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700779 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass305f5812016-02-21 21:09:00 -0700780 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000781 (u8)(data & ~DPCD_SCRAMBLING_DISABLED));
782 } else {
Simon Glass305f5812016-02-21 21:09:00 -0700783 exynos_dp_enable_scrambling(regs, DP_DISABLE);
784 exynos_dp_read_byte_from_dpcd(regs,
Simon Glass0de70892016-02-21 21:08:44 -0700785 DPCD_TRAINING_PATTERN_SET, &data);
Simon Glass305f5812016-02-21 21:09:00 -0700786 exynos_dp_write_byte_to_dpcd(regs, DPCD_TRAINING_PATTERN_SET,
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000787 (u8)(data | DPCD_SCRAMBLING_DISABLED));
788 }
789}
790
Simon Glass305f5812016-02-21 21:09:00 -0700791static unsigned int exynos_dp_config_video(struct exynos_dp *regs,
792 struct exynos_dp_priv *priv)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000793{
794 unsigned int ret = 0;
795 unsigned int retry_cnt;
796
797 mdelay(1);
798
Simon Glass305f5812016-02-21 21:09:00 -0700799 if (priv->video_info.master_mode) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000800 printf("DP does not support master mode\n");
801 return -ENODEV;
802 } else {
803 /* debug slave */
Simon Glass305f5812016-02-21 21:09:00 -0700804 exynos_dp_config_video_slave_mode(regs,
805 &priv->video_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000806 }
807
Simon Glass305f5812016-02-21 21:09:00 -0700808 exynos_dp_set_video_color_format(regs, &priv->video_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000809
Simon Glass305f5812016-02-21 21:09:00 -0700810 if (priv->video_info.bist_mode) {
811 if (exynos_dp_config_video_bist(regs, priv) != 0)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000812 return -1;
813 }
814
Simon Glass305f5812016-02-21 21:09:00 -0700815 ret = exynos_dp_get_pll_lock_status(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000816 if (ret != PLL_LOCKED) {
817 printf("DP PLL is not locked yet\n");
818 return -EIO;
819 }
820
Simon Glass305f5812016-02-21 21:09:00 -0700821 if (priv->video_info.master_mode == 0) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000822 retry_cnt = 10;
823 while (retry_cnt) {
Simon Glass305f5812016-02-21 21:09:00 -0700824 ret = exynos_dp_is_slave_video_stream_clock_on(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000825 if (ret != EXYNOS_DP_SUCCESS) {
826 if (retry_cnt == 0) {
827 printf("DP stream_clock_on failed\n");
828 return ret;
829 }
830 retry_cnt--;
831 mdelay(1);
832 } else
833 break;
834 }
835 }
836
837 /* Set to use the register calculated M/N video */
Simon Glass305f5812016-02-21 21:09:00 -0700838 exynos_dp_set_video_cr_mn(regs, CALCULATED_M, 0, 0);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000839
840 /* For video bist, Video timing must be generated by register */
Simon Glass305f5812016-02-21 21:09:00 -0700841 exynos_dp_set_video_timing_mode(regs, VIDEO_TIMING_FROM_CAPTURE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000842
843 /* Enable video bist */
Simon Glass305f5812016-02-21 21:09:00 -0700844 if (priv->video_info.bist_pattern != COLOR_RAMP &&
845 priv->video_info.bist_pattern != BALCK_WHITE_V_LINES &&
846 priv->video_info.bist_pattern != COLOR_SQUARE)
847 exynos_dp_enable_video_bist(regs,
848 priv->video_info.bist_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000849 else
Simon Glass305f5812016-02-21 21:09:00 -0700850 exynos_dp_enable_video_bist(regs, DP_DISABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000851
852 /* Disable video mute */
Simon Glass305f5812016-02-21 21:09:00 -0700853 exynos_dp_enable_video_mute(regs, DP_DISABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000854
855 /* Configure video Master or Slave mode */
Simon Glass305f5812016-02-21 21:09:00 -0700856 exynos_dp_enable_video_master(regs,
857 priv->video_info.master_mode);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000858
859 /* Enable video */
Simon Glass305f5812016-02-21 21:09:00 -0700860 exynos_dp_start_video(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000861
Simon Glass305f5812016-02-21 21:09:00 -0700862 if (priv->video_info.master_mode == 0) {
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000863 retry_cnt = 100;
864 while (retry_cnt) {
Simon Glass305f5812016-02-21 21:09:00 -0700865 ret = exynos_dp_is_video_stream_on(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000866 if (ret != EXYNOS_DP_SUCCESS) {
867 if (retry_cnt == 0) {
868 printf("DP Timeout of video stream\n");
869 return ret;
870 }
871 retry_cnt--;
872 mdelay(5);
873 } else
874 break;
875 }
876 }
877
878 return ret;
879}
Ajay Kumar65f50f92013-02-21 23:53:06 +0000880
Simon Glassaad29ae2020-12-03 16:55:21 -0700881static int exynos_dp_of_to_plat(struct udevice *dev)
Ajay Kumar65f50f92013-02-21 23:53:06 +0000882{
Simon Glassa1015ad2016-02-21 21:09:01 -0700883 struct exynos_dp_priv *priv = dev_get_priv(dev);
884 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700885 unsigned int node = dev_of_offset(dev);
Simon Glassa1015ad2016-02-21 21:09:01 -0700886 fdt_addr_t addr;
Ajay Kumar65f50f92013-02-21 23:53:06 +0000887
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900888 addr = dev_read_addr(dev);
Simon Glassa1015ad2016-02-21 21:09:01 -0700889 if (addr == FDT_ADDR_T_NONE) {
890 debug("Can't get the DP base address\n");
891 return -EINVAL;
892 }
893 priv->regs = (struct exynos_dp *)addr;
Simon Glass305f5812016-02-21 21:09:00 -0700894 priv->disp_info.h_res = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000895 "samsung,h-res", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700896 priv->disp_info.h_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000897 "samsung,h-sync-width", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700898 priv->disp_info.h_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000899 "samsung,h-back-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700900 priv->disp_info.h_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000901 "samsung,h-front-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700902 priv->disp_info.v_res = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000903 "samsung,v-res", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700904 priv->disp_info.v_sync_width = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000905 "samsung,v-sync-width", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700906 priv->disp_info.v_back_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000907 "samsung,v-back-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700908 priv->disp_info.v_front_porch = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000909 "samsung,v-front-porch", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700910 priv->disp_info.v_sync_rate = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000911 "samsung,v-sync-rate", 0);
912
Simon Glass305f5812016-02-21 21:09:00 -0700913 priv->lt_info.lt_status = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000914 "samsung,lt-status", 0);
915
Simon Glass305f5812016-02-21 21:09:00 -0700916 priv->video_info.master_mode = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000917 "samsung,master-mode", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700918 priv->video_info.bist_mode = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000919 "samsung,bist-mode", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700920 priv->video_info.bist_pattern = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000921 "samsung,bist-pattern", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700922 priv->video_info.h_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000923 "samsung,h-sync-polarity", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700924 priv->video_info.v_sync_polarity = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000925 "samsung,v-sync-polarity", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700926 priv->video_info.interlaced = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000927 "samsung,interlaced", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700928 priv->video_info.color_space = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000929 "samsung,color-space", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700930 priv->video_info.dynamic_range = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000931 "samsung,dynamic-range", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700932 priv->video_info.ycbcr_coeff = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000933 "samsung,ycbcr-coeff", 0);
Simon Glass305f5812016-02-21 21:09:00 -0700934 priv->video_info.color_depth = fdtdec_get_int(blob, node,
Ajay Kumar65f50f92013-02-21 23:53:06 +0000935 "samsung,color-depth", 0);
936 return 0;
937}
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000938
Simon Glassa1015ad2016-02-21 21:09:01 -0700939static int exynos_dp_bridge_init(struct udevice *dev)
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000940{
Simon Glassa1015ad2016-02-21 21:09:01 -0700941 const int max_tries = 10;
942 int num_tries;
943 int ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000944
Simon Glassa1015ad2016-02-21 21:09:01 -0700945 debug("%s\n", __func__);
946 ret = video_bridge_attach(dev);
947 if (ret) {
948 debug("video bridge init failed: %d\n", ret);
949 return ret;
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000950 }
951
Simon Glassa1015ad2016-02-21 21:09:01 -0700952 /*
953 * We need to wait for 90ms after bringing up the bridge since there
954 * is a phantom "high" on the HPD chip during its bootup. The phantom
955 * high comes within 7ms of de-asserting PD and persists for at least
956 * 15ms. The real high comes roughly 50ms after PD is de-asserted. The
957 * phantom high makes it hard for us to know when the NXP chip is up.
958 */
959 mdelay(90);
Donghwa Leeb73a88b2012-07-02 01:16:02 +0000960
Simon Glassa1015ad2016-02-21 21:09:01 -0700961 for (num_tries = 0; num_tries < max_tries; num_tries++) {
962 /* Check HPD. If it's high, or we don't have it, all is well */
963 ret = video_bridge_check_attached(dev);
964 if (!ret || ret == -ENOENT)
965 return 0;
Simon Glass0de70892016-02-21 21:08:44 -0700966
Simon Glassa1015ad2016-02-21 21:09:01 -0700967 debug("%s: eDP bridge failed to come up; try %d of %d\n",
968 __func__, num_tries, max_tries);
969 }
Ajay Kumarca7c3e12013-02-21 23:53:04 +0000970
Simon Glassa1015ad2016-02-21 21:09:01 -0700971 /* Immediately go into bridge reset if the hp line is not high */
972 return -EIO;
973}
974
975static int exynos_dp_bridge_setup(const void *blob)
976{
977 const int max_tries = 2;
978 int num_tries;
979 struct udevice *dev;
980 int ret;
981
982 /* Configure I2C registers for Parade bridge */
983 ret = uclass_get_device(UCLASS_VIDEO_BRIDGE, 0, &dev);
984 if (ret) {
985 debug("video bridge init failed: %d\n", ret);
986 return ret;
987 }
988
989 if (strncmp(dev->driver->name, "parade", 6)) {
990 /* Mux HPHPD to the special hotplug detect mode */
991 exynos_pinmux_config(PERIPH_ID_DPHPD, 0);
992 }
993
994 for (num_tries = 0; num_tries < max_tries; num_tries++) {
995 ret = exynos_dp_bridge_init(dev);
996 if (!ret)
997 return 0;
998 if (num_tries == max_tries - 1)
999 break;
1000
1001 /*
1002 * If we're here, the bridge chip failed to initialise.
1003 * Power down the bridge in an attempt to reset.
1004 */
1005 video_bridge_set_active(dev, false);
1006
1007 /*
1008 * Arbitrarily wait 300ms here with DP_N low. Don't know for
1009 * sure how long we should wait, but we're being paranoid.
1010 */
1011 mdelay(300);
1012 }
1013
1014 return ret;
1015}
1016int exynos_dp_enable(struct udevice *dev, int panel_bpp,
1017 const struct display_timing *timing)
1018{
1019 struct exynos_dp_priv *priv = dev_get_priv(dev);
1020 struct exynos_dp *regs = priv->regs;
1021 unsigned int ret;
1022
1023 debug("%s: start\n", __func__);
Simon Glass305f5812016-02-21 21:09:00 -07001024 exynos_dp_disp_info(&priv->disp_info);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001025
Simon Glassa1015ad2016-02-21 21:09:01 -07001026 ret = exynos_dp_bridge_setup(gd->fdt_blob);
1027 if (ret && ret != -ENODEV)
1028 printf("LCD bridge failed to enable: %d\n", ret);
1029
Simon Glassbfd239c2016-02-21 21:08:57 -07001030 exynos_dp_phy_ctrl(1);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001031
Simon Glass305f5812016-02-21 21:09:00 -07001032 ret = exynos_dp_init_dp(regs);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001033 if (ret != EXYNOS_DP_SUCCESS) {
1034 printf("DP exynos_dp_init_dp() failed\n");
1035 return ret;
1036 }
1037
Simon Glass305f5812016-02-21 21:09:00 -07001038 ret = exynos_dp_handle_edid(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001039 if (ret != EXYNOS_DP_SUCCESS) {
1040 printf("EDP handle_edid fail\n");
1041 return ret;
1042 }
1043
Simon Glass305f5812016-02-21 21:09:00 -07001044 ret = exynos_dp_set_link_train(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001045 if (ret != EXYNOS_DP_SUCCESS) {
1046 printf("DP link training fail\n");
1047 return ret;
1048 }
1049
Simon Glass305f5812016-02-21 21:09:00 -07001050 exynos_dp_enable_scramble(regs, DP_ENABLE);
1051 exynos_dp_enable_rx_to_enhanced_mode(regs, DP_ENABLE);
1052 exynos_dp_enable_enhanced_mode(regs, DP_ENABLE);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001053
Simon Glass305f5812016-02-21 21:09:00 -07001054 exynos_dp_set_link_bandwidth(regs, priv->lane_bw);
1055 exynos_dp_set_lane_count(regs, priv->lane_cnt);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001056
Simon Glass305f5812016-02-21 21:09:00 -07001057 exynos_dp_init_video(regs);
1058 ret = exynos_dp_config_video(regs, priv);
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001059 if (ret != EXYNOS_DP_SUCCESS) {
1060 printf("Exynos DP init failed\n");
1061 return ret;
1062 }
1063
Simon Glass5e7ab072015-07-02 18:16:14 -06001064 debug("Exynos DP init done\n");
Donghwa Leeb73a88b2012-07-02 01:16:02 +00001065
1066 return ret;
1067}
Simon Glassa1015ad2016-02-21 21:09:01 -07001068
1069
1070static const struct dm_display_ops exynos_dp_ops = {
1071 .enable = exynos_dp_enable,
1072};
1073
1074static const struct udevice_id exynos_dp_ids[] = {
1075 { .compatible = "samsung,exynos5-dp" },
1076 { }
1077};
1078
1079U_BOOT_DRIVER(exynos_dp) = {
Dongjin Kime51b7772017-10-27 23:08:51 -04001080 .name = "exynos_dp",
Simon Glassa1015ad2016-02-21 21:09:01 -07001081 .id = UCLASS_DISPLAY,
1082 .of_match = exynos_dp_ids,
1083 .ops = &exynos_dp_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07001084 .of_to_plat = exynos_dp_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001085 .priv_auto = sizeof(struct exynos_dp_priv),
Simon Glassa1015ad2016-02-21 21:09:01 -07001086};