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>
41 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/extres/hwreset/hwreset.h>
49 #include "hwreset_if.h"
51 #define RESET_OFFSET(index) ((index / 32) * 4)
52 #define RESET_SHIFT(index) (index % 32)
54 static struct ofw_compat_data compat_data[] = {
55 { "allwinner,sun6i-a31-ahb1-reset", 1 },
56 { "allwinner,sun6i-a31-clock-reset", 1 },
60 struct aw_reset_softc {
65 static struct resource_spec aw_reset_spec[] = {
66 { SYS_RES_MEMORY, 0, RF_ACTIVE },
70 #define RESET_READ(sc, reg) bus_read_4((sc)->res, (reg))
71 #define RESET_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
74 aw_reset_assert(device_t dev, intptr_t id, bool reset)
76 struct aw_reset_softc *sc;
79 sc = device_get_softc(dev);
82 reg_value = RESET_READ(sc, RESET_OFFSET(id));
84 reg_value &= ~(1 << RESET_SHIFT(id));
86 reg_value |= (1 << RESET_SHIFT(id));
87 RESET_WRITE(sc, RESET_OFFSET(id), reg_value);
94 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
96 struct aw_reset_softc *sc;
99 sc = device_get_softc(dev);
102 reg_value = RESET_READ(sc, RESET_OFFSET(id));
103 mtx_unlock(&sc->mtx);
105 *reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true;
111 aw_reset_probe(device_t dev)
113 if (!ofw_bus_status_okay(dev))
116 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
119 device_set_desc(dev, "Allwinner Module Resets");
120 return (BUS_PROBE_DEFAULT);
124 aw_reset_attach(device_t dev)
126 struct aw_reset_softc *sc;
128 sc = device_get_softc(dev);
130 if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) {
131 device_printf(dev, "cannot allocate resources for device\n");
135 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
137 hwreset_register_ofw_provider(dev);
142 static device_method_t aw_reset_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, aw_reset_probe),
145 DEVMETHOD(device_attach, aw_reset_attach),
147 /* Reset interface */
148 DEVMETHOD(hwreset_assert, aw_reset_assert),
149 DEVMETHOD(hwreset_is_asserted, aw_reset_is_asserted),
154 static driver_t aw_reset_driver = {
157 sizeof(struct aw_reset_softc),
160 static devclass_t aw_reset_devclass;
162 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, aw_reset_devclass,
163 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
164 MODULE_VERSION(aw_reset, 1);