Home | History | Annotate | Line # | Download | only in radeon
      1  1.4  riastrad /*	$NetBSD: radeon_atombios_dp.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2007-8 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      9  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     10  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     12  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     15  1.1  riastrad  * all copies or substantial portions of the Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     21  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     22  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     23  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors: Dave Airlie
     26  1.1  riastrad  *          Alex Deucher
     27  1.1  riastrad  *          Jerome Glisse
     28  1.1  riastrad  */
     29  1.4  riastrad 
     30  1.1  riastrad #include <sys/cdefs.h>
     31  1.4  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_atombios_dp.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $");
     32  1.1  riastrad 
     33  1.1  riastrad #include <drm/radeon_drm.h>
     34  1.1  riastrad #include "radeon.h"
     35  1.1  riastrad 
     36  1.1  riastrad #include "atom.h"
     37  1.1  riastrad #include "atom-bits.h"
     38  1.1  riastrad #include <drm/drm_dp_helper.h>
     39  1.1  riastrad 
     40  1.3       ryo #include <linux/nbsd-namespace.h>
     41  1.3       ryo 
     42  1.1  riastrad /* move these to drm_dp_helper.c/h */
     43  1.1  riastrad #define DP_LINK_CONFIGURATION_SIZE 9
     44  1.1  riastrad #define DP_DPCD_SIZE DP_RECEIVER_CAP_SIZE
     45  1.1  riastrad 
     46  1.1  riastrad static const char *voltage_names[] = {
     47  1.4  riastrad 	"0.4V", "0.6V", "0.8V", "1.2V"
     48  1.1  riastrad };
     49  1.1  riastrad static const char *pre_emph_names[] = {
     50  1.4  riastrad 	"0dB", "3.5dB", "6dB", "9.5dB"
     51  1.1  riastrad };
     52  1.1  riastrad 
     53  1.1  riastrad /***** radeon AUX functions *****/
     54  1.1  riastrad 
     55  1.1  riastrad /* Atom needs data in little endian format so swap as appropriate when copying
     56  1.1  riastrad  * data to or from atom. Note that atom operates on dw units.
     57  1.1  riastrad  *
     58  1.1  riastrad  * Use to_le=true when sending data to atom and provide at least
     59  1.1  riastrad  * ALIGN(num_bytes,4) bytes in the dst buffer.
     60  1.1  riastrad  *
     61  1.1  riastrad  * Use to_le=false when receiving data from atom and provide ALIGN(num_bytes,4)
     62  1.1  riastrad  * byes in the src buffer.
     63  1.1  riastrad  */
     64  1.1  riastrad void radeon_atom_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
     65  1.1  riastrad {
     66  1.1  riastrad #ifdef __BIG_ENDIAN
     67  1.1  riastrad 	u32 src_tmp[5], dst_tmp[5];
     68  1.1  riastrad 	int i;
     69  1.1  riastrad 	u8 align_num_bytes = ALIGN(num_bytes, 4);
     70  1.1  riastrad 
     71  1.1  riastrad 	if (to_le) {
     72  1.1  riastrad 		memcpy(src_tmp, src, num_bytes);
     73  1.1  riastrad 		for (i = 0; i < align_num_bytes / 4; i++)
     74  1.1  riastrad 			dst_tmp[i] = cpu_to_le32(src_tmp[i]);
     75  1.1  riastrad 		memcpy(dst, dst_tmp, align_num_bytes);
     76  1.1  riastrad 	} else {
     77  1.1  riastrad 		memcpy(src_tmp, src, align_num_bytes);
     78  1.1  riastrad 		for (i = 0; i < align_num_bytes / 4; i++)
     79  1.1  riastrad 			dst_tmp[i] = le32_to_cpu(src_tmp[i]);
     80  1.1  riastrad 		memcpy(dst, dst_tmp, num_bytes);
     81  1.1  riastrad 	}
     82  1.1  riastrad #else
     83  1.1  riastrad 	memcpy(dst, src, num_bytes);
     84  1.1  riastrad #endif
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad union aux_channel_transaction {
     88  1.1  riastrad 	PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
     89  1.1  riastrad 	PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
     90  1.1  riastrad };
     91  1.1  riastrad 
     92  1.1  riastrad static int radeon_process_aux_ch(struct radeon_i2c_chan *chan,
     93  1.1  riastrad 				 u8 *send, int send_bytes,
     94  1.1  riastrad 				 u8 *recv, int recv_size,
     95  1.1  riastrad 				 u8 delay, u8 *ack)
     96  1.1  riastrad {
     97  1.1  riastrad 	struct drm_device *dev = chan->dev;
     98  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     99  1.1  riastrad 	union aux_channel_transaction args;
    100  1.1  riastrad 	int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
    101  1.1  riastrad 	unsigned char *base;
    102  1.1  riastrad 	int recv_bytes;
    103  1.1  riastrad 	int r = 0;
    104  1.1  riastrad 
    105  1.1  riastrad 	memset(&args, 0, sizeof(args));
    106  1.1  riastrad 
    107  1.1  riastrad 	mutex_lock(&chan->mutex);
    108  1.1  riastrad 	mutex_lock(&rdev->mode_info.atom_context->scratch_mutex);
    109  1.1  riastrad 
    110  1.1  riastrad 	base = (unsigned char *)(rdev->mode_info.atom_context->scratch + 1);
    111  1.1  riastrad 
    112  1.1  riastrad 	radeon_atom_copy_swap(base, send, send_bytes, true);
    113  1.1  riastrad 
    114  1.1  riastrad 	args.v1.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
    115  1.1  riastrad 	args.v1.lpDataOut = cpu_to_le16((u16)(16 + 4));
    116  1.1  riastrad 	args.v1.ucDataOutLen = 0;
    117  1.1  riastrad 	args.v1.ucChannelID = chan->rec.i2c_id;
    118  1.1  riastrad 	args.v1.ucDelay = delay / 10;
    119  1.1  riastrad 	if (ASIC_IS_DCE4(rdev))
    120  1.1  riastrad 		args.v2.ucHPD_ID = chan->rec.hpd;
    121  1.1  riastrad 
    122  1.1  riastrad 	atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args);
    123  1.1  riastrad 
    124  1.1  riastrad 	*ack = args.v1.ucReplyStatus;
    125  1.1  riastrad 
    126  1.1  riastrad 	/* timeout */
    127  1.1  riastrad 	if (args.v1.ucReplyStatus == 1) {
    128  1.1  riastrad 		DRM_DEBUG_KMS("dp_aux_ch timeout\n");
    129  1.1  riastrad 		r = -ETIMEDOUT;
    130  1.1  riastrad 		goto done;
    131  1.1  riastrad 	}
    132  1.1  riastrad 
    133  1.1  riastrad 	/* flags not zero */
    134  1.1  riastrad 	if (args.v1.ucReplyStatus == 2) {
    135  1.1  riastrad 		DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
    136  1.1  riastrad 		r = -EIO;
    137  1.1  riastrad 		goto done;
    138  1.1  riastrad 	}
    139  1.1  riastrad 
    140  1.1  riastrad 	/* error */
    141  1.1  riastrad 	if (args.v1.ucReplyStatus == 3) {
    142  1.1  riastrad 		DRM_DEBUG_KMS("dp_aux_ch error\n");
    143  1.1  riastrad 		r = -EIO;
    144  1.1  riastrad 		goto done;
    145  1.1  riastrad 	}
    146  1.1  riastrad 
    147  1.1  riastrad 	recv_bytes = args.v1.ucDataOutLen;
    148  1.1  riastrad 	if (recv_bytes > recv_size)
    149  1.1  riastrad 		recv_bytes = recv_size;
    150  1.1  riastrad 
    151  1.1  riastrad 	if (recv && recv_size)
    152  1.1  riastrad 		radeon_atom_copy_swap(recv, base + 16, recv_bytes, false);
    153  1.1  riastrad 
    154  1.1  riastrad 	r = recv_bytes;
    155  1.1  riastrad done:
    156  1.1  riastrad 	mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex);
    157  1.1  riastrad 	mutex_unlock(&chan->mutex);
    158  1.1  riastrad 
    159  1.1  riastrad 	return r;
    160  1.1  riastrad }
    161  1.1  riastrad 
    162  1.1  riastrad #define BARE_ADDRESS_SIZE 3
    163  1.1  riastrad #define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
    164  1.1  riastrad 
    165  1.1  riastrad static ssize_t
    166  1.1  riastrad radeon_dp_aux_transfer_atom(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
    167  1.1  riastrad {
    168  1.1  riastrad 	struct radeon_i2c_chan *chan =
    169  1.1  riastrad 		container_of(aux, struct radeon_i2c_chan, aux);
    170  1.1  riastrad 	int ret;
    171  1.1  riastrad 	u8 tx_buf[20];
    172  1.1  riastrad 	size_t tx_size;
    173  1.1  riastrad 	u8 ack, delay = 0;
    174  1.1  riastrad 
    175  1.1  riastrad 	if (WARN_ON(msg->size > 16))
    176  1.1  riastrad 		return -E2BIG;
    177  1.1  riastrad 
    178  1.1  riastrad 	tx_buf[0] = msg->address & 0xff;
    179  1.1  riastrad 	tx_buf[1] = (msg->address >> 8) & 0xff;
    180  1.1  riastrad 	tx_buf[2] = (msg->request << 4) |
    181  1.1  riastrad 		((msg->address >> 16) & 0xf);
    182  1.1  riastrad 	tx_buf[3] = msg->size ? (msg->size - 1) : 0;
    183  1.1  riastrad 
    184  1.1  riastrad 	switch (msg->request & ~DP_AUX_I2C_MOT) {
    185  1.1  riastrad 	case DP_AUX_NATIVE_WRITE:
    186  1.1  riastrad 	case DP_AUX_I2C_WRITE:
    187  1.1  riastrad 	case DP_AUX_I2C_WRITE_STATUS_UPDATE:
    188  1.1  riastrad 		/* The atom implementation only supports writes with a max payload of
    189  1.1  riastrad 		 * 12 bytes since it uses 4 bits for the total count (header + payload)
    190  1.1  riastrad 		 * in the parameter space.  The atom interface supports 16 byte
    191  1.1  riastrad 		 * payloads for reads. The hw itself supports up to 16 bytes of payload.
    192  1.1  riastrad 		 */
    193  1.1  riastrad 		if (WARN_ON_ONCE(msg->size > 12))
    194  1.1  riastrad 			return -E2BIG;
    195  1.1  riastrad 		/* tx_size needs to be 4 even for bare address packets since the atom
    196  1.1  riastrad 		 * table needs the info in tx_buf[3].
    197  1.1  riastrad 		 */
    198  1.1  riastrad 		tx_size = HEADER_SIZE + msg->size;
    199  1.1  riastrad 		if (msg->size == 0)
    200  1.1  riastrad 			tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
    201  1.2   msaitoh 		else {
    202  1.1  riastrad 			tx_buf[3] |= tx_size << 4;
    203  1.2   msaitoh 			memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
    204  1.2   msaitoh 		}
    205  1.1  riastrad 		ret = radeon_process_aux_ch(chan,
    206  1.1  riastrad 					    tx_buf, tx_size, NULL, 0, delay, &ack);
    207  1.1  riastrad 		if (ret >= 0)
    208  1.1  riastrad 			/* Return payload size. */
    209  1.1  riastrad 			ret = msg->size;
    210  1.1  riastrad 		break;
    211  1.1  riastrad 	case DP_AUX_NATIVE_READ:
    212  1.1  riastrad 	case DP_AUX_I2C_READ:
    213  1.1  riastrad 		/* tx_size needs to be 4 even for bare address packets since the atom
    214  1.1  riastrad 		 * table needs the info in tx_buf[3].
    215  1.1  riastrad 		 */
    216  1.1  riastrad 		tx_size = HEADER_SIZE;
    217  1.1  riastrad 		if (msg->size == 0)
    218  1.1  riastrad 			tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
    219  1.1  riastrad 		else
    220  1.1  riastrad 			tx_buf[3] |= tx_size << 4;
    221  1.1  riastrad 		ret = radeon_process_aux_ch(chan,
    222  1.1  riastrad 					    tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
    223  1.1  riastrad 		break;
    224  1.1  riastrad 	default:
    225  1.1  riastrad 		ret = -EINVAL;
    226  1.1  riastrad 		break;
    227  1.1  riastrad 	}
    228  1.1  riastrad 
    229  1.1  riastrad 	if (ret >= 0)
    230  1.1  riastrad 		msg->reply = ack >> 4;
    231  1.1  riastrad 
    232  1.1  riastrad 	return ret;
    233  1.1  riastrad }
    234  1.1  riastrad 
    235  1.1  riastrad void radeon_dp_aux_init(struct radeon_connector *radeon_connector)
    236  1.1  riastrad {
    237  1.1  riastrad 	struct drm_device *dev = radeon_connector->base.dev;
    238  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    239  1.1  riastrad 	int ret;
    240  1.1  riastrad 
    241  1.1  riastrad 	radeon_connector->ddc_bus->rec.hpd = radeon_connector->hpd.hpd;
    242  1.1  riastrad 	radeon_connector->ddc_bus->aux.dev = radeon_connector->base.kdev;
    243  1.1  riastrad 	if (ASIC_IS_DCE5(rdev)) {
    244  1.1  riastrad 		if (radeon_auxch)
    245  1.1  riastrad 			radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_native;
    246  1.1  riastrad 		else
    247  1.1  riastrad 			radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_atom;
    248  1.1  riastrad 	} else {
    249  1.1  riastrad 		radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_atom;
    250  1.1  riastrad 	}
    251  1.1  riastrad 
    252  1.1  riastrad #ifdef __NetBSD__
    253  1.1  riastrad 	/* XXX dervied from sysfs/i2c on linux. */
    254  1.1  riastrad 	radeon_connector->ddc_bus->aux.name = "radeon_dp_aux";
    255  1.1  riastrad #endif
    256  1.1  riastrad 
    257  1.1  riastrad 	ret = drm_dp_aux_register(&radeon_connector->ddc_bus->aux);
    258  1.1  riastrad 	if (!ret)
    259  1.1  riastrad 		radeon_connector->ddc_bus->has_aux = true;
    260  1.1  riastrad 
    261  1.1  riastrad 	WARN(ret, "drm_dp_aux_register() failed with error %d\n", ret);
    262  1.1  riastrad }
    263  1.1  riastrad 
    264  1.1  riastrad /***** general DP utility functions *****/
    265  1.1  riastrad 
    266  1.1  riastrad #define DP_VOLTAGE_MAX         DP_TRAIN_VOLTAGE_SWING_LEVEL_3
    267  1.1  riastrad #define DP_PRE_EMPHASIS_MAX    DP_TRAIN_PRE_EMPH_LEVEL_3
    268  1.1  riastrad 
    269  1.1  riastrad static void dp_get_adjust_train(const u8 link_status[DP_LINK_STATUS_SIZE],
    270  1.1  riastrad 				int lane_count,
    271  1.1  riastrad 				u8 train_set[4])
    272  1.1  riastrad {
    273  1.1  riastrad 	u8 v = 0;
    274  1.1  riastrad 	u8 p = 0;
    275  1.1  riastrad 	int lane;
    276  1.1  riastrad 
    277  1.1  riastrad 	for (lane = 0; lane < lane_count; lane++) {
    278  1.1  riastrad 		u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
    279  1.1  riastrad 		u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
    280  1.1  riastrad 
    281  1.1  riastrad 		DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
    282  1.1  riastrad 			  lane,
    283  1.1  riastrad 			  voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
    284  1.1  riastrad 			  pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
    285  1.1  riastrad 
    286  1.1  riastrad 		if (this_v > v)
    287  1.1  riastrad 			v = this_v;
    288  1.1  riastrad 		if (this_p > p)
    289  1.1  riastrad 			p = this_p;
    290  1.1  riastrad 	}
    291  1.1  riastrad 
    292  1.1  riastrad 	if (v >= DP_VOLTAGE_MAX)
    293  1.1  riastrad 		v |= DP_TRAIN_MAX_SWING_REACHED;
    294  1.1  riastrad 
    295  1.1  riastrad 	if (p >= DP_PRE_EMPHASIS_MAX)
    296  1.1  riastrad 		p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
    297  1.1  riastrad 
    298  1.1  riastrad 	DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
    299  1.1  riastrad 		  voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
    300  1.1  riastrad 		  pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
    301  1.1  riastrad 
    302  1.1  riastrad 	for (lane = 0; lane < 4; lane++)
    303  1.1  riastrad 		train_set[lane] = v | p;
    304  1.1  riastrad }
    305  1.1  riastrad 
    306  1.1  riastrad /* convert bits per color to bits per pixel */
    307  1.1  riastrad /* get bpc from the EDID */
    308  1.1  riastrad static int convert_bpc_to_bpp(int bpc)
    309  1.1  riastrad {
    310  1.1  riastrad 	if (bpc == 0)
    311  1.1  riastrad 		return 24;
    312  1.1  riastrad 	else
    313  1.1  riastrad 		return bpc * 3;
    314  1.1  riastrad }
    315  1.1  riastrad 
    316  1.1  riastrad /***** radeon specific DP functions *****/
    317  1.1  riastrad 
    318  1.4  riastrad static int radeon_dp_get_dp_link_config(struct drm_connector *connector,
    319  1.4  riastrad 					const u8 dpcd[DP_DPCD_SIZE],
    320  1.4  riastrad 					unsigned pix_clock,
    321  1.4  riastrad 					unsigned *dp_lanes, unsigned *dp_rate)
    322  1.1  riastrad {
    323  1.1  riastrad 	int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
    324  1.1  riastrad 	static const unsigned link_rates[3] = { 162000, 270000, 540000 };
    325  1.1  riastrad 	unsigned max_link_rate = drm_dp_max_link_rate(dpcd);
    326  1.1  riastrad 	unsigned max_lane_num = drm_dp_max_lane_count(dpcd);
    327  1.1  riastrad 	unsigned lane_num, i, max_pix_clock;
    328  1.1  riastrad 
    329  1.1  riastrad 	if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
    330  1.1  riastrad 	    ENCODER_OBJECT_ID_NUTMEG) {
    331  1.1  riastrad 		for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
    332  1.1  riastrad 			max_pix_clock = (lane_num * 270000 * 8) / bpp;
    333  1.1  riastrad 			if (max_pix_clock >= pix_clock) {
    334  1.1  riastrad 				*dp_lanes = lane_num;
    335  1.1  riastrad 				*dp_rate = 270000;
    336  1.1  riastrad 				return 0;
    337  1.1  riastrad 			}
    338  1.1  riastrad 		}
    339  1.1  riastrad 	} else {
    340  1.1  riastrad 		for (i = 0; i < ARRAY_SIZE(link_rates) && link_rates[i] <= max_link_rate; i++) {
    341  1.1  riastrad 			for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
    342  1.1  riastrad 				max_pix_clock = (lane_num * link_rates[i] * 8) / bpp;
    343  1.1  riastrad 				if (max_pix_clock >= pix_clock) {
    344  1.1  riastrad 					*dp_lanes = lane_num;
    345  1.1  riastrad 					*dp_rate = link_rates[i];
    346  1.1  riastrad 					return 0;
    347  1.1  riastrad 				}
    348  1.1  riastrad 			}
    349  1.1  riastrad 		}
    350  1.1  riastrad 	}
    351  1.1  riastrad 
    352  1.1  riastrad 	return -EINVAL;
    353  1.1  riastrad }
    354  1.1  riastrad 
    355  1.1  riastrad static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
    356  1.1  riastrad 				    int action, int dp_clock,
    357  1.1  riastrad 				    u8 ucconfig, u8 lane_num)
    358  1.1  riastrad {
    359  1.1  riastrad 	DP_ENCODER_SERVICE_PARAMETERS args;
    360  1.1  riastrad 	int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
    361  1.1  riastrad 
    362  1.1  riastrad 	memset(&args, 0, sizeof(args));
    363  1.1  riastrad 	args.ucLinkClock = dp_clock / 10;
    364  1.1  riastrad 	args.ucConfig = ucconfig;
    365  1.1  riastrad 	args.ucAction = action;
    366  1.1  riastrad 	args.ucLaneNum = lane_num;
    367  1.1  riastrad 	args.ucStatus = 0;
    368  1.1  riastrad 
    369  1.1  riastrad 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
    370  1.1  riastrad 	return args.ucStatus;
    371  1.1  riastrad }
    372  1.1  riastrad 
    373  1.1  riastrad u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
    374  1.1  riastrad {
    375  1.1  riastrad 	struct drm_device *dev = radeon_connector->base.dev;
    376  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    377  1.1  riastrad 
    378  1.1  riastrad 	return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
    379  1.1  riastrad 					 radeon_connector->ddc_bus->rec.i2c_id, 0);
    380  1.1  riastrad }
    381  1.1  riastrad 
    382  1.1  riastrad static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
    383  1.1  riastrad {
    384  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
    385  1.1  riastrad 	u8 buf[3];
    386  1.1  riastrad 
    387  1.1  riastrad 	if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
    388  1.1  riastrad 		return;
    389  1.1  riastrad 
    390  1.1  riastrad 	if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_SINK_OUI, buf, 3) == 3)
    391  1.1  riastrad 		DRM_DEBUG_KMS("Sink OUI: %02hhx%02hhx%02hhx\n",
    392  1.1  riastrad 			      buf[0], buf[1], buf[2]);
    393  1.1  riastrad 
    394  1.1  riastrad 	if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_BRANCH_OUI, buf, 3) == 3)
    395  1.1  riastrad 		DRM_DEBUG_KMS("Branch OUI: %02hhx%02hhx%02hhx\n",
    396  1.1  riastrad 			      buf[0], buf[1], buf[2]);
    397  1.1  riastrad }
    398  1.1  riastrad 
    399  1.1  riastrad bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
    400  1.1  riastrad {
    401  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
    402  1.1  riastrad 	u8 msg[DP_DPCD_SIZE];
    403  1.4  riastrad 	int ret;
    404  1.1  riastrad 
    405  1.4  riastrad 	ret = drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_DPCD_REV, msg,
    406  1.4  riastrad 			       DP_DPCD_SIZE);
    407  1.4  riastrad 	if (ret == DP_DPCD_SIZE) {
    408  1.4  riastrad 		memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
    409  1.1  riastrad 
    410  1.4  riastrad 		DRM_DEBUG_KMS("DPCD: %*ph\n", (int)sizeof(dig_connector->dpcd),
    411  1.4  riastrad 			      dig_connector->dpcd);
    412  1.1  riastrad 
    413  1.4  riastrad 		radeon_dp_probe_oui(radeon_connector);
    414  1.1  riastrad 
    415  1.4  riastrad 		return true;
    416  1.1  riastrad 	}
    417  1.4  riastrad 
    418  1.1  riastrad 	dig_connector->dpcd[0] = 0;
    419  1.1  riastrad 	return false;
    420  1.1  riastrad }
    421  1.1  riastrad 
    422  1.1  riastrad int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
    423  1.1  riastrad 			     struct drm_connector *connector)
    424  1.1  riastrad {
    425  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    426  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    427  1.1  riastrad 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    428  1.1  riastrad 	int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
    429  1.1  riastrad 	u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
    430  1.1  riastrad 	u8 tmp;
    431  1.1  riastrad 
    432  1.1  riastrad 	if (!ASIC_IS_DCE4(rdev))
    433  1.1  riastrad 		return panel_mode;
    434  1.1  riastrad 
    435  1.1  riastrad 	if (!radeon_connector->con_priv)
    436  1.1  riastrad 		return panel_mode;
    437  1.1  riastrad 
    438  1.1  riastrad 	if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
    439  1.1  riastrad 		/* DP bridge chips */
    440  1.1  riastrad 		if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
    441  1.1  riastrad 				      DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
    442  1.1  riastrad 			if (tmp & 1)
    443  1.1  riastrad 				panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
    444  1.1  riastrad 			else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
    445  1.1  riastrad 				 (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
    446  1.1  riastrad 				panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
    447  1.1  riastrad 			else
    448  1.1  riastrad 				panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
    449  1.1  riastrad 		}
    450  1.1  riastrad 	} else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
    451  1.1  riastrad 		/* eDP */
    452  1.1  riastrad 		if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
    453  1.1  riastrad 				      DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
    454  1.1  riastrad 			if (tmp & 1)
    455  1.1  riastrad 				panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
    456  1.1  riastrad 		}
    457  1.1  riastrad 	}
    458  1.1  riastrad 
    459  1.1  riastrad 	return panel_mode;
    460  1.1  riastrad }
    461  1.1  riastrad 
    462  1.1  riastrad void radeon_dp_set_link_config(struct drm_connector *connector,
    463  1.1  riastrad 			       const struct drm_display_mode *mode)
    464  1.1  riastrad {
    465  1.1  riastrad 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    466  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector;
    467  1.1  riastrad 	int ret;
    468  1.1  riastrad 
    469  1.1  riastrad 	if (!radeon_connector->con_priv)
    470  1.1  riastrad 		return;
    471  1.1  riastrad 	dig_connector = radeon_connector->con_priv;
    472  1.1  riastrad 
    473  1.1  riastrad 	if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
    474  1.1  riastrad 	    (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
    475  1.1  riastrad 		ret = radeon_dp_get_dp_link_config(connector, dig_connector->dpcd,
    476  1.1  riastrad 						   mode->clock,
    477  1.1  riastrad 						   &dig_connector->dp_lane_count,
    478  1.1  riastrad 						   &dig_connector->dp_clock);
    479  1.1  riastrad 		if (ret) {
    480  1.1  riastrad 			dig_connector->dp_clock = 0;
    481  1.1  riastrad 			dig_connector->dp_lane_count = 0;
    482  1.1  riastrad 		}
    483  1.1  riastrad 	}
    484  1.1  riastrad }
    485  1.1  riastrad 
    486  1.1  riastrad int radeon_dp_mode_valid_helper(struct drm_connector *connector,
    487  1.1  riastrad 				struct drm_display_mode *mode)
    488  1.1  riastrad {
    489  1.1  riastrad 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    490  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector;
    491  1.1  riastrad 	unsigned dp_clock, dp_lanes;
    492  1.1  riastrad 	int ret;
    493  1.1  riastrad 
    494  1.1  riastrad 	if ((mode->clock > 340000) &&
    495  1.1  riastrad 	    (!radeon_connector_is_dp12_capable(connector)))
    496  1.1  riastrad 		return MODE_CLOCK_HIGH;
    497  1.1  riastrad 
    498  1.1  riastrad 	if (!radeon_connector->con_priv)
    499  1.1  riastrad 		return MODE_CLOCK_HIGH;
    500  1.1  riastrad 	dig_connector = radeon_connector->con_priv;
    501  1.1  riastrad 
    502  1.1  riastrad 	ret = radeon_dp_get_dp_link_config(connector, dig_connector->dpcd,
    503  1.1  riastrad 					   mode->clock,
    504  1.1  riastrad 					   &dp_lanes,
    505  1.1  riastrad 					   &dp_clock);
    506  1.1  riastrad 	if (ret)
    507  1.1  riastrad 		return MODE_CLOCK_HIGH;
    508  1.1  riastrad 
    509  1.1  riastrad 	if ((dp_clock == 540000) &&
    510  1.1  riastrad 	    (!radeon_connector_is_dp12_capable(connector)))
    511  1.1  riastrad 		return MODE_CLOCK_HIGH;
    512  1.1  riastrad 
    513  1.1  riastrad 	return MODE_OK;
    514  1.1  riastrad }
    515  1.1  riastrad 
    516  1.1  riastrad bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
    517  1.1  riastrad {
    518  1.1  riastrad 	u8 link_status[DP_LINK_STATUS_SIZE];
    519  1.1  riastrad 	struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
    520  1.1  riastrad 
    521  1.1  riastrad 	if (drm_dp_dpcd_read_link_status(&radeon_connector->ddc_bus->aux, link_status)
    522  1.1  riastrad 	    <= 0)
    523  1.1  riastrad 		return false;
    524  1.1  riastrad 	if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
    525  1.1  riastrad 		return false;
    526  1.1  riastrad 	return true;
    527  1.1  riastrad }
    528  1.1  riastrad 
    529  1.1  riastrad void radeon_dp_set_rx_power_state(struct drm_connector *connector,
    530  1.1  riastrad 				  u8 power_state)
    531  1.1  riastrad {
    532  1.1  riastrad 	struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    533  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector;
    534  1.1  riastrad 
    535  1.1  riastrad 	if (!radeon_connector->con_priv)
    536  1.1  riastrad 		return;
    537  1.1  riastrad 
    538  1.1  riastrad 	dig_connector = radeon_connector->con_priv;
    539  1.1  riastrad 
    540  1.1  riastrad 	/* power up/down the sink */
    541  1.1  riastrad 	if (dig_connector->dpcd[0] >= 0x11) {
    542  1.1  riastrad 		drm_dp_dpcd_writeb(&radeon_connector->ddc_bus->aux,
    543  1.1  riastrad 				   DP_SET_POWER, power_state);
    544  1.1  riastrad 		usleep_range(1000, 2000);
    545  1.1  riastrad 	}
    546  1.1  riastrad }
    547  1.1  riastrad 
    548  1.1  riastrad 
    549  1.1  riastrad struct radeon_dp_link_train_info {
    550  1.1  riastrad 	struct radeon_device *rdev;
    551  1.1  riastrad 	struct drm_encoder *encoder;
    552  1.1  riastrad 	struct drm_connector *connector;
    553  1.1  riastrad 	int enc_id;
    554  1.1  riastrad 	int dp_clock;
    555  1.1  riastrad 	int dp_lane_count;
    556  1.1  riastrad 	bool tp3_supported;
    557  1.1  riastrad 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
    558  1.1  riastrad 	u8 train_set[4];
    559  1.1  riastrad 	u8 link_status[DP_LINK_STATUS_SIZE];
    560  1.1  riastrad 	u8 tries;
    561  1.1  riastrad 	bool use_dpencoder;
    562  1.1  riastrad 	struct drm_dp_aux *aux;
    563  1.1  riastrad };
    564  1.1  riastrad 
    565  1.1  riastrad static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
    566  1.1  riastrad {
    567  1.1  riastrad 	/* set the initial vs/emph on the source */
    568  1.1  riastrad 	atombios_dig_transmitter_setup(dp_info->encoder,
    569  1.1  riastrad 				       ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
    570  1.1  riastrad 				       0, dp_info->train_set[0]); /* sets all lanes at once */
    571  1.1  riastrad 
    572  1.1  riastrad 	/* set the vs/emph on the sink */
    573  1.1  riastrad 	drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
    574  1.1  riastrad 			  dp_info->train_set, dp_info->dp_lane_count);
    575  1.1  riastrad }
    576  1.1  riastrad 
    577  1.1  riastrad static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
    578  1.1  riastrad {
    579  1.1  riastrad 	int rtp = 0;
    580  1.1  riastrad 
    581  1.1  riastrad 	/* set training pattern on the source */
    582  1.1  riastrad 	if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
    583  1.1  riastrad 		switch (tp) {
    584  1.1  riastrad 		case DP_TRAINING_PATTERN_1:
    585  1.1  riastrad 			rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
    586  1.1  riastrad 			break;
    587  1.1  riastrad 		case DP_TRAINING_PATTERN_2:
    588  1.1  riastrad 			rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
    589  1.1  riastrad 			break;
    590  1.1  riastrad 		case DP_TRAINING_PATTERN_3:
    591  1.1  riastrad 			rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
    592  1.1  riastrad 			break;
    593  1.1  riastrad 		}
    594  1.1  riastrad 		atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
    595  1.1  riastrad 	} else {
    596  1.1  riastrad 		switch (tp) {
    597  1.1  riastrad 		case DP_TRAINING_PATTERN_1:
    598  1.1  riastrad 			rtp = 0;
    599  1.1  riastrad 			break;
    600  1.1  riastrad 		case DP_TRAINING_PATTERN_2:
    601  1.1  riastrad 			rtp = 1;
    602  1.1  riastrad 			break;
    603  1.1  riastrad 		}
    604  1.1  riastrad 		radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
    605  1.1  riastrad 					  dp_info->dp_clock, dp_info->enc_id, rtp);
    606  1.1  riastrad 	}
    607  1.1  riastrad 
    608  1.1  riastrad 	/* enable training pattern on the sink */
    609  1.1  riastrad 	drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
    610  1.1  riastrad }
    611  1.1  riastrad 
    612  1.1  riastrad static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
    613  1.1  riastrad {
    614  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
    615  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    616  1.1  riastrad 	u8 tmp;
    617  1.1  riastrad 
    618  1.1  riastrad 	/* power up the sink */
    619  1.1  riastrad 	radeon_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
    620  1.1  riastrad 
    621  1.1  riastrad 	/* possibly enable downspread on the sink */
    622  1.1  riastrad 	if (dp_info->dpcd[3] & 0x1)
    623  1.1  riastrad 		drm_dp_dpcd_writeb(dp_info->aux,
    624  1.1  riastrad 				   DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
    625  1.1  riastrad 	else
    626  1.1  riastrad 		drm_dp_dpcd_writeb(dp_info->aux,
    627  1.1  riastrad 				   DP_DOWNSPREAD_CTRL, 0);
    628  1.1  riastrad 
    629  1.1  riastrad 	if (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)
    630  1.1  riastrad 		drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
    631  1.1  riastrad 
    632  1.1  riastrad 	/* set the lane count on the sink */
    633  1.1  riastrad 	tmp = dp_info->dp_lane_count;
    634  1.1  riastrad 	if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
    635  1.1  riastrad 		tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
    636  1.1  riastrad 	drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
    637  1.1  riastrad 
    638  1.1  riastrad 	/* set the link rate on the sink */
    639  1.1  riastrad 	tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
    640  1.1  riastrad 	drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
    641  1.1  riastrad 
    642  1.1  riastrad 	/* start training on the source */
    643  1.1  riastrad 	if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
    644  1.1  riastrad 		atombios_dig_encoder_setup(dp_info->encoder,
    645  1.1  riastrad 					   ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
    646  1.1  riastrad 	else
    647  1.1  riastrad 		radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
    648  1.1  riastrad 					  dp_info->dp_clock, dp_info->enc_id, 0);
    649  1.1  riastrad 
    650  1.1  riastrad 	/* disable the training pattern on the sink */
    651  1.1  riastrad 	drm_dp_dpcd_writeb(dp_info->aux,
    652  1.1  riastrad 			   DP_TRAINING_PATTERN_SET,
    653  1.1  riastrad 			   DP_TRAINING_PATTERN_DISABLE);
    654  1.1  riastrad 
    655  1.1  riastrad 	return 0;
    656  1.1  riastrad }
    657  1.1  riastrad 
    658  1.1  riastrad static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
    659  1.1  riastrad {
    660  1.1  riastrad 	udelay(400);
    661  1.1  riastrad 
    662  1.1  riastrad 	/* disable the training pattern on the sink */
    663  1.1  riastrad 	drm_dp_dpcd_writeb(dp_info->aux,
    664  1.1  riastrad 			   DP_TRAINING_PATTERN_SET,
    665  1.1  riastrad 			   DP_TRAINING_PATTERN_DISABLE);
    666  1.1  riastrad 
    667  1.1  riastrad 	/* disable the training pattern on the source */
    668  1.1  riastrad 	if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
    669  1.1  riastrad 		atombios_dig_encoder_setup(dp_info->encoder,
    670  1.1  riastrad 					   ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
    671  1.1  riastrad 	else
    672  1.1  riastrad 		radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
    673  1.1  riastrad 					  dp_info->dp_clock, dp_info->enc_id, 0);
    674  1.1  riastrad 
    675  1.1  riastrad 	return 0;
    676  1.1  riastrad }
    677  1.1  riastrad 
    678  1.1  riastrad static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
    679  1.1  riastrad {
    680  1.1  riastrad 	bool clock_recovery;
    681  1.1  riastrad  	u8 voltage;
    682  1.1  riastrad 	int i;
    683  1.1  riastrad 
    684  1.1  riastrad 	radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
    685  1.1  riastrad 	memset(dp_info->train_set, 0, 4);
    686  1.1  riastrad 	radeon_dp_update_vs_emph(dp_info);
    687  1.1  riastrad 
    688  1.1  riastrad 	udelay(400);
    689  1.1  riastrad 
    690  1.1  riastrad 	/* clock recovery loop */
    691  1.1  riastrad 	clock_recovery = false;
    692  1.1  riastrad 	dp_info->tries = 0;
    693  1.1  riastrad 	voltage = 0xff;
    694  1.1  riastrad 	while (1) {
    695  1.1  riastrad 		drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
    696  1.1  riastrad 
    697  1.1  riastrad 		if (drm_dp_dpcd_read_link_status(dp_info->aux,
    698  1.1  riastrad 						 dp_info->link_status) <= 0) {
    699  1.1  riastrad 			DRM_ERROR("displayport link status failed\n");
    700  1.1  riastrad 			break;
    701  1.1  riastrad 		}
    702  1.1  riastrad 
    703  1.1  riastrad 		if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
    704  1.1  riastrad 			clock_recovery = true;
    705  1.1  riastrad 			break;
    706  1.1  riastrad 		}
    707  1.1  riastrad 
    708  1.1  riastrad 		for (i = 0; i < dp_info->dp_lane_count; i++) {
    709  1.1  riastrad 			if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
    710  1.1  riastrad 				break;
    711  1.1  riastrad 		}
    712  1.1  riastrad 		if (i == dp_info->dp_lane_count) {
    713  1.1  riastrad 			DRM_ERROR("clock recovery reached max voltage\n");
    714  1.1  riastrad 			break;
    715  1.1  riastrad 		}
    716  1.1  riastrad 
    717  1.1  riastrad 		if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
    718  1.1  riastrad 			++dp_info->tries;
    719  1.1  riastrad 			if (dp_info->tries == 5) {
    720  1.1  riastrad 				DRM_ERROR("clock recovery tried 5 times\n");
    721  1.1  riastrad 				break;
    722  1.1  riastrad 			}
    723  1.1  riastrad 		} else
    724  1.1  riastrad 			dp_info->tries = 0;
    725  1.1  riastrad 
    726  1.1  riastrad 		voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
    727  1.1  riastrad 
    728  1.1  riastrad 		/* Compute new train_set as requested by sink */
    729  1.1  riastrad 		dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
    730  1.1  riastrad 
    731  1.1  riastrad 		radeon_dp_update_vs_emph(dp_info);
    732  1.1  riastrad 	}
    733  1.1  riastrad 	if (!clock_recovery) {
    734  1.1  riastrad 		DRM_ERROR("clock recovery failed\n");
    735  1.1  riastrad 		return -1;
    736  1.1  riastrad 	} else {
    737  1.1  riastrad 		DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
    738  1.1  riastrad 			  dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
    739  1.1  riastrad 			  (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
    740  1.1  riastrad 			  DP_TRAIN_PRE_EMPHASIS_SHIFT);
    741  1.1  riastrad 		return 0;
    742  1.1  riastrad 	}
    743  1.1  riastrad }
    744  1.1  riastrad 
    745  1.1  riastrad static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
    746  1.1  riastrad {
    747  1.1  riastrad 	bool channel_eq;
    748  1.1  riastrad 
    749  1.1  riastrad 	if (dp_info->tp3_supported)
    750  1.1  riastrad 		radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
    751  1.1  riastrad 	else
    752  1.1  riastrad 		radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
    753  1.1  riastrad 
    754  1.1  riastrad 	/* channel equalization loop */
    755  1.1  riastrad 	dp_info->tries = 0;
    756  1.1  riastrad 	channel_eq = false;
    757  1.1  riastrad 	while (1) {
    758  1.1  riastrad 		drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
    759  1.1  riastrad 
    760  1.1  riastrad 		if (drm_dp_dpcd_read_link_status(dp_info->aux,
    761  1.1  riastrad 						 dp_info->link_status) <= 0) {
    762  1.1  riastrad 			DRM_ERROR("displayport link status failed\n");
    763  1.1  riastrad 			break;
    764  1.1  riastrad 		}
    765  1.1  riastrad 
    766  1.1  riastrad 		if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
    767  1.1  riastrad 			channel_eq = true;
    768  1.1  riastrad 			break;
    769  1.1  riastrad 		}
    770  1.1  riastrad 
    771  1.1  riastrad 		/* Try 5 times */
    772  1.1  riastrad 		if (dp_info->tries > 5) {
    773  1.1  riastrad 			DRM_ERROR("channel eq failed: 5 tries\n");
    774  1.1  riastrad 			break;
    775  1.1  riastrad 		}
    776  1.1  riastrad 
    777  1.1  riastrad 		/* Compute new train_set as requested by sink */
    778  1.1  riastrad 		dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
    779  1.1  riastrad 
    780  1.1  riastrad 		radeon_dp_update_vs_emph(dp_info);
    781  1.1  riastrad 		dp_info->tries++;
    782  1.1  riastrad 	}
    783  1.1  riastrad 
    784  1.1  riastrad 	if (!channel_eq) {
    785  1.1  riastrad 		DRM_ERROR("channel eq failed\n");
    786  1.1  riastrad 		return -1;
    787  1.1  riastrad 	} else {
    788  1.1  riastrad 		DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
    789  1.1  riastrad 			  dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
    790  1.1  riastrad 			  (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
    791  1.1  riastrad 			  >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
    792  1.1  riastrad 		return 0;
    793  1.1  riastrad 	}
    794  1.1  riastrad }
    795  1.1  riastrad 
    796  1.1  riastrad void radeon_dp_link_train(struct drm_encoder *encoder,
    797  1.1  riastrad 			  struct drm_connector *connector)
    798  1.1  riastrad {
    799  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    800  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    801  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    802  1.1  riastrad 	struct radeon_encoder_atom_dig *dig;
    803  1.1  riastrad 	struct radeon_connector *radeon_connector;
    804  1.1  riastrad 	struct radeon_connector_atom_dig *dig_connector;
    805  1.1  riastrad 	struct radeon_dp_link_train_info dp_info;
    806  1.1  riastrad 	int index;
    807  1.1  riastrad 	u8 tmp, frev, crev;
    808  1.1  riastrad 
    809  1.1  riastrad 	if (!radeon_encoder->enc_priv)
    810  1.1  riastrad 		return;
    811  1.1  riastrad 	dig = radeon_encoder->enc_priv;
    812  1.1  riastrad 
    813  1.1  riastrad 	radeon_connector = to_radeon_connector(connector);
    814  1.1  riastrad 	if (!radeon_connector->con_priv)
    815  1.1  riastrad 		return;
    816  1.1  riastrad 	dig_connector = radeon_connector->con_priv;
    817  1.1  riastrad 
    818  1.1  riastrad 	if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
    819  1.1  riastrad 	    (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
    820  1.1  riastrad 		return;
    821  1.1  riastrad 
    822  1.1  riastrad 	/* DPEncoderService newer than 1.1 can't program properly the
    823  1.1  riastrad 	 * training pattern. When facing such version use the
    824  1.1  riastrad 	 * DIGXEncoderControl (X== 1 | 2)
    825  1.1  riastrad 	 */
    826  1.1  riastrad 	dp_info.use_dpencoder = true;
    827  1.1  riastrad 	index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
    828  1.1  riastrad 	if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
    829  1.4  riastrad 		if (crev > 1)
    830  1.1  riastrad 			dp_info.use_dpencoder = false;
    831  1.1  riastrad 	}
    832  1.1  riastrad 
    833  1.1  riastrad 	dp_info.enc_id = 0;
    834  1.1  riastrad 	if (dig->dig_encoder)
    835  1.1  riastrad 		dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
    836  1.1  riastrad 	else
    837  1.1  riastrad 		dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
    838  1.1  riastrad 	if (dig->linkb)
    839  1.1  riastrad 		dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
    840  1.1  riastrad 	else
    841  1.1  riastrad 		dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
    842  1.1  riastrad 
    843  1.1  riastrad 	if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux, DP_MAX_LANE_COUNT, &tmp)
    844  1.1  riastrad 	    == 1) {
    845  1.1  riastrad 		if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
    846  1.1  riastrad 			dp_info.tp3_supported = true;
    847  1.1  riastrad 		else
    848  1.1  riastrad 			dp_info.tp3_supported = false;
    849  1.1  riastrad 	} else {
    850  1.1  riastrad 		dp_info.tp3_supported = false;
    851  1.1  riastrad 	}
    852  1.1  riastrad 
    853  1.1  riastrad 	memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
    854  1.1  riastrad 	dp_info.rdev = rdev;
    855  1.1  riastrad 	dp_info.encoder = encoder;
    856  1.1  riastrad 	dp_info.connector = connector;
    857  1.1  riastrad 	dp_info.dp_lane_count = dig_connector->dp_lane_count;
    858  1.1  riastrad 	dp_info.dp_clock = dig_connector->dp_clock;
    859  1.1  riastrad 	dp_info.aux = &radeon_connector->ddc_bus->aux;
    860  1.1  riastrad 
    861  1.1  riastrad 	if (radeon_dp_link_train_init(&dp_info))
    862  1.1  riastrad 		goto done;
    863  1.1  riastrad 	if (radeon_dp_link_train_cr(&dp_info))
    864  1.1  riastrad 		goto done;
    865  1.1  riastrad 	if (radeon_dp_link_train_ce(&dp_info))
    866  1.1  riastrad 		goto done;
    867  1.1  riastrad done:
    868  1.1  riastrad 	if (radeon_dp_link_train_finish(&dp_info))
    869  1.1  riastrad 		return;
    870  1.1  riastrad }
    871