designware_i2c.c 9.6 KB

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