bfin-twi_i2c.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * i2c.c - driver for Blackfin on-chip TWI/I2C
  3. *
  4. * Copyright (c) 2006-2010 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/clock.h>
  12. #include <asm/mach-common/bits/twi.h>
  13. /* Every register is 32bit aligned, but only 16bits in size */
  14. #define ureg(name) u16 name; u16 __pad_##name;
  15. struct twi_regs {
  16. ureg(clkdiv);
  17. ureg(control);
  18. ureg(slave_ctl);
  19. ureg(slave_stat);
  20. ureg(slave_addr);
  21. ureg(master_ctl);
  22. ureg(master_stat);
  23. ureg(master_addr);
  24. ureg(int_stat);
  25. ureg(int_mask);
  26. ureg(fifo_ctl);
  27. ureg(fifo_stat);
  28. char __pad[0x50];
  29. ureg(xmt_data8);
  30. ureg(xmt_data16);
  31. ureg(rcv_data8);
  32. ureg(rcv_data16);
  33. };
  34. #undef ureg
  35. /* U-Boot I2C framework allows only one active device at a time. */
  36. #ifdef TWI_CLKDIV
  37. #define TWI0_CLKDIV TWI_CLKDIV
  38. #endif
  39. static volatile struct twi_regs *twi = (void *)TWI0_CLKDIV;
  40. #ifdef DEBUG
  41. # define dmemset(s, c, n) memset(s, c, n)
  42. #else
  43. # define dmemset(s, c, n)
  44. #endif
  45. #define debugi(fmt, args...) \
  46. debug( \
  47. "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t%-20s:%-3i: " fmt "\n", \
  48. twi->master_stat, twi->fifo_stat, twi->int_stat, \
  49. __func__, __LINE__, ## args)
  50. #ifdef CONFIG_TWICLK_KHZ
  51. # error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
  52. #endif
  53. /*
  54. * The way speed is changed into duty often results in integer truncation
  55. * with 50% duty, so we'll force rounding up to the next duty by adding 1
  56. * to the max. In practice this will get us a speed of something like
  57. * 385 KHz. The other limit is easy to handle as it is only 8 bits.
  58. */
  59. #define I2C_SPEED_MAX 400000
  60. #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
  61. #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
  62. #define I2C_DUTY_MIN 0xff /* 8 bit limited */
  63. #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
  64. /* Note: duty is inverse of speed, so the comparisons below are correct */
  65. #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
  66. # error "The Blackfin I2C hardware can only operate 20KHz - 400KHz"
  67. #endif
  68. /* All transfers are described by this data structure */
  69. struct i2c_msg {
  70. u8 flags;
  71. #define I2C_M_COMBO 0x4
  72. #define I2C_M_STOP 0x2
  73. #define I2C_M_READ 0x1
  74. int len; /* msg length */
  75. u8 *buf; /* pointer to msg data */
  76. int alen; /* addr length */
  77. u8 *abuf; /* addr buffer */
  78. };
  79. /* Allow msec timeout per ~byte transfer */
  80. #define I2C_TIMEOUT 10
  81. /**
  82. * wait_for_completion - manage the actual i2c transfer
  83. * @msg: the i2c msg
  84. */
  85. static int wait_for_completion(struct i2c_msg *msg)
  86. {
  87. uint16_t int_stat;
  88. ulong timebase = get_timer(0);
  89. do {
  90. int_stat = twi->int_stat;
  91. if (int_stat & XMTSERV) {
  92. debugi("processing XMTSERV");
  93. twi->int_stat = XMTSERV;
  94. SSYNC();
  95. if (msg->alen) {
  96. twi->xmt_data8 = *(msg->abuf++);
  97. --msg->alen;
  98. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  99. twi->xmt_data8 = *(msg->buf++);
  100. --msg->len;
  101. } else {
  102. twi->master_ctl |= (msg->flags & I2C_M_COMBO) ? RSTART | MDIR : STOP;
  103. SSYNC();
  104. }
  105. }
  106. if (int_stat & RCVSERV) {
  107. debugi("processing RCVSERV");
  108. twi->int_stat = RCVSERV;
  109. SSYNC();
  110. if (msg->len) {
  111. *(msg->buf++) = twi->rcv_data8;
  112. --msg->len;
  113. } else if (msg->flags & I2C_M_STOP) {
  114. twi->master_ctl |= STOP;
  115. SSYNC();
  116. }
  117. }
  118. if (int_stat & MERR) {
  119. debugi("processing MERR");
  120. twi->int_stat = MERR;
  121. SSYNC();
  122. return msg->len;
  123. }
  124. if (int_stat & MCOMP) {
  125. debugi("processing MCOMP");
  126. twi->int_stat = MCOMP;
  127. SSYNC();
  128. if (msg->flags & I2C_M_COMBO && msg->len) {
  129. twi->master_ctl = (twi->master_ctl & ~RSTART) |
  130. (min(msg->len, 0xff) << 6) | MEN | MDIR;
  131. SSYNC();
  132. } else
  133. break;
  134. }
  135. /* If we were able to do something, reset timeout */
  136. if (int_stat)
  137. timebase = get_timer(0);
  138. } while (get_timer(timebase) < I2C_TIMEOUT);
  139. return msg->len;
  140. }
  141. /**
  142. * i2c_transfer - setup an i2c transfer
  143. * @return: 0 if things worked, non-0 if things failed
  144. *
  145. * Here we just get the i2c stuff all prepped and ready, and then tail off
  146. * into wait_for_completion() for all the bits to go.
  147. */
  148. static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
  149. {
  150. uchar addr_buffer[] = {
  151. (addr >> 0),
  152. (addr >> 8),
  153. (addr >> 16),
  154. };
  155. struct i2c_msg msg = {
  156. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  157. .buf = buffer,
  158. .len = len,
  159. .abuf = addr_buffer,
  160. .alen = alen,
  161. };
  162. int ret;
  163. dmemset(buffer, 0xff, len);
  164. debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
  165. chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
  166. /* wait for things to settle */
  167. while (twi->master_stat & BUSBUSY)
  168. if (ctrlc())
  169. return 1;
  170. /* Set Transmit device address */
  171. twi->master_addr = chip;
  172. /* Clear the FIFO before starting things */
  173. twi->fifo_ctl = XMTFLUSH | RCVFLUSH;
  174. SSYNC();
  175. twi->fifo_ctl = 0;
  176. SSYNC();
  177. /* prime the pump */
  178. if (msg.alen) {
  179. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  180. debugi("first byte=0x%02x", *msg.abuf);
  181. twi->xmt_data8 = *(msg.abuf++);
  182. --msg.alen;
  183. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  184. debugi("first byte=0x%02x", *msg.buf);
  185. twi->xmt_data8 = *(msg.buf++);
  186. --msg.len;
  187. }
  188. /* clear int stat */
  189. twi->master_stat = -1;
  190. twi->int_stat = -1;
  191. twi->int_mask = 0;
  192. SSYNC();
  193. /* Master enable */
  194. twi->master_ctl =
  195. (twi->master_ctl & FAST) |
  196. (min(len, 0xff) << 6) | MEN |
  197. ((msg.flags & I2C_M_READ) ? MDIR : 0);
  198. SSYNC();
  199. debugi("CTL=0x%04x", twi->master_ctl);
  200. /* process the rest */
  201. ret = wait_for_completion(&msg);
  202. debugi("ret=%d", ret);
  203. if (ret) {
  204. twi->master_ctl &= ~MEN;
  205. twi->control &= ~TWI_ENA;
  206. SSYNC();
  207. twi->control |= TWI_ENA;
  208. SSYNC();
  209. }
  210. return ret;
  211. }
  212. /**
  213. * i2c_set_bus_speed - set i2c bus speed
  214. * @speed: bus speed (in HZ)
  215. */
  216. int i2c_set_bus_speed(unsigned int speed)
  217. {
  218. u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
  219. /* Set TWI interface clock */
  220. if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
  221. return -1;
  222. twi->clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
  223. /* Don't turn it on */
  224. twi->master_ctl = (speed > 100000 ? FAST : 0);
  225. return 0;
  226. }
  227. /**
  228. * i2c_get_bus_speed - get i2c bus speed
  229. * @speed: bus speed (in HZ)
  230. */
  231. unsigned int i2c_get_bus_speed(void)
  232. {
  233. /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */
  234. return 5000000 / (twi->clkdiv & 0xff);
  235. }
  236. /**
  237. * i2c_init - initialize the i2c bus
  238. * @speed: bus speed (in HZ)
  239. * @slaveaddr: address of device in slave mode (0 - not slave)
  240. *
  241. * Slave mode isn't actually implemented. It'll stay that way until
  242. * we get a real request for it.
  243. */
  244. void i2c_init(int speed, int slaveaddr)
  245. {
  246. uint8_t prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
  247. /* Set TWI internal clock as 10MHz */
  248. twi->control = prescale;
  249. /* Set TWI interface clock as specified */
  250. i2c_set_bus_speed(speed);
  251. /* Enable it */
  252. twi->control = TWI_ENA | prescale;
  253. SSYNC();
  254. debugi("CONTROL:0x%04x CLKDIV:0x%04x", twi->control, twi->clkdiv);
  255. #if CONFIG_SYS_I2C_SLAVE
  256. # error I2C slave support not tested/supported
  257. /* If they want us as a slave, do it */
  258. if (slaveaddr) {
  259. twi->slave_addr = slaveaddr;
  260. twi->slave_ctl = SEN;
  261. }
  262. #endif
  263. }
  264. /**
  265. * i2c_probe - test if a chip exists at a given i2c address
  266. * @chip: i2c chip addr to search for
  267. * @return: 0 if found, non-0 if not found
  268. */
  269. int i2c_probe(uchar chip)
  270. {
  271. u8 byte;
  272. return i2c_read(chip, 0, 0, &byte, 1);
  273. }
  274. /**
  275. * i2c_read - read data from an i2c device
  276. * @chip: i2c chip addr
  277. * @addr: memory (register) address in the chip
  278. * @alen: byte size of address
  279. * @buffer: buffer to store data read from chip
  280. * @len: how many bytes to read
  281. * @return: 0 on success, non-0 on failure
  282. */
  283. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  284. {
  285. return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
  286. }
  287. /**
  288. * i2c_write - write data to an i2c device
  289. * @chip: i2c chip addr
  290. * @addr: memory (register) address in the chip
  291. * @alen: byte size of address
  292. * @buffer: buffer holding data to write to chip
  293. * @len: how many bytes to write
  294. * @return: 0 on success, non-0 on failure
  295. */
  296. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  297. {
  298. return i2c_transfer(chip, addr, alen, buffer, len, 0);
  299. }
  300. /**
  301. * i2c_set_bus_num - change active I2C bus
  302. * @bus: bus index, zero based
  303. * @returns: 0 on success, non-0 on failure
  304. */
  305. int i2c_set_bus_num(unsigned int bus)
  306. {
  307. switch (bus) {
  308. #if CONFIG_SYS_MAX_I2C_BUS > 0
  309. case 0: twi = (void *)TWI0_CLKDIV; return 0;
  310. #endif
  311. #if CONFIG_SYS_MAX_I2C_BUS > 1
  312. case 1: twi = (void *)TWI1_CLKDIV; return 0;
  313. #endif
  314. #if CONFIG_SYS_MAX_I2C_BUS > 2
  315. case 2: twi = (void *)TWI2_CLKDIV; return 0;
  316. #endif
  317. default: return -1;
  318. }
  319. }
  320. /**
  321. * i2c_get_bus_num - returns index of active I2C bus
  322. */
  323. unsigned int i2c_get_bus_num(void)
  324. {
  325. switch ((unsigned long)twi) {
  326. #if CONFIG_SYS_MAX_I2C_BUS > 0
  327. case TWI0_CLKDIV: return 0;
  328. #endif
  329. #if CONFIG_SYS_MAX_I2C_BUS > 1
  330. case TWI1_CLKDIV: return 1;
  331. #endif
  332. #if CONFIG_SYS_MAX_I2C_BUS > 2
  333. case TWI2_CLKDIV: return 2;
  334. #endif
  335. default: return -1;
  336. }
  337. }