i2c-octeon-core.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <linux/atomic.h>
  2. #include <linux/clk.h>
  3. #include <linux/delay.h>
  4. #include <linux/device.h>
  5. #include <linux/i2c.h>
  6. #include <linux/i2c-smbus.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. /* Controller command patterns */
  11. #define SW_TWSI_V BIT_ULL(63) /* Valid bit */
  12. #define SW_TWSI_EIA BIT_ULL(61) /* Extended internal address */
  13. #define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
  14. #define SW_TWSI_SOVR BIT_ULL(55) /* Size override */
  15. #define SW_TWSI_SIZE_SHIFT 52
  16. #define SW_TWSI_ADDR_SHIFT 40
  17. #define SW_TWSI_IA_SHIFT 32 /* Internal address */
  18. /* Controller opcode word (bits 60:57) */
  19. #define SW_TWSI_OP_SHIFT 57
  20. #define SW_TWSI_OP_7 (0ULL << SW_TWSI_OP_SHIFT)
  21. #define SW_TWSI_OP_7_IA (1ULL << SW_TWSI_OP_SHIFT)
  22. #define SW_TWSI_OP_10 (2ULL << SW_TWSI_OP_SHIFT)
  23. #define SW_TWSI_OP_10_IA (3ULL << SW_TWSI_OP_SHIFT)
  24. #define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
  25. #define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
  26. /* Controller extended opcode word (bits 34:32) */
  27. #define SW_TWSI_EOP_SHIFT 32
  28. #define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
  29. #define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
  30. #define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
  31. #define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
  32. #define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
  33. /* Controller command and status bits */
  34. #define TWSI_CTL_CE 0x80 /* High level controller enable */
  35. #define TWSI_CTL_ENAB 0x40 /* Bus enable */
  36. #define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
  37. #define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
  38. #define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
  39. #define TWSI_CTL_AAK 0x04 /* Assert ACK */
  40. /* Status values */
  41. #define STAT_ERROR 0x00
  42. #define STAT_START 0x08
  43. #define STAT_REP_START 0x10
  44. #define STAT_TXADDR_ACK 0x18
  45. #define STAT_TXADDR_NAK 0x20
  46. #define STAT_TXDATA_ACK 0x28
  47. #define STAT_TXDATA_NAK 0x30
  48. #define STAT_LOST_ARB_38 0x38
  49. #define STAT_RXADDR_ACK 0x40
  50. #define STAT_RXADDR_NAK 0x48
  51. #define STAT_RXDATA_ACK 0x50
  52. #define STAT_RXDATA_NAK 0x58
  53. #define STAT_SLAVE_60 0x60
  54. #define STAT_LOST_ARB_68 0x68
  55. #define STAT_SLAVE_70 0x70
  56. #define STAT_LOST_ARB_78 0x78
  57. #define STAT_SLAVE_80 0x80
  58. #define STAT_SLAVE_88 0x88
  59. #define STAT_GENDATA_ACK 0x90
  60. #define STAT_GENDATA_NAK 0x98
  61. #define STAT_SLAVE_A0 0xA0
  62. #define STAT_SLAVE_A8 0xA8
  63. #define STAT_LOST_ARB_B0 0xB0
  64. #define STAT_SLAVE_LOST 0xB8
  65. #define STAT_SLAVE_NAK 0xC0
  66. #define STAT_SLAVE_ACK 0xC8
  67. #define STAT_AD2W_ACK 0xD0
  68. #define STAT_AD2W_NAK 0xD8
  69. #define STAT_IDLE 0xF8
  70. /* TWSI_INT values */
  71. #define TWSI_INT_ST_INT BIT_ULL(0)
  72. #define TWSI_INT_TS_INT BIT_ULL(1)
  73. #define TWSI_INT_CORE_INT BIT_ULL(2)
  74. #define TWSI_INT_ST_EN BIT_ULL(4)
  75. #define TWSI_INT_TS_EN BIT_ULL(5)
  76. #define TWSI_INT_CORE_EN BIT_ULL(6)
  77. #define TWSI_INT_SDA_OVR BIT_ULL(8)
  78. #define TWSI_INT_SCL_OVR BIT_ULL(9)
  79. #define TWSI_INT_SDA BIT_ULL(10)
  80. #define TWSI_INT_SCL BIT_ULL(11)
  81. #define I2C_OCTEON_EVENT_WAIT 80 /* microseconds */
  82. /* Register offsets */
  83. struct octeon_i2c_reg_offset {
  84. unsigned int sw_twsi;
  85. unsigned int twsi_int;
  86. unsigned int sw_twsi_ext;
  87. };
  88. #define SW_TWSI(x) (x->roff.sw_twsi)
  89. #define TWSI_INT(x) (x->roff.twsi_int)
  90. #define SW_TWSI_EXT(x) (x->roff.sw_twsi_ext)
  91. struct octeon_i2c {
  92. wait_queue_head_t queue;
  93. struct i2c_adapter adap;
  94. struct octeon_i2c_reg_offset roff;
  95. struct clk *clk;
  96. int irq;
  97. int hlc_irq; /* For cn7890 only */
  98. u32 twsi_freq;
  99. int sys_freq;
  100. void __iomem *twsi_base;
  101. struct device *dev;
  102. bool hlc_enabled;
  103. bool broken_irq_mode;
  104. bool broken_irq_check;
  105. void (*int_enable)(struct octeon_i2c *);
  106. void (*int_disable)(struct octeon_i2c *);
  107. void (*hlc_int_enable)(struct octeon_i2c *);
  108. void (*hlc_int_disable)(struct octeon_i2c *);
  109. atomic_t int_enable_cnt;
  110. atomic_t hlc_int_enable_cnt;
  111. #if IS_ENABLED(CONFIG_I2C_THUNDERX)
  112. struct msix_entry i2c_msix;
  113. #endif
  114. struct i2c_smbus_alert_setup alert_data;
  115. struct i2c_client *ara;
  116. };
  117. static inline void octeon_i2c_writeq_flush(u64 val, void __iomem *addr)
  118. {
  119. __raw_writeq(val, addr);
  120. __raw_readq(addr); /* wait for write to land */
  121. }
  122. /**
  123. * octeon_i2c_reg_write - write an I2C core register
  124. * @i2c: The struct octeon_i2c
  125. * @eop_reg: Register selector
  126. * @data: Value to be written
  127. *
  128. * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
  129. */
  130. static inline void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
  131. {
  132. u64 tmp;
  133. __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI(i2c));
  134. do {
  135. tmp = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  136. } while ((tmp & SW_TWSI_V) != 0);
  137. }
  138. #define octeon_i2c_ctl_write(i2c, val) \
  139. octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
  140. #define octeon_i2c_data_write(i2c, val) \
  141. octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
  142. /**
  143. * octeon_i2c_reg_read - read lower bits of an I2C core register
  144. * @i2c: The struct octeon_i2c
  145. * @eop_reg: Register selector
  146. *
  147. * Returns the data.
  148. *
  149. * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
  150. */
  151. static inline u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
  152. {
  153. u64 tmp;
  154. __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI(i2c));
  155. do {
  156. tmp = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  157. } while ((tmp & SW_TWSI_V) != 0);
  158. return tmp & 0xFF;
  159. }
  160. #define octeon_i2c_ctl_read(i2c) \
  161. octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
  162. #define octeon_i2c_data_read(i2c) \
  163. octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
  164. #define octeon_i2c_stat_read(i2c) \
  165. octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
  166. /**
  167. * octeon_i2c_read_int - read the TWSI_INT register
  168. * @i2c: The struct octeon_i2c
  169. *
  170. * Returns the value of the register.
  171. */
  172. static inline u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
  173. {
  174. return __raw_readq(i2c->twsi_base + TWSI_INT(i2c));
  175. }
  176. /**
  177. * octeon_i2c_write_int - write the TWSI_INT register
  178. * @i2c: The struct octeon_i2c
  179. * @data: Value to be written
  180. */
  181. static inline void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
  182. {
  183. octeon_i2c_writeq_flush(data, i2c->twsi_base + TWSI_INT(i2c));
  184. }
  185. /* Prototypes */
  186. irqreturn_t octeon_i2c_isr(int irq, void *dev_id);
  187. int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
  188. int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c);
  189. void octeon_i2c_set_clock(struct octeon_i2c *i2c);
  190. extern struct i2c_bus_recovery_info octeon_i2c_recovery_info;