]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/central/central.c
Update our devicetree to 4.19 for arm and arm64
[FreeBSD/FreeBSD.git] / sys / sparc64 / central / central.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Jake Burkholder.
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 #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/malloc.h>
37 #include <sys/module.h>
38
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45
46 #include <sys/rman.h>
47
48 #include <sparc64/sbus/ofw_sbus.h>
49
50 struct central_devinfo {
51         struct ofw_bus_devinfo  cdi_obdinfo;
52         struct resource_list    cdi_rl;
53 };
54
55 struct central_softc {
56         int                     sc_nrange;
57         struct sbus_ranges      *sc_ranges;
58 };
59
60 static device_probe_t central_probe;
61 static device_attach_t central_attach;
62 static bus_print_child_t central_print_child;
63 static bus_probe_nomatch_t central_probe_nomatch;
64 static bus_alloc_resource_t central_alloc_resource;
65 static bus_adjust_resource_t central_adjust_resource;
66 static bus_get_resource_list_t central_get_resource_list;
67 static ofw_bus_get_devinfo_t central_get_devinfo;
68
69 static int central_print_res(struct central_devinfo *);
70
71 static device_method_t central_methods[] = {
72         /* Device interface */
73         DEVMETHOD(device_probe,         central_probe),
74         DEVMETHOD(device_attach,        central_attach),
75         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
76         DEVMETHOD(device_suspend,       bus_generic_suspend),
77         DEVMETHOD(device_resume,        bus_generic_resume),
78
79         /* Bus interface */
80         DEVMETHOD(bus_print_child,      central_print_child),
81         DEVMETHOD(bus_probe_nomatch,    central_probe_nomatch),
82         DEVMETHOD(bus_alloc_resource,   central_alloc_resource),
83         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
84         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
85         DEVMETHOD(bus_adjust_resource,  central_adjust_resource),
86         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
87         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
88         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
89         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
90         DEVMETHOD(bus_get_resource_list, central_get_resource_list),
91         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
92
93         /* ofw_bus interface */
94         DEVMETHOD(ofw_bus_get_devinfo,  central_get_devinfo),
95         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
96         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
97         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
98         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
99         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
100
101         DEVMETHOD_END
102 };
103
104 static driver_t central_driver = {
105         "central",
106         central_methods,
107         sizeof(struct central_softc),
108 };
109
110 static devclass_t central_devclass;
111
112 EARLY_DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0,
113     BUS_PASS_BUS);
114 MODULE_DEPEND(fhc, nexus, 1, 1, 1);
115 MODULE_VERSION(central, 1);
116
117 static int
118 central_probe(device_t dev)
119 {
120
121         if (strcmp(ofw_bus_get_name(dev), "central") == 0) {
122                 device_set_desc(dev, "central");
123                 return (0);
124         }
125         return (ENXIO);
126 }
127
128 static int
129 central_attach(device_t dev)
130 {
131         struct central_devinfo *cdi;
132         struct sbus_regs *reg;
133         struct central_softc *sc;
134         phandle_t child;
135         phandle_t node;
136         device_t cdev;
137         int nreg;
138         int i;
139
140         sc = device_get_softc(dev);
141         node = ofw_bus_get_node(dev);
142
143         sc->sc_nrange = OF_getprop_alloc_multi(node, "ranges",
144             sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
145         if (sc->sc_nrange == -1) {
146                 device_printf(dev, "can't get ranges\n");
147                 return (ENXIO);
148         }
149
150         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
151                 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
152                 if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
153                         free(cdi, M_DEVBUF);
154                         continue;
155                 }
156                 nreg = OF_getprop_alloc_multi(child, "reg", sizeof(*reg),
157                     (void **)&reg);
158                 if (nreg == -1) {
159                         device_printf(dev, "<%s>: incomplete\n",
160                             cdi->cdi_obdinfo.obd_name);
161                         ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
162                         free(cdi, M_DEVBUF);
163                         continue;
164                 }
165                 resource_list_init(&cdi->cdi_rl);
166                 for (i = 0; i < nreg; i++)
167                         resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
168                             reg[i].sbr_offset, reg[i].sbr_offset +
169                             reg[i].sbr_size, reg[i].sbr_size);
170                 OF_prop_free(reg);
171                 cdev = device_add_child(dev, NULL, -1);
172                 if (cdev == NULL) {
173                         device_printf(dev, "<%s>: device_add_child failed\n",
174                             cdi->cdi_obdinfo.obd_name);
175                         resource_list_free(&cdi->cdi_rl);
176                         ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
177                         free(cdi, M_DEVBUF);
178                         continue;
179                 }
180                 device_set_ivars(cdev, cdi);
181         }
182
183         return (bus_generic_attach(dev));
184 }
185
186 static int
187 central_adjust_resource(device_t bus __unused, device_t child __unused,
188     int type __unused, struct resource *r __unused, rman_res_t start __unused,
189     rman_res_t end __unused)
190 {
191
192         return (ENXIO);
193 }
194
195 static int
196 central_print_child(device_t dev, device_t child)
197 {
198         int rv;
199
200         rv = bus_print_child_header(dev, child);
201         rv += central_print_res(device_get_ivars(child));
202         rv += bus_print_child_footer(dev, child);
203         return (rv);
204 }
205
206 static void
207 central_probe_nomatch(device_t dev, device_t child)
208 {
209         const char *type;
210
211         device_printf(dev, "<%s>", ofw_bus_get_name(child));
212         central_print_res(device_get_ivars(child));
213         type = ofw_bus_get_type(child);
214         printf(" type %s (no driver attached)\n",
215             type != NULL ? type : "unknown");
216 }
217
218 static struct resource *
219 central_alloc_resource(device_t bus, device_t child, int type, int *rid,
220     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
221 {
222         struct resource_list *rl;
223         struct resource_list_entry *rle;
224         struct central_softc *sc;
225         struct resource *res;
226         bus_addr_t coffset;
227         bus_addr_t cend;
228         bus_addr_t phys;
229         int isdefault;
230         int passthrough;
231         int i;
232
233         isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
234         passthrough = (device_get_parent(child) != bus);
235         res = NULL;
236         rle = NULL;
237         rl = BUS_GET_RESOURCE_LIST(bus, child);
238         sc = device_get_softc(bus);
239         switch (type) {
240         case SYS_RES_IRQ:
241                 return (resource_list_alloc(rl, bus, child, type, rid, start,
242                     end, count, flags));
243         case SYS_RES_MEMORY:
244                 if (!passthrough) {
245                         rle = resource_list_find(rl, type, *rid);
246                         if (rle == NULL)
247                                 return (NULL);
248                         if (rle->res != NULL)
249                                 panic("%s: resource entry is busy", __func__);
250                         if (isdefault) {
251                                 start = rle->start;
252                                 count = ulmax(count, rle->count);
253                                 end = ulmax(rle->end, start + count - 1);
254                         }
255                 }
256                 for (i = 0; i < sc->sc_nrange; i++) {
257                         coffset = sc->sc_ranges[i].coffset;
258                         cend = coffset + sc->sc_ranges[i].size - 1;
259                         if (start >= coffset && end <= cend) {
260                                 start -= coffset;
261                                 end -= coffset;
262                                 phys = sc->sc_ranges[i].poffset |
263                                     ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
264                                 res = bus_generic_alloc_resource(bus, child,
265                                     type, rid, phys + start, phys + end,
266                                     count, flags);
267                                 if (!passthrough)
268                                         rle->res = res;
269                                 break;
270                         }
271                 }
272                 break;
273         }
274         return (res);
275 }
276
277 static struct resource_list *
278 central_get_resource_list(device_t bus, device_t child)
279 {
280         struct central_devinfo *cdi;
281
282         cdi = device_get_ivars(child);
283         return (&cdi->cdi_rl);
284 }
285
286 static const struct ofw_bus_devinfo *
287 central_get_devinfo(device_t bus, device_t child)
288 {
289         struct central_devinfo *cdi;
290
291         cdi = device_get_ivars(child);
292         return (&cdi->cdi_obdinfo);
293 }
294
295 static int
296 central_print_res(struct central_devinfo *cdi)
297 {
298
299         return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
300             "%#jx"));
301 }