sh_i2c.c 7.7 KB

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