#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <arm/ti/ti_cpuid.h>
#include <arm/ti/ti_sysc.h>
#include <arm/ti/ti_i2c.h>
#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>
#include "iicbus_if.h"
struct ti_i2c_softc
{
device_t sc_dev;
struct resource* sc_irq_res;
struct resource* sc_mem_res;
device_t sc_iicbus;
void* sc_irq_h;
struct mtx sc_mtx;
struct iic_msg* sc_buffer;
int sc_bus_inuse;
int sc_buffer_pos;
int sc_error;
int sc_fifo_trsh;
int sc_timeout;
uint16_t sc_con_reg;
uint16_t sc_rev;
};
struct ti_i2c_clock_config
{
u_int frequency;
uint8_t psc;
uint8_t scll;
uint8_t sclh;
uint8_t hsscll;
uint8_t hssclh;
};
#if defined(SOC_TI_AM335X)
static struct ti_i2c_clock_config ti_am335x_i2c_clock_configs[] = {
{ 100000, 1, 111, 117, 0, 0},
{ 400000, 1, 23, 25, 0, 0},
{ 1000000, 1, 5, 7, 0, 0},
{ 0 }
};
#endif
#define TI_I2C_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
#define TI_I2C_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
#define TI_I2C_LOCK_INIT(_sc) \
mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
"ti_i2c", MTX_DEF)
#define TI_I2C_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx)
#define TI_I2C_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
#define TI_I2C_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
#ifdef DEBUG
#define ti_i2c_dbg(_sc, fmt, args...) \
device_printf((_sc)->sc_dev, fmt, ##args)
#else
#define ti_i2c_dbg(_sc, fmt, args...)
#endif
static inline uint16_t
ti_i2c_read_2(struct ti_i2c_softc *sc, bus_size_t off)
{
return (bus_read_2(sc->sc_mem_res, off));
}
static inline void
ti_i2c_write_2(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)
{
bus_write_2(sc->sc_mem_res, off, val);
}
static int
ti_i2c_transfer_intr(struct ti_i2c_softc* sc, uint16_t status)
{
int amount, done, i;
done = 0;
amount = 0;
if (status & I2C_STAT_NACK) {
ti_i2c_dbg(sc, "NACK\n");
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_NACK);
sc->sc_error = ENXIO;
} else if (status & I2C_STAT_AL) {
ti_i2c_dbg(sc, "Arbitration lost\n");
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_AL);
sc->sc_error = ENXIO;
}
if (status & I2C_STAT_ARDY) {
ti_i2c_dbg(sc, "ARDY transaction complete\n");
if (sc->sc_error != 0 && sc->sc_buffer->flags & IIC_M_NOSTOP) {
ti_i2c_write_2(sc, I2C_REG_CON,
sc->sc_con_reg | I2C_CON_STP);
}
ti_i2c_write_2(sc, I2C_REG_STATUS,
I2C_STAT_ARDY | I2C_STAT_RDR | I2C_STAT_RRDY |
I2C_STAT_XDR | I2C_STAT_XRDY);
return (1);
}
if (sc->sc_buffer->flags & IIC_M_RD) {
if (status & I2C_STAT_RDR) {
ti_i2c_dbg(sc, "Receive draining interrupt\n");
amount = sc->sc_buffer->len - sc->sc_buffer_pos;
} else if (status & I2C_STAT_RRDY) {
ti_i2c_dbg(sc, "Receive data ready interrupt\n");
amount = min(sc->sc_fifo_trsh,
sc->sc_buffer->len - sc->sc_buffer_pos);
}
for (i = 0; i < amount; i++)
sc->sc_buffer->buf[sc->sc_buffer_pos++] =
(uint8_t)(ti_i2c_read_2(sc, I2C_REG_DATA) & 0xff);
if (status & I2C_STAT_RDR)
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RDR);
if (status & I2C_STAT_RRDY)
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RRDY);
} else {
if (status & I2C_STAT_XDR) {
ti_i2c_dbg(sc, "Transmit draining interrupt\n");
amount = sc->sc_buffer->len - sc->sc_buffer_pos;
} else if (status & I2C_STAT_XRDY) {
ti_i2c_dbg(sc, "Transmit data ready interrupt\n");
amount = min(sc->sc_fifo_trsh,
sc->sc_buffer->len - sc->sc_buffer_pos);
}
for (i = 0; i < amount; i++)
ti_i2c_write_2(sc, I2C_REG_DATA,
sc->sc_buffer->buf[sc->sc_buffer_pos++]);
if (status & I2C_STAT_XDR)
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XDR);
if (status & I2C_STAT_XRDY)
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XRDY);
}
return (done);
}
static void
ti_i2c_intr(void *arg)
{
int done;
struct ti_i2c_softc *sc;
uint16_t events, status;
sc = (struct ti_i2c_softc *)arg;
TI_I2C_LOCK(sc);
status = ti_i2c_read_2(sc, I2C_REG_STATUS);
if (status == 0) {
TI_I2C_UNLOCK(sc);
return;
}
events = ti_i2c_read_2(sc, I2C_REG_IRQENABLE_SET);
status &= events;
done = 0;
if (sc->sc_buffer != NULL)
done = ti_i2c_transfer_intr(sc, status);
else {
ti_i2c_dbg(sc, "Transfer interrupt without buffer\n");
sc->sc_error = EINVAL;
done = 1;
}
if (done)
wakeup(sc);
TI_I2C_UNLOCK(sc);
}
static int
ti_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
{
int err, i, repstart, timeout;
struct ti_i2c_softc *sc;
uint16_t reg;
sc = device_get_softc(dev);
TI_I2C_LOCK(sc);
while (sc->sc_bus_inuse == 1)
mtx_sleep(sc, &sc->sc_mtx, 0, "i2cbuswait", 0);
sc->sc_bus_inuse = 1;
err = 0;
repstart = 0;
for (i = 0; i < nmsgs; i++) {
sc->sc_buffer = &msgs[i];
sc->sc_buffer_pos = 0;
sc->sc_error = 0;
if (sc->sc_buffer == NULL || sc->sc_buffer->buf == NULL ||
sc->sc_buffer->len == 0) {
err = EINVAL;
break;
}
if (repstart == 0) {
timeout = 0;
while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
if (timeout++ > 100) {
err = EBUSY;
goto out;
}
DELAY(1000);
}
timeout = 0;
} else
repstart = 0;
if (sc->sc_buffer->flags & IIC_M_NOSTOP)
repstart = 1;
ti_i2c_write_2(sc, I2C_REG_SA, msgs[i].slave >> 1);
ti_i2c_write_2(sc, I2C_REG_CNT, sc->sc_buffer->len);
reg = ti_i2c_read_2(sc, I2C_REG_BUF);
reg |= I2C_BUF_RXFIFO_CLR | I2C_BUF_TXFIFO_CLR;
ti_i2c_write_2(sc, I2C_REG_BUF, reg);
reg = sc->sc_con_reg | I2C_CON_STT;
if (repstart == 0)
reg |= I2C_CON_STP;
if ((sc->sc_buffer->flags & IIC_M_RD) == 0)
reg |= I2C_CON_TRX;
ti_i2c_write_2(sc, I2C_REG_CON, reg);
err = mtx_sleep(sc, &sc->sc_mtx, 0, "i2ciowait", sc->sc_timeout);
if (err == 0)
err = sc->sc_error;
if (err)
break;
}
out:
if (timeout == 0) {
while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
if (timeout++ > 100)
break;
DELAY(1000);
}
}
if ((ti_i2c_read_2(sc, I2C_REG_CON) & I2C_CON_MST) == 0)
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
sc->sc_buffer = NULL;
sc->sc_bus_inuse = 0;
wakeup(sc);
TI_I2C_UNLOCK(sc);
return (err);
}
static int
ti_i2c_reset(struct ti_i2c_softc *sc, u_char speed)
{
int timeout;
struct ti_i2c_clock_config *clkcfg;
u_int busfreq;
uint16_t fifo_trsh, reg, scll, sclh;
switch (ti_chip()) {
#ifdef SOC_TI_AM335X
case CHIP_AM335X:
clkcfg = ti_am335x_i2c_clock_configs;
break;
#endif
default:
panic("Unknown TI SoC, unable to reset the i2c");
}
if (sc->sc_iicbus == NULL)
busfreq = 100000;
else
busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
for (;;) {
if (clkcfg[1].frequency == 0 || clkcfg[1].frequency > busfreq)
break;
clkcfg++;
}
sc->sc_con_reg = 0;
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, I2C_REG_SYSC_SRST);
ti_i2c_write_2(sc, I2C_REG_CON, I2C_CON_I2C_EN);
timeout = 0;
while ((ti_i2c_read_2(sc, I2C_REG_SYSS) & I2C_SYSS_RDONE) == 0) {
if (timeout++ > 100)
return (EBUSY);
DELAY(100);
}
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
ti_i2c_write_2(sc, I2C_REG_PSC, clkcfg->psc);
scll = clkcfg->scll & I2C_SCLL_MASK;
sclh = clkcfg->sclh & I2C_SCLH_MASK;
ti_i2c_write_2(sc, I2C_REG_SCLL, scll);
ti_i2c_write_2(sc, I2C_REG_SCLH, sclh);
fifo_trsh = (sc->sc_fifo_trsh - 1) & I2C_BUF_TRSH_MASK;
reg = fifo_trsh | (fifo_trsh << I2C_BUF_RXTRSH_SHIFT);
ti_i2c_write_2(sc, I2C_REG_BUF, reg);
sc->sc_con_reg |= I2C_CON_I2C_EN | I2C_CON_MST;
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
reg = I2C_IE_XDR |
I2C_IE_XRDY |
I2C_IE_RDR |
I2C_IE_RRDY |
I2C_IE_ARDY |
I2C_IE_NACK |
I2C_IE_AL;
ti_i2c_write_2(sc, I2C_REG_IRQENABLE_SET, reg);
return (0);
}
static int
ti_i2c_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
struct ti_i2c_softc *sc;
int err;
sc = device_get_softc(dev);
TI_I2C_LOCK(sc);
err = ti_i2c_reset(sc, speed);
TI_I2C_UNLOCK(sc);
if (err)
return (err);
return (IIC_ENOADDR);
}
static int
ti_i2c_activate(device_t dev)
{
int err;
struct ti_i2c_softc *sc;
sc = (struct ti_i2c_softc*)device_get_softc(dev);
err = ti_sysc_clock_enable(device_get_parent(dev));
if (err)
return (err);
return (ti_i2c_reset(sc, IIC_UNKNOWN));
}
static void
ti_i2c_deactivate(device_t dev)
{
struct ti_i2c_softc *sc = device_get_softc(dev);
ti_i2c_write_2(sc, I2C_REG_IRQENABLE_CLR, 0xffff);
ti_i2c_write_2(sc, I2C_REG_STATUS, 0xffff);
ti_i2c_write_2(sc, I2C_REG_CON, 0);
if (sc->sc_irq_h != NULL) {
bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
sc->sc_irq_h = NULL;
}
if (sc->sc_mem_res != NULL) {
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
sc->sc_mem_res = NULL;
}
if (sc->sc_irq_res != NULL) {
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
sc->sc_irq_res = NULL;
}
ti_sysc_clock_disable(device_get_parent(dev));
}
static int
ti_i2c_sysctl_clk(SYSCTL_HANDLER_ARGS)
{
int clk, psc, sclh, scll;
struct ti_i2c_softc *sc;
sc = arg1;
TI_I2C_LOCK(sc);
psc = (int)ti_i2c_read_2(sc, I2C_REG_PSC) + 1;
scll = (int)ti_i2c_read_2(sc, I2C_REG_SCLL) & I2C_SCLL_MASK;
sclh = (int)ti_i2c_read_2(sc, I2C_REG_SCLH) & I2C_SCLH_MASK;
clk = I2C_CLK / psc / (scll + 7 + sclh + 5);
TI_I2C_UNLOCK(sc);
return (sysctl_handle_int(oidp, &clk, 0, req));
}
static int
ti_i2c_sysctl_timeout(SYSCTL_HANDLER_ARGS)
{
struct ti_i2c_softc *sc;
unsigned int val;
int err;
sc = arg1;
TI_I2C_LOCK(sc);
val = sc->sc_timeout;
TI_I2C_UNLOCK(sc);
err = sysctl_handle_int(oidp, &val, 0, req);
if ((err == 0) && (req->newptr != NULL)) {
TI_I2C_LOCK(sc);
sc->sc_timeout = val;
TI_I2C_UNLOCK(sc);
}
return (err);
}
static int
ti_i2c_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_is_compatible(dev, "ti,omap4-i2c"))
return (ENXIO);
device_set_desc(dev, "TI I2C Controller");
return (0);
}
static int
ti_i2c_attach(device_t dev)
{
int err, rid;
struct ti_i2c_softc *sc;
struct sysctl_ctx_list *ctx;
struct sysctl_oid_list *tree;
uint16_t fifosz;
sc = device_get_softc(dev);
sc->sc_dev = dev;
rid = 0;
sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
RF_ACTIVE);
if (sc->sc_mem_res == NULL) {
device_printf(dev, "Cannot map registers.\n");
return (ENXIO);
}
rid = 0;
sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_ACTIVE | RF_SHAREABLE);
if (sc->sc_irq_res == NULL) {
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
device_printf(dev, "Cannot allocate interrupt.\n");
return (ENXIO);
}
TI_I2C_LOCK_INIT(sc);
err = ti_i2c_activate(dev);
if (err) {
device_printf(dev, "ti_i2c_activate failed\n");
goto out;
}
sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff;
fifosz = ti_i2c_read_2(sc, I2C_REG_BUFSTAT);
fifosz >>= I2C_BUFSTAT_FIFODEPTH_SHIFT;
fifosz &= I2C_BUFSTAT_FIFODEPTH_MASK;
device_printf(dev, "I2C revision %d.%d FIFO size: %d bytes\n",
sc->sc_rev >> 4, sc->sc_rev & 0xf, 8 << fifosz);
sc->sc_fifo_trsh = 5;
sc->sc_timeout = 5*hz;
ctx = device_get_sysctl_ctx(dev);
tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_clock",
CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
ti_i2c_sysctl_clk, "IU", "I2C bus clock");
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_timeout",
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
ti_i2c_sysctl_timeout, "IU", "I2C bus timeout (in ticks)");
err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
NULL, ti_i2c_intr, sc, &sc->sc_irq_h);
if (err)
goto out;
if ((sc->sc_iicbus = device_add_child(dev, "iicbus",
DEVICE_UNIT_ANY)) == NULL) {
device_printf(dev, "could not allocate iicbus instance\n");
err = ENXIO;
goto out;
}
bus_delayed_attach_children(dev);
out:
if (err) {
ti_i2c_deactivate(dev);
TI_I2C_LOCK_DESTROY(sc);
}
return (err);
}
static int
ti_i2c_detach(device_t dev)
{
struct ti_i2c_softc *sc;
int rv;
sc = device_get_softc(dev);
if ((rv = bus_generic_detach(dev)) != 0) {
device_printf(dev, "cannot detach child devices\n");
return (rv);
}
ti_i2c_deactivate(dev);
TI_I2C_LOCK_DESTROY(sc);
return (0);
}
static phandle_t
ti_i2c_get_node(device_t bus, device_t dev)
{
return (ofw_bus_get_node(bus));
}
static device_method_t ti_i2c_methods[] = {
DEVMETHOD(device_probe, ti_i2c_probe),
DEVMETHOD(device_attach, ti_i2c_attach),
DEVMETHOD(device_detach, ti_i2c_detach),
DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
DEVMETHOD(bus_release_resource, bus_generic_release_resource),
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
DEVMETHOD(ofw_bus_get_node, ti_i2c_get_node),
DEVMETHOD(iicbus_callback, iicbus_null_callback),
DEVMETHOD(iicbus_reset, ti_i2c_iicbus_reset),
DEVMETHOD(iicbus_transfer, ti_i2c_transfer),
DEVMETHOD_END
};
static driver_t ti_i2c_driver = {
"iichb",
ti_i2c_methods,
sizeof(struct ti_i2c_softc),
};
DRIVER_MODULE(ti_iic, simplebus, ti_i2c_driver, 0, 0);
DRIVER_MODULE(iicbus, ti_iic, iicbus_driver, 0, 0);
MODULE_DEPEND(ti_iic, ti_sysc, 1, 1, 1);
MODULE_DEPEND(ti_iic, iicbus, 1, 1, 1);