2008-10-06 08:16:47 +00:00
|
|
|
/*
|
2014-11-13 07:30:53 +00:00
|
|
|
* i2c.c - driver for ADI TWI/I2C
|
2008-10-06 08:16:47 +00:00
|
|
|
*
|
2014-11-13 07:30:53 +00:00
|
|
|
* Copyright (c) 2006-2014 Analog Devices Inc.
|
2008-10-06 08:16:47 +00:00
|
|
|
*
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
|
2014-01-28 05:53:34 +00:00
|
|
|
#include <asm/clock.h>
|
2014-11-13 07:30:53 +00:00
|
|
|
#include <asm/twi.h>
|
2014-11-13 07:30:54 +00:00
|
|
|
#include <asm/io.h>
|
2008-10-06 08:16:47 +00:00
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static struct twi_regs *i2c_get_base(struct i2c_adapter *adap);
|
|
|
|
|
2010-05-05 07:20:30 +00:00
|
|
|
/* Every register is 32bit aligned, but only 16bits in size */
|
|
|
|
#define ureg(name) u16 name; u16 __pad_##name;
|
|
|
|
struct twi_regs {
|
|
|
|
ureg(clkdiv);
|
|
|
|
ureg(control);
|
|
|
|
ureg(slave_ctl);
|
|
|
|
ureg(slave_stat);
|
|
|
|
ureg(slave_addr);
|
|
|
|
ureg(master_ctl);
|
|
|
|
ureg(master_stat);
|
|
|
|
ureg(master_addr);
|
|
|
|
ureg(int_stat);
|
|
|
|
ureg(int_mask);
|
|
|
|
ureg(fifo_ctl);
|
|
|
|
ureg(fifo_stat);
|
|
|
|
char __pad[0x50];
|
|
|
|
ureg(xmt_data8);
|
|
|
|
ureg(xmt_data16);
|
|
|
|
ureg(rcv_data8);
|
|
|
|
ureg(rcv_data16);
|
|
|
|
};
|
|
|
|
#undef ureg
|
|
|
|
|
|
|
|
#ifdef TWI_CLKDIV
|
|
|
|
#define TWI0_CLKDIV TWI_CLKDIV
|
2014-11-13 07:30:55 +00:00
|
|
|
# ifdef CONFIG_SYS_MAX_I2C_BUS
|
|
|
|
# undef CONFIG_SYS_MAX_I2C_BUS
|
|
|
|
# endif
|
|
|
|
#define CONFIG_SYS_MAX_I2C_BUS 1
|
2008-10-06 08:16:47 +00:00
|
|
|
#endif
|
2009-10-14 23:27:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The way speed is changed into duty often results in integer truncation
|
|
|
|
* with 50% duty, so we'll force rounding up to the next duty by adding 1
|
|
|
|
* to the max. In practice this will get us a speed of something like
|
|
|
|
* 385 KHz. The other limit is easy to handle as it is only 8 bits.
|
|
|
|
*/
|
|
|
|
#define I2C_SPEED_MAX 400000
|
|
|
|
#define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
|
|
|
|
#define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
|
|
|
|
#define I2C_DUTY_MIN 0xff /* 8 bit limited */
|
|
|
|
#define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
|
|
|
|
/* Note: duty is inverse of speed, so the comparisons below are correct */
|
|
|
|
#if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
|
2014-11-13 07:30:55 +00:00
|
|
|
# error "The I2C hardware can only operate 20KHz - 400KHz"
|
2008-10-06 08:16:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* All transfers are described by this data structure */
|
|
|
|
struct i2c_msg {
|
|
|
|
u8 flags;
|
|
|
|
#define I2C_M_COMBO 0x4
|
|
|
|
#define I2C_M_STOP 0x2
|
|
|
|
#define I2C_M_READ 0x1
|
|
|
|
int len; /* msg length */
|
|
|
|
u8 *buf; /* pointer to msg data */
|
|
|
|
int alen; /* addr length */
|
|
|
|
u8 *abuf; /* addr buffer */
|
|
|
|
};
|
|
|
|
|
2009-10-14 23:27:26 +00:00
|
|
|
/* Allow msec timeout per ~byte transfer */
|
|
|
|
#define I2C_TIMEOUT 10
|
|
|
|
|
2008-10-06 08:16:47 +00:00
|
|
|
/**
|
|
|
|
* wait_for_completion - manage the actual i2c transfer
|
|
|
|
* @msg: the i2c msg
|
|
|
|
*/
|
2014-11-13 07:30:55 +00:00
|
|
|
static int wait_for_completion(struct twi_regs *twi, struct i2c_msg *msg)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:54 +00:00
|
|
|
u16 int_stat, ctl;
|
2009-10-14 23:27:26 +00:00
|
|
|
ulong timebase = get_timer(0);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
2009-10-14 23:27:26 +00:00
|
|
|
do {
|
2014-11-13 07:30:54 +00:00
|
|
|
int_stat = readw(&twi->int_stat);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
if (int_stat & XMTSERV) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(XMTSERV, &twi->int_stat);
|
2008-10-06 08:16:47 +00:00
|
|
|
if (msg->alen) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(*(msg->abuf++), &twi->xmt_data8);
|
2008-10-06 08:16:47 +00:00
|
|
|
--msg->alen;
|
|
|
|
} else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(*(msg->buf++), &twi->xmt_data8);
|
2008-10-06 08:16:47 +00:00
|
|
|
--msg->len;
|
|
|
|
} else {
|
2014-11-13 07:30:54 +00:00
|
|
|
ctl = readw(&twi->master_ctl);
|
|
|
|
if (msg->flags & I2C_M_COMBO)
|
|
|
|
writew(ctl | RSTART | MDIR,
|
|
|
|
&twi->master_ctl);
|
|
|
|
else
|
|
|
|
writew(ctl | STOP, &twi->master_ctl);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (int_stat & RCVSERV) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(RCVSERV, &twi->int_stat);
|
2008-10-06 08:16:47 +00:00
|
|
|
if (msg->len) {
|
2014-11-13 07:30:54 +00:00
|
|
|
*(msg->buf++) = readw(&twi->rcv_data8);
|
2008-10-06 08:16:47 +00:00
|
|
|
--msg->len;
|
|
|
|
} else if (msg->flags & I2C_M_STOP) {
|
2014-11-13 07:30:54 +00:00
|
|
|
ctl = readw(&twi->master_ctl);
|
|
|
|
writew(ctl | STOP, &twi->master_ctl);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (int_stat & MERR) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(MERR, &twi->int_stat);
|
2009-10-14 23:27:26 +00:00
|
|
|
return msg->len;
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
if (int_stat & MCOMP) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(MCOMP, &twi->int_stat);
|
2008-10-06 08:16:47 +00:00
|
|
|
if (msg->flags & I2C_M_COMBO && msg->len) {
|
2014-11-13 07:30:54 +00:00
|
|
|
ctl = readw(&twi->master_ctl);
|
|
|
|
ctl = (ctl & ~RSTART) |
|
2010-05-05 07:20:30 +00:00
|
|
|
(min(msg->len, 0xff) << 6) | MEN | MDIR;
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(ctl, &twi->master_ctl);
|
2008-10-06 08:16:47 +00:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2009-10-14 23:27:26 +00:00
|
|
|
|
|
|
|
/* If we were able to do something, reset timeout */
|
|
|
|
if (int_stat)
|
|
|
|
timebase = get_timer(0);
|
|
|
|
|
|
|
|
} while (get_timer(timebase) < I2C_TIMEOUT);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
return msg->len;
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static int i2c_transfer(struct i2c_adapter *adap, uint8_t chip, uint addr,
|
|
|
|
int alen, uint8_t *buffer, int len, uint8_t flags)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
struct twi_regs *twi = i2c_get_base(adap);
|
2014-11-13 07:30:54 +00:00
|
|
|
int ret;
|
|
|
|
u16 ctl;
|
2008-10-06 08:16:47 +00:00
|
|
|
uchar addr_buffer[] = {
|
|
|
|
(addr >> 0),
|
|
|
|
(addr >> 8),
|
|
|
|
(addr >> 16),
|
|
|
|
};
|
|
|
|
struct i2c_msg msg = {
|
|
|
|
.flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
|
|
|
|
.buf = buffer,
|
|
|
|
.len = len,
|
|
|
|
.abuf = addr_buffer,
|
|
|
|
.alen = alen,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* wait for things to settle */
|
2014-11-13 07:30:54 +00:00
|
|
|
while (readw(&twi->master_stat) & BUSBUSY)
|
2008-10-06 08:16:47 +00:00
|
|
|
if (ctrlc())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Set Transmit device address */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(chip, &twi->master_addr);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* Clear the FIFO before starting things */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(XMTFLUSH | RCVFLUSH, &twi->fifo_ctl);
|
|
|
|
writew(0, &twi->fifo_ctl);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* prime the pump */
|
|
|
|
if (msg.alen) {
|
2009-06-29 19:48:33 +00:00
|
|
|
len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(*(msg.abuf++), &twi->xmt_data8);
|
2008-10-06 08:16:47 +00:00
|
|
|
--msg.alen;
|
|
|
|
} else if (!(msg.flags & I2C_M_READ) && msg.len) {
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(*(msg.buf++), &twi->xmt_data8);
|
2008-10-06 08:16:47 +00:00
|
|
|
--msg.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear int stat */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(-1, &twi->master_stat);
|
|
|
|
writew(-1, &twi->int_stat);
|
|
|
|
writew(0, &twi->int_mask);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* Master enable */
|
2014-11-13 07:30:54 +00:00
|
|
|
ctl = readw(&twi->master_ctl);
|
|
|
|
ctl = (ctl & FAST) | (min(len, 0xff) << 6) | MEN |
|
|
|
|
((msg.flags & I2C_M_READ) ? MDIR : 0);
|
|
|
|
writew(ctl, &twi->master_ctl);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* process the rest */
|
2014-11-13 07:30:55 +00:00
|
|
|
ret = wait_for_completion(twi, &msg);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
if (ret) {
|
2014-11-13 07:30:54 +00:00
|
|
|
ctl = readw(&twi->master_ctl) & ~MEN;
|
|
|
|
writew(ctl, &twi->master_ctl);
|
|
|
|
ctl = readw(&twi->control) & ~TWI_ENA;
|
|
|
|
writew(ctl, &twi->control);
|
|
|
|
ctl = readw(&twi->control) | TWI_ENA;
|
|
|
|
writew(ctl, &twi->control);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static uint adi_i2c_setspeed(struct i2c_adapter *adap, uint speed)
|
2009-10-14 23:27:27 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
struct twi_regs *twi = i2c_get_base(adap);
|
2009-10-14 23:27:27 +00:00
|
|
|
u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
|
|
|
|
|
|
|
|
/* Set TWI interface clock */
|
|
|
|
if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
|
|
|
|
return -1;
|
2014-11-13 07:30:54 +00:00
|
|
|
clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
|
|
|
|
writew(clkdiv, &twi->clkdiv);
|
2009-10-14 23:27:27 +00:00
|
|
|
|
|
|
|
/* Don't turn it on */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(speed > 100000 ? FAST : 0, &twi->master_ctl);
|
2009-10-14 23:27:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static void adi_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
struct twi_regs *twi = i2c_get_base(adap);
|
|
|
|
u16 prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F;
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* Set TWI internal clock as 10MHz */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(prescale, &twi->control);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
|
|
|
/* Set TWI interface clock as specified */
|
2009-10-14 23:27:27 +00:00
|
|
|
i2c_set_bus_speed(speed);
|
2008-10-06 08:16:47 +00:00
|
|
|
|
2009-10-14 23:27:27 +00:00
|
|
|
/* Enable it */
|
2014-11-13 07:30:54 +00:00
|
|
|
writew(TWI_ENA | prescale, &twi->control);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static int adi_i2c_read(struct i2c_adapter *adap, uint8_t chip,
|
|
|
|
uint addr, int alen, uint8_t *buffer, int len)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
return i2c_transfer(adap, chip, addr, alen, buffer,
|
|
|
|
len, alen ? I2C_M_COMBO : I2C_M_READ);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static int adi_i2c_write(struct i2c_adapter *adap, uint8_t chip,
|
|
|
|
uint addr, int alen, uint8_t *buffer, int len)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
return i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static int adi_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
|
2008-10-06 08:16:47 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
u8 byte;
|
|
|
|
return adi_i2c_read(adap, chip, 0, 0, &byte, 1);
|
2008-10-06 08:16:47 +00:00
|
|
|
}
|
2010-05-05 07:20:30 +00:00
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
static struct twi_regs *i2c_get_base(struct i2c_adapter *adap)
|
2010-05-05 07:20:30 +00:00
|
|
|
{
|
2014-11-13 07:30:55 +00:00
|
|
|
switch (adap->hwadapnr) {
|
|
|
|
#if CONFIG_SYS_MAX_I2C_BUS > 2
|
|
|
|
case 2:
|
|
|
|
return (struct twi_regs *)TWI2_CLKDIV;
|
2010-05-05 07:20:30 +00:00
|
|
|
#endif
|
|
|
|
#if CONFIG_SYS_MAX_I2C_BUS > 1
|
2014-11-13 07:30:54 +00:00
|
|
|
case 1:
|
2014-11-13 07:30:55 +00:00
|
|
|
return (struct twi_regs *)TWI1_CLKDIV;
|
2010-05-05 07:20:30 +00:00
|
|
|
#endif
|
2014-11-13 07:30:55 +00:00
|
|
|
case 0:
|
|
|
|
return (struct twi_regs *)TWI0_CLKDIV;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("wrong hwadapnr: %d\n", adap->hwadapnr);
|
2010-05-05 07:20:30 +00:00
|
|
|
}
|
2014-11-13 07:30:55 +00:00
|
|
|
|
|
|
|
return NULL;
|
2010-05-05 07:20:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:30:55 +00:00
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(adi_i2c0, adi_i2c_init, adi_i2c_probe,
|
|
|
|
adi_i2c_read, adi_i2c_write,
|
|
|
|
adi_i2c_setspeed,
|
|
|
|
CONFIG_SYS_I2C_SPEED,
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
|
2010-05-05 07:20:30 +00:00
|
|
|
#if CONFIG_SYS_MAX_I2C_BUS > 1
|
2014-11-13 07:30:55 +00:00
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(adi_i2c1, adi_i2c_init, adi_i2c_probe,
|
|
|
|
adi_i2c_read, adi_i2c_write,
|
|
|
|
adi_i2c_setspeed,
|
|
|
|
CONFIG_SYS_I2C_SPEED,
|
|
|
|
0,
|
|
|
|
1)
|
2010-05-05 07:20:30 +00:00
|
|
|
#endif
|
2014-11-13 07:30:55 +00:00
|
|
|
|
2010-05-05 07:20:30 +00:00
|
|
|
#if CONFIG_SYS_MAX_I2C_BUS > 2
|
2014-11-13 07:30:55 +00:00
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(adi_i2c2, adi_i2c_init, adi_i2c_probe,
|
|
|
|
adi_i2c_read, adi_i2c_write,
|
|
|
|
adi_i2c_setspeed,
|
|
|
|
CONFIG_SYS_I2C_SPEED,
|
|
|
|
0,
|
|
|
|
2)
|
2010-05-05 07:20:30 +00:00
|
|
|
#endif
|