fh_i2c.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "inc/fh_driverlib.h"
  27. int I2C_WaitMasterIdle(struct fh_i2c_obj *i2c_obj)
  28. {
  29. UINT32 reg;
  30. int timeout = 200; //20 ms
  31. while (GET_REG(i2c_obj->base + OFFSET_I2C_STATUS) & DW_IC_STATUS_MASTER_ACTIVITY)
  32. {
  33. if(timeout < 0)
  34. {
  35. rt_kprintf( "ERROR: %s, timeout waiting for master not active, txflr: 0x%x, rxflr: 0x%x, stat: 0x%x\n",
  36. __func__, I2C_GetReceiveFifoLevel(i2c_obj), I2C_GetTransmitFifoLevel(i2c_obj), GET_REG(i2c_obj->base + OFFSET_I2C_INTR_STAT));
  37. return -RT_ETIMEOUT;
  38. }
  39. timeout--;
  40. udelay(100);
  41. }
  42. return 0;
  43. }
  44. int I2C_WaitDeviceIdle(struct fh_i2c_obj *i2c_obj)
  45. {
  46. UINT32 reg;
  47. int timeout = 2000; //200 ms
  48. while (GET_REG(i2c_obj->base + OFFSET_I2C_STATUS) & DW_IC_STATUS_ACTIVITY)
  49. {
  50. if(timeout < 0)
  51. {
  52. rt_kprintf( "ERROR: %s, timeout waiting for device not active\n", __func__);
  53. return -RT_ETIMEOUT;
  54. }
  55. timeout--;
  56. udelay(100);
  57. }
  58. return 0;
  59. }
  60. static inline UINT32 I2C_CalcSclHcnt(UINT32 ic_clk, UINT32 tSYMBOL, UINT32 tf, int cond, int offset)
  61. {
  62. /*
  63. * DesignWare I2C core doesn't seem to have solid strategy to meet
  64. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  65. * will result in violation of the tHD;STA spec.
  66. */
  67. if (cond)
  68. /*
  69. * Conditional expression:
  70. *
  71. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  72. *
  73. * This is based on the DW manuals, and represents an ideal
  74. * configuration. The resulting I2C bus speed will be
  75. * faster than any of the others.
  76. *
  77. * If your hardware is free from tHD;STA issue, try this one.
  78. */
  79. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  80. else
  81. /*
  82. * Conditional expression:
  83. *
  84. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  85. *
  86. * This is just experimental rule; the tHD;STA period turned
  87. * out to be proportinal to (_HCNT + 3). With this setting,
  88. * we could meet both tHIGH and tHD;STA timing specs.
  89. *
  90. * If unsure, you'd better to take this alternative.
  91. *
  92. * The reason why we need to take into account "tf" here,
  93. * is the same as described in i2c_fh_scl_lcnt().
  94. */
  95. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  96. }
  97. static inline UINT32 I2C_CalcSclLcnt(UINT32 ic_clk, UINT32 tLOW, UINT32 tf, int offset)
  98. {
  99. /*
  100. * Conditional expression:
  101. *
  102. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  103. *
  104. * DW I2C core starts counting the SCL CNTs for the LOW period
  105. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  106. * In order to meet the tLOW timing spec, we need to take into
  107. * account the fall time of SCL signal (tf). Default tf value
  108. * should be 0.3 us, for safety.
  109. */
  110. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  111. }
  112. static int I2C_SetSpeedCount(struct fh_i2c_obj *i2c_obj)
  113. {
  114. UINT32 hcnt, lcnt;
  115. /* set standard and fast speed count for high/low periods */
  116. /* Standard-mode */
  117. hcnt = I2C_CalcSclHcnt(i2c_obj->input_clock,
  118. 40, /* tHD;STA = tHIGH = 4.0 us */
  119. 3, /* tf = 0.3 us */
  120. 0, /* 0: DW default, 1: Ideal */
  121. 0); /* No offset */
  122. lcnt = I2C_CalcSclLcnt(i2c_obj->input_clock,
  123. 47, /* tLOW = 4.7 us */
  124. 3, /* tf = 0.3 us */
  125. 0); /* No offset */
  126. SET_REG(i2c_obj->base + OFFSET_I2C_SS_SCL_HCNT, hcnt);
  127. SET_REG(i2c_obj->base + OFFSET_I2C_SS_SCL_LCNT, lcnt);
  128. /* Fast-mode */
  129. hcnt = I2C_CalcSclHcnt(i2c_obj->input_clock,
  130. 6, /* tHD;STA = tHIGH = 0.6 us */
  131. 3, /* tf = 0.3 us */
  132. 0, /* 0: DW default, 1: Ideal */
  133. 0); /* No offset */
  134. lcnt = I2C_CalcSclLcnt(i2c_obj->input_clock,
  135. 13, /* tLOW = 1.3 us */
  136. 3, /* tf = 0.3 us */
  137. 0); /* No offset */
  138. SET_REG(i2c_obj->base + OFFSET_I2C_FS_SCL_HCNT, hcnt);
  139. SET_REG(i2c_obj->base + OFFSET_I2C_FS_SCL_LCNT, lcnt);
  140. return 0;
  141. }
  142. inline UINT8 I2C_GetData(struct fh_i2c_obj *i2c_obj)
  143. {
  144. return GET_REG(i2c_obj->base + OFFSET_I2C_DATA_CMD) & 0xff;
  145. }
  146. inline void I2C_SetDataCmd(struct fh_i2c_obj *i2c_obj, UINT32 reg)
  147. {
  148. SET_REG(i2c_obj->base + OFFSET_I2C_DATA_CMD, reg);
  149. }
  150. inline void I2C_SetInterruptMask(struct fh_i2c_obj *i2c_obj, UINT32 mask)
  151. {
  152. SET_REG(i2c_obj->base + OFFSET_I2C_INTR_MASK, mask);
  153. }
  154. inline UINT32 I2C_GetInterruptMask(struct fh_i2c_obj *i2c_obj)
  155. {
  156. return GET_REG(i2c_obj->base + OFFSET_I2C_INTR_MASK);
  157. }
  158. UINT32 I2C_ClearAndGetInterrupts(struct fh_i2c_obj *i2c_obj)
  159. {
  160. UINT32 stat;
  161. /*
  162. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  163. * Ths unmasked raw version of interrupt status bits are available
  164. * in the IC_RAW_INTR_STAT register.
  165. *
  166. * That is,
  167. * stat = readl(IC_INTR_STAT);
  168. * equals to,
  169. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  170. *
  171. * The raw version might be useful for debugging purposes.
  172. */
  173. stat = GET_REG(i2c_obj->base + OFFSET_I2C_INTR_STAT);
  174. /*
  175. * Do not use the IC_CLR_INTR register to clear interrupts, or
  176. * you'll miss some interrupts, triggered during the period from
  177. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  178. *
  179. * Instead, use the separately-prepared IC_CLR_* registers.
  180. */
  181. if (stat & DW_IC_INTR_RX_UNDER)
  182. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_UNDER);
  183. if (stat & DW_IC_INTR_RX_OVER)
  184. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_OVER);
  185. if (stat & DW_IC_INTR_TX_OVER)
  186. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_TX_OVER);
  187. if (stat & DW_IC_INTR_RD_REQ)
  188. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RD_REQ);
  189. if (stat & DW_IC_INTR_TX_ABRT)
  190. {
  191. /*
  192. * The IC_TX_ABRT_SOURCE register is cleared whenever
  193. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  194. */
  195. i2c_obj->abort_source = GET_REG(i2c_obj->base + OFFSET_I2C_TX_ABRT_SOURCE);
  196. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_TX_ABRT);
  197. }
  198. if (stat & DW_IC_INTR_RX_DONE)
  199. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_DONE);
  200. if (stat & DW_IC_INTR_ACTIVITY)
  201. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_ACTIVITY);
  202. if (stat & DW_IC_INTR_STOP_DET)
  203. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_STOP_DET);
  204. if (stat & DW_IC_INTR_START_DET)
  205. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_START_DET);
  206. if (stat & DW_IC_INTR_GEN_CALL)
  207. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_GEN_CALL);
  208. return stat;
  209. }
  210. int I2C_HandleTxAbort(struct fh_i2c_obj *i2c_obj)
  211. {
  212. unsigned long abort_source = i2c_obj->abort_source;
  213. int i;
  214. if (abort_source & DW_IC_TX_ABRT_NOACK)
  215. {
  216. //for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  217. // rt_kprintf( "%s: %s\n", __func__, abort_sources[i]);
  218. return 0;
  219. }
  220. //for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  221. // rt_kprintf( "%s: %s\n", __func__, abort_sources[i]);
  222. rt_kprintf("%s: abort_sources 0x%x\n", __func__, abort_sources);
  223. if (abort_source & DW_IC_TX_ARB_LOST)
  224. return 0;
  225. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  226. return 0; /* wrong msgs[] data */
  227. else
  228. return 0;
  229. }
  230. inline UINT32 I2C_SetTransmitThreshold(struct fh_i2c_obj *i2c_obj, int txtl)
  231. {
  232. return SET_REG(i2c_obj->base + OFFSET_I2C_TX_TL, txtl);
  233. }
  234. inline UINT32 I2C_GetReceiveFifoLevel(struct fh_i2c_obj *i2c_obj)
  235. {
  236. return GET_REG(i2c_obj->base + OFFSET_I2C_RXFLR);
  237. }
  238. inline UINT32 I2C_GetTransmitFifoLevel(struct fh_i2c_obj *i2c_obj)
  239. {
  240. return GET_REG(i2c_obj->base + OFFSET_I2C_TXFLR);
  241. }
  242. inline void I2C_SetSlaveAddress(struct fh_i2c_obj *i2c_obj, rt_uint16_t addr)
  243. {
  244. UINT32 reg;
  245. reg = GET_REG(i2c_obj->base + OFFSET_I2C_TAR);
  246. reg &= ~(0x3ff);
  247. reg |= addr & 0x3ff;
  248. SET_REG(i2c_obj->base + OFFSET_I2C_TAR, reg);
  249. }
  250. inline void I2C_Enable(struct fh_i2c_obj *i2c_obj, int enable)
  251. {
  252. SET_REG(i2c_obj->base + OFFSET_I2C_ENABLE, enable);
  253. }
  254. void I2C_Init(struct fh_i2c_obj *i2c_obj)
  255. {
  256. UINT32 ic_con;
  257. UINT32 param0 = GET_REG(i2c_obj->base + OFFSET_I2C_COMP_PARAM1);
  258. I2C_WaitMasterIdle(i2c_obj);
  259. I2C_Enable(i2c_obj, RT_FALSE);
  260. I2C_SetSpeedCount(i2c_obj);
  261. i2c_obj->config.tx_fifo_depth = ((param0 >> 16) & 0xff) + 1;
  262. i2c_obj->config.rx_fifo_depth = ((param0 >> 8) & 0xff) + 1;
  263. /* Configure Tx/Rx FIFO threshold levels */
  264. SET_REG(i2c_obj->base + OFFSET_I2C_TX_TL, i2c_obj->config.tx_fifo_depth - 1);
  265. SET_REG(i2c_obj->base + OFFSET_I2C_RX_TL, 0);
  266. /* configure the i2c master */
  267. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  268. /*OFFSET_I2C_CON_RESTART_EN |*/ DW_IC_CON_SPEED_FAST; //DW_IC_CON_SPEED_STD;
  269. SET_REG( i2c_obj->base + OFFSET_I2C_CON, ic_con);
  270. }