]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/nexus.c
This commit was generated by cvs2svn to compensate for changes in r170222,
[FreeBSD/FreeBSD.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  * $FreeBSD$
56  */
57 #include "opt_psim.h"
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/openfirm.h>
68
69 #include <machine/bus.h>
70 #include <machine/frame.h>
71 #include <machine/intr_machdep.h>
72 #include <machine/nexusvar.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 struct nexus_devinfo {
92         phandle_t       ndi_node;
93         /* Some common properties. */
94         const char      *ndi_name;
95         const char      *ndi_device_type;
96         const char      *ndi_compatible;
97 };
98
99 struct nexus_softc {
100         device_t        sc_pic;
101 };
102
103 /*
104  * Device interface
105  */
106 static int      nexus_probe(device_t);
107 static void     nexus_probe_nomatch(device_t, device_t);
108
109 /*
110  * Bus interface
111  */
112 static device_t nexus_add_child(device_t, int, const char *, int);
113 static int      nexus_read_ivar(device_t, device_t, int, uintptr_t *);
114 static int      nexus_write_ivar(device_t, device_t, int, uintptr_t);
115 static int      nexus_setup_intr(device_t, device_t, struct resource *, int,
116                     driver_filter_t *, driver_intr_t *, void *, void **);
117 static int      nexus_teardown_intr(device_t, device_t, struct resource *,
118                     void *);
119 static struct   resource *nexus_alloc_resource(device_t, device_t, int, int *,
120                     u_long, u_long, u_long, u_int);
121 static int      nexus_activate_resource(device_t, device_t, int, int,
122                     struct resource *);
123 static int      nexus_deactivate_resource(device_t, device_t, int, int,
124                     struct resource *);
125 static int      nexus_release_resource(device_t, device_t, int, int,
126                     struct resource *);
127
128 static phandle_t         nexus_ofw_get_node(device_t, device_t);
129 static const char       *nexus_ofw_get_name(device_t, device_t);
130 static const char       *nexus_ofw_get_type(device_t, device_t);
131 static const char       *nexus_ofw_get_compat(device_t, device_t);
132
133 /*
134  * Local routines
135  */
136 static device_t nexus_device_from_node(device_t, phandle_t);
137
138 static device_method_t nexus_methods[] = {
139         /* Device interface */
140         DEVMETHOD(device_probe,         nexus_probe),
141         DEVMETHOD(device_attach,        bus_generic_attach),
142         DEVMETHOD(device_detach,        bus_generic_detach),
143         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
144         DEVMETHOD(device_suspend,       bus_generic_suspend),
145         DEVMETHOD(device_resume,        bus_generic_resume),
146
147         /* Bus interface. Resource management is business of the children... */
148         DEVMETHOD(bus_add_child,        nexus_add_child),
149         DEVMETHOD(bus_print_child,      bus_generic_print_child),
150         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
151         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
152         DEVMETHOD(bus_write_ivar,       nexus_write_ivar),
153         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
154         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
155         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
156         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
157         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
158         DEVMETHOD(bus_release_resource, nexus_release_resource),
159
160         /* OFW bus interface */
161         DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node),
162         DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name),
163         DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type),
164         DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat),
165
166         { 0, 0 }
167 };
168
169 static driver_t nexus_driver = {
170         "nexus",
171         nexus_methods,
172         sizeof(struct nexus_softc),
173 };
174
175 static devclass_t nexus_devclass;
176
177 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
178
179 static int
180 nexus_probe(device_t dev)
181 {
182         phandle_t       root;
183         phandle_t       child;
184         struct          nexus_softc *sc;
185
186         if ((root = OF_peer(0)) == -1)
187                 panic("nexus_probe: OF_peer failed.");
188
189         sc = device_get_softc(dev);
190
191         /*
192          * Allow devices to identify
193          */
194         bus_generic_probe(dev);
195
196         /*
197          * Now walk the OFW tree to locate top-level devices
198          */
199         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
200                 if (child == -1)
201                         panic("nexus_probe(): OF_child failed.");
202                 (void)nexus_device_from_node(dev, child);
203
204         }
205         device_set_desc(dev, "Open Firmware Nexus device");
206         return (0);
207 }
208
209 static void
210 nexus_probe_nomatch(device_t dev, device_t child)
211 {
212         char    *name, *type;
213
214         if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
215             (uintptr_t *)&name) != 0 ||
216             BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
217             (uintptr_t *)&type) != 0)
218                 return;
219
220         if (type == NULL)
221                 type = "(unknown)";
222
223         if (bootverbose)
224                 device_printf(dev, "<%s>, type %s (no driver attached)\n",
225                     name, type);
226 }
227
228 static device_t
229 nexus_add_child(device_t dev, int order, const char *name, int unit)
230 {
231         device_t child;
232         struct nexus_devinfo *dinfo;
233
234         child = device_add_child_ordered(dev, order, name, unit);
235         if (child == NULL)
236                 return (NULL);
237
238         dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
239         if (dinfo == NULL)
240                 return (NULL);
241
242         dinfo->ndi_node = -1;
243         dinfo->ndi_name = name;
244         device_set_ivars(child, dinfo);
245
246         return (child);
247 }
248
249
250 static int
251 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
252 {
253         struct nexus_devinfo *dinfo;
254
255         if ((dinfo = device_get_ivars(child)) == 0)
256                 return (ENOENT);
257         switch (which) {
258         case NEXUS_IVAR_NODE:
259                 *result = dinfo->ndi_node;
260                 break;
261         case NEXUS_IVAR_NAME:
262                 *result = (uintptr_t)dinfo->ndi_name;
263                 break;
264         case NEXUS_IVAR_DEVICE_TYPE:
265                 *result = (uintptr_t)dinfo->ndi_device_type;
266                 break;
267         case NEXUS_IVAR_COMPATIBLE:
268                 *result = (uintptr_t)dinfo->ndi_compatible;
269                 break;
270         default:
271                 return (ENOENT);
272         }
273         return 0;
274 }
275
276 static int
277 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
278 {
279         struct nexus_devinfo *dinfo;
280
281         if ((dinfo = device_get_ivars(child)) == 0)
282                 return (ENOENT);
283
284         switch (which) {
285         case NEXUS_IVAR_NAME:
286                 return (EINVAL);
287
288         /* Identified devices may want to set these */
289         case NEXUS_IVAR_NODE:
290                 dinfo->ndi_node = (phandle_t)value;
291                 break;
292
293         case NEXUS_IVAR_DEVICE_TYPE:
294                 dinfo->ndi_device_type = (char *)value;
295                 break;
296
297         default:
298                 return (ENOENT);
299         }
300         return 0;
301 }
302
303 static int
304 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
305     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
306 {
307         struct  nexus_softc *sc;
308
309         sc = device_get_softc(dev);
310
311         if (device_get_state(sc->sc_pic) != DS_ATTACHED)
312                 panic("nexus_setup_intr: no pic attached\n");
313
314         return (PIC_SETUP_INTR(sc->sc_pic, child, res, flags, filter, intr, 
315             arg, cookiep));
316 }
317
318 static int
319 nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
320     void *ih)
321 {
322         struct  nexus_softc *sc;
323
324         sc = device_get_softc(dev);
325
326         if (device_get_state(sc->sc_pic) != DS_ATTACHED)
327                 panic("nexus_teardown_intr: no pic attached\n");
328
329         return (PIC_TEARDOWN_INTR(sc->sc_pic, child, res, ih));
330 }
331
332 /*
333  * Allocate resources at the behest of a child.  This only handles interrupts,
334  * since I/O resources are handled by child busses.
335  */
336 static struct resource *
337 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
338     u_long start, u_long end, u_long count, u_int flags)
339 {
340         struct  nexus_softc *sc;
341         struct  resource *rv;
342
343         sc = device_get_softc(bus);
344
345         if (type != SYS_RES_IRQ) {
346                 device_printf(bus, "unknown resource request from %s\n",
347                     device_get_nameunit(child));
348                 return (NULL);
349         }
350
351         if (device_get_state(sc->sc_pic) != DS_ATTACHED)
352                 panic("nexus_alloc_resource: no pic attached\n");
353
354         rv = PIC_ALLOCATE_INTR(sc->sc_pic, child, rid, start, flags);
355
356         return (rv);
357 }
358
359 static int
360 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
361     struct resource *res)
362 {
363
364         /* Not much to be done yet... */
365         return (rman_activate_resource(res));
366 }
367
368 static int
369 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
370     struct resource *res)
371 {
372
373         /* Not much to be done yet... */
374         return (rman_deactivate_resource(res));
375 }
376
377 static int
378 nexus_release_resource(device_t bus, device_t child, int type, int rid,
379     struct resource *res)
380 {
381         struct  nexus_softc *sc;
382
383         sc = device_get_softc(bus);
384
385         if (type != SYS_RES_IRQ) {
386                 device_printf(bus, "unknown resource request from %s\n",
387                     device_get_nameunit(child));
388                 return (EINVAL);
389         }
390
391         if (device_get_state(sc->sc_pic) != DS_ATTACHED)
392                 panic("nexus_release_resource: no pic attached\n");
393
394         return (PIC_RELEASE_INTR(sc->sc_pic, child, rid, res));
395 }
396
397 static device_t
398 nexus_device_from_node(device_t parent, phandle_t node)
399 {
400         device_t        cdev;
401         struct          nexus_devinfo *dinfo;
402         char            *name, *type, *compatible;
403
404         OF_getprop_alloc(node, "name", 1, (void **)&name);
405         OF_getprop_alloc(node, "device_type", 1, (void **)&type);
406         OF_getprop_alloc(node, "compatible", 1, (void **)&compatible);
407         cdev = device_add_child(parent, NULL, -1);
408         if (cdev != NULL) {
409                 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
410                 dinfo->ndi_node = node;
411                 dinfo->ndi_name = name;
412                 dinfo->ndi_device_type = type;
413                 dinfo->ndi_compatible = compatible;
414                 device_set_ivars(cdev, dinfo);
415         } else
416                 free(name, M_OFWPROP);
417
418         return (cdev);
419 }
420
421 int
422 nexus_install_intcntlr(device_t dev)
423 {
424         struct  nexus_softc *sc;
425
426         sc = device_get_softc(device_get_parent(dev));
427         sc->sc_pic = dev;
428
429         return (0);
430 }
431
432 static const char *
433 nexus_ofw_get_name(device_t bus, device_t dev)
434 {
435         struct nexus_devinfo *dinfo;
436
437         if ((dinfo = device_get_ivars(dev)) == NULL)
438                 return (NULL);
439         
440         return (dinfo->ndi_name);
441 }
442
443 static phandle_t
444 nexus_ofw_get_node(device_t bus, device_t dev)
445 {
446         struct nexus_devinfo *dinfo;
447
448         if ((dinfo = device_get_ivars(dev)) == NULL)
449                 return (0);
450         
451         return (dinfo->ndi_node);
452 }
453
454 static const char *
455 nexus_ofw_get_type(device_t bus, device_t dev)
456 {
457         struct nexus_devinfo *dinfo;
458
459         if ((dinfo = device_get_ivars(dev)) == NULL)
460                 return (NULL);
461         
462         return (dinfo->ndi_device_type);
463 }
464
465 static const char *
466 nexus_ofw_get_compat(device_t bus, device_t dev)
467 {
468         struct nexus_devinfo *dinfo;
469
470         if ((dinfo = device_get_ivars(dev)) == NULL)
471                 return (NULL);
472         
473         return (dinfo->ndi_compatible);
474 }