designware_i2c.c 9.5 KB

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