sh_sh7734_i2c.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (C) 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  3. * Copyright (C) 2012 Renesas Solutions Corp.
  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 <i2c.h>
  22. #include <asm/io.h>
  23. struct sh_i2c {
  24. u8 iccr1;
  25. u8 iccr2;
  26. u8 icmr;
  27. u8 icier;
  28. u8 icsr;
  29. u8 sar;
  30. u8 icdrt;
  31. u8 icdrr;
  32. u8 nf2cyc;
  33. u8 __pad0;
  34. u8 __pad1;
  35. };
  36. static struct sh_i2c *base;
  37. static u8 iccr1_cks, nf2cyc;
  38. /* ICCR1 */
  39. #define SH_I2C_ICCR1_ICE (1 << 7)
  40. #define SH_I2C_ICCR1_RCVD (1 << 6)
  41. #define SH_I2C_ICCR1_MST (1 << 5)
  42. #define SH_I2C_ICCR1_TRS (1 << 4)
  43. #define SH_I2C_ICCR1_MTRS \
  44. (SH_I2C_ICCR1_MST | SH_I2C_ICCR1_TRS)
  45. /* ICCR1 */
  46. #define SH_I2C_ICCR2_BBSY (1 << 7)
  47. #define SH_I2C_ICCR2_SCP (1 << 6)
  48. #define SH_I2C_ICCR2_SDAO (1 << 5)
  49. #define SH_I2C_ICCR2_SDAOP (1 << 4)
  50. #define SH_I2C_ICCR2_SCLO (1 << 3)
  51. #define SH_I2C_ICCR2_IICRST (1 << 1)
  52. #define SH_I2C_ICIER_TIE (1 << 7)
  53. #define SH_I2C_ICIER_TEIE (1 << 6)
  54. #define SH_I2C_ICIER_RIE (1 << 5)
  55. #define SH_I2C_ICIER_NAKIE (1 << 4)
  56. #define SH_I2C_ICIER_STIE (1 << 3)
  57. #define SH_I2C_ICIER_ACKE (1 << 2)
  58. #define SH_I2C_ICIER_ACKBR (1 << 1)
  59. #define SH_I2C_ICIER_ACKBT (1 << 0)
  60. #define SH_I2C_ICSR_TDRE (1 << 7)
  61. #define SH_I2C_ICSR_TEND (1 << 6)
  62. #define SH_I2C_ICSR_RDRF (1 << 5)
  63. #define SH_I2C_ICSR_NACKF (1 << 4)
  64. #define SH_I2C_ICSR_STOP (1 << 3)
  65. #define SH_I2C_ICSR_ALOVE (1 << 2)
  66. #define SH_I2C_ICSR_AAS (1 << 1)
  67. #define SH_I2C_ICSR_ADZ (1 << 0)
  68. #define IRQ_WAIT 1000
  69. static void sh_i2c_send_stop(struct sh_i2c *base)
  70. {
  71. clrbits_8(&base->iccr2, SH_I2C_ICCR2_BBSY | SH_I2C_ICCR2_SCP);
  72. }
  73. static int check_icsr_bits(struct sh_i2c *base, u8 bits)
  74. {
  75. int i;
  76. for (i = 0; i < IRQ_WAIT; i++) {
  77. if (bits & readb(&base->icsr))
  78. return 0;
  79. udelay(10);
  80. }
  81. return 1;
  82. }
  83. static int check_stop(struct sh_i2c *base)
  84. {
  85. int ret = check_icsr_bits(base, SH_I2C_ICSR_STOP);
  86. clrbits_8(&base->icsr, SH_I2C_ICSR_STOP);
  87. return ret;
  88. }
  89. static int check_tend(struct sh_i2c *base, int stop)
  90. {
  91. int ret = check_icsr_bits(base, SH_I2C_ICSR_TEND);
  92. if (stop) {
  93. clrbits_8(&base->icsr, SH_I2C_ICSR_STOP);
  94. sh_i2c_send_stop(base);
  95. }
  96. clrbits_8(&base->icsr, SH_I2C_ICSR_TEND);
  97. return ret;
  98. }
  99. static int check_tdre(struct sh_i2c *base)
  100. {
  101. return check_icsr_bits(base, SH_I2C_ICSR_TDRE);
  102. }
  103. static int check_rdrf(struct sh_i2c *base)
  104. {
  105. return check_icsr_bits(base, SH_I2C_ICSR_RDRF);
  106. }
  107. static int check_bbsy(struct sh_i2c *base)
  108. {
  109. int i;
  110. for (i = 0 ; i < IRQ_WAIT ; i++) {
  111. if (!(SH_I2C_ICCR2_BBSY & readb(&base->iccr2)))
  112. return 0;
  113. udelay(10);
  114. }
  115. return 1;
  116. }
  117. static int check_ackbr(struct sh_i2c *base)
  118. {
  119. int i;
  120. for (i = 0 ; i < IRQ_WAIT ; i++) {
  121. if (!(SH_I2C_ICIER_ACKBR & readb(&base->icier)))
  122. return 0;
  123. udelay(10);
  124. }
  125. return 1;
  126. }
  127. static void sh_i2c_reset(struct sh_i2c *base)
  128. {
  129. setbits_8(&base->iccr2, SH_I2C_ICCR2_IICRST);
  130. udelay(100);
  131. clrbits_8(&base->iccr2, SH_I2C_ICCR2_IICRST);
  132. }
  133. static int i2c_set_addr(struct sh_i2c *base, u8 id, u8 reg)
  134. {
  135. if (check_bbsy(base)) {
  136. puts("i2c bus busy\n");
  137. goto fail;
  138. }
  139. setbits_8(&base->iccr1, SH_I2C_ICCR1_MTRS);
  140. clrsetbits_8(&base->iccr2, SH_I2C_ICCR2_SCP, SH_I2C_ICCR2_BBSY);
  141. writeb((id << 1), &base->icdrt);
  142. if (check_tend(base, 0)) {
  143. puts("TEND check fail...\n");
  144. goto fail;
  145. }
  146. if (check_ackbr(base)) {
  147. check_tend(base, 0);
  148. sh_i2c_send_stop(base);
  149. goto fail;
  150. }
  151. writeb(reg, &base->icdrt);
  152. if (check_tdre(base)) {
  153. puts("TDRE check fail...\n");
  154. goto fail;
  155. }
  156. if (check_tend(base, 0)) {
  157. puts("TEND check fail...\n");
  158. goto fail;
  159. }
  160. return 0;
  161. fail:
  162. return 1;
  163. }
  164. static int
  165. i2c_raw_write(struct sh_i2c *base, u8 id, u8 reg, u8 *val, int size)
  166. {
  167. int i;
  168. if (i2c_set_addr(base, id, reg)) {
  169. puts("Fail set slave address\n");
  170. return 1;
  171. }
  172. for (i = 0; i < size; i++) {
  173. writeb(val[i], &base->icdrt);
  174. check_tdre(base);
  175. }
  176. check_tend(base, 1);
  177. check_stop(base);
  178. udelay(100);
  179. clrbits_8(&base->iccr1, SH_I2C_ICCR1_MTRS);
  180. clrbits_8(&base->icsr, SH_I2C_ICSR_TDRE);
  181. sh_i2c_reset(base);
  182. return 0;
  183. }
  184. static u8 i2c_raw_read(struct sh_i2c *base, u8 id, u8 reg)
  185. {
  186. u8 ret = 0;
  187. if (i2c_set_addr(base, id, reg)) {
  188. puts("Fail set slave address\n");
  189. goto fail;
  190. }
  191. clrsetbits_8(&base->iccr2, SH_I2C_ICCR2_SCP, SH_I2C_ICCR2_BBSY);
  192. writeb((id << 1) | 1, &base->icdrt);
  193. if (check_tend(base, 0))
  194. puts("TDRE check fail...\n");
  195. clrsetbits_8(&base->iccr1, SH_I2C_ICCR1_TRS, SH_I2C_ICCR1_MST);
  196. clrbits_8(&base->icsr, SH_I2C_ICSR_TDRE);
  197. setbits_8(&base->icier, SH_I2C_ICIER_ACKBT);
  198. setbits_8(&base->iccr1, SH_I2C_ICCR1_RCVD);
  199. /* read data (dummy) */
  200. ret = readb(&base->icdrr);
  201. if (check_rdrf(base)) {
  202. puts("check RDRF error\n");
  203. goto fail;
  204. }
  205. clrbits_8(&base->icsr, SH_I2C_ICSR_STOP);
  206. udelay(1000);
  207. sh_i2c_send_stop(base);
  208. if (check_stop(base)) {
  209. puts("check STOP error\n");
  210. goto fail;
  211. }
  212. clrbits_8(&base->iccr1, SH_I2C_ICCR1_MTRS);
  213. clrbits_8(&base->icsr, SH_I2C_ICSR_TDRE);
  214. /* data read */
  215. ret = readb(&base->icdrr);
  216. fail:
  217. clrbits_8(&base->iccr1, SH_I2C_ICCR1_RCVD);
  218. return ret;
  219. }
  220. #ifdef CONFIG_I2C_MULTI_BUS
  221. static unsigned int current_bus;
  222. /**
  223. * i2c_set_bus_num - change active I2C bus
  224. * @bus: bus index, zero based
  225. * @returns: 0 on success, non-0 on failure
  226. */
  227. int i2c_set_bus_num(unsigned int bus)
  228. {
  229. switch (bus) {
  230. case 0:
  231. base = (void *)CONFIG_SH_I2C_BASE0;
  232. break;
  233. case 1:
  234. base = (void *)CONFIG_SH_I2C_BASE1;
  235. break;
  236. default:
  237. printf("Bad bus: %d\n", bus);
  238. return -1;
  239. }
  240. current_bus = bus;
  241. return 0;
  242. }
  243. /**
  244. * i2c_get_bus_num - returns index of active I2C bus
  245. */
  246. unsigned int i2c_get_bus_num(void)
  247. {
  248. return current_bus;
  249. }
  250. #endif
  251. void i2c_init(int speed, int slaveaddr)
  252. {
  253. #ifdef CONFIG_I2C_MULTI_BUS
  254. current_bus = 0;
  255. #endif
  256. base = (struct sh_i2c *)CONFIG_SH_I2C_BASE0;
  257. if (speed == 400000)
  258. iccr1_cks = 0x07;
  259. else
  260. iccr1_cks = 0x0F;
  261. nf2cyc = 1;
  262. /* Reset */
  263. sh_i2c_reset(base);
  264. /* ICE enable and set clock */
  265. writeb(SH_I2C_ICCR1_ICE | iccr1_cks, &base->iccr1);
  266. writeb(nf2cyc, &base->nf2cyc);
  267. }
  268. /*
  269. * i2c_read: - Read multiple bytes from an i2c device
  270. *
  271. * The higher level routines take into account that this function is only
  272. * called with len < page length of the device (see configuration file)
  273. *
  274. * @chip: address of the chip which is to be read
  275. * @addr: i2c data address within the chip
  276. * @alen: length of the i2c data address (1..2 bytes)
  277. * @buffer: where to write the data
  278. * @len: how much byte do we want to read
  279. * @return: 0 in case of success
  280. */
  281. int i2c_read(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  282. {
  283. int i = 0;
  284. for (i = 0; i < len; i++)
  285. buffer[i] = i2c_raw_read(base, chip, addr + i);
  286. return 0;
  287. }
  288. /*
  289. * i2c_write: - Write multiple bytes to an i2c device
  290. *
  291. * The higher level routines take into account that this function is only
  292. * called with len < page length of the device (see configuration file)
  293. *
  294. * @chip: address of the chip which is to be written
  295. * @addr: i2c data address within the chip
  296. * @alen: length of the i2c data address (1..2 bytes)
  297. * @buffer: where to find the data to be written
  298. * @len: how much byte do we want to read
  299. * @return: 0 in case of success
  300. */
  301. int i2c_write(u8 chip, u32 addr, int alen, u8 *buffer, int len)
  302. {
  303. return i2c_raw_write(base, chip, addr, buffer, len);
  304. }
  305. /*
  306. * i2c_probe: - Test if a chip answers for a given i2c address
  307. *
  308. * @chip: address of the chip which is searched for
  309. * @return: 0 if a chip was found, -1 otherwhise
  310. */
  311. int i2c_probe(u8 chip)
  312. {
  313. u8 byte;
  314. return i2c_read(chip, 0, 0, &byte, 1);
  315. }