fh_i2c.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. #ifndef FH_I2C_H_
  27. #define FH_I2C_H_
  28. #include "fh_def.h"
  29. #define OFFSET_I2C_CON (0x0000)
  30. #define OFFSET_I2C_TAR (0x0004)
  31. #define OFFSET_I2C_SAR (0x0008)
  32. #define OFFSET_I2C_HS_MADDR (0x000C)
  33. #define OFFSET_I2C_DATA_CMD (0x0010)
  34. #define OFFSET_I2C_SS_SCL_HCNT (0x0014)
  35. #define OFFSET_I2C_SS_SCL_LCNT (0x0018)
  36. #define OFFSET_I2C_FS_SCL_HCNT (0x001C)
  37. #define OFFSET_I2C_FS_SCL_LCNT (0x0020)
  38. #define OFFSET_I2C_HS_SCL_HCNT (0x0024)
  39. #define OFFSET_I2C_HS_SCL_LCNT (0x0028)
  40. #define OFFSET_I2C_INTR_STAT (0x002c)
  41. #define OFFSET_I2C_INTR_MASK (0x0030)
  42. #define OFFSET_I2C_RAW_INTR_STAT (0x0034)
  43. #define OFFSET_I2C_RX_TL (0x0038)
  44. #define OFFSET_I2C_TX_TL (0x003c)
  45. #define OFFSET_I2C_CLR_INTR (0x0040)
  46. #define OFFSET_I2C_CLR_RX_UNDER (0x0044)
  47. #define OFFSET_I2C_CLR_RX_OVER (0x0048)
  48. #define OFFSET_I2C_CLR_TX_OVER (0x004c)
  49. #define OFFSET_I2C_CLR_RD_REQ (0x0050)
  50. #define OFFSET_I2C_CLR_TX_ABRT (0x0054)
  51. #define OFFSET_I2C_CLR_RX_DONE (0x0058)
  52. #define OFFSET_I2C_CLR_ACTIVITY (0x005c)
  53. #define OFFSET_I2C_CLR_STOP_DET (0x0060)
  54. #define OFFSET_I2C_CLR_START_DET (0x0064)
  55. #define OFFSET_I2C_CLR_GEN_CALL (0x0068)
  56. #define OFFSET_I2C_ENABLE (0x006c)
  57. #define OFFSET_I2C_STATUS (0x0070)
  58. #define OFFSET_I2C_TXFLR (0x0074)
  59. #define OFFSET_I2C_RXFLR (0x0078)
  60. #define OFFSET_I2C_DMA_CR (0x0088)
  61. #define OFFSET_I2C_DMA_TDLR (0x008c)
  62. #define OFFSET_I2C_DMA_RDLR (0x0090)
  63. #define OFFSET_I2C_COMP_PARAM1 (0x00f4)
  64. #define OFFSET_I2C_TX_ABRT_SOURCE (0x0080)
  65. #define I2C_M_TEN 0x0100 /* this is a ten bit chip address */
  66. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  67. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  68. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  69. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  70. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  71. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  72. #define DW_IC_CON_MASTER 0x1
  73. #define DW_IC_CON_SPEED_STD 0x2
  74. #define DW_IC_CON_SPEED_FAST 0x4
  75. #define DW_IC_CON_10BITADDR_MASTER 0x10
  76. #define DW_IC_CON_RESTART_EN 0x20
  77. #define DW_IC_CON_SLAVE_DISABLE 0x40
  78. #define DW_IC_INTR_RX_UNDER 0x001
  79. #define DW_IC_INTR_RX_OVER 0x002
  80. #define DW_IC_INTR_RX_FULL 0x004
  81. #define DW_IC_INTR_TX_OVER 0x008
  82. #define DW_IC_INTR_TX_EMPTY 0x010
  83. #define DW_IC_INTR_RD_REQ 0x020
  84. #define DW_IC_INTR_TX_ABRT 0x040
  85. #define DW_IC_INTR_RX_DONE 0x080
  86. #define DW_IC_INTR_ACTIVITY 0x100
  87. #define DW_IC_INTR_STOP_DET 0x200
  88. #define DW_IC_INTR_START_DET 0x400
  89. #define DW_IC_INTR_GEN_CALL 0x800
  90. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  91. DW_IC_INTR_TX_EMPTY | \
  92. DW_IC_INTR_TX_ABRT | \
  93. DW_IC_INTR_STOP_DET)
  94. #define DW_IC_STATUS_ACTIVITY 0x1
  95. #define DW_IC_STATUS_MASTER_ACTIVITY 0x20
  96. #define DW_IC_ERR_TX_ABRT 0x1
  97. /*
  98. * status codes
  99. */
  100. #define STATUS_IDLE 0x0
  101. #define STATUS_WRITE_IN_PROGRESS 0x1
  102. #define STATUS_READ_IN_PROGRESS 0x2
  103. #define TIMEOUT 20 /* ms */
  104. /*
  105. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  106. *
  107. * only expected abort codes are listed here
  108. * refer to the datasheet for the full list
  109. */
  110. #define ABRT_7B_ADDR_NOACK 0
  111. #define ABRT_10ADDR1_NOACK 1
  112. #define ABRT_10ADDR2_NOACK 2
  113. #define ABRT_TXDATA_NOACK 3
  114. #define ABRT_GCALL_NOACK 4
  115. #define ABRT_GCALL_READ 5
  116. #define ABRT_SBYTE_ACKDET 7
  117. #define ABRT_SBYTE_NORSTRT 9
  118. #define ABRT_10B_RD_NORSTRT 10
  119. #define ABRT_MASTER_DIS 11
  120. #define ARB_LOST 12
  121. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  122. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  123. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  124. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  125. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  126. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  127. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  128. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  129. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  130. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  131. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  132. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  133. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  134. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  135. DW_IC_TX_ABRT_TXDATA_NOACK | \
  136. DW_IC_TX_ABRT_GCALL_NOACK)
  137. static char *abort_sources[] = {
  138. [ABRT_7B_ADDR_NOACK] =
  139. "slave address not acknowledged (7bit mode)",
  140. [ABRT_10ADDR1_NOACK] =
  141. "first address byte not acknowledged (10bit mode)",
  142. [ABRT_10ADDR2_NOACK] =
  143. "second address byte not acknowledged (10bit mode)",
  144. [ABRT_TXDATA_NOACK] =
  145. "data not acknowledged",
  146. [ABRT_GCALL_NOACK] =
  147. "no acknowledgement for a general call",
  148. [ABRT_GCALL_READ] =
  149. "read after general call",
  150. [ABRT_SBYTE_ACKDET] =
  151. "start byte acknowledged",
  152. [ABRT_SBYTE_NORSTRT] =
  153. "trying to send start byte when restart is disabled",
  154. [ABRT_10B_RD_NORSTRT] =
  155. "trying to read when restart is disabled (10bit mode)",
  156. [ABRT_MASTER_DIS] =
  157. "trying to use disabled adapter",
  158. [ARB_LOST] =
  159. "lost arbitration",
  160. };
  161. /* i2c interrput definition */
  162. #define M_GEN_CALL (1<<11)
  163. #define M_START_DET (1<<10)
  164. #define M_STOP_DET (1<<9)
  165. #define M_ACTIVITY (1<<8)
  166. #define M_RX_DONE (1<<7)
  167. #define M_TX_ABRT (1<<6)
  168. #define M_RD_REQ (1<<5)
  169. #define M_TX_EMPTY (1<<4)
  170. #define M_TX_OVER (1<<3)
  171. #define M_RX_FULL (1<<2)
  172. #define M_RX_OVER (1<<1)
  173. #define M_RX_UNDER (1<<0)
  174. #define M_NONE (0)
  175. struct i2c_config
  176. {
  177. int speed_mode;
  178. UINT32 tx_fifo_depth;
  179. UINT32 rx_fifo_depth;
  180. };
  181. struct fh_i2c_obj
  182. {
  183. UINT32 id;
  184. UINT32 irq;
  185. UINT32 base;
  186. UINT32 input_clock;
  187. UINT32 abort_source;
  188. struct i2c_config config;
  189. };
  190. rt_inline UINT32 I2C_SetTransmitThreshold(struct fh_i2c_obj *i2c_obj, int txtl)
  191. {
  192. return SET_REG(i2c_obj->base + OFFSET_I2C_TX_TL, txtl);
  193. }
  194. rt_inline UINT32 I2C_GetReceiveFifoLevel(struct fh_i2c_obj *i2c_obj)
  195. {
  196. return GET_REG(i2c_obj->base + OFFSET_I2C_RXFLR);
  197. }
  198. rt_inline UINT32 I2C_GetTransmitFifoLevel(struct fh_i2c_obj *i2c_obj)
  199. {
  200. return GET_REG(i2c_obj->base + OFFSET_I2C_TXFLR);
  201. }
  202. rt_inline void I2C_SetSlaveAddress(struct fh_i2c_obj *i2c_obj, rt_uint16_t addr)
  203. {
  204. UINT32 reg;
  205. reg = GET_REG(i2c_obj->base + OFFSET_I2C_TAR);
  206. reg &= ~(0x3ff);
  207. reg |= addr & 0x3ff;
  208. SET_REG(i2c_obj->base + OFFSET_I2C_TAR, reg);
  209. }
  210. rt_inline void I2C_Enable(struct fh_i2c_obj *i2c_obj, int enable)
  211. {
  212. SET_REG(i2c_obj->base + OFFSET_I2C_ENABLE, enable);
  213. }
  214. rt_inline UINT8 I2C_GetData(struct fh_i2c_obj *i2c_obj)
  215. {
  216. return GET_REG(i2c_obj->base + OFFSET_I2C_DATA_CMD) & 0xff;
  217. }
  218. rt_inline void I2C_SetDataCmd(struct fh_i2c_obj *i2c_obj, UINT32 reg)
  219. {
  220. SET_REG(i2c_obj->base + OFFSET_I2C_DATA_CMD, reg);
  221. }
  222. rt_inline void I2C_SetInterruptMask(struct fh_i2c_obj *i2c_obj, UINT32 mask)
  223. {
  224. SET_REG(i2c_obj->base + OFFSET_I2C_INTR_MASK, mask);
  225. }
  226. rt_inline UINT32 I2C_GetInterruptMask(struct fh_i2c_obj *i2c_obj)
  227. {
  228. return GET_REG(i2c_obj->base + OFFSET_I2C_INTR_MASK);
  229. }
  230. void I2C_Init(struct fh_i2c_obj *i2c_obj);
  231. int I2C_HandleTxAbort(struct fh_i2c_obj *i2c_obj);
  232. UINT32 I2C_ClearAndGetInterrupts(struct fh_i2c_obj *i2c_obj);
  233. int I2C_WaitMasterIdle(struct fh_i2c_obj *i2c_obj);
  234. int I2C_WaitDeviceIdle(struct fh_i2c_obj *i2c_obj);
  235. #endif /* FH_I2C_H_ */