designware_i2c.c 9.7 KB

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