omap24xx_i2c.c 7.0 KB

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