xref: /freebsd/sys/dev/syscon/syscon_generic.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include "syscon_if.h"
51 #include "syscon.h"
52 #include "syscon_generic.h"
53 
54 MALLOC_DECLARE(M_SYSCON);
55 
56 static uint32_t syscon_generic_unlocked_read_4(struct syscon *syscon,
57     bus_size_t offset);
58 static int syscon_generic_unlocked_write_4(struct syscon *syscon,
59     bus_size_t offset, uint32_t val);
60 static int syscon_generic_unlocked_modify_4(struct syscon *syscon,
61     bus_size_t offset, uint32_t clear_bits, uint32_t set_bits);
62 static int syscon_generic_detach(device_t dev);
63 /*
64  * Generic syscon driver (FDT)
65  */
66 static struct ofw_compat_data compat_data[] = {
67 	{"syscon",	1},
68 	{NULL,		0}
69 };
70 
71 #define SYSCON_LOCK(_sc)		mtx_lock_spin(&(_sc)->mtx)
72 #define	SYSCON_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->mtx)
73 #define SYSCON_LOCK_INIT(_sc)		mtx_init(&(_sc)->mtx,		\
74 	    device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN)
75 #define SYSCON_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->mtx);
76 #define SYSCON_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_OWNED);
77 #define SYSCON_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
78 
79 static syscon_method_t syscon_generic_methods[] = {
80 	SYSCONMETHOD(syscon_unlocked_read_4,  syscon_generic_unlocked_read_4),
81 	SYSCONMETHOD(syscon_unlocked_write_4, syscon_generic_unlocked_write_4),
82 	SYSCONMETHOD(syscon_unlocked_modify_4, syscon_generic_unlocked_modify_4),
83 
84 	SYSCONMETHOD_END
85 };
86 DEFINE_CLASS_1(syscon_generic, syscon_generic_class, syscon_generic_methods,
87     0, syscon_class);
88 
89 static uint32_t
90 syscon_generic_unlocked_read_4(struct syscon *syscon, bus_size_t offset)
91 {
92 	struct syscon_generic_softc *sc;
93 	uint32_t val;
94 
95 	sc = device_get_softc(syscon->pdev);
96 	SYSCON_ASSERT_LOCKED(sc);
97 	val = bus_read_4(sc->mem_res, offset);
98 	return (val);
99 }
100 
101 static int
102 syscon_generic_unlocked_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val)
103 {
104 	struct syscon_generic_softc *sc;
105 
106 	sc = device_get_softc(syscon->pdev);
107 	SYSCON_ASSERT_LOCKED(sc);
108 	bus_write_4(sc->mem_res, offset, val);
109 	return (0);
110 }
111 
112 static int
113 syscon_generic_unlocked_modify_4(struct syscon *syscon, bus_size_t offset,
114     uint32_t clear_bits, uint32_t set_bits)
115 {
116 	struct syscon_generic_softc *sc;
117 	uint32_t val;
118 
119 	sc = device_get_softc(syscon->pdev);
120 	SYSCON_ASSERT_LOCKED(sc);
121 	val = bus_read_4(sc->mem_res, offset);
122 	val &= ~clear_bits;
123 	val |= set_bits;
124 	bus_write_4(sc->mem_res, offset, val);
125 	return (0);
126 }
127 
128 static void
129 syscon_generic_lock(device_t dev)
130 {
131 	struct syscon_generic_softc *sc;
132 
133 	sc = device_get_softc(dev);
134 	SYSCON_LOCK(sc);
135 }
136 
137 static void
138 syscon_generic_unlock(device_t dev)
139 {
140 	struct syscon_generic_softc *sc;
141 
142 	sc = device_get_softc(dev);
143 	SYSCON_UNLOCK(sc);
144 }
145 
146 static int
147 syscon_generic_probe(device_t dev)
148 {
149 
150 	if (!ofw_bus_status_okay(dev))
151 		return (ENXIO);
152 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
153 		return (ENXIO);
154 
155 	device_set_desc(dev, "syscon");
156 	if (!bootverbose)
157 		device_quiet(dev);
158 
159 	return (BUS_PROBE_GENERIC);
160 }
161 
162 static int
163 syscon_generic_attach(device_t dev)
164 {
165 	struct syscon_generic_softc *sc;
166 	int rid, rv;
167 
168 	sc = device_get_softc(dev);
169 	sc->dev = dev;
170 	rid = 0;
171 
172 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
173 	    RF_ACTIVE);
174 	if (sc->mem_res == NULL) {
175 		device_printf(dev, "Cannot allocate memory resource\n");
176 		return (ENXIO);
177 	}
178 
179 	SYSCON_LOCK_INIT(sc);
180 	sc->syscon = syscon_create_ofw_node(dev, &syscon_generic_class,
181 		ofw_bus_get_node(dev));
182 	if (sc->syscon == NULL) {
183 		device_printf(dev, "Failed to create/register syscon\n");
184 		syscon_generic_detach(dev);
185 		return (ENXIO);
186 	}
187 	if (ofw_bus_is_compatible(dev, "simple-bus")) {
188 		rv = simplebus_attach_impl(sc->dev);
189 		if (rv != 0) {
190 			device_printf(dev, "Failed to create simplebus\n");
191 			syscon_generic_detach(dev);
192 			return (ENXIO);
193 		}
194 		sc->simplebus_attached = true;
195 	}
196 
197 	return (bus_generic_attach(dev));
198 }
199 
200 static int
201 syscon_generic_detach(device_t dev)
202 {
203 	struct syscon_generic_softc *sc;
204 
205 	sc = device_get_softc(dev);
206 	if (sc->syscon != NULL) {
207 		syscon_unregister(sc->syscon);
208 		free(sc->syscon, M_SYSCON);
209 	}
210 	if (sc->simplebus_attached)
211 		simplebus_detach(dev);
212 	SYSCON_LOCK_DESTROY(sc);
213 
214 	if (sc->mem_res != NULL)
215 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
216 	return (0);
217 }
218 
219 static device_method_t syscon_generic_dmethods[] = {
220 	/* Device interface */
221 	DEVMETHOD(device_probe,		syscon_generic_probe),
222 	DEVMETHOD(device_attach,	syscon_generic_attach),
223 	DEVMETHOD(device_detach,	syscon_generic_detach),
224 
225 	DEVMETHOD(syscon_device_lock,	syscon_generic_lock),
226 	DEVMETHOD(syscon_device_unlock,	syscon_generic_unlock),
227 
228 	DEVMETHOD_END
229 };
230 
231 DEFINE_CLASS_1(syscon_generic_dev, syscon_generic_driver, syscon_generic_dmethods,
232     sizeof(struct syscon_generic_softc), simplebus_driver);
233 
234 EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, 0, 0,
235     BUS_PASS_DEFAULT);
236 MODULE_VERSION(syscon_generic, 1);
237