designware_i2c.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. writel(i2c_addr, &i2c_regs_p->ic_tar);
  132. }
  133. /*
  134. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  135. *
  136. * Flushes the i2c RX FIFO
  137. */
  138. static void i2c_flush_rxfifo(void)
  139. {
  140. while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
  141. readl(&i2c_regs_p->ic_cmd_data);
  142. }
  143. /*
  144. * i2c_wait_for_bb - Waits for bus busy
  145. *
  146. * Waits for bus busy
  147. */
  148. static int i2c_wait_for_bb(void)
  149. {
  150. unsigned long start_time_bb = get_timer(0);
  151. while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
  152. !(readl(&i2c_regs_p->ic_status) & IC_STATUS_TFE)) {
  153. /* Evaluate timeout */
  154. if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. /* check parameters for i2c_read and i2c_write */
  160. static int check_params(uint addr, int alen, uchar *buffer, int len)
  161. {
  162. if (buffer == NULL) {
  163. printf("Buffer is invalid\n");
  164. return 1;
  165. }
  166. if (alen > 1) {
  167. printf("addr len %d not supported\n", alen);
  168. return 1;
  169. }
  170. if (addr + len > 256) {
  171. printf("address out of range\n");
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. static int i2c_xfer_init(uchar chip, uint addr)
  177. {
  178. if (i2c_wait_for_bb())
  179. return 1;
  180. i2c_setaddress(chip);
  181. writel(addr, &i2c_regs_p->ic_cmd_data);
  182. return 0;
  183. }
  184. static int i2c_xfer_finish(void)
  185. {
  186. ulong start_stop_det = get_timer(0);
  187. while (1) {
  188. if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
  189. readl(&i2c_regs_p->ic_clr_stop_det);
  190. break;
  191. } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
  192. break;
  193. }
  194. }
  195. if (i2c_wait_for_bb()) {
  196. printf("Timed out waiting for bus\n");
  197. return 1;
  198. }
  199. i2c_flush_rxfifo();
  200. /* Wait for read/write operation to complete on actual memory */
  201. udelay(10000);
  202. return 0;
  203. }
  204. /*
  205. * i2c_read - Read from i2c memory
  206. * @chip: target i2c address
  207. * @addr: address to read from
  208. * @alen:
  209. * @buffer: buffer for read data
  210. * @len: no of bytes to be read
  211. *
  212. * Read from i2c memory.
  213. */
  214. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  215. {
  216. unsigned long start_time_rx;
  217. if (check_params(addr, alen, buffer, len))
  218. return 1;
  219. if (i2c_xfer_init(chip, addr))
  220. return 1;
  221. start_time_rx = get_timer(0);
  222. while (len) {
  223. if (len == 1)
  224. writel(IC_CMD | IC_STOP, &i2c_regs_p->ic_cmd_data);
  225. else
  226. writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
  227. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
  228. *buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
  229. len--;
  230. start_time_rx = get_timer(0);
  231. } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
  232. return 1;
  233. }
  234. }
  235. return i2c_xfer_finish();
  236. }
  237. /*
  238. * i2c_write - Write to i2c memory
  239. * @chip: target i2c address
  240. * @addr: address to read from
  241. * @alen:
  242. * @buffer: buffer for read data
  243. * @len: no of bytes to be read
  244. *
  245. * Write to i2c memory.
  246. */
  247. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  248. {
  249. int nb = len;
  250. unsigned long start_time_tx;
  251. if (check_params(addr, alen, buffer, len))
  252. return 1;
  253. if (i2c_xfer_init(chip, addr))
  254. return 1;
  255. start_time_tx = get_timer(0);
  256. while (len) {
  257. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
  258. if (--len == 0)
  259. writel(*buffer | IC_STOP, &i2c_regs_p->ic_cmd_data);
  260. else
  261. writel(*buffer, &i2c_regs_p->ic_cmd_data);
  262. buffer++;
  263. start_time_tx = get_timer(0);
  264. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  265. printf("Timed out. i2c write Failed\n");
  266. return 1;
  267. }
  268. }
  269. return i2c_xfer_finish();
  270. }
  271. /*
  272. * i2c_probe - Probe the i2c chip
  273. */
  274. int i2c_probe(uchar chip)
  275. {
  276. u32 tmp;
  277. int ret;
  278. /*
  279. * Try to read the first location of the chip.
  280. */
  281. ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  282. if (ret)
  283. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  284. return ret;
  285. }
  286. #ifdef CONFIG_I2C_MULTI_BUS
  287. int i2c_set_bus_num(unsigned int bus)
  288. {
  289. switch (bus) {
  290. case 0:
  291. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE;
  292. break;
  293. #ifdef CONFIG_SYS_I2C_BASE1
  294. case 1:
  295. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE1;
  296. break;
  297. #endif
  298. #ifdef CONFIG_SYS_I2C_BASE2
  299. case 2:
  300. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE2;
  301. break;
  302. #endif
  303. #ifdef CONFIG_SYS_I2C_BASE3
  304. case 3:
  305. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE3;
  306. break;
  307. #endif
  308. #ifdef CONFIG_SYS_I2C_BASE4
  309. case 4:
  310. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE4;
  311. break;
  312. #endif
  313. #ifdef CONFIG_SYS_I2C_BASE5
  314. case 5:
  315. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE5;
  316. break;
  317. #endif
  318. #ifdef CONFIG_SYS_I2C_BASE6
  319. case 6:
  320. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE6;
  321. break;
  322. #endif
  323. #ifdef CONFIG_SYS_I2C_BASE7
  324. case 7:
  325. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE7;
  326. break;
  327. #endif
  328. #ifdef CONFIG_SYS_I2C_BASE8
  329. case 8:
  330. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE8;
  331. break;
  332. #endif
  333. #ifdef CONFIG_SYS_I2C_BASE9
  334. case 9:
  335. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE9;
  336. break;
  337. #endif
  338. default:
  339. printf("Bad bus: %d\n", bus);
  340. return -1;
  341. }
  342. current_bus = bus;
  343. if (!bus_initialized[current_bus])
  344. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  345. return 0;
  346. }
  347. int i2c_get_bus_num(void)
  348. {
  349. return current_bus;
  350. }
  351. #endif