i2c.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include <asm/mach-common/bits/twi.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #ifdef DEBUG_I2C
  24. #define PRINTD(fmt,args...) do { \
  25. if (gd->have_console) \
  26. printf(fmt ,##args); \
  27. } while (0)
  28. #else
  29. #define PRINTD(fmt,args...)
  30. #endif
  31. #ifndef CONFIG_TWICLK_KHZ
  32. #define CONFIG_TWICLK_KHZ 50
  33. #endif
  34. /* All transfers are described by this data structure */
  35. struct i2c_msg {
  36. u16 addr; /* slave address */
  37. u16 flags;
  38. #define I2C_M_STOP 0x2
  39. #define I2C_M_RD 0x1
  40. u16 len; /* msg length */
  41. u8 *buf; /* pointer to msg data */
  42. };
  43. /**
  44. * i2c_reset: - reset the host controller
  45. *
  46. */
  47. static void i2c_reset(void)
  48. {
  49. /* Disable TWI */
  50. bfin_write_TWI_CONTROL(0);
  51. sync();
  52. /* Set TWI internal clock as 10MHz */
  53. bfin_write_TWI_CONTROL(((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
  54. /* Set Twi interface clock as specified */
  55. if (CONFIG_TWICLK_KHZ > 400)
  56. bfin_write_TWI_CLKDIV(((5 * 1024 / 400) << 8) | ((5 * 1024 /
  57. 400) & 0xFF));
  58. else
  59. bfin_write_TWI_CLKDIV(((5 * 1024 /
  60. CONFIG_TWICLK_KHZ) << 8) | ((5 * 1024 /
  61. CONFIG_TWICLK_KHZ)
  62. & 0xFF));
  63. /* Enable TWI */
  64. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
  65. sync();
  66. }
  67. int wait_for_completion(struct i2c_msg *msg, int timeout_count)
  68. {
  69. unsigned short twi_int_stat;
  70. unsigned short mast_stat;
  71. int i;
  72. for (i = 0; i < timeout_count; i++) {
  73. twi_int_stat = bfin_read_TWI_INT_STAT();
  74. mast_stat = bfin_read_TWI_MASTER_STAT();
  75. if (XMTSERV & twi_int_stat) {
  76. /* Transmit next data */
  77. if (msg->len > 0) {
  78. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  79. msg->len--;
  80. } else if (msg->flags & I2C_M_STOP)
  81. bfin_write_TWI_MASTER_CTL
  82. (bfin_read_TWI_MASTER_CTL() | STOP);
  83. sync();
  84. /* Clear status */
  85. bfin_write_TWI_INT_STAT(XMTSERV);
  86. sync();
  87. i = 0;
  88. }
  89. if (RCVSERV & twi_int_stat) {
  90. if (msg->len > 0) {
  91. /* Receive next data */
  92. *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
  93. msg->len--;
  94. } else if (msg->flags & I2C_M_STOP) {
  95. bfin_write_TWI_MASTER_CTL
  96. (bfin_read_TWI_MASTER_CTL() | STOP);
  97. sync();
  98. }
  99. /* Clear interrupt source */
  100. bfin_write_TWI_INT_STAT(RCVSERV);
  101. sync();
  102. i = 0;
  103. }
  104. if (MERR & twi_int_stat) {
  105. bfin_write_TWI_INT_STAT(MERR);
  106. bfin_write_TWI_INT_MASK(0);
  107. bfin_write_TWI_MASTER_STAT(0x3e);
  108. bfin_write_TWI_MASTER_CTL(0);
  109. sync();
  110. /*
  111. * if both err and complete int stats are set,
  112. * return proper results.
  113. */
  114. if (MCOMP & twi_int_stat) {
  115. bfin_write_TWI_INT_STAT(MCOMP);
  116. bfin_write_TWI_INT_MASK(0);
  117. bfin_write_TWI_MASTER_CTL(0);
  118. sync();
  119. /*
  120. * If it is a quick transfer,
  121. * only address bug no data, not an err.
  122. */
  123. if (msg->len == 0 && mast_stat & BUFRDERR)
  124. return 0;
  125. /*
  126. * If address not acknowledged return -3,
  127. * else return 0.
  128. */
  129. else if (!(mast_stat & ANAK))
  130. return 0;
  131. else
  132. return -3;
  133. }
  134. return -1;
  135. }
  136. if (MCOMP & twi_int_stat) {
  137. bfin_write_TWI_INT_STAT(MCOMP);
  138. sync();
  139. bfin_write_TWI_INT_MASK(0);
  140. bfin_write_TWI_MASTER_CTL(0);
  141. sync();
  142. return 0;
  143. }
  144. }
  145. if (msg->flags & I2C_M_RD)
  146. return -4;
  147. else
  148. return -2;
  149. }
  150. /**
  151. * i2c_transfer: - Transfer one byte over the i2c bus
  152. *
  153. * This function can tranfer a byte over the i2c bus in both directions.
  154. * It is used by the public API functions.
  155. *
  156. * @return: 0: transfer successful
  157. * -1: transfer fail
  158. * -2: transmit timeout
  159. * -3: ACK missing
  160. * -4: receive timeout
  161. * -5: controller not ready
  162. */
  163. int i2c_transfer(struct i2c_msg *msg)
  164. {
  165. int ret = 0;
  166. int timeout_count = 10000;
  167. int len = msg->len;
  168. if (!(bfin_read_TWI_CONTROL() & TWI_ENA)) {
  169. ret = -5;
  170. goto transfer_error;
  171. }
  172. while (bfin_read_TWI_MASTER_STAT() & BUSBUSY) ;
  173. /* Set Transmit device address */
  174. bfin_write_TWI_MASTER_ADDR(msg->addr);
  175. /*
  176. * FIFO Initiation.
  177. * Data in FIFO should be discarded before start a new operation.
  178. */
  179. bfin_write_TWI_FIFO_CTL(0x3);
  180. sync();
  181. bfin_write_TWI_FIFO_CTL(0);
  182. sync();
  183. if (!(msg->flags & I2C_M_RD)) {
  184. /* Transmit first data */
  185. if (msg->len > 0) {
  186. PRINTD("1 in i2c_transfer: buf=%d, len=%d\n", *msg->buf,
  187. len);
  188. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  189. msg->len--;
  190. sync();
  191. }
  192. }
  193. /* clear int stat */
  194. bfin_write_TWI_INT_STAT(MERR | MCOMP | XMTSERV | RCVSERV);
  195. /* Interrupt mask . Enable XMT, RCV interrupt */
  196. bfin_write_TWI_INT_MASK(MCOMP | MERR |
  197. ((msg->flags & I2C_M_RD) ? RCVSERV : XMTSERV));
  198. sync();
  199. if (len > 0 && len <= 255)
  200. bfin_write_TWI_MASTER_CTL((len << 6));
  201. else if (msg->len > 255) {
  202. bfin_write_TWI_MASTER_CTL((0xff << 6));
  203. msg->flags &= I2C_M_STOP;
  204. } else
  205. bfin_write_TWI_MASTER_CTL(0);
  206. /* Master enable */
  207. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | MEN |
  208. ((msg->flags & I2C_M_RD)
  209. ? MDIR : 0) | ((CONFIG_TWICLK_KHZ >
  210. 100) ? FAST : 0));
  211. sync();
  212. ret = wait_for_completion(msg, timeout_count);
  213. PRINTD("3 in i2c_transfer: ret=%d\n", ret);
  214. transfer_error:
  215. switch (ret) {
  216. case 1:
  217. PRINTD(("i2c_transfer: error: transfer fail\n"));
  218. break;
  219. case 2:
  220. PRINTD(("i2c_transfer: error: transmit timeout\n"));
  221. break;
  222. case 3:
  223. PRINTD(("i2c_transfer: error: ACK missing\n"));
  224. break;
  225. case 4:
  226. PRINTD(("i2c_transfer: error: receive timeout\n"));
  227. break;
  228. case 5:
  229. PRINTD(("i2c_transfer: error: controller not ready\n"));
  230. i2c_reset();
  231. break;
  232. default:
  233. break;
  234. }
  235. return ret;
  236. }
  237. /* ---------------------------------------------------------------------*/
  238. /* API Functions */
  239. /* ---------------------------------------------------------------------*/
  240. void i2c_init(int speed, int slaveaddr)
  241. {
  242. i2c_reset();
  243. }
  244. /**
  245. * i2c_probe: - Test if a chip answers for a given i2c address
  246. *
  247. * @chip: address of the chip which is searched for
  248. * @return: 0 if a chip was found, -1 otherwhise
  249. */
  250. int i2c_probe(uchar chip)
  251. {
  252. struct i2c_msg msg;
  253. u8 probebuf;
  254. i2c_reset();
  255. probebuf = 0;
  256. msg.addr = chip;
  257. msg.flags = 0;
  258. msg.len = 1;
  259. msg.buf = &probebuf;
  260. if (i2c_transfer(&msg))
  261. return -1;
  262. msg.addr = chip;
  263. msg.flags = I2C_M_RD;
  264. msg.len = 1;
  265. msg.buf = &probebuf;
  266. if (i2c_transfer(&msg))
  267. return -1;
  268. return 0;
  269. }
  270. /**
  271. * i2c_read: - Read multiple bytes from an i2c device
  272. *
  273. * chip: I2C chip address, range 0..127
  274. * addr: Memory (register) address within the chip
  275. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  276. * memories, 0 for register type devices with only one
  277. * register)
  278. * buffer: Where to read/write the data
  279. * len: How many bytes to read/write
  280. *
  281. * Returns: 0 on success, not 0 on failure
  282. */
  283. int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  284. {
  285. struct i2c_msg msg;
  286. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  287. PRINTD("i2c_read: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x\n", chip,
  288. addr, alen, len);
  289. if (alen > 0) {
  290. addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
  291. addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
  292. addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
  293. msg.addr = chip;
  294. msg.flags = 0;
  295. msg.len = alen;
  296. msg.buf = addr_bytes;
  297. if (i2c_transfer(&msg))
  298. return -1;
  299. }
  300. /* start read sequence */
  301. PRINTD(("i2c_read: start read sequence\n"));
  302. msg.addr = chip;
  303. msg.flags = I2C_M_RD;
  304. msg.len = len;
  305. msg.buf = buffer;
  306. if (i2c_transfer(&msg))
  307. return -1;
  308. return 0;
  309. }
  310. /**
  311. * i2c_write: - Write multiple bytes to 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_write(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
  328. ("i2c_write: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x, buf0=0x%x\n",
  329. chip, addr, alen, len, buffer[0]);
  330. /* chip address write */
  331. if (alen > 0) {
  332. addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
  333. addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
  334. addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
  335. msg.addr = chip;
  336. msg.flags = 0;
  337. msg.len = alen;
  338. msg.buf = addr_bytes;
  339. if (i2c_transfer(&msg))
  340. return -1;
  341. }
  342. /* start read sequence */
  343. PRINTD(("i2c_write: start write sequence\n"));
  344. msg.addr = chip;
  345. msg.flags = 0;
  346. msg.len = len;
  347. msg.buf = buffer;
  348. if (i2c_transfer(&msg))
  349. return -1;
  350. return 0;
  351. }
  352. uchar i2c_reg_read(uchar chip, uchar reg)
  353. {
  354. uchar buf;
  355. PRINTD("i2c_reg_read: chip=0x%02x, reg=0x%02x\n", chip, reg);
  356. i2c_read(chip, reg, 0, &buf, 1);
  357. return (buf);
  358. }
  359. void i2c_reg_write(uchar chip, uchar reg, uchar val)
  360. {
  361. PRINTD("i2c_reg_write: chip=0x%02x, reg=0x%02x, val=0x%02x\n", chip,
  362. reg, val);
  363. i2c_write(chip, reg, 0, &val, 1);
  364. }
  365. #endif /* CONFIG_HARD_I2C */