xref: /freebsd/sys/arm/allwinner/aw_reset.c (revision 315ee00f)
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/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/rman.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <machine/bus.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include <dev/extres/hwreset/hwreset.h>
45 
46 #include "hwreset_if.h"
47 
48 #define	RESET_OFFSET(index)	((index / 32) * 4)
49 #define	RESET_SHIFT(index)	(index % 32)
50 
51 static struct ofw_compat_data compat_data[] = {
52 	{ "allwinner,sun6i-a31-ahb1-reset",	1 },
53 	{ "allwinner,sun6i-a31-clock-reset",	1 },
54 	{ NULL,					0 }
55 };
56 
57 struct aw_reset_softc {
58 	struct resource		*res;
59 	struct mtx		mtx;
60 };
61 
62 static struct resource_spec aw_reset_spec[] = {
63 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
64 	{ -1, 0 }
65 };
66 
67 #define	RESET_READ(sc, reg)		bus_read_4((sc)->res, (reg))
68 #define	RESET_WRITE(sc, reg, val)	bus_write_4((sc)->res, (reg), (val))
69 
70 static int
71 aw_reset_assert(device_t dev, intptr_t id, bool reset)
72 {
73 	struct aw_reset_softc *sc;
74 	uint32_t reg_value;
75 
76 	sc = device_get_softc(dev);
77 
78 	mtx_lock(&sc->mtx);
79 	reg_value = RESET_READ(sc, RESET_OFFSET(id));
80 	if (reset)
81 		reg_value &= ~(1 << RESET_SHIFT(id));
82 	else
83 		reg_value |= (1 << RESET_SHIFT(id));
84 	RESET_WRITE(sc, RESET_OFFSET(id), reg_value);
85 	mtx_unlock(&sc->mtx);
86 
87 	return (0);
88 }
89 
90 static int
91 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
92 {
93 	struct aw_reset_softc *sc;
94 	uint32_t reg_value;
95 
96 	sc = device_get_softc(dev);
97 
98 	mtx_lock(&sc->mtx);
99 	reg_value = RESET_READ(sc, RESET_OFFSET(id));
100 	mtx_unlock(&sc->mtx);
101 
102 	*reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true;
103 
104 	return (0);
105 }
106 
107 static int
108 aw_reset_probe(device_t dev)
109 {
110 	if (!ofw_bus_status_okay(dev))
111 		return (ENXIO);
112 
113 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
114 		return (ENXIO);
115 
116 	device_set_desc(dev, "Allwinner Module Resets");
117 	return (BUS_PROBE_DEFAULT);
118 }
119 
120 static int
121 aw_reset_attach(device_t dev)
122 {
123 	struct aw_reset_softc *sc;
124 
125 	sc = device_get_softc(dev);
126 
127 	if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) {
128 		device_printf(dev, "cannot allocate resources for device\n");
129 		return (ENXIO);
130 	}
131 
132 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
133 
134 	hwreset_register_ofw_provider(dev);
135 
136 	return (0);
137 }
138 
139 static device_method_t aw_reset_methods[] = {
140 	/* Device interface */
141 	DEVMETHOD(device_probe,		aw_reset_probe),
142 	DEVMETHOD(device_attach,	aw_reset_attach),
143 
144 	/* Reset interface */
145 	DEVMETHOD(hwreset_assert,	aw_reset_assert),
146 	DEVMETHOD(hwreset_is_asserted,	aw_reset_is_asserted),
147 
148 	DEVMETHOD_END
149 };
150 
151 static driver_t aw_reset_driver = {
152 	"aw_reset",
153 	aw_reset_methods,
154 	sizeof(struct aw_reset_softc),
155 };
156 
157 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, 0, 0,
158     BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
159 MODULE_VERSION(aw_reset, 1);
160