omap24xx_i2c.c 10 KB

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