1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Michal Meloun
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * This is a generic syscon driver, whose purpose is to provide access to
31  * various unrelated bits packed in a single register space. It is usually used
32  * as a fallback to more specific driver, but works well enough for simple
33  * access.
34  */
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/rman.h>
45 
46 #include <machine/bus.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include "syscon_if.h"
52 #include "syscon.h"
53 #include "syscon_generic.h"
54 
55 MALLOC_DECLARE(M_SYSCON);
56 
57 static uint32_t syscon_generic_unlocked_read_4(struct syscon *syscon,
58     bus_size_t offset);
59 static int syscon_generic_unlocked_write_4(struct syscon *syscon,
60     bus_size_t offset, uint32_t val);
61 static int syscon_generic_unlocked_modify_4(struct syscon *syscon,
62     bus_size_t offset, uint32_t clear_bits, uint32_t set_bits);
63 static int syscon_generic_detach(device_t dev);
64 /*
65  * Generic syscon driver (FDT)
66  */
67 static struct ofw_compat_data compat_data[] = {
68 	{"syscon",	1},
69 	{NULL,		0}
70 };
71 
72 #define SYSCON_LOCK(_sc)		mtx_lock_spin(&(_sc)->mtx)
73 #define	SYSCON_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->mtx)
74 #define SYSCON_LOCK_INIT(_sc)		mtx_init(&(_sc)->mtx,		\
75 	    device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN)
76 #define SYSCON_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->mtx);
77 #define SYSCON_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_OWNED);
78 #define SYSCON_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
79 
80 static syscon_method_t syscon_generic_methods[] = {
81 	SYSCONMETHOD(syscon_unlocked_read_4,  syscon_generic_unlocked_read_4),
82 	SYSCONMETHOD(syscon_unlocked_write_4, syscon_generic_unlocked_write_4),
83 	SYSCONMETHOD(syscon_unlocked_modify_4, syscon_generic_unlocked_modify_4),
84 
85 	SYSCONMETHOD_END
86 };
87 DEFINE_CLASS_1(syscon_generic, syscon_generic_class, syscon_generic_methods,
88     0, syscon_class);
89 
90 static uint32_t
syscon_generic_unlocked_read_4(struct syscon * syscon,bus_size_t offset)91 syscon_generic_unlocked_read_4(struct syscon *syscon, bus_size_t offset)
92 {
93 	struct syscon_generic_softc *sc;
94 	uint32_t val;
95 
96 	sc = device_get_softc(syscon->pdev);
97 	SYSCON_ASSERT_LOCKED(sc);
98 	val = bus_read_4(sc->mem_res, offset);
99 	return (val);
100 }
101 
102 static int
syscon_generic_unlocked_write_4(struct syscon * syscon,bus_size_t offset,uint32_t val)103 syscon_generic_unlocked_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val)
104 {
105 	struct syscon_generic_softc *sc;
106 
107 	sc = device_get_softc(syscon->pdev);
108 	SYSCON_ASSERT_LOCKED(sc);
109 	bus_write_4(sc->mem_res, offset, val);
110 	return (0);
111 }
112 
113 static int
syscon_generic_unlocked_modify_4(struct syscon * syscon,bus_size_t offset,uint32_t clear_bits,uint32_t set_bits)114 syscon_generic_unlocked_modify_4(struct syscon *syscon, bus_size_t offset,
115     uint32_t clear_bits, uint32_t set_bits)
116 {
117 	struct syscon_generic_softc *sc;
118 	uint32_t val;
119 
120 	sc = device_get_softc(syscon->pdev);
121 	SYSCON_ASSERT_LOCKED(sc);
122 	val = bus_read_4(sc->mem_res, offset);
123 	val &= ~clear_bits;
124 	val |= set_bits;
125 	bus_write_4(sc->mem_res, offset, val);
126 	return (0);
127 }
128 
129 static void
syscon_generic_lock(device_t dev)130 syscon_generic_lock(device_t dev)
131 {
132 	struct syscon_generic_softc *sc;
133 
134 	sc = device_get_softc(dev);
135 	SYSCON_LOCK(sc);
136 }
137 
138 static void
syscon_generic_unlock(device_t dev)139 syscon_generic_unlock(device_t dev)
140 {
141 	struct syscon_generic_softc *sc;
142 
143 	sc = device_get_softc(dev);
144 	SYSCON_UNLOCK(sc);
145 }
146 
147 static int
syscon_generic_probe(device_t dev)148 syscon_generic_probe(device_t dev)
149 {
150 
151 	if (!ofw_bus_status_okay(dev))
152 		return (ENXIO);
153 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
154 		return (ENXIO);
155 
156 	device_set_desc(dev, "syscon");
157 	if (!bootverbose)
158 		device_quiet(dev);
159 
160 	return (BUS_PROBE_GENERIC);
161 }
162 
163 static int
syscon_generic_attach(device_t dev)164 syscon_generic_attach(device_t dev)
165 {
166 	struct syscon_generic_softc *sc;
167 	int rid, rv;
168 
169 	sc = device_get_softc(dev);
170 	sc->dev = dev;
171 	rid = 0;
172 
173 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
174 	    RF_ACTIVE);
175 	if (sc->mem_res == NULL) {
176 		device_printf(dev, "Cannot allocate memory resource\n");
177 		return (ENXIO);
178 	}
179 
180 	SYSCON_LOCK_INIT(sc);
181 	sc->syscon = syscon_create_ofw_node(dev, &syscon_generic_class,
182 		ofw_bus_get_node(dev));
183 	if (sc->syscon == NULL) {
184 		device_printf(dev, "Failed to create/register syscon\n");
185 		syscon_generic_detach(dev);
186 		return (ENXIO);
187 	}
188 	if (ofw_bus_is_compatible(dev, "simple-bus")) {
189 		rv = simplebus_attach_impl(sc->dev);
190 		if (rv != 0) {
191 			device_printf(dev, "Failed to create simplebus\n");
192 			syscon_generic_detach(dev);
193 			return (ENXIO);
194 		}
195 		sc->simplebus_attached = true;
196 	}
197 
198 	return (bus_generic_attach(dev));
199 }
200 
201 static int
syscon_generic_detach(device_t dev)202 syscon_generic_detach(device_t dev)
203 {
204 	struct syscon_generic_softc *sc;
205 
206 	sc = device_get_softc(dev);
207 	if (sc->syscon != NULL) {
208 		syscon_unregister(sc->syscon);
209 		free(sc->syscon, M_SYSCON);
210 	}
211 	if (sc->simplebus_attached)
212 		simplebus_detach(dev);
213 	SYSCON_LOCK_DESTROY(sc);
214 
215 	if (sc->mem_res != NULL)
216 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
217 	return (0);
218 }
219 
220 static device_method_t syscon_generic_dmethods[] = {
221 	/* Device interface */
222 	DEVMETHOD(device_probe,		syscon_generic_probe),
223 	DEVMETHOD(device_attach,	syscon_generic_attach),
224 	DEVMETHOD(device_detach,	syscon_generic_detach),
225 
226 	DEVMETHOD(syscon_device_lock,	syscon_generic_lock),
227 	DEVMETHOD(syscon_device_unlock,	syscon_generic_unlock),
228 
229 	DEVMETHOD_END
230 };
231 
232 DEFINE_CLASS_1(syscon_generic_dev, syscon_generic_driver, syscon_generic_dmethods,
233     sizeof(struct syscon_generic_softc), simplebus_driver);
234 
235 EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, 0, 0,
236     BUS_PASS_DEFAULT);
237 MODULE_VERSION(syscon_generic, 1);
238