]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/aim/nexus.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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/clock.h>
64 #include <sys/cons.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67
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 "clock_if.h"
78 #include "ofw_bus_if.h"
79 #include "pic_if.h"
80
81 /*
82  * The nexus (which is a pseudo-bus actually) iterates over the nodes that
83  * exist in Open Firmware and adds them as devices to this bus so that drivers
84  * can be attached to them.
85  *
86  * Maybe this code should get into dev/ofw to some extent, as some of it should
87  * work for all Open Firmware based machines...
88  */
89
90 static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
91
92 enum nexus_ivars {
93         NEXUS_IVAR_NODE,
94         NEXUS_IVAR_NAME,
95         NEXUS_IVAR_DEVICE_TYPE,
96         NEXUS_IVAR_COMPATIBLE,
97 };
98
99 struct nexus_devinfo {
100         phandle_t       ndi_node;
101         /* Some common properties. */
102         const char      *ndi_name;
103         const char      *ndi_device_type;
104         const char      *ndi_compatible;
105 };
106
107 struct nexus_softc {
108         struct rman     sc_rman;
109 };
110
111 /*
112  * Device interface
113  */
114 static int      nexus_probe(device_t);
115 static int      nexus_attach(device_t);
116
117 /*
118  * Bus interface
119  */
120 static device_t nexus_add_child(device_t, int, const char *, int);
121 static void     nexus_probe_nomatch(device_t, device_t);
122 static int      nexus_read_ivar(device_t, device_t, int, uintptr_t *);
123 static int      nexus_write_ivar(device_t, device_t, int, uintptr_t);
124 static int      nexus_setup_intr(device_t, device_t, struct resource *, int,
125                     driver_filter_t *, driver_intr_t *, void *, void **);
126 static int      nexus_teardown_intr(device_t, device_t, struct resource *,
127                     void *);
128 static struct   resource *nexus_alloc_resource(device_t, device_t, int, int *,
129                     u_long, u_long, u_long, u_int);
130 static int      nexus_activate_resource(device_t, device_t, int, int,
131                     struct resource *);
132 static int      nexus_deactivate_resource(device_t, device_t, int, int,
133                     struct resource *);
134 static int      nexus_release_resource(device_t, device_t, int, int,
135                     struct resource *);
136
137 /*
138  * OFW bus interface.
139  */
140 static phandle_t         nexus_ofw_get_node(device_t, device_t);
141 static const char       *nexus_ofw_get_name(device_t, device_t);
142 static const char       *nexus_ofw_get_type(device_t, device_t);
143 static const char       *nexus_ofw_get_compat(device_t, device_t);
144
145 /*
146  * Clock interface.
147  */
148 static int nexus_gettime(device_t, struct timespec *);
149 static int nexus_settime(device_t, struct timespec *);
150
151 /*
152  * Local routines
153  */
154 static device_t nexus_device_from_node(device_t, phandle_t);
155
156 static device_method_t nexus_methods[] = {
157         /* Device interface */
158         DEVMETHOD(device_probe,         nexus_probe),
159         DEVMETHOD(device_attach,        nexus_attach),
160         DEVMETHOD(device_detach,        bus_generic_detach),
161         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
162         DEVMETHOD(device_suspend,       bus_generic_suspend),
163         DEVMETHOD(device_resume,        bus_generic_resume),
164
165         /* Bus interface. Resource management is business of the children... */
166         DEVMETHOD(bus_add_child,        nexus_add_child),
167         DEVMETHOD(bus_print_child,      bus_generic_print_child),
168         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
169         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
170         DEVMETHOD(bus_write_ivar,       nexus_write_ivar),
171         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
172         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
173         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
174         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
175         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
176         DEVMETHOD(bus_release_resource, nexus_release_resource),
177
178         /* OFW bus interface */
179         DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node),
180         DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name),
181         DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type),
182         DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat),
183
184         /* Clock interface */
185         DEVMETHOD(clock_gettime,        nexus_gettime),
186         DEVMETHOD(clock_settime,        nexus_settime),
187
188         { 0, 0 }
189 };
190
191 static driver_t nexus_driver = {
192         "nexus",
193         nexus_methods,
194         sizeof(struct nexus_softc),
195 };
196
197 static devclass_t nexus_devclass;
198
199 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
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         if ((root = OF_peer(0)) == -1)
218                 panic("nexus_probe: OF_peer failed.");
219
220         sc = device_get_softc(dev);
221
222         start = 0;
223         end = INTR_VECTORS - 1;
224
225         sc->sc_rman.rm_start = start;
226         sc->sc_rman.rm_end = end;
227         sc->sc_rman.rm_type = RMAN_ARRAY;
228         sc->sc_rman.rm_descr = "Interrupt request lines";
229         if (rman_init(&sc->sc_rman) ||
230             rman_manage_region(&sc->sc_rman, start, end))
231                 panic("nexus_probe IRQ rman");
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         clock_register(dev, 1000);
244         return (bus_generic_attach(dev));
245 }
246
247 static void
248 nexus_probe_nomatch(device_t dev, device_t child)
249 {
250         char    *name, *type;
251
252         if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
253             (uintptr_t *)&name) != 0 ||
254             BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
255             (uintptr_t *)&type) != 0)
256                 return;
257
258         if (type == NULL)
259                 type = "(unknown)";
260
261         if (bootverbose)
262                 device_printf(dev, "<%s>, type %s (no driver attached)\n",
263                     name, type);
264 }
265
266 static device_t
267 nexus_add_child(device_t dev, int order, const char *name, int unit)
268 {
269         device_t child;
270         struct nexus_devinfo *dinfo;
271
272         child = device_add_child_ordered(dev, order, name, unit);
273         if (child == NULL)
274                 return (NULL);
275
276         dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
277         if (dinfo == NULL)
278                 return (NULL);
279
280         dinfo->ndi_node = -1;
281         dinfo->ndi_name = name;
282         device_set_ivars(child, dinfo);
283
284         return (child);
285 }
286
287
288 static int
289 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
290 {
291         struct nexus_devinfo *dinfo;
292
293         if ((dinfo = device_get_ivars(child)) == 0)
294                 return (ENOENT);
295         switch (which) {
296         case NEXUS_IVAR_NODE:
297                 *result = dinfo->ndi_node;
298                 break;
299         case NEXUS_IVAR_NAME:
300                 *result = (uintptr_t)dinfo->ndi_name;
301                 break;
302         case NEXUS_IVAR_DEVICE_TYPE:
303                 *result = (uintptr_t)dinfo->ndi_device_type;
304                 break;
305         case NEXUS_IVAR_COMPATIBLE:
306                 *result = (uintptr_t)dinfo->ndi_compatible;
307                 break;
308         default:
309                 return (ENOENT);
310         }
311         return 0;
312 }
313
314 static int
315 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
316 {
317         struct nexus_devinfo *dinfo;
318
319         if ((dinfo = device_get_ivars(child)) == 0)
320                 return (ENOENT);
321
322         switch (which) {
323         case NEXUS_IVAR_NAME:
324                 return (EINVAL);
325
326         /* Identified devices may want to set these */
327         case NEXUS_IVAR_NODE:
328                 dinfo->ndi_node = (phandle_t)value;
329                 break;
330
331         case NEXUS_IVAR_DEVICE_TYPE:
332                 dinfo->ndi_device_type = (char *)value;
333                 break;
334
335         default:
336                 return (ENOENT);
337         }
338         return 0;
339 }
340
341 static int
342 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
343     driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep)
344 {
345         driver_t        *driver;
346         int             error;
347
348         /* somebody tried to setup an irq that failed to allocate! */
349         if (res == NULL)
350                 panic("nexus_setup_intr: NULL irq resource!");
351
352         *cookiep = 0;
353         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
354                 flags |= INTR_EXCL;
355
356         driver = device_get_driver(child);
357
358         /*
359          * We depend here on rman_activate_resource() being idempotent.
360          */
361         error = rman_activate_resource(res);
362         if (error)
363                 return (error);
364
365         error = powerpc_setup_intr(device_get_nameunit(child),
366             rman_get_start(res), filter, ihand, arg, flags, cookiep);
367
368         return (error);
369 }
370
371 static int
372 nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
373     void *cookie)
374 {
375
376         return (powerpc_teardown_intr(cookie));
377 }
378
379 /*
380  * Allocate resources at the behest of a child.  This only handles interrupts,
381  * since I/O resources are handled by child busses.
382  */
383 static struct resource *
384 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
385     u_long start, u_long end, u_long count, u_int flags)
386 {
387         struct nexus_softc *sc;
388         struct resource *rv;
389
390         if (type != SYS_RES_IRQ) {
391                 device_printf(bus, "unknown resource request from %s\n",
392                     device_get_nameunit(child));
393                 return (NULL);
394         }
395
396         if (count == 0 || start + count - 1 != end) {
397                 device_printf(bus, "invalid IRQ allocation from %s\n",
398                     device_get_nameunit(child));
399                 return (NULL);
400         }
401
402         sc = device_get_softc(bus);
403
404         rv = rman_reserve_resource(&sc->sc_rman, start, end, count,
405             flags, child);
406         if (rv == NULL) {
407                 device_printf(bus, "IRQ allocation failed for %s\n",
408                     device_get_nameunit(child));
409         } else
410                 rman_set_rid(rv, *rid);
411
412         return (rv);
413 }
414
415 static int
416 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
417     struct resource *res)
418 {
419
420         /* Not much to be done yet... */
421         return (rman_activate_resource(res));
422 }
423
424 static int
425 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
426     struct resource *res)
427 {
428
429         /* Not much to be done yet... */
430         return (rman_deactivate_resource(res));
431 }
432
433 static int
434 nexus_release_resource(device_t bus, device_t child, int type, int rid,
435     struct resource *res)
436 {
437
438         if (type != SYS_RES_IRQ) {
439                 device_printf(bus, "unknown resource request from %s\n",
440                     device_get_nameunit(child));
441                 return (EINVAL);
442         }
443
444         return (rman_release_resource(res));
445 }
446
447 static device_t
448 nexus_device_from_node(device_t parent, phandle_t node)
449 {
450         device_t        cdev;
451         struct          nexus_devinfo *dinfo;
452         char            *name, *type, *compatible;
453
454         OF_getprop_alloc(node, "name", 1, (void **)&name);
455         OF_getprop_alloc(node, "device_type", 1, (void **)&type);
456         OF_getprop_alloc(node, "compatible", 1, (void **)&compatible);
457         cdev = device_add_child(parent, NULL, -1);
458         if (cdev != NULL) {
459                 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
460                 dinfo->ndi_node = node;
461                 dinfo->ndi_name = name;
462                 dinfo->ndi_device_type = type;
463                 dinfo->ndi_compatible = compatible;
464                 device_set_ivars(cdev, dinfo);
465         } else
466                 free(name, M_OFWPROP);
467
468         return (cdev);
469 }
470
471 static const char *
472 nexus_ofw_get_name(device_t bus, device_t dev)
473 {
474         struct nexus_devinfo *dinfo;
475
476         if ((dinfo = device_get_ivars(dev)) == NULL)
477                 return (NULL);
478         
479         return (dinfo->ndi_name);
480 }
481
482 static phandle_t
483 nexus_ofw_get_node(device_t bus, device_t dev)
484 {
485         struct nexus_devinfo *dinfo;
486
487         if ((dinfo = device_get_ivars(dev)) == NULL)
488                 return (0);
489         
490         return (dinfo->ndi_node);
491 }
492
493 static const char *
494 nexus_ofw_get_type(device_t bus, device_t dev)
495 {
496         struct nexus_devinfo *dinfo;
497
498         if ((dinfo = device_get_ivars(dev)) == NULL)
499                 return (NULL);
500         
501         return (dinfo->ndi_device_type);
502 }
503
504 static const char *
505 nexus_ofw_get_compat(device_t bus, device_t dev)
506 {
507         struct nexus_devinfo *dinfo;
508
509         if ((dinfo = device_get_ivars(dev)) == NULL)
510                 return (NULL);
511         
512         return (dinfo->ndi_compatible);
513 }
514
515 #define DIFF19041970    2082844800
516
517 static int
518 nexus_gettime(device_t dev, struct timespec *ts)
519 {
520         char path[128];
521         ihandle_t ih;
522         phandle_t ph;
523         u_int rtc;
524
525         ph = OF_finddevice("rtc");
526         if (ph == -1)
527                 return (ENOENT);
528
529         OF_package_to_path(ph, path, sizeof(path));
530         ih = OF_open(path);
531         if (ih == -1)
532                 return (ENXIO);
533
534         if (OF_call_method("read-rtc", ih, 0, 1, &rtc))
535                 return (EIO);
536
537         ts->tv_sec = rtc - DIFF19041970;
538         ts->tv_nsec = 0;
539         return (0);
540 }
541
542 static int
543 nexus_settime(device_t dev, struct timespec *ts)
544 {
545         char path[128];
546         ihandle_t ih;
547         phandle_t ph;     
548         u_int rtc;
549
550         ph = OF_finddevice("rtc");     
551         if (ph == -1)     
552                 return (ENOENT);
553
554         OF_package_to_path(ph, path, sizeof(path));                   
555         ih = OF_open(path);
556         if (ih == -1)
557                 return (ENXIO);
558
559         rtc = ts->tv_sec + DIFF19041970;
560         return ((OF_call_method("write-rtc", ih, 1, 0, rtc) != 0) ? EIO : 0);
561 }