]>
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.
8 * NOTE: This driver should be converted to driver model before June 2017.
9 * Please see doc/driver-model/i2c-howto.txt for instructions.
16 #include <asm/clock.h>
20 static struct twi_regs
*i2c_get_base(struct i2c_adapter
*adap
);
22 /* Every register is 32bit aligned, but only 16bits in size */
23 #define ureg(name) u16 name; u16 __pad_##name;
46 #define TWI0_CLKDIV TWI_CLKDIV
47 # ifdef CONFIG_SYS_MAX_I2C_BUS
48 # undef CONFIG_SYS_MAX_I2C_BUS
50 #define CONFIG_SYS_MAX_I2C_BUS 1
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.
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 I2C hardware can only operate 20KHz - 400KHz"
69 /* All transfers are described by this data structure */
72 #define I2C_M_COMBO 0x4
73 #define I2C_M_STOP 0x2
74 #define I2C_M_READ 0x1
75 int len
; /* msg length */
76 u8
*buf
; /* pointer to msg data */
77 int alen
; /* addr length */
78 u8
*abuf
; /* addr buffer */
81 /* Allow msec timeout per ~byte transfer */
82 #define I2C_TIMEOUT 10
85 * wait_for_completion - manage the actual i2c transfer
88 static int wait_for_completion(struct twi_regs
*twi
, struct adi_i2c_msg
*msg
)
91 ulong timebase
= get_timer(0);
94 int_stat
= readw(&twi
->int_stat
);
96 if (int_stat
& XMTSERV
) {
97 writew(XMTSERV
, &twi
->int_stat
);
99 writew(*(msg
->abuf
++), &twi
->xmt_data8
);
101 } else if (!(msg
->flags
& I2C_M_COMBO
) && msg
->len
) {
102 writew(*(msg
->buf
++), &twi
->xmt_data8
);
105 ctl
= readw(&twi
->master_ctl
);
106 if (msg
->flags
& I2C_M_COMBO
)
107 writew(ctl
| RSTART
| MDIR
,
110 writew(ctl
| STOP
, &twi
->master_ctl
);
113 if (int_stat
& RCVSERV
) {
114 writew(RCVSERV
, &twi
->int_stat
);
116 *(msg
->buf
++) = readw(&twi
->rcv_data8
);
118 } else if (msg
->flags
& I2C_M_STOP
) {
119 ctl
= readw(&twi
->master_ctl
);
120 writew(ctl
| STOP
, &twi
->master_ctl
);
123 if (int_stat
& MERR
) {
124 writew(MERR
, &twi
->int_stat
);
127 if (int_stat
& MCOMP
) {
128 writew(MCOMP
, &twi
->int_stat
);
129 if (msg
->flags
& I2C_M_COMBO
&& msg
->len
) {
130 ctl
= readw(&twi
->master_ctl
);
131 ctl
= (ctl
& ~RSTART
) |
132 (min(msg
->len
, 0xff) << 6) | MEN
| MDIR
;
133 writew(ctl
, &twi
->master_ctl
);
138 /* If we were able to do something, reset timeout */
140 timebase
= get_timer(0);
142 } while (get_timer(timebase
) < I2C_TIMEOUT
);
147 static int i2c_transfer(struct i2c_adapter
*adap
, uint8_t chip
, uint addr
,
148 int alen
, uint8_t *buffer
, int len
, uint8_t flags
)
150 struct twi_regs
*twi
= i2c_get_base(adap
);
153 uchar addr_buffer
[] = {
158 struct adi_i2c_msg msg
= {
159 .flags
= flags
| (len
>= 0xff ? I2C_M_STOP
: 0),
166 /* wait for things to settle */
167 while (readw(&twi
->master_stat
) & BUSBUSY
)
171 /* Set Transmit device address */
172 writew(chip
, &twi
->master_addr
);
174 /* Clear the FIFO before starting things */
175 writew(XMTFLUSH
| RCVFLUSH
, &twi
->fifo_ctl
);
176 writew(0, &twi
->fifo_ctl
);
180 len
= (msg
.flags
& I2C_M_COMBO
) ? msg
.alen
: msg
.alen
+ len
;
181 writew(*(msg
.abuf
++), &twi
->xmt_data8
);
183 } else if (!(msg
.flags
& I2C_M_READ
) && msg
.len
) {
184 writew(*(msg
.buf
++), &twi
->xmt_data8
);
189 writew(-1, &twi
->master_stat
);
190 writew(-1, &twi
->int_stat
);
191 writew(0, &twi
->int_mask
);
194 ctl
= readw(&twi
->master_ctl
);
195 ctl
= (ctl
& FAST
) | (min(len
, 0xff) << 6) | MEN
|
196 ((msg
.flags
& I2C_M_READ
) ? MDIR
: 0);
197 writew(ctl
, &twi
->master_ctl
);
199 /* process the rest */
200 ret
= wait_for_completion(twi
, &msg
);
203 ctl
= readw(&twi
->master_ctl
) & ~MEN
;
204 writew(ctl
, &twi
->master_ctl
);
205 ctl
= readw(&twi
->control
) & ~TWI_ENA
;
206 writew(ctl
, &twi
->control
);
207 ctl
= readw(&twi
->control
) | TWI_ENA
;
208 writew(ctl
, &twi
->control
);
214 static uint
adi_i2c_setspeed(struct i2c_adapter
*adap
, uint speed
)
216 struct twi_regs
*twi
= i2c_get_base(adap
);
217 u16 clkdiv
= I2C_SPEED_TO_DUTY(speed
);
219 /* Set TWI interface clock */
220 if (clkdiv
< I2C_DUTY_MAX
|| clkdiv
> I2C_DUTY_MIN
)
222 clkdiv
= (clkdiv
<< 8) | (clkdiv
& 0xff);
223 writew(clkdiv
, &twi
->clkdiv
);
225 /* Don't turn it on */
226 writew(speed
> 100000 ? FAST
: 0, &twi
->master_ctl
);
231 static void adi_i2c_init(struct i2c_adapter
*adap
, int speed
, int slaveaddr
)
233 struct twi_regs
*twi
= i2c_get_base(adap
);
234 u16 prescale
= ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
236 /* Set TWI internal clock as 10MHz */
237 writew(prescale
, &twi
->control
);
239 /* Set TWI interface clock as specified */
240 i2c_set_bus_speed(speed
);
243 writew(TWI_ENA
| prescale
, &twi
->control
);
246 static int adi_i2c_read(struct i2c_adapter
*adap
, uint8_t chip
,
247 uint addr
, int alen
, uint8_t *buffer
, int len
)
249 return i2c_transfer(adap
, chip
, addr
, alen
, buffer
,
250 len
, alen
? I2C_M_COMBO
: I2C_M_READ
);
253 static int adi_i2c_write(struct i2c_adapter
*adap
, uint8_t chip
,
254 uint addr
, int alen
, uint8_t *buffer
, int len
)
256 return i2c_transfer(adap
, chip
, addr
, alen
, buffer
, len
, 0);
259 static int adi_i2c_probe(struct i2c_adapter
*adap
, uint8_t chip
)
262 return adi_i2c_read(adap
, chip
, 0, 0, &byte
, 1);
265 static struct twi_regs
*i2c_get_base(struct i2c_adapter
*adap
)
267 switch (adap
->hwadapnr
) {
268 #if CONFIG_SYS_MAX_I2C_BUS > 2
270 return (struct twi_regs
*)TWI2_CLKDIV
;
272 #if CONFIG_SYS_MAX_I2C_BUS > 1
274 return (struct twi_regs
*)TWI1_CLKDIV
;
277 return (struct twi_regs
*)TWI0_CLKDIV
;
280 printf("wrong hwadapnr: %d\n", adap
->hwadapnr
);
286 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c0
, adi_i2c_init
, adi_i2c_probe
,
287 adi_i2c_read
, adi_i2c_write
,
289 CONFIG_SYS_I2C_SPEED
,
293 #if CONFIG_SYS_MAX_I2C_BUS > 1
294 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c1
, adi_i2c_init
, adi_i2c_probe
,
295 adi_i2c_read
, adi_i2c_write
,
297 CONFIG_SYS_I2C_SPEED
,
302 #if CONFIG_SYS_MAX_I2C_BUS > 2
303 U_BOOT_I2C_ADAP_COMPLETE(adi_i2c2
, adi_i2c_init
, adi_i2c_probe
,
304 adi_i2c_read
, adi_i2c_write
,
306 CONFIG_SYS_I2C_SPEED
,