bfin-twi_i2c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /* Allow msec timeout per ~byte transfer */
  57. #define I2C_TIMEOUT 10
  58. /**
  59. * wait_for_completion - manage the actual i2c transfer
  60. * @msg: the i2c msg
  61. */
  62. static int wait_for_completion(struct i2c_msg *msg)
  63. {
  64. uint16_t int_stat;
  65. ulong timebase = get_timer(0);
  66. do {
  67. int_stat = bfin_read_TWI_INT_STAT();
  68. if (int_stat & XMTSERV) {
  69. debugi("processing XMTSERV");
  70. bfin_write_TWI_INT_STAT(XMTSERV);
  71. SSYNC();
  72. if (msg->alen) {
  73. bfin_write_TWI_XMT_DATA8(*(msg->abuf++));
  74. --msg->alen;
  75. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  76. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  77. --msg->len;
  78. } else {
  79. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() |
  80. (msg->flags & I2C_M_COMBO ? RSTART | MDIR : STOP));
  81. SSYNC();
  82. }
  83. }
  84. if (int_stat & RCVSERV) {
  85. debugi("processing RCVSERV");
  86. bfin_write_TWI_INT_STAT(RCVSERV);
  87. SSYNC();
  88. if (msg->len) {
  89. *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
  90. --msg->len;
  91. } else if (msg->flags & I2C_M_STOP) {
  92. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | STOP);
  93. SSYNC();
  94. }
  95. }
  96. if (int_stat & MERR) {
  97. debugi("processing MERR");
  98. bfin_write_TWI_INT_STAT(MERR);
  99. SSYNC();
  100. return msg->len;
  101. }
  102. if (int_stat & MCOMP) {
  103. debugi("processing MCOMP");
  104. bfin_write_TWI_INT_STAT(MCOMP);
  105. SSYNC();
  106. if (msg->flags & I2C_M_COMBO && msg->len) {
  107. bfin_write_TWI_MASTER_CTL((bfin_read_TWI_MASTER_CTL() & ~RSTART) |
  108. (min(msg->len, 0xff) << 6) | MEN | MDIR);
  109. SSYNC();
  110. } else
  111. break;
  112. }
  113. /* If we were able to do something, reset timeout */
  114. if (int_stat)
  115. timebase = get_timer(0);
  116. } while (get_timer(timebase) < I2C_TIMEOUT);
  117. return msg->len;
  118. }
  119. /**
  120. * i2c_transfer - setup an i2c transfer
  121. * @return: 0 if things worked, non-0 if things failed
  122. *
  123. * Here we just get the i2c stuff all prepped and ready, and then tail off
  124. * into wait_for_completion() for all the bits to go.
  125. */
  126. static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
  127. {
  128. uchar addr_buffer[] = {
  129. (addr >> 0),
  130. (addr >> 8),
  131. (addr >> 16),
  132. };
  133. struct i2c_msg msg = {
  134. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  135. .buf = buffer,
  136. .len = len,
  137. .abuf = addr_buffer,
  138. .alen = alen,
  139. };
  140. int ret;
  141. dmemset(buffer, 0xff, len);
  142. debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
  143. chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
  144. /* wait for things to settle */
  145. while (bfin_read_TWI_MASTER_STAT() & BUSBUSY)
  146. if (ctrlc())
  147. return 1;
  148. /* Set Transmit device address */
  149. bfin_write_TWI_MASTER_ADDR(chip);
  150. /* Clear the FIFO before starting things */
  151. bfin_write_TWI_FIFO_CTL(XMTFLUSH | RCVFLUSH);
  152. SSYNC();
  153. bfin_write_TWI_FIFO_CTL(0);
  154. SSYNC();
  155. /* prime the pump */
  156. if (msg.alen) {
  157. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  158. debugi("first byte=0x%02x", *msg.abuf);
  159. bfin_write_TWI_XMT_DATA8(*(msg.abuf++));
  160. --msg.alen;
  161. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  162. debugi("first byte=0x%02x", *msg.buf);
  163. bfin_write_TWI_XMT_DATA8(*(msg.buf++));
  164. --msg.len;
  165. }
  166. /* clear int stat */
  167. bfin_write_TWI_MASTER_STAT(-1);
  168. bfin_write_TWI_INT_STAT(-1);
  169. bfin_write_TWI_INT_MASK(0);
  170. SSYNC();
  171. /* Master enable */
  172. bfin_write_TWI_MASTER_CTL(
  173. (bfin_read_TWI_MASTER_CTL() & FAST) |
  174. (min(len, 0xff) << 6) | MEN |
  175. ((msg.flags & I2C_M_READ) ? MDIR : 0)
  176. );
  177. SSYNC();
  178. debugi("CTL=0x%04x", bfin_read_TWI_MASTER_CTL());
  179. /* process the rest */
  180. ret = wait_for_completion(&msg);
  181. debugi("ret=%d", ret);
  182. if (ret) {
  183. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() & ~MEN);
  184. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() & ~TWI_ENA);
  185. SSYNC();
  186. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
  187. SSYNC();
  188. }
  189. return ret;
  190. }
  191. /*
  192. * i2c_init - initialize the i2c bus
  193. * @speed: bus speed (in HZ)
  194. * @slaveaddr: address of device in slave mode (0 - not slave)
  195. *
  196. * Slave mode isn't actually implemented. It'll stay that way until
  197. * we get a real request for it.
  198. */
  199. void i2c_init(int speed, int slaveaddr)
  200. {
  201. uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
  202. /* Set TWI internal clock as 10MHz */
  203. bfin_write_TWI_CONTROL(prescale);
  204. /* Set TWI interface clock as specified */
  205. bfin_write_TWI_CLKDIV(
  206. ((5 * 1024 / (speed / 1000)) << 8) |
  207. ((5 * 1024 / (speed / 1000)) & 0xFF)
  208. );
  209. /* Don't turn it on */
  210. bfin_write_TWI_MASTER_CTL(speed > 100000 ? FAST : 0);
  211. /* But enable it */
  212. bfin_write_TWI_CONTROL(TWI_ENA | prescale);
  213. SSYNC();
  214. debugi("CONTROL:0x%04x CLKDIV:0x%04x",
  215. bfin_read_TWI_CONTROL(), bfin_read_TWI_CLKDIV());
  216. #if CONFIG_SYS_I2C_SLAVE
  217. # error I2C slave support not tested/supported
  218. /* If they want us as a slave, do it */
  219. if (slaveaddr) {
  220. bfin_write_TWI_SLAVE_ADDR(slaveaddr);
  221. bfin_write_TWI_SLAVE_CTL(SEN);
  222. }
  223. #endif
  224. }
  225. /**
  226. * i2c_probe - test if a chip exists at a given i2c address
  227. * @chip: i2c chip addr to search for
  228. * @return: 0 if found, non-0 if not found
  229. */
  230. int i2c_probe(uchar chip)
  231. {
  232. u8 byte;
  233. return i2c_read(chip, 0, 0, &byte, 1);
  234. }
  235. /**
  236. * i2c_read - read data from an i2c device
  237. * @chip: i2c chip addr
  238. * @addr: memory (register) address in the chip
  239. * @alen: byte size of address
  240. * @buffer: buffer to store data read from chip
  241. * @len: how many bytes to read
  242. * @return: 0 on success, non-0 on failure
  243. */
  244. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  245. {
  246. return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
  247. }
  248. /**
  249. * i2c_write - write data to an i2c device
  250. * @chip: i2c chip addr
  251. * @addr: memory (register) address in the chip
  252. * @alen: byte size of address
  253. * @buffer: buffer holding data to write to chip
  254. * @len: how many bytes to write
  255. * @return: 0 on success, non-0 on failure
  256. */
  257. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  258. {
  259. return i2c_transfer(chip, addr, alen, buffer, len, 0);
  260. }