designware_i2c.c 9.4 KB

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