1 /*-
2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 /*
27 * Allwinner module software reset registers
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/rman.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <machine/bus.h>
39
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42
43 #include <dev/hwreset/hwreset.h>
44
45 #include "hwreset_if.h"
46
47 #define RESET_OFFSET(index) ((index / 32) * 4)
48 #define RESET_SHIFT(index) (index % 32)
49
50 static struct ofw_compat_data compat_data[] = {
51 { "allwinner,sun6i-a31-ahb1-reset", 1 },
52 { "allwinner,sun6i-a31-clock-reset", 1 },
53 { NULL, 0 }
54 };
55
56 struct aw_reset_softc {
57 struct resource *res;
58 struct mtx mtx;
59 };
60
61 static struct resource_spec aw_reset_spec[] = {
62 { SYS_RES_MEMORY, 0, RF_ACTIVE },
63 { -1, 0 }
64 };
65
66 #define RESET_READ(sc, reg) bus_read_4((sc)->res, (reg))
67 #define RESET_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
68
69 static int
aw_reset_assert(device_t dev,intptr_t id,bool reset)70 aw_reset_assert(device_t dev, intptr_t id, bool reset)
71 {
72 struct aw_reset_softc *sc;
73 uint32_t reg_value;
74
75 sc = device_get_softc(dev);
76
77 mtx_lock(&sc->mtx);
78 reg_value = RESET_READ(sc, RESET_OFFSET(id));
79 if (reset)
80 reg_value &= ~(1 << RESET_SHIFT(id));
81 else
82 reg_value |= (1 << RESET_SHIFT(id));
83 RESET_WRITE(sc, RESET_OFFSET(id), reg_value);
84 mtx_unlock(&sc->mtx);
85
86 return (0);
87 }
88
89 static int
aw_reset_is_asserted(device_t dev,intptr_t id,bool * reset)90 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
91 {
92 struct aw_reset_softc *sc;
93 uint32_t reg_value;
94
95 sc = device_get_softc(dev);
96
97 mtx_lock(&sc->mtx);
98 reg_value = RESET_READ(sc, RESET_OFFSET(id));
99 mtx_unlock(&sc->mtx);
100
101 *reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true;
102
103 return (0);
104 }
105
106 static int
aw_reset_probe(device_t dev)107 aw_reset_probe(device_t dev)
108 {
109 if (!ofw_bus_status_okay(dev))
110 return (ENXIO);
111
112 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
113 return (ENXIO);
114
115 device_set_desc(dev, "Allwinner Module Resets");
116 return (BUS_PROBE_DEFAULT);
117 }
118
119 static int
aw_reset_attach(device_t dev)120 aw_reset_attach(device_t dev)
121 {
122 struct aw_reset_softc *sc;
123
124 sc = device_get_softc(dev);
125
126 if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) {
127 device_printf(dev, "cannot allocate resources for device\n");
128 return (ENXIO);
129 }
130
131 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
132
133 hwreset_register_ofw_provider(dev);
134
135 return (0);
136 }
137
138 static device_method_t aw_reset_methods[] = {
139 /* Device interface */
140 DEVMETHOD(device_probe, aw_reset_probe),
141 DEVMETHOD(device_attach, aw_reset_attach),
142
143 /* Reset interface */
144 DEVMETHOD(hwreset_assert, aw_reset_assert),
145 DEVMETHOD(hwreset_is_asserted, aw_reset_is_asserted),
146
147 DEVMETHOD_END
148 };
149
150 static driver_t aw_reset_driver = {
151 "aw_reset",
152 aw_reset_methods,
153 sizeof(struct aw_reset_softc),
154 };
155
156 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, 0, 0,
157 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
158 MODULE_VERSION(aw_reset, 1);
159