]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/powerpc/aim/nexus.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / powerpc / aim / nexus.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  *      from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
54  */
55
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/module.h>
62 #include <sys/bus.h>
63 #include <sys/cons.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66
67 #include <dev/ofw/ofw_bus_subr.h>
68 #include <dev/ofw/openfirm.h>
69
70 #include <machine/bus.h>
71 #include <machine/frame.h>
72 #include <machine/intr_machdep.h>
73 #include <machine/resource.h>
74
75 #include <sys/rman.h>
76
77 #include "ofw_bus_if.h"
78 #include "pic_if.h"
79
80 /*
81  * The nexus (which is a pseudo-bus actually) iterates over the nodes that
82  * exist in Open Firmware and adds them as devices to this bus so that drivers
83  * can be attached to them.
84  *
85  * Maybe this code should get into dev/ofw to some extent, as some of it should
86  * work for all Open Firmware based machines...
87  */
88
89 static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
90
91 enum nexus_ivars {
92         NEXUS_IVAR_NODE,
93         NEXUS_IVAR_NAME,
94         NEXUS_IVAR_DEVICE_TYPE,
95         NEXUS_IVAR_COMPATIBLE,
96 };
97
98 struct nexus_devinfo {
99         phandle_t       ndi_node;
100         /* Some common properties. */
101         const char      *ndi_name;
102         const char      *ndi_device_type;
103         const char      *ndi_compatible;
104 };
105
106 struct nexus_softc {
107         struct rman     sc_rman;
108 };
109
110 /*
111  * Device interface
112  */
113 static int      nexus_probe(device_t);
114 static int      nexus_attach(device_t);
115
116 /*
117  * Bus interface
118  */
119 static device_t nexus_add_child(device_t, u_int, const char *, int);
120 static void     nexus_probe_nomatch(device_t, device_t);
121 static int      nexus_read_ivar(device_t, device_t, int, uintptr_t *);
122 static int      nexus_write_ivar(device_t, device_t, int, uintptr_t);
123 #ifdef SMP
124 static int      nexus_bind_intr(device_t dev, device_t child,
125                     struct resource *irq, int cpu);
126 #endif
127 static int      nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
128                     enum intr_polarity pol);
129 static int      nexus_setup_intr(device_t, device_t, struct resource *, int,
130                     driver_filter_t *, driver_intr_t *, void *, void **);
131 static int      nexus_teardown_intr(device_t, device_t, struct resource *,
132                     void *);
133 static struct   resource *nexus_alloc_resource(device_t, device_t, int, int *,
134                     u_long, u_long, u_long, u_int);
135 static int      nexus_activate_resource(device_t, device_t, int, int,
136                     struct resource *);
137 static int      nexus_deactivate_resource(device_t, device_t, int, int,
138                     struct resource *);
139 static int      nexus_release_resource(device_t, device_t, int, int,
140                     struct resource *);
141
142 /*
143  * OFW bus interface.
144  */
145 static phandle_t         nexus_ofw_get_node(device_t, device_t);
146 static const char       *nexus_ofw_get_name(device_t, device_t);
147 static const char       *nexus_ofw_get_type(device_t, device_t);
148 static const char       *nexus_ofw_get_compat(device_t, device_t);
149
150 /*
151  * Local routines
152  */
153 static device_t nexus_device_from_node(device_t, phandle_t);
154
155 static device_method_t nexus_methods[] = {
156         /* Device interface */
157         DEVMETHOD(device_probe,         nexus_probe),
158         DEVMETHOD(device_attach,        nexus_attach),
159         DEVMETHOD(device_detach,        bus_generic_detach),
160         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
161         DEVMETHOD(device_suspend,       bus_generic_suspend),
162         DEVMETHOD(device_resume,        bus_generic_resume),
163
164         /* Bus interface. Resource management is business of the children... */
165         DEVMETHOD(bus_add_child,        nexus_add_child),
166         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
167         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
168         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
169         DEVMETHOD(bus_write_ivar,       nexus_write_ivar),
170         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
171         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
172 #ifdef SMP
173         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
174 #endif
175         DEVMETHOD(bus_config_intr,      nexus_config_intr),
176         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
177         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
178         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
179         DEVMETHOD(bus_release_resource, nexus_release_resource),
180
181         /* OFW bus interface */
182         DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node),
183         DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name),
184         DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type),
185         DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat),
186
187         DEVMETHOD_END
188 };
189
190 static driver_t nexus_driver = {
191         "nexus",
192         nexus_methods,
193         sizeof(struct nexus_softc),
194 };
195
196 static devclass_t nexus_devclass;
197
198 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
199     BUS_PASS_BUS);
200
201 static int
202 nexus_probe(device_t dev)
203 {
204         bus_generic_probe(dev);
205         device_set_desc(dev, "Open Firmware Nexus device");
206         return (0);
207 }
208
209 static int
210 nexus_attach(device_t dev)
211 {
212         phandle_t       root;
213         phandle_t       child;
214         struct          nexus_softc *sc;
215         u_long          start, end;
216
217         sc = device_get_softc(dev);
218
219         start = 0;
220         end = ~0;
221
222         sc->sc_rman.rm_start = start;
223         sc->sc_rman.rm_end = end;
224         sc->sc_rman.rm_type = RMAN_ARRAY;
225         sc->sc_rman.rm_descr = "Interrupt request lines";
226         if (rman_init(&sc->sc_rman) ||
227             rman_manage_region(&sc->sc_rman, start, end))
228                 panic("nexus_probe IRQ rman");
229
230         if ((root = OF_peer(0)) == 0)
231                 return (bus_generic_attach(dev));
232                 
233         /*
234          * Now walk the OFW tree to locate top-level devices
235          */
236         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
237                 if (child == -1)
238                         panic("nexus_probe(): OF_child failed.");
239                 (void)nexus_device_from_node(dev, child);
240
241         }
242
243         return (bus_generic_attach(dev));
244 }
245
246 static void
247 nexus_probe_nomatch(device_t dev, device_t child)
248 {
249         char    *name, *type;
250
251         if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
252             (uintptr_t *)&name) != 0 ||
253             BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
254             (uintptr_t *)&type) != 0)
255                 return;
256
257         if (type == NULL)
258                 type = "(unknown)";
259
260         if (bootverbose)
261                 device_printf(dev, "<%s>, type %s (no driver attached)\n",
262                     name, type);
263 }
264
265 static device_t
266 nexus_add_child(device_t dev, u_int order, const char *name, int unit)
267 {
268         device_t child;
269         struct nexus_devinfo *dinfo;
270
271         child = device_add_child_ordered(dev, order, name, unit);
272         if (child == NULL)
273                 return (NULL);
274
275         dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
276         if (dinfo == NULL)
277                 return (NULL);
278
279         dinfo->ndi_node = -1;
280         dinfo->ndi_name = name;
281         device_set_ivars(child, dinfo);
282
283         return (child);
284 }
285
286
287 static int
288 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
289 {
290         struct nexus_devinfo *dinfo;
291
292         if ((dinfo = device_get_ivars(child)) == 0)
293                 return (ENOENT);
294         switch (which) {
295         case NEXUS_IVAR_NODE:
296                 *result = dinfo->ndi_node;
297                 break;
298         case NEXUS_IVAR_NAME:
299                 *result = (uintptr_t)dinfo->ndi_name;
300                 break;
301         case NEXUS_IVAR_DEVICE_TYPE:
302                 *result = (uintptr_t)dinfo->ndi_device_type;
303                 break;
304         case NEXUS_IVAR_COMPATIBLE:
305                 *result = (uintptr_t)dinfo->ndi_compatible;
306                 break;
307         default:
308                 return (ENOENT);
309         }
310         return 0;
311 }
312
313 static int
314 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
315 {
316         struct nexus_devinfo *dinfo;
317
318         if ((dinfo = device_get_ivars(child)) == 0)
319                 return (ENOENT);
320
321         switch (which) {
322         case NEXUS_IVAR_NAME:
323                 return (EINVAL);
324
325         /* Identified devices may want to set these */
326         case NEXUS_IVAR_NODE:
327                 dinfo->ndi_node = (phandle_t)value;
328                 break;
329
330         case NEXUS_IVAR_DEVICE_TYPE:
331                 dinfo->ndi_device_type = (char *)value;
332                 break;
333
334         default:
335                 return (ENOENT);
336         }
337         return 0;
338 }
339
340 static int
341 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
342     driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep)
343 {
344         int             error;
345
346         /* somebody tried to setup an irq that failed to allocate! */
347         if (res == NULL)
348                 panic("nexus_setup_intr: NULL irq resource!");
349
350         *cookiep = 0;
351         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
352                 flags |= INTR_EXCL;
353
354         /*
355          * We depend here on rman_activate_resource() being idempotent.
356          */
357         error = rman_activate_resource(res);
358         if (error)
359                 return (error);
360
361         error = powerpc_setup_intr(device_get_nameunit(child),
362             rman_get_start(res), filter, ihand, arg, flags, cookiep);
363
364         return (error);
365 }
366
367 static int
368 nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
369     void *cookie)
370 {
371
372         return (powerpc_teardown_intr(cookie));
373 }
374
375 #ifdef SMP
376 static int
377 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
378 {
379
380         return (powerpc_bind_intr(rman_get_start(irq), cpu));
381 }
382 #endif
383         
384 static int
385 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
386     enum intr_polarity pol)
387 {
388
389         return (powerpc_config_intr(irq, trig, pol));
390 }
391
392 /*
393  * Allocate resources at the behest of a child.  This only handles interrupts,
394  * since I/O resources are handled by child busses.
395  */
396 static struct resource *
397 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
398     u_long start, u_long end, u_long count, u_int flags)
399 {
400         struct nexus_softc *sc;
401         struct resource *rv;
402
403         if (type != SYS_RES_IRQ) {
404                 device_printf(bus, "unknown resource request from %s\n",
405                     device_get_nameunit(child));
406                 return (NULL);
407         }
408
409         if (count == 0 || start + count - 1 != end) {
410                 device_printf(bus, "invalid IRQ allocation from %s\n",
411                     device_get_nameunit(child));
412                 return (NULL);
413         }
414
415         sc = device_get_softc(bus);
416
417         rv = rman_reserve_resource(&sc->sc_rman, start, end, count,
418             flags, child);
419         if (rv == NULL) {
420                 device_printf(bus, "IRQ allocation failed for %s\n",
421                     device_get_nameunit(child));
422         } else
423                 rman_set_rid(rv, *rid);
424
425         return (rv);
426 }
427
428 static int
429 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
430     struct resource *res)
431 {
432
433         /* Not much to be done yet... */
434         return (rman_activate_resource(res));
435 }
436
437 static int
438 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
439     struct resource *res)
440 {
441
442         /* Not much to be done yet... */
443         return (rman_deactivate_resource(res));
444 }
445
446 static int
447 nexus_release_resource(device_t bus, device_t child, int type, int rid,
448     struct resource *res)
449 {
450
451         if (type != SYS_RES_IRQ) {
452                 device_printf(bus, "unknown resource request from %s\n",
453                     device_get_nameunit(child));
454                 return (EINVAL);
455         }
456
457         return (rman_release_resource(res));
458 }
459
460 static device_t
461 nexus_device_from_node(device_t parent, phandle_t node)
462 {
463         device_t        cdev;
464         struct          nexus_devinfo *dinfo;
465         char            *name, *type, *compatible;
466
467         OF_getprop_alloc(node, "name", 1, (void **)&name);
468         OF_getprop_alloc(node, "device_type", 1, (void **)&type);
469         OF_getprop_alloc(node, "compatible", 1, (void **)&compatible);
470         cdev = device_add_child(parent, NULL, -1);
471         if (cdev != NULL) {
472                 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
473                 dinfo->ndi_node = node;
474                 dinfo->ndi_name = name;
475                 dinfo->ndi_device_type = type;
476                 dinfo->ndi_compatible = compatible;
477                 device_set_ivars(cdev, dinfo);
478         } else
479                 free(name, M_OFWPROP);
480
481         return (cdev);
482 }
483
484 static const char *
485 nexus_ofw_get_name(device_t bus, device_t dev)
486 {
487         struct nexus_devinfo *dinfo;
488
489         if ((dinfo = device_get_ivars(dev)) == NULL)
490                 return (NULL);
491         
492         return (dinfo->ndi_name);
493 }
494
495 static phandle_t
496 nexus_ofw_get_node(device_t bus, device_t dev)
497 {
498         struct nexus_devinfo *dinfo;
499
500         if ((dinfo = device_get_ivars(dev)) == NULL)
501                 return (0);
502         
503         return (dinfo->ndi_node);
504 }
505
506 static const char *
507 nexus_ofw_get_type(device_t bus, device_t dev)
508 {
509         struct nexus_devinfo *dinfo;
510
511         if ((dinfo = device_get_ivars(dev)) == NULL)
512                 return (NULL);
513         
514         return (dinfo->ndi_device_type);
515 }
516
517 static const char *
518 nexus_ofw_get_compat(device_t bus, device_t dev)
519 {
520         struct nexus_devinfo *dinfo;
521
522         if ((dinfo = device_get_ivars(dev)) == NULL)
523                 return (NULL);
524         
525         return (dinfo->ndi_compatible);
526 }
527