#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <machine/bus.h>
#include <dev/fdt/simplebus.h>
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus_subr.h>
#include "dwgpio_if.h"
struct dwgpiobus_softc {
struct simplebus_softc simplebus_sc;
device_t dev;
struct resource *res[1];
};
static struct resource_spec dwgpio_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ -1, 0 }
};
static int
dwgpiobus_probe(device_t dev)
{
if (!ofw_bus_is_compatible(dev, "snps,dw-apb-gpio"))
return (ENXIO);
if (!ofw_bus_status_okay(dev))
return (ENXIO);
device_set_desc(dev, "Synopsys® DesignWare® APB GPIO BUS");
return (BUS_PROBE_DEFAULT);
}
static int
dwgpiobus_attach(device_t dev)
{
struct dwgpiobus_softc *sc;
phandle_t node;
sc = device_get_softc(dev);
sc->dev = dev;
node = ofw_bus_get_node(dev);
if (node == -1)
return (ENXIO);
if (bus_alloc_resources(dev, dwgpio_spec, sc->res)) {
device_printf(dev, "Could not allocate resources.\n");
return (ENXIO);
}
simplebus_init(dev, node);
bus_identify_children(dev);
for (node = OF_child(node); node > 0; node = OF_peer(node))
simplebus_add_device(dev, node, 0, NULL, -1, NULL);
bus_attach_children(dev);
return (0);
}
static int
dwgpiobus_detach(device_t dev)
{
struct dwgpiobus_softc *sc;
sc = device_get_softc(dev);
bus_release_resources(dev, dwgpio_spec, sc->res);
return (0);
}
static int
dwgpiobus_write(device_t dev, bus_size_t offset, int val)
{
struct dwgpiobus_softc *sc;
sc = device_get_softc(dev);
bus_write_4(sc->res[0], offset, val);
return (0);
};
static int
dwgpiobus_read(device_t dev, bus_size_t offset)
{
struct dwgpiobus_softc *sc;
int val;
sc = device_get_softc(dev);
val = bus_read_4(sc->res[0], offset);
return (val);
};
static device_method_t dwgpiobus_methods[] = {
DEVMETHOD(device_probe, dwgpiobus_probe),
DEVMETHOD(device_attach, dwgpiobus_attach),
DEVMETHOD(device_detach, dwgpiobus_detach),
DEVMETHOD(dwgpio_write, dwgpiobus_write),
DEVMETHOD(dwgpio_read, dwgpiobus_read),
DEVMETHOD_END
};
DEFINE_CLASS_1(dwgpiobus, dwgpiobus_driver, dwgpiobus_methods,
sizeof(struct dwgpiobus_softc), simplebus_driver);
EARLY_DRIVER_MODULE(dwgpiobus, simplebus, dwgpiobus_driver, 0, 0,
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
MODULE_VERSION(dwgpiobus, 1);