drv_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <rthw.h>
  4. #include "board.h"
  5. #include "drv_clock.h"
  6. #include "drv_gpio.h"
  7. #define I2C_DEBUG 0
  8. #if I2C_DEBUG
  9. #define I2C_DBG(...) rt_kprintf("[I2C]"),rt_kprintf(__VA_ARGS__)
  10. #else
  11. #define I2C_DBG(...)
  12. #endif
  13. #define I2C_CTRL (0x00)
  14. #define I2C_TAR (0x04)
  15. #define I2C_SAR (0x08)
  16. #define I2C_DC (0x10)
  17. #define I2C_SHCNT (0x14)
  18. #define I2C_SLCNT (0x18)
  19. #define I2C_FHCNT (0x1C)
  20. #define I2C_FLCNT (0x20)
  21. #define I2C_INTST (0x2C)
  22. #define I2C_INTM (0x30)
  23. #define I2C_RXTL (0x38)
  24. #define I2C_TXTL (0x3c)
  25. #define I2C_CINTR (0x40)
  26. #define I2C_CRXUF (0x44)
  27. #define I2C_CRXOF (0x48)
  28. #define I2C_CTXOF (0x4C)
  29. #define I2C_CRXREQ (0x50)
  30. #define I2C_CTXABRT (0x54)
  31. #define I2C_CRXDONE (0x58)
  32. #define I2C_CACT (0x5C)
  33. #define I2C_CSTP (0x60)
  34. #define I2C_CSTT (0x64)
  35. #define I2C_CGC (0x68)
  36. #define I2C_ENB (0x6C)
  37. #define I2C_STA (0x70)
  38. #define I2C_TXFLR (0x74)
  39. #define I2C_RXFLR (0x78)
  40. #define I2C_SDAHD (0x7C)
  41. #define I2C_TXABRT (0x80)
  42. #define I2C_DMACR (0x88)
  43. #define I2C_DMATDLR (0x8c)
  44. #define I2C_DMARDLR (0x90)
  45. #define I2C_SDASU (0x94)
  46. #define I2C_ACKGC (0x98)
  47. #define I2C_ENSTA (0x9C)
  48. #define I2C_FLT (0xA0)
  49. /* I2C Control Register (I2C_CTRL) */
  50. #define I2C_CTRL_SLVDIS (1 << 6) /* after reset slave is disabled */
  51. #define I2C_CTRL_REST (1 << 5)
  52. #define I2C_CTRL_MATP (1 << 4) /* 1: 10bit address 0: 7bit addressing */
  53. #define I2C_CTRL_SATP (1 << 3) /* 1: 10bit address 0: 7bit address */
  54. #define I2C_CTRL_SPDF (2 << 1) /* fast mode 400kbps */
  55. #define I2C_CTRL_SPDS (1 << 1) /* standard mode 100kbps */
  56. #define I2C_CTRL_MD (1 << 0) /* master enabled */
  57. /* I2C Status Register (I2C_STA) */
  58. #define I2C_STA_SLVACT (1 << 6) /* Slave FSM is not in IDLE state */
  59. #define I2C_STA_MSTACT (1 << 5) /* Master FSM is not in IDLE state */
  60. #define I2C_STA_RFF (1 << 4) /* RFIFO if full */
  61. #define I2C_STA_RFNE (1 << 3) /* RFIFO is not empty */
  62. #define I2C_STA_TFE (1 << 2) /* TFIFO is empty */
  63. #define I2C_STA_TFNF (1 << 1) /* TFIFO is not full */
  64. #define I2C_STA_ACT (1 << 0) /* I2C Activity Status */
  65. /* i2c interrupt status (I2C_INTST) */
  66. #define I2C_INTST_IGC (1 << 11)
  67. #define I2C_INTST_ISTT (1 << 10)
  68. #define I2C_INTST_ISTP (1 << 9)
  69. #define I2C_INTST_IACT (1 << 8)
  70. #define I2C_INTST_RXDN (1 << 7)
  71. #define I2C_INTST_TXABT (1 << 6)
  72. #define I2C_INTST_RDREQ (1 << 5)
  73. #define I2C_INTST_TXEMP (1 << 4)
  74. #define I2C_INTST_TXOF (1 << 3)
  75. #define I2C_INTST_RXFL (1 << 2)
  76. #define I2C_INTST_RXOF (1 << 1)
  77. #define I2C_INTST_RXUF (1 << 0)
  78. /* i2c interrupt mask status (I2C_INTM) */
  79. #define I2C_INTM_MIGC (1 << 11)
  80. #define I2C_INTM_MISTT (1 << 10)
  81. #define I2C_INTM_MISTP (1 << 9)
  82. #define I2C_INTM_MIACT (1 << 8)
  83. #define I2C_INTM_MRXDN (1 << 7)
  84. #define I2C_INTM_MTXABT (1 << 6)
  85. #define I2C_INTM_MRDREQ (1 << 5)
  86. #define I2C_INTM_MTXEMP (1 << 4)
  87. #define I2C_INTM_MTXOF (1 << 3)
  88. #define I2C_INTM_MRXFL (1 << 2)
  89. #define I2C_INTM_MRXOF (1 << 1)
  90. #define I2C_INTM_MRXUF (1 << 0)
  91. #define I2C_DC_REST (1 << 10)
  92. #define I2C_DC_STP (1 << 9)
  93. #define I2C_DC_READ (1 << 8)
  94. #define I2C_ENB_I2CENB (1 << 0) /* Enable the i2c */
  95. #define CONFIG_I2C_FIFO_LEN 64
  96. #define I2C_FIFO_LEN (CONFIG_I2C_FIFO_LEN)
  97. #define TX_LEVEL (I2C_FIFO_LEN / 2)
  98. #define RX_LEVEL (I2C_FIFO_LEN / 2 - 1)
  99. #define TIMEOUT 0xff
  100. /*
  101. * msg_end_type: The bus control which need to be send at end of transfer.
  102. * @MSG_END_STOP: Send stop pulse at end of transfer.
  103. * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
  104. */
  105. enum msg_end_type
  106. {
  107. MSG_END_STOP,
  108. MSG_END_CONTINUE,
  109. MSG_END_REPEAT_START,
  110. };
  111. /* I2C standard mode high count register(I2CSHCNT) */
  112. #define I2CSHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
  113. /* I2C standard mode low count register(I2CSLCNT) */
  114. #define I2CSLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
  115. /* I2C fast mode high count register(I2CFHCNT) */
  116. #define I2CFHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
  117. /* I2C fast mode low count register(I2CFLCNT) */
  118. #define I2CFLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
  119. /* I2C Transmit Abort Status Register (I2C_TXABRT) */
  120. static const char *abrt_src[] =
  121. {
  122. "I2C_TXABRT_ABRT_7B_ADDR_NOACK",
  123. "I2C_TXABRT_ABRT_10ADDR1_NOACK",
  124. "I2C_TXABRT_ABRT_10ADDR2_NOACK",
  125. "I2C_TXABRT_ABRT_XDATA_NOACK",
  126. "I2C_TXABRT_ABRT_GCALL_NOACK",
  127. "I2C_TXABRT_ABRT_GCALL_READ",
  128. "I2C_TXABRT_ABRT_HS_ACKD",
  129. "I2C_TXABRT_SBYTE_ACKDET",
  130. "I2C_TXABRT_ABRT_HS_NORSTRT",
  131. "I2C_TXABRT_SBYTE_NORSTRT",
  132. "I2C_TXABRT_ABRT_10B_RD_NORSTRT",
  133. "I2C_TXABRT_ABRT_MASTER_DIS",
  134. "I2C_TXABRT_ARB_LOST",
  135. "I2C_TXABRT_SLVFLUSH_TXFIFO",
  136. "I2C_TXABRT_SLV_ARBLOST",
  137. "I2C_TXABRT_SLVRD_INTX",
  138. };
  139. struct ingenic_i2c_bus
  140. {
  141. struct rt_i2c_bus_device parent;
  142. rt_uint32_t hwaddr;
  143. rt_uint32_t irqno;
  144. struct clk *clk;
  145. enum msg_end_type w_end_type;
  146. enum msg_end_type r_end_type;
  147. unsigned char *rbuf;
  148. unsigned char *wbuf;
  149. unsigned int rd_len;
  150. int len;
  151. unsigned int rate;
  152. struct rt_completion completion;
  153. };
  154. #ifdef RT_USING_I2C0
  155. static struct ingenic_i2c_bus ingenic_i2c0;
  156. #endif
  157. #ifdef RT_USING_I2C1
  158. static struct ingenic_i2c_bus ingenic_i2c1;
  159. #endif
  160. #ifdef RT_USING_I2C2
  161. static struct ingenic_i2c_bus ingenic_i2c2;
  162. #endif
  163. static inline unsigned short i2c_readl(struct ingenic_i2c_bus *i2c,
  164. unsigned short offset)
  165. {
  166. return readl(i2c->hwaddr + offset);
  167. }
  168. static inline void i2c_writel(struct ingenic_i2c_bus *i2c, unsigned short offset,
  169. unsigned short value)
  170. {
  171. writel(value, i2c->hwaddr + offset);
  172. }
  173. static int ingenic_i2c_disable(struct ingenic_i2c_bus *i2c)
  174. {
  175. int timeout = TIMEOUT;
  176. i2c_writel(i2c, I2C_ENB, 0);
  177. while ((i2c_readl(i2c, I2C_ENSTA) & I2C_ENB_I2CENB) && (--timeout > 0))
  178. rt_thread_delay(1);
  179. if (timeout) return 0;
  180. I2C_DBG("disable i2c failed!\n");
  181. return -RT_ETIMEOUT;
  182. }
  183. static int ingenic_i2c_enable(struct ingenic_i2c_bus *i2c)
  184. {
  185. int timeout = TIMEOUT;
  186. i2c_writel(i2c, I2C_ENB, 1);
  187. while (!(i2c_readl(i2c, I2C_ENSTA) & I2C_ENB_I2CENB) && (--timeout > 0))
  188. rt_thread_delay(1);
  189. if (timeout) return 0;
  190. I2C_DBG("enable i2c failed\n");
  191. return -RT_ETIMEOUT;
  192. }
  193. static void ingenic_i2c_reset(struct ingenic_i2c_bus *i2c)
  194. {
  195. i2c_readl(i2c, I2C_CTXABRT);
  196. i2c_readl(i2c, I2C_INTST);
  197. ingenic_i2c_disable(i2c);
  198. rt_thread_delay(1);
  199. ingenic_i2c_enable(i2c);
  200. }
  201. static int ingenic_i2c_set_speed(struct ingenic_i2c_bus *i2c, int rate)
  202. {
  203. /*ns */
  204. long dev_clk = clk_get_rate(i2c->clk);
  205. long cnt_high = 0; /* HIGH period count of the SCL clock */
  206. long cnt_low = 0; /* LOW period count of the SCL clock */
  207. long setup_time = 0;
  208. long hold_time = 0;
  209. unsigned short tmp;
  210. i2c->rate = rate;
  211. if (ingenic_i2c_disable(i2c))I2C_DBG("i2c not disable\n");
  212. if (rate <= 100000)
  213. {
  214. tmp = 0x43 | (1 << 5); /* standard speed mode */
  215. i2c_writel(i2c, I2C_CTRL, tmp);
  216. }
  217. else
  218. {
  219. tmp = 0x45 | (1 << 5); /* fast speed mode */
  220. i2c_writel(i2c, I2C_CTRL, tmp);
  221. }
  222. /* high
  223. * ____ ____ ____ ____
  224. * clk __| | |___| |____| |____| |___
  225. * | | |
  226. * | | |
  227. * |_|_| _________ ____
  228. * data __/ | |\___/ \____/ \____
  229. * setup->| |<|
  230. * ->| |<-hold
  231. */
  232. //setup_time = (10 000 000/(rate*4)) + 1;
  233. setup_time = (dev_clk / (rate * 4));
  234. if (setup_time > 1) setup_time -= 1;
  235. //hold_time = (10000000/(rate*4)) - 1;
  236. hold_time = (dev_clk / (rate * 4));
  237. /* high
  238. * ____ ____
  239. * clk __| |___| |____
  240. * low
  241. * |<--period--->|
  242. *
  243. */
  244. cnt_high = dev_clk / (rate * 2);
  245. cnt_low = dev_clk / (rate * 2);
  246. if (setup_time > 255) setup_time = 255;
  247. if (setup_time <= 0) setup_time = 1;
  248. if (hold_time > 0xFFFF) hold_time = 0xFFFF;
  249. if (rate <= 100000)
  250. {
  251. i2c_writel(i2c, I2C_SHCNT, I2CSHCNT_ADJUST(cnt_high));
  252. i2c_writel(i2c, I2C_SLCNT, I2CSLCNT_ADJUST(cnt_low));
  253. }
  254. else
  255. {
  256. i2c_writel(i2c, I2C_FHCNT, I2CFHCNT_ADJUST(cnt_high));
  257. i2c_writel(i2c, I2C_FLCNT, I2CFLCNT_ADJUST(cnt_low));
  258. }
  259. i2c_writel(i2c, I2C_SDASU, setup_time & 0xff);
  260. i2c_writel(i2c, I2C_SDAHD, hold_time);
  261. return 0;
  262. }
  263. /* function: send read command
  264. * return: 0, successful
  265. * 1, txfifo valid entry is more than receive fifo, before send read command,
  266. * must be read.
  267. * 2, txfifo count is 0 or rxfifo count is 0.
  268. * */
  269. static inline unsigned int i2c_send_rcmd(struct ingenic_i2c_bus *i2c)
  270. {
  271. unsigned int tx_count, rx_count, count, tx_valid, rx_valid;
  272. tx_valid = i2c_readl(i2c, I2C_TXFLR);
  273. rx_valid = i2c_readl(i2c, I2C_RXFLR);
  274. tx_count = I2C_FIFO_LEN - tx_valid;
  275. rx_count = I2C_FIFO_LEN - rx_valid;
  276. if (tx_valid > rx_count)
  277. {
  278. I2C_DBG("###Warning: I2C transfer fifo valid entry is more receive fifo, "
  279. "before send read cmd, please read data from "
  280. "the read fifo.\n");
  281. return 1;
  282. }
  283. if (!tx_count || !rx_count)
  284. {
  285. I2C_DBG("###Warning: I2C receive fifo or transfer fifo is full, "
  286. "before send read cmd, please read data from "
  287. "the read fifo or wait some time.\n\n");
  288. return 2;
  289. }
  290. count = (i2c->rd_len < tx_count)? i2c->rd_len : tx_count;
  291. count = (count < rx_count)? count : rx_count;
  292. i2c->rd_len -= count;
  293. if (!i2c->rd_len)
  294. {
  295. while (count > 1)
  296. {
  297. i2c_writel(i2c, I2C_DC, I2C_DC_READ);
  298. count--;
  299. }
  300. if (i2c->r_end_type == MSG_END_STOP)
  301. {
  302. i2c_writel(i2c, I2C_DC, I2C_DC_READ | I2C_DC_STP);
  303. }
  304. else
  305. {
  306. i2c_writel(i2c, I2C_DC, I2C_DC_READ);
  307. }
  308. }
  309. else
  310. {
  311. while (count > 0)
  312. {
  313. i2c_writel(i2c, I2C_DC, I2C_DC_READ);
  314. count--;
  315. }
  316. }
  317. return 0;
  318. }
  319. static void txabrt(struct ingenic_i2c_bus *i2c, int src)
  320. {
  321. int i;
  322. I2C_DBG("--I2C txabrt:\n");
  323. for (i = 0; i < 16; i++)
  324. {
  325. if (src & (0x1 << i))
  326. I2C_DBG("--I2C TXABRT[%d]=%s\n", i, abrt_src[i]);
  327. }
  328. }
  329. static int i2c_disable_clk(struct ingenic_i2c_bus *i2c)
  330. {
  331. int timeout = 10;
  332. int tmp = i2c_readl(i2c, I2C_STA);
  333. while ((tmp & I2C_STA_MSTACT) && (--timeout > 0))
  334. {
  335. rt_thread_delay(2);
  336. tmp = i2c_readl(i2c, I2C_STA);
  337. }
  338. if (timeout > 0)
  339. {
  340. clk_disable(i2c->clk);
  341. return 0;
  342. }
  343. else
  344. {
  345. I2C_DBG("--I2C disable clk timeout, I2C_STA = 0x%x\n", tmp);
  346. ingenic_i2c_reset(i2c);
  347. clk_disable(i2c->clk);
  348. return -RT_ETIMEOUT;
  349. }
  350. }
  351. static inline int xfer_read(struct ingenic_i2c_bus *i2c, unsigned char *buf, int len, enum msg_end_type end_type)
  352. {
  353. int ret = 0;
  354. long timeout;
  355. unsigned short tmp;
  356. rt_memset(buf, 0, len);
  357. i2c->rd_len = len;
  358. i2c->len = len;
  359. i2c->rbuf = buf;
  360. i2c->r_end_type = end_type;
  361. i2c_readl(i2c, I2C_CSTP); /* clear STP bit */
  362. i2c_readl(i2c, I2C_CTXOF); /* clear TXOF bit */
  363. i2c_readl(i2c, I2C_CTXABRT); /* clear TXABRT bit */
  364. I2C_DBG("i2c: read %d len data\n", len);
  365. if (len <= I2C_FIFO_LEN)
  366. {
  367. i2c_writel(i2c, I2C_RXTL, len - 1);
  368. }
  369. else
  370. {
  371. i2c_writel(i2c, I2C_RXTL, RX_LEVEL);
  372. }
  373. // I2C_DBG("RXTL: %x\n", i2c_readl(i2c, I2C_RXTL));
  374. I2C_DBG("read len: %d\n", i2c_readl(i2c, I2C_RXFLR));
  375. while (i2c_readl(i2c, I2C_STA) & I2C_STA_RFNE)
  376. {
  377. i2c_readl(i2c, I2C_DC);
  378. }
  379. if (i2c_send_rcmd(i2c))
  380. {
  381. I2C_DBG("i2c: send read command failed!\n");
  382. }
  383. else
  384. {
  385. I2C_DBG("i2c: send read command OK!\n");
  386. }
  387. tmp = I2C_INTM_MRXFL | I2C_INTM_MTXABT;
  388. if (end_type == MSG_END_STOP) tmp |= I2C_INTM_MISTP;
  389. i2c_writel(i2c, I2C_INTM, tmp);
  390. /* wait for finish */
  391. while(rt_completion_wait(&(i2c->completion), RT_TICK_PER_SECOND) == -RT_ETIMEOUT)
  392. I2C_DBG("fifo len: %d\n", i2c_readl(i2c, I2C_RXFLR));
  393. tmp = i2c_readl(i2c, I2C_TXABRT);
  394. if (tmp)
  395. {
  396. txabrt(i2c, tmp);
  397. if (tmp > 0x1 && tmp < 0x10)
  398. ret = -RT_EIO;
  399. else
  400. ret = -RT_EIO;
  401. /* abort read */
  402. if (tmp & (1 << 5)) {
  403. ret = -RT_ERROR;
  404. }
  405. i2c_readl(i2c, I2C_CTXABRT);
  406. }
  407. if (ret < 0) ingenic_i2c_reset(i2c);
  408. return ret;
  409. }
  410. static inline int xfer_write(struct ingenic_i2c_bus *i2c, unsigned char *buf, int len, enum msg_end_type end_type)
  411. {
  412. int ret = 0;
  413. long timeout = TIMEOUT;
  414. unsigned short reg_tmp;
  415. i2c->wbuf = buf;
  416. i2c->len = len;
  417. i2c_writel(i2c, I2C_TXTL, TX_LEVEL);
  418. i2c_readl(i2c, I2C_CSTP); /* clear STP bit */
  419. i2c_readl(i2c, I2C_CTXOF); /* clear TXOF bit */
  420. i2c_readl(i2c, I2C_CTXABRT); /* clear TXABRT bit */
  421. I2C_DBG("i2c: write %d len data\n", len);
  422. i2c->w_end_type = end_type;
  423. while ((i2c_readl(i2c, I2C_STA) & I2C_STA_TFNF) && (i2c->len > 0))
  424. {
  425. reg_tmp = *i2c->wbuf++;
  426. if (i2c->len == 1)
  427. {
  428. if (end_type == MSG_END_STOP)
  429. {
  430. reg_tmp |= I2C_DC_STP;
  431. }
  432. }
  433. i2c_writel(i2c, I2C_DC, reg_tmp);
  434. i2c->len -= 1;
  435. }
  436. if (i2c->len == 0)
  437. {
  438. i2c_writel(i2c, I2C_TXTL, 0);
  439. }
  440. reg_tmp = I2C_INTM_MTXEMP | I2C_INTM_MTXABT | I2C_INTM_MTXOF;
  441. if (end_type == MSG_END_STOP) reg_tmp |= I2C_INTM_MISTP;
  442. i2c_writel(i2c, I2C_INTM, reg_tmp);
  443. /* wait for finish */
  444. rt_completion_wait(&(i2c->completion), rt_tick_from_millisecond(2000));
  445. reg_tmp = i2c_readl(i2c, I2C_TXABRT);
  446. if (reg_tmp)
  447. {
  448. txabrt(i2c, reg_tmp);
  449. if (reg_tmp > 0x1 && reg_tmp < 0x10)
  450. ret = -RT_EIO;
  451. else
  452. ret = -RT_EIO;
  453. //after I2C_TXABRT_ABRT_XDATA_NOACK error,this required core to resend
  454. if (reg_tmp & 8)
  455. {
  456. ret = -RT_ERROR;
  457. }
  458. i2c_readl(i2c, I2C_CTXABRT);
  459. }
  460. if (ret < 0) ingenic_i2c_reset(i2c);
  461. return ret;
  462. }
  463. static rt_size_t ingenic_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  464. {
  465. int i;
  466. int ret = RT_EOK;
  467. struct ingenic_i2c_bus *i2c;
  468. i2c = (struct ingenic_i2c_bus*)bus;
  469. clk_enable(i2c->clk);
  470. i2c_writel(i2c, I2C_TAR, msgs[0].addr);
  471. for (i = 0; i < num; i++)
  472. {
  473. enum msg_end_type end_type = MSG_END_STOP;
  474. if (i < (num - 1))
  475. {
  476. if (msgs[i + 1].flags & RT_I2C_NO_START)
  477. {
  478. end_type = MSG_END_CONTINUE; /* have no STOP and START */
  479. }
  480. else
  481. {
  482. end_type = MSG_END_REPEAT_START; /* have no STOP but have RESTART */
  483. }
  484. }
  485. /* initialize completion */
  486. rt_completion_init(&(i2c->completion));
  487. if (msgs[i].flags & RT_I2C_RD)
  488. {
  489. ret = xfer_read(i2c, msgs[i].buf, msgs[i].len, end_type);
  490. }
  491. else
  492. {
  493. ret = xfer_write(i2c, msgs[i].buf, msgs[i].len, end_type);
  494. }
  495. if (ret < 0)
  496. {
  497. clk_disable(i2c->clk);
  498. goto _ERR;
  499. }
  500. }
  501. ret = i2c_disable_clk(i2c);
  502. _ERR:
  503. return ret < 0? ret : i;
  504. }
  505. static const struct rt_i2c_bus_device_ops i2c_ops =
  506. {
  507. ingenic_i2c_xfer,
  508. RT_NULL,
  509. RT_NULL
  510. };
  511. static void i2c_irq_handler(int irqno, void *param)
  512. {
  513. unsigned short tmp, intst, intmsk;
  514. struct ingenic_i2c_bus *i2c;
  515. i2c = (struct ingenic_i2c_bus*)param;
  516. intst = i2c_readl(i2c, I2C_INTST);
  517. intmsk = i2c_readl(i2c, I2C_INTM);
  518. I2C_DBG("i2c irq!!\n");
  519. if ((intst & I2C_INTST_TXABT) && (intmsk & I2C_INTM_MTXABT))
  520. {
  521. I2C_DBG("%s %d, I2C transfer error, ABORT interrupt\n", __func__, __LINE__);
  522. goto END_TRSF_IRQ_HND;
  523. }
  524. if ((intst & I2C_INTST_ISTP) && (intmsk & I2C_INTM_MISTP))
  525. {
  526. i2c_readl(i2c, I2C_CSTP); /* clear STP bit */
  527. if (i2c->len == 0)
  528. goto END_TRSF_IRQ_HND;
  529. }
  530. if ((intmsk & I2C_INTM_MTXEMP) && (intst & I2C_INTST_TXEMP))
  531. {
  532. if (!i2c->len)
  533. {
  534. if (i2c->w_end_type == MSG_END_REPEAT_START)
  535. {
  536. goto END_TRSF_IRQ_HND;
  537. }
  538. else
  539. {
  540. tmp = i2c_readl(i2c, I2C_INTM);
  541. tmp &= ~I2C_INTM_MTXEMP;
  542. i2c_writel(i2c, I2C_INTM, tmp);
  543. }
  544. }
  545. else
  546. {
  547. while ((i2c->len > 0) && (i2c_readl(i2c, I2C_STA) & I2C_STA_TFNF))
  548. {
  549. tmp = *i2c->wbuf++;
  550. if (i2c->len == 1)
  551. {
  552. if (i2c->w_end_type == MSG_END_STOP)
  553. tmp |= I2C_DC_STP;
  554. }
  555. i2c_writel(i2c, I2C_DC, tmp);
  556. i2c->len -= 1;
  557. }
  558. if (i2c->len == 0)
  559. {
  560. i2c_writel(i2c, I2C_TXTL, 0);
  561. }
  562. }
  563. }
  564. if ((intst & I2C_INTST_RXFL) && (intmsk & I2C_INTM_MRXFL))
  565. {
  566. I2C_DBG("I2C RXFL\n");
  567. while ((i2c_readl(i2c, I2C_STA) & I2C_STA_RFNE) && (i2c->len > 0))
  568. {
  569. tmp = i2c_readl(i2c, I2C_DC) & 0xff;
  570. *i2c->rbuf++ = tmp;
  571. i2c->len--;
  572. }
  573. if (i2c->len == 0)
  574. {
  575. goto END_RECE_IRQ_HND;
  576. }
  577. if (i2c->len <= I2C_FIFO_LEN)
  578. {
  579. i2c_writel(i2c, I2C_RXTL, i2c->len - 1);
  580. }
  581. if (i2c_send_rcmd(i2c))
  582. {
  583. I2C_DBG("%s %d, I2C controller has BUG, RXFLR or TXFLR can not clear\n", __func__, __LINE__);
  584. }
  585. }
  586. if ((intst & I2C_INTST_RXOF) && (intmsk & I2C_INTM_MRXOF))
  587. {
  588. I2C_DBG("%s %d, I2C transfer error, RXFIFO over full\n", __func__, __LINE__);
  589. i2c_readl(i2c, I2C_CRXOF); /* clear RXOF bit */
  590. }
  591. if ((intst & I2C_INTST_TXOF) && (intmsk & I2C_INTM_MTXOF))
  592. {
  593. I2C_DBG("%s %d, I2C transfer error, TXFIFO over full\n", __func__, __LINE__);
  594. i2c_readl(i2c, I2C_CTXOF); /* clear TXOF bit */
  595. goto END_TRSF_IRQ_HND;
  596. }
  597. return ;
  598. END_RECE_IRQ_HND:
  599. END_TRSF_IRQ_HND:
  600. i2c_writel(i2c, I2C_INTM, 0);
  601. rt_completion_done(&i2c->completion);
  602. }
  603. int rt_hw_i2c_init(void)
  604. {
  605. struct ingenic_i2c_bus *i2c;
  606. struct rt_i2c_bus_device *i2c_bus;
  607. #ifdef RT_USING_I2C0
  608. {
  609. i2c = &ingenic_i2c0;
  610. rt_memset((void *)i2c, 0, sizeof(struct ingenic_i2c_bus));
  611. i2c->hwaddr = I2C0_BASE;
  612. i2c->irqno = IRQ_I2C0;
  613. /* Set PB23 PB24 in func0 (I2C0) */
  614. gpio_set_func(GPIO_PORT_B, GPIO_Pin_24, GPIO_FUNC_0);
  615. gpio_set_func(GPIO_PORT_B, GPIO_Pin_23, GPIO_FUNC_0);
  616. /* enable clock */
  617. i2c->clk = clk_get("i2c0");
  618. clk_enable(i2c->clk);
  619. i2c_bus = &i2c->parent;
  620. i2c_bus->ops = &i2c_ops;
  621. rt_i2c_bus_device_register(i2c_bus, "i2c0");
  622. ingenic_i2c_set_speed(i2c, 400 * 1000);
  623. /* reset I2C */
  624. i2c_writel(i2c, I2C_CTRL, i2c_readl(i2c, I2C_CTRL) | I2C_CTRL_REST);
  625. i2c_writel(i2c, I2C_INTM, 0x0);
  626. ingenic_i2c_enable(i2c);
  627. clk_disable(i2c->clk);
  628. /* install interrupt */
  629. rt_hw_interrupt_install(i2c->irqno, i2c_irq_handler, i2c, "i2c0");
  630. rt_hw_interrupt_umask(i2c->irqno);
  631. }
  632. #endif
  633. #ifdef RT_USING_I2C1
  634. {
  635. i2c = &ingenic_i2c1;
  636. rt_memset((void *)i2c, 0, sizeof(struct ingenic_i2c_bus));
  637. i2c->hwaddr = I2C1_BASE;
  638. i2c->irqno = IRQ_I2C1;
  639. /* Set PC26 PC27 in func0 (I2C1) */
  640. gpio_set_func(GPIO_PORT_C, GPIO_Pin_26, GPIO_FUNC_0);
  641. gpio_set_func(GPIO_PORT_C, GPIO_Pin_27, GPIO_FUNC_0);
  642. /* enable clock */
  643. i2c->clk = clk_get("i2c1");
  644. clk_enable(i2c->clk);
  645. i2c_bus = &i2c->parent;
  646. i2c_bus->ops = &i2c_ops;
  647. rt_i2c_bus_device_register(i2c_bus, "i2c1");
  648. ingenic_i2c_set_speed(i2c, 400 * 1000);
  649. /* reset I2C */
  650. i2c_writel(i2c, I2C_CTRL, i2c_readl(i2c, I2C_CTRL) | I2C_CTRL_REST);
  651. i2c_writel(i2c, I2C_INTM, 0x0);
  652. ingenic_i2c_enable(i2c);
  653. clk_disable(i2c->clk);
  654. /* install interrupt */
  655. rt_hw_interrupt_install(i2c->irqno, i2c_irq_handler, i2c, "i2c1");
  656. rt_hw_interrupt_umask(i2c->irqno);
  657. }
  658. #endif
  659. #ifdef RT_USING_I2C2
  660. {
  661. i2c = &ingenic_i2c2;
  662. rt_memset((void *)i2c, 0, sizeof(struct ingenic_i2c_bus));
  663. i2c->hwaddr = I2C2_BASE;
  664. i2c->irqno = IRQ_I2C2;
  665. /* Set PC26 PC27 in func0 (I2C1) */
  666. gpio_set_func(GPIO_PORT_D, GPIO_Pin_0, GPIO_FUNC_1);
  667. gpio_set_func(GPIO_PORT_D, GPIO_Pin_1, GPIO_FUNC_1);
  668. /* enable clock */
  669. i2c->clk = clk_get("i2c2");
  670. clk_enable(i2c->clk);
  671. i2c_bus = &i2c->parent;
  672. i2c_bus->ops = &i2c_ops;
  673. rt_i2c_bus_device_register(i2c_bus, "i2c2");
  674. ingenic_i2c_set_speed(i2c, 400 * 1000);
  675. /* reset I2C */
  676. i2c_writel(i2c, I2C_CTRL, i2c_readl(i2c, I2C_CTRL) | I2C_CTRL_REST);
  677. i2c_writel(i2c, I2C_INTM, 0x0);
  678. ingenic_i2c_enable(i2c);
  679. clk_disable(i2c->clk);
  680. /* install interrupt */
  681. rt_hw_interrupt_install(i2c->irqno, i2c_irq_handler, i2c, "i2c2");
  682. rt_hw_interrupt_umask(i2c->irqno);
  683. }
  684. #endif
  685. return 0;
  686. }
  687. INIT_BOARD_EXPORT(rt_hw_i2c_init);