]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/i2c/adi_i2c.c
2 * i2c.c - driver for ADI TWI/I2C
4 * Copyright (c) 2006-2014 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
12 #include <asm/clock.h>
16 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;
42 #define TWI0_CLKDIV TWI_CLKDIV
43 # ifdef CONFIG_SYS_MAX_I2C_BUS
44 # undef CONFIG_SYS_MAX_I2C_BUS
46 #define CONFIG_SYS_MAX_I2C_BUS 1
50 * The way speed is changed into duty often results in integer truncation
51 * with 50% duty, so we'll force rounding up to the next duty by adding 1
52 * to the max. In practice this will get us a speed of something like
53 * 385 KHz. The other limit is easy to handle as it is only 8 bits.
55 #define I2C_SPEED_MAX 400000
56 #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
57 #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
58 #define I2C_DUTY_MIN 0xff /* 8 bit limited */
59 #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
60 /* Note: duty is inverse of speed, so the comparisons below are correct */
61 #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
62 # error "The I2C hardware can only operate 20KHz - 400KHz"
65 /* All transfers are described by this data structure */
68 #define I2C_M_COMBO 0x4
69 #define I2C_M_STOP 0x2
70 #define I2C_M_READ 0x1
71 int len
; /* msg length */
72 u8
*buf
; /* pointer to msg data */
73 int alen
; /* addr length */
74 u8
*abuf
; /* addr buffer */
77 /* Allow msec timeout per ~byte transfer */
78 #define I2C_TIMEOUT 10
81 * wait_for_completion - manage the actual i2c transfer
84 static int wait_for_completion(struct twi_regs
*twi
, struct i2c_msg
*msg
)
87 ulong timebase
= get_timer(0);
90 int_stat
= readw(&twi
->int_stat
);
92 if (int_stat
& XMTSERV
) {
93 writew(XMTSERV
, &twi
->int_stat
);
95 writew(*(msg
->abuf
++), &twi
->xmt_data8
);
97 } else if (!(msg
->flags
& I2C_M_COMBO
) && msg
->len
) {
98 writew(*(msg
->buf
++), &twi
->xmt_data8
);
101 ctl
= readw(&twi
->master_ctl
);
102 if (msg
->flags
& I2C_M_COMBO
)
103 writew(ctl
| RSTART
| MDIR
,
106 writew(ctl
| STOP
, &twi
->master_ctl
);
109 if (int_stat
& RCVSERV
) {
110 writew(RCVSERV
, &twi
->int_stat
);
112 *(msg
->buf
++) = readw(&twi
->rcv_data8
);
114 } else if (msg
->flags
& I2C_M_STOP
) {
115 ctl
= readw(&twi
->master_ctl
);
116 writew(ctl
| STOP
, &twi
->master_ctl
);
119 if (int_stat
& MERR
) {
120 writew(MERR
, &twi
->int_stat
);
123 if (int_stat
& MCOMP
) {
124 writew(MCOMP
, &twi
->int_stat
);
125 if (msg
->flags
& I2C_M_COMBO
&& msg
->len
) {
126 ctl
= readw(&twi
->master_ctl
);
127 ctl
= (ctl
& ~RSTART
) |
128 (min(msg
->len
, 0xff) << 6) | MEN
| MDIR
;
129 writew(ctl
, &twi
->master_ctl
);
134 /* If we were able to do something, reset timeout */
136 timebase
= get_timer(0);
138 } while (get_timer(timebase
) < I2C_TIMEOUT
);
143 static int i2c_transfer(struct i2c_adapter
*adap
, uint8_t chip
, uint addr
,
144 int alen
, uint8_t *buffer
, int len
, uint8_t flags
)
146 struct twi_regs
*twi
= i2c_get_base(adap
);
149 uchar addr_buffer
[] = {
154 struct i2c_msg msg
= {
155 .flags
= flags
| (len
>= 0xff ? I2C_M_STOP
: 0),
162 /* wait for things to settle */
163 while (readw(&twi
->master_stat
) & BUSBUSY
)
167 /* Set Transmit device address */
168 writew(chip
, &twi
->master_addr
);
170 /* Clear the FIFO before starting things */
171 writew(XMTFLUSH
| RCVFLUSH
, &twi
->fifo_ctl
);
172 writew(0, &twi
->fifo_ctl
);
176 len
= (msg
.flags
& I2C_M_COMBO
) ? msg
.alen
: msg
.alen
+ len
;
177 writew(*(msg
.abuf
++), &twi
->xmt_data8
);
179 } else if (!(msg
.flags
& I2C_M_READ
) && msg
.len
) {
180 writew(*(msg
.buf
++), &twi
->xmt_data8
);
185 writew(-1, &twi
->master_stat
);
186 writew(-1, &twi
->int_stat
);
187 writew(0, &twi
->int_mask
);
190 ctl
= readw(&twi
->master_ctl
);
191 ctl
= (ctl
& FAST
) | (min(len
, 0xff) << 6) | MEN
|
192 ((msg
.flags
& I2C_M_READ
) ? MDIR
: 0);
193 writew(ctl
, &twi
->master_ctl
);
195 /* process the rest */
196 ret
= wait_for_completion(twi
, &msg
);
199 ctl
= readw(&twi
->master_ctl
) & ~MEN
;
200 writew(ctl
, &twi
->master_ctl
);
201 ctl
= readw(&twi
->control
) & ~TWI_ENA
;
202 writew(ctl
, &twi
->control
);
203 ctl
= readw(&twi
->control
) | TWI_ENA
;
204 writew(ctl
, &twi
->control
);
210 static uint
adi_i2c_setspeed(struct i2c_adapter
*adap
, uint speed
)
212 struct twi_regs
*twi
= i2c_get_base(adap
);
213 u16 clkdiv
= I2C_SPEED_TO_DUTY(speed
);
215 /* Set TWI interface clock */
216 if (clkdiv
< I2C_DUTY_MAX
|| clkdiv
> I2C_DUTY_MIN
)
218 clkdiv
= (clkdiv
<< 8) | (clkdiv
& 0xff);
219 writew(clkdiv
, &twi
->clkdiv
);
221 /* Don't turn it on */
222 writew(speed
> 100000 ? FAST
: 0, &twi
->master_ctl
);
227 static void adi_i2c_init(struct i2c_adapter
*adap
, int speed
, int slaveaddr
)
229 struct twi_regs
*twi
= i2c_get_base(adap
);
230 u16 prescale
= ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
232 /* Set TWI internal clock as 10MHz */
233 writew(prescale
, &twi
->control
);
235 /* Set TWI interface clock as specified */
236 i2c_set_bus_speed(speed
);
239 writew(TWI_ENA
| prescale
, &twi
->control
);
242 static int adi_i2c_read(struct i2c_adapter
*adap
, uint8_t chip
,
243 uint addr
, int alen
, uint8_t *buffer
, int len
)
245 return i2c_transfer(adap
, chip
, addr
, alen
, buffer
,
246 len
, alen
? I2C_M_COMBO
: I2C_M_READ
);
249 static int adi_i2c_write(struct i2c_adapter
*adap
, uint8_t chip
,
250 uint addr
, int alen
, uint8_t *buffer
, int len
)
252 return i2c_transfer(adap
, chip
, addr
, alen
, buffer
, len
, 0);
255 static int adi_i2c_probe(struct i2c_adapter
*adap
, uint8_t chip
)
258 return adi_i2c_read(adap
, chip
, 0, 0, &byte
, 1);
261 static struct twi_regs
*i2c_get_base(struct i2c_adapter
*adap
)
263 switch (adap
->hwadapnr
) {
264 #if CONFIG_SYS_MAX_I2C_BUS > 2
266 return (struct twi_regs
*)TWI2_CLKDIV
;
268 #if CONFIG_SYS_MAX_I2C_BUS > 1
270 return (struct twi_regs
*)TWI1_CLKDIV
;
273 return (struct twi_regs
*)TWI0_CLKDIV
;
276 printf("wrong hwadapnr: %d\n", adap
->hwadapnr
);
282 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c0
, adi_i2c_init
, adi_i2c_probe
,
283 adi_i2c_read
, adi_i2c_write
,
285 CONFIG_SYS_I2C_SPEED
,
289 #if CONFIG_SYS_MAX_I2C_BUS > 1
290 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c1
, adi_i2c_init
, adi_i2c_probe
,
291 adi_i2c_read
, adi_i2c_write
,
293 CONFIG_SYS_I2C_SPEED
,
298 #if CONFIG_SYS_MAX_I2C_BUS > 2
299 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c2
, adi_i2c_init
, adi_i2c_probe
,
300 adi_i2c_read
, adi_i2c_write
,
302 CONFIG_SYS_I2C_SPEED
,