sh_sh7734_i2c.c 7.4 KB

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