adi_i2c.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * i2c.c - driver for ADI TWI/I2C
  3. *
  4. * Copyright (c) 2006-2013 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <i2c.h>
  10. #include <asm/clock.h>
  11. #include <asm/twi.h>
  12. #include <asm/io.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 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. u16 int_stat, ctl;
  88. ulong timebase = get_timer(0);
  89. do {
  90. int_stat = readw(&twi->int_stat);
  91. if (int_stat & XMTSERV) {
  92. debugi("processing XMTSERV");
  93. writew(XMTSERV, &twi->int_stat);
  94. if (msg->alen) {
  95. writew(*(msg->abuf++), &twi->xmt_data8);
  96. --msg->alen;
  97. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  98. writew(*(msg->buf++), &twi->xmt_data8);
  99. --msg->len;
  100. } else {
  101. ctl = readw(&twi->master_ctl);
  102. if (msg->flags & I2C_M_COMBO)
  103. writew(ctl | RSTART | MDIR,
  104. &twi->master_ctl);
  105. else
  106. writew(ctl | STOP, &twi->master_ctl);
  107. }
  108. }
  109. if (int_stat & RCVSERV) {
  110. debugi("processing RCVSERV");
  111. writew(RCVSERV, &twi->int_stat);
  112. if (msg->len) {
  113. *(msg->buf++) = readw(&twi->rcv_data8);
  114. --msg->len;
  115. } else if (msg->flags & I2C_M_STOP) {
  116. ctl = readw(&twi->master_ctl);
  117. writew(ctl | STOP, &twi->master_ctl);
  118. }
  119. }
  120. if (int_stat & MERR) {
  121. debugi("processing MERR");
  122. writew(MERR, &twi->int_stat);
  123. return msg->len;
  124. }
  125. if (int_stat & MCOMP) {
  126. debugi("processing MCOMP");
  127. writew(MCOMP, &twi->int_stat);
  128. if (msg->flags & I2C_M_COMBO && msg->len) {
  129. ctl = readw(&twi->master_ctl);
  130. ctl = (ctl & ~RSTART) |
  131. (min(msg->len, 0xff) << 6) | MEN | MDIR;
  132. writew(ctl, &twi->master_ctl);
  133. } else
  134. break;
  135. }
  136. /* If we were able to do something, reset timeout */
  137. if (int_stat)
  138. timebase = get_timer(0);
  139. } while (get_timer(timebase) < I2C_TIMEOUT);
  140. return msg->len;
  141. }
  142. /**
  143. * i2c_transfer - setup an i2c transfer
  144. * @return: 0 if things worked, non-0 if things failed
  145. *
  146. * Here we just get the i2c stuff all prepped and ready, and then tail off
  147. * into wait_for_completion() for all the bits to go.
  148. */
  149. static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer,
  150. int len, u8 flags)
  151. {
  152. int ret;
  153. u16 ctl;
  154. uchar addr_buffer[] = {
  155. (addr >> 0),
  156. (addr >> 8),
  157. (addr >> 16),
  158. };
  159. struct i2c_msg msg = {
  160. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  161. .buf = buffer,
  162. .len = len,
  163. .abuf = addr_buffer,
  164. .alen = alen,
  165. };
  166. dmemset(buffer, 0xff, len);
  167. debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i ",
  168. chip, addr, alen, buffer[0], len);
  169. debugi("flags=0x%02x[%s] ", flags,
  170. (flags & I2C_M_READ ? "rd" : "wr"));
  171. /* wait for things to settle */
  172. while (readw(&twi->master_stat) & BUSBUSY)
  173. if (ctrlc())
  174. return 1;
  175. /* Set Transmit device address */
  176. writew(chip, &twi->master_addr);
  177. /* Clear the FIFO before starting things */
  178. writew(XMTFLUSH | RCVFLUSH, &twi->fifo_ctl);
  179. writew(0, &twi->fifo_ctl);
  180. /* prime the pump */
  181. if (msg.alen) {
  182. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  183. debugi("first byte=0x%02x", *msg.abuf);
  184. writew(*(msg.abuf++), &twi->xmt_data8);
  185. --msg.alen;
  186. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  187. debugi("first byte=0x%02x", *msg.buf);
  188. writew(*(msg.buf++), &twi->xmt_data8);
  189. --msg.len;
  190. }
  191. /* clear int stat */
  192. writew(-1, &twi->master_stat);
  193. writew(-1, &twi->int_stat);
  194. writew(0, &twi->int_mask);
  195. /* Master enable */
  196. ctl = readw(&twi->master_ctl);
  197. ctl = (ctl & FAST) | (min(len, 0xff) << 6) | MEN |
  198. ((msg.flags & I2C_M_READ) ? MDIR : 0);
  199. writew(ctl, &twi->master_ctl);
  200. /* process the rest */
  201. ret = wait_for_completion(&msg);
  202. debugi("ret=%d", ret);
  203. if (ret) {
  204. ctl = readw(&twi->master_ctl) & ~MEN;
  205. writew(ctl, &twi->master_ctl);
  206. ctl = readw(&twi->control) & ~TWI_ENA;
  207. writew(ctl, &twi->control);
  208. ctl = readw(&twi->control) | TWI_ENA;
  209. writew(ctl, &twi->control);
  210. }
  211. return ret;
  212. }
  213. /**
  214. * i2c_set_bus_speed - set i2c bus speed
  215. * @speed: bus speed (in HZ)
  216. */
  217. int i2c_set_bus_speed(unsigned int speed)
  218. {
  219. u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
  220. /* Set TWI interface clock */
  221. if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
  222. return -1;
  223. clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
  224. writew(clkdiv, &twi->clkdiv);
  225. /* Don't turn it on */
  226. writew(speed > 100000 ? FAST : 0, &twi->master_ctl);
  227. return 0;
  228. }
  229. /**
  230. * i2c_get_bus_speed - get i2c bus speed
  231. * @speed: bus speed (in HZ)
  232. */
  233. unsigned int i2c_get_bus_speed(void)
  234. {
  235. u16 clkdiv = readw(&twi->clkdiv) & 0xff;
  236. /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */
  237. return 5000000 / clkdiv;
  238. }
  239. /**
  240. * i2c_init - initialize the i2c bus
  241. * @speed: bus speed (in HZ)
  242. * @slaveaddr: address of device in slave mode (0 - not slave)
  243. *
  244. * Slave mode isn't actually implemented. It'll stay that way until
  245. * we get a real request for it.
  246. */
  247. void i2c_init(int speed, int slaveaddr)
  248. {
  249. u16 prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
  250. /* Set TWI internal clock as 10MHz */
  251. writew(prescale, &twi->control);
  252. /* Set TWI interface clock as specified */
  253. i2c_set_bus_speed(speed);
  254. /* Enable it */
  255. writew(TWI_ENA | prescale, &twi->control);
  256. debugi("CONTROL:0x%04x CLKDIV:0x%04x", readw(&twi->control),
  257. readw(&twi->clkdiv));
  258. #if CONFIG_SYS_I2C_SLAVE
  259. # error I2C slave support not tested/supported
  260. #endif
  261. }
  262. /**
  263. * i2c_probe - test if a chip exists at a given i2c address
  264. * @chip: i2c chip addr to search for
  265. * @return: 0 if found, non-0 if not found
  266. */
  267. int i2c_probe(uchar chip)
  268. {
  269. u8 byte;
  270. return i2c_read(chip, 0, 0, &byte, 1);
  271. }
  272. /**
  273. * i2c_read - read data from an i2c device
  274. * @chip: i2c chip addr
  275. * @addr: memory (register) address in the chip
  276. * @alen: byte size of address
  277. * @buffer: buffer to store data read from chip
  278. * @len: how many bytes to read
  279. * @return: 0 on success, non-0 on failure
  280. */
  281. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  282. {
  283. return i2c_transfer(chip, addr, alen, buffer,
  284. 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:
  309. twi = (void *)TWI0_CLKDIV;
  310. return 0;
  311. #endif
  312. #if CONFIG_SYS_MAX_I2C_BUS > 1
  313. case 1:
  314. twi = (void *)TWI1_CLKDIV;
  315. return 0;
  316. #endif
  317. #if CONFIG_SYS_MAX_I2C_BUS > 2
  318. case 2:
  319. twi = (void *)TWI2_CLKDIV;
  320. return 0;
  321. #endif
  322. default: return -1;
  323. }
  324. }
  325. /**
  326. * i2c_get_bus_num - returns index of active I2C bus
  327. */
  328. unsigned int i2c_get_bus_num(void)
  329. {
  330. switch ((unsigned long)twi) {
  331. #if CONFIG_SYS_MAX_I2C_BUS > 0
  332. case TWI0_CLKDIV:
  333. return 0;
  334. #endif
  335. #if CONFIG_SYS_MAX_I2C_BUS > 1
  336. case TWI1_CLKDIV:
  337. return 1;
  338. #endif
  339. #if CONFIG_SYS_MAX_I2C_BUS > 2
  340. case TWI2_CLKDIV:
  341. return 2;
  342. #endif
  343. default: return -1;
  344. }
  345. }