]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fdt/simplebus.c
Adjust ofwbus and simplebus to attach at BUS_PASS_ORDER_MIDDLE, so that
[FreeBSD/FreeBSD.git] / sys / dev / fdt / simplebus.c
1 /*-
2  * Copyright (c) 2013 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/rman.h>
36
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40
41 struct simplebus_range {
42         uint64_t bus;
43         uint64_t host;
44         uint64_t size;
45 };
46
47 struct simplebus_softc {
48         device_t dev;
49         phandle_t node;
50
51         struct simplebus_range *ranges;
52         int nranges;
53
54         pcell_t acells, scells;
55 };
56
57 struct simplebus_devinfo {
58         struct ofw_bus_devinfo  obdinfo;
59         struct resource_list    rl;
60 };
61
62 /*
63  * Bus interface.
64  */
65 static int              simplebus_probe(device_t dev);
66 static int              simplebus_attach(device_t dev);
67 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
68     int *, u_long, u_long, u_long, u_int);
69 static void             simplebus_probe_nomatch(device_t bus, device_t child);
70 static int              simplebus_print_child(device_t bus, device_t child);
71
72 /*
73  * ofw_bus interface
74  */
75 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
76     device_t child);
77
78 /*
79  * local methods
80  */
81
82 static int simplebus_fill_ranges(phandle_t node,
83     struct simplebus_softc *sc);
84 static struct simplebus_devinfo *simplebus_setup_dinfo(device_t dev,
85     phandle_t node);
86
87 /*
88  * Driver methods.
89  */
90 static device_method_t  simplebus_methods[] = {
91         /* Device interface */
92         DEVMETHOD(device_probe,         simplebus_probe),
93         DEVMETHOD(device_attach,        simplebus_attach),
94
95         /* Bus interface */
96         DEVMETHOD(bus_print_child,      simplebus_print_child),
97         DEVMETHOD(bus_probe_nomatch,    simplebus_probe_nomatch),
98         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
99         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
100         DEVMETHOD(bus_alloc_resource,   simplebus_alloc_resource),
101         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
102         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
103         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
104         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
105         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
106
107         /* ofw_bus interface */
108         DEVMETHOD(ofw_bus_get_devinfo,  simplebus_get_devinfo),
109         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
110         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
111         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
112         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
113         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
114
115         DEVMETHOD_END
116 };
117
118 static driver_t simplebus_driver = {
119         "simplebus",
120         simplebus_methods,
121         sizeof(struct simplebus_softc)
122 };
123 static devclass_t simplebus_devclass;
124 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
125     0, 0, BUS_PASS_BUS);
126 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
127     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
128
129 static int
130 simplebus_probe(device_t dev)
131 {
132  
133         if (!ofw_bus_status_okay(dev))
134                 return (ENXIO);
135
136         if (!ofw_bus_is_compatible(dev, "simple-bus") &&
137             (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
138              "soc") != 0))
139                 return (ENXIO);
140
141         device_set_desc(dev, "Flattened device tree simple bus");
142
143         return (BUS_PROBE_GENERIC);
144 }
145
146 static int
147 simplebus_attach(device_t dev)
148 {
149         struct          simplebus_softc *sc;
150         struct          simplebus_devinfo *di;
151         phandle_t       node;
152         device_t        cdev;
153
154         node = ofw_bus_get_node(dev);
155         sc = device_get_softc(dev);
156
157         sc->dev = dev;
158         sc->node = node;
159
160         /*
161          * Some important numbers
162          */
163         sc->acells = 2;
164         OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
165         sc->scells = 1;
166         OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
167
168         if (simplebus_fill_ranges(node, sc) < 0) {
169                 device_printf(dev, "could not get ranges\n");
170                 return (ENXIO);
171         }
172
173         /*
174          * In principle, simplebus could have an interrupt map, but ignore that
175          * for now
176          */
177
178         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
179                 if ((di = simplebus_setup_dinfo(dev, node)) == NULL)
180                         continue;
181                 cdev = device_add_child(dev, NULL, -1);
182                 if (cdev == NULL) {
183                         device_printf(dev, "<%s>: device_add_child failed\n",
184                             di->obdinfo.obd_name);
185                         resource_list_free(&di->rl);
186                         ofw_bus_gen_destroy_devinfo(&di->obdinfo);
187                         free(di, M_DEVBUF);
188                         continue;
189                 }
190                 device_set_ivars(cdev, di);
191         }
192
193         return (bus_generic_attach(dev));
194 }
195
196 static int
197 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
198 {
199         int host_address_cells;
200         cell_t *base_ranges;
201         ssize_t nbase_ranges;
202         int err;
203         int i, j, k;
204
205         err = OF_searchencprop(OF_parent(node), "#address-cells",
206             &host_address_cells, sizeof(host_address_cells));
207         if (err <= 0)
208                 return (-1);
209
210         nbase_ranges = OF_getproplen(node, "ranges");
211         if (nbase_ranges < 0)
212                 return (-1);
213         sc->nranges = nbase_ranges / sizeof(cell_t) /
214             (sc->acells + host_address_cells + sc->scells);
215         if (sc->nranges == 0)
216                 return (0);
217
218         sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
219             M_DEVBUF, M_WAITOK);
220         base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
221         OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
222
223         for (i = 0, j = 0; i < sc->nranges; i++) {
224                 sc->ranges[i].bus = 0;
225                 for (k = 0; k < sc->acells; k++) {
226                         sc->ranges[i].bus <<= 32;
227                         sc->ranges[i].bus |= base_ranges[j++];
228                 }
229                 sc->ranges[i].host = 0;
230                 for (k = 0; k < host_address_cells; k++) {
231                         sc->ranges[i].host <<= 32;
232                         sc->ranges[i].host |= base_ranges[j++];
233                 }
234                 sc->ranges[i].size = 0;
235                 for (k = 0; k < sc->scells; k++) {
236                         sc->ranges[i].size <<= 32;
237                         sc->ranges[i].size |= base_ranges[j++];
238                 }
239         }
240
241         free(base_ranges, M_DEVBUF);
242         return (sc->nranges);
243 }
244
245 static struct simplebus_devinfo *
246 simplebus_setup_dinfo(device_t dev, phandle_t node)
247 {
248         struct simplebus_softc *sc;
249         struct simplebus_devinfo *ndi;
250         uint32_t *reg, *intr, icells;
251         uint64_t phys, size;
252         phandle_t iparent;
253         int i, j, k;
254         int nintr;
255         int nreg;
256
257         sc = device_get_softc(dev);
258
259         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
260         if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
261                 free(ndi, M_DEVBUF);
262                 return (NULL);
263         }
264
265         resource_list_init(&ndi->rl);
266         nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
267         if (nreg == -1)
268                 nreg = 0;
269         if (nreg % (sc->acells + sc->scells) != 0) {
270                 if (bootverbose)
271                         device_printf(dev, "Malformed reg property on <%s>\n",
272                             ndi->obdinfo.obd_name);
273                 nreg = 0;
274         }
275
276         for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
277                 phys = size = 0;
278                 for (j = 0; j < sc->acells; j++) {
279                         phys <<= 32;
280                         phys |= reg[i + j];
281                 }
282                 for (j = 0; j < sc->scells; j++) {
283                         size <<= 32;
284                         size |= reg[i + sc->acells + j];
285                 }
286                 
287                 resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
288                     phys, phys + size - 1, size);
289         }
290         free(reg, M_OFWPROP);
291
292         nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
293             (void **)&intr);
294         if (nintr > 0) {
295                 if (OF_searchencprop(node, "interrupt-parent", &iparent,
296                     sizeof(iparent)) == -1) {
297                         device_printf(dev, "No interrupt-parent found, "
298                             "assuming direct parent\n");
299                         iparent = OF_parent(node);
300                 }
301                 if (OF_searchencprop(OF_xref_phandle(iparent), 
302                     "#interrupt-cells", &icells, sizeof(icells)) == -1) {
303                         device_printf(dev, "Missing #interrupt-cells property, "
304                             "assuming <1>\n");
305                         icells = 1;
306                 }
307                 if (icells < 1 || icells > nintr) {
308                         device_printf(dev, "Invalid #interrupt-cells property "
309                             "value <%d>, assuming <1>\n", icells);
310                         icells = 1;
311                 }
312                 for (i = 0, k = 0; i < nintr; i += icells, k++) {
313                         intr[i] = ofw_bus_map_intr(dev, iparent, icells,
314                             &intr[i]);
315                         resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
316                             intr[i], 1);
317                 }
318                 free(intr, M_OFWPROP);
319         }
320
321         return (ndi);
322 }
323
324 static const struct ofw_bus_devinfo *
325 simplebus_get_devinfo(device_t bus __unused, device_t child)
326 {
327         struct simplebus_devinfo *ndi;
328         
329         ndi = device_get_ivars(child);
330         return (&ndi->obdinfo);
331 }
332
333 static struct resource *
334 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
335     u_long start, u_long end, u_long count, u_int flags)
336 {
337         struct simplebus_softc *sc;
338         struct simplebus_devinfo *di;
339         struct resource_list_entry *rle;
340         int j;
341
342         sc = device_get_softc(bus);
343
344         /*
345          * Request for the default allocation with a given rid: use resource
346          * list stored in the local device info.
347          */
348         if ((start == 0UL) && (end == ~0UL)) {
349                 if ((di = device_get_ivars(child)) == NULL)
350                         return (NULL);
351
352                 if (type == SYS_RES_IOPORT)
353                         type = SYS_RES_MEMORY;
354
355                 rle = resource_list_find(&di->rl, type, *rid);
356                 if (rle == NULL) {
357                         if (bootverbose)
358                                 device_printf(bus, "no default resources for "
359                                     "rid = %d, type = %d\n", *rid, type);
360                         return (NULL);
361                 }
362                 start = rle->start;
363                 end = rle->end;
364                 count = rle->count;
365         }
366
367         if (type == SYS_RES_MEMORY) {
368                 /* Remap through ranges property */
369                 for (j = 0; j < sc->nranges; j++) {
370                         if (start >= sc->ranges[j].bus && end <
371                             sc->ranges[j].bus + sc->ranges[j].size) {
372                                 start -= sc->ranges[j].bus;
373                                 start += sc->ranges[j].host;
374                                 end -= sc->ranges[j].bus;
375                                 end += sc->ranges[j].host;
376                                 break;
377                         }
378                 }
379                 if (j == sc->nranges && sc->nranges != 0) {
380                         if (bootverbose)
381                                 device_printf(bus, "Could not map resource "
382                                     "%#lx-%#lx\n", start, end);
383
384                         return (NULL);
385                 }
386         }
387
388         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
389             count, flags));
390 }
391
392 static int
393 simplebus_print_res(struct simplebus_devinfo *di)
394 {
395         int rv;
396
397         rv = 0;
398         rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#lx");
399         rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%ld");
400         return (rv);
401 }
402
403 static void
404 simplebus_probe_nomatch(device_t bus, device_t child)
405 {
406         const char *name, *type, *compat;
407
408         if (!bootverbose)
409                 return;
410
411         name = ofw_bus_get_name(child);
412         type = ofw_bus_get_type(child);
413         compat = ofw_bus_get_compat(child);
414
415         device_printf(bus, "<%s>", name != NULL ? name : "unknown");
416         simplebus_print_res(device_get_ivars(child));
417         if (!ofw_bus_status_okay(child))
418                 printf(" disabled");
419         if (type)
420                 printf(" type %s", type);
421         if (compat)
422                 printf(" compat %s", compat);
423         printf(" (no driver attached)\n");
424 }
425
426 static int
427 simplebus_print_child(device_t bus, device_t child)
428 {
429         int rv;
430
431         rv = bus_print_child_header(bus, child);
432         rv += simplebus_print_res(device_get_ivars(child));
433         if (!ofw_bus_status_okay(child))
434                 rv += printf(" disabled");
435         rv += bus_print_child_footer(bus, child);
436         return (rv);
437 }