sh_i2c.c 7.1 KB

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