From 229a41531c64f7fcc574b62e2bf82452b5170b93 Mon Sep 17 00:00:00 2001 From: rpaulo Date: Sun, 7 Jul 2013 21:23:58 +0000 Subject: Convert bcm2835_mbox to the new mbox interface. Reviewed by: gonzo --- sys/arm/broadcom/bcm2835/bcm2835_mbox.c | 102 ++++++++++++++++---------------- 1 file changed, 52 insertions(+), 50 deletions(-) (limited to 'sys/arm/broadcom/bcm2835/bcm2835_mbox.c') diff --git a/sys/arm/broadcom/bcm2835/bcm2835_mbox.c b/sys/arm/broadcom/bcm2835/bcm2835_mbox.c index f18102b..4278742 100644 --- a/sys/arm/broadcom/bcm2835/bcm2835_mbox.c +++ b/sys/arm/broadcom/bcm2835/bcm2835_mbox.c @@ -49,8 +49,11 @@ __FBSDID("$FreeBSD$"); #include #include + #include +#include "mbox_if.h" + #define REG_READ 0x00 #define REG_POL 0x10 #define REG_SENDER 0x14 @@ -65,12 +68,12 @@ __FBSDID("$FreeBSD$"); #define MBOX_CHAN(msg) ((msg) & 0xf) #define MBOX_DATA(msg) ((msg) & ~0xf) -#define MBOX_LOCK do { \ - mtx_lock(&bcm_mbox_sc->lock); \ +#define MBOX_LOCK(sc) do { \ + mtx_lock(&(sc)->lock); \ } while(0) -#define MBOX_UNLOCK do { \ - mtx_unlock(&bcm_mbox_sc->lock); \ +#define MBOX_UNLOCK(sc) do { \ + mtx_unlock(&(sc)->lock); \ } while(0) #ifdef DEBUG @@ -90,12 +93,10 @@ struct bcm_mbox_softc { int msg[BCM2835_MBOX_CHANS]; }; -static struct bcm_mbox_softc *bcm_mbox_sc = NULL; - -#define mbox_read_4(reg) \ - bus_space_read_4(bcm_mbox_sc->bst, bcm_mbox_sc->bsh, reg) -#define mbox_write_4(reg, val) \ - bus_space_write_4(bcm_mbox_sc->bst, bcm_mbox_sc->bsh, reg, val) +#define mbox_read_4(sc, reg) \ + bus_space_read_4((sc)->bst, (sc)->bsh, reg) +#define mbox_write_4(sc, reg, val) \ + bus_space_write_4((sc)->bst, (sc)->bsh, reg, val) static void bcm_mbox_intr(void *arg) @@ -105,9 +106,9 @@ bcm_mbox_intr(void *arg) uint32_t data; uint32_t msg; - MBOX_LOCK; - while (!(mbox_read_4(REG_STATUS) & STATUS_EMPTY)) { - msg = mbox_read_4(REG_READ); + MBOX_LOCK(sc); + while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) { + msg = mbox_read_4(sc, REG_READ); dprintf("bcm_mbox_intr: raw data %08x\n", msg); chan = MBOX_CHAN(msg); data = MBOX_DATA(msg); @@ -121,7 +122,7 @@ bcm_mbox_intr(void *arg) wakeup(&sc->msg[chan]); } - MBOX_UNLOCK; + MBOX_UNLOCK(sc); } static int @@ -143,9 +144,6 @@ bcm_mbox_attach(device_t dev) int i; int rid = 0; - if (bcm_mbox_sc != NULL) - return (EINVAL); - sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); if (sc->mem_res == NULL) { device_printf(dev, "could not allocate memory resource\n"); @@ -176,72 +174,76 @@ bcm_mbox_attach(device_t dev) sc->msg[0] = 0; } - bcm_mbox_sc = sc; /* Read all pending messages */ bcm_mbox_intr(sc); - /* Should be called after bcm_mbox_sc initialization */ - mbox_write_4(REG_CONFIG, CONFIG_DATA_IRQ); + mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ); return (0); } -static device_method_t bcm_mbox_methods[] = { - DEVMETHOD(device_probe, bcm_mbox_probe), - DEVMETHOD(device_attach, bcm_mbox_attach), - { 0, 0 } -}; - -static driver_t bcm_mbox_driver = { - "mbox", - bcm_mbox_methods, - sizeof(struct bcm_mbox_softc), -}; - -static devclass_t bcm_mbox_devclass; - -DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0); - /* * Mailbox API */ -int -bcm_mbox_write(int chan, uint32_t data) +static int +bcm_mbox_write(device_t dev, int chan, uint32_t data) { int limit = 20000; + struct bcm_mbox_softc *sc = device_get_softc(dev); dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data); - MBOX_LOCK; + MBOX_LOCK(sc); - while ((mbox_read_4(REG_STATUS) & STATUS_FULL) && limit--) { + while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && limit--) { DELAY(2); } if (limit == 0) { printf("bcm_mbox_write: STATUS_FULL stuck"); - MBOX_UNLOCK; + MBOX_UNLOCK(sc); return (EAGAIN); } - mbox_write_4(REG_WRITE, MBOX_MSG(chan, data)); + mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data)); - MBOX_UNLOCK; + MBOX_UNLOCK(sc); return (0); } -int -bcm_mbox_read(int chan, uint32_t *data) +static int +bcm_mbox_read(device_t dev, int chan, uint32_t *data) { - struct bcm_mbox_softc *sc = bcm_mbox_sc; + struct bcm_mbox_softc *sc = device_get_softc(dev); dprintf("bcm_mbox_read: chan %d\n", chan); - MBOX_LOCK; + MBOX_LOCK(sc); while (!sc->valid[chan]) msleep(&sc->msg[chan], &sc->lock, PZERO, "vcio mbox read", 0); - *data = bcm_mbox_sc->msg[chan]; - bcm_mbox_sc->valid[chan] = 0; - MBOX_UNLOCK; + *data = sc->msg[chan]; + sc->valid[chan] = 0; + MBOX_UNLOCK(sc); dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data); return (0); } + +static device_method_t bcm_mbox_methods[] = { + DEVMETHOD(device_probe, bcm_mbox_probe), + DEVMETHOD(device_attach, bcm_mbox_attach), + + DEVMETHOD(mbox_read, bcm_mbox_read), + DEVMETHOD(mbox_write, bcm_mbox_write), + + DEVMETHOD_END +}; + +static driver_t bcm_mbox_driver = { + "mbox", + bcm_mbox_methods, + sizeof(struct bcm_mbox_softc), +}; + +static devclass_t bcm_mbox_devclass; + +DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0); + -- cgit v1.1