#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/bus.h>
#include <machine/resource.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <arm/broadcom/bcm2835/bcm2835_bscreg.h>
#include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
#include "iicbus_if.h"
static struct ofw_compat_data compat_data[] = {
{"broadcom,bcm2835-bsc", 1},
{"brcm,bcm2708-i2c", 1},
{"brcm,bcm2835-i2c", 1},
{NULL, 0}
};
#define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
if ((lvl) <= (sc)->sc_debug) \
device_printf((sc)->sc_dev, fmt, ##args)
#define DEBUGF(sc, lvl, fmt, args...) \
if ((lvl) <= (sc)->sc_debug) \
printf(fmt, ##args)
static void bcm_bsc_intr(void *);
static int bcm_bsc_detach(device_t);
static void
bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
uint32_t value)
{
uint32_t reg;
mtx_assert(&sc->sc_mtx, MA_OWNED);
reg = BCM_BSC_READ(sc, off);
reg &= ~mask;
reg |= value;
BCM_BSC_WRITE(sc, off, reg);
}
static int
bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
{
struct bcm_bsc_softc *sc;
uint32_t clk;
sc = (struct bcm_bsc_softc *)arg1;
BCM_BSC_LOCK(sc);
clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
BCM_BSC_UNLOCK(sc);
clk &= 0xffff;
if (clk == 0)
clk = 32768;
clk = BCM_BSC_CORE_CLK / clk;
return (sysctl_handle_int(oidp, &clk, 0, req));
}
static int
bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
{
struct bcm_bsc_softc *sc;
uint32_t clkt;
int error;
sc = (struct bcm_bsc_softc *)arg1;
BCM_BSC_LOCK(sc);
clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
BCM_BSC_UNLOCK(sc);
clkt &= 0xffff;
error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
if (error != 0 || req->newptr == NULL)
return (error);
BCM_BSC_LOCK(sc);
BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
BCM_BSC_UNLOCK(sc);
return (0);
}
static int
bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
{
struct bcm_bsc_softc *sc;
uint32_t clk, reg;
int error;
sc = (struct bcm_bsc_softc *)arg1;
BCM_BSC_LOCK(sc);
reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
BCM_BSC_UNLOCK(sc);
reg >>= 16;
error = sysctl_handle_int(oidp, ®, sizeof(reg), req);
if (error != 0 || req->newptr == NULL)
return (error);
BCM_BSC_LOCK(sc);
clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
clk = BCM_BSC_CORE_CLK / clk;
if (reg > clk / 2)
reg = clk / 2 - 1;
bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
BCM_BSC_UNLOCK(sc);
return (0);
}
static int
bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
{
struct bcm_bsc_softc *sc;
uint32_t clk, reg;
int error;
sc = (struct bcm_bsc_softc *)arg1;
BCM_BSC_LOCK(sc);
reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
BCM_BSC_UNLOCK(sc);
reg &= 0xffff;
error = sysctl_handle_int(oidp, ®, sizeof(reg), req);
if (error != 0 || req->newptr == NULL)
return (error);
BCM_BSC_LOCK(sc);
clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
clk = BCM_BSC_CORE_CLK / clk;
if (reg > clk / 2)
reg = clk / 2 - 1;
bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
BCM_BSC_UNLOCK(sc);
return (0);
}
static void
bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
{
struct sysctl_ctx_list *ctx;
struct sysctl_oid *tree_node;
struct sysctl_oid_list *tree;
ctx = device_get_sysctl_ctx(sc->sc_dev);
tree_node = device_get_sysctl_tree(sc->sc_dev);
tree = SYSCTL_CHILDREN(tree_node);
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
sc, sizeof(*sc),
bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
sc, sizeof(*sc),
bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
sc, sizeof(*sc),
bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
sc, sizeof(*sc),
bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "debug",
CTLFLAG_RWTUN, &sc->sc_debug, 0,
"Enable debug; 1=reads/writes, 2=add starts/stops");
}
static void
bcm_bsc_reset(struct bcm_bsc_softc *sc)
{
BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
BCM_BSC_CTRL_CLEAR0);
}
static int
bcm_bsc_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
return (ENXIO);
device_set_desc(dev, "BCM2708/2835 BSC controller");
return (BUS_PROBE_DEFAULT);
}
static int
bcm_bsc_attach(device_t dev)
{
struct bcm_bsc_softc *sc;
int rid;
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) {
device_printf(dev, "cannot allocate memory window\n");
return (ENXIO);
}
sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
rid = 0;
sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_ACTIVE | RF_SHAREABLE);
if (!sc->sc_irq_res) {
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
device_printf(dev, "cannot allocate interrupt\n");
return (ENXIO);
}
if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
device_printf(dev, "cannot setup the interrupt handler\n");
return (ENXIO);
}
mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
bcm_bsc_sysctl_init(sc);
BCM_BSC_LOCK(sc);
bcm_bsc_reset(sc);
BCM_BSC_UNLOCK(sc);
sc->sc_iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
if (sc->sc_iicbus == NULL) {
bcm_bsc_detach(dev);
return (ENXIO);
}
bus_delayed_attach_children(dev);
return (0);
}
static int
bcm_bsc_detach(device_t dev)
{
struct bcm_bsc_softc *sc;
bus_generic_detach(dev);
sc = device_get_softc(dev);
mtx_destroy(&sc->sc_mtx);
if (sc->sc_intrhand)
bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
if (sc->sc_irq_res)
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
if (sc->sc_mem_res)
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
return (0);
}
static void
bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc *sc)
{
uint32_t status;
do {
if (sc->sc_resid == 0) {
sc->sc_data = sc->sc_curmsg->buf;
sc->sc_dlen = sc->sc_curmsg->len;
sc->sc_resid = sc->sc_dlen;
++sc->sc_curmsg;
}
do {
*sc->sc_data = BCM_BSC_READ(sc, BCM_BSC_DATA);
DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
++sc->sc_data;
--sc->sc_resid;
--sc->sc_totlen;
status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD));
} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_RXD));
}
static void
bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc *sc)
{
uint32_t status;
do {
if (sc->sc_resid == 0) {
sc->sc_data = sc->sc_curmsg->buf;
sc->sc_dlen = sc->sc_curmsg->len;
sc->sc_resid = sc->sc_dlen;
++sc->sc_curmsg;
}
do {
BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
++sc->sc_data;
--sc->sc_resid;
--sc->sc_totlen;
status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD));
if (sc->sc_replen > 0 && sc->sc_resid == 0) {
sc->sc_replen -= sc->sc_dlen;
if (sc->sc_replen == 0) {
DEBUGF(sc, 1, " err=0\n");
DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n",
sc->sc_curmsg->slave | 0x01);
DEVICE_DEBUGF(sc, 1,
"read 0x%02x len %d: ",
sc->sc_curmsg->slave | 0x01,
sc->sc_totlen);
sc->sc_flags |= BCM_I2C_READ;
return;
}
}
} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_TXD));
}
static void
bcm_bsc_intr(void *arg)
{
struct bcm_bsc_softc *sc;
uint32_t status;
sc = (struct bcm_bsc_softc *)arg;
BCM_BSC_LOCK(sc);
if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
BCM_BSC_UNLOCK(sc);
return;
}
status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
DEBUGF(sc, 4, " <intrstatus=0x%08x> ", status);
if ((sc->sc_flags & BCM_I2C_READ) && (status & BCM_BSC_STATUS_RXD))
bcm_bsc_empty_rx_fifo(sc);
if (status & (BCM_BSC_STATUS_ERRBITS | BCM_BSC_STATUS_DONE)) {
sc->sc_flags |= BCM_I2C_DONE;
if (status & BCM_BSC_STATUS_ERRBITS)
sc->sc_flags |= BCM_I2C_ERROR;
bcm_bsc_reset(sc);
wakeup(sc);
} else if (!(sc->sc_flags & BCM_I2C_READ)) {
if ((status & BCM_BSC_STATUS_TXD) && sc->sc_totlen > 0)
bcm_bsc_fill_tx_fifo(sc);
}
BCM_BSC_UNLOCK(sc);
}
static int
bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
{
struct bcm_bsc_softc *sc;
struct iic_msg *endmsgs, *nxtmsg;
uint32_t readctl, status;
int err;
uint16_t curlen;
uint8_t curisread, curslave, nxtisread, nxtslave;
sc = device_get_softc(dev);
BCM_BSC_LOCK(sc);
while (sc->sc_flags & BCM_I2C_BUSY)
mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
sc->sc_flags = BCM_I2C_BUSY;
DEVICE_DEBUGF(sc, 3, "Transfer %d msgs\n", nmsgs);
bcm_bsc_reset(sc);
err = 0;
sc->sc_resid = 0;
sc->sc_curmsg = msgs;
endmsgs = &msgs[nmsgs];
while (sc->sc_curmsg < endmsgs) {
readctl = 0;
curslave = sc->sc_curmsg->slave >> 1;
curisread = sc->sc_curmsg->flags & IIC_M_RD;
sc->sc_replen = 0;
sc->sc_totlen = sc->sc_curmsg->len;
for (nxtmsg = sc->sc_curmsg + 1; nxtmsg < endmsgs; ++nxtmsg) {
nxtslave = nxtmsg->slave >> 1;
if (curslave == nxtslave) {
nxtisread = nxtmsg->flags & IIC_M_RD;
if (curisread == nxtisread) {
sc->sc_totlen += nxtmsg->len;
continue;
} else if (curisread == IIC_M_WR) {
curisread = IIC_M_RD;
sc->sc_replen = sc->sc_totlen;
sc->sc_totlen += nxtmsg->len;
continue;
}
}
break;
}
curisread = (sc->sc_curmsg->flags & IIC_M_RD) ? 1 : 0;
curslave = sc->sc_curmsg->slave | curisread;
BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, curslave >> 1);
DEVICE_DEBUGF(sc, 2, "start 0x%02x\n", curslave);
if (sc->sc_replen == 0) {
DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
(curisread) ? "read" : "write", curslave,
sc->sc_totlen);
curlen = sc->sc_totlen;
if (curisread) {
readctl = BCM_BSC_CTRL_READ;
sc->sc_flags |= BCM_I2C_READ;
} else {
readctl = 0;
sc->sc_flags &= ~BCM_I2C_READ;
}
} else {
DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
(curisread) ? "read" : "write", curslave,
sc->sc_replen);
BCM_BSC_WRITE(sc, BCM_BSC_DLEN, sc->sc_replen);
BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
BCM_BSC_CTRL_ST);
do {
status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
if (status & BCM_BSC_STATUS_ERR) {
err = EIO;
goto xfer_done;
}
} while ((status & BCM_BSC_STATUS_TA) == 0);
curlen = sc->sc_totlen - sc->sc_replen;
readctl = BCM_BSC_CTRL_READ;
sc->sc_flags &= ~BCM_I2C_READ;
}
BCM_BSC_WRITE(sc, BCM_BSC_DLEN, curlen);
BCM_BSC_WRITE(sc, BCM_BSC_CTRL, readctl | BCM_BSC_CTRL_I2CEN |
BCM_BSC_CTRL_ST | BCM_BSC_CTRL_INT_ALL);
if (!(sc->sc_curmsg->flags & IIC_M_RD)) {
bcm_bsc_fill_tx_fifo(sc);
}
while (err == 0 && !(sc->sc_flags & BCM_I2C_DONE)) {
err = mtx_sleep(sc, &sc->sc_mtx, 0, "bsciow", hz);
}
if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
err = EIO;
xfer_done:
DEBUGF(sc, 1, " err=%d\n", err);
DEVICE_DEBUGF(sc, 2, "stop\n");
if (err != 0)
break;
}
bcm_bsc_reset(sc);
sc->sc_flags = 0;
wakeup(dev);
BCM_BSC_UNLOCK(sc);
return (err);
}
static int
bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
struct bcm_bsc_softc *sc;
uint32_t busfreq;
sc = device_get_softc(dev);
BCM_BSC_LOCK(sc);
bcm_bsc_reset(sc);
if (sc->sc_iicbus == NULL)
busfreq = 100000;
else
busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
BCM_BSC_UNLOCK(sc);
return (IIC_ENOADDR);
}
static phandle_t
bcm_bsc_get_node(device_t bus, device_t dev)
{
return (ofw_bus_get_node(bus));
}
static device_method_t bcm_bsc_methods[] = {
DEVMETHOD(device_probe, bcm_bsc_probe),
DEVMETHOD(device_attach, bcm_bsc_attach),
DEVMETHOD(device_detach, bcm_bsc_detach),
DEVMETHOD(iicbus_reset, bcm_bsc_iicbus_reset),
DEVMETHOD(iicbus_callback, iicbus_null_callback),
DEVMETHOD(iicbus_transfer, bcm_bsc_transfer),
DEVMETHOD(ofw_bus_get_node, bcm_bsc_get_node),
DEVMETHOD_END
};
static driver_t bcm_bsc_driver = {
"iichb",
bcm_bsc_methods,
sizeof(struct bcm_bsc_softc),
};
DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, 0, 0);
DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, 0, 0);