i2c-designware-core.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Synopsys DesignWare I2C adapter driver.
  4. *
  5. * Based on the TI DAVINCI I2C adapter driver.
  6. *
  7. * Copyright (C) 2006 Texas Instruments.
  8. * Copyright (C) 2007 MontaVista Software Inc.
  9. * Copyright (C) 2009 Provigent Ltd.
  10. */
  11. #include <linux/i2c.h>
  12. #define DW_IC_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
  13. I2C_FUNC_SMBUS_BYTE | \
  14. I2C_FUNC_SMBUS_BYTE_DATA | \
  15. I2C_FUNC_SMBUS_WORD_DATA | \
  16. I2C_FUNC_SMBUS_BLOCK_DATA | \
  17. I2C_FUNC_SMBUS_I2C_BLOCK)
  18. #define DW_IC_CON_MASTER 0x1
  19. #define DW_IC_CON_SPEED_STD 0x2
  20. #define DW_IC_CON_SPEED_FAST 0x4
  21. #define DW_IC_CON_SPEED_HIGH 0x6
  22. #define DW_IC_CON_SPEED_MASK 0x6
  23. #define DW_IC_CON_10BITADDR_SLAVE 0x8
  24. #define DW_IC_CON_10BITADDR_MASTER 0x10
  25. #define DW_IC_CON_RESTART_EN 0x20
  26. #define DW_IC_CON_SLAVE_DISABLE 0x40
  27. #define DW_IC_CON_STOP_DET_IFADDRESSED 0x80
  28. #define DW_IC_CON_TX_EMPTY_CTRL 0x100
  29. #define DW_IC_CON_RX_FIFO_FULL_HLD_CTRL 0x200
  30. /*
  31. * Registers offset
  32. */
  33. #define DW_IC_CON 0x0
  34. #define DW_IC_TAR 0x4
  35. #define DW_IC_SAR 0x8
  36. #define DW_IC_DATA_CMD 0x10
  37. #define DW_IC_SS_SCL_HCNT 0x14
  38. #define DW_IC_SS_SCL_LCNT 0x18
  39. #define DW_IC_FS_SCL_HCNT 0x1c
  40. #define DW_IC_FS_SCL_LCNT 0x20
  41. #define DW_IC_HS_SCL_HCNT 0x24
  42. #define DW_IC_HS_SCL_LCNT 0x28
  43. #define DW_IC_INTR_STAT 0x2c
  44. #define DW_IC_INTR_MASK 0x30
  45. #define DW_IC_RAW_INTR_STAT 0x34
  46. #define DW_IC_RX_TL 0x38
  47. #define DW_IC_TX_TL 0x3c
  48. #define DW_IC_CLR_INTR 0x40
  49. #define DW_IC_CLR_RX_UNDER 0x44
  50. #define DW_IC_CLR_RX_OVER 0x48
  51. #define DW_IC_CLR_TX_OVER 0x4c
  52. #define DW_IC_CLR_RD_REQ 0x50
  53. #define DW_IC_CLR_TX_ABRT 0x54
  54. #define DW_IC_CLR_RX_DONE 0x58
  55. #define DW_IC_CLR_ACTIVITY 0x5c
  56. #define DW_IC_CLR_STOP_DET 0x60
  57. #define DW_IC_CLR_START_DET 0x64
  58. #define DW_IC_CLR_GEN_CALL 0x68
  59. #define DW_IC_ENABLE 0x6c
  60. #define DW_IC_STATUS 0x70
  61. #define DW_IC_TXFLR 0x74
  62. #define DW_IC_RXFLR 0x78
  63. #define DW_IC_SDA_HOLD 0x7c
  64. #define DW_IC_TX_ABRT_SOURCE 0x80
  65. #define DW_IC_ENABLE_STATUS 0x9c
  66. #define DW_IC_CLR_RESTART_DET 0xa8
  67. #define DW_IC_COMP_PARAM_1 0xf4
  68. #define DW_IC_COMP_VERSION 0xf8
  69. #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
  70. #define DW_IC_COMP_TYPE 0xfc
  71. #define DW_IC_COMP_TYPE_VALUE 0x44570140
  72. #define DW_IC_INTR_RX_UNDER 0x001
  73. #define DW_IC_INTR_RX_OVER 0x002
  74. #define DW_IC_INTR_RX_FULL 0x004
  75. #define DW_IC_INTR_TX_OVER 0x008
  76. #define DW_IC_INTR_TX_EMPTY 0x010
  77. #define DW_IC_INTR_RD_REQ 0x020
  78. #define DW_IC_INTR_TX_ABRT 0x040
  79. #define DW_IC_INTR_RX_DONE 0x080
  80. #define DW_IC_INTR_ACTIVITY 0x100
  81. #define DW_IC_INTR_STOP_DET 0x200
  82. #define DW_IC_INTR_START_DET 0x400
  83. #define DW_IC_INTR_GEN_CALL 0x800
  84. #define DW_IC_INTR_RESTART_DET 0x1000
  85. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  86. DW_IC_INTR_TX_ABRT | \
  87. DW_IC_INTR_STOP_DET)
  88. #define DW_IC_INTR_MASTER_MASK (DW_IC_INTR_DEFAULT_MASK | \
  89. DW_IC_INTR_TX_EMPTY)
  90. #define DW_IC_INTR_SLAVE_MASK (DW_IC_INTR_DEFAULT_MASK | \
  91. DW_IC_INTR_RX_DONE | \
  92. DW_IC_INTR_RX_UNDER | \
  93. DW_IC_INTR_RD_REQ)
  94. #define DW_IC_STATUS_ACTIVITY 0x1
  95. #define DW_IC_STATUS_TFE BIT(2)
  96. #define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)
  97. #define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)
  98. #define DW_IC_SDA_HOLD_RX_SHIFT 16
  99. #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT)
  100. #define DW_IC_ERR_TX_ABRT 0x1
  101. #define DW_IC_TAR_10BITADDR_MASTER BIT(12)
  102. #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
  103. #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
  104. /*
  105. * status codes
  106. */
  107. #define STATUS_IDLE 0x0
  108. #define STATUS_WRITE_IN_PROGRESS 0x1
  109. #define STATUS_READ_IN_PROGRESS 0x2
  110. #define TIMEOUT 20 /* ms */
  111. /*
  112. * operation modes
  113. */
  114. #define DW_IC_MASTER 0
  115. #define DW_IC_SLAVE 1
  116. /*
  117. * Hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  118. *
  119. * Only expected abort codes are listed here
  120. * refer to the datasheet for the full list
  121. */
  122. #define ABRT_7B_ADDR_NOACK 0
  123. #define ABRT_10ADDR1_NOACK 1
  124. #define ABRT_10ADDR2_NOACK 2
  125. #define ABRT_TXDATA_NOACK 3
  126. #define ABRT_GCALL_NOACK 4
  127. #define ABRT_GCALL_READ 5
  128. #define ABRT_SBYTE_ACKDET 7
  129. #define ABRT_SBYTE_NORSTRT 9
  130. #define ABRT_10B_RD_NORSTRT 10
  131. #define ABRT_MASTER_DIS 11
  132. #define ARB_LOST 12
  133. #define ABRT_SLAVE_FLUSH_TXFIFO 13
  134. #define ABRT_SLAVE_ARBLOST 14
  135. #define ABRT_SLAVE_RD_INTX 15
  136. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  137. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  138. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  139. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  140. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  141. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  142. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  143. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  144. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  145. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  146. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  147. #define DW_IC_RX_ABRT_SLAVE_RD_INTX (1UL << ABRT_SLAVE_RD_INTX)
  148. #define DW_IC_RX_ABRT_SLAVE_ARBLOST (1UL << ABRT_SLAVE_ARBLOST)
  149. #define DW_IC_RX_ABRT_SLAVE_FLUSH_TXFIFO (1UL << ABRT_SLAVE_FLUSH_TXFIFO)
  150. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  151. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  152. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  153. DW_IC_TX_ABRT_TXDATA_NOACK | \
  154. DW_IC_TX_ABRT_GCALL_NOACK)
  155. /**
  156. * struct dw_i2c_dev - private i2c-designware data
  157. * @dev: driver model device node
  158. * @base: IO registers pointer
  159. * @cmd_complete: tx completion indicator
  160. * @clk: input reference clock
  161. * @slave: represent an I2C slave device
  162. * @cmd_err: run time hadware error code
  163. * @msgs: points to an array of messages currently being transferred
  164. * @msgs_num: the number of elements in msgs
  165. * @msg_write_idx: the element index of the current tx message in the msgs
  166. * array
  167. * @tx_buf_len: the length of the current tx buffer
  168. * @tx_buf: the current tx buffer
  169. * @msg_read_idx: the element index of the current rx message in the msgs
  170. * array
  171. * @rx_buf_len: the length of the current rx buffer
  172. * @rx_buf: the current rx buffer
  173. * @msg_err: error status of the current transfer
  174. * @status: i2c master status, one of STATUS_*
  175. * @abort_source: copy of the TX_ABRT_SOURCE register
  176. * @irq: interrupt number for the i2c master
  177. * @adapter: i2c subsystem adapter node
  178. * @slave_cfg: configuration for the slave device
  179. * @tx_fifo_depth: depth of the hardware tx fifo
  180. * @rx_fifo_depth: depth of the hardware rx fifo
  181. * @rx_outstanding: current master-rx elements in tx fifo
  182. * @timings: bus clock frequency, SDA hold and other timings
  183. * @sda_hold_time: SDA hold value
  184. * @ss_hcnt: standard speed HCNT value
  185. * @ss_lcnt: standard speed LCNT value
  186. * @fs_hcnt: fast speed HCNT value
  187. * @fs_lcnt: fast speed LCNT value
  188. * @fp_hcnt: fast plus HCNT value
  189. * @fp_lcnt: fast plus LCNT value
  190. * @hs_hcnt: high speed HCNT value
  191. * @hs_lcnt: high speed LCNT value
  192. * @acquire_lock: function to acquire a hardware lock on the bus
  193. * @release_lock: function to release a hardware lock on the bus
  194. * @shared_with_punit: true if this bus is shared with the SoCs PUNIT
  195. * @disable: function to disable the controller
  196. * @disable_int: function to disable all interrupts
  197. * @init: function to initialize the I2C hardware
  198. * @mode: operation mode - DW_IC_MASTER or DW_IC_SLAVE
  199. *
  200. * HCNT and LCNT parameters can be used if the platform knows more accurate
  201. * values than the one computed based only on the input clock frequency.
  202. * Leave them to be %0 if not used.
  203. */
  204. struct dw_i2c_dev {
  205. struct device *dev;
  206. void __iomem *base;
  207. void __iomem *ext;
  208. struct completion cmd_complete;
  209. struct clk *clk;
  210. struct reset_control *rst;
  211. struct i2c_client *slave;
  212. u32 (*get_clk_rate_khz) (struct dw_i2c_dev *dev);
  213. struct dw_pci_controller *controller;
  214. int cmd_err;
  215. struct i2c_msg *msgs;
  216. int msgs_num;
  217. int msg_write_idx;
  218. u32 tx_buf_len;
  219. u8 *tx_buf;
  220. int msg_read_idx;
  221. u32 rx_buf_len;
  222. u8 *rx_buf;
  223. int msg_err;
  224. unsigned int status;
  225. u32 abort_source;
  226. int irq;
  227. u32 flags;
  228. struct i2c_adapter adapter;
  229. u32 functionality;
  230. u32 master_cfg;
  231. u32 slave_cfg;
  232. unsigned int tx_fifo_depth;
  233. unsigned int rx_fifo_depth;
  234. int rx_outstanding;
  235. struct i2c_timings timings;
  236. u32 sda_hold_time;
  237. u16 ss_hcnt;
  238. u16 ss_lcnt;
  239. u16 fs_hcnt;
  240. u16 fs_lcnt;
  241. u16 fp_hcnt;
  242. u16 fp_lcnt;
  243. u16 hs_hcnt;
  244. u16 hs_lcnt;
  245. int (*acquire_lock)(void);
  246. void (*release_lock)(void);
  247. bool shared_with_punit;
  248. void (*disable)(struct dw_i2c_dev *dev);
  249. void (*disable_int)(struct dw_i2c_dev *dev);
  250. int (*init)(struct dw_i2c_dev *dev);
  251. int (*set_sda_hold_time)(struct dw_i2c_dev *dev);
  252. int mode;
  253. struct i2c_bus_recovery_info rinfo;
  254. };
  255. #define ACCESS_SWAP 0x00000001
  256. #define ACCESS_16BIT 0x00000002
  257. #define ACCESS_INTR_MASK 0x00000004
  258. #define ACCESS_NO_IRQ_SUSPEND 0x00000008
  259. #define MODEL_CHERRYTRAIL 0x00000100
  260. #define MODEL_MSCC_OCELOT 0x00000200
  261. #define MODEL_MASK 0x00000f00
  262. u32 dw_readl(struct dw_i2c_dev *dev, int offset);
  263. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset);
  264. int i2c_dw_set_reg_access(struct dw_i2c_dev *dev);
  265. u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset);
  266. u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset);
  267. int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev);
  268. unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev);
  269. int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare);
  270. int i2c_dw_acquire_lock(struct dw_i2c_dev *dev);
  271. void i2c_dw_release_lock(struct dw_i2c_dev *dev);
  272. int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev);
  273. int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev);
  274. u32 i2c_dw_func(struct i2c_adapter *adap);
  275. void i2c_dw_disable(struct dw_i2c_dev *dev);
  276. void i2c_dw_disable_int(struct dw_i2c_dev *dev);
  277. static inline void __i2c_dw_enable(struct dw_i2c_dev *dev)
  278. {
  279. dw_writel(dev, 1, DW_IC_ENABLE);
  280. }
  281. static inline void __i2c_dw_disable_nowait(struct dw_i2c_dev *dev)
  282. {
  283. dw_writel(dev, 0, DW_IC_ENABLE);
  284. }
  285. void __i2c_dw_disable(struct dw_i2c_dev *dev);
  286. extern u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev);
  287. extern int i2c_dw_probe(struct dw_i2c_dev *dev);
  288. #if IS_ENABLED(CONFIG_I2C_DESIGNWARE_SLAVE)
  289. extern int i2c_dw_probe_slave(struct dw_i2c_dev *dev);
  290. #else
  291. static inline int i2c_dw_probe_slave(struct dw_i2c_dev *dev) { return -EINVAL; }
  292. #endif
  293. #if IS_ENABLED(CONFIG_I2C_DESIGNWARE_BAYTRAIL)
  294. extern int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev);
  295. #else
  296. static inline int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev) { return 0; }
  297. #endif