adi_i2c.c 7.5 KB

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