lpc32xx_i2c.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_base {
  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_base *lpc32xx_i2c[] = {
  56. (struct lpc32xx_i2c_base *)I2C1_BASE,
  57. (struct lpc32xx_i2c_base *)I2C2_BASE,
  58. (struct lpc32xx_i2c_base *)(USB_BASE + 0x300)
  59. };
  60. /* Set I2C bus speed */
  61. static unsigned int __i2c_set_bus_speed(struct lpc32xx_i2c_base *base,
  62. unsigned int speed, unsigned int chip)
  63. {
  64. int half_period;
  65. if (speed == 0)
  66. return -EINVAL;
  67. /* OTG I2C clock source and CLK registers are different */
  68. if (chip == 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, &base->clk_hi);
  78. writel(half_period, &base->clk_lo);
  79. return 0;
  80. }
  81. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  82. static void __i2c_init(struct lpc32xx_i2c_base *base,
  83. int requested_speed, int slaveadd, unsigned int chip)
  84. {
  85. /* soft reset (auto-clears) */
  86. writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
  87. /* set HI and LO periods for half of the default speed */
  88. __i2c_set_bus_speed(base, requested_speed, chip);
  89. }
  90. /* I2C probe called by cmd_i2c when doing 'i2c probe'. */
  91. static int __i2c_probe_chip(struct lpc32xx_i2c_base *base, u8 dev)
  92. {
  93. int stat;
  94. /* Soft-reset the controller */
  95. writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
  96. while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
  97. ;
  98. /* Addre slave for write with start before and stop after */
  99. writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
  100. &base->tx);
  101. /* wait for end of transation */
  102. while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
  103. ;
  104. /* was there no acknowledge? */
  105. return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
  106. }
  107. /*
  108. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  109. * Begin write, send address byte(s), begin read, receive data bytes, end.
  110. */
  111. static int __i2c_read(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
  112. int alen, u8 *data, int length)
  113. {
  114. int stat, wlen;
  115. /* Soft-reset the controller */
  116. writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
  117. while (readl(&base->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, &base->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, &base->tx);
  131. }
  132. /* wait for end of transation */
  133. while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
  134. ;
  135. /* clear end-of-transaction flag */
  136. writel(1, &base->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, &base->tx);
  142. wlen = length;
  143. /* get data */
  144. while (length | wlen) {
  145. /* read status for TFF and RFE */
  146. stat = readl(&base->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, &base->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(&base->rx);
  161. }
  162. }
  163. /* wait for end of transation */
  164. while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
  165. ;
  166. /* clear end-of-transaction flag */
  167. writel(1, &base->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 __i2c_write(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
  177. int alen, u8 *data, int length)
  178. {
  179. int stat;
  180. /* Soft-reset the controller */
  181. writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
  182. while (readl(&base->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, &base->tx);
  188. else
  189. return 0;
  190. /* write address bytes */
  191. while (alen) {
  192. /* wait for transmit fifo not full */
  193. stat = readl(&base->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, &base->tx);
  201. }
  202. }
  203. while (length) {
  204. /* wait for transmit fifo not full */
  205. stat = readl(&base->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, &base->tx);
  214. }
  215. }
  216. /* wait for end of transation */
  217. while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
  218. ;
  219. /* clear end-of-transaction flag */
  220. writel(1, &base->stat);
  221. return 0;
  222. }
  223. static void lpc32xx_i2c_init(struct i2c_adapter *adap,
  224. int requested_speed, int slaveadd)
  225. {
  226. __i2c_init(lpc32xx_i2c[adap->hwadapnr], requested_speed, slaveadd,
  227. adap->hwadapnr);
  228. }
  229. static int lpc32xx_i2c_probe_chip(struct i2c_adapter *adap, u8 dev)
  230. {
  231. return __i2c_probe_chip(lpc32xx_i2c[adap->hwadapnr], dev);
  232. }
  233. static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
  234. int alen, u8 *data, int length)
  235. {
  236. return __i2c_read(lpc32xx_i2c[adap->hwadapnr], dev, addr,
  237. alen, data, length);
  238. }
  239. static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
  240. int alen, u8 *data, int length)
  241. {
  242. return __i2c_write(lpc32xx_i2c[adap->hwadapnr], dev, addr,
  243. alen, data, length);
  244. }
  245. static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
  246. unsigned int speed)
  247. {
  248. return __i2c_set_bus_speed(lpc32xx_i2c[adap->hwadapnr], speed,
  249. adap->hwadapnr);
  250. }
  251. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
  252. lpc32xx_i2c_read, lpc32xx_i2c_write,
  253. lpc32xx_i2c_set_bus_speed,
  254. CONFIG_SYS_I2C_LPC32XX_SPEED,
  255. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  256. 0)
  257. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
  258. lpc32xx_i2c_read, lpc32xx_i2c_write,
  259. lpc32xx_i2c_set_bus_speed,
  260. CONFIG_SYS_I2C_LPC32XX_SPEED,
  261. CONFIG_SYS_I2C_LPC32XX_SLAVE,
  262. 1)
  263. U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, lpc32xx_i2c_init, NULL,
  264. lpc32xx_i2c_read, lpc32xx_i2c_write,
  265. lpc32xx_i2c_set_bus_speed,
  266. 100000,
  267. 0,
  268. 2)