omap24xx_i2c.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 void wait_for_bb(void);
  29. static u16 wait_for_pin(void);
  30. static void flush_fifo(void);
  31. static struct i2c *i2c_base = (struct i2c *)I2C_DEFAULT_BASE;
  32. static unsigned int bus_initialized[I2C_BUS_MAX];
  33. static unsigned int current_bus;
  34. void i2c_init(int speed, int slaveadd)
  35. {
  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. puts("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. puts("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. puts("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. puts("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 |
  132. I2C_CON_TRX, &i2c_base->con);
  133. /* send register offset */
  134. while (1) {
  135. status = wait_for_pin();
  136. if (status == 0 || status & I2C_STAT_NACK) {
  137. i2c_error = 1;
  138. goto read_exit;
  139. }
  140. if (status & I2C_STAT_XRDY) {
  141. /* Important: have to use byte access */
  142. writeb(regoffset, &i2c_base->data);
  143. writew(I2C_STAT_XRDY, &i2c_base->stat);
  144. }
  145. if (status & I2C_STAT_ARDY) {
  146. writew(I2C_STAT_ARDY, &i2c_base->stat);
  147. break;
  148. }
  149. }
  150. /* set slave address */
  151. writew(devaddr, &i2c_base->sa);
  152. /* read one byte from slave */
  153. writew(1, &i2c_base->cnt);
  154. /* need stop bit here */
  155. writew(I2C_CON_EN | I2C_CON_MST |
  156. I2C_CON_STT | I2C_CON_STP,
  157. &i2c_base->con);
  158. /* receive data */
  159. while (1) {
  160. status = wait_for_pin();
  161. if (status == 0 || status & I2C_STAT_NACK) {
  162. i2c_error = 1;
  163. goto read_exit;
  164. }
  165. if (status & I2C_STAT_RRDY) {
  166. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  167. defined(CONFIG_OMAP44XX)
  168. *value = readb(&i2c_base->data);
  169. #else
  170. *value = readw(&i2c_base->data);
  171. #endif
  172. writew(I2C_STAT_RRDY, &i2c_base->stat);
  173. }
  174. if (status & I2C_STAT_ARDY) {
  175. writew(I2C_STAT_ARDY, &i2c_base->stat);
  176. break;
  177. }
  178. }
  179. read_exit:
  180. flush_fifo();
  181. writew(0xFFFF, &i2c_base->stat);
  182. writew(0, &i2c_base->cnt);
  183. return i2c_error;
  184. }
  185. static void flush_fifo(void)
  186. { u16 stat;
  187. /* note: if you try and read data when its not there or ready
  188. * you get a bus error
  189. */
  190. while (1) {
  191. stat = readw(&i2c_base->stat);
  192. if (stat == I2C_STAT_RRDY) {
  193. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  194. defined(CONFIG_OMAP44XX)
  195. readb(&i2c_base->data);
  196. #else
  197. readw(&i2c_base->data);
  198. #endif
  199. writew(I2C_STAT_RRDY, &i2c_base->stat);
  200. udelay(1000);
  201. } else
  202. break;
  203. }
  204. }
  205. int i2c_probe(uchar chip)
  206. {
  207. u16 status;
  208. int res = 1; /* default = fail */
  209. if (chip == readw(&i2c_base->oa))
  210. return res;
  211. /* wait until bus not busy */
  212. wait_for_bb();
  213. /* try to write one byte */
  214. writew(1, &i2c_base->cnt);
  215. /* set slave address */
  216. writew(chip, &i2c_base->sa);
  217. /* stop bit needed here */
  218. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  219. I2C_CON_STP, &i2c_base->con);
  220. status = wait_for_pin();
  221. /* check for ACK (!NAK) */
  222. if (!(status & I2C_STAT_NACK))
  223. res = 0;
  224. /* abort transfer (force idle state) */
  225. writew(0, &i2c_base->con);
  226. flush_fifo();
  227. /* don't allow any more data in... we don't want it. */
  228. writew(0, &i2c_base->cnt);
  229. writew(0xFFFF, &i2c_base->stat);
  230. return res;
  231. }
  232. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  233. {
  234. int i;
  235. if (alen > 1) {
  236. printf("I2C read: addr len %d not supported\n", alen);
  237. return 1;
  238. }
  239. if (addr + len > 256) {
  240. puts("I2C read: address out of range\n");
  241. return 1;
  242. }
  243. for (i = 0; i < len; i++) {
  244. if (i2c_read_byte(chip, addr + i, &buffer[i])) {
  245. puts("I2C read: I/O error\n");
  246. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  247. return 1;
  248. }
  249. }
  250. return 0;
  251. }
  252. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  253. {
  254. int i;
  255. u16 status;
  256. int i2c_error = 0;
  257. if (alen > 1) {
  258. printf("I2C write: addr len %d not supported\n", alen);
  259. return 1;
  260. }
  261. if (addr + len > 256) {
  262. printf("I2C write: address 0x%x + 0x%x out of range\n",
  263. addr, len);
  264. return 1;
  265. }
  266. /* wait until bus not busy */
  267. wait_for_bb();
  268. /* start address phase - will write regoffset + len bytes data */
  269. /* TODO consider case when !CONFIG_OMAP243X/34XX/44XX */
  270. writew(alen + len, &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_TRX |
  275. I2C_CON_STP, &i2c_base->con);
  276. /* Send address byte */
  277. status = wait_for_pin();
  278. if (status == 0 || status & I2C_STAT_NACK) {
  279. i2c_error = 1;
  280. printf("error waiting for i2c address ACK (status=0x%x)\n",
  281. status);
  282. goto write_exit;
  283. }
  284. if (status & I2C_STAT_XRDY) {
  285. writeb(addr & 0xFF, &i2c_base->data);
  286. writew(I2C_STAT_XRDY, &i2c_base->stat);
  287. } else {
  288. i2c_error = 1;
  289. printf("i2c bus not ready for transmit (status=0x%x)\n",
  290. status);
  291. goto write_exit;
  292. }
  293. /* address phase is over, now write data */
  294. for (i = 0; i < len; i++) {
  295. status = wait_for_pin();
  296. if (status == 0 || status & I2C_STAT_NACK) {
  297. i2c_error = 1;
  298. printf("i2c error waiting for data ACK (status=0x%x)\n",
  299. status);
  300. goto write_exit;
  301. }
  302. if (status & I2C_STAT_XRDY) {
  303. writeb(buffer[i], &i2c_base->data);
  304. writew(I2C_STAT_XRDY, &i2c_base->stat);
  305. } else {
  306. i2c_error = 1;
  307. printf("i2c bus not ready for Tx (i=%d)\n", i);
  308. goto write_exit;
  309. }
  310. }
  311. write_exit:
  312. flush_fifo();
  313. writew(0xFFFF, &i2c_base->stat);
  314. return i2c_error;
  315. }
  316. static void wait_for_bb(void)
  317. {
  318. int timeout = I2C_TIMEOUT;
  319. u16 stat;
  320. writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
  321. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  322. writew(stat, &i2c_base->stat);
  323. udelay(1000);
  324. }
  325. if (timeout <= 0) {
  326. printf("timed out in wait_for_bb: I2C_STAT=%x\n",
  327. readw(&i2c_base->stat));
  328. }
  329. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  330. }
  331. static u16 wait_for_pin(void)
  332. {
  333. u16 status;
  334. int timeout = I2C_TIMEOUT;
  335. do {
  336. udelay(1000);
  337. status = readw(&i2c_base->stat);
  338. } while (!(status &
  339. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  340. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  341. I2C_STAT_AL)) && timeout--);
  342. if (timeout <= 0) {
  343. printf("timed out in wait_for_pin: I2C_STAT=%x\n",
  344. readw(&i2c_base->stat));
  345. writew(0xFFFF, &i2c_base->stat);
  346. status = 0;
  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. }
  370. int i2c_get_bus_num(void)
  371. {
  372. return (int) current_bus;
  373. }