i2c.c 11 KB

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