designware_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <asm/arch/hardware.h>
  10. #include "designware_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(int speed)
  69. {
  70. if (speed >= I2C_MAX_SPEED)
  71. set_speed(IC_SPEED_MODE_MAX);
  72. else if (speed >= I2C_FAST_SPEED)
  73. set_speed(IC_SPEED_MODE_FAST);
  74. else
  75. set_speed(IC_SPEED_MODE_STANDARD);
  76. return 0;
  77. }
  78. /*
  79. * i2c_get_bus_speed - Gets the i2c speed
  80. *
  81. * Gets the i2c speed.
  82. */
  83. int i2c_get_bus_speed(void)
  84. {
  85. u32 cntl;
  86. cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
  87. if (cntl == IC_CON_SPD_HS)
  88. return I2C_MAX_SPEED;
  89. else if (cntl == IC_CON_SPD_FS)
  90. return I2C_FAST_SPEED;
  91. else if (cntl == IC_CON_SPD_SS)
  92. return I2C_STANDARD_SPEED;
  93. return 0;
  94. }
  95. /*
  96. * i2c_init - Init function
  97. * @speed: required i2c speed
  98. * @slaveadd: slave address for the device
  99. *
  100. * Initialization function.
  101. */
  102. void i2c_init(int speed, int slaveadd)
  103. {
  104. unsigned int enbl;
  105. /* Disable i2c */
  106. enbl = readl(&i2c_regs_p->ic_enable);
  107. enbl &= ~IC_ENABLE_0B;
  108. writel(enbl, &i2c_regs_p->ic_enable);
  109. writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
  110. writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
  111. writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
  112. i2c_set_bus_speed(speed);
  113. writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
  114. writel(slaveadd, &i2c_regs_p->ic_sar);
  115. /* Enable i2c */
  116. enbl = readl(&i2c_regs_p->ic_enable);
  117. enbl |= IC_ENABLE_0B;
  118. writel(enbl, &i2c_regs_p->ic_enable);
  119. #ifdef CONFIG_I2C_MULTI_BUS
  120. bus_initialized[current_bus] = 1;
  121. #endif
  122. }
  123. /*
  124. * i2c_setaddress - Sets the target slave address
  125. * @i2c_addr: target i2c address
  126. *
  127. * Sets the target slave address.
  128. */
  129. static void i2c_setaddress(unsigned int i2c_addr)
  130. {
  131. unsigned int enbl;
  132. /* Disable i2c */
  133. enbl = readl(&i2c_regs_p->ic_enable);
  134. enbl &= ~IC_ENABLE_0B;
  135. writel(enbl, &i2c_regs_p->ic_enable);
  136. writel(i2c_addr, &i2c_regs_p->ic_tar);
  137. /* Enable i2c */
  138. enbl = readl(&i2c_regs_p->ic_enable);
  139. enbl |= IC_ENABLE_0B;
  140. writel(enbl, &i2c_regs_p->ic_enable);
  141. }
  142. /*
  143. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  144. *
  145. * Flushes the i2c RX FIFO
  146. */
  147. static void i2c_flush_rxfifo(void)
  148. {
  149. while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
  150. readl(&i2c_regs_p->ic_cmd_data);
  151. }
  152. /*
  153. * i2c_wait_for_bb - Waits for bus busy
  154. *
  155. * Waits for bus busy
  156. */
  157. static int i2c_wait_for_bb(void)
  158. {
  159. unsigned long start_time_bb = get_timer(0);
  160. while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
  161. !(readl(&i2c_regs_p->ic_status) & IC_STATUS_TFE)) {
  162. /* Evaluate timeout */
  163. if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. /* check parameters for i2c_read and i2c_write */
  169. static int check_params(uint addr, int alen, uchar *buffer, int len)
  170. {
  171. if (buffer == NULL) {
  172. printf("Buffer is invalid\n");
  173. return 1;
  174. }
  175. if (alen > 1) {
  176. printf("addr len %d not supported\n", alen);
  177. return 1;
  178. }
  179. if (addr + len > 256) {
  180. printf("address out of range\n");
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. static int i2c_xfer_init(uchar chip, uint addr)
  186. {
  187. if (i2c_wait_for_bb())
  188. return 1;
  189. i2c_setaddress(chip);
  190. writel(addr, &i2c_regs_p->ic_cmd_data);
  191. return 0;
  192. }
  193. static int i2c_xfer_finish(void)
  194. {
  195. ulong start_stop_det = get_timer(0);
  196. while (1) {
  197. if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
  198. readl(&i2c_regs_p->ic_clr_stop_det);
  199. break;
  200. } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
  201. break;
  202. }
  203. }
  204. if (i2c_wait_for_bb()) {
  205. printf("Timed out waiting for bus\n");
  206. return 1;
  207. }
  208. i2c_flush_rxfifo();
  209. /* Wait for read/write operation to complete on actual memory */
  210. udelay(10000);
  211. return 0;
  212. }
  213. /*
  214. * i2c_read - Read from i2c memory
  215. * @chip: target i2c address
  216. * @addr: address to read from
  217. * @alen:
  218. * @buffer: buffer for read data
  219. * @len: no of bytes to be read
  220. *
  221. * Read from i2c memory.
  222. */
  223. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  224. {
  225. unsigned long start_time_rx;
  226. if (check_params(addr, alen, buffer, len))
  227. return 1;
  228. if (i2c_xfer_init(chip, addr))
  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. if (check_params(addr, alen, buffer, len))
  261. return 1;
  262. if (i2c_xfer_init(chip, addr))
  263. return 1;
  264. start_time_tx = get_timer(0);
  265. while (len) {
  266. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
  267. if (--len == 0)
  268. writel(*buffer | IC_STOP, &i2c_regs_p->ic_cmd_data);
  269. else
  270. writel(*buffer, &i2c_regs_p->ic_cmd_data);
  271. buffer++;
  272. start_time_tx = get_timer(0);
  273. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  274. printf("Timed out. i2c write Failed\n");
  275. return 1;
  276. }
  277. }
  278. return i2c_xfer_finish();
  279. }
  280. /*
  281. * i2c_probe - Probe the i2c chip
  282. */
  283. int i2c_probe(uchar chip)
  284. {
  285. u32 tmp;
  286. int ret;
  287. /*
  288. * Try to read the first location of the chip.
  289. */
  290. ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  291. if (ret)
  292. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  293. return ret;
  294. }
  295. #ifdef CONFIG_I2C_MULTI_BUS
  296. int i2c_set_bus_num(unsigned int bus)
  297. {
  298. switch (bus) {
  299. case 0:
  300. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE;
  301. break;
  302. #ifdef CONFIG_SYS_I2C_BASE1
  303. case 1:
  304. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE1;
  305. break;
  306. #endif
  307. #ifdef CONFIG_SYS_I2C_BASE2
  308. case 2:
  309. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE2;
  310. break;
  311. #endif
  312. #ifdef CONFIG_SYS_I2C_BASE3
  313. case 3:
  314. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE3;
  315. break;
  316. #endif
  317. #ifdef CONFIG_SYS_I2C_BASE4
  318. case 4:
  319. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE4;
  320. break;
  321. #endif
  322. #ifdef CONFIG_SYS_I2C_BASE5
  323. case 5:
  324. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE5;
  325. break;
  326. #endif
  327. #ifdef CONFIG_SYS_I2C_BASE6
  328. case 6:
  329. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE6;
  330. break;
  331. #endif
  332. #ifdef CONFIG_SYS_I2C_BASE7
  333. case 7:
  334. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE7;
  335. break;
  336. #endif
  337. #ifdef CONFIG_SYS_I2C_BASE8
  338. case 8:
  339. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE8;
  340. break;
  341. #endif
  342. #ifdef CONFIG_SYS_I2C_BASE9
  343. case 9:
  344. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE9;
  345. break;
  346. #endif
  347. default:
  348. printf("Bad bus: %d\n", bus);
  349. return -1;
  350. }
  351. current_bus = bus;
  352. if (!bus_initialized[current_bus])
  353. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  354. return 0;
  355. }
  356. int i2c_get_bus_num(void)
  357. {
  358. return current_bus;
  359. }
  360. #endif