fh_i2c.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. UINT32 I2C_ClearAndGetInterrupts(struct fh_i2c_obj *i2c_obj)
  143. {
  144. UINT32 stat;
  145. /*
  146. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  147. * Ths unmasked raw version of interrupt status bits are available
  148. * in the IC_RAW_INTR_STAT register.
  149. *
  150. * That is,
  151. * stat = readl(IC_INTR_STAT);
  152. * equals to,
  153. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  154. *
  155. * The raw version might be useful for debugging purposes.
  156. */
  157. stat = GET_REG(i2c_obj->base + OFFSET_I2C_INTR_STAT);
  158. /*
  159. * Do not use the IC_CLR_INTR register to clear interrupts, or
  160. * you'll miss some interrupts, triggered during the period from
  161. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  162. *
  163. * Instead, use the separately-prepared IC_CLR_* registers.
  164. */
  165. if (stat & DW_IC_INTR_RX_UNDER)
  166. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_UNDER);
  167. if (stat & DW_IC_INTR_RX_OVER)
  168. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_OVER);
  169. if (stat & DW_IC_INTR_TX_OVER)
  170. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_TX_OVER);
  171. if (stat & DW_IC_INTR_RD_REQ)
  172. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RD_REQ);
  173. if (stat & DW_IC_INTR_TX_ABRT)
  174. {
  175. /*
  176. * The IC_TX_ABRT_SOURCE register is cleared whenever
  177. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  178. */
  179. i2c_obj->abort_source = GET_REG(i2c_obj->base + OFFSET_I2C_TX_ABRT_SOURCE);
  180. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_TX_ABRT);
  181. }
  182. if (stat & DW_IC_INTR_RX_DONE)
  183. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_RX_DONE);
  184. if (stat & DW_IC_INTR_ACTIVITY)
  185. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_ACTIVITY);
  186. if (stat & DW_IC_INTR_STOP_DET)
  187. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_STOP_DET);
  188. if (stat & DW_IC_INTR_START_DET)
  189. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_START_DET);
  190. if (stat & DW_IC_INTR_GEN_CALL)
  191. GET_REG(i2c_obj->base + OFFSET_I2C_CLR_GEN_CALL);
  192. return stat;
  193. }
  194. int I2C_HandleTxAbort(struct fh_i2c_obj *i2c_obj)
  195. {
  196. unsigned long abort_source = i2c_obj->abort_source;
  197. int i;
  198. if (abort_source & DW_IC_TX_ABRT_NOACK)
  199. {
  200. //for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  201. // rt_kprintf( "%s: %s\n", __func__, abort_sources[i]);
  202. return 0;
  203. }
  204. //for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  205. // rt_kprintf( "%s: %s\n", __func__, abort_sources[i]);
  206. rt_kprintf("%s: abort_sources 0x%x\n", __func__, abort_sources);
  207. if (abort_source & DW_IC_TX_ARB_LOST)
  208. return 0;
  209. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  210. return 0; /* wrong msgs[] data */
  211. else
  212. return 0;
  213. }
  214. void I2C_Init(struct fh_i2c_obj *i2c_obj)
  215. {
  216. UINT32 ic_con;
  217. UINT32 param0 = GET_REG(i2c_obj->base + OFFSET_I2C_COMP_PARAM1);
  218. I2C_WaitMasterIdle(i2c_obj);
  219. I2C_Enable(i2c_obj, RT_FALSE);
  220. I2C_SetSpeedCount(i2c_obj);
  221. i2c_obj->config.tx_fifo_depth = ((param0 >> 16) & 0xff) + 1;
  222. i2c_obj->config.rx_fifo_depth = ((param0 >> 8) & 0xff) + 1;
  223. /* Configure Tx/Rx FIFO threshold levels */
  224. SET_REG(i2c_obj->base + OFFSET_I2C_TX_TL, i2c_obj->config.tx_fifo_depth - 1);
  225. SET_REG(i2c_obj->base + OFFSET_I2C_RX_TL, 0);
  226. /* configure the i2c master */
  227. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  228. /*OFFSET_I2C_CON_RESTART_EN |*/ DW_IC_CON_SPEED_FAST; //DW_IC_CON_SPEED_STD;
  229. SET_REG( i2c_obj->base + OFFSET_I2C_CON, ic_con);
  230. }