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