sh_i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2011 Renesas Solutions Corp.
  3. * Copyright (C) 2011 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. /* Every register is 32bit aligned, but only 8bits in size */
  10. #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
  11. struct sh_i2c {
  12. ureg(icdr);
  13. ureg(iccr);
  14. ureg(icsr);
  15. ureg(icic);
  16. ureg(iccl);
  17. ureg(icch);
  18. };
  19. #undef ureg
  20. static struct sh_i2c *base;
  21. /* ICCR */
  22. #define SH_I2C_ICCR_ICE (1 << 7)
  23. #define SH_I2C_ICCR_RACK (1 << 6)
  24. #define SH_I2C_ICCR_RTS (1 << 4)
  25. #define SH_I2C_ICCR_BUSY (1 << 2)
  26. #define SH_I2C_ICCR_SCP (1 << 0)
  27. /* ICSR / ICIC */
  28. #define SH_IC_BUSY (1 << 4)
  29. #define SH_IC_TACK (1 << 2)
  30. #define SH_IC_WAIT (1 << 1)
  31. #define SH_IC_DTE (1 << 0)
  32. #ifdef CONFIG_SH_I2C_8BIT
  33. /* store 8th bit of iccl and icch in ICIC register */
  34. #define SH_I2C_ICIC_ICCLB8 (1 << 7)
  35. #define SH_I2C_ICIC_ICCHB8 (1 << 6)
  36. #endif
  37. static u16 iccl, icch;
  38. #define IRQ_WAIT 1000
  39. static void irq_dte(struct sh_i2c *base)
  40. {
  41. int i;
  42. for (i = 0 ; i < IRQ_WAIT ; i++) {
  43. if (SH_IC_DTE & readb(&base->icsr))
  44. break;
  45. udelay(10);
  46. }
  47. }
  48. static int irq_dte_with_tack(struct sh_i2c *base)
  49. {
  50. int i;
  51. for (i = 0 ; i < IRQ_WAIT ; i++) {
  52. if (SH_IC_DTE & readb(&base->icsr))
  53. break;
  54. if (SH_IC_TACK & readb(&base->icsr))
  55. return -1;
  56. udelay(10);
  57. }
  58. return 0;
  59. }
  60. static void irq_busy(struct sh_i2c *base)
  61. {
  62. int i;
  63. for (i = 0 ; i < IRQ_WAIT ; i++) {
  64. if (!(SH_IC_BUSY & readb(&base->icsr)))
  65. break;
  66. udelay(10);
  67. }
  68. }
  69. static int i2c_set_addr(struct sh_i2c *base, u8 id, u8 reg, int stop)
  70. {
  71. u8 icic = SH_IC_TACK;
  72. clrbits_8(&base->iccr, SH_I2C_ICCR_ICE);
  73. setbits_8(&base->iccr, SH_I2C_ICCR_ICE);
  74. writeb(iccl & 0xff, &base->iccl);
  75. writeb(icch & 0xff, &base->icch);
  76. #ifdef CONFIG_SH_I2C_8BIT
  77. if (iccl > 0xff)
  78. icic |= SH_I2C_ICIC_ICCLB8;
  79. if (icch > 0xff)
  80. icic |= SH_I2C_ICIC_ICCHB8;
  81. #endif
  82. writeb(icic, &base->icic);
  83. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &base->iccr);
  84. irq_dte(base);
  85. clrbits_8(&base->icsr, SH_IC_TACK);
  86. writeb(id << 1, &base->icdr);
  87. if (irq_dte_with_tack(base) != 0)
  88. return -1;
  89. writeb(reg, &base->icdr);
  90. if (stop)
  91. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &base->iccr);
  92. if (irq_dte_with_tack(base) != 0)
  93. return -1;
  94. return 0;
  95. }
  96. static void i2c_finish(struct sh_i2c *base)
  97. {
  98. writeb(0, &base->icsr);
  99. clrbits_8(&base->iccr, SH_I2C_ICCR_ICE);
  100. }
  101. static int i2c_raw_write(struct sh_i2c *base, u8 id, u8 reg, u8 val)
  102. {
  103. int ret = -1;
  104. if (i2c_set_addr(base, id, reg, 0) != 0)
  105. goto exit0;
  106. udelay(10);
  107. writeb(val, &base->icdr);
  108. if (irq_dte_with_tack(base) != 0)
  109. goto exit0;
  110. writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &base->iccr);
  111. if (irq_dte_with_tack(base) != 0)
  112. goto exit0;
  113. irq_busy(base);
  114. ret = 0;
  115. exit0:
  116. i2c_finish(base);
  117. return ret;
  118. }
  119. static int i2c_raw_read(struct sh_i2c *base, u8 id, u8 reg)
  120. {
  121. int ret = -1;
  122. #if defined(CONFIG_SH73A0)
  123. if (i2c_set_addr(base, id, reg, 0) != 0)
  124. goto exit0;
  125. #else
  126. if (i2c_set_addr(base, id, reg, 1) != 0)
  127. goto exit0;
  128. udelay(100);
  129. #endif
  130. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &base->iccr);
  131. irq_dte(base);
  132. writeb(id << 1 | 0x01, &base->icdr);
  133. if (irq_dte_with_tack(base) != 0)
  134. goto exit0;
  135. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &base->iccr);
  136. if (irq_dte_with_tack(base) != 0)
  137. goto exit0;
  138. ret = readb(&base->icdr) & 0xff;
  139. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &base->iccr);
  140. readb(&base->icdr); /* Dummy read */
  141. irq_busy(base);
  142. exit0:
  143. i2c_finish(base);
  144. return ret;
  145. }
  146. #ifdef CONFIG_I2C_MULTI_BUS
  147. static unsigned int current_bus;
  148. /**
  149. * i2c_set_bus_num - change active I2C bus
  150. * @bus: bus index, zero based
  151. * @returns: 0 on success, non-0 on failure
  152. */
  153. int i2c_set_bus_num(unsigned int bus)
  154. {
  155. if ((bus < 0) || (bus >= CONFIG_SYS_MAX_I2C_BUS)) {
  156. printf("Bad bus: %d\n", bus);
  157. return -1;
  158. }
  159. switch (bus) {
  160. case 0:
  161. base = (void *)CONFIG_SH_I2C_BASE0;
  162. break;
  163. case 1:
  164. base = (void *)CONFIG_SH_I2C_BASE1;
  165. break;
  166. #ifdef CONFIG_SH_I2C_BASE2
  167. case 2:
  168. base = (void *)CONFIG_SH_I2C_BASE2;
  169. break;
  170. #endif
  171. #ifdef CONFIG_SH_I2C_BASE3
  172. case 3:
  173. base = (void *)CONFIG_SH_I2C_BASE3;
  174. break;
  175. #endif
  176. #ifdef CONFIG_SH_I2C_BASE4
  177. case 4:
  178. base = (void *)CONFIG_SH_I2C_BASE4;
  179. break;
  180. #endif
  181. default:
  182. return -1;
  183. }
  184. current_bus = bus;
  185. return 0;
  186. }
  187. /**
  188. * i2c_get_bus_num - returns index of active I2C bus
  189. */
  190. unsigned int i2c_get_bus_num(void)
  191. {
  192. return current_bus;
  193. }
  194. #endif
  195. #define SH_I2C_ICCL_CALC(clk, date, t_low, t_high) \
  196. ((clk / rate) * (t_low / t_low + t_high))
  197. #define SH_I2C_ICCH_CALC(clk, date, t_low, t_high) \
  198. ((clk / rate) * (t_high / t_low + t_high))
  199. void i2c_init(int speed, int slaveaddr)
  200. {
  201. int num, denom, tmp;
  202. #ifdef CONFIG_I2C_MULTI_BUS
  203. current_bus = 0;
  204. #endif
  205. base = (struct sh_i2c *)CONFIG_SH_I2C_BASE0;
  206. /*
  207. * Calculate the value for iccl. From the data sheet:
  208. * iccl = (p-clock / transfer-rate) * (L / (L + H))
  209. * where L and H are the SCL low and high ratio.
  210. */
  211. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
  212. denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
  213. tmp = num * 10 / denom;
  214. if (tmp % 10 >= 5)
  215. iccl = (u16)((num/denom) + 1);
  216. else
  217. iccl = (u16)(num/denom);
  218. /* Calculate the value for icch. From the data sheet:
  219. icch = (p clock / transfer rate) * (H / (L + H)) */
  220. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
  221. tmp = num * 10 / denom;
  222. if (tmp % 10 >= 5)
  223. icch = (u16)((num/denom) + 1);
  224. else
  225. icch = (u16)(num/denom);
  226. }
  227. /*
  228. * i2c_read: - Read multiple bytes from an i2c device
  229. *
  230. * The higher level routines take into account that this function is only
  231. * called with len < page length of the device (see configuration file)
  232. *
  233. * @chip: address of the chip which is to be read
  234. * @addr: i2c data address within the chip
  235. * @alen: length of the i2c data address (1..2 bytes)
  236. * @buffer: where to write the data
  237. * @len: how much byte do we want to read
  238. * @return: 0 in case of success
  239. */
  240. int i2c_read(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  241. {
  242. int ret;
  243. int i = 0;
  244. for (i = 0 ; i < len ; i++) {
  245. ret = i2c_raw_read(base, chip, addr + i);
  246. if (ret < 0)
  247. return -1;
  248. buffer[i] = ret & 0xff;
  249. }
  250. return 0;
  251. }
  252. /*
  253. * i2c_write: - Write multiple bytes to an i2c device
  254. *
  255. * The higher level routines take into account that this function is only
  256. * called with len < page length of the device (see configuration file)
  257. *
  258. * @chip: address of the chip which is to be written
  259. * @addr: i2c data address within the chip
  260. * @alen: length of the i2c data address (1..2 bytes)
  261. * @buffer: where to find the data to be written
  262. * @len: how much byte do we want to read
  263. * @return: 0 in case of success
  264. */
  265. int i2c_write(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  266. {
  267. int i = 0;
  268. for (i = 0; i < len ; i++)
  269. if (i2c_raw_write(base, chip, addr + i, buffer[i]) != 0)
  270. return -1;
  271. return 0;
  272. }
  273. /*
  274. * i2c_probe: - Test if a chip answers for a given i2c address
  275. *
  276. * @chip: address of the chip which is searched for
  277. * @return: 0 if a chip was found, -1 otherwhise
  278. */
  279. int i2c_probe(u8 chip)
  280. {
  281. int ret;
  282. ret = i2c_set_addr(base, chip, 0, 1);
  283. i2c_finish(base);
  284. return ret;
  285. }