omap24xx_i2c.c 11 KB

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