lpc32xx_i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * LPC32xx I2C interface driver
  3. *
  4. * (C) Copyright 2014 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. };
  56. /* Set I2C bus speed */
  57. static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
  58. unsigned int speed)
  59. {
  60. int half_period;
  61. if (speed == 0)
  62. return -EINVAL;
  63. half_period = (105000000 / speed) / 2;
  64. if ((half_period > 255) || (half_period < 0))
  65. return -EINVAL;
  66. writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_hi);
  67. writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_lo);
  68. return 0;
  69. }
  70. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  71. static void _i2c_init(struct i2c_adapter *adap,
  72. int requested_speed, int slaveadd)
  73. {
  74. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  75. /* soft reset (auto-clears) */
  76. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  77. /* set HI and LO periods for about 350 kHz */
  78. lpc32xx_i2c_set_bus_speed(adap, requested_speed);
  79. }
  80. /* I2C probe called by cmd_i2c when doing 'i2c probe'. */
  81. static int lpc32xx_i2c_probe(struct i2c_adapter *adap, u8 dev)
  82. {
  83. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  84. int stat;
  85. /* Soft-reset the controller */
  86. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  87. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  88. ;
  89. /* Addre slave for write with start before and stop after */
  90. writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
  91. &i2c->tx);
  92. /* wait for end of transation */
  93. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  94. ;
  95. /* was there no acknowledge? */
  96. return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
  97. }
  98. /*
  99. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  100. * Begin write, send address byte(s), begin read, receive data bytes, end.
  101. */
  102. static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
  103. int alen, u8 *data, int length)
  104. {
  105. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  106. int stat, wlen;
  107. /* Soft-reset the controller */
  108. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  109. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  110. ;
  111. /* do we need to write an address at all? */
  112. if (alen) {
  113. /* Address slave in write mode */
  114. writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  115. /* write address bytes */
  116. while (alen--) {
  117. /* compute address byte + stop for the last one */
  118. int a = (addr >> (8 * alen)) & 0xff;
  119. if (!alen)
  120. a |= LPC32XX_I2C_TX_STOP;
  121. /* Send address byte */
  122. writel(a, &i2c->tx);
  123. }
  124. /* wait for end of transation */
  125. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  126. ;
  127. /* clear end-of-transaction flag */
  128. writel(1, &i2c->stat);
  129. }
  130. /* do we have to read data at all? */
  131. if (length) {
  132. /* Address slave in read mode */
  133. writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  134. wlen = length;
  135. /* get data */
  136. while (length | wlen) {
  137. /* read status for TFF and RFE */
  138. stat = readl(&i2c->stat);
  139. /* must we, can we write a trigger byte? */
  140. if ((wlen > 0)
  141. & (!(stat & LPC32XX_I2C_STAT_TFF))) {
  142. wlen--;
  143. /* write trigger byte + stop if last */
  144. writel(wlen ? 0 :
  145. LPC32XX_I2C_TX_STOP, &i2c->tx);
  146. }
  147. /* must we, can we read a data byte? */
  148. if ((length > 0)
  149. & (!(stat & LPC32XX_I2C_STAT_RFE))) {
  150. length--;
  151. /* read byte */
  152. *(data++) = readl(&i2c->rx);
  153. }
  154. }
  155. }
  156. /* wait for end of transation */
  157. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  158. ;
  159. /* clear end-of-transaction flag */
  160. writel(1, &i2c->stat);
  161. /* success */
  162. return 0;
  163. }
  164. /*
  165. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  166. * Begin write, send address byte(s), send data bytes, end.
  167. */
  168. static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
  169. int alen, u8 *data, int length)
  170. {
  171. struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
  172. int stat;
  173. /* Soft-reset the controller */
  174. writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
  175. while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
  176. ;
  177. /* do we need to write anything at all? */
  178. if (alen | length)
  179. /* Address slave in write mode */
  180. writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
  181. /* write address bytes */
  182. while (alen) {
  183. /* wait for transmit fifo not full */
  184. stat = readl(&i2c->stat);
  185. if (!(stat & LPC32XX_I2C_STAT_TFF)) {
  186. alen--;
  187. int a = (addr >> (8 * alen)) & 0xff;
  188. if (!(alen | length))
  189. a |= LPC32XX_I2C_TX_STOP;
  190. /* Send address byte */
  191. writel(a, &i2c->tx);
  192. }
  193. }
  194. while (length) {
  195. /* wait for transmit fifo not full */
  196. stat = readl(&i2c->stat);
  197. if (!(stat & LPC32XX_I2C_STAT_TFF)) {
  198. /* compute data byte, add stop if length==0 */
  199. length--;
  200. int d = *(data++);
  201. if (!length)
  202. d |= LPC32XX_I2C_TX_STOP;
  203. /* Send data byte */
  204. writel(d, &i2c->tx);
  205. }
  206. }
  207. /* wait for end of transation */
  208. while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
  209. ;
  210. /* clear end-of-transaction flag */
  211. writel(1, &i2c->stat);
  212. return 0;
  213. }
  214. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, _i2c_init, lpc32xx_i2c_probe,
  215. lpc32xx_i2c_read, lpc32xx_i2c_write,
  216. lpc32xx_i2c_set_bus_speed,
  217. CONFIG_SYS_I2C_LPC32XX_SPEED,
  218. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  219. 0)
  220. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, _i2c_init, lpc32xx_i2c_probe,
  221. lpc32xx_i2c_read, lpc32xx_i2c_write,
  222. lpc32xx_i2c_set_bus_speed,
  223. CONFIG_SYS_I2C_LPC32XX_SPEED,
  224. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  225. 1)