omap24xx_i2c.c 11 KB

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