]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/powerpc/powerpc/nexus.c
MFC r256814, r256816, r256818, r256846, r256855, r256864 (by nwhitehorn):
[FreeBSD/stable/10.git] / sys / powerpc / powerpc / 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.h>
68 #include <dev/ofw/ofw_bus_subr.h>
69 #include <dev/ofw/openfirm.h>
70
71 #include <machine/bus.h>
72 #include <machine/frame.h>
73 #include <machine/intr_machdep.h>
74 #include <machine/resource.h>
75
76 #include <sys/rman.h>
77
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 struct nexus_devinfo {
93         struct ofw_bus_devinfo ndi_ofwinfo;
94 };
95
96 struct nexus_softc {
97         struct rman     sc_rman;
98 };
99
100 /*
101  * Device interface
102  */
103 static int      nexus_probe(device_t);
104 static int      nexus_attach(device_t);
105
106 /*
107  * Bus interface
108  */
109 static device_t nexus_add_child(device_t, u_int, const char *, int);
110 static void     nexus_probe_nomatch(device_t, device_t);
111 #ifdef SMP
112 static int      nexus_bind_intr(device_t dev, device_t child,
113                     struct resource *irq, int cpu);
114 #endif
115 static int      nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
116                     enum intr_polarity pol);
117 static int      nexus_setup_intr(device_t, device_t, struct resource *, int,
118                     driver_filter_t *, driver_intr_t *, void *, void **);
119 static int      nexus_teardown_intr(device_t, device_t, struct resource *,
120                     void *);
121 static struct   resource *nexus_alloc_resource(device_t, device_t, int, int *,
122                     u_long, u_long, u_long, u_int);
123 static int      nexus_activate_resource(device_t, device_t, int, int,
124                     struct resource *);
125 static int      nexus_deactivate_resource(device_t, device_t, int, int,
126                     struct resource *);
127 static int      nexus_release_resource(device_t, device_t, int, int,
128                     struct resource *);
129 static const struct ofw_bus_devinfo *nexus_get_devinfo(device_t dev,
130                     device_t child);
131
132 /*
133  * Local routines
134  */
135 static device_t nexus_device_from_node(device_t, phandle_t);
136
137 static device_method_t nexus_methods[] = {
138         /* Device interface */
139         DEVMETHOD(device_probe,         nexus_probe),
140         DEVMETHOD(device_attach,        nexus_attach),
141         DEVMETHOD(device_detach,        bus_generic_detach),
142         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
143         DEVMETHOD(device_suspend,       bus_generic_suspend),
144         DEVMETHOD(device_resume,        bus_generic_resume),
145
146         /* Bus interface. Resource management is business of the children... */
147         DEVMETHOD(bus_add_child,        nexus_add_child),
148         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
149         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
150         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
151         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
152 #ifdef SMP
153         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
154 #endif
155         DEVMETHOD(bus_config_intr,      nexus_config_intr),
156         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
157         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
158         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
159         DEVMETHOD(bus_release_resource, nexus_release_resource),
160
161         /* OFW bus interface */
162         DEVMETHOD(ofw_bus_get_devinfo,  nexus_get_devinfo),
163         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
164         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
165         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
166         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
167         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
168
169         DEVMETHOD_END
170 };
171
172 static driver_t nexus_driver = {
173         "nexus",
174         nexus_methods,
175         sizeof(struct nexus_softc),
176 };
177
178 static devclass_t nexus_devclass;
179
180 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
181     BUS_PASS_BUS);
182
183 static int
184 nexus_probe(device_t dev)
185 {
186         bus_generic_probe(dev);
187         device_set_desc(dev, "Open Firmware Nexus device");
188         return (0);
189 }
190
191 static int
192 nexus_attach(device_t dev)
193 {
194         phandle_t       root;
195         phandle_t       child;
196         struct          nexus_softc *sc;
197         u_long          start, end;
198
199         sc = device_get_softc(dev);
200
201         start = 0;
202         end = ~0;
203
204         sc->sc_rman.rm_start = start;
205         sc->sc_rman.rm_end = end;
206         sc->sc_rman.rm_type = RMAN_ARRAY;
207         sc->sc_rman.rm_descr = "Interrupt request lines";
208         if (rman_init(&sc->sc_rman) ||
209             rman_manage_region(&sc->sc_rman, start, end))
210                 panic("nexus_probe IRQ rman");
211
212         if ((root = OF_peer(0)) == 0)
213                 return (bus_generic_attach(dev));
214                 
215         /*
216          * Now walk the OFW tree to locate top-level devices
217          */
218         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
219                 if (child == -1)
220                         panic("nexus_probe(): OF_child failed.");
221                 (void)nexus_device_from_node(dev, child);
222
223         }
224
225         return (bus_generic_attach(dev));
226 }
227
228 static void
229 nexus_probe_nomatch(device_t dev, device_t child)
230 {
231         const char *name, *type;
232
233         name = ofw_bus_get_name(child);
234         type = ofw_bus_get_type(child);
235
236         if (name == NULL)
237                 name = "unknown";
238         if (type == NULL)
239                 type = "(unknown)";
240
241         if (bootverbose)
242                 device_printf(dev, "<%s>, type %s (no driver attached)\n",
243                     name, type);
244 }
245
246 static device_t
247 nexus_add_child(device_t dev, u_int order, const char *name, int unit)
248 {
249         device_t child;
250         struct nexus_devinfo *dinfo;
251
252         child = device_add_child_ordered(dev, order, name, unit);
253         if (child == NULL)
254                 return (NULL);
255
256         dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
257         if (dinfo == NULL)
258                 return (NULL);
259
260         dinfo->ndi_ofwinfo.obd_node = -1;
261         dinfo->ndi_ofwinfo.obd_name = NULL;
262         dinfo->ndi_ofwinfo.obd_compat = NULL;
263         dinfo->ndi_ofwinfo.obd_type = NULL;
264         dinfo->ndi_ofwinfo.obd_model = NULL;
265
266         device_set_ivars(child, dinfo);
267
268         return (child);
269 }
270
271 static int
272 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
273     driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep)
274 {
275         int             error;
276
277         /* somebody tried to setup an irq that failed to allocate! */
278         if (res == NULL)
279                 panic("nexus_setup_intr: NULL irq resource!");
280
281         *cookiep = 0;
282         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
283                 flags |= INTR_EXCL;
284
285         /*
286          * We depend here on rman_activate_resource() being idempotent.
287          */
288         error = rman_activate_resource(res);
289         if (error)
290                 return (error);
291
292         error = powerpc_setup_intr(device_get_nameunit(child),
293             rman_get_start(res), filter, ihand, arg, flags, cookiep);
294
295         return (error);
296 }
297
298 static int
299 nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
300     void *cookie)
301 {
302
303         return (powerpc_teardown_intr(cookie));
304 }
305
306 #ifdef SMP
307 static int
308 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
309 {
310
311         return (powerpc_bind_intr(rman_get_start(irq), cpu));
312 }
313 #endif
314         
315 static int
316 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
317     enum intr_polarity pol)
318 {
319
320         return (powerpc_config_intr(irq, trig, pol));
321 }
322
323 /*
324  * Allocate resources at the behest of a child.  This only handles interrupts,
325  * since I/O resources are handled by child busses.
326  */
327 static struct resource *
328 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
329     u_long start, u_long end, u_long count, u_int flags)
330 {
331         struct nexus_softc *sc;
332         struct resource *rv;
333
334         if (type != SYS_RES_IRQ) {
335                 device_printf(bus, "unknown resource request from %s\n",
336                     device_get_nameunit(child));
337                 return (NULL);
338         }
339
340         if (count == 0 || start + count - 1 != end) {
341                 device_printf(bus, "invalid IRQ allocation from %s\n",
342                     device_get_nameunit(child));
343                 return (NULL);
344         }
345
346         sc = device_get_softc(bus);
347
348         rv = rman_reserve_resource(&sc->sc_rman, start, end, count,
349             flags, child);
350         if (rv == NULL) {
351                 device_printf(bus, "IRQ allocation failed for %s\n",
352                     device_get_nameunit(child));
353         } else
354                 rman_set_rid(rv, *rid);
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
382         if (type != SYS_RES_IRQ) {
383                 device_printf(bus, "unknown resource request from %s\n",
384                     device_get_nameunit(child));
385                 return (EINVAL);
386         }
387
388         return (rman_release_resource(res));
389 }
390
391 static device_t
392 nexus_device_from_node(device_t parent, phandle_t node)
393 {
394         device_t        cdev;
395         struct          nexus_devinfo *dinfo;
396
397         cdev = device_add_child(parent, NULL, -1);
398         if (cdev != NULL) {
399                 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
400                 ofw_bus_gen_setup_devinfo(&dinfo->ndi_ofwinfo, node);
401                 device_set_ivars(cdev, dinfo);
402         }
403
404         return (cdev);
405 }
406
407 static const struct ofw_bus_devinfo *
408 nexus_get_devinfo(device_t dev, device_t child)
409 {
410         struct nexus_devinfo *dinfo;
411
412         dinfo = device_get_ivars(child);
413         return (&dinfo->ndi_ofwinfo);
414 }
415