]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fdt/simple_mfd.c
openssh: update to OpenSSH v8.7p1
[FreeBSD/FreeBSD.git] / sys / dev / fdt / simple_mfd.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40
41 #include <machine/bus.h>
42
43 #include <dev/fdt/simplebus.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <dev/fdt/simple_mfd.h>
49
50 device_t simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
51     const char *name, int unit, struct simplebus_devinfo *di);
52 struct simplebus_devinfo *simple_mfd_setup_dinfo(device_t dev, phandle_t node,
53     struct simplebus_devinfo *di);
54
55 #include "syscon_if.h"
56 #include <dev/extres/syscon/syscon.h>
57
58 MALLOC_DECLARE(M_SYSCON);
59
60 static uint32_t simple_mfd_syscon_read_4(struct syscon *syscon,
61     bus_size_t offset);
62 static int simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
63     uint32_t val);
64 static int simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
65     uint32_t clear_bits, uint32_t set_bits);
66
67 #define SYSCON_LOCK(_sc)                mtx_lock_spin(&(_sc)->mtx)
68 #define SYSCON_UNLOCK(_sc)              mtx_unlock_spin(&(_sc)->mtx)
69 #define SYSCON_LOCK_INIT(_sc)           mtx_init(&(_sc)->mtx,   \
70     device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN)
71 #define SYSCON_LOCK_DESTROY(_sc)        mtx_destroy(&(_sc)->mtx);
72 #define SYSCON_ASSERT_LOCKED(_sc)       mtx_assert(&(_sc)->mtx, MA_OWNED);
73 #define SYSCON_ASSERT_UNLOCKED(_sc)     mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
74
75 static syscon_method_t simple_mfd_syscon_methods[] = {
76         SYSCONMETHOD(syscon_unlocked_read_4,    simple_mfd_syscon_read_4),
77         SYSCONMETHOD(syscon_unlocked_write_4,   simple_mfd_syscon_write_4),
78         SYSCONMETHOD(syscon_unlocked_modify_4,  simple_mfd_syscon_modify_4),
79
80         SYSCONMETHOD_END
81 };
82 DEFINE_CLASS_1(simple_mfd_syscon, simple_mfd_syscon_class,
83     simple_mfd_syscon_methods, 0, syscon_class);
84
85 static uint32_t
86 simple_mfd_syscon_read_4(struct syscon *syscon, bus_size_t offset)
87 {
88         struct simple_mfd_softc *sc;
89         uint32_t val;
90
91         sc = device_get_softc(syscon->pdev);
92         SYSCON_ASSERT_LOCKED(sc);;
93         val = bus_read_4(sc->mem_res, offset);
94         return (val);
95 }
96
97 static int
98 simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
99     uint32_t val)
100 {
101         struct simple_mfd_softc *sc;
102
103         sc = device_get_softc(syscon->pdev);
104         SYSCON_ASSERT_LOCKED(sc);
105         bus_write_4(sc->mem_res, offset, val);
106         return (0);
107 }
108
109 static int
110 simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
111     uint32_t clear_bits, uint32_t set_bits)
112 {
113         struct simple_mfd_softc *sc;
114         uint32_t val;
115
116         sc = device_get_softc(syscon->pdev);
117         SYSCON_ASSERT_LOCKED(sc);
118         val = bus_read_4(sc->mem_res, offset);
119         val &= ~clear_bits;
120         val |= set_bits;
121         bus_write_4(sc->mem_res, offset, val);
122         return (0);
123 }
124
125 static int
126 simple_mfd_syscon_get_handle(device_t dev, struct syscon **syscon)
127 {
128         struct simple_mfd_softc *sc;
129
130         sc = device_get_softc(dev);
131         *syscon = sc->syscon;
132         if (*syscon == NULL)
133                 return (ENODEV);
134         return (0);
135 }
136
137 static void
138 simple_mfd_syscon_lock(device_t dev)
139 {
140         struct simple_mfd_softc *sc;
141
142         sc = device_get_softc(dev);
143         SYSCON_LOCK(sc);
144 }
145
146 static void
147 simple_mfd_syscon_unlock(device_t dev)
148 {
149         struct simple_mfd_softc *sc;
150
151         sc = device_get_softc(dev);
152         SYSCON_UNLOCK(sc);
153 }
154
155 static int
156 simple_mfd_probe(device_t dev)
157 {
158
159         if (!ofw_bus_status_okay(dev))
160                 return (ENXIO);
161         if (!ofw_bus_is_compatible(dev, "simple-mfd"))
162                 return (ENXIO);
163
164         device_set_desc(dev, "Simple MFD (Multi-Functions Device)");
165
166         return (BUS_PROBE_GENERIC);
167 }
168
169 static int
170 simple_mfd_attach(device_t dev)
171 {
172         struct simple_mfd_softc *sc;
173         phandle_t node, child;
174         device_t cdev;
175         int rid;
176
177         sc = device_get_softc(dev);
178         node = ofw_bus_get_node(dev);
179
180         sc->dev = dev;
181         rid = 0;
182
183         /* Parse address-cells and size-cells from the parent node as a fallback */
184         if (OF_getencprop(node, "#address-cells", &sc->sc.acells,
185             sizeof(sc->sc.acells)) == -1) {
186                 if (OF_getencprop(OF_parent(node), "#address-cells", &sc->sc.acells,
187                     sizeof(sc->sc.acells)) == -1) {
188                         sc->sc.acells = 2;
189                 }
190         }
191         if (OF_getencprop(node, "#size-cells", &sc->sc.scells,
192             sizeof(sc->sc.scells)) == -1) {
193                 if (OF_getencprop(OF_parent(node), "#size-cells", &sc->sc.scells,
194                     sizeof(sc->sc.scells)) == -1) {
195                         sc->sc.scells = 1;
196                 }
197         }
198
199         /* If the node has a ranges prop, parse it so children mapping will be done correctly */
200         if (OF_hasprop(node, "ranges")) {
201                 if (simplebus_fill_ranges(node, &sc->sc) < 0) {
202                         device_printf(dev, "could not get ranges\n");
203                         return (ENXIO);
204                 }
205         }
206
207         /* Attach child devices */
208         for (child = OF_child(node); child > 0; child = OF_peer(child)) {
209                 cdev = simple_mfd_add_device(dev, child, 0, NULL, -1, NULL);
210                 if (cdev != NULL)
211                         device_probe_and_attach(cdev);
212         }
213
214         if (ofw_bus_is_compatible(dev, "syscon")) {
215                 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
216                     RF_ACTIVE);
217                 if (sc->mem_res == NULL) {
218                         device_printf(dev,
219                             "Cannot allocate memory resource\n");
220                         return (ENXIO);
221                 }
222
223                 SYSCON_LOCK_INIT(sc);
224                 sc->syscon = syscon_create_ofw_node(dev,
225                     &simple_mfd_syscon_class, ofw_bus_get_node(dev));
226                 if (sc->syscon == NULL) {
227                         device_printf(dev,
228                             "Failed to create/register syscon\n");
229                         return (ENXIO);
230                 }
231         }
232         return (bus_generic_attach(dev));
233 }
234
235 static int
236 simple_mfd_detach(device_t dev)
237 {
238         struct simple_mfd_softc *sc;
239
240         sc = device_get_softc(dev);
241         if (ofw_bus_is_compatible(dev, "syscon")) {
242                 if (sc->syscon != NULL) {
243                         syscon_unregister(sc->syscon);
244                         free(sc->syscon, M_SYSCON);
245                 }
246
247                 SYSCON_LOCK_DESTROY(sc);
248
249                 if (sc->mem_res != NULL)
250                         bus_release_resource(dev, SYS_RES_MEMORY, 0,
251                             sc->mem_res);
252         }
253         return (0);
254 }
255
256 struct simplebus_devinfo *
257 simple_mfd_setup_dinfo(device_t dev, phandle_t node,
258     struct simplebus_devinfo *di)
259 {
260         struct simplebus_softc *sc;
261         struct simplebus_devinfo *ndi;
262
263         sc = device_get_softc(dev);
264         if (di == NULL)
265                 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
266         else
267                 ndi = di;
268         if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
269                 if (di == NULL)
270                         free(ndi, M_DEVBUF);
271                 return (NULL);
272         }
273
274         /* reg resources is from the parent but interrupts is on the node itself */
275         resource_list_init(&ndi->rl);
276         ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl);
277         ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
278
279         return (ndi);
280 }
281
282 device_t
283 simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
284     const char *name, int unit, struct simplebus_devinfo *di)
285 {
286         struct simplebus_devinfo *ndi;
287         device_t cdev;
288
289         if ((ndi = simple_mfd_setup_dinfo(dev, node, di)) == NULL)
290                 return (NULL);
291         cdev = device_add_child_ordered(dev, order, name, unit);
292         if (cdev == NULL) {
293                 device_printf(dev, "<%s>: device_add_child failed\n",
294                     ndi->obdinfo.obd_name);
295                 resource_list_free(&ndi->rl);
296                 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
297                 if (di == NULL)
298                         free(ndi, M_DEVBUF);
299                 return (NULL);
300         }
301         device_set_ivars(cdev, ndi);
302
303         return(cdev);
304 }
305
306 static device_method_t simple_mfd_methods[] = {
307         /* syscon interface */
308         DEVMETHOD(syscon_get_handle,    simple_mfd_syscon_get_handle),
309         DEVMETHOD(syscon_device_lock,   simple_mfd_syscon_lock),
310         DEVMETHOD(syscon_device_unlock, simple_mfd_syscon_unlock),
311
312         /* Device interface */
313         DEVMETHOD(device_probe,         simple_mfd_probe),
314         DEVMETHOD(device_attach,        simple_mfd_attach),
315         DEVMETHOD(device_detach,        simple_mfd_detach),
316
317         DEVMETHOD_END
318 };
319
320 DEFINE_CLASS_1(simple_mfd, simple_mfd_driver, simple_mfd_methods,
321   sizeof(struct simple_mfd_softc), simplebus_driver);
322
323 static devclass_t simple_mfd_devclass;
324
325 EARLY_DRIVER_MODULE(simple_mfd, simplebus, simple_mfd_driver,
326     simple_mfd_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
327 MODULE_VERSION(simple_mfd, 1);