designware_i2c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. void 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. }
  88. /*
  89. * i2c_get_bus_speed - Gets the i2c speed
  90. *
  91. * Gets the i2c speed.
  92. */
  93. int i2c_get_bus_speed(void)
  94. {
  95. u32 cntl;
  96. cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
  97. if (cntl == IC_CON_SPD_HS)
  98. return I2C_MAX_SPEED;
  99. else if (cntl == IC_CON_SPD_FS)
  100. return I2C_FAST_SPEED;
  101. else if (cntl == IC_CON_SPD_SS)
  102. return I2C_STANDARD_SPEED;
  103. return 0;
  104. }
  105. /*
  106. * i2c_init - Init function
  107. * @speed: required i2c speed
  108. * @slaveadd: slave address for the device
  109. *
  110. * Initialization function.
  111. */
  112. void i2c_init(int speed, int slaveadd)
  113. {
  114. unsigned int enbl;
  115. /* Disable i2c */
  116. enbl = readl(&i2c_regs_p->ic_enable);
  117. enbl &= ~IC_ENABLE_0B;
  118. writel(enbl, &i2c_regs_p->ic_enable);
  119. writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
  120. writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
  121. writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
  122. i2c_set_bus_speed(speed);
  123. writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
  124. writel(slaveadd, &i2c_regs_p->ic_sar);
  125. /* Enable i2c */
  126. enbl = readl(&i2c_regs_p->ic_enable);
  127. enbl |= IC_ENABLE_0B;
  128. writel(enbl, &i2c_regs_p->ic_enable);
  129. }
  130. /*
  131. * i2c_setaddress - Sets the target slave address
  132. * @i2c_addr: target i2c address
  133. *
  134. * Sets the target slave address.
  135. */
  136. static void i2c_setaddress(unsigned int i2c_addr)
  137. {
  138. writel(i2c_addr, &i2c_regs_p->ic_tar);
  139. }
  140. /*
  141. * i2c_flush_rxfifo - Flushes the i2c RX FIFO
  142. *
  143. * Flushes the i2c RX FIFO
  144. */
  145. static void i2c_flush_rxfifo(void)
  146. {
  147. while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
  148. readl(&i2c_regs_p->ic_cmd_data);
  149. }
  150. /*
  151. * i2c_wait_for_bb - Waits for bus busy
  152. *
  153. * Waits for bus busy
  154. */
  155. static int i2c_wait_for_bb(void)
  156. {
  157. unsigned long start_time_bb = get_timer(0);
  158. while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
  159. !(readl(&i2c_regs_p->ic_status) & IC_STATUS_TFE)) {
  160. /* Evaluate timeout */
  161. if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. /* check parameters for i2c_read and i2c_write */
  167. static int check_params(uint addr, int alen, uchar *buffer, int len)
  168. {
  169. if (buffer == NULL) {
  170. printf("Buffer is invalid\n");
  171. return 1;
  172. }
  173. if (alen > 1) {
  174. printf("addr len %d not supported\n", alen);
  175. return 1;
  176. }
  177. if (addr + len > 256) {
  178. printf("address out of range\n");
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. static int i2c_xfer_init(uchar chip, uint addr)
  184. {
  185. if (i2c_wait_for_bb()) {
  186. printf("Timed out waiting for bus\n");
  187. return 1;
  188. }
  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. writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
  233. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
  234. *buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
  235. len--;
  236. start_time_rx = get_timer(0);
  237. } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
  238. printf("Timed out. i2c read Failed\n");
  239. return 1;
  240. }
  241. }
  242. return i2c_xfer_finish();
  243. }
  244. /*
  245. * i2c_write - Write to i2c memory
  246. * @chip: target i2c address
  247. * @addr: address to read from
  248. * @alen:
  249. * @buffer: buffer for read data
  250. * @len: no of bytes to be read
  251. *
  252. * Write to i2c memory.
  253. */
  254. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  255. {
  256. int nb = len;
  257. unsigned long start_time_tx;
  258. if (check_params(addr, alen, buffer, len))
  259. return 1;
  260. if (i2c_xfer_init(chip, addr))
  261. return 1;
  262. start_time_tx = get_timer(0);
  263. while (len) {
  264. if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
  265. writel(*buffer, &i2c_regs_p->ic_cmd_data);
  266. buffer++;
  267. len--;
  268. start_time_tx = get_timer(0);
  269. } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
  270. printf("Timed out. i2c write Failed\n");
  271. return 1;
  272. }
  273. }
  274. return i2c_xfer_finish();
  275. }
  276. /*
  277. * i2c_probe - Probe the i2c chip
  278. */
  279. int i2c_probe(uchar chip)
  280. {
  281. u32 tmp;
  282. /*
  283. * Try to read the first location of the chip.
  284. */
  285. return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  286. }