sh_i2c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 int irq_dte_with_tack(struct sh_i2c *base)
  62. {
  63. int i;
  64. for (i = 0 ; i < IRQ_WAIT ; i++) {
  65. if (SH_IC_DTE & readb(&base->icsr))
  66. break;
  67. if (SH_IC_TACK & readb(&base->icsr))
  68. return -1;
  69. udelay(10);
  70. }
  71. return 0;
  72. }
  73. static void irq_busy(struct sh_i2c *base)
  74. {
  75. int i;
  76. for (i = 0 ; i < IRQ_WAIT ; i++) {
  77. if (!(SH_IC_BUSY & readb(&base->icsr)))
  78. break;
  79. udelay(10);
  80. }
  81. }
  82. static int i2c_set_addr(struct sh_i2c *base, u8 id, u8 reg, int stop)
  83. {
  84. u8 icic = SH_IC_TACK;
  85. writeb(readb(&base->iccr) & ~SH_I2C_ICCR_ICE, &base->iccr);
  86. writeb(readb(&base->iccr) | SH_I2C_ICCR_ICE, &base->iccr);
  87. writeb(iccl & 0xff, &base->iccl);
  88. writeb(icch & 0xff, &base->icch);
  89. #ifdef CONFIG_SH_I2C_8BIT
  90. if (iccl > 0xff)
  91. icic |= SH_I2C_ICIC_ICCLB8;
  92. if (icch > 0xff)
  93. icic |= SH_I2C_ICIC_ICCHB8;
  94. #endif
  95. writeb(icic, &base->icic);
  96. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &base->iccr);
  97. irq_dte(base);
  98. writeb(readb(&base->icsr) & ~SH_IC_TACK, &base->icsr);
  99. writeb(id << 1, &base->icdr);
  100. if (irq_dte_with_tack(base) != 0)
  101. return -1;
  102. writeb(reg, &base->icdr);
  103. if (stop)
  104. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &base->iccr);
  105. if (irq_dte_with_tack(base) != 0)
  106. return -1;
  107. return 0;
  108. }
  109. static void i2c_finish(struct sh_i2c *base)
  110. {
  111. writeb(0, &base->icsr);
  112. writeb(readb(&base->iccr) & ~SH_I2C_ICCR_ICE, &base->iccr);
  113. }
  114. static void i2c_raw_write(struct sh_i2c *base, u8 id, u8 reg, u8 val)
  115. {
  116. i2c_set_addr(base, id, reg, 0);
  117. udelay(10);
  118. writeb(val, &base->icdr);
  119. irq_dte(base);
  120. writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &base->iccr);
  121. irq_dte(base);
  122. irq_busy(base);
  123. i2c_finish(base);
  124. }
  125. static u8 i2c_raw_read(struct sh_i2c *base, u8 id, u8 reg)
  126. {
  127. u8 ret;
  128. #if defined(CONFIG_SH73A0)
  129. i2c_set_addr(base, id, reg, 0);
  130. #else
  131. i2c_set_addr(base, id, reg, 1);
  132. udelay(100);
  133. #endif
  134. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &base->iccr);
  135. irq_dte(base);
  136. writeb(id << 1 | 0x01, &base->icdr);
  137. irq_dte(base);
  138. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &base->iccr);
  139. irq_dte(base);
  140. ret = readb(&base->icdr);
  141. writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &base->iccr);
  142. readb(&base->icdr); /* Dummy read */
  143. irq_busy(base);
  144. i2c_finish(base);
  145. return ret;
  146. }
  147. #ifdef CONFIG_I2C_MULTI_BUS
  148. static unsigned int current_bus;
  149. /**
  150. * i2c_set_bus_num - change active I2C bus
  151. * @bus: bus index, zero based
  152. * @returns: 0 on success, non-0 on failure
  153. */
  154. int i2c_set_bus_num(unsigned int bus)
  155. {
  156. if ((bus < 0) || (bus >= CONFIG_SYS_MAX_I2C_BUS)) {
  157. printf("Bad bus: %d\n", bus);
  158. return -1;
  159. }
  160. switch (bus) {
  161. case 0:
  162. base = (void *)CONFIG_SH_I2C_BASE0;
  163. break;
  164. case 1:
  165. base = (void *)CONFIG_SH_I2C_BASE1;
  166. break;
  167. #ifdef CONFIG_SH_I2C_BASE2
  168. case 2:
  169. base = (void *)CONFIG_SH_I2C_BASE2;
  170. break;
  171. #endif
  172. #ifdef CONFIG_SH_I2C_BASE3
  173. case 3:
  174. base = (void *)CONFIG_SH_I2C_BASE3;
  175. break;
  176. #endif
  177. #ifdef CONFIG_SH_I2C_BASE4
  178. case 4:
  179. base = (void *)CONFIG_SH_I2C_BASE4;
  180. break;
  181. #endif
  182. default:
  183. return -1;
  184. }
  185. current_bus = bus;
  186. return 0;
  187. }
  188. /**
  189. * i2c_get_bus_num - returns index of active I2C bus
  190. */
  191. unsigned int i2c_get_bus_num(void)
  192. {
  193. return current_bus;
  194. }
  195. #endif
  196. #define SH_I2C_ICCL_CALC(clk, date, t_low, t_high) \
  197. ((clk / rate) * (t_low / t_low + t_high))
  198. #define SH_I2C_ICCH_CALC(clk, date, t_low, t_high) \
  199. ((clk / rate) * (t_high / t_low + t_high))
  200. void i2c_init(int speed, int slaveaddr)
  201. {
  202. int num, denom, tmp;
  203. #ifdef CONFIG_I2C_MULTI_BUS
  204. current_bus = 0;
  205. #endif
  206. base = (struct sh_i2c *)CONFIG_SH_I2C_BASE0;
  207. /*
  208. * Calculate the value for iccl. From the data sheet:
  209. * iccl = (p-clock / transfer-rate) * (L / (L + H))
  210. * where L and H are the SCL low and high ratio.
  211. */
  212. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
  213. denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
  214. tmp = num * 10 / denom;
  215. if (tmp % 10 >= 5)
  216. iccl = (u16)((num/denom) + 1);
  217. else
  218. iccl = (u16)(num/denom);
  219. /* Calculate the value for icch. From the data sheet:
  220. icch = (p clock / transfer rate) * (H / (L + H)) */
  221. num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
  222. tmp = num * 10 / denom;
  223. if (tmp % 10 >= 5)
  224. icch = (u16)((num/denom) + 1);
  225. else
  226. icch = (u16)(num/denom);
  227. }
  228. /*
  229. * i2c_read: - Read multiple bytes from an i2c device
  230. *
  231. * The higher level routines take into account that this function is only
  232. * called with len < page length of the device (see configuration file)
  233. *
  234. * @chip: address of the chip which is to be read
  235. * @addr: i2c data address within the chip
  236. * @alen: length of the i2c data address (1..2 bytes)
  237. * @buffer: where to write the data
  238. * @len: how much byte do we want to read
  239. * @return: 0 in case of success
  240. */
  241. int i2c_read(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  242. {
  243. int i = 0;
  244. for (i = 0 ; i < len ; i++)
  245. buffer[i] = i2c_raw_read(base, chip, addr + i);
  246. return 0;
  247. }
  248. /*
  249. * i2c_write: - Write multiple bytes to an i2c device
  250. *
  251. * The higher level routines take into account that this function is only
  252. * called with len < page length of the device (see configuration file)
  253. *
  254. * @chip: address of the chip which is to be written
  255. * @addr: i2c data address within the chip
  256. * @alen: length of the i2c data address (1..2 bytes)
  257. * @buffer: where to find the data to be written
  258. * @len: how much byte do we want to read
  259. * @return: 0 in case of success
  260. */
  261. int i2c_write(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  262. {
  263. int i = 0;
  264. for (i = 0; i < len ; i++)
  265. i2c_raw_write(base, chip, addr + i, buffer[i]);
  266. return 0;
  267. }
  268. /*
  269. * i2c_probe: - Test if a chip answers for a given i2c address
  270. *
  271. * @chip: address of the chip which is searched for
  272. * @return: 0 if a chip was found, -1 otherwhise
  273. */
  274. int i2c_probe(u8 chip)
  275. {
  276. int ret;
  277. ret = i2c_set_addr(base, chip, 0, 1);
  278. i2c_finish(base);
  279. return ret;
  280. }