lpc32xx_i2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * LPC32xx I2C interface driver
  3. *
  4. * (C) Copyright 2014-2015 DENX Software Engineering GmbH
  5. * Written-by: Albert ARIBAUD - 3ADEV <albert.aribaud@3adev.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <i2c.h>
  12. #include <asm/errno.h>
  13. #include <asm/arch/clk.h>
  14. /*
  15. * Provide default speed and slave if target did not
  16. */
  17. #if !defined(CONFIG_SYS_I2C_LPC32XX_SPEED)
  18. #define CONFIG_SYS_I2C_LPC32XX_SPEED 350000
  19. #endif
  20. #if !defined(CONFIG_SYS_I2C_LPC32XX_SLAVE)
  21. #define CONFIG_SYS_I2C_LPC32XX_SLAVE 0
  22. #endif
  23. /* i2c register set */
  24. struct lpc32xx_i2c_registers {
  25. union {
  26. u32 rx;
  27. u32 tx;
  28. };
  29. u32 stat;
  30. u32 ctrl;
  31. u32 clk_hi;
  32. u32 clk_lo;
  33. u32 adr;
  34. u32 rxfl;
  35. u32 txfl;
  36. u32 rxb;
  37. u32 txb;
  38. u32 stx;
  39. u32 stxfl;
  40. };
  41. /* TX register fields */
  42. #define LPC32XX_I2C_TX_START 0x00000100
  43. #define LPC32XX_I2C_TX_STOP 0x00000200
  44. /* Control register values */
  45. #define LPC32XX_I2C_SOFT_RESET 0x00000100
  46. /* Status register values */
  47. #define LPC32XX_I2C_STAT_TFF 0x00000400
  48. #define LPC32XX_I2C_STAT_RFE 0x00000200
  49. #define LPC32XX_I2C_STAT_DRMI 0x00000008
  50. #define LPC32XX_I2C_STAT_NAI 0x00000004
  51. #define LPC32XX_I2C_STAT_TDI 0x00000001
  52. static struct lpc32xx_i2c_registers *lpc32xx_i2c[] = {
  53. (struct lpc32xx_i2c_registers *)I2C1_BASE,
  54. (struct lpc32xx_i2c_registers *)I2C2_BASE,
  55. (struct lpc32xx_i2c_registers *)(USB_BASE + 0x300)
  56. };
  57. /* Set I2C bus speed */
  58. static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
  59. unsigned int speed)
  60. {
  61. int half_period;
  62. if (speed == 0)
  63. return -EINVAL;
  64. /* OTG I2C clock source and CLK registers are different */
  65. if (adap->hwadapnr == 2) {
  66. half_period = (get_periph_clk_rate() / speed) / 2;
  67. if (half_period > 0xFF)
  68. return -EINVAL;
  69. } else {
  70. half_period = (get_hclk_clk_rate() / speed) / 2;
  71. if (half_period > 0x3FF)
  72. return -EINVAL;
  73. }
  74. writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_hi);
  75. writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_lo);
  76. return 0;
  77. }
  78. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  79. static void _i2c_init(struct i2c_adapter *adap,
  80. int requested_speed, int slaveadd)
  81. {
  82. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  83. /* soft reset (auto-clears) */
  84. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  85. /* set HI and LO periods for half of the default speed */
  86. lpc32xx_i2c_set_bus_speed(adap, requested_speed);
  87. }
  88. /* I2C probe called by cmd_i2c when doing 'i2c probe'. */
  89. static int lpc32xx_i2c_probe(struct i2c_adapter *adap, u8 dev)
  90. {
  91. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  92. int stat;
  93. /* Soft-reset the controller */
  94. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  95. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  96. ;
  97. /* Addre slave for write with start before and stop after */
  98. writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
  99. &i2c->tx);
  100. /* wait for end of transation */
  101. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  102. ;
  103. /* was there no acknowledge? */
  104. return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
  105. }
  106. /*
  107. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  108. * Begin write, send address byte(s), begin read, receive data bytes, end.
  109. */
  110. static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
  111. int alen, u8 *data, int length)
  112. {
  113. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  114. int stat, wlen;
  115. /* Soft-reset the controller */
  116. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  117. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  118. ;
  119. /* do we need to write an address at all? */
  120. if (alen) {
  121. /* Address slave in write mode */
  122. writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  123. /* write address bytes */
  124. while (alen--) {
  125. /* compute address byte + stop for the last one */
  126. int a = (addr >> (8 * alen)) & 0xff;
  127. if (!alen)
  128. a |= LPC32XX_I2C_TX_STOP;
  129. /* Send address byte */
  130. writel(a, &i2c->tx);
  131. }
  132. /* wait for end of transation */
  133. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  134. ;
  135. /* clear end-of-transaction flag */
  136. writel(1, &i2c->stat);
  137. }
  138. /* do we have to read data at all? */
  139. if (length) {
  140. /* Address slave in read mode */
  141. writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  142. wlen = length;
  143. /* get data */
  144. while (length | wlen) {
  145. /* read status for TFF and RFE */
  146. stat = readl(&i2c->stat);
  147. /* must we, can we write a trigger byte? */
  148. if ((wlen > 0)
  149. & (!(stat & LPC32XX_I2C_STAT_TFF))) {
  150. wlen--;
  151. /* write trigger byte + stop if last */
  152. writel(wlen ? 0 :
  153. LPC32XX_I2C_TX_STOP, &i2c->tx);
  154. }
  155. /* must we, can we read a data byte? */
  156. if ((length > 0)
  157. & (!(stat & LPC32XX_I2C_STAT_RFE))) {
  158. length--;
  159. /* read byte */
  160. *(data++) = readl(&i2c->rx);
  161. }
  162. }
  163. /* wait for end of transation */
  164. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  165. ;
  166. /* clear end-of-transaction flag */
  167. writel(1, &i2c->stat);
  168. }
  169. /* success */
  170. return 0;
  171. }
  172. /*
  173. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  174. * Begin write, send address byte(s), send data bytes, end.
  175. */
  176. static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
  177. int alen, u8 *data, int length)
  178. {
  179. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  180. int stat;
  181. /* Soft-reset the controller */
  182. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  183. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  184. ;
  185. /* do we need to write anything at all? */
  186. if (alen | length)
  187. /* Address slave in write mode */
  188. writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  189. else
  190. return 0;
  191. /* write address bytes */
  192. while (alen) {
  193. /* wait for transmit fifo not full */
  194. stat = readl(&i2c->stat);
  195. if (!(stat & LPC32XX_I2C_STAT_TFF)) {
  196. alen--;
  197. int a = (addr >> (8 * alen)) & 0xff;
  198. if (!(alen | length))
  199. a |= LPC32XX_I2C_TX_STOP;
  200. /* Send address byte */
  201. writel(a, &i2c->tx);
  202. }
  203. }
  204. while (length) {
  205. /* wait for transmit fifo not full */
  206. stat = readl(&i2c->stat);
  207. if (!(stat & LPC32XX_I2C_STAT_TFF)) {
  208. /* compute data byte, add stop if length==0 */
  209. length--;
  210. int d = *(data++);
  211. if (!length)
  212. d |= LPC32XX_I2C_TX_STOP;
  213. /* Send data byte */
  214. writel(d, &i2c->tx);
  215. }
  216. }
  217. /* wait for end of transation */
  218. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  219. ;
  220. /* clear end-of-transaction flag */
  221. writel(1, &i2c->stat);
  222. return 0;
  223. }
  224. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, _i2c_init, lpc32xx_i2c_probe,
  225. lpc32xx_i2c_read, lpc32xx_i2c_write,
  226. lpc32xx_i2c_set_bus_speed,
  227. CONFIG_SYS_I2C_LPC32XX_SPEED,
  228. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  229. 0)
  230. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, _i2c_init, lpc32xx_i2c_probe,
  231. lpc32xx_i2c_read, lpc32xx_i2c_write,
  232. lpc32xx_i2c_set_bus_speed,
  233. CONFIG_SYS_I2C_LPC32XX_SPEED,
  234. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  235. 1)
  236. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, _i2c_init, NULL,
  237. lpc32xx_i2c_read, lpc32xx_i2c_write,
  238. lpc32xx_i2c_set_bus_speed,
  239. 100000,
  240. 0,
  241. 2)