lpc32xx_i2c.c 7.0 KB

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