omap24xx_i2c.c 10 KB

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