commit a2b6f63fbc8e4ed46f1e6c68c1e36da74d7fcb02 Author: Mitchell Horne Date: Mon Apr 15 17:18:25 2024 -0300 WIP: armada38x: implement gateclk clkdev methods Implement basic clkdev methods required by the generic clk_gate device. This should make it functional, and prevent panics when invoking the clock's CLKNODE_GET_GATE method (e.g. with `sysctl -a`). Additionally, we need to fill in other key fields of the clk_gate_def structure before registering each clk. PR: 278188 diff --git a/sys/arm/mv/clk/armada38x_gateclk.c b/sys/arm/mv/clk/armada38x_gateclk.c index 24355d9c9dac..220a0612b8d9 100644 --- a/sys/arm/mv/clk/armada38x_gateclk.c +++ b/sys/arm/mv/clk/armada38x_gateclk.c @@ -43,11 +43,20 @@ #include "clkdev_if.h" +static struct resource_spec armada38x_gateclk_specs[] = { + { SYS_RES_MEMORY, 0, RF_ACTIVE }, + { -1, 0 } +}; + +#define RD4(_sc, addr) bus_read_4(_sc->res, addr) +#define WR4(_sc, addr, val) bus_write_4(_sc->res, addr, val) + struct armada38x_gateclk_softc { - struct clkdom *clkdom; - struct mtx mtx; - const char* parent; + struct resource *res; + struct clkdom *clkdom; + struct mtx mtx; + const char* parent; }; static struct clk_gate_def gateclk_nodes[] = @@ -219,10 +228,66 @@ static struct clk_gate_def gateclk_nodes[] = static int armada38x_gateclk_probe(device_t dev); static int armada38x_gateclk_attach(device_t dev); +static int +armada38x_gateclk_write_4(device_t dev, bus_addr_t addr, uint32_t val) +{ + struct armada38x_gateclk_softc *sc = device_get_softc(dev); + + WR4(sc, addr, val); + return (0); +} + +static int +armada38x_gateclk_read_4(device_t dev, bus_addr_t addr, uint32_t *val) +{ + struct armada38x_gateclk_softc *sc = device_get_softc(dev); + + *val = RD4(sc, addr); + return (0); +} + +static int +armada38x_gateclk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, + uint32_t set) +{ + struct armada38x_gateclk_softc *sc = device_get_softc(dev); + uint32_t reg; + + reg = RD4(sc, addr); + reg &= ~clr; + reg |= set; + WR4(sc, addr, reg); + + return (0); +} + +static void +armada38x_gateclk_device_lock(device_t dev) +{ + struct armada38x_gateclk_softc *sc = device_get_softc(dev); + + mtx_lock(&sc->mtx); +} + +static void +armada38x_gateclk_device_unlock(device_t dev) +{ + struct armada38x_gateclk_softc *sc = device_get_softc(dev); + + mtx_unlock(&sc->mtx); +} + static device_method_t armada38x_gateclk_methods[] = { DEVMETHOD(device_probe, armada38x_gateclk_probe), DEVMETHOD(device_attach, armada38x_gateclk_attach), + /* clkdev interface */ + DEVMETHOD(clkdev_write_4, armada38x_gateclk_write_4), + DEVMETHOD(clkdev_read_4, armada38x_gateclk_read_4), + DEVMETHOD(clkdev_modify_4, armada38x_gateclk_modify_4), + DEVMETHOD(clkdev_device_lock, armada38x_gateclk_device_lock), + DEVMETHOD(clkdev_device_unlock, armada38x_gateclk_device_unlock), + DEVMETHOD_END }; @@ -254,6 +319,7 @@ static int armada38x_gateclk_attach(device_t dev) { struct armada38x_gateclk_softc *sc; + struct clk_gate_def *defp; phandle_t node; int i, error; clk_t clock; @@ -261,6 +327,11 @@ armada38x_gateclk_attach(device_t dev) sc = device_get_softc(dev); node = ofw_bus_get_node(dev); + if (bus_alloc_resources(dev, armada38x_gateclk_specs, &sc->res) != 0) { + device_printf(dev, "Cannot allocate resources.\n"); + return (ENXIO); + } + mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); sc->clkdom = clkdom_create(dev); @@ -276,8 +347,15 @@ armada38x_gateclk_attach(device_t dev) sc->parent = clk_get_name(clock); for (i = 0; i < nitems(gateclk_nodes); ++i) { - gateclk_nodes[i].clkdef.parent_names = &sc->parent; - error = clknode_gate_register(sc->clkdom, &gateclk_nodes[i]); + /* Fill clk_gate fields. */ + defp = &gateclk_nodes[i]; + defp->clkdef.parent_names = &sc->parent; + defp->offset = 0; + defp->mask = 0x1; + defp->on_value = 1; + defp->off_value = 0; + + error = clknode_gate_register(sc->clkdom, defp); if (error != 0) { device_printf(dev, "Cannot create gate nodes\n"); return (error);