bfin-twi_i2c.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include <i2c.h>
  10. #include <asm/blackfin.h>
  11. #include <asm/mach-common/bits/twi.h>
  12. #ifdef DEBUG
  13. # define dmemset(s, c, n) memset(s, c, n)
  14. #else
  15. # define dmemset(s, c, n)
  16. #endif
  17. #define debugi(fmt, args...) \
  18. debug( \
  19. "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t" \
  20. "%-20s:%-3i: " fmt "\n", \
  21. bfin_read_TWI_MASTER_STAT(), bfin_read_TWI_FIFO_STAT(), bfin_read_TWI_INT_STAT(), \
  22. __func__, __LINE__, ## args)
  23. #ifdef TWI0_CLKDIV
  24. #define bfin_write_TWI_CLKDIV(val) bfin_write_TWI0_CLKDIV(val)
  25. #define bfin_write_TWI_CONTROL(val) bfin_write_TWI0_CONTROL(val)
  26. #define bfin_read_TWI_CONTROL(val) bfin_read_TWI0_CONTROL(val)
  27. #define bfin_write_TWI_MASTER_ADDR(val) bfin_write_TWI0_MASTER_ADDR(val)
  28. #define bfin_write_TWI_XMT_DATA8(val) bfin_write_TWI0_XMT_DATA8(val)
  29. #define bfin_read_TWI_RCV_DATA8() bfin_read_TWI0_RCV_DATA8()
  30. #define bfin_read_TWI_INT_STAT() bfin_read_TWI0_INT_STAT()
  31. #define bfin_write_TWI_INT_STAT(val) bfin_write_TWI0_INT_STAT(val)
  32. #define bfin_read_TWI_MASTER_STAT() bfin_read_TWI0_MASTER_STAT()
  33. #define bfin_write_TWI_MASTER_STAT(val) bfin_write_TWI0_MASTER_STAT(val)
  34. #define bfin_read_TWI_MASTER_CTL() bfin_read_TWI0_MASTER_CTL()
  35. #define bfin_write_TWI_MASTER_CTL(val) bfin_write_TWI0_MASTER_CTL(val)
  36. #define bfin_write_TWI_INT_MASK(val) bfin_write_TWI0_INT_MASK(val)
  37. #define bfin_write_TWI_FIFO_CTL(val) bfin_write_TWI0_FIFO_CTL(val)
  38. #endif
  39. #ifdef CONFIG_TWICLK_KHZ
  40. # error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
  41. #endif
  42. #if CONFIG_SYS_I2C_SPEED > 400000
  43. # error The Blackfin I2C hardware can only operate at 400KHz max
  44. #endif
  45. /* All transfers are described by this data structure */
  46. struct i2c_msg {
  47. u8 flags;
  48. #define I2C_M_COMBO 0x4
  49. #define I2C_M_STOP 0x2
  50. #define I2C_M_READ 0x1
  51. int len; /* msg length */
  52. u8 *buf; /* pointer to msg data */
  53. int alen; /* addr length */
  54. u8 *abuf; /* addr buffer */
  55. };
  56. /**
  57. * wait_for_completion - manage the actual i2c transfer
  58. * @msg: the i2c msg
  59. */
  60. static int wait_for_completion(struct i2c_msg *msg)
  61. {
  62. uint16_t int_stat;
  63. while (!ctrlc()) {
  64. int_stat = bfin_read_TWI_INT_STAT();
  65. if (int_stat & XMTSERV) {
  66. debugi("processing XMTSERV");
  67. bfin_write_TWI_INT_STAT(XMTSERV);
  68. SSYNC();
  69. if (msg->alen) {
  70. bfin_write_TWI_XMT_DATA8(*(msg->abuf++));
  71. --msg->alen;
  72. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  73. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  74. --msg->len;
  75. } else {
  76. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() |
  77. (msg->flags & I2C_M_COMBO ? RSTART | MDIR : STOP));
  78. SSYNC();
  79. }
  80. }
  81. if (int_stat & RCVSERV) {
  82. debugi("processing RCVSERV");
  83. bfin_write_TWI_INT_STAT(RCVSERV);
  84. SSYNC();
  85. if (msg->len) {
  86. *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
  87. --msg->len;
  88. } else if (msg->flags & I2C_M_STOP) {
  89. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | STOP);
  90. SSYNC();
  91. }
  92. }
  93. if (int_stat & MERR) {
  94. debugi("processing MERR");
  95. bfin_write_TWI_INT_STAT(MERR);
  96. SSYNC();
  97. break;
  98. }
  99. if (int_stat & MCOMP) {
  100. debugi("processing MCOMP");
  101. bfin_write_TWI_INT_STAT(MCOMP);
  102. SSYNC();
  103. if (msg->flags & I2C_M_COMBO && msg->len) {
  104. bfin_write_TWI_MASTER_CTL((bfin_read_TWI_MASTER_CTL() & ~RSTART) |
  105. (min(msg->len, 0xff) << 6) | MEN | MDIR);
  106. SSYNC();
  107. } else
  108. break;
  109. }
  110. }
  111. return msg->len;
  112. }
  113. /**
  114. * i2c_transfer - setup an i2c transfer
  115. * @return: 0 if things worked, non-0 if things failed
  116. *
  117. * Here we just get the i2c stuff all prepped and ready, and then tail off
  118. * into wait_for_completion() for all the bits to go.
  119. */
  120. static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
  121. {
  122. uchar addr_buffer[] = {
  123. (addr >> 0),
  124. (addr >> 8),
  125. (addr >> 16),
  126. };
  127. struct i2c_msg msg = {
  128. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  129. .buf = buffer,
  130. .len = len,
  131. .abuf = addr_buffer,
  132. .alen = alen,
  133. };
  134. int ret;
  135. dmemset(buffer, 0xff, len);
  136. debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
  137. chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
  138. /* wait for things to settle */
  139. while (bfin_read_TWI_MASTER_STAT() & BUSBUSY)
  140. if (ctrlc())
  141. return 1;
  142. /* Set Transmit device address */
  143. bfin_write_TWI_MASTER_ADDR(chip);
  144. /* Clear the FIFO before starting things */
  145. bfin_write_TWI_FIFO_CTL(XMTFLUSH | RCVFLUSH);
  146. SSYNC();
  147. bfin_write_TWI_FIFO_CTL(0);
  148. SSYNC();
  149. /* prime the pump */
  150. if (msg.alen) {
  151. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  152. debugi("first byte=0x%02x", *msg.abuf);
  153. bfin_write_TWI_XMT_DATA8(*(msg.abuf++));
  154. --msg.alen;
  155. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  156. debugi("first byte=0x%02x", *msg.buf);
  157. bfin_write_TWI_XMT_DATA8(*(msg.buf++));
  158. --msg.len;
  159. }
  160. /* clear int stat */
  161. bfin_write_TWI_MASTER_STAT(-1);
  162. bfin_write_TWI_INT_STAT(-1);
  163. bfin_write_TWI_INT_MASK(0);
  164. SSYNC();
  165. /* Master enable */
  166. bfin_write_TWI_MASTER_CTL(
  167. (bfin_read_TWI_MASTER_CTL() & FAST) |
  168. (min(len, 0xff) << 6) | MEN |
  169. ((msg.flags & I2C_M_READ) ? MDIR : 0)
  170. );
  171. SSYNC();
  172. debugi("CTL=0x%04x", bfin_read_TWI_MASTER_CTL());
  173. /* process the rest */
  174. ret = wait_for_completion(&msg);
  175. debugi("ret=%d", ret);
  176. if (ret) {
  177. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() & ~MEN);
  178. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() & ~TWI_ENA);
  179. SSYNC();
  180. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
  181. SSYNC();
  182. }
  183. return ret;
  184. }
  185. /*
  186. * i2c_init - initialize the i2c bus
  187. * @speed: bus speed (in HZ)
  188. * @slaveaddr: address of device in slave mode (0 - not slave)
  189. *
  190. * Slave mode isn't actually implemented. It'll stay that way until
  191. * we get a real request for it.
  192. */
  193. void i2c_init(int speed, int slaveaddr)
  194. {
  195. uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
  196. /* Set TWI internal clock as 10MHz */
  197. bfin_write_TWI_CONTROL(prescale);
  198. /* Set TWI interface clock as specified */
  199. bfin_write_TWI_CLKDIV(
  200. ((5 * 1024 / (speed / 1000)) << 8) |
  201. ((5 * 1024 / (speed / 1000)) & 0xFF)
  202. );
  203. /* Don't turn it on */
  204. bfin_write_TWI_MASTER_CTL(speed > 100000 ? FAST : 0);
  205. /* But enable it */
  206. bfin_write_TWI_CONTROL(TWI_ENA | prescale);
  207. SSYNC();
  208. debugi("CONTROL:0x%04x CLKDIV:0x%04x",
  209. bfin_read_TWI_CONTROL(), bfin_read_TWI_CLKDIV());
  210. #if CONFIG_SYS_I2C_SLAVE
  211. # error I2C slave support not tested/supported
  212. /* If they want us as a slave, do it */
  213. if (slaveaddr) {
  214. bfin_write_TWI_SLAVE_ADDR(slaveaddr);
  215. bfin_write_TWI_SLAVE_CTL(SEN);
  216. }
  217. #endif
  218. }
  219. /**
  220. * i2c_probe - test if a chip exists at a given i2c address
  221. * @chip: i2c chip addr to search for
  222. * @return: 0 if found, non-0 if not found
  223. */
  224. int i2c_probe(uchar chip)
  225. {
  226. u8 byte;
  227. return i2c_read(chip, 0, 0, &byte, 1);
  228. }
  229. /**
  230. * i2c_read - read data from an i2c device
  231. * @chip: i2c chip addr
  232. * @addr: memory (register) address in the chip
  233. * @alen: byte size of address
  234. * @buffer: buffer to store data read from chip
  235. * @len: how many bytes to read
  236. * @return: 0 on success, non-0 on failure
  237. */
  238. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  239. {
  240. return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
  241. }
  242. /**
  243. * i2c_write - write data to an i2c device
  244. * @chip: i2c chip addr
  245. * @addr: memory (register) address in the chip
  246. * @alen: byte size of address
  247. * @buffer: buffer holding data to write to chip
  248. * @len: how many bytes to write
  249. * @return: 0 on success, non-0 on failure
  250. */
  251. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  252. {
  253. return i2c_transfer(chip, addr, alen, buffer, len, 0);
  254. }