i2c-octeon-core.h 7.6 KB

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