Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_dp_auxch.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2015 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Dave Airlie
     25  1.1  riastrad  */
     26  1.3  riastrad 
     27  1.1  riastrad #include <sys/cdefs.h>
     28  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_dp_auxch.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     29  1.1  riastrad 
     30  1.1  riastrad #include <drm/radeon_drm.h>
     31  1.1  riastrad #include "radeon.h"
     32  1.1  riastrad #include "nid.h"
     33  1.1  riastrad 
     34  1.1  riastrad #define AUX_RX_ERROR_FLAGS (AUX_SW_RX_OVERFLOW |	     \
     35  1.1  riastrad 			    AUX_SW_RX_HPD_DISCON |	     \
     36  1.1  riastrad 			    AUX_SW_RX_PARTIAL_BYTE |	     \
     37  1.1  riastrad 			    AUX_SW_NON_AUX_MODE |	     \
     38  1.1  riastrad 			    AUX_SW_RX_SYNC_INVALID_L |	     \
     39  1.1  riastrad 			    AUX_SW_RX_SYNC_INVALID_H |	     \
     40  1.1  riastrad 			    AUX_SW_RX_INVALID_START |	     \
     41  1.1  riastrad 			    AUX_SW_RX_RECV_NO_DET |	     \
     42  1.1  riastrad 			    AUX_SW_RX_RECV_INVALID_H |	     \
     43  1.1  riastrad 			    AUX_SW_RX_RECV_INVALID_V)
     44  1.1  riastrad 
     45  1.1  riastrad #define AUX_SW_REPLY_GET_BYTE_COUNT(x) (((x) >> 24) & 0x1f)
     46  1.1  riastrad 
     47  1.1  riastrad #define BARE_ADDRESS_SIZE 3
     48  1.1  riastrad 
     49  1.1  riastrad static const u32 aux_offset[] =
     50  1.1  riastrad {
     51  1.1  riastrad 	0x6200 - 0x6200,
     52  1.1  riastrad 	0x6250 - 0x6200,
     53  1.1  riastrad 	0x62a0 - 0x6200,
     54  1.1  riastrad 	0x6300 - 0x6200,
     55  1.1  riastrad 	0x6350 - 0x6200,
     56  1.1  riastrad 	0x63a0 - 0x6200,
     57  1.1  riastrad };
     58  1.1  riastrad 
     59  1.1  riastrad ssize_t
     60  1.1  riastrad radeon_dp_aux_transfer_native(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
     61  1.1  riastrad {
     62  1.1  riastrad 	struct radeon_i2c_chan *chan =
     63  1.1  riastrad 		container_of(aux, struct radeon_i2c_chan, aux);
     64  1.1  riastrad 	struct drm_device *dev = chan->dev;
     65  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     66  1.1  riastrad 	int ret = 0, i;
     67  1.1  riastrad 	uint32_t tmp, ack = 0;
     68  1.1  riastrad 	int instance = chan->rec.i2c_id & 0xf;
     69  1.1  riastrad 	u8 byte;
     70  1.1  riastrad 	u8 *buf = msg->buffer;
     71  1.1  riastrad 	int retry_count = 0;
     72  1.1  riastrad 	int bytes;
     73  1.1  riastrad 	int msize;
     74  1.1  riastrad 	bool is_write = false;
     75  1.1  riastrad 
     76  1.1  riastrad 	if (WARN_ON(msg->size > 16))
     77  1.1  riastrad 		return -E2BIG;
     78  1.1  riastrad 
     79  1.1  riastrad 	switch (msg->request & ~DP_AUX_I2C_MOT) {
     80  1.1  riastrad 	case DP_AUX_NATIVE_WRITE:
     81  1.1  riastrad 	case DP_AUX_I2C_WRITE:
     82  1.1  riastrad 		is_write = true;
     83  1.1  riastrad 		break;
     84  1.1  riastrad 	case DP_AUX_NATIVE_READ:
     85  1.1  riastrad 	case DP_AUX_I2C_READ:
     86  1.1  riastrad 		break;
     87  1.1  riastrad 	default:
     88  1.1  riastrad 		return -EINVAL;
     89  1.1  riastrad 	}
     90  1.1  riastrad 
     91  1.1  riastrad 	/* work out two sizes required */
     92  1.1  riastrad 	msize = 0;
     93  1.1  riastrad 	bytes = BARE_ADDRESS_SIZE;
     94  1.1  riastrad 	if (msg->size) {
     95  1.1  riastrad 		msize = msg->size - 1;
     96  1.1  riastrad 		bytes++;
     97  1.1  riastrad 		if (is_write)
     98  1.1  riastrad 			bytes += msg->size;
     99  1.1  riastrad 	}
    100  1.1  riastrad 
    101  1.1  riastrad 	mutex_lock(&chan->mutex);
    102  1.1  riastrad 
    103  1.1  riastrad 	/* switch the pad to aux mode */
    104  1.1  riastrad 	tmp = RREG32(chan->rec.mask_clk_reg);
    105  1.1  riastrad 	tmp |= (1 << 16);
    106  1.1  riastrad 	WREG32(chan->rec.mask_clk_reg, tmp);
    107  1.1  riastrad 
    108  1.1  riastrad 	/* setup AUX control register with correct HPD pin */
    109  1.1  riastrad 	tmp = RREG32(AUX_CONTROL + aux_offset[instance]);
    110  1.1  riastrad 
    111  1.1  riastrad 	tmp &= AUX_HPD_SEL(0x7);
    112  1.1  riastrad 	tmp |= AUX_HPD_SEL(chan->rec.hpd);
    113  1.1  riastrad 	tmp |= AUX_EN | AUX_LS_READ_EN;
    114  1.1  riastrad 
    115  1.1  riastrad 	WREG32(AUX_CONTROL + aux_offset[instance], tmp);
    116  1.1  riastrad 
    117  1.1  riastrad 	/* atombios appears to write this twice lets copy it */
    118  1.1  riastrad 	WREG32(AUX_SW_CONTROL + aux_offset[instance],
    119  1.1  riastrad 	       AUX_SW_WR_BYTES(bytes));
    120  1.1  riastrad 	WREG32(AUX_SW_CONTROL + aux_offset[instance],
    121  1.1  riastrad 	       AUX_SW_WR_BYTES(bytes));
    122  1.1  riastrad 
    123  1.1  riastrad 	/* write the data header into the registers */
    124  1.1  riastrad 	/* request, address, msg size */
    125  1.1  riastrad 	byte = (msg->request << 4) | ((msg->address >> 16) & 0xf);
    126  1.1  riastrad 	WREG32(AUX_SW_DATA + aux_offset[instance],
    127  1.1  riastrad 	       AUX_SW_DATA_MASK(byte) | AUX_SW_AUTOINCREMENT_DISABLE);
    128  1.1  riastrad 
    129  1.1  riastrad 	byte = (msg->address >> 8) & 0xff;
    130  1.1  riastrad 	WREG32(AUX_SW_DATA + aux_offset[instance],
    131  1.1  riastrad 	       AUX_SW_DATA_MASK(byte));
    132  1.1  riastrad 
    133  1.1  riastrad 	byte = msg->address & 0xff;
    134  1.1  riastrad 	WREG32(AUX_SW_DATA + aux_offset[instance],
    135  1.1  riastrad 	       AUX_SW_DATA_MASK(byte));
    136  1.1  riastrad 
    137  1.1  riastrad 	byte = msize;
    138  1.1  riastrad 	WREG32(AUX_SW_DATA + aux_offset[instance],
    139  1.1  riastrad 	       AUX_SW_DATA_MASK(byte));
    140  1.1  riastrad 
    141  1.1  riastrad 	/* if we are writing - write the msg buffer */
    142  1.1  riastrad 	if (is_write) {
    143  1.1  riastrad 		for (i = 0; i < msg->size; i++) {
    144  1.1  riastrad 			WREG32(AUX_SW_DATA + aux_offset[instance],
    145  1.1  riastrad 			       AUX_SW_DATA_MASK(buf[i]));
    146  1.1  riastrad 		}
    147  1.1  riastrad 	}
    148  1.1  riastrad 
    149  1.1  riastrad 	/* clear the ACK */
    150  1.1  riastrad 	WREG32(AUX_SW_INTERRUPT_CONTROL + aux_offset[instance], AUX_SW_DONE_ACK);
    151  1.1  riastrad 
    152  1.1  riastrad 	/* write the size and GO bits */
    153  1.1  riastrad 	WREG32(AUX_SW_CONTROL + aux_offset[instance],
    154  1.1  riastrad 	       AUX_SW_WR_BYTES(bytes) | AUX_SW_GO);
    155  1.1  riastrad 
    156  1.1  riastrad 	/* poll the status registers - TODO irq support */
    157  1.1  riastrad 	do {
    158  1.1  riastrad 		tmp = RREG32(AUX_SW_STATUS + aux_offset[instance]);
    159  1.1  riastrad 		if (tmp & AUX_SW_DONE) {
    160  1.1  riastrad 			break;
    161  1.1  riastrad 		}
    162  1.1  riastrad 		usleep_range(100, 200);
    163  1.1  riastrad 	} while (retry_count++ < 1000);
    164  1.1  riastrad 
    165  1.1  riastrad 	if (retry_count >= 1000) {
    166  1.1  riastrad 		DRM_ERROR("auxch hw never signalled completion, error %08x\n", tmp);
    167  1.1  riastrad 		ret = -EIO;
    168  1.1  riastrad 		goto done;
    169  1.1  riastrad 	}
    170  1.1  riastrad 
    171  1.1  riastrad 	if (tmp & AUX_SW_RX_TIMEOUT) {
    172  1.1  riastrad 		ret = -ETIMEDOUT;
    173  1.1  riastrad 		goto done;
    174  1.1  riastrad 	}
    175  1.1  riastrad 	if (tmp & AUX_RX_ERROR_FLAGS) {
    176  1.3  riastrad 		DRM_DEBUG_KMS_RATELIMITED("dp_aux_ch flags not zero: %08x\n",
    177  1.3  riastrad 					  tmp);
    178  1.1  riastrad 		ret = -EIO;
    179  1.1  riastrad 		goto done;
    180  1.1  riastrad 	}
    181  1.1  riastrad 
    182  1.1  riastrad 	bytes = AUX_SW_REPLY_GET_BYTE_COUNT(tmp);
    183  1.1  riastrad 	if (bytes) {
    184  1.1  riastrad 		WREG32(AUX_SW_DATA + aux_offset[instance],
    185  1.1  riastrad 		       AUX_SW_DATA_RW | AUX_SW_AUTOINCREMENT_DISABLE);
    186  1.1  riastrad 
    187  1.1  riastrad 		tmp = RREG32(AUX_SW_DATA + aux_offset[instance]);
    188  1.1  riastrad 		ack = (tmp >> 8) & 0xff;
    189  1.1  riastrad 
    190  1.1  riastrad 		for (i = 0; i < bytes - 1; i++) {
    191  1.1  riastrad 			tmp = RREG32(AUX_SW_DATA + aux_offset[instance]);
    192  1.1  riastrad 			if (buf)
    193  1.1  riastrad 				buf[i] = (tmp >> 8) & 0xff;
    194  1.1  riastrad 		}
    195  1.1  riastrad 		if (buf)
    196  1.1  riastrad 			ret = bytes - 1;
    197  1.1  riastrad 	}
    198  1.1  riastrad 
    199  1.1  riastrad 	WREG32(AUX_SW_INTERRUPT_CONTROL + aux_offset[instance], AUX_SW_DONE_ACK);
    200  1.1  riastrad 
    201  1.1  riastrad 	if (is_write)
    202  1.1  riastrad 		ret = msg->size;
    203  1.1  riastrad done:
    204  1.1  riastrad 	mutex_unlock(&chan->mutex);
    205  1.1  riastrad 
    206  1.1  riastrad 	if (ret >= 0)
    207  1.1  riastrad 		msg->reply = ack >> 4;
    208  1.1  riastrad 	return ret;
    209  1.1  riastrad }
    210