designware_i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * (C) Copyright 2009
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/hardware.h>
  26. #include "designware_i2c.h"
  27. #ifdef CONFIG_I2C_MULTI_BUS
  28. static unsigned int bus_initialized[CONFIG_SYS_I2C_BUS_MAX];
  29. static unsigned int current_bus = 0;
  30. #endif
  31. static struct i2c_regs *i2c_regs_p =
  32. (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
  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(int i2c_spd)
  40. {
  41. unsigned int cntl;
  42. unsigned int hcnt, lcnt;
  43. unsigned int high, low;
  44. unsigned int enbl;
  45. /* to set speed cltr must be disabled */
  46. enbl = readl(&i2c_regs_p->ic_enable);
  47. enbl &= ~IC_ENABLE_0B;
  48. writel(enbl, &i2c_regs_p->ic_enable);
  49. cntl = (readl(&i2c_regs_p->ic_con) & (~IC_CON_SPD_MSK));
  50. switch (i2c_spd) {
  51. case IC_SPEED_MODE_MAX:
  52. cntl |= IC_CON_SPD_HS;
  53. high = MIN_HS_SCL_HIGHTIME;
  54. low = MIN_HS_SCL_LOWTIME;
  55. break;
  56. case IC_SPEED_MODE_STANDARD:
  57. cntl |= IC_CON_SPD_SS;
  58. high = MIN_SS_SCL_HIGHTIME;
  59. low = MIN_SS_SCL_LOWTIME;
  60. break;
  61. case IC_SPEED_MODE_FAST:
  62. default:
  63. cntl |= IC_CON_SPD_FS;
  64. high = MIN_FS_SCL_HIGHTIME;
  65. low = MIN_FS_SCL_LOWTIME;
  66. break;
  67. }
  68. writel(cntl, &i2c_regs_p->ic_con);
  69. hcnt = (IC_CLK * high) / NANO_TO_MICRO;
  70. writel(hcnt, &i2c_regs_p->ic_fs_scl_hcnt);
  71. lcnt = (IC_CLK * low) / NANO_TO_MICRO;
  72. writel(lcnt, &i2c_regs_p->ic_fs_scl_lcnt);
  73. /* re-enable i2c ctrl back now that speed is set */
  74. enbl |= IC_ENABLE_0B;
  75. writel(enbl, &i2c_regs_p->ic_enable);
  76. }
  77. /*
  78. * i2c_set_bus_speed - Set the i2c speed
  79. * @speed: required i2c speed
  80. *
  81. * Set the i2c speed.
  82. */
  83. int i2c_set_bus_speed(int speed)
  84. {
  85. if (speed >= I2C_MAX_SPEED)
  86. set_speed(IC_SPEED_MODE_MAX);
  87. else if (speed >= I2C_FAST_SPEED)
  88. set_speed(IC_SPEED_MODE_FAST);
  89. else
  90. set_speed(IC_SPEED_MODE_STANDARD);
  91. return 0;
  92. }
  93. /*
  94. * i2c_get_bus_speed - Gets the i2c speed
  95. *
  96. * Gets the i2c speed.
  97. */
  98. int i2c_get_bus_speed(void)
  99. {
  100. u32 cntl;
  101. cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
  102. if (cntl == IC_CON_SPD_HS)
  103. return I2C_MAX_SPEED;
  104. else if (cntl == IC_CON_SPD_FS)
  105. return I2C_FAST_SPEED;
  106. else if (cntl == IC_CON_SPD_SS)
  107. return I2C_STANDARD_SPEED;
  108. return 0;
  109. }
  110. /*
  111. * i2c_init - Init function
  112. * @speed: required i2c speed
  113. * @slaveadd: slave address for the device
  114. *
  115. * Initialization function.
  116. */
  117. void i2c_init(int speed, int slaveadd)
  118. {
  119. unsigned int enbl;
  120. /* Disable i2c */
  121. enbl = readl(&i2c_regs_p->ic_enable);
  122. enbl &= ~IC_ENABLE_0B;
  123. writel(enbl, &i2c_regs_p->ic_enable);
  124. writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
  125. writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
  126. writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
  127. i2c_set_bus_speed(speed);
  128. writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
  129. writel(slaveadd, &i2c_regs_p->ic_sar);
  130. /* Enable i2c */
  131. enbl = readl(&i2c_regs_p->ic_enable);
  132. enbl |= IC_ENABLE_0B;
  133. writel(enbl, &i2c_regs_p->ic_enable);
  134. #ifdef CONFIG_I2C_MULTI_BUS
  135. bus_initialized[current_bus] = 1;
  136. #endif
  137. }
  138. /*
  139. * i2c_setaddress - Sets the target slave address
  140. * @i2c_addr: target i2c address
  141. *
  142. * Sets the target slave address.
  143. */
  144. static void i2c_setaddress(unsigned int i2c_addr)
  145. {
  146. writel(i2c_addr, &i2c_regs_p->ic_tar);
  147. }
  148. /*
  149. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  150. *
  151. * Flushes the i2c RX FIFO
  152. */
  153. static void i2c_flush_rxfifo(void)
  154. {
  155. while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
  156. readl(&i2c_regs_p->ic_cmd_data);
  157. }
  158. /*
  159. * i2c_wait_for_bb - Waits for bus busy
  160. *
  161. * Waits for bus busy
  162. */
  163. static int i2c_wait_for_bb(void)
  164. {
  165. unsigned long start_time_bb = get_timer(0);
  166. while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
  167. !(readl(&i2c_regs_p->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. /* check parameters for i2c_read and i2c_write */
  175. static int check_params(uint addr, int alen, uchar *buffer, int len)
  176. {
  177. if (buffer == NULL) {
  178. printf("Buffer is invalid\n");
  179. return 1;
  180. }
  181. if (alen > 1) {
  182. printf("addr len %d not supported\n", alen);
  183. return 1;
  184. }
  185. if (addr + len > 256) {
  186. printf("address out of range\n");
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static int i2c_xfer_init(uchar chip, uint addr)
  192. {
  193. if (i2c_wait_for_bb())
  194. return 1;
  195. i2c_setaddress(chip);
  196. writel(addr, &i2c_regs_p->ic_cmd_data);
  197. return 0;
  198. }
  199. static int i2c_xfer_finish(void)
  200. {
  201. ulong start_stop_det = get_timer(0);
  202. while (1) {
  203. if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
  204. readl(&i2c_regs_p->ic_clr_stop_det);
  205. break;
  206. } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
  207. break;
  208. }
  209. }
  210. if (i2c_wait_for_bb()) {
  211. printf("Timed out waiting for bus\n");
  212. return 1;
  213. }
  214. i2c_flush_rxfifo();
  215. /* Wait for read/write operation to complete on actual memory */
  216. udelay(10000);
  217. return 0;
  218. }
  219. /*
  220. * i2c_read - Read from i2c memory
  221. * @chip: target i2c address
  222. * @addr: address to read from
  223. * @alen:
  224. * @buffer: buffer for read data
  225. * @len: no of bytes to be read
  226. *
  227. * Read from i2c memory.
  228. */
  229. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  230. {
  231. unsigned long start_time_rx;
  232. if (check_params(addr, alen, buffer, len))
  233. return 1;
  234. if (i2c_xfer_init(chip, addr))
  235. return 1;
  236. start_time_rx = get_timer(0);
  237. while (len) {
  238. if (len == 1)
  239. writel(IC_CMD | IC_STOP, &i2c_regs_p->ic_cmd_data);
  240. else
  241. writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
  242. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
  243. *buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
  244. len--;
  245. start_time_rx = get_timer(0);
  246. } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
  247. return 1;
  248. }
  249. }
  250. return i2c_xfer_finish();
  251. }
  252. /*
  253. * i2c_write - Write to i2c memory
  254. * @chip: target i2c address
  255. * @addr: address to read from
  256. * @alen:
  257. * @buffer: buffer for read data
  258. * @len: no of bytes to be read
  259. *
  260. * Write to i2c memory.
  261. */
  262. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  263. {
  264. int nb = len;
  265. unsigned long start_time_tx;
  266. if (check_params(addr, alen, buffer, len))
  267. return 1;
  268. if (i2c_xfer_init(chip, addr))
  269. return 1;
  270. start_time_tx = get_timer(0);
  271. while (len) {
  272. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
  273. if (--len == 0)
  274. writel(*buffer | IC_STOP, &i2c_regs_p->ic_cmd_data);
  275. else
  276. writel(*buffer, &i2c_regs_p->ic_cmd_data);
  277. buffer++;
  278. start_time_tx = get_timer(0);
  279. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  280. printf("Timed out. i2c write Failed\n");
  281. return 1;
  282. }
  283. }
  284. return i2c_xfer_finish();
  285. }
  286. /*
  287. * i2c_probe - Probe the i2c chip
  288. */
  289. int i2c_probe(uchar chip)
  290. {
  291. u32 tmp;
  292. int ret;
  293. /*
  294. * Try to read the first location of the chip.
  295. */
  296. ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  297. if (ret)
  298. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  299. return ret;
  300. }
  301. #ifdef CONFIG_I2C_MULTI_BUS
  302. int i2c_set_bus_num(unsigned int bus)
  303. {
  304. switch (bus) {
  305. case 0:
  306. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE;
  307. break;
  308. #ifdef CONFIG_SYS_I2C_BASE1
  309. case 1:
  310. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE1;
  311. break;
  312. #endif
  313. #ifdef CONFIG_SYS_I2C_BASE2
  314. case 2:
  315. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE2;
  316. break;
  317. #endif
  318. #ifdef CONFIG_SYS_I2C_BASE3
  319. case 3:
  320. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE3;
  321. break;
  322. #endif
  323. #ifdef CONFIG_SYS_I2C_BASE4
  324. case 4:
  325. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE4;
  326. break;
  327. #endif
  328. #ifdef CONFIG_SYS_I2C_BASE5
  329. case 5:
  330. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE5;
  331. break;
  332. #endif
  333. #ifdef CONFIG_SYS_I2C_BASE6
  334. case 6:
  335. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE6;
  336. break;
  337. #endif
  338. #ifdef CONFIG_SYS_I2C_BASE7
  339. case 7:
  340. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE7;
  341. break;
  342. #endif
  343. #ifdef CONFIG_SYS_I2C_BASE8
  344. case 8:
  345. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE8;
  346. break;
  347. #endif
  348. #ifdef CONFIG_SYS_I2C_BASE9
  349. case 9:
  350. i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE9;
  351. break;
  352. #endif
  353. default:
  354. printf("Bad bus: %d\n", bus);
  355. return -1;
  356. }
  357. current_bus = bus;
  358. if (!bus_initialized[current_bus])
  359. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  360. return 0;
  361. }
  362. int i2c_get_bus_num(void)
  363. {
  364. return current_bus;
  365. }
  366. #endif