2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
28 #include <drm/radeon_drm.h>
32 #include "atom-bits.h"
33 #include <drm/drm_dp_helper.h>
35 /* move these to drm_dp_helper.c/h */
36 #define DP_LINK_CONFIGURATION_SIZE 9
37 #define DP_DPCD_SIZE DP_RECEIVER_CAP_SIZE
39 static char *voltage_names[] = {
40 "0.4V", "0.6V", "0.8V", "1.2V"
42 static char *pre_emph_names[] = {
43 "0dB", "3.5dB", "6dB", "9.5dB"
46 /***** radeon AUX functions *****/
48 /* Atom needs data in little endian format
49 * so swap as appropriate when copying data to
50 * or from atom. Note that atom operates on
53 void radeon_atom_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
56 u8 src_tmp[20], dst_tmp[20]; /* used for byteswapping */
60 memcpy(src_tmp, src, num_bytes);
61 src32 = (u32 *)src_tmp;
62 dst32 = (u32 *)dst_tmp;
64 for (i = 0; i < ((num_bytes + 3) / 4); i++)
65 dst32[i] = cpu_to_le32(src32[i]);
66 memcpy(dst, dst_tmp, num_bytes);
68 u8 dws = num_bytes & ~3;
69 for (i = 0; i < ((num_bytes + 3) / 4); i++)
70 dst32[i] = le32_to_cpu(src32[i]);
71 memcpy(dst, dst_tmp, dws);
73 for (i = 0; i < (num_bytes % 4); i++)
74 dst[dws+i] = dst_tmp[dws+i];
78 memcpy(dst, src, num_bytes);
82 union aux_channel_transaction {
83 PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
84 PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
87 static int radeon_process_aux_ch(struct radeon_i2c_chan *chan,
88 u8 *send, int send_bytes,
89 u8 *recv, int recv_size,
92 struct drm_device *dev = chan->dev;
93 struct radeon_device *rdev = dev->dev_private;
94 union aux_channel_transaction args;
95 int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
99 memset(&args, 0, sizeof(args));
101 base = (unsigned char *)(rdev->mode_info.atom_context->scratch + 1);
103 radeon_atom_copy_swap(base, send, send_bytes, true);
105 args.v1.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
106 args.v1.lpDataOut = cpu_to_le16((u16)(16 + 4));
107 args.v1.ucDataOutLen = 0;
108 args.v1.ucChannelID = chan->rec.i2c_id;
109 args.v1.ucDelay = delay / 10;
110 if (ASIC_IS_DCE4(rdev))
111 args.v2.ucHPD_ID = chan->rec.hpd;
113 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
115 *ack = args.v1.ucReplyStatus;
118 if (args.v1.ucReplyStatus == 1) {
119 DRM_DEBUG_KMS("dp_aux_ch timeout\n");
124 if (args.v1.ucReplyStatus == 2) {
125 DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
130 if (args.v1.ucReplyStatus == 3) {
131 DRM_DEBUG_KMS("dp_aux_ch error\n");
135 recv_bytes = args.v1.ucDataOutLen;
136 if (recv_bytes > recv_size)
137 recv_bytes = recv_size;
139 if (recv && recv_size)
140 radeon_atom_copy_swap(recv, base + 16, recv_bytes, false);
145 #define HEADER_SIZE 4
148 radeon_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
150 struct radeon_i2c_chan *chan =
151 container_of(aux, struct radeon_i2c_chan, aux);
157 if (WARN_ON(msg->size > 16))
160 tx_buf[0] = msg->address & 0xff;
161 tx_buf[1] = msg->address >> 8;
162 tx_buf[2] = msg->request << 4;
163 tx_buf[3] = msg->size - 1;
165 switch (msg->request & ~DP_AUX_I2C_MOT) {
166 case DP_AUX_NATIVE_WRITE:
167 case DP_AUX_I2C_WRITE:
168 tx_size = HEADER_SIZE + msg->size;
169 tx_buf[3] |= tx_size << 4;
170 memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
171 ret = radeon_process_aux_ch(chan,
172 tx_buf, tx_size, NULL, 0, delay, &ack);
174 /* Return payload size. */
177 case DP_AUX_NATIVE_READ:
178 case DP_AUX_I2C_READ:
179 tx_size = HEADER_SIZE;
180 tx_buf[3] |= tx_size << 4;
181 ret = radeon_process_aux_ch(chan,
182 tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
190 msg->reply = ack >> 4;
195 void radeon_dp_aux_init(struct radeon_connector *radeon_connector)
197 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
199 dig_connector->dp_i2c_bus->aux.dev = radeon_connector->base.kdev;
200 dig_connector->dp_i2c_bus->aux.transfer = radeon_dp_aux_transfer;
203 int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
204 u8 write_byte, u8 *read_byte)
206 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
207 struct radeon_i2c_chan *auxch = i2c_get_adapdata(adapter);
208 u16 address = algo_data->address;
217 /* Set up the address */
219 msg[1] = address >> 8;
221 /* Set up the command byte */
222 if (mode & MODE_I2C_READ) {
223 msg[2] = DP_AUX_I2C_READ << 4;
225 msg[3] = msg_bytes << 4;
227 msg[2] = DP_AUX_I2C_WRITE << 4;
229 msg[3] = msg_bytes << 4;
233 /* special handling for start/stop */
234 if (mode & (MODE_I2C_START | MODE_I2C_STOP))
237 /* Set MOT bit for all but stop */
238 if ((mode & MODE_I2C_STOP) == 0)
239 msg[2] |= DP_AUX_I2C_MOT << 4;
241 for (retry = 0; retry < 7; retry++) {
242 ret = radeon_process_aux_ch(auxch,
243 msg, msg_bytes, reply, reply_bytes, 0, &ack);
247 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
251 switch ((ack >> 4) & DP_AUX_NATIVE_REPLY_MASK) {
252 case DP_AUX_NATIVE_REPLY_ACK:
253 /* I2C-over-AUX Reply field is only valid
254 * when paired with AUX ACK.
257 case DP_AUX_NATIVE_REPLY_NACK:
258 DRM_DEBUG_KMS("aux_ch native nack\n");
260 case DP_AUX_NATIVE_REPLY_DEFER:
261 DRM_DEBUG_KMS("aux_ch native defer\n");
262 usleep_range(500, 600);
265 DRM_ERROR("aux_ch invalid native reply 0x%02x\n", ack);
269 switch ((ack >> 4) & DP_AUX_I2C_REPLY_MASK) {
270 case DP_AUX_I2C_REPLY_ACK:
271 if (mode == MODE_I2C_READ)
272 *read_byte = reply[0];
274 case DP_AUX_I2C_REPLY_NACK:
275 DRM_DEBUG_KMS("aux_i2c nack\n");
277 case DP_AUX_I2C_REPLY_DEFER:
278 DRM_DEBUG_KMS("aux_i2c defer\n");
279 usleep_range(400, 500);
282 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", ack);
287 DRM_DEBUG_KMS("aux i2c too many retries, giving up\n");
291 /***** general DP utility functions *****/
293 #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
294 #define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPHASIS_9_5
296 static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
304 for (lane = 0; lane < lane_count; lane++) {
305 u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
306 u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
308 DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
310 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
311 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
319 if (v >= DP_VOLTAGE_MAX)
320 v |= DP_TRAIN_MAX_SWING_REACHED;
322 if (p >= DP_PRE_EMPHASIS_MAX)
323 p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
325 DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
326 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
327 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
329 for (lane = 0; lane < 4; lane++)
330 train_set[lane] = v | p;
333 /* convert bits per color to bits per pixel */
334 /* get bpc from the EDID */
335 static int convert_bpc_to_bpp(int bpc)
343 /* get the max pix clock supported by the link rate and lane num */
344 static int dp_get_max_dp_pix_clock(int link_rate,
348 return (link_rate * lane_num * 8) / bpp;
351 /***** radeon specific DP functions *****/
353 /* First get the min lane# when low rate is used according to pixel clock
354 * (prefer low rate), second check max lane# supported by DP panel,
355 * if the max lane# < low rate lane# then use max lane# instead.
357 static int radeon_dp_get_dp_lane_number(struct drm_connector *connector,
358 u8 dpcd[DP_DPCD_SIZE],
361 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
362 int max_link_rate = drm_dp_max_link_rate(dpcd);
363 int max_lane_num = drm_dp_max_lane_count(dpcd);
365 int max_dp_pix_clock;
367 for (lane_num = 1; lane_num < max_lane_num; lane_num <<= 1) {
368 max_dp_pix_clock = dp_get_max_dp_pix_clock(max_link_rate, lane_num, bpp);
369 if (pix_clock <= max_dp_pix_clock)
376 static int radeon_dp_get_dp_link_clock(struct drm_connector *connector,
377 u8 dpcd[DP_DPCD_SIZE],
380 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
381 int lane_num, max_pix_clock;
383 if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
384 ENCODER_OBJECT_ID_NUTMEG)
387 lane_num = radeon_dp_get_dp_lane_number(connector, dpcd, pix_clock);
388 max_pix_clock = dp_get_max_dp_pix_clock(162000, lane_num, bpp);
389 if (pix_clock <= max_pix_clock)
391 max_pix_clock = dp_get_max_dp_pix_clock(270000, lane_num, bpp);
392 if (pix_clock <= max_pix_clock)
394 if (radeon_connector_is_dp12_capable(connector)) {
395 max_pix_clock = dp_get_max_dp_pix_clock(540000, lane_num, bpp);
396 if (pix_clock <= max_pix_clock)
400 return drm_dp_max_link_rate(dpcd);
403 static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
404 int action, int dp_clock,
405 u8 ucconfig, u8 lane_num)
407 DP_ENCODER_SERVICE_PARAMETERS args;
408 int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
410 memset(&args, 0, sizeof(args));
411 args.ucLinkClock = dp_clock / 10;
412 args.ucConfig = ucconfig;
413 args.ucAction = action;
414 args.ucLaneNum = lane_num;
417 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
418 return args.ucStatus;
421 u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
423 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
424 struct drm_device *dev = radeon_connector->base.dev;
425 struct radeon_device *rdev = dev->dev_private;
427 return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
428 dig_connector->dp_i2c_bus->rec.i2c_id, 0);
431 static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
433 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
436 if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
439 if (drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_SINK_OUI, buf, 3))
440 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
441 buf[0], buf[1], buf[2]);
443 if (drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_BRANCH_OUI, buf, 3))
444 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
445 buf[0], buf[1], buf[2]);
448 bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
450 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
451 u8 msg[DP_DPCD_SIZE];
454 ret = drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_DPCD_REV, msg,
457 memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
458 DRM_DEBUG_KMS("DPCD: ");
459 for (i = 0; i < DP_DPCD_SIZE; i++)
460 DRM_DEBUG_KMS("%02x ", msg[i]);
463 radeon_dp_probe_oui(radeon_connector);
467 dig_connector->dpcd[0] = 0;
471 int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
472 struct drm_connector *connector)
474 struct drm_device *dev = encoder->dev;
475 struct radeon_device *rdev = dev->dev_private;
476 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
477 struct radeon_connector_atom_dig *dig_connector;
478 int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
479 u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
482 if (!ASIC_IS_DCE4(rdev))
485 if (!radeon_connector->con_priv)
488 dig_connector = radeon_connector->con_priv;
490 if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
491 /* DP bridge chips */
492 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux,
493 DP_EDP_CONFIGURATION_CAP, &tmp);
495 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
496 else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
497 (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
498 panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
500 panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
501 } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
503 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux,
504 DP_EDP_CONFIGURATION_CAP, &tmp);
506 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
512 void radeon_dp_set_link_config(struct drm_connector *connector,
513 const struct drm_display_mode *mode)
515 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
516 struct radeon_connector_atom_dig *dig_connector;
518 if (!radeon_connector->con_priv)
520 dig_connector = radeon_connector->con_priv;
522 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
523 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
524 dig_connector->dp_clock =
525 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
526 dig_connector->dp_lane_count =
527 radeon_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
531 int radeon_dp_mode_valid_helper(struct drm_connector *connector,
532 struct drm_display_mode *mode)
534 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
535 struct radeon_connector_atom_dig *dig_connector;
538 if (!radeon_connector->con_priv)
539 return MODE_CLOCK_HIGH;
540 dig_connector = radeon_connector->con_priv;
543 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
545 if ((dp_clock == 540000) &&
546 (!radeon_connector_is_dp12_capable(connector)))
547 return MODE_CLOCK_HIGH;
552 bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
554 u8 link_status[DP_LINK_STATUS_SIZE];
555 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
557 if (drm_dp_dpcd_read_link_status(&dig->dp_i2c_bus->aux, link_status) <= 0)
559 if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
564 void radeon_dp_set_rx_power_state(struct drm_connector *connector,
567 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
568 struct radeon_connector_atom_dig *dig_connector;
570 if (!radeon_connector->con_priv)
573 dig_connector = radeon_connector->con_priv;
575 /* power up/down the sink */
576 if (dig_connector->dpcd[0] >= 0x11) {
577 drm_dp_dpcd_writeb(&dig_connector->dp_i2c_bus->aux,
578 DP_SET_POWER, power_state);
579 usleep_range(1000, 2000);
584 struct radeon_dp_link_train_info {
585 struct radeon_device *rdev;
586 struct drm_encoder *encoder;
587 struct drm_connector *connector;
592 u8 dpcd[DP_RECEIVER_CAP_SIZE];
594 u8 link_status[DP_LINK_STATUS_SIZE];
597 struct drm_dp_aux *aux;
600 static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
602 /* set the initial vs/emph on the source */
603 atombios_dig_transmitter_setup(dp_info->encoder,
604 ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
605 0, dp_info->train_set[0]); /* sets all lanes at once */
607 /* set the vs/emph on the sink */
608 drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
609 dp_info->train_set, dp_info->dp_lane_count);
612 static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
616 /* set training pattern on the source */
617 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
619 case DP_TRAINING_PATTERN_1:
620 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
622 case DP_TRAINING_PATTERN_2:
623 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
625 case DP_TRAINING_PATTERN_3:
626 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
629 atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
632 case DP_TRAINING_PATTERN_1:
635 case DP_TRAINING_PATTERN_2:
639 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
640 dp_info->dp_clock, dp_info->enc_id, rtp);
643 /* enable training pattern on the sink */
644 drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
647 static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
649 struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
650 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
653 /* power up the sink */
654 radeon_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
656 /* possibly enable downspread on the sink */
657 if (dp_info->dpcd[3] & 0x1)
658 drm_dp_dpcd_writeb(dp_info->aux,
659 DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
661 drm_dp_dpcd_writeb(dp_info->aux,
662 DP_DOWNSPREAD_CTRL, 0);
664 if ((dp_info->connector->connector_type == DRM_MODE_CONNECTOR_eDP) &&
665 (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)) {
666 drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
669 /* set the lane count on the sink */
670 tmp = dp_info->dp_lane_count;
671 if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
672 tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
673 drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
675 /* set the link rate on the sink */
676 tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
677 drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
679 /* start training on the source */
680 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
681 atombios_dig_encoder_setup(dp_info->encoder,
682 ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
684 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
685 dp_info->dp_clock, dp_info->enc_id, 0);
687 /* disable the training pattern on the sink */
688 drm_dp_dpcd_writeb(dp_info->aux,
689 DP_TRAINING_PATTERN_SET,
690 DP_TRAINING_PATTERN_DISABLE);
695 static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
699 /* disable the training pattern on the sink */
700 drm_dp_dpcd_writeb(dp_info->aux,
701 DP_TRAINING_PATTERN_SET,
702 DP_TRAINING_PATTERN_DISABLE);
704 /* disable the training pattern on the source */
705 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
706 atombios_dig_encoder_setup(dp_info->encoder,
707 ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
709 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
710 dp_info->dp_clock, dp_info->enc_id, 0);
715 static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
721 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
722 memset(dp_info->train_set, 0, 4);
723 radeon_dp_update_vs_emph(dp_info);
727 /* clock recovery loop */
728 clock_recovery = false;
732 drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
734 if (drm_dp_dpcd_read_link_status(dp_info->aux,
735 dp_info->link_status) <= 0) {
736 DRM_ERROR("displayport link status failed\n");
740 if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
741 clock_recovery = true;
745 for (i = 0; i < dp_info->dp_lane_count; i++) {
746 if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
749 if (i == dp_info->dp_lane_count) {
750 DRM_ERROR("clock recovery reached max voltage\n");
754 if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
756 if (dp_info->tries == 5) {
757 DRM_ERROR("clock recovery tried 5 times\n");
763 voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
765 /* Compute new train_set as requested by sink */
766 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
768 radeon_dp_update_vs_emph(dp_info);
770 if (!clock_recovery) {
771 DRM_ERROR("clock recovery failed\n");
774 DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
775 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
776 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
777 DP_TRAIN_PRE_EMPHASIS_SHIFT);
782 static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
786 if (dp_info->tp3_supported)
787 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
789 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
791 /* channel equalization loop */
795 drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
797 if (drm_dp_dpcd_read_link_status(dp_info->aux,
798 dp_info->link_status) <= 0) {
799 DRM_ERROR("displayport link status failed\n");
803 if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
809 if (dp_info->tries > 5) {
810 DRM_ERROR("channel eq failed: 5 tries\n");
814 /* Compute new train_set as requested by sink */
815 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
817 radeon_dp_update_vs_emph(dp_info);
822 DRM_ERROR("channel eq failed\n");
825 DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
826 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
827 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
828 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
833 void radeon_dp_link_train(struct drm_encoder *encoder,
834 struct drm_connector *connector)
836 struct drm_device *dev = encoder->dev;
837 struct radeon_device *rdev = dev->dev_private;
838 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
839 struct radeon_encoder_atom_dig *dig;
840 struct radeon_connector *radeon_connector;
841 struct radeon_connector_atom_dig *dig_connector;
842 struct radeon_dp_link_train_info dp_info;
846 if (!radeon_encoder->enc_priv)
848 dig = radeon_encoder->enc_priv;
850 radeon_connector = to_radeon_connector(connector);
851 if (!radeon_connector->con_priv)
853 dig_connector = radeon_connector->con_priv;
855 if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
856 (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
859 /* DPEncoderService newer than 1.1 can't program properly the
860 * training pattern. When facing such version use the
861 * DIGXEncoderControl (X== 1 | 2)
863 dp_info.use_dpencoder = true;
864 index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
865 if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
867 dp_info.use_dpencoder = false;
872 if (dig->dig_encoder)
873 dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
875 dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
877 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
879 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
881 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux, DP_MAX_LANE_COUNT, &tmp);
882 if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
883 dp_info.tp3_supported = true;
885 dp_info.tp3_supported = false;
887 memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
889 dp_info.encoder = encoder;
890 dp_info.connector = connector;
891 dp_info.dp_lane_count = dig_connector->dp_lane_count;
892 dp_info.dp_clock = dig_connector->dp_clock;
893 dp_info.aux = &dig_connector->dp_i2c_bus->aux;
895 if (radeon_dp_link_train_init(&dp_info))
897 if (radeon_dp_link_train_cr(&dp_info))
899 if (radeon_dp_link_train_ce(&dp_info))
902 if (radeon_dp_link_train_finish(&dp_info))