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