omap24xx_i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Basic I2C functions
  3. *
  4. * Copyright (c) 2004 Texas Instruments
  5. *
  6. * This package is free software; you can redistribute it and/or
  7. * modify it under the terms of the license found in the file
  8. * named COPYING that should have accompanied this file.
  9. *
  10. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  11. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  12. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * Author: Jian Zhang jzhang@ti.com, Texas Instruments
  15. *
  16. * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
  17. * Rewritten to fit into the current U-Boot framework
  18. *
  19. * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
  20. *
  21. */
  22. #include <common.h>
  23. #include <asm/arch/i2c.h>
  24. #include <asm/io.h>
  25. static void wait_for_bb (void);
  26. static u16 wait_for_pin (void);
  27. static void flush_fifo(void);
  28. void i2c_init (int speed, int slaveadd)
  29. {
  30. u16 scl;
  31. writew(0x2, I2C_SYSC); /* for ES2 after soft reset */
  32. udelay(1000);
  33. writew(0x0, I2C_SYSC); /* will probably self clear but */
  34. if (readw (I2C_CON) & I2C_CON_EN) {
  35. writew (0, I2C_CON);
  36. udelay (50000);
  37. }
  38. /* 12MHz I2C module clock */
  39. writew (0, I2C_PSC);
  40. speed = speed/1000; /* 100 or 400 */
  41. scl = ((12000/(speed*2)) - 7); /* use 7 when PSC = 0 */
  42. writew (scl, I2C_SCLL);
  43. writew (scl, I2C_SCLH);
  44. /* own address */
  45. writew (slaveadd, I2C_OA);
  46. writew (I2C_CON_EN, I2C_CON);
  47. /* have to enable intrrupts or OMAP i2c module doesn't work */
  48. writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  49. I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
  50. udelay (1000);
  51. flush_fifo();
  52. writew (0xFFFF, I2C_STAT);
  53. writew (0, I2C_CNT);
  54. }
  55. static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
  56. {
  57. int i2c_error = 0;
  58. u16 status;
  59. /* wait until bus not busy */
  60. wait_for_bb ();
  61. /* one byte only */
  62. writew (1, I2C_CNT);
  63. /* set slave address */
  64. writew (devaddr, I2C_SA);
  65. /* no stop bit needed here */
  66. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
  67. status = wait_for_pin ();
  68. if (status & I2C_STAT_XRDY) {
  69. /* Important: have to use byte access */
  70. writeb (regoffset, I2C_DATA);
  71. udelay (20000);
  72. if (readw (I2C_STAT) & I2C_STAT_NACK) {
  73. i2c_error = 1;
  74. }
  75. } else {
  76. i2c_error = 1;
  77. }
  78. if (!i2c_error) {
  79. /* free bus, otherwise we can't use a combined transction */
  80. writew (0, I2C_CON);
  81. while (readw (I2C_STAT) || (readw (I2C_CON) & I2C_CON_MST)) {
  82. udelay (10000);
  83. /* Have to clear pending interrupt to clear I2C_STAT */
  84. writew (0xFFFF, I2C_STAT);
  85. }
  86. wait_for_bb ();
  87. /* set slave address */
  88. writew (devaddr, I2C_SA);
  89. /* read one byte from slave */
  90. writew (1, I2C_CNT);
  91. /* need stop bit here */
  92. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  93. I2C_CON);
  94. status = wait_for_pin ();
  95. if (status & I2C_STAT_RRDY) {
  96. *value = readw (I2C_DATA);
  97. udelay (20000);
  98. } else {
  99. i2c_error = 1;
  100. }
  101. if (!i2c_error) {
  102. writew (I2C_CON_EN, I2C_CON);
  103. while (readw (I2C_STAT)
  104. || (readw (I2C_CON) & I2C_CON_MST)) {
  105. udelay (10000);
  106. writew (0xFFFF, I2C_STAT);
  107. }
  108. }
  109. }
  110. flush_fifo();
  111. writew (0xFFFF, I2C_STAT);
  112. writew (0, I2C_CNT);
  113. return i2c_error;
  114. }
  115. static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
  116. {
  117. int i2c_error = 0;
  118. u16 status, stat;
  119. /* wait until bus not busy */
  120. wait_for_bb ();
  121. /* two bytes */
  122. writew (2, I2C_CNT);
  123. /* set slave address */
  124. writew (devaddr, I2C_SA);
  125. /* stop bit needed here */
  126. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  127. I2C_CON_STP, I2C_CON);
  128. /* wait until state change */
  129. status = wait_for_pin ();
  130. if (status & I2C_STAT_XRDY) {
  131. /* send out two bytes */
  132. writew ((value << 8) + regoffset, I2C_DATA);
  133. /* must have enough delay to allow BB bit to go low */
  134. udelay (50000);
  135. if (readw (I2C_STAT) & I2C_STAT_NACK) {
  136. i2c_error = 1;
  137. }
  138. } else {
  139. i2c_error = 1;
  140. }
  141. if (!i2c_error) {
  142. int eout = 200;
  143. writew (I2C_CON_EN, I2C_CON);
  144. while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) {
  145. udelay (1000);
  146. /* have to read to clear intrrupt */
  147. writew (0xFFFF, I2C_STAT);
  148. if(--eout == 0) /* better leave with error than hang */
  149. break;
  150. }
  151. }
  152. flush_fifo();
  153. writew (0xFFFF, I2C_STAT);
  154. writew (0, I2C_CNT);
  155. return i2c_error;
  156. }
  157. static void flush_fifo(void)
  158. { u16 stat;
  159. /* note: if you try and read data when its not there or ready
  160. * you get a bus error
  161. */
  162. while(1){
  163. stat = readw(I2C_STAT);
  164. if(stat == I2C_STAT_RRDY){
  165. readw(I2C_DATA);
  166. writew(I2C_STAT_RRDY,I2C_STAT);
  167. udelay(1000);
  168. }else
  169. break;
  170. }
  171. }
  172. int i2c_probe (uchar chip)
  173. {
  174. int res = 1; /* default = fail */
  175. if (chip == readw (I2C_OA)) {
  176. return res;
  177. }
  178. /* wait until bus not busy */
  179. wait_for_bb ();
  180. /* try to read one byte */
  181. writew (1, I2C_CNT);
  182. /* set slave address */
  183. writew (chip, I2C_SA);
  184. /* stop bit needed here */
  185. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
  186. /* enough delay for the NACK bit set */
  187. udelay (50000);
  188. if (!(readw (I2C_STAT) & I2C_STAT_NACK)) {
  189. res = 0; /* success case */
  190. flush_fifo();
  191. writew(0xFFFF, I2C_STAT);
  192. } else {
  193. writew(0xFFFF, I2C_STAT); /* failue, clear sources*/
  194. writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
  195. udelay(20000);
  196. wait_for_bb ();
  197. }
  198. flush_fifo();
  199. writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
  200. writew(0xFFFF, I2C_STAT);
  201. return res;
  202. }
  203. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  204. {
  205. int i;
  206. if (alen > 1) {
  207. printf ("I2C read: addr len %d not supported\n", alen);
  208. return 1;
  209. }
  210. if (addr + len > 256) {
  211. printf ("I2C read: address out of range\n");
  212. return 1;
  213. }
  214. for (i = 0; i < len; i++) {
  215. if (i2c_read_byte (chip, addr + i, &buffer[i])) {
  216. printf ("I2C read: I/O error\n");
  217. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  218. return 1;
  219. }
  220. }
  221. return 0;
  222. }
  223. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  224. {
  225. int i;
  226. if (alen > 1) {
  227. printf ("I2C read: addr len %d not supported\n", alen);
  228. return 1;
  229. }
  230. if (addr + len > 256) {
  231. printf ("I2C read: address out of range\n");
  232. return 1;
  233. }
  234. for (i = 0; i < len; i++) {
  235. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  236. printf ("I2C read: I/O error\n");
  237. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  238. return 1;
  239. }
  240. }
  241. return 0;
  242. }
  243. static void wait_for_bb (void)
  244. {
  245. int timeout = 10;
  246. u16 stat;
  247. writew(0xFFFF, I2C_STAT); /* clear current interruts...*/
  248. while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
  249. writew (stat, I2C_STAT);
  250. udelay (50000);
  251. }
  252. if (timeout <= 0) {
  253. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  254. readw (I2C_STAT));
  255. }
  256. writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/
  257. }
  258. static u16 wait_for_pin (void)
  259. {
  260. u16 status;
  261. int timeout = 10;
  262. do {
  263. udelay (1000);
  264. status = readw (I2C_STAT);
  265. } while ( !(status &
  266. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  267. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  268. I2C_STAT_AL)) && timeout--);
  269. if (timeout <= 0) {
  270. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  271. readw (I2C_STAT));
  272. writew(0xFFFF, I2C_STAT);
  273. }
  274. return status;
  275. }