omap24xx_i2c.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  97. *value = readb (I2C_DATA);
  98. #else
  99. *value = readw (I2C_DATA);
  100. #endif
  101. udelay (20000);
  102. } else {
  103. i2c_error = 1;
  104. }
  105. if (!i2c_error) {
  106. writew (I2C_CON_EN, I2C_CON);
  107. while (readw (I2C_STAT)
  108. || (readw (I2C_CON) & I2C_CON_MST)) {
  109. udelay (10000);
  110. writew (0xFFFF, I2C_STAT);
  111. }
  112. }
  113. }
  114. flush_fifo();
  115. writew (0xFFFF, I2C_STAT);
  116. writew (0, I2C_CNT);
  117. return i2c_error;
  118. }
  119. static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
  120. {
  121. int i2c_error = 0;
  122. u16 status, stat;
  123. /* wait until bus not busy */
  124. wait_for_bb ();
  125. /* two bytes */
  126. writew (2, I2C_CNT);
  127. /* set slave address */
  128. writew (devaddr, I2C_SA);
  129. /* stop bit needed here */
  130. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  131. I2C_CON_STP, I2C_CON);
  132. /* wait until state change */
  133. status = wait_for_pin ();
  134. if (status & I2C_STAT_XRDY) {
  135. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  136. /* send out 1 byte */
  137. writeb (regoffset, I2C_DATA);
  138. writew (I2C_STAT_XRDY, I2C_STAT);
  139. status = wait_for_pin ();
  140. if ((status & I2C_STAT_XRDY)) {
  141. /* send out next 1 byte */
  142. writeb (value, I2C_DATA);
  143. writew (I2C_STAT_XRDY, I2C_STAT);
  144. } else {
  145. i2c_error = 1;
  146. }
  147. #else
  148. /* send out two bytes */
  149. writew ((value << 8) + regoffset, I2C_DATA);
  150. #endif
  151. /* must have enough delay to allow BB bit to go low */
  152. udelay (50000);
  153. if (readw (I2C_STAT) & I2C_STAT_NACK) {
  154. i2c_error = 1;
  155. }
  156. } else {
  157. i2c_error = 1;
  158. }
  159. if (!i2c_error) {
  160. int eout = 200;
  161. writew (I2C_CON_EN, I2C_CON);
  162. while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) {
  163. udelay (1000);
  164. /* have to read to clear intrrupt */
  165. writew (0xFFFF, I2C_STAT);
  166. if(--eout == 0) /* better leave with error than hang */
  167. break;
  168. }
  169. }
  170. flush_fifo();
  171. writew (0xFFFF, I2C_STAT);
  172. writew (0, I2C_CNT);
  173. return i2c_error;
  174. }
  175. static void flush_fifo(void)
  176. { u16 stat;
  177. /* note: if you try and read data when its not there or ready
  178. * you get a bus error
  179. */
  180. while(1){
  181. stat = readw(I2C_STAT);
  182. if(stat == I2C_STAT_RRDY){
  183. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  184. readb(I2C_DATA);
  185. #else
  186. readw(I2C_DATA);
  187. #endif
  188. writew(I2C_STAT_RRDY,I2C_STAT);
  189. udelay(1000);
  190. }else
  191. break;
  192. }
  193. }
  194. int i2c_probe (uchar chip)
  195. {
  196. int res = 1; /* default = fail */
  197. if (chip == readw (I2C_OA)) {
  198. return res;
  199. }
  200. /* wait until bus not busy */
  201. wait_for_bb ();
  202. /* try to read one byte */
  203. writew (1, I2C_CNT);
  204. /* set slave address */
  205. writew (chip, I2C_SA);
  206. /* stop bit needed here */
  207. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
  208. /* enough delay for the NACK bit set */
  209. udelay (50000);
  210. if (!(readw (I2C_STAT) & I2C_STAT_NACK)) {
  211. res = 0; /* success case */
  212. flush_fifo();
  213. writew(0xFFFF, I2C_STAT);
  214. } else {
  215. writew(0xFFFF, I2C_STAT); /* failue, clear sources*/
  216. writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
  217. udelay(20000);
  218. wait_for_bb ();
  219. }
  220. flush_fifo();
  221. writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
  222. writew(0xFFFF, I2C_STAT);
  223. return res;
  224. }
  225. int i2c_read (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_read_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. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  246. {
  247. int i;
  248. if (alen > 1) {
  249. printf ("I2C read: addr len %d not supported\n", alen);
  250. return 1;
  251. }
  252. if (addr + len > 256) {
  253. printf ("I2C read: address out of range\n");
  254. return 1;
  255. }
  256. for (i = 0; i < len; i++) {
  257. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  258. printf ("I2C read: I/O error\n");
  259. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  260. return 1;
  261. }
  262. }
  263. return 0;
  264. }
  265. static void wait_for_bb (void)
  266. {
  267. int timeout = 10;
  268. u16 stat;
  269. writew(0xFFFF, I2C_STAT); /* clear current interruts...*/
  270. while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
  271. writew (stat, I2C_STAT);
  272. udelay (50000);
  273. }
  274. if (timeout <= 0) {
  275. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  276. readw (I2C_STAT));
  277. }
  278. writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/
  279. }
  280. static u16 wait_for_pin (void)
  281. {
  282. u16 status;
  283. int timeout = 10;
  284. do {
  285. udelay (1000);
  286. status = readw (I2C_STAT);
  287. } while ( !(status &
  288. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  289. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  290. I2C_STAT_AL)) && timeout--);
  291. if (timeout <= 0) {
  292. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  293. readw (I2C_STAT));
  294. writew(0xFFFF, I2C_STAT);
  295. }
  296. return status;
  297. }