fti2c010.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Faraday I2C Controller
  3. *
  4. * (C) Copyright 2010 Faraday Technology
  5. * Dante Su <dantesu@faraday-tech.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. * NOTE: This driver should be converted to driver model before June 2017.
  10. * Please see doc/driver-model/i2c-howto.txt for instructions.
  11. */
  12. #include <common.h>
  13. #include <asm/io.h>
  14. #include <i2c.h>
  15. #include "fti2c010.h"
  16. #ifndef CONFIG_SYS_I2C_SPEED
  17. #define CONFIG_SYS_I2C_SPEED 5000
  18. #endif
  19. #ifndef CONFIG_SYS_I2C_SLAVE
  20. #define CONFIG_SYS_I2C_SLAVE 0
  21. #endif
  22. #ifndef CONFIG_FTI2C010_CLOCK
  23. #define CONFIG_FTI2C010_CLOCK clk_get_rate("I2C")
  24. #endif
  25. #ifndef CONFIG_FTI2C010_TIMEOUT
  26. #define CONFIG_FTI2C010_TIMEOUT 10 /* ms */
  27. #endif
  28. /* 7-bit dev address + 1-bit read/write */
  29. #define I2C_RD(dev) ((((dev) << 1) & 0xfe) | 1)
  30. #define I2C_WR(dev) (((dev) << 1) & 0xfe)
  31. struct fti2c010_chip {
  32. struct fti2c010_regs *regs;
  33. };
  34. static struct fti2c010_chip chip_list[] = {
  35. {
  36. .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE,
  37. },
  38. #ifdef CONFIG_FTI2C010_BASE1
  39. {
  40. .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE1,
  41. },
  42. #endif
  43. #ifdef CONFIG_FTI2C010_BASE2
  44. {
  45. .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE2,
  46. },
  47. #endif
  48. #ifdef CONFIG_FTI2C010_BASE3
  49. {
  50. .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE3,
  51. },
  52. #endif
  53. };
  54. static int fti2c010_reset(struct fti2c010_chip *chip)
  55. {
  56. ulong ts;
  57. int ret = -1;
  58. struct fti2c010_regs *regs = chip->regs;
  59. writel(CR_I2CRST, &regs->cr);
  60. for (ts = get_timer(0); get_timer(ts) < CONFIG_FTI2C010_TIMEOUT; ) {
  61. if (!(readl(&regs->cr) & CR_I2CRST)) {
  62. ret = 0;
  63. break;
  64. }
  65. }
  66. if (ret)
  67. printf("fti2c010: reset timeout\n");
  68. return ret;
  69. }
  70. static int fti2c010_wait(struct fti2c010_chip *chip, uint32_t mask)
  71. {
  72. int ret = -1;
  73. uint32_t stat, ts;
  74. struct fti2c010_regs *regs = chip->regs;
  75. for (ts = get_timer(0); get_timer(ts) < CONFIG_FTI2C010_TIMEOUT; ) {
  76. stat = readl(&regs->sr);
  77. if ((stat & mask) == mask) {
  78. ret = 0;
  79. break;
  80. }
  81. }
  82. return ret;
  83. }
  84. static unsigned int set_i2c_bus_speed(struct fti2c010_chip *chip,
  85. unsigned int speed)
  86. {
  87. struct fti2c010_regs *regs = chip->regs;
  88. unsigned int clk = CONFIG_FTI2C010_CLOCK;
  89. unsigned int gsr = 0;
  90. unsigned int tsr = 32;
  91. unsigned int div, rate;
  92. for (div = 0; div < 0x3ffff; ++div) {
  93. /* SCLout = PCLK/(2*(COUNT + 2) + GSR) */
  94. rate = clk / (2 * (div + 2) + gsr);
  95. if (rate <= speed)
  96. break;
  97. }
  98. writel(TGSR_GSR(gsr) | TGSR_TSR(tsr), &regs->tgsr);
  99. writel(CDR_DIV(div), &regs->cdr);
  100. return rate;
  101. }
  102. /*
  103. * Initialization, must be called once on start up, may be called
  104. * repeatedly to change the speed and slave addresses.
  105. */
  106. static void fti2c010_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  107. {
  108. struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
  109. if (adap->init_done)
  110. return;
  111. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  112. /* Call board specific i2c bus reset routine before accessing the
  113. * environment, which might be in a chip on that bus. For details
  114. * about this problem see doc/I2C_Edge_Conditions.
  115. */
  116. i2c_init_board();
  117. #endif
  118. /* master init */
  119. fti2c010_reset(chip);
  120. set_i2c_bus_speed(chip, speed);
  121. /* slave init, don't care */
  122. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  123. /* Call board specific i2c bus reset routine AFTER the bus has been
  124. * initialized. Use either this callpoint or i2c_init_board;
  125. * which is called before fti2c010_init operations.
  126. * For details about this problem see doc/I2C_Edge_Conditions.
  127. */
  128. i2c_board_late_init();
  129. #endif
  130. }
  131. /*
  132. * Probe the given I2C chip address. Returns 0 if a chip responded,
  133. * not 0 on failure.
  134. */
  135. static int fti2c010_probe(struct i2c_adapter *adap, u8 dev)
  136. {
  137. struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
  138. struct fti2c010_regs *regs = chip->regs;
  139. int ret;
  140. /* 1. Select slave device (7bits Address + 1bit R/W) */
  141. writel(I2C_WR(dev), &regs->dr);
  142. writel(CR_ENABLE | CR_TBEN | CR_START, &regs->cr);
  143. ret = fti2c010_wait(chip, SR_DT);
  144. if (ret)
  145. return ret;
  146. /* 2. Select device register */
  147. writel(0, &regs->dr);
  148. writel(CR_ENABLE | CR_TBEN, &regs->cr);
  149. ret = fti2c010_wait(chip, SR_DT);
  150. return ret;
  151. }
  152. static void to_i2c_addr(u8 *buf, uint32_t addr, int alen)
  153. {
  154. int i, shift;
  155. if (!buf || alen <= 0)
  156. return;
  157. /* MSB first */
  158. i = 0;
  159. shift = (alen - 1) * 8;
  160. while (alen-- > 0) {
  161. buf[i] = (u8)(addr >> shift);
  162. shift -= 8;
  163. }
  164. }
  165. static int fti2c010_read(struct i2c_adapter *adap,
  166. u8 dev, uint addr, int alen, uchar *buf, int len)
  167. {
  168. struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
  169. struct fti2c010_regs *regs = chip->regs;
  170. int ret, pos;
  171. uchar paddr[4] = { 0 };
  172. to_i2c_addr(paddr, addr, alen);
  173. /*
  174. * Phase A. Set register address
  175. */
  176. /* A.1 Select slave device (7bits Address + 1bit R/W) */
  177. writel(I2C_WR(dev), &regs->dr);
  178. writel(CR_ENABLE | CR_TBEN | CR_START, &regs->cr);
  179. ret = fti2c010_wait(chip, SR_DT);
  180. if (ret)
  181. return ret;
  182. /* A.2 Select device register */
  183. for (pos = 0; pos < alen; ++pos) {
  184. uint32_t ctrl = CR_ENABLE | CR_TBEN;
  185. writel(paddr[pos], &regs->dr);
  186. writel(ctrl, &regs->cr);
  187. ret = fti2c010_wait(chip, SR_DT);
  188. if (ret)
  189. return ret;
  190. }
  191. /*
  192. * Phase B. Get register data
  193. */
  194. /* B.1 Select slave device (7bits Address + 1bit R/W) */
  195. writel(I2C_RD(dev), &regs->dr);
  196. writel(CR_ENABLE | CR_TBEN | CR_START, &regs->cr);
  197. ret = fti2c010_wait(chip, SR_DT);
  198. if (ret)
  199. return ret;
  200. /* B.2 Get register data */
  201. for (pos = 0; pos < len; ++pos) {
  202. uint32_t ctrl = CR_ENABLE | CR_TBEN;
  203. uint32_t stat = SR_DR;
  204. if (pos == len - 1) {
  205. ctrl |= CR_NAK | CR_STOP;
  206. stat |= SR_ACK;
  207. }
  208. writel(ctrl, &regs->cr);
  209. ret = fti2c010_wait(chip, stat);
  210. if (ret)
  211. break;
  212. buf[pos] = (uchar)(readl(&regs->dr) & 0xFF);
  213. }
  214. return ret;
  215. }
  216. static int fti2c010_write(struct i2c_adapter *adap,
  217. u8 dev, uint addr, int alen, u8 *buf, int len)
  218. {
  219. struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
  220. struct fti2c010_regs *regs = chip->regs;
  221. int ret, pos;
  222. uchar paddr[4] = { 0 };
  223. to_i2c_addr(paddr, addr, alen);
  224. /*
  225. * Phase A. Set register address
  226. *
  227. * A.1 Select slave device (7bits Address + 1bit R/W)
  228. */
  229. writel(I2C_WR(dev), &regs->dr);
  230. writel(CR_ENABLE | CR_TBEN | CR_START, &regs->cr);
  231. ret = fti2c010_wait(chip, SR_DT);
  232. if (ret)
  233. return ret;
  234. /* A.2 Select device register */
  235. for (pos = 0; pos < alen; ++pos) {
  236. uint32_t ctrl = CR_ENABLE | CR_TBEN;
  237. writel(paddr[pos], &regs->dr);
  238. writel(ctrl, &regs->cr);
  239. ret = fti2c010_wait(chip, SR_DT);
  240. if (ret)
  241. return ret;
  242. }
  243. /*
  244. * Phase B. Set register data
  245. */
  246. for (pos = 0; pos < len; ++pos) {
  247. uint32_t ctrl = CR_ENABLE | CR_TBEN;
  248. if (pos == len - 1)
  249. ctrl |= CR_STOP;
  250. writel(buf[pos], &regs->dr);
  251. writel(ctrl, &regs->cr);
  252. ret = fti2c010_wait(chip, SR_DT);
  253. if (ret)
  254. break;
  255. }
  256. return ret;
  257. }
  258. static unsigned int fti2c010_set_bus_speed(struct i2c_adapter *adap,
  259. unsigned int speed)
  260. {
  261. struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
  262. int ret;
  263. fti2c010_reset(chip);
  264. ret = set_i2c_bus_speed(chip, speed);
  265. return ret;
  266. }
  267. /*
  268. * Register i2c adapters
  269. */
  270. U_BOOT_I2C_ADAP_COMPLETE(i2c_0, fti2c010_init, fti2c010_probe, fti2c010_read,
  271. fti2c010_write, fti2c010_set_bus_speed,
  272. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
  273. 0)
  274. #ifdef CONFIG_FTI2C010_BASE1
  275. U_BOOT_I2C_ADAP_COMPLETE(i2c_1, fti2c010_init, fti2c010_probe, fti2c010_read,
  276. fti2c010_write, fti2c010_set_bus_speed,
  277. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
  278. 1)
  279. #endif
  280. #ifdef CONFIG_FTI2C010_BASE2
  281. U_BOOT_I2C_ADAP_COMPLETE(i2c_2, fti2c010_init, fti2c010_probe, fti2c010_read,
  282. fti2c010_write, fti2c010_set_bus_speed,
  283. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
  284. 2)
  285. #endif
  286. #ifdef CONFIG_FTI2C010_BASE3
  287. U_BOOT_I2C_ADAP_COMPLETE(i2c_3, fti2c010_init, fti2c010_probe, fti2c010_read,
  288. fti2c010_write, fti2c010_set_bus_speed,
  289. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
  290. 3)
  291. #endif