bfin-twi_i2c.c 8.9 KB

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