adi_i2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * i2c.c - driver for ADI TWI/I2C
  3. *
  4. * Copyright (c) 2006-2014 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <console.h>
  10. #include <i2c.h>
  11. #include <asm/clock.h>
  12. #include <asm/twi.h>
  13. #include <asm/io.h>
  14. static struct twi_regs *i2c_get_base(struct i2c_adapter *adap);
  15. /* Every register is 32bit aligned, but only 16bits in size */
  16. #define ureg(name) u16 name; u16 __pad_##name;
  17. struct twi_regs {
  18. ureg(clkdiv);
  19. ureg(control);
  20. ureg(slave_ctl);
  21. ureg(slave_stat);
  22. ureg(slave_addr);
  23. ureg(master_ctl);
  24. ureg(master_stat);
  25. ureg(master_addr);
  26. ureg(int_stat);
  27. ureg(int_mask);
  28. ureg(fifo_ctl);
  29. ureg(fifo_stat);
  30. char __pad[0x50];
  31. ureg(xmt_data8);
  32. ureg(xmt_data16);
  33. ureg(rcv_data8);
  34. ureg(rcv_data16);
  35. };
  36. #undef ureg
  37. #ifdef TWI_CLKDIV
  38. #define TWI0_CLKDIV TWI_CLKDIV
  39. # ifdef CONFIG_SYS_MAX_I2C_BUS
  40. # undef CONFIG_SYS_MAX_I2C_BUS
  41. # endif
  42. #define CONFIG_SYS_MAX_I2C_BUS 1
  43. #endif
  44. /*
  45. * The way speed is changed into duty often results in integer truncation
  46. * with 50% duty, so we'll force rounding up to the next duty by adding 1
  47. * to the max. In practice this will get us a speed of something like
  48. * 385 KHz. The other limit is easy to handle as it is only 8 bits.
  49. */
  50. #define I2C_SPEED_MAX 400000
  51. #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
  52. #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
  53. #define I2C_DUTY_MIN 0xff /* 8 bit limited */
  54. #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
  55. /* Note: duty is inverse of speed, so the comparisons below are correct */
  56. #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
  57. # error "The I2C hardware can only operate 20KHz - 400KHz"
  58. #endif
  59. /* All transfers are described by this data structure */
  60. struct adi_i2c_msg {
  61. u8 flags;
  62. #define I2C_M_COMBO 0x4
  63. #define I2C_M_STOP 0x2
  64. #define I2C_M_READ 0x1
  65. int len; /* msg length */
  66. u8 *buf; /* pointer to msg data */
  67. int alen; /* addr length */
  68. u8 *abuf; /* addr buffer */
  69. };
  70. /* Allow msec timeout per ~byte transfer */
  71. #define I2C_TIMEOUT 10
  72. /**
  73. * wait_for_completion - manage the actual i2c transfer
  74. * @msg: the i2c msg
  75. */
  76. static int wait_for_completion(struct twi_regs *twi, struct adi_i2c_msg *msg)
  77. {
  78. u16 int_stat, ctl;
  79. ulong timebase = get_timer(0);
  80. do {
  81. int_stat = readw(&twi->int_stat);
  82. if (int_stat & XMTSERV) {
  83. writew(XMTSERV, &twi->int_stat);
  84. if (msg->alen) {
  85. writew(*(msg->abuf++), &twi->xmt_data8);
  86. --msg->alen;
  87. } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
  88. writew(*(msg->buf++), &twi->xmt_data8);
  89. --msg->len;
  90. } else {
  91. ctl = readw(&twi->master_ctl);
  92. if (msg->flags & I2C_M_COMBO)
  93. writew(ctl | RSTART | MDIR,
  94. &twi->master_ctl);
  95. else
  96. writew(ctl | STOP, &twi->master_ctl);
  97. }
  98. }
  99. if (int_stat & RCVSERV) {
  100. writew(RCVSERV, &twi->int_stat);
  101. if (msg->len) {
  102. *(msg->buf++) = readw(&twi->rcv_data8);
  103. --msg->len;
  104. } else if (msg->flags & I2C_M_STOP) {
  105. ctl = readw(&twi->master_ctl);
  106. writew(ctl | STOP, &twi->master_ctl);
  107. }
  108. }
  109. if (int_stat & MERR) {
  110. writew(MERR, &twi->int_stat);
  111. return msg->len;
  112. }
  113. if (int_stat & MCOMP) {
  114. writew(MCOMP, &twi->int_stat);
  115. if (msg->flags & I2C_M_COMBO && msg->len) {
  116. ctl = readw(&twi->master_ctl);
  117. ctl = (ctl & ~RSTART) |
  118. (min(msg->len, 0xff) << 6) | MEN | MDIR;
  119. writew(ctl, &twi->master_ctl);
  120. } else
  121. break;
  122. }
  123. /* If we were able to do something, reset timeout */
  124. if (int_stat)
  125. timebase = get_timer(0);
  126. } while (get_timer(timebase) < I2C_TIMEOUT);
  127. return msg->len;
  128. }
  129. static int i2c_transfer(struct i2c_adapter *adap, uint8_t chip, uint addr,
  130. int alen, uint8_t *buffer, int len, uint8_t flags)
  131. {
  132. struct twi_regs *twi = i2c_get_base(adap);
  133. int ret;
  134. u16 ctl;
  135. uchar addr_buffer[] = {
  136. (addr >> 0),
  137. (addr >> 8),
  138. (addr >> 16),
  139. };
  140. struct adi_i2c_msg msg = {
  141. .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
  142. .buf = buffer,
  143. .len = len,
  144. .abuf = addr_buffer,
  145. .alen = alen,
  146. };
  147. /* wait for things to settle */
  148. while (readw(&twi->master_stat) & BUSBUSY)
  149. if (ctrlc())
  150. return 1;
  151. /* Set Transmit device address */
  152. writew(chip, &twi->master_addr);
  153. /* Clear the FIFO before starting things */
  154. writew(XMTFLUSH | RCVFLUSH, &twi->fifo_ctl);
  155. writew(0, &twi->fifo_ctl);
  156. /* prime the pump */
  157. if (msg.alen) {
  158. len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
  159. writew(*(msg.abuf++), &twi->xmt_data8);
  160. --msg.alen;
  161. } else if (!(msg.flags & I2C_M_READ) && msg.len) {
  162. writew(*(msg.buf++), &twi->xmt_data8);
  163. --msg.len;
  164. }
  165. /* clear int stat */
  166. writew(-1, &twi->master_stat);
  167. writew(-1, &twi->int_stat);
  168. writew(0, &twi->int_mask);
  169. /* Master enable */
  170. ctl = readw(&twi->master_ctl);
  171. ctl = (ctl & FAST) | (min(len, 0xff) << 6) | MEN |
  172. ((msg.flags & I2C_M_READ) ? MDIR : 0);
  173. writew(ctl, &twi->master_ctl);
  174. /* process the rest */
  175. ret = wait_for_completion(twi, &msg);
  176. if (ret) {
  177. ctl = readw(&twi->master_ctl) & ~MEN;
  178. writew(ctl, &twi->master_ctl);
  179. ctl = readw(&twi->control) & ~TWI_ENA;
  180. writew(ctl, &twi->control);
  181. ctl = readw(&twi->control) | TWI_ENA;
  182. writew(ctl, &twi->control);
  183. }
  184. return ret;
  185. }
  186. static uint adi_i2c_setspeed(struct i2c_adapter *adap, uint speed)
  187. {
  188. struct twi_regs *twi = i2c_get_base(adap);
  189. u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
  190. /* Set TWI interface clock */
  191. if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
  192. return -1;
  193. clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
  194. writew(clkdiv, &twi->clkdiv);
  195. /* Don't turn it on */
  196. writew(speed > 100000 ? FAST : 0, &twi->master_ctl);
  197. return 0;
  198. }
  199. static void adi_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  200. {
  201. struct twi_regs *twi = i2c_get_base(adap);
  202. u16 prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
  203. /* Set TWI internal clock as 10MHz */
  204. writew(prescale, &twi->control);
  205. /* Set TWI interface clock as specified */
  206. i2c_set_bus_speed(speed);
  207. /* Enable it */
  208. writew(TWI_ENA | prescale, &twi->control);
  209. }
  210. static int adi_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  211. uint addr, int alen, uint8_t *buffer, int len)
  212. {
  213. return i2c_transfer(adap, chip, addr, alen, buffer,
  214. len, alen ? I2C_M_COMBO : I2C_M_READ);
  215. }
  216. static int adi_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  217. uint addr, int alen, uint8_t *buffer, int len)
  218. {
  219. return i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
  220. }
  221. static int adi_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  222. {
  223. u8 byte;
  224. return adi_i2c_read(adap, chip, 0, 0, &byte, 1);
  225. }
  226. static struct twi_regs *i2c_get_base(struct i2c_adapter *adap)
  227. {
  228. switch (adap->hwadapnr) {
  229. #if CONFIG_SYS_MAX_I2C_BUS > 2
  230. case 2:
  231. return (struct twi_regs *)TWI2_CLKDIV;
  232. #endif
  233. #if CONFIG_SYS_MAX_I2C_BUS > 1
  234. case 1:
  235. return (struct twi_regs *)TWI1_CLKDIV;
  236. #endif
  237. case 0:
  238. return (struct twi_regs *)TWI0_CLKDIV;
  239. default:
  240. printf("wrong hwadapnr: %d\n", adap->hwadapnr);
  241. }
  242. return NULL;
  243. }
  244. U_BOOT_I2C_ADAP_COMPLETE(adi_i2c0, adi_i2c_init, adi_i2c_probe,
  245. adi_i2c_read, adi_i2c_write,
  246. adi_i2c_setspeed,
  247. CONFIG_SYS_I2C_SPEED,
  248. 0,
  249. 0)
  250. #if CONFIG_SYS_MAX_I2C_BUS > 1
  251. U_BOOT_I2C_ADAP_COMPLETE(adi_i2c1, adi_i2c_init, adi_i2c_probe,
  252. adi_i2c_read, adi_i2c_write,
  253. adi_i2c_setspeed,
  254. CONFIG_SYS_I2C_SPEED,
  255. 0,
  256. 1)
  257. #endif
  258. #if CONFIG_SYS_MAX_I2C_BUS > 2
  259. U_BOOT_I2C_ADAP_COMPLETE(adi_i2c2, adi_i2c_init, adi_i2c_probe,
  260. adi_i2c_read, adi_i2c_write,
  261. adi_i2c_setspeed,
  262. CONFIG_SYS_I2C_SPEED,
  263. 0,
  264. 2)
  265. #endif