omap24xx_i2c.c 7.0 KB

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