blob: e00b8adde18d8c3d77b69a94a1a283db1f9dd937 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 * Jerome Glisse
26 */
27#include <drm/drmP.h>
28#include <drm/amdgpu_drm.h>
29#include "amdgpu.h"
30
31#include "atom.h"
32#include "atom-bits.h"
33#include "atombios_encoders.h"
34#include "atombios_dp.h"
35#include "amdgpu_connectors.h"
36#include "amdgpu_atombios.h"
37#include <drm/drm_dp_helper.h>
38
39/* move these to drm_dp_helper.c/h */
40#define DP_LINK_CONFIGURATION_SIZE 9
41#define DP_DPCD_SIZE DP_RECEIVER_CAP_SIZE
42
43static char *voltage_names[] = {
44 "0.4V", "0.6V", "0.8V", "1.2V"
45};
46static char *pre_emph_names[] = {
47 "0dB", "3.5dB", "6dB", "9.5dB"
48};
49
50/***** amdgpu AUX functions *****/
51
52union aux_channel_transaction {
53 PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
54 PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
55};
56
57static int amdgpu_atombios_dp_process_aux_ch(struct amdgpu_i2c_chan *chan,
58 u8 *send, int send_bytes,
59 u8 *recv, int recv_size,
60 u8 delay, u8 *ack)
61{
62 struct drm_device *dev = chan->dev;
63 struct amdgpu_device *adev = dev->dev_private;
64 union aux_channel_transaction args;
65 int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
66 unsigned char *base;
67 int recv_bytes;
68 int r = 0;
69
70 memset(&args, 0, sizeof(args));
71
72 mutex_lock(&chan->mutex);
73
74 base = (unsigned char *)(adev->mode_info.atom_context->scratch + 1);
75
76 amdgpu_atombios_copy_swap(base, send, send_bytes, true);
77
78 args.v2.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
79 args.v2.lpDataOut = cpu_to_le16((u16)(16 + 4));
80 args.v2.ucDataOutLen = 0;
81 args.v2.ucChannelID = chan->rec.i2c_id;
82 args.v2.ucDelay = delay / 10;
83 args.v2.ucHPD_ID = chan->rec.hpd;
84
85 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
86
87 *ack = args.v2.ucReplyStatus;
88
89 /* timeout */
90 if (args.v2.ucReplyStatus == 1) {
91 DRM_DEBUG_KMS("dp_aux_ch timeout\n");
92 r = -ETIMEDOUT;
93 goto done;
94 }
95
96 /* flags not zero */
97 if (args.v2.ucReplyStatus == 2) {
98 DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
99 r = -EIO;
100 goto done;
101 }
102
103 /* error */
104 if (args.v2.ucReplyStatus == 3) {
105 DRM_DEBUG_KMS("dp_aux_ch error\n");
106 r = -EIO;
107 goto done;
108 }
109
110 recv_bytes = args.v1.ucDataOutLen;
111 if (recv_bytes > recv_size)
112 recv_bytes = recv_size;
113
114 if (recv && recv_size)
115 amdgpu_atombios_copy_swap(recv, base + 16, recv_bytes, false);
116
117 r = recv_bytes;
118done:
119 mutex_unlock(&chan->mutex);
120
121 return r;
122}
123
124#define BARE_ADDRESS_SIZE 3
125#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
126
127static ssize_t
128amdgpu_atombios_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
129{
130 struct amdgpu_i2c_chan *chan =
131 container_of(aux, struct amdgpu_i2c_chan, aux);
132 int ret;
133 u8 tx_buf[20];
134 size_t tx_size;
135 u8 ack, delay = 0;
136
137 if (WARN_ON(msg->size > 16))
138 return -E2BIG;
139
140 tx_buf[0] = msg->address & 0xff;
141 tx_buf[1] = msg->address >> 8;
142 tx_buf[2] = msg->request << 4;
143 tx_buf[3] = msg->size ? (msg->size - 1) : 0;
144
145 switch (msg->request & ~DP_AUX_I2C_MOT) {
146 case DP_AUX_NATIVE_WRITE:
147 case DP_AUX_I2C_WRITE:
148 /* tx_size needs to be 4 even for bare address packets since the atom
149 * table needs the info in tx_buf[3].
150 */
151 tx_size = HEADER_SIZE + msg->size;
152 if (msg->size == 0)
153 tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
154 else
155 tx_buf[3] |= tx_size << 4;
156 memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
157 ret = amdgpu_atombios_dp_process_aux_ch(chan,
158 tx_buf, tx_size, NULL, 0, delay, &ack);
159 if (ret >= 0)
160 /* Return payload size. */
161 ret = msg->size;
162 break;
163 case DP_AUX_NATIVE_READ:
164 case DP_AUX_I2C_READ:
165 /* tx_size needs to be 4 even for bare address packets since the atom
166 * table needs the info in tx_buf[3].
167 */
168 tx_size = HEADER_SIZE;
169 if (msg->size == 0)
170 tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
171 else
172 tx_buf[3] |= tx_size << 4;
173 ret = amdgpu_atombios_dp_process_aux_ch(chan,
174 tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
175 break;
176 default:
177 ret = -EINVAL;
178 break;
179 }
180
181 if (ret >= 0)
182 msg->reply = ack >> 4;
183
184 return ret;
185}
186
187void amdgpu_atombios_dp_aux_init(struct amdgpu_connector *amdgpu_connector)
188{
189 int ret;
190
191 amdgpu_connector->ddc_bus->rec.hpd = amdgpu_connector->hpd.hpd;
192 amdgpu_connector->ddc_bus->aux.dev = amdgpu_connector->base.kdev;
193 amdgpu_connector->ddc_bus->aux.transfer = amdgpu_atombios_dp_aux_transfer;
194 ret = drm_dp_aux_register(&amdgpu_connector->ddc_bus->aux);
195 if (!ret)
196 amdgpu_connector->ddc_bus->has_aux = true;
197
198 WARN(ret, "drm_dp_aux_register_i2c_bus() failed with error %d\n", ret);
199}
200
201/***** general DP utility functions *****/
202
203#define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_LEVEL_3
204#define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPH_LEVEL_3
205
206static void amdgpu_atombios_dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
207 int lane_count,
208 u8 train_set[4])
209{
210 u8 v = 0;
211 u8 p = 0;
212 int lane;
213
214 for (lane = 0; lane < lane_count; lane++) {
215 u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
216 u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
217
218 DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
219 lane,
220 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
221 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
222
223 if (this_v > v)
224 v = this_v;
225 if (this_p > p)
226 p = this_p;
227 }
228
229 if (v >= DP_VOLTAGE_MAX)
230 v |= DP_TRAIN_MAX_SWING_REACHED;
231
232 if (p >= DP_PRE_EMPHASIS_MAX)
233 p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
234
235 DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
236 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
237 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
238
239 for (lane = 0; lane < 4; lane++)
240 train_set[lane] = v | p;
241}
242
243/* convert bits per color to bits per pixel */
244/* get bpc from the EDID */
245static int amdgpu_atombios_dp_convert_bpc_to_bpp(int bpc)
246{
247 if (bpc == 0)
248 return 24;
249 else
250 return bpc * 3;
251}
252
253/* get the max pix clock supported by the link rate and lane num */
254static int amdgpu_atombios_dp_get_max_dp_pix_clock(int link_rate,
255 int lane_num,
256 int bpp)
257{
258 return (link_rate * lane_num * 8) / bpp;
259}
260
261/***** amdgpu specific DP functions *****/
262
263/* First get the min lane# when low rate is used according to pixel clock
264 * (prefer low rate), second check max lane# supported by DP panel,
265 * if the max lane# < low rate lane# then use max lane# instead.
266 */
267static int amdgpu_atombios_dp_get_dp_lane_number(struct drm_connector *connector,
268 u8 dpcd[DP_DPCD_SIZE],
269 int pix_clock)
270{
271 int bpp = amdgpu_atombios_dp_convert_bpc_to_bpp(amdgpu_connector_get_monitor_bpc(connector));
272 int max_link_rate = drm_dp_max_link_rate(dpcd);
273 int max_lane_num = drm_dp_max_lane_count(dpcd);
274 int lane_num;
275 int max_dp_pix_clock;
276
277 for (lane_num = 1; lane_num < max_lane_num; lane_num <<= 1) {
278 max_dp_pix_clock = amdgpu_atombios_dp_get_max_dp_pix_clock(max_link_rate, lane_num, bpp);
279 if (pix_clock <= max_dp_pix_clock)
280 break;
281 }
282
283 return lane_num;
284}
285
286static int amdgpu_atombios_dp_get_dp_link_clock(struct drm_connector *connector,
287 u8 dpcd[DP_DPCD_SIZE],
288 int pix_clock)
289{
290 int bpp = amdgpu_atombios_dp_convert_bpc_to_bpp(amdgpu_connector_get_monitor_bpc(connector));
291 int lane_num, max_pix_clock;
292
293 if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) ==
294 ENCODER_OBJECT_ID_NUTMEG)
295 return 270000;
296
297 lane_num = amdgpu_atombios_dp_get_dp_lane_number(connector, dpcd, pix_clock);
298 max_pix_clock = amdgpu_atombios_dp_get_max_dp_pix_clock(162000, lane_num, bpp);
299 if (pix_clock <= max_pix_clock)
300 return 162000;
301 max_pix_clock = amdgpu_atombios_dp_get_max_dp_pix_clock(270000, lane_num, bpp);
302 if (pix_clock <= max_pix_clock)
303 return 270000;
304 if (amdgpu_connector_is_dp12_capable(connector)) {
305 max_pix_clock = amdgpu_atombios_dp_get_max_dp_pix_clock(540000, lane_num, bpp);
306 if (pix_clock <= max_pix_clock)
307 return 540000;
308 }
309
310 return drm_dp_max_link_rate(dpcd);
311}
312
313static u8 amdgpu_atombios_dp_encoder_service(struct amdgpu_device *adev,
314 int action, int dp_clock,
315 u8 ucconfig, u8 lane_num)
316{
317 DP_ENCODER_SERVICE_PARAMETERS args;
318 int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
319
320 memset(&args, 0, sizeof(args));
321 args.ucLinkClock = dp_clock / 10;
322 args.ucConfig = ucconfig;
323 args.ucAction = action;
324 args.ucLaneNum = lane_num;
325 args.ucStatus = 0;
326
327 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
328 return args.ucStatus;
329}
330
331u8 amdgpu_atombios_dp_get_sinktype(struct amdgpu_connector *amdgpu_connector)
332{
333 struct drm_device *dev = amdgpu_connector->base.dev;
334 struct amdgpu_device *adev = dev->dev_private;
335
336 return amdgpu_atombios_dp_encoder_service(adev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
337 amdgpu_connector->ddc_bus->rec.i2c_id, 0);
338}
339
340static void amdgpu_atombios_dp_probe_oui(struct amdgpu_connector *amdgpu_connector)
341{
342 struct amdgpu_connector_atom_dig *dig_connector = amdgpu_connector->con_priv;
343 u8 buf[3];
344
345 if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
346 return;
347
348 if (drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_SINK_OUI, buf, 3) == 3)
349 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
350 buf[0], buf[1], buf[2]);
351
352 if (drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_BRANCH_OUI, buf, 3) == 3)
353 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
354 buf[0], buf[1], buf[2]);
355}
356
357int amdgpu_atombios_dp_get_dpcd(struct amdgpu_connector *amdgpu_connector)
358{
359 struct amdgpu_connector_atom_dig *dig_connector = amdgpu_connector->con_priv;
360 u8 msg[DP_DPCD_SIZE];
361 int ret, i;
362
363 ret = drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_DPCD_REV, msg,
364 DP_DPCD_SIZE);
365 if (ret > 0) {
366 memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
367 DRM_DEBUG_KMS("DPCD: ");
368 for (i = 0; i < DP_DPCD_SIZE; i++)
369 DRM_DEBUG_KMS("%02x ", msg[i]);
370 DRM_DEBUG_KMS("\n");
371
372 amdgpu_atombios_dp_probe_oui(amdgpu_connector);
373
374 return 0;
375 }
376 dig_connector->dpcd[0] = 0;
377 return -EINVAL;
378}
379
380int amdgpu_atombios_dp_get_panel_mode(struct drm_encoder *encoder,
381 struct drm_connector *connector)
382{
383 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
384 struct amdgpu_connector_atom_dig *dig_connector;
385 int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
386 u16 dp_bridge = amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector);
387 u8 tmp;
388
389 if (!amdgpu_connector->con_priv)
390 return panel_mode;
391
392 dig_connector = amdgpu_connector->con_priv;
393
394 if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
395 /* DP bridge chips */
396 if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux,
397 DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
398 if (tmp & 1)
399 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
400 else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
401 (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
402 panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
403 else
404 panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
405 }
406 } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
407 /* eDP */
408 if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux,
409 DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
410 if (tmp & 1)
411 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
412 }
413 }
414
415 return panel_mode;
416}
417
418void amdgpu_atombios_dp_set_link_config(struct drm_connector *connector,
419 const struct drm_display_mode *mode)
420{
421 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
422 struct amdgpu_connector_atom_dig *dig_connector;
423
424 if (!amdgpu_connector->con_priv)
425 return;
426 dig_connector = amdgpu_connector->con_priv;
427
428 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
429 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
430 dig_connector->dp_clock =
431 amdgpu_atombios_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
432 dig_connector->dp_lane_count =
433 amdgpu_atombios_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
434 }
435}
436
437int amdgpu_atombios_dp_mode_valid_helper(struct drm_connector *connector,
438 struct drm_display_mode *mode)
439{
440 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
441 struct amdgpu_connector_atom_dig *dig_connector;
442 int dp_clock;
443
444 if (!amdgpu_connector->con_priv)
445 return MODE_CLOCK_HIGH;
446 dig_connector = amdgpu_connector->con_priv;
447
448 dp_clock =
449 amdgpu_atombios_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
450
451 if ((dp_clock == 540000) &&
452 (!amdgpu_connector_is_dp12_capable(connector)))
453 return MODE_CLOCK_HIGH;
454
455 return MODE_OK;
456}
457
458bool amdgpu_atombios_dp_needs_link_train(struct amdgpu_connector *amdgpu_connector)
459{
460 u8 link_status[DP_LINK_STATUS_SIZE];
461 struct amdgpu_connector_atom_dig *dig = amdgpu_connector->con_priv;
462
463 if (drm_dp_dpcd_read_link_status(&amdgpu_connector->ddc_bus->aux, link_status)
464 <= 0)
465 return false;
466 if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
467 return false;
468 return true;
469}
470
471void amdgpu_atombios_dp_set_rx_power_state(struct drm_connector *connector,
472 u8 power_state)
473{
474 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
475 struct amdgpu_connector_atom_dig *dig_connector;
476
477 if (!amdgpu_connector->con_priv)
478 return;
479
480 dig_connector = amdgpu_connector->con_priv;
481
482 /* power up/down the sink */
483 if (dig_connector->dpcd[0] >= 0x11) {
484 drm_dp_dpcd_writeb(&amdgpu_connector->ddc_bus->aux,
485 DP_SET_POWER, power_state);
486 usleep_range(1000, 2000);
487 }
488}
489
490struct amdgpu_atombios_dp_link_train_info {
491 struct amdgpu_device *adev;
492 struct drm_encoder *encoder;
493 struct drm_connector *connector;
494 int dp_clock;
495 int dp_lane_count;
496 bool tp3_supported;
497 u8 dpcd[DP_RECEIVER_CAP_SIZE];
498 u8 train_set[4];
499 u8 link_status[DP_LINK_STATUS_SIZE];
500 u8 tries;
501 struct drm_dp_aux *aux;
502};
503
504static void
505amdgpu_atombios_dp_update_vs_emph(struct amdgpu_atombios_dp_link_train_info *dp_info)
506{
507 /* set the initial vs/emph on the source */
508 amdgpu_atombios_encoder_setup_dig_transmitter(dp_info->encoder,
509 ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
510 0, dp_info->train_set[0]); /* sets all lanes at once */
511
512 /* set the vs/emph on the sink */
513 drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
514 dp_info->train_set, dp_info->dp_lane_count);
515}
516
517static void
518amdgpu_atombios_dp_set_tp(struct amdgpu_atombios_dp_link_train_info *dp_info, int tp)
519{
520 int rtp = 0;
521
522 /* set training pattern on the source */
523 switch (tp) {
524 case DP_TRAINING_PATTERN_1:
525 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
526 break;
527 case DP_TRAINING_PATTERN_2:
528 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
529 break;
530 case DP_TRAINING_PATTERN_3:
531 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
532 break;
533 }
534 amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder, rtp, 0);
535
536 /* enable training pattern on the sink */
537 drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
538}
539
540static int
541amdgpu_atombios_dp_link_train_init(struct amdgpu_atombios_dp_link_train_info *dp_info)
542{
543 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(dp_info->encoder);
544 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
545 u8 tmp;
546
547 /* power up the sink */
548 amdgpu_atombios_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
549
550 /* possibly enable downspread on the sink */
551 if (dp_info->dpcd[3] & 0x1)
552 drm_dp_dpcd_writeb(dp_info->aux,
553 DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
554 else
555 drm_dp_dpcd_writeb(dp_info->aux,
556 DP_DOWNSPREAD_CTRL, 0);
557
558 if (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)
559 drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
560
561 /* set the lane count on the sink */
562 tmp = dp_info->dp_lane_count;
563 if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
564 tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
565 drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
566
567 /* set the link rate on the sink */
568 tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
569 drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
570
571 /* start training on the source */
572 amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder,
573 ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
574
575 /* disable the training pattern on the sink */
576 drm_dp_dpcd_writeb(dp_info->aux,
577 DP_TRAINING_PATTERN_SET,
578 DP_TRAINING_PATTERN_DISABLE);
579
580 return 0;
581}
582
583static int
584amdgpu_atombios_dp_link_train_finish(struct amdgpu_atombios_dp_link_train_info *dp_info)
585{
586 udelay(400);
587
588 /* disable the training pattern on the sink */
589 drm_dp_dpcd_writeb(dp_info->aux,
590 DP_TRAINING_PATTERN_SET,
591 DP_TRAINING_PATTERN_DISABLE);
592
593 /* disable the training pattern on the source */
594 amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder,
595 ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
596
597 return 0;
598}
599
600static int
601amdgpu_atombios_dp_link_train_cr(struct amdgpu_atombios_dp_link_train_info *dp_info)
602{
603 bool clock_recovery;
604 u8 voltage;
605 int i;
606
607 amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
608 memset(dp_info->train_set, 0, 4);
609 amdgpu_atombios_dp_update_vs_emph(dp_info);
610
611 udelay(400);
612
613 /* clock recovery loop */
614 clock_recovery = false;
615 dp_info->tries = 0;
616 voltage = 0xff;
617 while (1) {
618 drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
619
620 if (drm_dp_dpcd_read_link_status(dp_info->aux,
621 dp_info->link_status) <= 0) {
622 DRM_ERROR("displayport link status failed\n");
623 break;
624 }
625
626 if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
627 clock_recovery = true;
628 break;
629 }
630
631 for (i = 0; i < dp_info->dp_lane_count; i++) {
632 if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
633 break;
634 }
635 if (i == dp_info->dp_lane_count) {
636 DRM_ERROR("clock recovery reached max voltage\n");
637 break;
638 }
639
640 if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
641 ++dp_info->tries;
642 if (dp_info->tries == 5) {
643 DRM_ERROR("clock recovery tried 5 times\n");
644 break;
645 }
646 } else
647 dp_info->tries = 0;
648
649 voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
650
651 /* Compute new train_set as requested by sink */
652 amdgpu_atombios_dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count,
653 dp_info->train_set);
654
655 amdgpu_atombios_dp_update_vs_emph(dp_info);
656 }
657 if (!clock_recovery) {
658 DRM_ERROR("clock recovery failed\n");
659 return -1;
660 } else {
661 DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
662 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
663 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
664 DP_TRAIN_PRE_EMPHASIS_SHIFT);
665 return 0;
666 }
667}
668
669static int
670amdgpu_atombios_dp_link_train_ce(struct amdgpu_atombios_dp_link_train_info *dp_info)
671{
672 bool channel_eq;
673
674 if (dp_info->tp3_supported)
675 amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
676 else
677 amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
678
679 /* channel equalization loop */
680 dp_info->tries = 0;
681 channel_eq = false;
682 while (1) {
683 drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
684
685 if (drm_dp_dpcd_read_link_status(dp_info->aux,
686 dp_info->link_status) <= 0) {
687 DRM_ERROR("displayport link status failed\n");
688 break;
689 }
690
691 if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
692 channel_eq = true;
693 break;
694 }
695
696 /* Try 5 times */
697 if (dp_info->tries > 5) {
698 DRM_ERROR("channel eq failed: 5 tries\n");
699 break;
700 }
701
702 /* Compute new train_set as requested by sink */
703 amdgpu_atombios_dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count,
704 dp_info->train_set);
705
706 amdgpu_atombios_dp_update_vs_emph(dp_info);
707 dp_info->tries++;
708 }
709
710 if (!channel_eq) {
711 DRM_ERROR("channel eq failed\n");
712 return -1;
713 } else {
714 DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
715 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
716 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
717 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
718 return 0;
719 }
720}
721
722void amdgpu_atombios_dp_link_train(struct drm_encoder *encoder,
723 struct drm_connector *connector)
724{
725 struct drm_device *dev = encoder->dev;
726 struct amdgpu_device *adev = dev->dev_private;
727 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
728 struct amdgpu_encoder_atom_dig *dig;
729 struct amdgpu_connector *amdgpu_connector;
730 struct amdgpu_connector_atom_dig *dig_connector;
731 struct amdgpu_atombios_dp_link_train_info dp_info;
732 u8 tmp;
733
734 if (!amdgpu_encoder->enc_priv)
735 return;
736 dig = amdgpu_encoder->enc_priv;
737
738 amdgpu_connector = to_amdgpu_connector(connector);
739 if (!amdgpu_connector->con_priv)
740 return;
741 dig_connector = amdgpu_connector->con_priv;
742
743 if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
744 (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
745 return;
746
747 if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux, DP_MAX_LANE_COUNT, &tmp)
748 == 1) {
749 if (tmp & DP_TPS3_SUPPORTED)
750 dp_info.tp3_supported = true;
751 else
752 dp_info.tp3_supported = false;
753 } else {
754 dp_info.tp3_supported = false;
755 }
756
757 memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
758 dp_info.adev = adev;
759 dp_info.encoder = encoder;
760 dp_info.connector = connector;
761 dp_info.dp_lane_count = dig_connector->dp_lane_count;
762 dp_info.dp_clock = dig_connector->dp_clock;
763 dp_info.aux = &amdgpu_connector->ddc_bus->aux;
764
765 if (amdgpu_atombios_dp_link_train_init(&dp_info))
766 goto done;
767 if (amdgpu_atombios_dp_link_train_cr(&dp_info))
768 goto done;
769 if (amdgpu_atombios_dp_link_train_ce(&dp_info))
770 goto done;
771done:
772 if (amdgpu_atombios_dp_link_train_finish(&dp_info))
773 return;
774}