]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/mips/beri/beri_simplebus.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / mips / beri / beri_simplebus.c
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/ktr.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/openfirm.h>
46
47 #include <dev/fdt/fdt_common.h>
48 #include "fdt_ic_if.h"
49 #include "ofw_bus_if.h"
50
51 #ifdef DEBUG
52 #define debugf(fmt, args...) do { printf("%s(): ", __func__);   \
53     printf(fmt,##args); } while (0)
54 #else
55 #define debugf(fmt, args...)
56 #endif
57
58 static MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information");
59
60 struct simplebus_softc {
61         int     sc_addr_cells;
62         int     sc_size_cells;
63 };
64
65 struct simplebus_devinfo {
66         struct ofw_bus_devinfo  di_ofw;
67         struct resource_list    di_res;
68
69         /* Interrupts sense-level info for this device */
70         struct fdt_sense_level  di_intr_sl[DI_MAX_INTR_NUM];
71 };
72
73 /*
74  * Prototypes.
75  */
76 static int simplebus_probe(device_t);
77 static int simplebus_attach(device_t);
78
79 static int simplebus_print_child(device_t, device_t);
80 static int simplebus_setup_intr(device_t, device_t, struct resource *, int,
81     driver_filter_t *, driver_intr_t *, void *, void **);
82 static int simplebus_teardown_intr(device_t, device_t, struct resource *,
83     void *);
84
85 static int simplebus_activate_resource(device_t, device_t, int, int,
86     struct resource *);
87 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
88     int *, u_long, u_long, u_long, u_int);
89 static int simplebus_deactivate_resource(device_t, device_t, int, int,
90     struct resource *);
91 static int simplebus_release_resource(device_t, device_t, int, int,
92     struct resource *);
93 static device_t simplebus_get_interrupt_parent(device_t);
94 static struct resource_list *simplebus_get_resource_list(device_t, device_t);
95
96 static ofw_bus_get_devinfo_t simplebus_get_devinfo;
97
98 /*
99  * Bus interface definition.
100  */
101 static device_method_t simplebus_methods[] = {
102         /* Device interface */
103         DEVMETHOD(device_probe,         simplebus_probe),
104         DEVMETHOD(device_attach,        simplebus_attach),
105         DEVMETHOD(device_detach,        bus_generic_detach),
106         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
107         DEVMETHOD(device_suspend,       bus_generic_suspend),
108         DEVMETHOD(device_resume,        bus_generic_resume),
109
110         /* Bus interface */
111         DEVMETHOD(bus_print_child,      simplebus_print_child),
112         DEVMETHOD(bus_alloc_resource,   simplebus_alloc_resource),
113         DEVMETHOD(bus_release_resource, simplebus_release_resource),
114         DEVMETHOD(bus_activate_resource, simplebus_activate_resource),
115         DEVMETHOD(bus_deactivate_resource, simplebus_deactivate_resource),
116         DEVMETHOD(bus_setup_intr,       simplebus_setup_intr),
117         DEVMETHOD(bus_teardown_intr,    simplebus_teardown_intr),
118         DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
119
120         /* OFW bus interface */
121         DEVMETHOD(ofw_bus_get_devinfo,  simplebus_get_devinfo),
122         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
123         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
124         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
125         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
126         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
127
128         { 0, 0 }
129 };
130
131 static driver_t simplebus_driver = {
132         "simplebus",
133         simplebus_methods,
134         sizeof(struct simplebus_softc)
135 };
136
137 devclass_t simplebus_devclass;
138
139 DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass, 0, 0);
140 DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass, 0,
141     0);
142
143 static int
144 simplebus_probe(device_t dev)
145 {
146
147         if (!ofw_bus_status_okay(dev))
148                 return (ENXIO);
149
150         if (!ofw_bus_is_compatible(dev, "simple-bus"))
151                 return (ENXIO);
152
153         device_set_desc(dev, "Flattened device tree simple bus");
154
155         return (BUS_PROBE_SPECIFIC);
156 }
157
158 static int
159 simplebus_attach(device_t dev)
160 {
161         device_t dev_child;
162         struct simplebus_devinfo *di;
163         struct simplebus_softc *sc;
164         phandle_t dt_node, dt_child;
165
166         sc = device_get_softc(dev);
167
168         /*
169          * Walk simple-bus and add direct subordinates as our children.
170          */
171         dt_node = ofw_bus_get_node(dev);
172         for (dt_child = OF_child(dt_node); dt_child != 0;
173             dt_child = OF_peer(dt_child)) {
174
175                 /* Check and process 'status' property. */
176                 if (!(fdt_is_enabled(dt_child)))
177                         continue;
178
179                 if (!(fdt_pm_is_enabled(dt_child)))
180                         continue;
181
182                 di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO);
183
184                 if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
185                         free(di, M_SIMPLEBUS);
186                         device_printf(dev, "could not set up devinfo\n");
187                         continue;
188                 }
189
190                 resource_list_init(&di->di_res);
191                 if (fdt_reg_to_rl(dt_child, &di->di_res)) {
192                         device_printf(dev,
193                             "%s: could not process 'reg' "
194                             "property\n", di->di_ofw.obd_name);
195                         ofw_bus_gen_destroy_devinfo(&di->di_ofw);
196                         free(di, M_SIMPLEBUS);
197                         continue;
198                 }
199
200                 if (ofw_bus_intr_to_rl(dev, dt_child, &di->di_res)) {
201                         device_printf(dev, "%s: could not process "
202                             "'interrupts' property\n", di->di_ofw.obd_name);
203                         resource_list_free(&di->di_res);
204                         ofw_bus_gen_destroy_devinfo(&di->di_ofw);
205                         free(di, M_SIMPLEBUS);
206                         continue;
207                 }
208
209                 /* Add newbus device for this FDT node */
210                 dev_child = device_add_child(dev, NULL, -1);
211                 if (dev_child == NULL) {
212                         device_printf(dev, "could not add child: %s\n",
213                             di->di_ofw.obd_name);
214                         resource_list_free(&di->di_res);
215                         ofw_bus_gen_destroy_devinfo(&di->di_ofw);
216                         free(di, M_SIMPLEBUS);
217                         continue;
218                 }
219 #ifdef DEBUG
220                 device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name);
221 #endif
222                 device_set_ivars(dev_child, di);
223         }
224
225         return (bus_generic_attach(dev));
226 }
227
228 static int
229 simplebus_print_child(device_t dev, device_t child)
230 {
231         device_t ip;
232         struct simplebus_devinfo *di;
233         struct resource_list *rl;
234         int rv;
235
236         di = device_get_ivars(child);
237         rl = &di->di_res;
238
239         rv = 0;
240         rv += bus_print_child_header(dev, child);
241         rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
242         rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
243         if ((ip = simplebus_get_interrupt_parent(child)) != NULL)
244                 rv += printf(" (%s)", device_get_nameunit(ip));
245         rv += bus_print_child_footer(dev, child);
246
247         return (rv);
248 }
249
250 static struct resource *
251 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
252     u_long start, u_long end, u_long count, u_int flags)
253 {
254         device_t ic;
255         struct simplebus_devinfo *di;
256         struct resource_list_entry *rle;
257
258         /*
259          * Request for the default allocation with a given rid: use resource
260          * list stored in the local device info.
261          */
262         if ((start == 0UL) && (end == ~0UL)) {
263                 if ((di = device_get_ivars(child)) == NULL)
264                         return (NULL);
265
266                 if (type == SYS_RES_IOPORT)
267                         type = SYS_RES_MEMORY;
268
269                 rle = resource_list_find(&di->di_res, type, *rid);
270                 if (rle == NULL) {
271                         if (bootverbose)
272                                 device_printf(bus, "no default resources for "
273                                     "rid = %d, type = %d\n", *rid, type);
274                         return (NULL);
275                 }
276                 start = rle->start;
277                 end = rle->end;
278                 count = rle->count;
279         }
280
281         if (type == SYS_RES_IRQ &&
282             (ic = simplebus_get_interrupt_parent(child)) != NULL)
283                 return(FDT_IC_ALLOC_INTR(ic, child, rid, start, flags));
284
285         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
286             count, flags));
287 }
288
289 static int
290 simplebus_activate_resource(device_t dev, device_t child, int type, int rid,
291     struct resource *r)
292 {
293         device_t ic;
294
295         if (type == SYS_RES_IRQ &&
296             (ic = simplebus_get_interrupt_parent(child)) != NULL)
297                 return (FDT_IC_ACTIVATE_INTR(ic, r));
298
299         return (bus_generic_activate_resource(dev, child, type, rid, r));
300 }
301
302 static int
303 simplebus_deactivate_resource(device_t dev, device_t child, int type, int rid,
304     struct resource *r)
305 {
306         device_t ic;
307
308         if (type == SYS_RES_IRQ &&
309             (ic = simplebus_get_interrupt_parent(child)) != NULL)
310                 return (FDT_IC_DEACTIVATE_INTR(ic, r));
311
312         return (bus_generic_deactivate_resource(dev, child, type, rid, r));
313 }
314
315 static int
316 simplebus_release_resource(device_t dev, device_t child, int type, int rid,
317     struct resource *r)
318 {
319         device_t ic;
320
321         if (type == SYS_RES_IRQ &&
322             (ic = simplebus_get_interrupt_parent(child)) != NULL)
323                 return (FDT_IC_RELEASE_INTR(ic, r));
324
325         return (bus_generic_release_resource(dev, child, type, rid, r));
326 }
327
328 static struct resource_list *
329 simplebus_get_resource_list(device_t bus, device_t child)
330 {
331         struct simplebus_devinfo *di;
332
333         di = device_get_ivars(child);
334         return (&di->di_res);
335 }
336
337 static device_t
338 simplebus_get_interrupt_parent(device_t dev)
339 {
340         struct simplebus_devinfo *di;
341         struct fdt_ic *ic;
342         device_t ip;
343         phandle_t ph, iph;
344
345         ip = NULL;
346
347         di = device_get_ivars(dev);
348         if (di == NULL)
349                 return (NULL);
350
351         if (OF_getencprop(di->di_ofw.obd_node, "interrupt-parent", &iph,
352             sizeof(iph)) > 0) {
353                 ph = OF_node_from_xref(iph);
354                 SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) {
355                         if (ic->iph == ph) {
356                                 ip = ic->dev;
357                                 break;
358                         }
359                 }
360         }
361         return (ip);
362 }
363
364 static int
365 simplebus_setup_intr(device_t bus, device_t child, struct resource *res,
366     int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg,
367     void **cookiep)
368 {
369         struct simplebus_devinfo *di;
370         device_t ic;
371         enum intr_trigger trig;
372         enum intr_polarity pol;
373         int error, irq, rid;
374
375         di = device_get_ivars(child);
376         if (di == NULL)
377                 return (ENXIO);
378
379         if (res == NULL)
380                 return (EINVAL);
381
382         rid = rman_get_rid(res);
383         if (rid >= DI_MAX_INTR_NUM)
384                 return (ENOENT);
385
386         ic = simplebus_get_interrupt_parent(child);
387
388         trig = di->di_intr_sl[rid].trig;
389         pol = di->di_intr_sl[rid].pol;
390         if (trig != INTR_TRIGGER_CONFORM || pol != INTR_POLARITY_CONFORM) {
391                 irq = rman_get_start(res);
392                 if (ic != NULL)
393                         error = FDT_IC_CONFIG_INTR(ic, irq, trig, pol);
394                 else
395                         error = bus_generic_config_intr(bus, irq, trig, pol);
396                 if (error)
397                         return (error);
398         }
399
400         if (ic != NULL)
401                 error = FDT_IC_SETUP_INTR(ic, child, res, flags, filter,
402                     ihand, arg, cookiep);
403         else
404                 error = bus_generic_setup_intr(bus, child, res, flags, filter,
405                     ihand, arg, cookiep);
406         return (error);
407 }
408
409 static int
410 simplebus_teardown_intr(device_t bus, device_t child, struct resource *res,
411     void *cookie)
412 {
413         device_t ic;
414
415         if ((ic = simplebus_get_interrupt_parent(child)) != NULL)
416                 return (FDT_IC_TEARDOWN_INTR(ic, child, res, cookie));
417
418         return (bus_generic_teardown_intr(bus, child, res, cookie));
419 }
420
421 static const struct ofw_bus_devinfo *
422 simplebus_get_devinfo(device_t bus, device_t child)
423 {
424         struct simplebus_devinfo *di;
425
426         di = device_get_ivars(child);
427         return (&di->di_ofw);
428 }