spr_i2c.c 6.9 KB

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