sh_sh7734_i2c.c 7.4 KB

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