drv_hw_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include "drv_hw_i2c.h"
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #ifdef RT_USING_I2C
  14. #define DBG_TAG "drv.i2c"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #define false 0
  18. #define true 1
  19. struct _i2c_bus
  20. {
  21. struct rt_i2c_bus_device parent;
  22. uint8_t i2c_id;
  23. char *device_name;
  24. };
  25. static struct _i2c_bus _i2c_obj[] =
  26. {
  27. #ifdef BSP_USING_I2C0
  28. {
  29. .i2c_id = I2C0,
  30. .device_name = "i2c0",
  31. },
  32. #endif /* BSP_USING_I2C0 */
  33. #ifdef BSP_USING_I2C1
  34. {
  35. .i2c_id = I2C1,
  36. .device_name = "i2c1",
  37. },
  38. #endif /* BSP_USING_I2C1 */
  39. };
  40. static struct i2c_regs *get_i2c_base(uint8_t i2c_id)
  41. {
  42. struct i2c_regs *i2c_base = NULL;
  43. switch (i2c_id) {
  44. case I2C0:
  45. i2c_base = (struct i2c_regs *)I2C0_BASE;
  46. break;
  47. case I2C1:
  48. i2c_base = (struct i2c_regs *)I2C1_BASE;
  49. break;
  50. case I2C2:
  51. i2c_base = (struct i2c_regs *)I2C2_BASE;
  52. break;
  53. case I2C3:
  54. i2c_base = (struct i2c_regs *)I2C3_BASE;
  55. break;
  56. case I2C4:
  57. i2c_base = (struct i2c_regs *)I2C4_BASE;
  58. break;
  59. }
  60. return i2c_base;
  61. }
  62. static uint32_t get_i2c_intr(uint8_t i2c_id)
  63. {
  64. uint32_t i2c_intr = 0;
  65. switch (i2c_id) {
  66. case I2C0:
  67. i2c_intr = I2C0_IRQ;
  68. break;
  69. case I2C1:
  70. i2c_intr = I2C1_IRQ;
  71. break;
  72. case I2C2:
  73. i2c_intr = I2C2_IRQ;
  74. break;
  75. case I2C3:
  76. i2c_intr = I2C3_IRQ;
  77. break;
  78. case I2C4:
  79. i2c_intr = I2C4_IRQ;
  80. break;
  81. }
  82. return i2c_intr;
  83. }
  84. void i2c_write_cmd_data(struct i2c_regs *i2c, uint16_t value)
  85. {
  86. mmio_write_32((uintptr_t)&i2c->ic_cmd_data, value);
  87. }
  88. static void i2c_enable(struct i2c_regs *i2c, uint8_t enable)
  89. {
  90. uint32_t ena = enable ? IC_ENABLE : 0;
  91. int timeout = 100;
  92. do {
  93. mmio_write_32((uintptr_t)&i2c->ic_enable, ena);
  94. if ((mmio_read_32((uintptr_t)&i2c->ic_enable_status) & IC_ENABLE) == ena)
  95. return;
  96. /*
  97. * Wait 10 times the signaling period of the highest I2C
  98. * transfer supported by the driver (for 400KHz this is
  99. * 25us) as described in the DesignWare I2C databook.
  100. */
  101. rt_hw_us_delay(25);
  102. } while (timeout--);
  103. LOG_I("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
  104. }
  105. static void i2c_disable(struct i2c_regs *i2c)
  106. {
  107. int timeout = 100;
  108. do {
  109. mmio_write_32((uintptr_t)&i2c->ic_enable, 0x0);
  110. if ((mmio_read_32((uintptr_t)&i2c->ic_enable_status) & IC_ENABLE) == 0x0)
  111. return;
  112. /*
  113. * Wait 10 times the signaling period of the highest I2C
  114. * transfer supported by the driver (for 400KHz this is
  115. * 25us) as described in the DesignWare I2C databook.
  116. */
  117. rt_hw_us_delay(25);
  118. } while (timeout--);
  119. LOG_I("timeout in disabling I2C adapter\n");
  120. }
  121. /*
  122. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  123. *
  124. * Flushes the i2c RX FIFO
  125. */
  126. static void i2c_flush_rxfifo(struct i2c_regs *i2c)
  127. {
  128. while (mmio_read_32((uintptr_t)&i2c->ic_status) & IC_STATUS_RFNE)
  129. mmio_read_32((uintptr_t)&i2c->ic_cmd_data);
  130. }
  131. /*
  132. * i2c_wait_for_bb - Waits for bus busy
  133. *
  134. * Waits for bus busy
  135. */
  136. static int i2c_wait_for_bb(struct i2c_regs *i2c)
  137. {
  138. uint16_t timeout = 0;
  139. while ((mmio_read_32((uintptr_t)&i2c->ic_status) & IC_STATUS_MA) ||
  140. !(mmio_read_32((uintptr_t)&i2c->ic_status) & IC_STATUS_TFE)) {
  141. /* Evaluate timeout */
  142. rt_hw_us_delay(5);
  143. timeout++;
  144. if (timeout > 200) /* exceed 1 ms */
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. /*
  150. * i2c_setaddress - Sets the target slave address
  151. * @i2c_addr: target i2c address
  152. *
  153. * Sets the target slave address.
  154. */
  155. static void i2c_setaddress(struct i2c_regs *i2c, uint16_t i2c_addr)
  156. {
  157. /* Disable i2c */
  158. i2c_enable(i2c, false);
  159. mmio_write_32((uintptr_t)&i2c->ic_tar, i2c_addr);
  160. /* Enable i2c */
  161. i2c_enable(i2c, true);
  162. }
  163. static int i2c_xfer_init(struct i2c_regs *i2c, uint16_t chip, uint16_t addr, uint16_t alen)
  164. {
  165. if (i2c_wait_for_bb(i2c))
  166. return 1;
  167. i2c_setaddress(i2c, chip);
  168. while (alen) {
  169. alen--;
  170. /* high byte address going out first */
  171. i2c_write_cmd_data(i2c, (addr >> (alen * 8)) & 0xff); // TODO
  172. //mmio_write_32((uintptr_t)&i2c_base->ic_cmd_data, (addr >> (alen * 8)) & 0xff);
  173. }
  174. return 0;
  175. }
  176. static int i2c_xfer_finish(struct i2c_regs *i2c)
  177. {
  178. uint16_t timeout = 0;
  179. while (1) {
  180. if ((mmio_read_32((uintptr_t)&i2c->ic_raw_intr_stat) & IC_STOP_DET)) {
  181. mmio_read_32((uintptr_t)&i2c->ic_clr_stop_det);
  182. break;
  183. } else {
  184. timeout++;
  185. rt_hw_us_delay(5);
  186. if (timeout > I2C_STOPDET_TO * 100) {
  187. LOG_I("%s, tiemout\n", __func__);
  188. break;
  189. }
  190. }
  191. }
  192. if (i2c_wait_for_bb(i2c))
  193. return 1;
  194. i2c_flush_rxfifo(i2c);
  195. return 0;
  196. }
  197. /*
  198. * i2c_read - Read from i2c memory
  199. * @chip: target i2c address
  200. * @addr: address to read from
  201. * @alen:
  202. * @buffer: buffer for read data
  203. * @len: no of bytes to be read
  204. *
  205. * Read from i2c memory.
  206. */
  207. static int hal_i2c_read(uint8_t i2c_id, uint8_t dev, uint16_t addr, uint16_t alen, uint8_t *buffer, uint16_t len)
  208. {
  209. unsigned int active = 0;
  210. unsigned int time_count = 0;
  211. struct i2c_regs *i2c;
  212. int ret = 0;
  213. i2c = get_i2c_base(i2c_id);
  214. i2c_enable(i2c, true);
  215. if (i2c_xfer_init(i2c, dev, addr, alen))
  216. return 1;
  217. while (len) {
  218. if (!active) {
  219. /*
  220. * Avoid writing to ic_cmd_data multiple times
  221. * in case this loop spins too quickly and the
  222. * ic_status RFNE bit isn't set after the first
  223. * write. Subsequent writes to ic_cmd_data can
  224. * trigger spurious i2c transfer.
  225. */
  226. i2c_write_cmd_data(i2c, (dev <<1) | BIT_I2C_CMD_DATA_READ_BIT | BIT_I2C_CMD_DATA_STOP_BIT);
  227. //mmio_write_32((uintptr_t)&i2c_base->ic_cmd_data, (dev <<1) | BIT_I2C_CMD_DATA_READ_BIT | BIT_I2C_CMD_DATA_STOP_BIT);
  228. active = 1;
  229. }
  230. if (mmio_read_32((uintptr_t)&i2c->ic_raw_intr_stat) & BIT_I2C_INT_RX_FULL) {
  231. *buffer++ = (uint8_t)mmio_read_32((uintptr_t)&i2c->ic_cmd_data);
  232. len--;
  233. time_count = 0;
  234. active = 0;
  235. }
  236. else {
  237. rt_hw_us_delay(5);
  238. time_count++;
  239. if (time_count >= I2C_BYTE_TO * 100)
  240. return 1;
  241. }
  242. }
  243. ret = i2c_xfer_finish(i2c);
  244. i2c_disable(i2c);
  245. return ret;
  246. }
  247. /*
  248. * i2c_write - Write to i2c memory
  249. * @chip: target i2c address
  250. * @addr: address to read from
  251. * @alen:
  252. * @buffer: buffer for read data
  253. * @len: no of bytes to be read
  254. *
  255. * Write to i2c memory.
  256. */
  257. static int hal_i2c_write(uint8_t i2c_id, uint8_t dev, uint16_t addr, uint16_t alen, uint8_t *buffer, uint16_t len)
  258. {
  259. struct i2c_regs *i2c;
  260. int ret = 0;
  261. i2c = get_i2c_base(i2c_id);
  262. i2c_enable(i2c, true);
  263. if (i2c_xfer_init(i2c, dev, addr, alen))
  264. return 1;
  265. while (len) {
  266. if (i2c->ic_status & IC_STATUS_TFNF) {
  267. if (--len == 0) {
  268. i2c_write_cmd_data(i2c, *buffer | IC_STOP);
  269. //mmio_write_32((uintptr_t)&i2c_base->ic_cmd_data, *buffer | IC_STOP);
  270. } else {
  271. i2c_write_cmd_data(i2c, *buffer);
  272. //mmio_write_32((uintptr_t)&i2c_base->ic_cmd_data, *buffer);
  273. }
  274. buffer++;
  275. } else
  276. LOG_I("len=%d, ic status is not TFNF\n", len);
  277. }
  278. ret = i2c_xfer_finish(i2c);
  279. i2c_disable(i2c);
  280. return ret;
  281. }
  282. /*
  283. * hal_i2c_set_bus_speed - Set the i2c speed
  284. * @speed: required i2c speed
  285. *
  286. * Set the i2c speed.
  287. */
  288. static void i2c_set_bus_speed(struct i2c_regs *i2c, unsigned int speed)
  289. {
  290. unsigned int cntl;
  291. unsigned int hcnt, lcnt;
  292. int i2c_spd;
  293. if (speed > I2C_FAST_SPEED)
  294. i2c_spd = IC_SPEED_MODE_MAX;
  295. else if ((speed <= I2C_FAST_SPEED) && (speed > I2C_STANDARD_SPEED))
  296. i2c_spd = IC_SPEED_MODE_FAST;
  297. else
  298. i2c_spd = IC_SPEED_MODE_STANDARD;
  299. /* to set speed cltr must be disabled */
  300. i2c_enable(i2c, false);
  301. cntl = (mmio_read_32((uintptr_t)&i2c->ic_con) & (~IC_CON_SPD_MSK));
  302. switch (i2c_spd) {
  303. case IC_SPEED_MODE_MAX:
  304. cntl |= IC_CON_SPD_HS;
  305. //hcnt = (u16)(((IC_CLK * MIN_HS100pF_SCL_HIGHTIME) / 1000) - 8);
  306. /* 7 = 6+1 == MIN LEN +IC_FS_SPKLEN */
  307. //lcnt = (u16)(((IC_CLK * MIN_HS100pF_SCL_LOWTIME) / 1000) - 1);
  308. hcnt = 6;
  309. lcnt = 8;
  310. mmio_write_32((uintptr_t)&i2c->ic_hs_scl_hcnt, hcnt);
  311. mmio_write_32((uintptr_t)&i2c->ic_hs_scl_lcnt, lcnt);
  312. break;
  313. case IC_SPEED_MODE_STANDARD:
  314. cntl |= IC_CON_SPD_SS;
  315. hcnt = (uint16_t)(((IC_CLK * MIN_SS_SCL_HIGHTIME) / 1000) - 7);
  316. lcnt = (uint16_t)(((IC_CLK * MIN_SS_SCL_LOWTIME) / 1000) - 1);
  317. mmio_write_32((uintptr_t)&i2c->ic_ss_scl_hcnt, hcnt);
  318. mmio_write_32((uintptr_t)&i2c->ic_ss_scl_lcnt, lcnt);
  319. break;
  320. case IC_SPEED_MODE_FAST:
  321. default:
  322. cntl |= IC_CON_SPD_FS;
  323. hcnt = (uint16_t)(((IC_CLK * MIN_FS_SCL_HIGHTIME) / 1000) - 7);
  324. lcnt = (uint16_t)(((IC_CLK * MIN_FS_SCL_LOWTIME) / 1000) - 1);
  325. mmio_write_32((uintptr_t)&i2c->ic_fs_scl_hcnt, hcnt);
  326. mmio_write_32((uintptr_t)&i2c->ic_fs_scl_lcnt, lcnt);
  327. break;
  328. }
  329. mmio_write_32((uintptr_t)&i2c->ic_con, cntl);
  330. /* Enable back i2c now speed set */
  331. i2c_enable(i2c, true);
  332. }
  333. /*
  334. * __hal_i2c_init - Init function
  335. * @speed: required i2c speed
  336. * @slaveaddr: slave address for the device
  337. *
  338. * Initialization function.
  339. */
  340. static void hal_i2c_init(uint8_t i2c_id)
  341. {
  342. struct i2c_regs *i2c;
  343. uint32_t i2c_intr;
  344. LOG_I("%s, i2c-%d\n", __func__, i2c_id);
  345. /* Disable i2c */
  346. //Need to acquire lock here
  347. i2c = get_i2c_base(i2c_id);
  348. i2c_intr = get_i2c_intr(i2c_id);
  349. // request_irq(i2c_intr, i2c_dw_isr, 0, "IC2_INTR int", &dw_i2c[i2c_id]);
  350. i2c_enable(i2c, false);
  351. mmio_write_32((uintptr_t)&i2c->ic_con, (IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM | IC_CON_RE));
  352. mmio_write_32((uintptr_t)&i2c->ic_rx_tl, IC_RX_TL);
  353. mmio_write_32((uintptr_t)&i2c->ic_tx_tl, IC_TX_TL);
  354. mmio_write_32((uintptr_t)&i2c->ic_intr_mask, 0x0);
  355. i2c_set_bus_speed(i2c, I2C_SPEED);
  356. //mmio_write_32((uintptr_t)&i2c->ic_sar, slaveaddr);
  357. /* Enable i2c */
  358. i2c_enable(i2c, false);
  359. //Need to release lock here
  360. }
  361. static rt_ssize_t _master_xfer(struct rt_i2c_bus_device *bus,
  362. struct rt_i2c_msg msgs[],
  363. rt_uint32_t num)
  364. {
  365. struct rt_i2c_msg *msg;
  366. rt_uint32_t i;
  367. rt_ssize_t ret = -RT_ERROR;
  368. struct _i2c_bus *i2c = (struct _i2c_bus *)bus;
  369. for (i = 0; i < num; i++)
  370. {
  371. msg = &msgs[i];
  372. if (msg->flags & RT_I2C_RD)
  373. {
  374. hal_i2c_read(i2c->i2c_id, msg->addr, RT_NULL, 1, msg->buf, msg->len);
  375. }
  376. else
  377. {
  378. hal_i2c_write(i2c->i2c_id, msg->addr, RT_NULL, 1, msg->buf, msg->len);
  379. }
  380. }
  381. return ret;
  382. }
  383. static void rt_hw_i2c_isr(int irqno, void *param)
  384. {
  385. uint32_t stat, enabled;
  386. struct i2c_regs *i2c = (struct i2c_regs *)param;
  387. enabled = mmio_read_32((uintptr_t)&i2c->ic_enable);
  388. stat = mmio_read_32((uintptr_t)&i2c->ic_intr_stat);
  389. LOG_I("i2c interrupt stat: 0x%08x", stat);
  390. }
  391. static const struct rt_i2c_bus_device_ops i2c_ops =
  392. {
  393. .master_xfer = _master_xfer,
  394. .slave_xfer = RT_NULL,
  395. .i2c_bus_control = RT_NULL
  396. };
  397. int rt_hw_i2c_init(void)
  398. {
  399. int result = RT_EOK;
  400. #ifdef BSP_USING_I2C0
  401. PINMUX_CONFIG(IIC0_SCL, IIC0_SCL);
  402. PINMUX_CONFIG(IIC0_SDA, IIC0_SDA);
  403. #endif /* BSP_USING_I2C0 */
  404. #ifdef BSP_USING_I2C1
  405. PINMUX_CONFIG(PAD_MIPIRX1P, IIC1_SDA);
  406. PINMUX_CONFIG(PAD_MIPIRX0N, IIC1_SCL);
  407. #endif /* BSP_USING_I2C1 */
  408. for (rt_size_t i = 0; i < sizeof(_i2c_obj) / sizeof(struct _i2c_bus); i++)
  409. {
  410. hal_i2c_init(_i2c_obj->i2c_id);
  411. _i2c_obj[i].parent.ops = &i2c_ops;
  412. /* register i2c device */
  413. if (rt_i2c_bus_device_register(&_i2c_obj[i].parent, _i2c_obj[i].device_name) == RT_EOK)
  414. {
  415. LOG_D("%s init success", _i2c_obj[i].device_name);
  416. }
  417. else
  418. {
  419. LOG_E("%s register failed", _i2c_obj[i].device_name);
  420. result = -RT_ERROR;
  421. }
  422. uint32_t irqno = get_i2c_intr(_i2c_obj[i].i2c_id);
  423. struct i2c_regs *_i2c = get_i2c_base(_i2c_obj[i].i2c_id);
  424. rt_hw_interrupt_install(irqno, rt_hw_i2c_isr, _i2c, _i2c_obj[i].device_name);
  425. }
  426. return result;
  427. }
  428. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  429. #endif /* RT_USING_I2C */