drv_hw_i2c.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. *2024-02-14 ShichengChu first version
  9. */
  10. #ifndef __DRV_HW_I2C_H__
  11. #define __DRV_HW_I2C_H__
  12. #include <rtthread.h>
  13. #include "rtdevice.h"
  14. #include <rthw.h>
  15. #include "pinctrl.h"
  16. #include "mmio.h"
  17. #define I2C0 0x0
  18. #define I2C1 0x1
  19. #define I2C2 0x2
  20. #define I2C3 0x3
  21. #define I2C4 0x4
  22. #define I2C0_BASE 0x4000000
  23. #define I2C1_BASE 0x4010000
  24. #define I2C2_BASE 0x4020000
  25. #define I2C3_BASE 0x4030000
  26. #define I2C4_BASE 0x4040000
  27. #define BIT_I2C_CMD_DATA_READ_BIT (0x01 << 8)
  28. #define BIT_I2C_CMD_DATA_STOP_BIT (0x01 << 9)
  29. /* bit definition */
  30. #define BIT_I2C_CON_MASTER_MODE (0x01 << 0)
  31. #define BIT_I2C_CON_STANDARD_SPEED (0x01 << 1)
  32. #define BIT_I2C_CON_FULL_SPEED (0x02 << 1)
  33. #define BIT_I2C_CON_HIGH_SPEED (0x03 << 1)
  34. #define BIT_I2C_CON_10B_ADDR_SLAVE (0x01 << 3)
  35. #define BIT_I2C_CON_10B_ADDR_MASTER (0x01 << 4)
  36. #define BIT_I2C_CON_RESTART_EN (0x01 << 5)
  37. #define BIT_I2C_CON_SLAVE_DIS (0x01 << 6)
  38. #define BIT_I2C_TAR_10B_ADDR_MASTER (0x01 << 12)
  39. #define BIT_I2C_INT_RX_UNDER (0x01 << 0)
  40. #define BIT_I2C_INT_RX_OVER (0x01 << 1)
  41. #define BIT_I2C_INT_RX_FULL (0x01 << 2)
  42. #define BIT_I2C_INT_TX_OVER (0x01 << 3)
  43. #define BIT_I2C_INT_TX_EMPTY (0x01 << 4)
  44. #define BIT_I2C_INT_RD_REQ (0x01 << 5)
  45. #define BIT_I2C_INT_TX_ABRT (0x01 << 6)
  46. #define BIT_I2C_INT_RX_DONE (0x01 << 7)
  47. #define BIT_I2C_INT_ACTIVITY (0x01 << 8)
  48. #define BIT_I2C_INT_STOP_DET (0x01 << 9)
  49. #define BIT_I2C_INT_START_DET (0x01 << 10)
  50. #define BIT_I2C_INT_GEN_ALL (0x01 << 11)
  51. #define I2C_INTR_MASTER_MASK (BIT_I2C_INT_TX_ABRT | \
  52. BIT_I2C_INT_STOP_DET | \
  53. BIT_I2C_INT_RX_FULL | \
  54. BIT_I2C_INT_TX_EMPTY)
  55. #define BIT_I2C_INT_RX_UNDER_MASK (0x01 << 0)
  56. #define BIT_I2C_INT_RX_OVER_MASK (0x01 << 1)
  57. #define BIT_I2C_INT_RX_FULL_MASK (0x01 << 2)
  58. #define BIT_I2C_INT_TX_OVER_MASK (0x01 << 3)
  59. #define BIT_I2C_INT_TX_EMPTY_MASK (0x01 << 4)
  60. #define BIT_I2C_INT_RD_REQ_MASK (0x01 << 5)
  61. #define BIT_I2C_INT_TX_ABRT_MASK (0x01 << 6)
  62. #define BIT_I2C_INT_RX_DONE_MASK (0x01 << 7)
  63. #define BIT_I2C_INT_ACTIVITY_MASK (0x01 << 8)
  64. #define BIT_I2C_INT_STOP_DET_MASK (0x01 << 9)
  65. #define BIT_I2C_INT_START_DET_MASK (0x01 << 10)
  66. #define BIT_I2C_INT_GEN_ALL_MASK (0x01 << 11)
  67. #define BIT_I2C_INT_RX_UNDER_RAW (0x01 << 0)
  68. #define BIT_I2C_INT_RX_OVER_RAW (0x01 << 1)
  69. #define BIT_I2C_INT_RX_FULL_RAW (0x01 << 2)
  70. #define BIT_I2C_INT_TX_OVER_RAW (0x01 << 3)
  71. #define BIT_I2C_INT_TX_EMPTY_RAW (0x01 << 4)
  72. #define BIT_I2C_INT_RD_REQ_RAW (0x01 << 5)
  73. #define BIT_I2C_INT_TX_ABRT_RAW (0x01 << 6)
  74. #define BIT_I2C_INT_RX_DONE_RAW (0x01 << 7)
  75. #define BIT_I2C_INT_ACTIVITY_RAW (0x01 << 8)
  76. #define BIT_I2C_INT_STOP_DET_RAW (0x01 << 9)
  77. #define BIT_I2C_INT_START_DET_RAW (0x01 << 10)
  78. #define BIT_I2C_INT_GEN_ALL_RAW (0x01 << 11)
  79. #define BIT_I2C_DMA_CR_TDMAE (0x01 << 1)
  80. #define BIT_I2C_DMA_CR_RDMAE (0x01 << 0)
  81. struct i2c_regs {
  82. volatile uint32_t ic_con; /* 0x00 */
  83. volatile uint32_t ic_tar; /* 0x04 */
  84. volatile uint32_t ic_sar; /* 0x08 */
  85. volatile uint32_t ic_hs_maddr; /* 0x0c */
  86. volatile uint32_t ic_cmd_data; /* 0x10 */
  87. volatile uint32_t ic_ss_scl_hcnt; /* 0x14 */
  88. volatile uint32_t ic_ss_scl_lcnt; /* 0x18 */
  89. volatile uint32_t ic_fs_scl_hcnt; /* 0x1c */
  90. volatile uint32_t ic_fs_scl_lcnt; /* 0x20 */
  91. volatile uint32_t ic_hs_scl_hcnt; /* 0x24 */
  92. volatile uint32_t ic_hs_scl_lcnt; /* 0x28 */
  93. volatile uint32_t ic_intr_stat; /* 0x2c */
  94. volatile uint32_t ic_intr_mask; /* 0x30 */
  95. volatile uint32_t ic_raw_intr_stat; /* 0x34 */
  96. volatile uint32_t ic_rx_tl; /* 0x38 */
  97. volatile uint32_t ic_tx_tl; /* 0x3c */
  98. volatile uint32_t ic_clr_intr; /* 0x40 */
  99. volatile uint32_t ic_clr_rx_under; /* 0x44 */
  100. volatile uint32_t ic_clr_rx_over; /* 0x48 */
  101. volatile uint32_t ic_clr_tx_over; /* 0x4c */
  102. volatile uint32_t ic_clr_rd_req; /* 0x50 */
  103. volatile uint32_t ic_clr_tx_abrt; /* 0x54 */
  104. volatile uint32_t ic_clr_rx_done; /* 0x58 */
  105. volatile uint32_t ic_clr_activity; /* 0x5c */
  106. volatile uint32_t ic_clr_stop_det; /* 0x60 */
  107. volatile uint32_t ic_clr_start_det; /* 0x64 */
  108. volatile uint32_t ic_clr_gen_call; /* 0x68 */
  109. volatile uint32_t ic_enable; /* 0x6c */
  110. volatile uint32_t ic_status; /* 0x70 */
  111. volatile uint32_t ic_txflr; /* 0x74 */
  112. volatile uint32_t ic_rxflr; /* 0x78 */
  113. volatile uint32_t ic_sda_hold; /* 0x7c */
  114. volatile uint32_t ic_tx_abrt_source; /* 0x80 */
  115. volatile uint32_t ic_slv_dat_nack_only; /* 0x84 */
  116. volatile uint32_t ic_dma_cr; /* 0x88 */
  117. volatile uint32_t ic_dma_tdlr; /* 0x8c */
  118. volatile uint32_t ic_dma_rdlr; /* 0x90 */
  119. volatile uint32_t ic_sda_setup; /* 0x94 */
  120. volatile uint32_t ic_ack_general_call; /* 0x98 */
  121. volatile uint32_t ic_enable_status; /* 0x9c */
  122. volatile uint32_t ic_fs_spklen; /* 0xa0 */
  123. volatile uint32_t ic_hs_spklen; /* 0xa4 */
  124. };
  125. #if !defined(IC_CLK)
  126. #define IC_CLK 100
  127. #endif
  128. #define NANO_TO_MICRO 1000
  129. /* High and low times in different speed modes (in ns) */
  130. #define MIN_SS_SCL_HIGHTIME 4000
  131. #define MIN_SS_SCL_LOWTIME 4700
  132. #define MIN_FS_SCL_HIGHTIME 600
  133. #define MIN_FS_SCL_LOWTIME 1300
  134. #define MIN_HS100pF_SCL_HIGHTIME 60
  135. #define MIN_HS100pF_SCL_LOWTIME 120
  136. #define MIN_HS400pF_SCL_HIGHTIME 160
  137. #define MIN_HS400pF_SCL_LOWTIME 320
  138. #define CONFIG_SYS_HZ 1000 /* decrementer freq: 1ms ticks */
  139. /* Worst case timeout for 1 byte is kept as 2ms */
  140. #define I2C_BYTE_TO (CONFIG_SYS_HZ/500)
  141. #define I2C_STOPDET_TO (CONFIG_SYS_HZ/500)
  142. #define I2C_BYTE_TO_BB (I2C_BYTE_TO * 16)
  143. /* i2c control register definitions */
  144. #define IC_CON_SD 0x0040
  145. #define IC_CON_RE 0x0020
  146. #define IC_CON_10BITADDRMASTER 0x0010
  147. #define IC_CON_10BITADDR_SLAVE 0x0008
  148. #define IC_CON_SPD_MSK 0x0006
  149. #define IC_CON_SPD_SS 0x0002
  150. #define IC_CON_SPD_FS 0x0004
  151. #define IC_CON_SPD_HS 0x0006
  152. #define IC_CON_MM 0x0001
  153. /* i2c data buffer and command register definitions */
  154. #define IC_CMD 0x0100
  155. #define IC_STOP 0x0200
  156. /* i2c interrupt status register definitions */
  157. #define IC_GEN_CALL 0x0800
  158. #define IC_START_DET 0x0400
  159. #define IC_STOP_DET 0x0200
  160. #define IC_ACTIVITY 0x0100
  161. #define IC_RX_DONE 0x0080
  162. #define IC_TX_ABRT 0x0040
  163. #define IC_RD_REQ 0x0020
  164. #define IC_TX_EMPTY 0x0010
  165. #define IC_TX_OVER 0x0008
  166. #define IC_RX_FULL 0x0004
  167. #define IC_RX_OVER 0x0002
  168. #define IC_RX_UNDER 0x0001
  169. /* fifo threshold register definitions */
  170. #define IC_TL0 0x00
  171. #define IC_TL1 0x01
  172. #define IC_TL2 0x02
  173. #define IC_TL3 0x03
  174. #define IC_TL4 0x04
  175. #define IC_TL5 0x05
  176. #define IC_TL6 0x06
  177. #define IC_TL7 0x07
  178. #define IC_RX_TL IC_TL0
  179. #define IC_TX_TL IC_TL0
  180. /* i2c enable register definitions */
  181. #define IC_ENABLE 0x0001
  182. /* i2c status register definitions */
  183. #define IC_STATUS_SA 0x0040
  184. #define IC_STATUS_MA 0x0020
  185. #define IC_STATUS_RFF 0x0010
  186. #define IC_STATUS_RFNE 0x0008
  187. #define IC_STATUS_TFE 0x0004
  188. #define IC_STATUS_TFNF 0x0002
  189. #define IC_STATUS_ACT 0x0001
  190. /* Speed Selection */
  191. #define IC_SPEED_MODE_STANDARD 1
  192. #define IC_SPEED_MODE_FAST 2
  193. #define IC_SPEED_MODE_MAX 3
  194. #define I2C_MAX_SPEED 3400000
  195. #define I2C_FAST_SPEED 400000
  196. #define I2C_STANDARD_SPEED 100000
  197. #define I2C_SPEED I2C_FAST_SPEED
  198. #define I2C0_IRQ (I2C_IRQ_BASE + 0)
  199. #define I2C1_IRQ (I2C_IRQ_BASE + 1)
  200. #define I2C2_IRQ (I2C_IRQ_BASE + 2)
  201. #define I2C3_IRQ (I2C_IRQ_BASE + 3)
  202. #define I2C4_IRQ (I2C_IRQ_BASE + 4)
  203. int rt_hw_i2c_init(void);
  204. #endif /* __DRV_HW_I2C_H__ */