2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Allwinner module software reset registers
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <machine/bus.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/extres/hwreset/hwreset.h>
51 #include "hwreset_if.h"
53 #define RESET_OFFSET(index) ((index / 32) * 4)
54 #define RESET_SHIFT(index) (index % 32)
56 static struct ofw_compat_data compat_data[] = {
57 { "allwinner,sun6i-a31-ahb1-reset", 1 },
58 { "allwinner,sun6i-a31-clock-reset", 1 },
62 struct aw_reset_softc {
67 static struct resource_spec aw_reset_spec[] = {
68 { SYS_RES_MEMORY, 0, RF_ACTIVE },
72 #define RESET_READ(sc, reg) bus_read_4((sc)->res, (reg))
73 #define RESET_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
76 aw_reset_assert(device_t dev, intptr_t id, bool reset)
78 struct aw_reset_softc *sc;
81 sc = device_get_softc(dev);
84 reg_value = RESET_READ(sc, RESET_OFFSET(id));
86 reg_value &= ~(1 << RESET_SHIFT(id));
88 reg_value |= (1 << RESET_SHIFT(id));
89 RESET_WRITE(sc, RESET_OFFSET(id), reg_value);
96 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
98 struct aw_reset_softc *sc;
101 sc = device_get_softc(dev);
104 reg_value = RESET_READ(sc, RESET_OFFSET(id));
105 mtx_unlock(&sc->mtx);
107 *reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true;
113 aw_reset_probe(device_t dev)
115 if (!ofw_bus_status_okay(dev))
118 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
121 device_set_desc(dev, "Allwinner Module Resets");
122 return (BUS_PROBE_DEFAULT);
126 aw_reset_attach(device_t dev)
128 struct aw_reset_softc *sc;
130 sc = device_get_softc(dev);
132 if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) {
133 device_printf(dev, "cannot allocate resources for device\n");
137 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
139 hwreset_register_ofw_provider(dev);
144 static device_method_t aw_reset_methods[] = {
145 /* Device interface */
146 DEVMETHOD(device_probe, aw_reset_probe),
147 DEVMETHOD(device_attach, aw_reset_attach),
149 /* Reset interface */
150 DEVMETHOD(hwreset_assert, aw_reset_assert),
151 DEVMETHOD(hwreset_is_asserted, aw_reset_is_asserted),
156 static driver_t aw_reset_driver = {
159 sizeof(struct aw_reset_softc),
162 static devclass_t aw_reset_devclass;
164 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, aw_reset_devclass,
165 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
166 MODULE_VERSION(aw_reset, 1);