sh_i2c.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. i2c_set_addr(base, id, reg, 1);
  113. udelay(100);
  114. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &base->iccr);
  115. irq_dte(base);
  116. writeb(id << 1 | 0x01, &base->icdr);
  117. irq_dte(base);
  118. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &base->iccr);
  119. irq_dte(base);
  120. ret = readb(&base->icdr);
  121. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &base->iccr);
  122. readb(&base->icdr); /* Dummy read */
  123. irq_busy(base);
  124. i2c_finish(base);
  125. return ret;
  126. }
  127. #ifdef CONFIG_I2C_MULTI_BUS
  128. static unsigned int current_bus;
  129. /**
  130. * i2c_set_bus_num - change active I2C bus
  131. * @bus: bus index, zero based
  132. * @returns: 0 on success, non-0 on failure
  133. */
  134. int i2c_set_bus_num(unsigned int bus)
  135. {
  136. if ((bus < 0) || (bus >= CONFIG_SYS_MAX_I2C_BUS)) {
  137. printf("Bad bus: %d\n", bus);
  138. return -1;
  139. }
  140. switch (bus) {
  141. case 0:
  142. base = (void *)CONFIG_SH_I2C_BASE0;
  143. break;
  144. case 1:
  145. base = (void *)CONFIG_SH_I2C_BASE1;
  146. break;
  147. default:
  148. return -1;
  149. }
  150. current_bus = bus;
  151. return 0;
  152. }
  153. /**
  154. * i2c_get_bus_num - returns index of active I2C bus
  155. */
  156. unsigned int i2c_get_bus_num(void)
  157. {
  158. return current_bus;
  159. }
  160. #endif
  161. #define SH_I2C_ICCL_CALC(clk, date, t_low, t_high) \
  162. ((clk / rate) * (t_low / t_low + t_high))
  163. #define SH_I2C_ICCH_CALC(clk, date, t_low, t_high) \
  164. ((clk / rate) * (t_high / t_low + t_high))
  165. void i2c_init(int speed, int slaveaddr)
  166. {
  167. int num, denom, tmp;
  168. #ifdef CONFIG_I2C_MULTI_BUS
  169. current_bus = 0;
  170. #endif
  171. base = (struct sh_i2c *)CONFIG_SH_I2C_BASE0;
  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. }
  193. /*
  194. * i2c_read: - Read multiple bytes from an i2c device
  195. *
  196. * The higher level routines take into account that this function is only
  197. * called with len < page length of the device (see configuration file)
  198. *
  199. * @chip: address of the chip which is to be read
  200. * @addr: i2c data address within the chip
  201. * @alen: length of the i2c data address (1..2 bytes)
  202. * @buffer: where to write the data
  203. * @len: how much byte do we want to read
  204. * @return: 0 in case of success
  205. */
  206. int i2c_read(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  207. {
  208. int i = 0;
  209. for (i = 0 ; i < len ; i++)
  210. buffer[i] = i2c_raw_read(base, chip, addr + i);
  211. return 0;
  212. }
  213. /*
  214. * i2c_write: - Write multiple bytes to an i2c device
  215. *
  216. * The higher level routines take into account that this function is only
  217. * called with len < page length of the device (see configuration file)
  218. *
  219. * @chip: address of the chip which is to be written
  220. * @addr: i2c data address within the chip
  221. * @alen: length of the i2c data address (1..2 bytes)
  222. * @buffer: where to find the data to be written
  223. * @len: how much byte do we want to read
  224. * @return: 0 in case of success
  225. */
  226. int i2c_write(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  227. {
  228. int i = 0;
  229. for (i = 0; i < len ; i++)
  230. i2c_raw_write(base, chip, addr + i, buffer[i]);
  231. return 0;
  232. }
  233. /*
  234. * i2c_probe: - Test if a chip answers for a given i2c address
  235. *
  236. * @chip: address of the chip which is searched for
  237. * @return: 0 if a chip was found, -1 otherwhise
  238. */
  239. int i2c_probe(u8 chip)
  240. {
  241. return 0;
  242. }