omap24xx_i2c.c 6.8 KB

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