xref: /freebsd/sys/arm/allwinner/aw_reset.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Allwinner module software reset registers
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <machine/bus.h>
45 
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <dev/extres/hwreset/hwreset.h>
50 
51 #include "hwreset_if.h"
52 
53 #define	RESET_OFFSET(index)	((index / 32) * 4)
54 #define	RESET_SHIFT(index)	(index % 32)
55 
56 static struct ofw_compat_data compat_data[] = {
57 	{ "allwinner,sun6i-a31-ahb1-reset",	1 },
58 	{ "allwinner,sun6i-a31-clock-reset",	1 },
59 	{ NULL,					0 }
60 };
61 
62 struct aw_reset_softc {
63 	struct resource		*res;
64 	struct mtx		mtx;
65 };
66 
67 static struct resource_spec aw_reset_spec[] = {
68 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
69 	{ -1, 0 }
70 };
71 
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))
74 
75 static int
76 aw_reset_assert(device_t dev, intptr_t id, bool reset)
77 {
78 	struct aw_reset_softc *sc;
79 	uint32_t reg_value;
80 
81 	sc = device_get_softc(dev);
82 
83 	mtx_lock(&sc->mtx);
84 	reg_value = RESET_READ(sc, RESET_OFFSET(id));
85 	if (reset)
86 		reg_value &= ~(1 << RESET_SHIFT(id));
87 	else
88 		reg_value |= (1 << RESET_SHIFT(id));
89 	RESET_WRITE(sc, RESET_OFFSET(id), reg_value);
90 	mtx_unlock(&sc->mtx);
91 
92 	return (0);
93 }
94 
95 static int
96 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
97 {
98 	struct aw_reset_softc *sc;
99 	uint32_t reg_value;
100 
101 	sc = device_get_softc(dev);
102 
103 	mtx_lock(&sc->mtx);
104 	reg_value = RESET_READ(sc, RESET_OFFSET(id));
105 	mtx_unlock(&sc->mtx);
106 
107 	*reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true;
108 
109 	return (0);
110 }
111 
112 static int
113 aw_reset_probe(device_t dev)
114 {
115 	if (!ofw_bus_status_okay(dev))
116 		return (ENXIO);
117 
118 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
119 		return (ENXIO);
120 
121 	device_set_desc(dev, "Allwinner Module Resets");
122 	return (BUS_PROBE_DEFAULT);
123 }
124 
125 static int
126 aw_reset_attach(device_t dev)
127 {
128 	struct aw_reset_softc *sc;
129 
130 	sc = device_get_softc(dev);
131 
132 	if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) {
133 		device_printf(dev, "cannot allocate resources for device\n");
134 		return (ENXIO);
135 	}
136 
137 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
138 
139 	hwreset_register_ofw_provider(dev);
140 
141 	return (0);
142 }
143 
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),
148 
149 	/* Reset interface */
150 	DEVMETHOD(hwreset_assert,	aw_reset_assert),
151 	DEVMETHOD(hwreset_is_asserted,	aw_reset_is_asserted),
152 
153 	DEVMETHOD_END
154 };
155 
156 static driver_t aw_reset_driver = {
157 	"aw_reset",
158 	aw_reset_methods,
159 	sizeof(struct aw_reset_softc),
160 };
161 
162 static devclass_t aw_reset_devclass;
163 
164 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, aw_reset_devclass,
165     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
166 MODULE_VERSION(aw_reset, 1);
167