designware_i2c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. static struct i2c_regs *const i2c_regs_p =
  28. (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
  29. /*
  30. * set_speed - Set the i2c speed mode (standard, high, fast)
  31. * @i2c_spd: required i2c speed mode
  32. *
  33. * Set the i2c speed mode (standard, high, fast)
  34. */
  35. static void set_speed(int i2c_spd)
  36. {
  37. unsigned int cntl;
  38. unsigned int hcnt, lcnt;
  39. unsigned int high, low;
  40. unsigned int enbl;
  41. /* to set speed cltr must be disabled */
  42. enbl = readl(&i2c_regs_p->ic_enable);
  43. enbl &= ~IC_ENABLE_0B;
  44. writel(enbl, &i2c_regs_p->ic_enable);
  45. cntl = (readl(&i2c_regs_p->ic_con) & (~IC_CON_SPD_MSK));
  46. switch (i2c_spd) {
  47. case IC_SPEED_MODE_MAX:
  48. cntl |= IC_CON_SPD_HS;
  49. high = MIN_HS_SCL_HIGHTIME;
  50. low = MIN_HS_SCL_LOWTIME;
  51. break;
  52. case IC_SPEED_MODE_STANDARD:
  53. cntl |= IC_CON_SPD_SS;
  54. high = MIN_SS_SCL_HIGHTIME;
  55. low = MIN_SS_SCL_LOWTIME;
  56. break;
  57. case IC_SPEED_MODE_FAST:
  58. default:
  59. cntl |= IC_CON_SPD_FS;
  60. high = MIN_FS_SCL_HIGHTIME;
  61. low = MIN_FS_SCL_LOWTIME;
  62. break;
  63. }
  64. writel(cntl, &i2c_regs_p->ic_con);
  65. hcnt = (IC_CLK * high) / NANO_TO_MICRO;
  66. writel(hcnt, &i2c_regs_p->ic_fs_scl_hcnt);
  67. lcnt = (IC_CLK * low) / NANO_TO_MICRO;
  68. writel(lcnt, &i2c_regs_p->ic_fs_scl_lcnt);
  69. /* re-enable i2c ctrl back now that speed is set */
  70. enbl |= IC_ENABLE_0B;
  71. writel(enbl, &i2c_regs_p->ic_enable);
  72. }
  73. /*
  74. * i2c_set_bus_speed - Set the i2c speed
  75. * @speed: required i2c speed
  76. *
  77. * Set the i2c speed.
  78. */
  79. int i2c_set_bus_speed(int speed)
  80. {
  81. if (speed >= I2C_MAX_SPEED)
  82. set_speed(IC_SPEED_MODE_MAX);
  83. else if (speed >= I2C_FAST_SPEED)
  84. set_speed(IC_SPEED_MODE_FAST);
  85. else
  86. set_speed(IC_SPEED_MODE_STANDARD);
  87. return 0;
  88. }
  89. /*
  90. * i2c_get_bus_speed - Gets the i2c speed
  91. *
  92. * Gets the i2c speed.
  93. */
  94. int i2c_get_bus_speed(void)
  95. {
  96. u32 cntl;
  97. cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
  98. if (cntl == IC_CON_SPD_HS)
  99. return I2C_MAX_SPEED;
  100. else if (cntl == IC_CON_SPD_FS)
  101. return I2C_FAST_SPEED;
  102. else if (cntl == IC_CON_SPD_SS)
  103. return I2C_STANDARD_SPEED;
  104. return 0;
  105. }
  106. /*
  107. * i2c_init - Init function
  108. * @speed: required i2c speed
  109. * @slaveadd: slave address for the device
  110. *
  111. * Initialization function.
  112. */
  113. void i2c_init(int speed, int slaveadd)
  114. {
  115. unsigned int enbl;
  116. /* Disable i2c */
  117. enbl = readl(&i2c_regs_p->ic_enable);
  118. enbl &= ~IC_ENABLE_0B;
  119. writel(enbl, &i2c_regs_p->ic_enable);
  120. writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
  121. writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
  122. writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
  123. i2c_set_bus_speed(speed);
  124. writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
  125. writel(slaveadd, &i2c_regs_p->ic_sar);
  126. /* Enable i2c */
  127. enbl = readl(&i2c_regs_p->ic_enable);
  128. enbl |= IC_ENABLE_0B;
  129. writel(enbl, &i2c_regs_p->ic_enable);
  130. }
  131. /*
  132. * i2c_setaddress - Sets the target slave address
  133. * @i2c_addr: target i2c address
  134. *
  135. * Sets the target slave address.
  136. */
  137. static void i2c_setaddress(unsigned int i2c_addr)
  138. {
  139. writel(i2c_addr, &i2c_regs_p->ic_tar);
  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. /* check parameters for i2c_read and i2c_write */
  168. static int check_params(uint addr, int alen, uchar *buffer, int len)
  169. {
  170. if (buffer == NULL) {
  171. printf("Buffer is invalid\n");
  172. return 1;
  173. }
  174. if (alen > 1) {
  175. printf("addr len %d not supported\n", alen);
  176. return 1;
  177. }
  178. if (addr + len > 256) {
  179. printf("address out of range\n");
  180. return 1;
  181. }
  182. return 0;
  183. }
  184. static int i2c_xfer_init(uchar chip, uint addr)
  185. {
  186. if (i2c_wait_for_bb())
  187. return 1;
  188. i2c_setaddress(chip);
  189. writel(addr, &i2c_regs_p->ic_cmd_data);
  190. return 0;
  191. }
  192. static int i2c_xfer_finish(void)
  193. {
  194. ulong start_stop_det = get_timer(0);
  195. while (1) {
  196. if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
  197. readl(&i2c_regs_p->ic_clr_stop_det);
  198. break;
  199. } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
  200. break;
  201. }
  202. }
  203. if (i2c_wait_for_bb()) {
  204. printf("Timed out waiting for bus\n");
  205. return 1;
  206. }
  207. i2c_flush_rxfifo();
  208. /* Wait for read/write operation to complete on actual memory */
  209. udelay(10000);
  210. return 0;
  211. }
  212. /*
  213. * i2c_read - Read from i2c memory
  214. * @chip: target i2c address
  215. * @addr: address to read from
  216. * @alen:
  217. * @buffer: buffer for read data
  218. * @len: no of bytes to be read
  219. *
  220. * Read from i2c memory.
  221. */
  222. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  223. {
  224. unsigned long start_time_rx;
  225. if (check_params(addr, alen, buffer, len))
  226. return 1;
  227. if (i2c_xfer_init(chip, addr))
  228. return 1;
  229. start_time_rx = get_timer(0);
  230. while (len) {
  231. writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
  232. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
  233. *buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
  234. len--;
  235. start_time_rx = get_timer(0);
  236. } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
  237. return 1;
  238. }
  239. }
  240. return i2c_xfer_finish();
  241. }
  242. /*
  243. * i2c_write - Write to i2c memory
  244. * @chip: target i2c address
  245. * @addr: address to read from
  246. * @alen:
  247. * @buffer: buffer for read data
  248. * @len: no of bytes to be read
  249. *
  250. * Write to i2c memory.
  251. */
  252. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  253. {
  254. int nb = len;
  255. unsigned long start_time_tx;
  256. if (check_params(addr, alen, buffer, len))
  257. return 1;
  258. if (i2c_xfer_init(chip, addr))
  259. return 1;
  260. start_time_tx = get_timer(0);
  261. while (len) {
  262. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
  263. writel(*buffer, &i2c_regs_p->ic_cmd_data);
  264. buffer++;
  265. len--;
  266. start_time_tx = get_timer(0);
  267. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  268. printf("Timed out. i2c write Failed\n");
  269. return 1;
  270. }
  271. }
  272. return i2c_xfer_finish();
  273. }
  274. /*
  275. * i2c_probe - Probe the i2c chip
  276. */
  277. int i2c_probe(uchar chip)
  278. {
  279. u32 tmp;
  280. int ret;
  281. /*
  282. * Try to read the first location of the chip.
  283. */
  284. ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  285. if (ret)
  286. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  287. return ret;
  288. }