sh_sh7734_i2c.c 7.3 KB

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