designware_i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * (C) Copyright 2009
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <asm/io.h>
  10. #include "designware_i2c.h"
  11. static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
  12. {
  13. switch (adap->hwadapnr) {
  14. #if CONFIG_SYS_I2C_BUS_MAX >= 4
  15. case 3:
  16. return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
  17. #endif
  18. #if CONFIG_SYS_I2C_BUS_MAX >= 3
  19. case 2:
  20. return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
  21. #endif
  22. #if CONFIG_SYS_I2C_BUS_MAX >= 2
  23. case 1:
  24. return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
  25. #endif
  26. case 0:
  27. return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
  28. default:
  29. printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
  30. }
  31. return NULL;
  32. }
  33. static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
  34. {
  35. u32 ena = enable ? IC_ENABLE_0B : 0;
  36. int timeout = 100;
  37. do {
  38. writel(ena, &i2c_base->ic_enable);
  39. if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
  40. return;
  41. /*
  42. * Wait 10 times the signaling period of the highest I2C
  43. * transfer supported by the driver (for 400KHz this is
  44. * 25us) as described in the DesignWare I2C databook.
  45. */
  46. udelay(25);
  47. } while (timeout--);
  48. printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
  49. }
  50. /*
  51. * i2c_set_bus_speed - Set the i2c speed
  52. * @speed: required i2c speed
  53. *
  54. * Set the i2c speed.
  55. */
  56. static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
  57. unsigned int speed)
  58. {
  59. struct i2c_regs *i2c_base = i2c_get_base(adap);
  60. unsigned int cntl;
  61. unsigned int hcnt, lcnt;
  62. int i2c_spd;
  63. if (speed >= I2C_MAX_SPEED)
  64. i2c_spd = IC_SPEED_MODE_MAX;
  65. else if (speed >= I2C_FAST_SPEED)
  66. i2c_spd = IC_SPEED_MODE_FAST;
  67. else
  68. i2c_spd = IC_SPEED_MODE_STANDARD;
  69. /* to set speed cltr must be disabled */
  70. dw_i2c_enable(i2c_base, false);
  71. cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
  72. switch (i2c_spd) {
  73. case IC_SPEED_MODE_MAX:
  74. cntl |= IC_CON_SPD_HS;
  75. hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
  76. writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
  77. lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
  78. writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
  79. break;
  80. case IC_SPEED_MODE_STANDARD:
  81. cntl |= IC_CON_SPD_SS;
  82. hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
  83. writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
  84. lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
  85. writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
  86. break;
  87. case IC_SPEED_MODE_FAST:
  88. default:
  89. cntl |= IC_CON_SPD_FS;
  90. hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
  91. writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
  92. lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
  93. writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
  94. break;
  95. }
  96. writel(cntl, &i2c_base->ic_con);
  97. /* Enable back i2c now speed set */
  98. dw_i2c_enable(i2c_base, true);
  99. adap->speed = speed;
  100. return 0;
  101. }
  102. /*
  103. * i2c_init - Init function
  104. * @speed: required i2c speed
  105. * @slaveaddr: slave address for the device
  106. *
  107. * Initialization function.
  108. */
  109. static void dw_i2c_init(struct i2c_adapter *adap, int speed,
  110. int slaveaddr)
  111. {
  112. struct i2c_regs *i2c_base = i2c_get_base(adap);
  113. /* Disable i2c */
  114. dw_i2c_enable(i2c_base, false);
  115. writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
  116. writel(IC_RX_TL, &i2c_base->ic_rx_tl);
  117. writel(IC_TX_TL, &i2c_base->ic_tx_tl);
  118. dw_i2c_set_bus_speed(adap, speed);
  119. writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
  120. writel(slaveaddr, &i2c_base->ic_sar);
  121. /* Enable i2c */
  122. dw_i2c_enable(i2c_base, true);
  123. }
  124. /*
  125. * i2c_setaddress - Sets the target slave address
  126. * @i2c_addr: target i2c address
  127. *
  128. * Sets the target slave address.
  129. */
  130. static void i2c_setaddress(struct i2c_adapter *adap, unsigned int i2c_addr)
  131. {
  132. struct i2c_regs *i2c_base = i2c_get_base(adap);
  133. /* Disable i2c */
  134. dw_i2c_enable(i2c_base, false);
  135. writel(i2c_addr, &i2c_base->ic_tar);
  136. /* Enable i2c */
  137. dw_i2c_enable(i2c_base, true);
  138. }
  139. /*
  140. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  141. *
  142. * Flushes the i2c RX FIFO
  143. */
  144. static void i2c_flush_rxfifo(struct i2c_adapter *adap)
  145. {
  146. struct i2c_regs *i2c_base = i2c_get_base(adap);
  147. while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
  148. readl(&i2c_base->ic_cmd_data);
  149. }
  150. /*
  151. * i2c_wait_for_bb - Waits for bus busy
  152. *
  153. * Waits for bus busy
  154. */
  155. static int i2c_wait_for_bb(struct i2c_adapter *adap)
  156. {
  157. struct i2c_regs *i2c_base = i2c_get_base(adap);
  158. unsigned long start_time_bb = get_timer(0);
  159. while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
  160. !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
  161. /* Evaluate timeout */
  162. if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. static int i2c_xfer_init(struct i2c_adapter *adap, uchar chip, uint addr,
  168. int alen)
  169. {
  170. struct i2c_regs *i2c_base = i2c_get_base(adap);
  171. if (i2c_wait_for_bb(adap))
  172. return 1;
  173. i2c_setaddress(adap, chip);
  174. while (alen) {
  175. alen--;
  176. /* high byte address going out first */
  177. writel((addr >> (alen * 8)) & 0xff,
  178. &i2c_base->ic_cmd_data);
  179. }
  180. return 0;
  181. }
  182. static int i2c_xfer_finish(struct i2c_adapter *adap)
  183. {
  184. struct i2c_regs *i2c_base = i2c_get_base(adap);
  185. ulong start_stop_det = get_timer(0);
  186. while (1) {
  187. if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
  188. readl(&i2c_base->ic_clr_stop_det);
  189. break;
  190. } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
  191. break;
  192. }
  193. }
  194. if (i2c_wait_for_bb(adap)) {
  195. printf("Timed out waiting for bus\n");
  196. return 1;
  197. }
  198. i2c_flush_rxfifo(adap);
  199. return 0;
  200. }
  201. /*
  202. * i2c_read - Read from i2c memory
  203. * @chip: target i2c address
  204. * @addr: address to read from
  205. * @alen:
  206. * @buffer: buffer for read data
  207. * @len: no of bytes to be read
  208. *
  209. * Read from i2c memory.
  210. */
  211. static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
  212. int alen, u8 *buffer, int len)
  213. {
  214. struct i2c_regs *i2c_base = i2c_get_base(adap);
  215. unsigned long start_time_rx;
  216. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  217. /*
  218. * EEPROM chips that implement "address overflow" are ones
  219. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  220. * address and the extra bits end up in the "chip address"
  221. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  222. * four 256 byte chips.
  223. *
  224. * Note that we consider the length of the address field to
  225. * still be one byte because the extra address bits are
  226. * hidden in the chip address.
  227. */
  228. dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  229. addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
  230. debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
  231. addr);
  232. #endif
  233. if (i2c_xfer_init(adap, dev, addr, alen))
  234. return 1;
  235. start_time_rx = get_timer(0);
  236. while (len) {
  237. if (len == 1)
  238. writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
  239. else
  240. writel(IC_CMD, &i2c_base->ic_cmd_data);
  241. if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
  242. *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
  243. len--;
  244. start_time_rx = get_timer(0);
  245. } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
  246. return 1;
  247. }
  248. }
  249. return i2c_xfer_finish(adap);
  250. }
  251. /*
  252. * i2c_write - Write to i2c memory
  253. * @chip: target i2c address
  254. * @addr: address to read from
  255. * @alen:
  256. * @buffer: buffer for read data
  257. * @len: no of bytes to be read
  258. *
  259. * Write to i2c memory.
  260. */
  261. static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
  262. int alen, u8 *buffer, int len)
  263. {
  264. struct i2c_regs *i2c_base = i2c_get_base(adap);
  265. int nb = len;
  266. unsigned long start_time_tx;
  267. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  268. /*
  269. * EEPROM chips that implement "address overflow" are ones
  270. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  271. * address and the extra bits end up in the "chip address"
  272. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  273. * four 256 byte chips.
  274. *
  275. * Note that we consider the length of the address field to
  276. * still be one byte because the extra address bits are
  277. * hidden in the chip address.
  278. */
  279. dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  280. addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
  281. debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
  282. addr);
  283. #endif
  284. if (i2c_xfer_init(adap, dev, addr, alen))
  285. return 1;
  286. start_time_tx = get_timer(0);
  287. while (len) {
  288. if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
  289. if (--len == 0) {
  290. writel(*buffer | IC_STOP,
  291. &i2c_base->ic_cmd_data);
  292. } else {
  293. writel(*buffer, &i2c_base->ic_cmd_data);
  294. }
  295. buffer++;
  296. start_time_tx = get_timer(0);
  297. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  298. printf("Timed out. i2c write Failed\n");
  299. return 1;
  300. }
  301. }
  302. return i2c_xfer_finish(adap);
  303. }
  304. /*
  305. * i2c_probe - Probe the i2c chip
  306. */
  307. static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
  308. {
  309. u32 tmp;
  310. int ret;
  311. /*
  312. * Try to read the first location of the chip.
  313. */
  314. ret = dw_i2c_read(adap, dev, 0, 1, (uchar *)&tmp, 1);
  315. if (ret)
  316. dw_i2c_init(adap, adap->speed, adap->slaveaddr);
  317. return ret;
  318. }
  319. U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
  320. dw_i2c_write, dw_i2c_set_bus_speed,
  321. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  322. #if CONFIG_SYS_I2C_BUS_MAX >= 2
  323. U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
  324. dw_i2c_write, dw_i2c_set_bus_speed,
  325. CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
  326. #endif
  327. #if CONFIG_SYS_I2C_BUS_MAX >= 3
  328. U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
  329. dw_i2c_write, dw_i2c_set_bus_speed,
  330. CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
  331. #endif
  332. #if CONFIG_SYS_I2C_BUS_MAX >= 4
  333. U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
  334. dw_i2c_write, dw_i2c_set_bus_speed,
  335. CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
  336. #endif