13253c27bSkettenis /*
23253c27bSkettenis * Copyright 2015 Red Hat Inc.
33253c27bSkettenis *
43253c27bSkettenis * Permission is hereby granted, free of charge, to any person obtaining a
53253c27bSkettenis * copy of this software and associated documentation files (the "Software"),
63253c27bSkettenis * to deal in the Software without restriction, including without limitation
73253c27bSkettenis * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83253c27bSkettenis * and/or sell copies of the Software, and to permit persons to whom the
93253c27bSkettenis * Software is furnished to do so, subject to the following conditions:
103253c27bSkettenis *
113253c27bSkettenis * The above copyright notice and this permission notice shall be included in
123253c27bSkettenis * all copies or substantial portions of the Software.
133253c27bSkettenis *
143253c27bSkettenis * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153253c27bSkettenis * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163253c27bSkettenis * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
173253c27bSkettenis * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
183253c27bSkettenis * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193253c27bSkettenis * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203253c27bSkettenis * OTHER DEALINGS IN THE SOFTWARE.
213253c27bSkettenis *
223253c27bSkettenis * Authors: Dave Airlie
233253c27bSkettenis */
24c349dbc7Sjsg
257f4dd379Sjsg #include <drm/radeon_drm.h>
263253c27bSkettenis #include "radeon.h"
273253c27bSkettenis #include "nid.h"
283253c27bSkettenis
293253c27bSkettenis #define AUX_RX_ERROR_FLAGS (AUX_SW_RX_OVERFLOW | \
303253c27bSkettenis AUX_SW_RX_HPD_DISCON | \
313253c27bSkettenis AUX_SW_RX_PARTIAL_BYTE | \
323253c27bSkettenis AUX_SW_NON_AUX_MODE | \
333253c27bSkettenis AUX_SW_RX_SYNC_INVALID_L | \
343253c27bSkettenis AUX_SW_RX_SYNC_INVALID_H | \
353253c27bSkettenis AUX_SW_RX_INVALID_START | \
363253c27bSkettenis AUX_SW_RX_RECV_NO_DET | \
373253c27bSkettenis AUX_SW_RX_RECV_INVALID_H | \
383253c27bSkettenis AUX_SW_RX_RECV_INVALID_V)
393253c27bSkettenis
403253c27bSkettenis #define AUX_SW_REPLY_GET_BYTE_COUNT(x) (((x) >> 24) & 0x1f)
413253c27bSkettenis
423253c27bSkettenis #define BARE_ADDRESS_SIZE 3
433253c27bSkettenis
443253c27bSkettenis static const u32 aux_offset[] =
453253c27bSkettenis {
463253c27bSkettenis 0x6200 - 0x6200,
473253c27bSkettenis 0x6250 - 0x6200,
483253c27bSkettenis 0x62a0 - 0x6200,
493253c27bSkettenis 0x6300 - 0x6200,
503253c27bSkettenis 0x6350 - 0x6200,
513253c27bSkettenis 0x63a0 - 0x6200,
523253c27bSkettenis };
533253c27bSkettenis
543253c27bSkettenis ssize_t
radeon_dp_aux_transfer_native(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)553253c27bSkettenis radeon_dp_aux_transfer_native(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
563253c27bSkettenis {
573253c27bSkettenis struct radeon_i2c_chan *chan =
583253c27bSkettenis container_of(aux, struct radeon_i2c_chan, aux);
593253c27bSkettenis struct drm_device *dev = chan->dev;
603253c27bSkettenis struct radeon_device *rdev = dev->dev_private;
613253c27bSkettenis int ret = 0, i;
623253c27bSkettenis uint32_t tmp, ack = 0;
633253c27bSkettenis int instance = chan->rec.i2c_id & 0xf;
643253c27bSkettenis u8 byte;
653253c27bSkettenis u8 *buf = msg->buffer;
663253c27bSkettenis int retry_count = 0;
673253c27bSkettenis int bytes;
683253c27bSkettenis int msize;
693253c27bSkettenis bool is_write = false;
703253c27bSkettenis
713253c27bSkettenis if (WARN_ON(msg->size > 16))
723253c27bSkettenis return -E2BIG;
733253c27bSkettenis
743253c27bSkettenis switch (msg->request & ~DP_AUX_I2C_MOT) {
753253c27bSkettenis case DP_AUX_NATIVE_WRITE:
763253c27bSkettenis case DP_AUX_I2C_WRITE:
773253c27bSkettenis is_write = true;
783253c27bSkettenis break;
793253c27bSkettenis case DP_AUX_NATIVE_READ:
803253c27bSkettenis case DP_AUX_I2C_READ:
813253c27bSkettenis break;
823253c27bSkettenis default:
833253c27bSkettenis return -EINVAL;
843253c27bSkettenis }
853253c27bSkettenis
863253c27bSkettenis /* work out two sizes required */
873253c27bSkettenis msize = 0;
883253c27bSkettenis bytes = BARE_ADDRESS_SIZE;
893253c27bSkettenis if (msg->size) {
903253c27bSkettenis msize = msg->size - 1;
913253c27bSkettenis bytes++;
923253c27bSkettenis if (is_write)
933253c27bSkettenis bytes += msg->size;
943253c27bSkettenis }
953253c27bSkettenis
963253c27bSkettenis mutex_lock(&chan->mutex);
973253c27bSkettenis
983253c27bSkettenis /* switch the pad to aux mode */
993253c27bSkettenis tmp = RREG32(chan->rec.mask_clk_reg);
1003253c27bSkettenis tmp |= (1 << 16);
1013253c27bSkettenis WREG32(chan->rec.mask_clk_reg, tmp);
1023253c27bSkettenis
1033253c27bSkettenis /* setup AUX control register with correct HPD pin */
1043253c27bSkettenis tmp = RREG32(AUX_CONTROL + aux_offset[instance]);
1053253c27bSkettenis
1063253c27bSkettenis tmp &= AUX_HPD_SEL(0x7);
1073253c27bSkettenis tmp |= AUX_HPD_SEL(chan->rec.hpd);
1083253c27bSkettenis tmp |= AUX_EN | AUX_LS_READ_EN;
1093253c27bSkettenis
1103253c27bSkettenis WREG32(AUX_CONTROL + aux_offset[instance], tmp);
1113253c27bSkettenis
1123253c27bSkettenis /* atombios appears to write this twice lets copy it */
1133253c27bSkettenis WREG32(AUX_SW_CONTROL + aux_offset[instance],
1143253c27bSkettenis AUX_SW_WR_BYTES(bytes));
1153253c27bSkettenis WREG32(AUX_SW_CONTROL + aux_offset[instance],
1163253c27bSkettenis AUX_SW_WR_BYTES(bytes));
1173253c27bSkettenis
1183253c27bSkettenis /* write the data header into the registers */
1193253c27bSkettenis /* request, address, msg size */
1203253c27bSkettenis byte = (msg->request << 4) | ((msg->address >> 16) & 0xf);
1213253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1223253c27bSkettenis AUX_SW_DATA_MASK(byte) | AUX_SW_AUTOINCREMENT_DISABLE);
1233253c27bSkettenis
1243253c27bSkettenis byte = (msg->address >> 8) & 0xff;
1253253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1263253c27bSkettenis AUX_SW_DATA_MASK(byte));
1273253c27bSkettenis
1283253c27bSkettenis byte = msg->address & 0xff;
1293253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1303253c27bSkettenis AUX_SW_DATA_MASK(byte));
1313253c27bSkettenis
1323253c27bSkettenis byte = msize;
1333253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1343253c27bSkettenis AUX_SW_DATA_MASK(byte));
1353253c27bSkettenis
1363253c27bSkettenis /* if we are writing - write the msg buffer */
1373253c27bSkettenis if (is_write) {
1383253c27bSkettenis for (i = 0; i < msg->size; i++) {
1393253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1403253c27bSkettenis AUX_SW_DATA_MASK(buf[i]));
1413253c27bSkettenis }
1423253c27bSkettenis }
1433253c27bSkettenis
1443253c27bSkettenis /* clear the ACK */
1453253c27bSkettenis WREG32(AUX_SW_INTERRUPT_CONTROL + aux_offset[instance], AUX_SW_DONE_ACK);
1463253c27bSkettenis
1473253c27bSkettenis /* write the size and GO bits */
1483253c27bSkettenis WREG32(AUX_SW_CONTROL + aux_offset[instance],
1493253c27bSkettenis AUX_SW_WR_BYTES(bytes) | AUX_SW_GO);
1503253c27bSkettenis
1513253c27bSkettenis /* poll the status registers - TODO irq support */
1523253c27bSkettenis do {
1533253c27bSkettenis tmp = RREG32(AUX_SW_STATUS + aux_offset[instance]);
1543253c27bSkettenis if (tmp & AUX_SW_DONE) {
1553253c27bSkettenis break;
1563253c27bSkettenis }
1573253c27bSkettenis usleep_range(100, 200);
1583253c27bSkettenis } while (retry_count++ < 1000);
1593253c27bSkettenis
1603253c27bSkettenis if (retry_count >= 1000) {
161*f005ef32Sjsg dev_err(rdev->dev, "auxch hw never signalled completion, error %08x\n", tmp);
1623253c27bSkettenis ret = -EIO;
1633253c27bSkettenis goto done;
1643253c27bSkettenis }
1653253c27bSkettenis
1663253c27bSkettenis if (tmp & AUX_SW_RX_TIMEOUT) {
1673253c27bSkettenis ret = -ETIMEDOUT;
1683253c27bSkettenis goto done;
1693253c27bSkettenis }
1703253c27bSkettenis if (tmp & AUX_RX_ERROR_FLAGS) {
171*f005ef32Sjsg drm_dbg_kms_ratelimited(dev, "dp_aux_ch flags not zero: %08x\n", tmp);
1723253c27bSkettenis ret = -EIO;
1733253c27bSkettenis goto done;
1743253c27bSkettenis }
1753253c27bSkettenis
1763253c27bSkettenis bytes = AUX_SW_REPLY_GET_BYTE_COUNT(tmp);
1773253c27bSkettenis if (bytes) {
1783253c27bSkettenis WREG32(AUX_SW_DATA + aux_offset[instance],
1793253c27bSkettenis AUX_SW_DATA_RW | AUX_SW_AUTOINCREMENT_DISABLE);
1803253c27bSkettenis
1813253c27bSkettenis tmp = RREG32(AUX_SW_DATA + aux_offset[instance]);
1823253c27bSkettenis ack = (tmp >> 8) & 0xff;
1833253c27bSkettenis
1843253c27bSkettenis for (i = 0; i < bytes - 1; i++) {
1853253c27bSkettenis tmp = RREG32(AUX_SW_DATA + aux_offset[instance]);
1863253c27bSkettenis if (buf)
1873253c27bSkettenis buf[i] = (tmp >> 8) & 0xff;
1883253c27bSkettenis }
1893253c27bSkettenis if (buf)
1903253c27bSkettenis ret = bytes - 1;
1913253c27bSkettenis }
1923253c27bSkettenis
1933253c27bSkettenis WREG32(AUX_SW_INTERRUPT_CONTROL + aux_offset[instance], AUX_SW_DONE_ACK);
1943253c27bSkettenis
1953253c27bSkettenis if (is_write)
1963253c27bSkettenis ret = msg->size;
1973253c27bSkettenis done:
1983253c27bSkettenis mutex_unlock(&chan->mutex);
1993253c27bSkettenis
2003253c27bSkettenis if (ret >= 0)
2013253c27bSkettenis msg->reply = ack >> 4;
2023253c27bSkettenis return ret;
2033253c27bSkettenis }
204