i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /****************************************************************
  2. * $ID: i2c.c 24 Oct 2006 12:00:00 +0800 $ *
  3. * *
  4. * Description: *
  5. * *
  6. * Maintainer: sonicz <sonic.zhang@analog.com> *
  7. * *
  8. * CopyRight (c) 2006 Analog Device *
  9. * All rights reserved. *
  10. * *
  11. * This file is free software; *
  12. * you are free to modify and/or redistribute it *
  13. * under the terms of the GNU General Public Licence (GPL).*
  14. * *
  15. ****************************************************************/
  16. #include <common.h>
  17. #ifdef CONFIG_HARD_I2C
  18. #include <asm/blackfin.h>
  19. #include <i2c.h>
  20. #include <asm/io.h>
  21. #define bfin_read16(addr) ({ unsigned __v; \
  22. __asm__ __volatile__ (\
  23. "%0 = w[%1] (z);\n\t"\
  24. : "=d"(__v) : "a"(addr)); (unsigned short)__v; })
  25. #define bfin_write16(addr,val) ({\
  26. __asm__ __volatile__ (\
  27. "w[%0] = %1;\n\t"\
  28. : : "a"(addr) , "d"(val) : "memory");})
  29. /* Two-Wire Interface (0xFFC01400 - 0xFFC014FF) */
  30. #define bfin_read_TWI_CLKDIV() bfin_read16(TWI_CLKDIV)
  31. #define bfin_write_TWI_CLKDIV(val) bfin_write16(TWI_CLKDIV,val)
  32. #define bfin_read_TWI_CONTROL() bfin_read16(TWI_CONTROL)
  33. #define bfin_write_TWI_CONTROL(val) bfin_write16(TWI_CONTROL,val)
  34. #define bfin_read_TWI_SLAVE_CTL() bfin_read16(TWI_SLAVE_CTL)
  35. #define bfin_write_TWI_SLAVE_CTL(val) bfin_write16(TWI_SLAVE_CTL,val)
  36. #define bfin_read_TWI_SLAVE_STAT() bfin_read16(TWI_SLAVE_STAT)
  37. #define bfin_write_TWI_SLAVE_STAT(val) bfin_write16(TWI_SLAVE_STAT,val)
  38. #define bfin_read_TWI_SLAVE_ADDR() bfin_read16(TWI_SLAVE_ADDR)
  39. #define bfin_write_TWI_SLAVE_ADDR(val) bfin_write16(TWI_SLAVE_ADDR,val)
  40. #define bfin_read_TWI_MASTER_CTL() bfin_read16(TWI_MASTER_CTL)
  41. #define bfin_write_TWI_MASTER_CTL(val) bfin_write16(TWI_MASTER_CTL,val)
  42. #define bfin_read_TWI_MASTER_STAT() bfin_read16(TWI_MASTER_STAT)
  43. #define bfin_write_TWI_MASTER_STAT(val) bfin_write16(TWI_MASTER_STAT,val)
  44. #define bfin_read_TWI_MASTER_ADDR() bfin_read16(TWI_MASTER_ADDR)
  45. #define bfin_write_TWI_MASTER_ADDR(val) bfin_write16(TWI_MASTER_ADDR,val)
  46. #define bfin_read_TWI_INT_STAT() bfin_read16(TWI_INT_STAT)
  47. #define bfin_write_TWI_INT_STAT(val) bfin_write16(TWI_INT_STAT,val)
  48. #define bfin_read_TWI_INT_MASK() bfin_read16(TWI_INT_MASK)
  49. #define bfin_write_TWI_INT_MASK(val) bfin_write16(TWI_INT_MASK,val)
  50. #define bfin_read_TWI_FIFO_CTL() bfin_read16(TWI_FIFO_CTL)
  51. #define bfin_write_TWI_FIFO_CTL(val) bfin_write16(TWI_FIFO_CTL,val)
  52. #define bfin_read_TWI_FIFO_STAT() bfin_read16(TWI_FIFO_STAT)
  53. #define bfin_write_TWI_FIFO_STAT(val) bfin_write16(TWI_FIFO_STAT,val)
  54. #define bfin_read_TWI_XMT_DATA8() bfin_read16(TWI_XMT_DATA8)
  55. #define bfin_write_TWI_XMT_DATA8(val) bfin_write16(TWI_XMT_DATA8,val)
  56. #define bfin_read_TWI_XMT_DATA16() bfin_read16(TWI_XMT_DATA16)
  57. #define bfin_write_TWI_XMT_DATA16(val) bfin_write16(TWI_XMT_DATA16,val)
  58. #define bfin_read_TWI_RCV_DATA8() bfin_read16(TWI_RCV_DATA8)
  59. #define bfin_write_TWI_RCV_DATA8(val) bfin_write16(TWI_RCV_DATA8,val)
  60. #define bfin_read_TWI_RCV_DATA16() bfin_read16(TWI_RCV_DATA16)
  61. #define bfin_write_TWI_RCV_DATA16(val) bfin_write16(TWI_RCV_DATA16,val)
  62. #ifdef DEBUG_I2C
  63. #define PRINTD(fmt,args...) do { \
  64. DECLARE_GLOBAL_DATA_PTR; \
  65. if (gd->have_console) \
  66. printf(fmt ,##args); \
  67. } while (0)
  68. #else
  69. #define PRINTD(fmt,args...)
  70. #endif
  71. #ifndef CONFIG_TWICLK_KHZ
  72. #define CONFIG_TWICLK_KHZ 50
  73. #endif
  74. /* All transfers are described by this data structure */
  75. struct i2c_msg {
  76. u16 addr; /* slave address */
  77. u16 flags;
  78. #define I2C_M_STOP 0x2
  79. #define I2C_M_RD 0x1
  80. u16 len; /* msg length */
  81. u8 *buf; /* pointer to msg data */
  82. };
  83. /**
  84. * i2c_reset: - reset the host controller
  85. *
  86. */
  87. static void i2c_reset(void)
  88. {
  89. /* Disable TWI */
  90. bfin_write_TWI_CONTROL(0);
  91. sync();
  92. /* Set TWI internal clock as 10MHz */
  93. bfin_write_TWI_CONTROL(((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
  94. /* Set Twi interface clock as specified */
  95. if (CONFIG_TWICLK_KHZ > 400)
  96. bfin_write_TWI_CLKDIV(((5 * 1024 / 400) << 8) | ((5 * 1024 /
  97. 400) & 0xFF));
  98. else
  99. bfin_write_TWI_CLKDIV(((5 * 1024 /
  100. CONFIG_TWICLK_KHZ) << 8) | ((5 * 1024 /
  101. CONFIG_TWICLK_KHZ)
  102. & 0xFF));
  103. /* Enable TWI */
  104. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
  105. sync();
  106. }
  107. int wait_for_completion(struct i2c_msg *msg, int timeout_count)
  108. {
  109. unsigned short twi_int_stat;
  110. unsigned short mast_stat;
  111. int i;
  112. for (i = 0; i < timeout_count; i++) {
  113. twi_int_stat = bfin_read_TWI_INT_STAT();
  114. mast_stat = bfin_read_TWI_MASTER_STAT();
  115. if (XMTSERV & twi_int_stat) {
  116. /* Transmit next data */
  117. if (msg->len > 0) {
  118. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  119. msg->len--;
  120. } else if (msg->flags & I2C_M_STOP)
  121. bfin_write_TWI_MASTER_CTL
  122. (bfin_read_TWI_MASTER_CTL() | STOP);
  123. sync();
  124. /* Clear status */
  125. bfin_write_TWI_INT_STAT(XMTSERV);
  126. sync();
  127. i = 0;
  128. }
  129. if (RCVSERV & twi_int_stat) {
  130. if (msg->len > 0) {
  131. /* Receive next data */
  132. *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
  133. msg->len--;
  134. } else if (msg->flags & I2C_M_STOP) {
  135. bfin_write_TWI_MASTER_CTL
  136. (bfin_read_TWI_MASTER_CTL() | STOP);
  137. sync();
  138. }
  139. /* Clear interrupt source */
  140. bfin_write_TWI_INT_STAT(RCVSERV);
  141. sync();
  142. i = 0;
  143. }
  144. if (MERR & twi_int_stat) {
  145. bfin_write_TWI_INT_STAT(MERR);
  146. bfin_write_TWI_INT_MASK(0);
  147. bfin_write_TWI_MASTER_STAT(0x3e);
  148. bfin_write_TWI_MASTER_CTL(0);
  149. sync();
  150. /*
  151. * if both err and complete int stats are set,
  152. * return proper results.
  153. */
  154. if (MCOMP & twi_int_stat) {
  155. bfin_write_TWI_INT_STAT(MCOMP);
  156. bfin_write_TWI_INT_MASK(0);
  157. bfin_write_TWI_MASTER_CTL(0);
  158. sync();
  159. /*
  160. * If it is a quick transfer,
  161. * only address bug no data, not an err.
  162. */
  163. if (msg->len == 0 && mast_stat & BUFRDERR)
  164. return 0;
  165. /*
  166. * If address not acknowledged return -3,
  167. * else return 0.
  168. */
  169. else if (!(mast_stat & ANAK))
  170. return 0;
  171. else
  172. return -3;
  173. }
  174. return -1;
  175. }
  176. if (MCOMP & twi_int_stat) {
  177. bfin_write_TWI_INT_STAT(MCOMP);
  178. sync();
  179. bfin_write_TWI_INT_MASK(0);
  180. bfin_write_TWI_MASTER_CTL(0);
  181. sync();
  182. return 0;
  183. }
  184. }
  185. if (msg->flags & I2C_M_RD)
  186. return -4;
  187. else
  188. return -2;
  189. }
  190. /**
  191. * i2c_transfer: - Transfer one byte over the i2c bus
  192. *
  193. * This function can tranfer a byte over the i2c bus in both directions.
  194. * It is used by the public API functions.
  195. *
  196. * @return: 0: transfer successful
  197. * -1: transfer fail
  198. * -2: transmit timeout
  199. * -3: ACK missing
  200. * -4: receive timeout
  201. * -5: controller not ready
  202. */
  203. int i2c_transfer(struct i2c_msg *msg)
  204. {
  205. int ret = 0;
  206. int timeout_count = 10000;
  207. int len = msg->len;
  208. if (!(bfin_read_TWI_CONTROL() & TWI_ENA)) {
  209. ret = -5;
  210. goto transfer_error;
  211. }
  212. while (bfin_read_TWI_MASTER_STAT() & BUSBUSY) ;
  213. /* Set Transmit device address */
  214. bfin_write_TWI_MASTER_ADDR(msg->addr);
  215. /*
  216. * FIFO Initiation.
  217. * Data in FIFO should be discarded before start a new operation.
  218. */
  219. bfin_write_TWI_FIFO_CTL(0x3);
  220. sync();
  221. bfin_write_TWI_FIFO_CTL(0);
  222. sync();
  223. if (!(msg->flags & I2C_M_RD)) {
  224. /* Transmit first data */
  225. if (msg->len > 0) {
  226. PRINTD("1 in i2c_transfer: buf=%d, len=%d\n", *msg->buf,
  227. len);
  228. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  229. msg->len--;
  230. sync();
  231. }
  232. }
  233. /* clear int stat */
  234. bfin_write_TWI_INT_STAT(MERR | MCOMP | XMTSERV | RCVSERV);
  235. /* Interrupt mask . Enable XMT, RCV interrupt */
  236. bfin_write_TWI_INT_MASK(MCOMP | MERR |
  237. ((msg->flags & I2C_M_RD) ? RCVSERV : XMTSERV));
  238. sync();
  239. if (len > 0 && len <= 255)
  240. bfin_write_TWI_MASTER_CTL((len << 6));
  241. else if (msg->len > 255) {
  242. bfin_write_TWI_MASTER_CTL((0xff << 6));
  243. msg->flags &= I2C_M_STOP;
  244. } else
  245. bfin_write_TWI_MASTER_CTL(0);
  246. /* Master enable */
  247. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | MEN |
  248. ((msg->flags & I2C_M_RD)
  249. ? MDIR : 0) | ((CONFIG_TWICLK_KHZ >
  250. 100) ? FAST : 0));
  251. sync();
  252. ret = wait_for_completion(msg, timeout_count);
  253. PRINTD("3 in i2c_transfer: ret=%d\n", ret);
  254. transfer_error:
  255. switch (ret) {
  256. case 1:
  257. PRINTD(("i2c_transfer: error: transfer fail\n"));
  258. break;
  259. case 2:
  260. PRINTD(("i2c_transfer: error: transmit timeout\n"));
  261. break;
  262. case 3:
  263. PRINTD(("i2c_transfer: error: ACK missing\n"));
  264. break;
  265. case 4:
  266. PRINTD(("i2c_transfer: error: receive timeout\n"));
  267. break;
  268. case 5:
  269. PRINTD(("i2c_transfer: error: controller not ready\n"));
  270. i2c_reset();
  271. break;
  272. default:
  273. break;
  274. }
  275. return ret;
  276. }
  277. /* ---------------------------------------------------------------------*/
  278. /* API Functions */
  279. /* ---------------------------------------------------------------------*/
  280. void i2c_init(int speed, int slaveaddr)
  281. {
  282. i2c_reset();
  283. }
  284. /**
  285. * i2c_probe: - Test if a chip answers for a given i2c address
  286. *
  287. * @chip: address of the chip which is searched for
  288. * @return: 0 if a chip was found, -1 otherwhise
  289. */
  290. int i2c_probe(uchar chip)
  291. {
  292. struct i2c_msg msg;
  293. u8 probebuf;
  294. i2c_reset();
  295. probebuf = 0;
  296. msg.addr = chip;
  297. msg.flags = 0;
  298. msg.len = 1;
  299. msg.buf = &probebuf;
  300. if (i2c_transfer(&msg))
  301. return -1;
  302. msg.addr = chip;
  303. msg.flags = I2C_M_RD;
  304. msg.len = 1;
  305. msg.buf = &probebuf;
  306. if (i2c_transfer(&msg))
  307. return -1;
  308. return 0;
  309. }
  310. /**
  311. * i2c_read: - Read multiple bytes from an i2c device
  312. *
  313. * chip: I2C chip address, range 0..127
  314. * addr: Memory (register) address within the chip
  315. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  316. * memories, 0 for register type devices with only one
  317. * register)
  318. * buffer: Where to read/write the data
  319. * len: How many bytes to read/write
  320. *
  321. * Returns: 0 on success, not 0 on failure
  322. */
  323. int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  324. {
  325. struct i2c_msg msg;
  326. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  327. PRINTD("i2c_read: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x\n", chip,
  328. addr, alen, len);
  329. if (alen > 0) {
  330. addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
  331. addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
  332. addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
  333. msg.addr = chip;
  334. msg.flags = 0;
  335. msg.len = alen;
  336. msg.buf = addr_bytes;
  337. if (i2c_transfer(&msg))
  338. return -1;
  339. }
  340. /* start read sequence */
  341. PRINTD(("i2c_read: start read sequence\n"));
  342. msg.addr = chip;
  343. msg.flags = I2C_M_RD;
  344. msg.len = len;
  345. msg.buf = buffer;
  346. if (i2c_transfer(&msg))
  347. return -1;
  348. return 0;
  349. }
  350. /**
  351. * i2c_write: - Write multiple bytes to an i2c device
  352. *
  353. * chip: I2C chip address, range 0..127
  354. * addr: Memory (register) address within the chip
  355. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  356. * memories, 0 for register type devices with only one
  357. * register)
  358. * buffer: Where to read/write the data
  359. * len: How many bytes to read/write
  360. *
  361. * Returns: 0 on success, not 0 on failure
  362. */
  363. int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  364. {
  365. struct i2c_msg msg;
  366. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  367. PRINTD
  368. ("i2c_write: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x, buf0=0x%x\n",
  369. chip, addr, alen, len, buffer[0]);
  370. /* chip address write */
  371. if (alen > 0) {
  372. addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
  373. addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
  374. addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
  375. msg.addr = chip;
  376. msg.flags = 0;
  377. msg.len = alen;
  378. msg.buf = addr_bytes;
  379. if (i2c_transfer(&msg))
  380. return -1;
  381. }
  382. /* start read sequence */
  383. PRINTD(("i2c_write: start write sequence\n"));
  384. msg.addr = chip;
  385. msg.flags = 0;
  386. msg.len = len;
  387. msg.buf = buffer;
  388. if (i2c_transfer(&msg))
  389. return -1;
  390. return 0;
  391. }
  392. uchar i2c_reg_read(uchar chip, uchar reg)
  393. {
  394. uchar buf;
  395. PRINTD("i2c_reg_read: chip=0x%02x, reg=0x%02x\n", chip, reg);
  396. i2c_read(chip, reg, 0, &buf, 1);
  397. return (buf);
  398. }
  399. void i2c_reg_write(uchar chip, uchar reg, uchar val)
  400. {
  401. PRINTD("i2c_reg_write: chip=0x%02x, reg=0x%02x, val=0x%02x\n", chip,
  402. reg, val);
  403. i2c_write(chip, reg, 0, &val, 1);
  404. }
  405. #endif /* CONFIG_HARD_I2C */