omap24xx_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 1000
  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. /* send register offset */
  133. while (1) {
  134. status = wait_for_pin();
  135. if (status == 0 || status & I2C_STAT_NACK) {
  136. i2c_error = 1;
  137. goto read_exit;
  138. }
  139. if (status & I2C_STAT_XRDY) {
  140. /* Important: have to use byte access */
  141. writeb(regoffset, &i2c_base->data);
  142. writew(I2C_STAT_XRDY, &i2c_base->stat);
  143. }
  144. if (status & I2C_STAT_ARDY) {
  145. writew(I2C_STAT_ARDY, &i2c_base->stat);
  146. break;
  147. }
  148. }
  149. /* set slave address */
  150. writew(devaddr, &i2c_base->sa);
  151. /* read one byte from slave */
  152. writew(1, &i2c_base->cnt);
  153. /* need stop bit here */
  154. writew(I2C_CON_EN | I2C_CON_MST |
  155. I2C_CON_STT | I2C_CON_STP,
  156. &i2c_base->con);
  157. /* receive data */
  158. while (1) {
  159. status = wait_for_pin();
  160. if (status == 0 || status & I2C_STAT_NACK) {
  161. i2c_error = 1;
  162. goto read_exit;
  163. }
  164. if (status & I2C_STAT_RRDY) {
  165. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  166. defined(CONFIG_OMAP44XX)
  167. *value = readb(&i2c_base->data);
  168. #else
  169. *value = readw(&i2c_base->data);
  170. #endif
  171. writew(I2C_STAT_RRDY, &i2c_base->stat);
  172. }
  173. if (status & I2C_STAT_ARDY) {
  174. writew(I2C_STAT_ARDY, &i2c_base->stat);
  175. break;
  176. }
  177. }
  178. read_exit:
  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;
  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. while (1) {
  198. status = wait_for_pin();
  199. if (status == 0 || status & I2C_STAT_NACK) {
  200. i2c_error = 1;
  201. goto write_exit;
  202. }
  203. if (status & I2C_STAT_XRDY) {
  204. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  205. defined(CONFIG_OMAP44XX)
  206. /* send register offset */
  207. writeb(regoffset, &i2c_base->data);
  208. writew(I2C_STAT_XRDY, &i2c_base->stat);
  209. while (1) {
  210. status = wait_for_pin();
  211. if (status == 0 || status & I2C_STAT_NACK) {
  212. i2c_error = 1;
  213. goto write_exit;
  214. }
  215. if (status & I2C_STAT_XRDY) {
  216. /* send data */
  217. writeb(value, &i2c_base->data);
  218. writew(I2C_STAT_XRDY, &i2c_base->stat);
  219. }
  220. if (status & I2C_STAT_ARDY) {
  221. writew(I2C_STAT_ARDY, &i2c_base->stat);
  222. break;
  223. }
  224. }
  225. break;
  226. #else
  227. /* send out two bytes */
  228. writew((value << 8) + regoffset, &i2c_base->data);
  229. writew(I2C_STAT_XRDY, &i2c_base->stat);
  230. #endif
  231. }
  232. if (status & I2C_STAT_ARDY) {
  233. writew(I2C_STAT_ARDY, &i2c_base->stat);
  234. break;
  235. }
  236. }
  237. wait_for_bb();
  238. status = readw(&i2c_base->stat);
  239. if (status & I2C_STAT_NACK)
  240. i2c_error = 1;
  241. write_exit:
  242. flush_fifo();
  243. writew (0xFFFF, &i2c_base->stat);
  244. writew (0, &i2c_base->cnt);
  245. return i2c_error;
  246. }
  247. static void flush_fifo(void)
  248. { u16 stat;
  249. /* note: if you try and read data when its not there or ready
  250. * you get a bus error
  251. */
  252. while(1){
  253. stat = readw(&i2c_base->stat);
  254. if(stat == I2C_STAT_RRDY){
  255. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  256. defined(CONFIG_OMAP44XX)
  257. readb(&i2c_base->data);
  258. #else
  259. readw(&i2c_base->data);
  260. #endif
  261. writew(I2C_STAT_RRDY,&i2c_base->stat);
  262. udelay(1000);
  263. }else
  264. break;
  265. }
  266. }
  267. int i2c_probe (uchar chip)
  268. {
  269. int res = 1; /* default = fail */
  270. if (chip == readw (&i2c_base->oa)) {
  271. return res;
  272. }
  273. /* wait until bus not busy */
  274. wait_for_bb ();
  275. /* try to read one byte */
  276. writew (1, &i2c_base->cnt);
  277. /* set slave address */
  278. writew (chip, &i2c_base->sa);
  279. /* stop bit needed here */
  280. writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con);
  281. /* enough delay for the NACK bit set */
  282. udelay (50000);
  283. if (!(readw (&i2c_base->stat) & I2C_STAT_NACK)) {
  284. res = 0; /* success case */
  285. flush_fifo();
  286. writew(0xFFFF, &i2c_base->stat);
  287. } else {
  288. writew(0xFFFF, &i2c_base->stat); /* failue, clear sources*/
  289. writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con); /* finish up xfer */
  290. udelay(20000);
  291. wait_for_bb ();
  292. }
  293. flush_fifo();
  294. writew (0, &i2c_base->cnt); /* don't allow any more data in...we don't want it.*/
  295. writew(0xFFFF, &i2c_base->stat);
  296. return res;
  297. }
  298. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  299. {
  300. int i;
  301. if (alen > 1) {
  302. printf ("I2C read: addr len %d not supported\n", alen);
  303. return 1;
  304. }
  305. if (addr + len > 256) {
  306. printf ("I2C read: address out of range\n");
  307. return 1;
  308. }
  309. for (i = 0; i < len; i++) {
  310. if (i2c_read_byte (chip, addr + i, &buffer[i])) {
  311. printf ("I2C read: I/O error\n");
  312. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  313. return 1;
  314. }
  315. }
  316. return 0;
  317. }
  318. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  319. {
  320. int i;
  321. if (alen > 1) {
  322. printf ("I2C read: addr len %d not supported\n", alen);
  323. return 1;
  324. }
  325. if (addr + len > 256) {
  326. printf ("I2C read: address out of range\n");
  327. return 1;
  328. }
  329. for (i = 0; i < len; i++) {
  330. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  331. printf ("I2C read: I/O error\n");
  332. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  333. return 1;
  334. }
  335. }
  336. return 0;
  337. }
  338. static void wait_for_bb (void)
  339. {
  340. int timeout = I2C_TIMEOUT;
  341. u16 stat;
  342. writew(0xFFFF, &i2c_base->stat); /* clear current interruts...*/
  343. while ((stat = readw (&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  344. writew (stat, &i2c_base->stat);
  345. udelay(1000);
  346. }
  347. if (timeout <= 0) {
  348. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  349. readw (&i2c_base->stat));
  350. }
  351. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  352. }
  353. static u16 wait_for_pin (void)
  354. {
  355. u16 status;
  356. int timeout = I2C_TIMEOUT;
  357. do {
  358. udelay (1000);
  359. status = readw (&i2c_base->stat);
  360. } while ( !(status &
  361. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  362. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  363. I2C_STAT_AL)) && timeout--);
  364. if (timeout <= 0) {
  365. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  366. readw (&i2c_base->stat));
  367. writew(0xFFFF, &i2c_base->stat);
  368. status = 0;
  369. }
  370. return status;
  371. }
  372. int i2c_set_bus_num(unsigned int bus)
  373. {
  374. if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
  375. printf("Bad bus: %d\n", bus);
  376. return -1;
  377. }
  378. #if I2C_BUS_MAX==3
  379. if (bus == 2)
  380. i2c_base = (struct i2c *)I2C_BASE3;
  381. else
  382. #endif
  383. if (bus == 1)
  384. i2c_base = (struct i2c *)I2C_BASE2;
  385. else
  386. i2c_base = (struct i2c *)I2C_BASE1;
  387. current_bus = bus;
  388. if(!bus_initialized[current_bus])
  389. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  390. return 0;
  391. }
  392. int i2c_get_bus_num(void)
  393. {
  394. return (int) current_bus;
  395. }