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
27 #include "radeon_drm.h"
31 #include "atom-bits.h"
32 #include "drm_dp_helper.h"
34 /* move these to drm_dp_helper.c/h */
35 #define DP_LINK_CONFIGURATION_SIZE 9
36 #define DP_LINK_STATUS_SIZE 6
37 #define DP_DPCD_SIZE 8
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"
45 static char *link_train_names[] = {
46 "pattern 1", "pattern 2", "idle", "off"
49 static const int dp_clocks[] = {
50 54000, // 1 lane, 1.62 Ghz
51 90000, // 1 lane, 2.70 Ghz
52 108000, // 2 lane, 1.62 Ghz
53 180000, // 2 lane, 2.70 Ghz
54 216000, // 4 lane, 1.62 Ghz
55 360000, // 4 lane, 2.70 Ghz
58 static const int num_dp_clocks = sizeof(dp_clocks) / sizeof(int);
60 /* common helper functions */
61 static int dp_lanes_for_mode_clock(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
70 max_link_bw = dpcd[DP_MAX_LINK_RATE];
71 max_lane_count = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
73 switch (max_link_bw) {
76 for (i = 0; i < num_dp_clocks; i++) {
79 switch (max_lane_count) {
92 if (dp_clocks[i] > mode_clock) {
103 for (i = 0; i < num_dp_clocks; i++) {
104 switch (max_lane_count) {
117 if (dp_clocks[i] > mode_clock) {
132 static int dp_link_clock_for_mode_clock(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
141 max_link_bw = dpcd[DP_MAX_LINK_RATE];
142 max_lane_count = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
144 switch (max_link_bw) {
145 case DP_LINK_BW_1_62:
147 for (i = 0; i < num_dp_clocks; i++) {
150 switch (max_lane_count) {
163 if (dp_clocks[i] > mode_clock)
168 for (i = 0; i < num_dp_clocks; i++) {
169 switch (max_lane_count) {
182 if (dp_clocks[i] > mode_clock)
183 return (i % 2) ? 270000 : 162000;
190 int dp_mode_valid(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
192 int lanes = dp_lanes_for_mode_clock(dpcd, mode_clock);
193 int bw = dp_lanes_for_mode_clock(dpcd, mode_clock);
195 if ((lanes == 0) || (bw == 0))
196 return MODE_CLOCK_HIGH;
201 static u8 dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE], int r)
203 return link_status[r - DP_LANE0_1_STATUS];
206 static u8 dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],
209 int i = DP_LANE0_1_STATUS + (lane >> 1);
210 int s = (lane & 1) * 4;
211 u8 l = dp_link_status(link_status, i);
212 return (l >> s) & 0xf;
215 static bool dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],
221 for (lane = 0; lane < lane_count; lane++) {
222 lane_status = dp_get_lane_status(link_status, lane);
223 if ((lane_status & DP_LANE_CR_DONE) == 0)
229 static bool dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],
236 lane_align = dp_link_status(link_status,
237 DP_LANE_ALIGN_STATUS_UPDATED);
238 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
240 for (lane = 0; lane < lane_count; lane++) {
241 lane_status = dp_get_lane_status(link_status, lane);
242 if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
248 static u8 dp_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
252 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
253 int s = ((lane & 1) ?
254 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
255 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
256 u8 l = dp_link_status(link_status, i);
258 return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
261 static u8 dp_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
264 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
265 int s = ((lane & 1) ?
266 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
267 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
268 u8 l = dp_link_status(link_status, i);
270 return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
273 /* XXX fix me -- chip specific */
274 #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
275 static u8 dp_pre_emphasis_max(u8 voltage_swing)
277 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
278 case DP_TRAIN_VOLTAGE_SWING_400:
279 return DP_TRAIN_PRE_EMPHASIS_6;
280 case DP_TRAIN_VOLTAGE_SWING_600:
281 return DP_TRAIN_PRE_EMPHASIS_6;
282 case DP_TRAIN_VOLTAGE_SWING_800:
283 return DP_TRAIN_PRE_EMPHASIS_3_5;
284 case DP_TRAIN_VOLTAGE_SWING_1200:
286 return DP_TRAIN_PRE_EMPHASIS_0;
290 static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
298 for (lane = 0; lane < lane_count; lane++) {
299 u8 this_v = dp_get_adjust_request_voltage(link_status, lane);
300 u8 this_p = dp_get_adjust_request_pre_emphasis(link_status, lane);
302 DRM_INFO("requested signal parameters: lane %d voltage %s pre_emph %s\n",
304 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
305 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
313 if (v >= DP_VOLTAGE_MAX)
314 v = DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
316 if (p >= dp_pre_emphasis_max(v))
317 p = dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
319 DRM_INFO("using signal parameters: voltage %s pre_emph %s\n",
320 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
321 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
323 for (lane = 0; lane < 4; lane++)
324 train_set[lane] = v | p;
328 /* radeon aux chan functions */
329 bool radeon_process_aux_ch(struct radeon_i2c_chan *chan, u8 *req_bytes,
330 int num_bytes, u8 *read_byte,
331 u8 read_buf_len, u8 delay)
333 struct drm_device *dev = chan->dev;
334 struct radeon_device *rdev = dev->dev_private;
335 PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION args;
336 int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
339 memset(&args, 0, sizeof(args));
341 base = (unsigned char *)rdev->mode_info.atom_context->scratch;
343 memcpy(base, req_bytes, num_bytes);
345 args.lpAuxRequest = 0;
347 args.ucDataOutLen = 0;
348 args.ucChannelID = chan->rec.i2c_id;
349 args.ucDelay = delay / 10;
351 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
353 if (args.ucReplyStatus) {
354 DRM_ERROR("failed to get auxch %02x%02x %02x %02x 0x%02x %02x\n",
355 req_bytes[1], req_bytes[0], req_bytes[2], req_bytes[3],
356 chan->rec.i2c_id, args.ucReplyStatus);
360 if (args.ucDataOutLen && read_byte && read_buf_len) {
361 if (read_buf_len < args.ucDataOutLen) {
362 DRM_ERROR("Buffer to small for return answer %d %d\n",
363 read_buf_len, args.ucDataOutLen);
367 int len = min(read_buf_len, args.ucDataOutLen);
368 memcpy(read_byte, base + 16, len);
374 bool radeon_dp_aux_native_write(struct radeon_connector *radeon_connector, uint16_t address,
375 uint8_t send_bytes, uint8_t *send)
377 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
379 u8 msg_len, dp_msg_len;
384 msg[1] = address >> 8;
385 msg[2] = AUX_NATIVE_WRITE << 4;
386 dp_msg_len += send_bytes;
387 msg[3] = (dp_msg_len << 4) | (send_bytes - 1);
392 memcpy(&msg[4], send, send_bytes);
393 msg_len = 4 + send_bytes;
394 ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, NULL, 0, 0);
398 bool radeon_dp_aux_native_read(struct radeon_connector *radeon_connector, uint16_t address,
399 uint8_t delay, uint8_t expected_bytes,
402 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
404 u8 msg_len, dp_msg_len;
409 msg[1] = address >> 8;
410 msg[2] = AUX_NATIVE_READ << 4;
411 msg[3] = (dp_msg_len) << 4;
412 msg[3] |= expected_bytes - 1;
414 ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, read_p, expected_bytes, delay);
418 /* radeon dp functions */
419 static u8 radeon_dp_encoder_service(struct radeon_device *rdev, int action, int dp_clock,
420 uint8_t ucconfig, uint8_t lane_num)
422 DP_ENCODER_SERVICE_PARAMETERS args;
423 int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
425 memset(&args, 0, sizeof(args));
426 args.ucLinkClock = dp_clock / 10;
427 args.ucConfig = ucconfig;
428 args.ucAction = action;
429 args.ucLaneNum = lane_num;
432 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
433 return args.ucStatus;
436 u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
438 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
439 struct drm_device *dev = radeon_connector->base.dev;
440 struct radeon_device *rdev = dev->dev_private;
442 return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
443 dig_connector->dp_i2c_bus->rec.i2c_id, 0);
446 void radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
448 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
452 ret = radeon_dp_aux_native_read(radeon_connector, DP_DPCD_REV, 0, 8, msg);
454 memcpy(dig_connector->dpcd, msg, 8);
458 for (i = 0; i < 8; i++)
459 printk("%02x ", msg[i]);
463 dig_connector->dpcd[0] = 0;
467 void radeon_dp_set_link_config(struct drm_connector *connector,
468 struct drm_display_mode *mode)
470 struct radeon_connector *radeon_connector;
471 struct radeon_connector_atom_dig *dig_connector;
473 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
476 radeon_connector = to_radeon_connector(connector);
477 if (!radeon_connector->con_priv)
479 dig_connector = radeon_connector->con_priv;
481 dig_connector->dp_clock =
482 dp_link_clock_for_mode_clock(dig_connector->dpcd, mode->clock);
483 dig_connector->dp_lane_count =
484 dp_lanes_for_mode_clock(dig_connector->dpcd, mode->clock);
487 int radeon_dp_mode_valid_helper(struct radeon_connector *radeon_connector,
488 struct drm_display_mode *mode)
490 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
492 return dp_mode_valid(dig_connector->dpcd, mode->clock);
495 static bool atom_dp_get_link_status(struct radeon_connector *radeon_connector,
496 u8 link_status[DP_LINK_STATUS_SIZE])
499 ret = radeon_dp_aux_native_read(radeon_connector, DP_LANE0_1_STATUS, 100,
500 DP_LINK_STATUS_SIZE, link_status);
502 DRM_ERROR("displayport link status failed\n");
506 DRM_INFO("link status %02x %02x %02x %02x %02x %02x\n",
507 link_status[0], link_status[1], link_status[2],
508 link_status[3], link_status[4], link_status[5]);
512 static void dp_set_power(struct radeon_connector *radeon_connector, u8 power_state)
514 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
516 if (dig_connector->dpcd[0] >= 0x11) {
517 radeon_dp_aux_native_write(radeon_connector, DP_SET_POWER, 1,
522 static void dp_set_downspread(struct radeon_connector *radeon_connector, u8 downspread)
524 radeon_dp_aux_native_write(radeon_connector, DP_DOWNSPREAD_CTRL, 1,
528 static void dp_set_link_bw_lanes(struct radeon_connector *radeon_connector,
529 u8 link_configuration[DP_LINK_CONFIGURATION_SIZE])
531 radeon_dp_aux_native_write(radeon_connector, DP_LINK_BW_SET, 2,
535 static void dp_update_dpvs_emph(struct radeon_connector *radeon_connector,
536 struct drm_encoder *encoder,
539 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
542 for (i = 0; i < dig_connector->dp_lane_count; i++)
543 atombios_dig_transmitter_setup(encoder,
544 ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
547 radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_LANE0_SET,
548 dig_connector->dp_lane_count, train_set);
551 static void dp_set_training(struct radeon_connector *radeon_connector,
554 radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_PATTERN_SET,
558 void dp_link_train(struct drm_encoder *encoder,
559 struct drm_connector *connector)
561 struct drm_device *dev = encoder->dev;
562 struct radeon_device *rdev = dev->dev_private;
563 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
564 struct radeon_encoder_atom_dig *dig;
565 struct radeon_connector *radeon_connector;
566 struct radeon_connector_atom_dig *dig_connector;
568 bool clock_recovery, channel_eq;
569 u8 link_status[DP_LINK_STATUS_SIZE];
570 u8 link_configuration[DP_LINK_CONFIGURATION_SIZE];
575 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
578 if (!radeon_encoder->enc_priv)
580 dig = radeon_encoder->enc_priv;
582 radeon_connector = to_radeon_connector(connector);
583 if (!radeon_connector->con_priv)
585 dig_connector = radeon_connector->con_priv;
587 if (ASIC_IS_DCE32(rdev)) {
589 enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
591 enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
592 if (dig_connector->linkb)
593 enc_id |= ATOM_DP_CONFIG_LINK_B;
595 enc_id |= ATOM_DP_CONFIG_LINK_A;
597 if (dig_connector->linkb)
598 enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER | ATOM_DP_CONFIG_LINK_B;
600 enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER | ATOM_DP_CONFIG_LINK_A;
603 memset(link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
604 if (dig_connector->dp_clock == 270000)
605 link_configuration[0] = DP_LINK_BW_2_7;
607 link_configuration[0] = DP_LINK_BW_1_62;
608 link_configuration[1] = dig_connector->dp_lane_count;
609 if (dig_connector->dpcd[0] >= 0x11)
610 link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
612 /* power up the sink */
613 dp_set_power(radeon_connector, DP_SET_POWER_D0);
614 /* disable the training pattern on the sink */
615 dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
616 /* set link bw and lanes on the sink */
617 dp_set_link_bw_lanes(radeon_connector, link_configuration);
618 /* disable downspread on the sink */
619 dp_set_downspread(radeon_connector, 0);
620 /* start training on the source */
621 radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_START,
622 dig_connector->dp_clock, enc_id, 0);
623 /* set training pattern 1 on the source */
624 radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
625 dig_connector->dp_clock, enc_id, 0);
627 /* set initial vs/emph */
628 memset(train_set, 0, 4);
629 dp_update_dpvs_emph(radeon_connector, encoder, train_set);
631 /* set training pattern 1 on the sink */
632 dp_set_training(radeon_connector, DP_TRAINING_PATTERN_1);
634 /* clock recovery loop */
635 clock_recovery = false;
640 if (!atom_dp_get_link_status(radeon_connector, link_status))
643 if (dp_clock_recovery_ok(link_status, dig_connector->dp_lane_count)) {
644 clock_recovery = true;
648 for (i = 0; i < dig_connector->dp_lane_count; i++) {
649 if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
652 if (i == dig_connector->dp_lane_count) {
653 DRM_ERROR("clock recovery reached max voltage\n");
657 if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
660 DRM_ERROR("clock recovery tried 5 times\n");
666 voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
668 /* Compute new train_set as requested by sink */
669 dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
670 dp_update_dpvs_emph(radeon_connector, encoder, train_set);
673 DRM_ERROR("clock recovery failed\n");
675 DRM_INFO("clock recovery at voltage %d pre-emphasis %d\n",
676 train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
677 (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
678 DP_TRAIN_PRE_EMPHASIS_SHIFT);
681 /* set training pattern 2 on the sink */
682 dp_set_training(radeon_connector, DP_TRAINING_PATTERN_2);
683 /* set training pattern 2 on the source */
684 radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
685 dig_connector->dp_clock, enc_id, 1);
687 /* channel equalization loop */
692 if (!atom_dp_get_link_status(radeon_connector, link_status))
695 if (dp_channel_eq_ok(link_status, dig_connector->dp_lane_count)) {
702 DRM_ERROR("channel eq failed: 5 tries\n");
706 /* Compute new train_set as requested by sink */
707 dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
708 dp_update_dpvs_emph(radeon_connector, encoder, train_set);
714 DRM_ERROR("channel eq failed\n");
716 DRM_INFO("channel eq at voltage %d pre-emphasis %d\n",
717 train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
718 (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
719 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
721 /* disable the training pattern on the sink */
722 dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
724 radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
725 dig_connector->dp_clock, enc_id, 0);
728 int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
729 uint8_t write_byte, uint8_t *read_byte)
731 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
732 struct radeon_i2c_chan *auxch = (struct radeon_i2c_chan *)adapter;
734 uint16_t address = algo_data->address;
737 int msg_len, dp_msg_len;
740 /* Set up the command byte */
741 if (mode & MODE_I2C_READ)
742 msg[2] = AUX_I2C_READ << 4;
744 msg[2] = AUX_I2C_WRITE << 4;
746 if (!(mode & MODE_I2C_STOP))
747 msg[2] |= AUX_I2C_MOT << 4;
750 msg[1] = address >> 8;
769 msg[3] = (dp_msg_len) << 4;
770 ret = radeon_process_aux_ch(auxch, msg, msg_len, reply, reply_bytes, 0);
774 *read_byte = reply[0];