sh_i2c.c 7.0 KB

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