bfin-twi_i2c.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_read_TWI_CLKDIV(val) bfin_read_TWI0_CLKDIV(val)
  26. #define bfin_write_TWI_CONTROL(val) bfin_write_TWI0_CONTROL(val)
  27. #define bfin_read_TWI_CONTROL(val) bfin_read_TWI0_CONTROL(val)
  28. #define bfin_write_TWI_MASTER_ADDR(val) bfin_write_TWI0_MASTER_ADDR(val)
  29. #define bfin_write_TWI_XMT_DATA8(val) bfin_write_TWI0_XMT_DATA8(val)
  30. #define bfin_read_TWI_RCV_DATA8() bfin_read_TWI0_RCV_DATA8()
  31. #define bfin_read_TWI_INT_STAT() bfin_read_TWI0_INT_STAT()
  32. #define bfin_write_TWI_INT_STAT(val) bfin_write_TWI0_INT_STAT(val)
  33. #define bfin_read_TWI_MASTER_STAT() bfin_read_TWI0_MASTER_STAT()
  34. #define bfin_write_TWI_MASTER_STAT(val) bfin_write_TWI0_MASTER_STAT(val)
  35. #define bfin_read_TWI_MASTER_CTL() bfin_read_TWI0_MASTER_CTL()
  36. #define bfin_write_TWI_MASTER_CTL(val) bfin_write_TWI0_MASTER_CTL(val)
  37. #define bfin_write_TWI_INT_MASK(val) bfin_write_TWI0_INT_MASK(val)
  38. #define bfin_write_TWI_FIFO_CTL(val) bfin_write_TWI0_FIFO_CTL(val)
  39. #endif
  40. #ifdef CONFIG_TWICLK_KHZ
  41. # error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
  42. #endif
  43. /*
  44. * The way speed is changed into duty often results in integer truncation
  45. * with 50% duty, so we'll force rounding up to the next duty by adding 1
  46. * to the max. In practice this will get us a speed of something like
  47. * 385 KHz. The other limit is easy to handle as it is only 8 bits.
  48. */
  49. #define I2C_SPEED_MAX 400000
  50. #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
  51. #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
  52. #define I2C_DUTY_MIN 0xff /* 8 bit limited */
  53. #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
  54. /* Note: duty is inverse of speed, so the comparisons below are correct */
  55. #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
  56. # error "The Blackfin I2C hardware can only operate 20KHz - 400KHz"
  57. #endif
  58. /* All transfers are described by this data structure */
  59. struct i2c_msg {
  60. u8 flags;
  61. #define I2C_M_COMBO 0x4
  62. #define I2C_M_STOP 0x2
  63. #define I2C_M_READ 0x1
  64. int len; /* msg length */
  65. u8 *buf; /* pointer to msg data */
  66. int alen; /* addr length */
  67. u8 *abuf; /* addr buffer */
  68. };
  69. /* Allow msec timeout per ~byte transfer */
  70. #define I2C_TIMEOUT 10
  71. /**
  72. * wait_for_completion - manage the actual i2c transfer
  73. * @msg: the i2c msg
  74. */
  75. static int wait_for_completion(struct i2c_msg *msg)
  76. {
  77. uint16_t int_stat;
  78. ulong timebase = get_timer(0);
  79. do {
  80. int_stat = bfin_read_TWI_INT_STAT();
  81. if (int_stat & XMTSERV) {
  82. debugi("processing XMTSERV");
  83. bfin_write_TWI_INT_STAT(XMTSERV);
  84. SSYNC();
  85. if (msg->alen) {
  86. bfin_write_TWI_XMT_DATA8(*(msg->abuf++));
  87. --msg->alen;
  88. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  89. bfin_write_TWI_XMT_DATA8(*(msg->buf++));
  90. --msg->len;
  91. } else {
  92. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() |
  93. (msg->flags & I2C_M_COMBO ? RSTART | MDIR : STOP));
  94. SSYNC();
  95. }
  96. }
  97. if (int_stat & RCVSERV) {
  98. debugi("processing RCVSERV");
  99. bfin_write_TWI_INT_STAT(RCVSERV);
  100. SSYNC();
  101. if (msg->len) {
  102. *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
  103. --msg->len;
  104. } else if (msg->flags & I2C_M_STOP) {
  105. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | STOP);
  106. SSYNC();
  107. }
  108. }
  109. if (int_stat & MERR) {
  110. debugi("processing MERR");
  111. bfin_write_TWI_INT_STAT(MERR);
  112. SSYNC();
  113. return msg->len;
  114. }
  115. if (int_stat & MCOMP) {
  116. debugi("processing MCOMP");
  117. bfin_write_TWI_INT_STAT(MCOMP);
  118. SSYNC();
  119. if (msg->flags & I2C_M_COMBO && msg->len) {
  120. bfin_write_TWI_MASTER_CTL((bfin_read_TWI_MASTER_CTL() & ~RSTART) |
  121. (min(msg->len, 0xff) << 6) | MEN | MDIR);
  122. SSYNC();
  123. } else
  124. break;
  125. }
  126. /* If we were able to do something, reset timeout */
  127. if (int_stat)
  128. timebase = get_timer(0);
  129. } while (get_timer(timebase) < I2C_TIMEOUT);
  130. return msg->len;
  131. }
  132. /**
  133. * i2c_transfer - setup an i2c transfer
  134. * @return: 0 if things worked, non-0 if things failed
  135. *
  136. * Here we just get the i2c stuff all prepped and ready, and then tail off
  137. * into wait_for_completion() for all the bits to go.
  138. */
  139. static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
  140. {
  141. uchar addr_buffer[] = {
  142. (addr >> 0),
  143. (addr >> 8),
  144. (addr >> 16),
  145. };
  146. struct i2c_msg msg = {
  147. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  148. .buf = buffer,
  149. .len = len,
  150. .abuf = addr_buffer,
  151. .alen = alen,
  152. };
  153. int ret;
  154. dmemset(buffer, 0xff, len);
  155. debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
  156. chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
  157. /* wait for things to settle */
  158. while (bfin_read_TWI_MASTER_STAT() & BUSBUSY)
  159. if (ctrlc())
  160. return 1;
  161. /* Set Transmit device address */
  162. bfin_write_TWI_MASTER_ADDR(chip);
  163. /* Clear the FIFO before starting things */
  164. bfin_write_TWI_FIFO_CTL(XMTFLUSH | RCVFLUSH);
  165. SSYNC();
  166. bfin_write_TWI_FIFO_CTL(0);
  167. SSYNC();
  168. /* prime the pump */
  169. if (msg.alen) {
  170. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  171. debugi("first byte=0x%02x", *msg.abuf);
  172. bfin_write_TWI_XMT_DATA8(*(msg.abuf++));
  173. --msg.alen;
  174. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  175. debugi("first byte=0x%02x", *msg.buf);
  176. bfin_write_TWI_XMT_DATA8(*(msg.buf++));
  177. --msg.len;
  178. }
  179. /* clear int stat */
  180. bfin_write_TWI_MASTER_STAT(-1);
  181. bfin_write_TWI_INT_STAT(-1);
  182. bfin_write_TWI_INT_MASK(0);
  183. SSYNC();
  184. /* Master enable */
  185. bfin_write_TWI_MASTER_CTL(
  186. (bfin_read_TWI_MASTER_CTL() & FAST) |
  187. (min(len, 0xff) << 6) | MEN |
  188. ((msg.flags & I2C_M_READ) ? MDIR : 0)
  189. );
  190. SSYNC();
  191. debugi("CTL=0x%04x", bfin_read_TWI_MASTER_CTL());
  192. /* process the rest */
  193. ret = wait_for_completion(&msg);
  194. debugi("ret=%d", ret);
  195. if (ret) {
  196. bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() & ~MEN);
  197. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() & ~TWI_ENA);
  198. SSYNC();
  199. bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
  200. SSYNC();
  201. }
  202. return ret;
  203. }
  204. /**
  205. * i2c_set_bus_speed - set i2c bus speed
  206. * @speed: bus speed (in HZ)
  207. */
  208. int i2c_set_bus_speed(unsigned int speed)
  209. {
  210. u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
  211. /* Set TWI interface clock */
  212. if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
  213. return -1;
  214. bfin_write_TWI_CLKDIV((clkdiv << 8) | (clkdiv & 0xff));
  215. /* Don't turn it on */
  216. bfin_write_TWI_MASTER_CTL(speed > 100000 ? FAST : 0);
  217. return 0;
  218. }
  219. /**
  220. * i2c_get_bus_speed - get i2c bus speed
  221. * @speed: bus speed (in HZ)
  222. */
  223. unsigned int i2c_get_bus_speed(void)
  224. {
  225. /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */
  226. return 5000000 / (bfin_read_TWI_CLKDIV() & 0xff);
  227. }
  228. /**
  229. * i2c_init - initialize the i2c bus
  230. * @speed: bus speed (in HZ)
  231. * @slaveaddr: address of device in slave mode (0 - not slave)
  232. *
  233. * Slave mode isn't actually implemented. It'll stay that way until
  234. * we get a real request for it.
  235. */
  236. void i2c_init(int speed, int slaveaddr)
  237. {
  238. uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
  239. /* Set TWI internal clock as 10MHz */
  240. bfin_write_TWI_CONTROL(prescale);
  241. /* Set TWI interface clock as specified */
  242. i2c_set_bus_speed(speed);
  243. /* Enable it */
  244. bfin_write_TWI_CONTROL(TWI_ENA | prescale);
  245. SSYNC();
  246. debugi("CONTROL:0x%04x CLKDIV:0x%04x",
  247. bfin_read_TWI_CONTROL(), bfin_read_TWI_CLKDIV());
  248. #if CONFIG_SYS_I2C_SLAVE
  249. # error I2C slave support not tested/supported
  250. /* If they want us as a slave, do it */
  251. if (slaveaddr) {
  252. bfin_write_TWI_SLAVE_ADDR(slaveaddr);
  253. bfin_write_TWI_SLAVE_CTL(SEN);
  254. }
  255. #endif
  256. }
  257. /**
  258. * i2c_probe - test if a chip exists at a given i2c address
  259. * @chip: i2c chip addr to search for
  260. * @return: 0 if found, non-0 if not found
  261. */
  262. int i2c_probe(uchar chip)
  263. {
  264. u8 byte;
  265. return i2c_read(chip, 0, 0, &byte, 1);
  266. }
  267. /**
  268. * i2c_read - read data from an i2c device
  269. * @chip: i2c chip addr
  270. * @addr: memory (register) address in the chip
  271. * @alen: byte size of address
  272. * @buffer: buffer to store data read from chip
  273. * @len: how many bytes to read
  274. * @return: 0 on success, non-0 on failure
  275. */
  276. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  277. {
  278. return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
  279. }
  280. /**
  281. * i2c_write - write data to an i2c device
  282. * @chip: i2c chip addr
  283. * @addr: memory (register) address in the chip
  284. * @alen: byte size of address
  285. * @buffer: buffer holding data to write to chip
  286. * @len: how many bytes to write
  287. * @return: 0 on success, non-0 on failure
  288. */
  289. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  290. {
  291. return i2c_transfer(chip, addr, alen, buffer, len, 0);
  292. }