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