sh_i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2011, 2013 Renesas Solutions Corp.
  3. * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <asm/io.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. /* Every register is 32bit aligned, but only 8bits in size */
  12. #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
  13. struct sh_i2c {
  14. ureg(icdr);
  15. ureg(iccr);
  16. ureg(icsr);
  17. ureg(icic);
  18. ureg(iccl);
  19. ureg(icch);
  20. };
  21. #undef ureg
  22. /* ICCR */
  23. #define SH_I2C_ICCR_ICE (1 << 7)
  24. #define SH_I2C_ICCR_RACK (1 << 6)
  25. #define SH_I2C_ICCR_RTS (1 << 4)
  26. #define SH_I2C_ICCR_BUSY (1 << 2)
  27. #define SH_I2C_ICCR_SCP (1 << 0)
  28. /* ICSR / ICIC */
  29. #define SH_IC_BUSY (1 << 4)
  30. #define SH_IC_TACK (1 << 2)
  31. #define SH_IC_WAIT (1 << 1)
  32. #define SH_IC_DTE (1 << 0)
  33. #ifdef CONFIG_SH_I2C_8BIT
  34. /* store 8th bit of iccl and icch in ICIC register */
  35. #define SH_I2C_ICIC_ICCLB8 (1 << 7)
  36. #define SH_I2C_ICIC_ICCHB8 (1 << 6)
  37. #endif
  38. static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = {
  39. (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0,
  40. #ifdef CONFIG_SYS_I2C_SH_BASE1
  41. (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1,
  42. #endif
  43. #ifdef CONFIG_SYS_I2C_SH_BASE2
  44. (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
  45. #endif
  46. #ifdef CONFIG_SYS_I2C_SH_BASE3
  47. (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
  48. #endif
  49. #ifdef CONFIG_SYS_I2C_SH_BASE4
  50. (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
  51. #endif
  52. };
  53. static u16 iccl, icch;
  54. #define IRQ_WAIT 1000
  55. static void sh_irq_dte(struct sh_i2c *dev)
  56. {
  57. int i;
  58. for (i = 0; i < IRQ_WAIT; i++) {
  59. if (SH_IC_DTE & readb(&dev->icsr))
  60. break;
  61. udelay(10);
  62. }
  63. }
  64. static int sh_irq_dte_with_tack(struct sh_i2c *dev)
  65. {
  66. int i;
  67. for (i = 0; i < IRQ_WAIT; i++) {
  68. if (SH_IC_DTE & readb(&dev->icsr))
  69. break;
  70. if (SH_IC_TACK & readb(&dev->icsr))
  71. return -1;
  72. udelay(10);
  73. }
  74. return 0;
  75. }
  76. static void sh_irq_busy(struct sh_i2c *dev)
  77. {
  78. int i;
  79. for (i = 0; i < IRQ_WAIT; i++) {
  80. if (!(SH_IC_BUSY & readb(&dev->icsr)))
  81. break;
  82. udelay(10);
  83. }
  84. }
  85. static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
  86. {
  87. u8 icic = SH_IC_TACK;
  88. debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n",
  89. __func__, chip, addr, iccl, icch);
  90. clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
  91. setbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
  92. writeb(iccl & 0xff, &dev->iccl);
  93. writeb(icch & 0xff, &dev->icch);
  94. #ifdef CONFIG_SH_I2C_8BIT
  95. if (iccl > 0xff)
  96. icic |= SH_I2C_ICIC_ICCLB8;
  97. if (icch > 0xff)
  98. icic |= SH_I2C_ICIC_ICCHB8;
  99. #endif
  100. writeb(icic, &dev->icic);
  101. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
  102. sh_irq_dte(dev);
  103. clrbits_8(&dev->icsr, SH_IC_TACK);
  104. writeb(chip << 1, &dev->icdr);
  105. if (sh_irq_dte_with_tack(dev) != 0)
  106. return -1;
  107. writeb(addr, &dev->icdr);
  108. if (stop)
  109. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
  110. if (sh_irq_dte_with_tack(dev) != 0)
  111. return -1;
  112. return 0;
  113. }
  114. static void sh_i2c_finish(struct sh_i2c *dev)
  115. {
  116. writeb(0, &dev->icsr);
  117. clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
  118. }
  119. static int
  120. sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
  121. {
  122. int ret = -1;
  123. if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
  124. goto exit0;
  125. udelay(10);
  126. writeb(val, &dev->icdr);
  127. if (sh_irq_dte_with_tack(dev) != 0)
  128. goto exit0;
  129. writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
  130. if (sh_irq_dte_with_tack(dev) != 0)
  131. goto exit0;
  132. sh_irq_busy(dev);
  133. ret = 0;
  134. exit0:
  135. sh_i2c_finish(dev);
  136. return ret;
  137. }
  138. static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
  139. {
  140. int ret = -1;
  141. #if defined(CONFIG_SH73A0)
  142. if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
  143. goto exit0;
  144. #else
  145. if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
  146. goto exit0;
  147. udelay(100);
  148. #endif
  149. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
  150. sh_irq_dte(dev);
  151. writeb(chip << 1 | 0x01, &dev->icdr);
  152. if (sh_irq_dte_with_tack(dev) != 0)
  153. goto exit0;
  154. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr);
  155. if (sh_irq_dte_with_tack(dev) != 0)
  156. goto exit0;
  157. ret = readb(&dev->icdr) & 0xff;
  158. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr);
  159. readb(&dev->icdr); /* Dummy read */
  160. sh_irq_busy(dev);
  161. exit0:
  162. sh_i2c_finish(dev);
  163. return ret;
  164. }
  165. static void
  166. sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  167. {
  168. int num, denom, tmp;
  169. /* No i2c support prior to relocation */
  170. if (!(gd->flags & GD_FLG_RELOC))
  171. return;
  172. /*
  173. * Calculate the value for iccl. From the data sheet:
  174. * iccl = (p-clock / transfer-rate) * (L / (L + H))
  175. * where L and H are the SCL low and high ratio.
  176. */
  177. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
  178. denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
  179. tmp = num * 10 / denom;
  180. if (tmp % 10 >= 5)
  181. iccl = (u16)((num/denom) + 1);
  182. else
  183. iccl = (u16)(num/denom);
  184. /* Calculate the value for icch. From the data sheet:
  185. icch = (p clock / transfer rate) * (H / (L + H)) */
  186. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
  187. tmp = num * 10 / denom;
  188. if (tmp % 10 >= 5)
  189. icch = (u16)((num/denom) + 1);
  190. else
  191. icch = (u16)(num/denom);
  192. debug("clock: %d, speed %d, iccl: %x, icch: %x\n",
  193. CONFIG_SH_I2C_CLOCK, speed, iccl, icch);
  194. }
  195. static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  196. uint addr, int alen, u8 *data, int len)
  197. {
  198. int ret, i;
  199. struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
  200. for (i = 0; i < len; i++) {
  201. ret = sh_i2c_raw_read(dev, chip, addr + i);
  202. if (ret < 0)
  203. return -1;
  204. data[i] = ret & 0xff;
  205. debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
  206. }
  207. return 0;
  208. }
  209. static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
  210. int alen, u8 *data, int len)
  211. {
  212. struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
  213. int i;
  214. for (i = 0; i < len; i++) {
  215. debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
  216. if (sh_i2c_raw_write(dev, chip, addr + i, data[i]) != 0)
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. static int
  222. sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
  223. {
  224. u8 dummy[1];
  225. return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
  226. }
  227. static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap,
  228. unsigned int speed)
  229. {
  230. struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
  231. sh_i2c_finish(dev);
  232. sh_i2c_init(adap, speed, 0);
  233. return 0;
  234. }
  235. /*
  236. * Register RCAR i2c adapters
  237. */
  238. U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
  239. sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED0, 0, 0)
  240. #ifdef CONFIG_SYS_I2C_SH_BASE1
  241. U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
  242. sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED1, 0, 1)
  243. #endif
  244. #ifdef CONFIG_SYS_I2C_SH_BASE2
  245. U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
  246. sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED2, 0, 2)
  247. #endif
  248. #ifdef CONFIG_SYS_I2C_SH_BASE3
  249. U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
  250. sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED3, 0, 3)
  251. #endif
  252. #ifdef CONFIG_SYS_I2C_SH_BASE4
  253. U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
  254. sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED4, 0, 4)
  255. #endif