2 * i2c.c - driver for Blackfin on-chip TWI/I2C
4 * Copyright (c) 2006-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
12 #include <asm/blackfin.h>
13 #include <asm/mach-common/bits/twi.h>
15 /* Every register is 32bit aligned, but only 16bits in size */
16 #define ureg(name) u16 name; u16 __pad_##name;
38 /* U-Boot I2C framework allows only one active device at a time. */
40 #define TWI0_CLKDIV TWI_CLKDIV
42 static volatile struct twi_regs *twi = (void *)TWI0_CLKDIV;
45 # define dmemset(s, c, n) memset(s, c, n)
47 # define dmemset(s, c, n)
49 #define debugi(fmt, args...) \
51 "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t%-20s:%-3i: " fmt "\n", \
52 twi->master_stat, twi->fifo_stat, twi->int_stat, \
53 __func__, __LINE__, ## args)
55 #ifdef CONFIG_TWICLK_KHZ
56 # error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
60 * The way speed is changed into duty often results in integer truncation
61 * with 50% duty, so we'll force rounding up to the next duty by adding 1
62 * to the max. In practice this will get us a speed of something like
63 * 385 KHz. The other limit is easy to handle as it is only 8 bits.
65 #define I2C_SPEED_MAX 400000
66 #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
67 #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
68 #define I2C_DUTY_MIN 0xff /* 8 bit limited */
69 #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
70 /* Note: duty is inverse of speed, so the comparisons below are correct */
71 #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
72 # error "The Blackfin I2C hardware can only operate 20KHz - 400KHz"
75 /* All transfers are described by this data structure */
78 #define I2C_M_COMBO 0x4
79 #define I2C_M_STOP 0x2
80 #define I2C_M_READ 0x1
81 int len; /* msg length */
82 u8 *buf; /* pointer to msg data */
83 int alen; /* addr length */
84 u8 *abuf; /* addr buffer */
87 /* Allow msec timeout per ~byte transfer */
88 #define I2C_TIMEOUT 10
91 * wait_for_completion - manage the actual i2c transfer
94 static int wait_for_completion(struct i2c_msg *msg)
97 ulong timebase = get_timer(0);
100 int_stat = twi->int_stat;
102 if (int_stat & XMTSERV) {
103 debugi("processing XMTSERV");
104 twi->int_stat = XMTSERV;
107 twi->xmt_data8 = *(msg->abuf++);
109 } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
110 twi->xmt_data8 = *(msg->buf++);
113 twi->master_ctl |= (msg->flags & I2C_M_COMBO) ? RSTART | MDIR : STOP;
117 if (int_stat & RCVSERV) {
118 debugi("processing RCVSERV");
119 twi->int_stat = RCVSERV;
122 *(msg->buf++) = twi->rcv_data8;
124 } else if (msg->flags & I2C_M_STOP) {
125 twi->master_ctl |= STOP;
129 if (int_stat & MERR) {
130 debugi("processing MERR");
131 twi->int_stat = MERR;
135 if (int_stat & MCOMP) {
136 debugi("processing MCOMP");
137 twi->int_stat = MCOMP;
139 if (msg->flags & I2C_M_COMBO && msg->len) {
140 twi->master_ctl = (twi->master_ctl & ~RSTART) |
141 (min(msg->len, 0xff) << 6) | MEN | MDIR;
147 /* If we were able to do something, reset timeout */
149 timebase = get_timer(0);
151 } while (get_timer(timebase) < I2C_TIMEOUT);
157 * i2c_transfer - setup an i2c transfer
158 * @return: 0 if things worked, non-0 if things failed
160 * Here we just get the i2c stuff all prepped and ready, and then tail off
161 * into wait_for_completion() for all the bits to go.
163 static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
165 uchar addr_buffer[] = {
170 struct i2c_msg msg = {
171 .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
179 dmemset(buffer, 0xff, len);
180 debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
181 chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
183 /* wait for things to settle */
184 while (twi->master_stat & BUSBUSY)
188 /* Set Transmit device address */
189 twi->master_addr = chip;
191 /* Clear the FIFO before starting things */
192 twi->fifo_ctl = XMTFLUSH | RCVFLUSH;
199 len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
200 debugi("first byte=0x%02x", *msg.abuf);
201 twi->xmt_data8 = *(msg.abuf++);
203 } else if (!(msg.flags & I2C_M_READ) && msg.len) {
204 debugi("first byte=0x%02x", *msg.buf);
205 twi->xmt_data8 = *(msg.buf++);
210 twi->master_stat = -1;
217 (twi->master_ctl & FAST) |
218 (min(len, 0xff) << 6) | MEN |
219 ((msg.flags & I2C_M_READ) ? MDIR : 0);
221 debugi("CTL=0x%04x", twi->master_ctl);
223 /* process the rest */
224 ret = wait_for_completion(&msg);
225 debugi("ret=%d", ret);
228 twi->master_ctl &= ~MEN;
229 twi->control &= ~TWI_ENA;
231 twi->control |= TWI_ENA;
239 * i2c_set_bus_speed - set i2c bus speed
240 * @speed: bus speed (in HZ)
242 int i2c_set_bus_speed(unsigned int speed)
244 u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
246 /* Set TWI interface clock */
247 if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
249 twi->clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
251 /* Don't turn it on */
252 twi->master_ctl = (speed > 100000 ? FAST : 0);
258 * i2c_get_bus_speed - get i2c bus speed
259 * @speed: bus speed (in HZ)
261 unsigned int i2c_get_bus_speed(void)
263 /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */
264 return 5000000 / (twi->clkdiv & 0xff);
268 * i2c_init - initialize the i2c bus
269 * @speed: bus speed (in HZ)
270 * @slaveaddr: address of device in slave mode (0 - not slave)
272 * Slave mode isn't actually implemented. It'll stay that way until
273 * we get a real request for it.
275 void i2c_init(int speed, int slaveaddr)
277 uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
279 /* Set TWI internal clock as 10MHz */
280 twi->control = prescale;
282 /* Set TWI interface clock as specified */
283 i2c_set_bus_speed(speed);
286 twi->control = TWI_ENA | prescale;
289 debugi("CONTROL:0x%04x CLKDIV:0x%04x", twi->control, twi->clkdiv);
291 #if CONFIG_SYS_I2C_SLAVE
292 # error I2C slave support not tested/supported
293 /* If they want us as a slave, do it */
295 twi->slave_addr = slaveaddr;
296 twi->slave_ctl = SEN;
302 * i2c_probe - test if a chip exists at a given i2c address
303 * @chip: i2c chip addr to search for
304 * @return: 0 if found, non-0 if not found
306 int i2c_probe(uchar chip)
309 return i2c_read(chip, 0, 0, &byte, 1);
313 * i2c_read - read data from an i2c device
314 * @chip: i2c chip addr
315 * @addr: memory (register) address in the chip
316 * @alen: byte size of address
317 * @buffer: buffer to store data read from chip
318 * @len: how many bytes to read
319 * @return: 0 on success, non-0 on failure
321 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
323 return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
327 * i2c_write - write data to an i2c device
328 * @chip: i2c chip addr
329 * @addr: memory (register) address in the chip
330 * @alen: byte size of address
331 * @buffer: buffer holding data to write to chip
332 * @len: how many bytes to write
333 * @return: 0 on success, non-0 on failure
335 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
337 return i2c_transfer(chip, addr, alen, buffer, len, 0);
341 * i2c_set_bus_num - change active I2C bus
342 * @bus: bus index, zero based
343 * @returns: 0 on success, non-0 on failure
345 int i2c_set_bus_num(unsigned int bus)
348 #if CONFIG_SYS_MAX_I2C_BUS > 0
349 case 0: twi = (void *)TWI0_CLKDIV; return 0;
351 #if CONFIG_SYS_MAX_I2C_BUS > 1
352 case 1: twi = (void *)TWI1_CLKDIV; return 0;
354 #if CONFIG_SYS_MAX_I2C_BUS > 2
355 case 2: twi = (void *)TWI2_CLKDIV; return 0;
362 * i2c_get_bus_num - returns index of active I2C bus
364 unsigned int i2c_get_bus_num(void)
366 switch ((unsigned long)twi) {
367 #if CONFIG_SYS_MAX_I2C_BUS > 0
368 case TWI0_CLKDIV: return 0;
370 #if CONFIG_SYS_MAX_I2C_BUS > 1
371 case TWI1_CLKDIV: return 1;
373 #if CONFIG_SYS_MAX_I2C_BUS > 2
374 case TWI2_CLKDIV: return 2;