radeon_dp_auxch.c 5.4 KB

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