2 * i2c.c - driver for ADI TWI/I2C
4 * Copyright (c) 2006-2014 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
13 #include <asm/clock.h>
17 static struct twi_regs *i2c_get_base(struct i2c_adapter *adap);
19 /* Every register is 32bit aligned, but only 16bits in size */
20 #define ureg(name) u16 name; u16 __pad_##name;
43 #define TWI0_CLKDIV TWI_CLKDIV
44 # ifdef CONFIG_SYS_MAX_I2C_BUS
45 # undef CONFIG_SYS_MAX_I2C_BUS
47 #define CONFIG_SYS_MAX_I2C_BUS 1
51 * The way speed is changed into duty often results in integer truncation
52 * with 50% duty, so we'll force rounding up to the next duty by adding 1
53 * to the max. In practice this will get us a speed of something like
54 * 385 KHz. The other limit is easy to handle as it is only 8 bits.
56 #define I2C_SPEED_MAX 400000
57 #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
58 #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
59 #define I2C_DUTY_MIN 0xff /* 8 bit limited */
60 #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
61 /* Note: duty is inverse of speed, so the comparisons below are correct */
62 #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
63 # error "The I2C hardware can only operate 20KHz - 400KHz"
66 /* All transfers are described by this data structure */
69 #define I2C_M_COMBO 0x4
70 #define I2C_M_STOP 0x2
71 #define I2C_M_READ 0x1
72 int len; /* msg length */
73 u8 *buf; /* pointer to msg data */
74 int alen; /* addr length */
75 u8 *abuf; /* addr buffer */
78 /* Allow msec timeout per ~byte transfer */
79 #define I2C_TIMEOUT 10
82 * wait_for_completion - manage the actual i2c transfer
85 static int wait_for_completion(struct twi_regs *twi, struct adi_i2c_msg *msg)
88 ulong timebase = get_timer(0);
91 int_stat = readw(&twi->int_stat);
93 if (int_stat & XMTSERV) {
94 writew(XMTSERV, &twi->int_stat);
96 writew(*(msg->abuf++), &twi->xmt_data8);
98 } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
99 writew(*(msg->buf++), &twi->xmt_data8);
102 ctl = readw(&twi->master_ctl);
103 if (msg->flags & I2C_M_COMBO)
104 writew(ctl | RSTART | MDIR,
107 writew(ctl | STOP, &twi->master_ctl);
110 if (int_stat & RCVSERV) {
111 writew(RCVSERV, &twi->int_stat);
113 *(msg->buf++) = readw(&twi->rcv_data8);
115 } else if (msg->flags & I2C_M_STOP) {
116 ctl = readw(&twi->master_ctl);
117 writew(ctl | STOP, &twi->master_ctl);
120 if (int_stat & MERR) {
121 writew(MERR, &twi->int_stat);
124 if (int_stat & MCOMP) {
125 writew(MCOMP, &twi->int_stat);
126 if (msg->flags & I2C_M_COMBO && msg->len) {
127 ctl = readw(&twi->master_ctl);
128 ctl = (ctl & ~RSTART) |
129 (min(msg->len, 0xff) << 6) | MEN | MDIR;
130 writew(ctl, &twi->master_ctl);
135 /* If we were able to do something, reset timeout */
137 timebase = get_timer(0);
139 } while (get_timer(timebase) < I2C_TIMEOUT);
144 static int i2c_transfer(struct i2c_adapter *adap, uint8_t chip, uint addr,
145 int alen, uint8_t *buffer, int len, uint8_t flags)
147 struct twi_regs *twi = i2c_get_base(adap);
150 uchar addr_buffer[] = {
155 struct adi_i2c_msg msg = {
156 .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
163 /* wait for things to settle */
164 while (readw(&twi->master_stat) & BUSBUSY)
168 /* Set Transmit device address */
169 writew(chip, &twi->master_addr);
171 /* Clear the FIFO before starting things */
172 writew(XMTFLUSH | RCVFLUSH, &twi->fifo_ctl);
173 writew(0, &twi->fifo_ctl);
177 len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
178 writew(*(msg.abuf++), &twi->xmt_data8);
180 } else if (!(msg.flags & I2C_M_READ) && msg.len) {
181 writew(*(msg.buf++), &twi->xmt_data8);
186 writew(-1, &twi->master_stat);
187 writew(-1, &twi->int_stat);
188 writew(0, &twi->int_mask);
191 ctl = readw(&twi->master_ctl);
192 ctl = (ctl & FAST) | (min(len, 0xff) << 6) | MEN |
193 ((msg.flags & I2C_M_READ) ? MDIR : 0);
194 writew(ctl, &twi->master_ctl);
196 /* process the rest */
197 ret = wait_for_completion(twi, &msg);
200 ctl = readw(&twi->master_ctl) & ~MEN;
201 writew(ctl, &twi->master_ctl);
202 ctl = readw(&twi->control) & ~TWI_ENA;
203 writew(ctl, &twi->control);
204 ctl = readw(&twi->control) | TWI_ENA;
205 writew(ctl, &twi->control);
211 static uint adi_i2c_setspeed(struct i2c_adapter *adap, uint speed)
213 struct twi_regs *twi = i2c_get_base(adap);
214 u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
216 /* Set TWI interface clock */
217 if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
219 clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
220 writew(clkdiv, &twi->clkdiv);
222 /* Don't turn it on */
223 writew(speed > 100000 ? FAST : 0, &twi->master_ctl);
228 static void adi_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
230 struct twi_regs *twi = i2c_get_base(adap);
231 u16 prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
233 /* Set TWI internal clock as 10MHz */
234 writew(prescale, &twi->control);
236 /* Set TWI interface clock as specified */
237 i2c_set_bus_speed(speed);
240 writew(TWI_ENA | prescale, &twi->control);
243 static int adi_i2c_read(struct i2c_adapter *adap, uint8_t chip,
244 uint addr, int alen, uint8_t *buffer, int len)
246 return i2c_transfer(adap, chip, addr, alen, buffer,
247 len, alen ? I2C_M_COMBO : I2C_M_READ);
250 static int adi_i2c_write(struct i2c_adapter *adap, uint8_t chip,
251 uint addr, int alen, uint8_t *buffer, int len)
253 return i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
256 static int adi_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
259 return adi_i2c_read(adap, chip, 0, 0, &byte, 1);
262 static struct twi_regs *i2c_get_base(struct i2c_adapter *adap)
264 switch (adap->hwadapnr) {
265 #if CONFIG_SYS_MAX_I2C_BUS > 2
267 return (struct twi_regs *)TWI2_CLKDIV;
269 #if CONFIG_SYS_MAX_I2C_BUS > 1
271 return (struct twi_regs *)TWI1_CLKDIV;
274 return (struct twi_regs *)TWI0_CLKDIV;
277 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
283 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c0, adi_i2c_init, adi_i2c_probe,
284 adi_i2c_read, adi_i2c_write,
286 CONFIG_SYS_I2C_SPEED,
290 #if CONFIG_SYS_MAX_I2C_BUS > 1
291 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c1, adi_i2c_init, adi_i2c_probe,
292 adi_i2c_read, adi_i2c_write,
294 CONFIG_SYS_I2C_SPEED,
299 #if CONFIG_SYS_MAX_I2C_BUS > 2
300 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c2, adi_i2c_init, adi_i2c_probe,
301 adi_i2c_read, adi_i2c_write,
303 CONFIG_SYS_I2C_SPEED,