omap24xx_i2c.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. int psc, fsscll, fssclh;
  31. int hsscll = 0, hssclh = 0;
  32. u32 scll, sclh;
  33. /* Only handle standard, fast and high speeds */
  34. if ((speed != OMAP_I2C_STANDARD) &&
  35. (speed != OMAP_I2C_FAST_MODE) &&
  36. (speed != OMAP_I2C_HIGH_SPEED)) {
  37. printf("Error : I2C unsupported speed %d\n", speed);
  38. return;
  39. }
  40. psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
  41. psc -= 1;
  42. if (psc < I2C_PSC_MIN) {
  43. printf("Error : I2C unsupported prescalar %d\n", psc);
  44. return;
  45. }
  46. if (speed == OMAP_I2C_HIGH_SPEED) {
  47. /* High speed */
  48. /* For first phase of HS mode */
  49. fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
  50. (2 * OMAP_I2C_FAST_MODE);
  51. fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
  52. fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
  53. if (((fsscll < 0) || (fssclh < 0)) ||
  54. ((fsscll > 255) || (fssclh > 255))) {
  55. printf("Error : I2C initializing first phase clock\n");
  56. return;
  57. }
  58. /* For second phase of HS mode */
  59. hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  60. hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
  61. hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
  62. if (((fsscll < 0) || (fssclh < 0)) ||
  63. ((fsscll > 255) || (fssclh > 255))) {
  64. printf("Error : I2C initializing second phase clock\n");
  65. return;
  66. }
  67. scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
  68. sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
  69. } else {
  70. /* Standard and fast speed */
  71. fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  72. fsscll -= I2C_FASTSPEED_SCLL_TRIM;
  73. fssclh -= I2C_FASTSPEED_SCLH_TRIM;
  74. if (((fsscll < 0) || (fssclh < 0)) ||
  75. ((fsscll > 255) || (fssclh > 255))) {
  76. printf("Error : I2C initializing clock\n");
  77. return;
  78. }
  79. scll = (unsigned int)fsscll;
  80. sclh = (unsigned int)fssclh;
  81. }
  82. writew(0x2, I2C_SYSC); /* for ES2 after soft reset */
  83. udelay(1000);
  84. writew(0x0, I2C_SYSC); /* will probably self clear but */
  85. if (readw (I2C_CON) & I2C_CON_EN) {
  86. writew (0, I2C_CON);
  87. udelay (50000);
  88. }
  89. writew(psc, I2C_PSC);
  90. writew(scll, I2C_SCLL);
  91. writew(sclh, I2C_SCLH);
  92. /* own address */
  93. writew (slaveadd, I2C_OA);
  94. writew (I2C_CON_EN, I2C_CON);
  95. /* have to enable intrrupts or OMAP i2c module doesn't work */
  96. writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  97. I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
  98. udelay (1000);
  99. flush_fifo();
  100. writew (0xFFFF, I2C_STAT);
  101. writew (0, I2C_CNT);
  102. }
  103. static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
  104. {
  105. int i2c_error = 0;
  106. u16 status;
  107. /* wait until bus not busy */
  108. wait_for_bb ();
  109. /* one byte only */
  110. writew (1, I2C_CNT);
  111. /* set slave address */
  112. writew (devaddr, I2C_SA);
  113. /* no stop bit needed here */
  114. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
  115. status = wait_for_pin ();
  116. if (status & I2C_STAT_XRDY) {
  117. /* Important: have to use byte access */
  118. writeb (regoffset, I2C_DATA);
  119. udelay (20000);
  120. if (readw (I2C_STAT) & I2C_STAT_NACK) {
  121. i2c_error = 1;
  122. }
  123. } else {
  124. i2c_error = 1;
  125. }
  126. if (!i2c_error) {
  127. /* free bus, otherwise we can't use a combined transction */
  128. writew (0, I2C_CON);
  129. while (readw (I2C_STAT) || (readw (I2C_CON) & I2C_CON_MST)) {
  130. udelay (10000);
  131. /* Have to clear pending interrupt to clear I2C_STAT */
  132. writew (0xFFFF, I2C_STAT);
  133. }
  134. wait_for_bb ();
  135. /* set slave address */
  136. writew (devaddr, I2C_SA);
  137. /* read one byte from slave */
  138. writew (1, I2C_CNT);
  139. /* need stop bit here */
  140. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  141. I2C_CON);
  142. status = wait_for_pin ();
  143. if (status & I2C_STAT_RRDY) {
  144. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  145. *value = readb (I2C_DATA);
  146. #else
  147. *value = readw (I2C_DATA);
  148. #endif
  149. udelay (20000);
  150. } else {
  151. i2c_error = 1;
  152. }
  153. if (!i2c_error) {
  154. writew (I2C_CON_EN, I2C_CON);
  155. while (readw (I2C_STAT)
  156. || (readw (I2C_CON) & I2C_CON_MST)) {
  157. udelay (10000);
  158. writew (0xFFFF, I2C_STAT);
  159. }
  160. }
  161. }
  162. flush_fifo();
  163. writew (0xFFFF, I2C_STAT);
  164. writew (0, I2C_CNT);
  165. return i2c_error;
  166. }
  167. static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
  168. {
  169. int i2c_error = 0;
  170. u16 status, stat;
  171. /* wait until bus not busy */
  172. wait_for_bb ();
  173. /* two bytes */
  174. writew (2, I2C_CNT);
  175. /* set slave address */
  176. writew (devaddr, I2C_SA);
  177. /* stop bit needed here */
  178. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  179. I2C_CON_STP, I2C_CON);
  180. /* wait until state change */
  181. status = wait_for_pin ();
  182. if (status & I2C_STAT_XRDY) {
  183. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  184. /* send out 1 byte */
  185. writeb (regoffset, I2C_DATA);
  186. writew (I2C_STAT_XRDY, I2C_STAT);
  187. status = wait_for_pin ();
  188. if ((status & I2C_STAT_XRDY)) {
  189. /* send out next 1 byte */
  190. writeb (value, I2C_DATA);
  191. writew (I2C_STAT_XRDY, I2C_STAT);
  192. } else {
  193. i2c_error = 1;
  194. }
  195. #else
  196. /* send out two bytes */
  197. writew ((value << 8) + regoffset, I2C_DATA);
  198. #endif
  199. /* must have enough delay to allow BB bit to go low */
  200. udelay (50000);
  201. if (readw (I2C_STAT) & I2C_STAT_NACK) {
  202. i2c_error = 1;
  203. }
  204. } else {
  205. i2c_error = 1;
  206. }
  207. if (!i2c_error) {
  208. int eout = 200;
  209. writew (I2C_CON_EN, I2C_CON);
  210. while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) {
  211. udelay (1000);
  212. /* have to read to clear intrrupt */
  213. writew (0xFFFF, I2C_STAT);
  214. if(--eout == 0) /* better leave with error than hang */
  215. break;
  216. }
  217. }
  218. flush_fifo();
  219. writew (0xFFFF, I2C_STAT);
  220. writew (0, I2C_CNT);
  221. return i2c_error;
  222. }
  223. static void flush_fifo(void)
  224. { u16 stat;
  225. /* note: if you try and read data when its not there or ready
  226. * you get a bus error
  227. */
  228. while(1){
  229. stat = readw(I2C_STAT);
  230. if(stat == I2C_STAT_RRDY){
  231. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  232. readb(I2C_DATA);
  233. #else
  234. readw(I2C_DATA);
  235. #endif
  236. writew(I2C_STAT_RRDY,I2C_STAT);
  237. udelay(1000);
  238. }else
  239. break;
  240. }
  241. }
  242. int i2c_probe (uchar chip)
  243. {
  244. int res = 1; /* default = fail */
  245. if (chip == readw (I2C_OA)) {
  246. return res;
  247. }
  248. /* wait until bus not busy */
  249. wait_for_bb ();
  250. /* try to read one byte */
  251. writew (1, I2C_CNT);
  252. /* set slave address */
  253. writew (chip, I2C_SA);
  254. /* stop bit needed here */
  255. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
  256. /* enough delay for the NACK bit set */
  257. udelay (50000);
  258. if (!(readw (I2C_STAT) & I2C_STAT_NACK)) {
  259. res = 0; /* success case */
  260. flush_fifo();
  261. writew(0xFFFF, I2C_STAT);
  262. } else {
  263. writew(0xFFFF, I2C_STAT); /* failue, clear sources*/
  264. writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
  265. udelay(20000);
  266. wait_for_bb ();
  267. }
  268. flush_fifo();
  269. writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
  270. writew(0xFFFF, I2C_STAT);
  271. return res;
  272. }
  273. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  274. {
  275. int i;
  276. if (alen > 1) {
  277. printf ("I2C read: addr len %d not supported\n", alen);
  278. return 1;
  279. }
  280. if (addr + len > 256) {
  281. printf ("I2C read: address out of range\n");
  282. return 1;
  283. }
  284. for (i = 0; i < len; i++) {
  285. if (i2c_read_byte (chip, addr + i, &buffer[i])) {
  286. printf ("I2C read: I/O error\n");
  287. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  288. return 1;
  289. }
  290. }
  291. return 0;
  292. }
  293. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  294. {
  295. int i;
  296. if (alen > 1) {
  297. printf ("I2C read: addr len %d not supported\n", alen);
  298. return 1;
  299. }
  300. if (addr + len > 256) {
  301. printf ("I2C read: address out of range\n");
  302. return 1;
  303. }
  304. for (i = 0; i < len; i++) {
  305. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  306. printf ("I2C read: I/O error\n");
  307. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  308. return 1;
  309. }
  310. }
  311. return 0;
  312. }
  313. static void wait_for_bb (void)
  314. {
  315. int timeout = 10;
  316. u16 stat;
  317. writew(0xFFFF, I2C_STAT); /* clear current interruts...*/
  318. while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
  319. writew (stat, I2C_STAT);
  320. udelay (50000);
  321. }
  322. if (timeout <= 0) {
  323. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  324. readw (I2C_STAT));
  325. }
  326. writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/
  327. }
  328. static u16 wait_for_pin (void)
  329. {
  330. u16 status;
  331. int timeout = 10;
  332. do {
  333. udelay (1000);
  334. status = readw (I2C_STAT);
  335. } while ( !(status &
  336. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  337. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  338. I2C_STAT_AL)) && timeout--);
  339. if (timeout <= 0) {
  340. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  341. readw (I2C_STAT));
  342. writew(0xFFFF, I2C_STAT);
  343. }
  344. return status;
  345. }