i2c-dw.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef _I2C_DW_H
  2. #define _I2C_DW_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define DW_IC_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
  7. I2C_FUNC_SMBUS_BYTE | \
  8. I2C_FUNC_SMBUS_BYTE_DATA | \
  9. I2C_FUNC_SMBUS_WORD_DATA | \
  10. I2C_FUNC_SMBUS_BLOCK_DATA | \
  11. I2C_FUNC_SMBUS_I2C_BLOCK)
  12. #define DW_IC_CON_MASTER 0x1
  13. #define DW_IC_CON_SPEED_STD 0x2
  14. #define DW_IC_CON_SPEED_FAST 0x4
  15. #define DW_IC_CON_SPEED_HIGH 0x6
  16. #define DW_IC_CON_SPEED_MASK 0x6
  17. #define DW_IC_CON_10BITADDR_SLAVE 0x8
  18. #define DW_IC_CON_10BITADDR_MASTER 0x10
  19. #define DW_IC_CON_RESTART_EN 0x20
  20. #define DW_IC_CON_SLAVE_DISABLE 0x40
  21. #define DW_IC_CON_STOP_DET_IFADDRESSED 0x80
  22. #define DW_IC_CON_TX_EMPTY_CTRL 0x100
  23. #define DW_IC_CON_RX_FIFO_FULL_HLD_CTRL 0x200
  24. /*
  25. * Registers offset
  26. */
  27. #define DW_IC_CON 0x0
  28. #define DW_IC_TAR 0x4
  29. #define DW_IC_SAR 0x8
  30. #define DW_IC_DATA_CMD 0x10
  31. #define DW_IC_SS_SCL_HCNT 0x14
  32. #define DW_IC_SS_SCL_LCNT 0x18
  33. #define DW_IC_FS_SCL_HCNT 0x1c
  34. #define DW_IC_FS_SCL_LCNT 0x20
  35. #define DW_IC_HS_SCL_HCNT 0x24
  36. #define DW_IC_HS_SCL_LCNT 0x28
  37. #define DW_IC_INTR_STAT 0x2c
  38. #define DW_IC_INTR_MASK 0x30
  39. #define DW_IC_RAW_INTR_STAT 0x34
  40. #define DW_IC_RX_TL 0x38
  41. #define DW_IC_TX_TL 0x3c
  42. #define DW_IC_CLR_INTR 0x40
  43. #define DW_IC_CLR_RX_UNDER 0x44
  44. #define DW_IC_CLR_RX_OVER 0x48
  45. #define DW_IC_CLR_TX_OVER 0x4c
  46. #define DW_IC_CLR_RD_REQ 0x50
  47. #define DW_IC_CLR_TX_ABRT 0x54
  48. #define DW_IC_CLR_RX_DONE 0x58
  49. #define DW_IC_CLR_ACTIVITY 0x5c
  50. #define DW_IC_CLR_STOP_DET 0x60
  51. #define DW_IC_CLR_START_DET 0x64
  52. #define DW_IC_CLR_GEN_CALL 0x68
  53. #define DW_IC_ENABLE 0x6c
  54. #define DW_IC_STATUS 0x70
  55. #define DW_IC_TXFLR 0x74
  56. #define DW_IC_RXFLR 0x78
  57. #define DW_IC_SDA_HOLD 0x7c
  58. #define DW_IC_TX_ABRT_SOURCE 0x80
  59. #define DW_IC_ENABLE_STATUS 0x9c
  60. #define DW_IC_CLR_RESTART_DET 0xa8
  61. #define DW_IC_COMP_PARAM_1 0xf4
  62. #define DW_IC_COMP_VERSION 0xf8
  63. #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
  64. #define DW_IC_COMP_TYPE 0xfc
  65. #define DW_IC_COMP_TYPE_VALUE 0x44570140
  66. #define DW_IC_INTR_RX_UNDER 0x001
  67. #define DW_IC_INTR_RX_OVER 0x002
  68. #define DW_IC_INTR_RX_FULL 0x004
  69. #define DW_IC_INTR_TX_OVER 0x008
  70. #define DW_IC_INTR_TX_EMPTY 0x010
  71. #define DW_IC_INTR_RD_REQ 0x020
  72. #define DW_IC_INTR_TX_ABRT 0x040
  73. #define DW_IC_INTR_RX_DONE 0x080
  74. #define DW_IC_INTR_ACTIVITY 0x100
  75. #define DW_IC_INTR_STOP_DET 0x200
  76. #define DW_IC_INTR_START_DET 0x400
  77. #define DW_IC_INTR_GEN_CALL 0x800
  78. #define DW_IC_INTR_RESTART_DET 0x1000
  79. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  80. DW_IC_INTR_TX_ABRT | \
  81. DW_IC_INTR_STOP_DET)
  82. #define DW_IC_INTR_MASTER_MASK (DW_IC_INTR_DEFAULT_MASK | \
  83. DW_IC_INTR_TX_EMPTY)
  84. #define DW_IC_INTR_SLAVE_MASK (DW_IC_INTR_DEFAULT_MASK | \
  85. DW_IC_INTR_RX_DONE | \
  86. DW_IC_INTR_RX_UNDER | \
  87. DW_IC_INTR_RD_REQ)
  88. #define DW_IC_STATUS_ACTIVITY 0x1
  89. #define DW_IC_STATUS_TFE BIT(2)
  90. #define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)
  91. #define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)
  92. #define DW_IC_SDA_HOLD_RX_SHIFT 16
  93. #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT)
  94. #define DW_IC_ERR_TX_ABRT 0x1
  95. #define DW_IC_TAR_10BITADDR_MASTER BIT(12)
  96. #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
  97. #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
  98. /*
  99. * status codes
  100. */
  101. #define STATUS_IDLE 0x0
  102. #define STATUS_WRITE_IN_PROGRESS 0x1
  103. #define STATUS_READ_IN_PROGRESS 0x2
  104. #define TIMEOUT 20 /* ms */
  105. /*
  106. * operation modes
  107. */
  108. #define DW_IC_MASTER 0
  109. #define DW_IC_SLAVE 1
  110. /*
  111. * Hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  112. *
  113. * Only expected abort codes are listed here
  114. * refer to the datasheet for the full list
  115. */
  116. #define ABRT_7B_ADDR_NOACK 0
  117. #define ABRT_10ADDR1_NOACK 1
  118. #define ABRT_10ADDR2_NOACK 2
  119. #define ABRT_TXDATA_NOACK 3
  120. #define ABRT_GCALL_NOACK 4
  121. #define ABRT_GCALL_READ 5
  122. #define ABRT_SBYTE_ACKDET 7
  123. #define ABRT_SBYTE_NORSTRT 9
  124. #define ABRT_10B_RD_NORSTRT 10
  125. #define ABRT_MASTER_DIS 11
  126. #define ARB_LOST 12
  127. #define ABRT_SLAVE_FLUSH_TXFIFO 13
  128. #define ABRT_SLAVE_ARBLOST 14
  129. #define ABRT_SLAVE_RD_INTX 15
  130. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  131. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  132. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  133. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  134. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  135. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  136. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  137. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  138. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  139. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  140. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  141. #define DW_IC_RX_ABRT_SLAVE_RD_INTX (1UL << ABRT_SLAVE_RD_INTX)
  142. #define DW_IC_RX_ABRT_SLAVE_ARBLOST (1UL << ABRT_SLAVE_ARBLOST)
  143. #define DW_IC_RX_ABRT_SLAVE_FLUSH_TXFIFO (1UL << ABRT_SLAVE_FLUSH_TXFIFO)
  144. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  145. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  146. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  147. DW_IC_TX_ABRT_TXDATA_NOACK | \
  148. DW_IC_TX_ABRT_GCALL_NOACK)
  149. /**
  150. * struct dw_i2c_dev - private i2c-designware data
  151. * @dev: driver model device node
  152. * @base: IO registers pointer
  153. * @cmd_complete: tx completion indicator
  154. * @clk: input reference clock
  155. * @slave: represent an I2C slave device
  156. * @cmd_err: run time hadware error code
  157. * @msgs: points to an array of messages currently being transferred
  158. * @msgs_num: the number of elements in msgs
  159. * @msg_write_idx: the element index of the current tx message in the msgs
  160. * array
  161. * @tx_buf_len: the length of the current tx buffer
  162. * @tx_buf: the current tx buffer
  163. * @msg_read_idx: the element index of the current rx message in the msgs
  164. * array
  165. * @rx_buf_len: the length of the current rx buffer
  166. * @rx_buf: the current rx buffer
  167. * @msg_err: error status of the current transfer
  168. * @status: i2c master status, one of STATUS_*
  169. * @abort_source: copy of the TX_ABRT_SOURCE register
  170. * @irq: interrupt number for the i2c master
  171. * @adapter: i2c subsystem adapter node
  172. * @slave_cfg: configuration for the slave device
  173. * @tx_fifo_depth: depth of the hardware tx fifo
  174. * @rx_fifo_depth: depth of the hardware rx fifo
  175. * @rx_outstanding: current master-rx elements in tx fifo
  176. * @clk_freq: bus clock frequency
  177. * @ss_hcnt: standard speed HCNT value
  178. * @ss_lcnt: standard speed LCNT value
  179. * @fs_hcnt: fast speed HCNT value
  180. * @fs_lcnt: fast speed LCNT value
  181. * @fp_hcnt: fast plus HCNT value
  182. * @fp_lcnt: fast plus LCNT value
  183. * @hs_hcnt: high speed HCNT value
  184. * @hs_lcnt: high speed LCNT value
  185. * @pm_qos: pm_qos_request used while holding a hardware lock on the bus
  186. * @acquire_lock: function to acquire a hardware lock on the bus
  187. * @release_lock: function to release a hardware lock on the bus
  188. * @disable: function to disable the controller
  189. * @disable_int: function to disable all interrupts
  190. * @init: function to initialize the I2C hardware
  191. * @mode: operation mode - DW_IC_MASTER or DW_IC_SLAVE
  192. *
  193. * HCNT and LCNT parameters can be used if the platform knows more accurate
  194. * values than the one computed based only on the input clock frequency.
  195. * Leave them to be %0 if not used.
  196. */
  197. struct dw_i2c_dev {
  198. u32 base;
  199. int irq;
  200. QueueHandle_t cmd_complete;
  201. struct i2c_adapter *slave;
  202. u32 (*get_clk_rate_khz) (struct dw_i2c_dev *dev);
  203. int cmd_err;
  204. struct i2c_msg *msgs;
  205. int msgs_num;
  206. int msg_write_idx;
  207. u32 tx_buf_len;
  208. u8 *tx_buf;
  209. int msg_read_idx;
  210. u32 rx_buf_len;
  211. u8 *rx_buf;
  212. int msg_err;
  213. unsigned int status;
  214. u32 abort_source;
  215. u32 flags;
  216. struct i2c_adapter adapter;
  217. u32 functionality;
  218. u32 master_cfg;
  219. u32 slave_cfg;
  220. unsigned int tx_fifo_depth;
  221. unsigned int rx_fifo_depth;
  222. int rx_outstanding;
  223. u32 clk_freq;
  224. u32 sda_hold_time;
  225. u32 sda_falling_time;
  226. u32 scl_falling_time;
  227. u16 ss_hcnt;
  228. u16 ss_lcnt;
  229. u16 fs_hcnt;
  230. u16 fs_lcnt;
  231. u16 fp_hcnt;
  232. u16 fp_lcnt;
  233. u16 hs_hcnt;
  234. u16 hs_lcnt;
  235. int (*acquire_lock)(struct dw_i2c_dev *dev);
  236. void (*release_lock)(struct dw_i2c_dev *dev);
  237. void (*disable)(struct dw_i2c_dev *dev);
  238. void (*disable_int)(struct dw_i2c_dev *dev);
  239. int (*init)(struct dw_i2c_dev *dev);
  240. int mode;
  241. };
  242. #define ACCESS_SWAP 0x00000001
  243. #define ACCESS_16BIT 0x00000002
  244. #define ACCESS_INTR_MASK 0x00000004
  245. #define MODEL_CHERRYTRAIL 0x00000100
  246. int i2c_dw_init(int id);
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif