ofwbus.c revision 205506
11558Srgrimes/*-
254263Sshin * Copyright 1998 Massachusetts Institute of Technology
31558Srgrimes *
4160747Syar * Permission to use, copy, modify, and distribute this software and
5160747Syar * its documentation for any purpose and without fee is hereby
61558Srgrimes * granted, provided that both the above copyright notice and this
774815Sru * permission notice appear in all copies, that both the above
824554Sphk * copyright notice and this permission notice appear in all
9126178Sjohan * supporting documentation, and that the name of M.I.T. not be used
1094499Sru * in advertising or publicity pertaining to distribution of the
111558Srgrimes * software without specific, written prior permission.  M.I.T. makes
12160747Syar * no representations about the suitability of this software for any
13160747Syar * purpose.  It is provided "as is" without express or implied
14160747Syar * warranty.
15160747Syar *
16160747Syar * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17160747Syar * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18160747Syar * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19160747Syar * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
201558Srgrimes * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211558Srgrimes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22118449Sache * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
231558Srgrimes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
241558Srgrimes * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
251558Srgrimes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
261558Srgrimes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes/*-
301558Srgrimes * 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: head/sys/powerpc/aim/nexus.c 205506 2010-03-23 03:14:44Z nwhitehorn $
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/resource.h>
73
74#include <sys/rman.h>
75
76#include "ofw_bus_if.h"
77#include "pic_if.h"
78
79/*
80 * The nexus (which is a pseudo-bus actually) iterates over the nodes that
81 * exist in Open Firmware and adds them as devices to this bus so that drivers
82 * can be attached to them.
83 *
84 * Maybe this code should get into dev/ofw to some extent, as some of it should
85 * work for all Open Firmware based machines...
86 */
87
88static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
89
90enum nexus_ivars {
91	NEXUS_IVAR_NODE,
92	NEXUS_IVAR_NAME,
93	NEXUS_IVAR_DEVICE_TYPE,
94	NEXUS_IVAR_COMPATIBLE,
95};
96
97struct nexus_devinfo {
98	phandle_t	ndi_node;
99	/* Some common properties. */
100	const char     	*ndi_name;
101	const char     	*ndi_device_type;
102	const char     	*ndi_compatible;
103};
104
105struct nexus_softc {
106	struct rman	sc_rman;
107};
108
109/*
110 * Device interface
111 */
112static int	nexus_probe(device_t);
113static int	nexus_attach(device_t);
114
115/*
116 * Bus interface
117 */
118static device_t nexus_add_child(device_t, int, const char *, int);
119static void	nexus_probe_nomatch(device_t, device_t);
120static int	nexus_read_ivar(device_t, device_t, int, uintptr_t *);
121static int	nexus_write_ivar(device_t, device_t, int, uintptr_t);
122static int	nexus_setup_intr(device_t, device_t, struct resource *, int,
123		    driver_filter_t *, driver_intr_t *, void *, void **);
124static int	nexus_teardown_intr(device_t, device_t, struct resource *,
125		    void *);
126static struct	resource *nexus_alloc_resource(device_t, device_t, int, int *,
127		    u_long, u_long, u_long, u_int);
128static int	nexus_activate_resource(device_t, device_t, int, int,
129		    struct resource *);
130static int	nexus_deactivate_resource(device_t, device_t, int, int,
131		    struct resource *);
132static int	nexus_release_resource(device_t, device_t, int, int,
133		    struct resource *);
134
135/*
136 * OFW bus interface.
137 */
138static phandle_t	 nexus_ofw_get_node(device_t, device_t);
139static const char	*nexus_ofw_get_name(device_t, device_t);
140static const char	*nexus_ofw_get_type(device_t, device_t);
141static const char	*nexus_ofw_get_compat(device_t, device_t);
142
143/*
144 * Local routines
145 */
146static device_t	nexus_device_from_node(device_t, phandle_t);
147
148static device_method_t nexus_methods[] = {
149	/* Device interface */
150	DEVMETHOD(device_probe,		nexus_probe),
151	DEVMETHOD(device_attach,	nexus_attach),
152	DEVMETHOD(device_detach,	bus_generic_detach),
153	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
154	DEVMETHOD(device_suspend,	bus_generic_suspend),
155	DEVMETHOD(device_resume,	bus_generic_resume),
156
157	/* Bus interface. Resource management is business of the children... */
158	DEVMETHOD(bus_add_child,	nexus_add_child),
159	DEVMETHOD(bus_print_child,	bus_generic_print_child),
160	DEVMETHOD(bus_probe_nomatch,	nexus_probe_nomatch),
161	DEVMETHOD(bus_read_ivar,	nexus_read_ivar),
162	DEVMETHOD(bus_write_ivar,	nexus_write_ivar),
163	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
164	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
165	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
166	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
167	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
168	DEVMETHOD(bus_release_resource,	nexus_release_resource),
169
170	/* OFW bus interface */
171	DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node),
172	DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name),
173	DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type),
174	DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat),
175
176	{ 0, 0 }
177};
178
179static driver_t nexus_driver = {
180	"nexus",
181	nexus_methods,
182	sizeof(struct nexus_softc),
183};
184
185static devclass_t nexus_devclass;
186
187DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
188
189static int
190nexus_probe(device_t dev)
191{
192	bus_generic_probe(dev);
193	device_set_desc(dev, "Open Firmware Nexus device");
194	return (0);
195}
196
197static int
198nexus_attach(device_t dev)
199{
200	phandle_t	root;
201	phandle_t	child;
202	struct		nexus_softc *sc;
203	u_long		start, end;
204
205	if ((root = OF_peer(0)) == -1)
206		panic("nexus_probe: OF_peer failed.");
207
208	sc = device_get_softc(dev);
209
210	start = 0;
211	end = INTR_VECTORS - 1;
212
213	sc->sc_rman.rm_start = start;
214	sc->sc_rman.rm_end = end;
215	sc->sc_rman.rm_type = RMAN_ARRAY;
216	sc->sc_rman.rm_descr = "Interrupt request lines";
217	if (rman_init(&sc->sc_rman) ||
218	    rman_manage_region(&sc->sc_rman, start, end))
219		panic("nexus_probe IRQ rman");
220
221	/*
222	 * Now walk the OFW tree to locate top-level devices
223	 */
224	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
225		if (child == -1)
226			panic("nexus_probe(): OF_child failed.");
227		(void)nexus_device_from_node(dev, child);
228
229	}
230
231	return (bus_generic_attach(dev));
232}
233
234static void
235nexus_probe_nomatch(device_t dev, device_t child)
236{
237	char	*name, *type;
238
239	if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
240	    (uintptr_t *)&name) != 0 ||
241	    BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
242	    (uintptr_t *)&type) != 0)
243		return;
244
245	if (type == NULL)
246		type = "(unknown)";
247
248	if (bootverbose)
249		device_printf(dev, "<%s>, type %s (no driver attached)\n",
250		    name, type);
251}
252
253static device_t
254nexus_add_child(device_t dev, int order, const char *name, int unit)
255{
256	device_t child;
257	struct nexus_devinfo *dinfo;
258
259	child = device_add_child_ordered(dev, order, name, unit);
260	if (child == NULL)
261		return (NULL);
262
263	dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
264	if (dinfo == NULL)
265		return (NULL);
266
267	dinfo->ndi_node = -1;
268	dinfo->ndi_name = name;
269	device_set_ivars(child, dinfo);
270
271        return (child);
272}
273
274
275static int
276nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
277{
278	struct nexus_devinfo *dinfo;
279
280	if ((dinfo = device_get_ivars(child)) == 0)
281		return (ENOENT);
282	switch (which) {
283	case NEXUS_IVAR_NODE:
284		*result = dinfo->ndi_node;
285		break;
286	case NEXUS_IVAR_NAME:
287		*result = (uintptr_t)dinfo->ndi_name;
288		break;
289	case NEXUS_IVAR_DEVICE_TYPE:
290		*result = (uintptr_t)dinfo->ndi_device_type;
291		break;
292	case NEXUS_IVAR_COMPATIBLE:
293		*result = (uintptr_t)dinfo->ndi_compatible;
294		break;
295	default:
296		return (ENOENT);
297	}
298	return 0;
299}
300
301static int
302nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
303{
304	struct nexus_devinfo *dinfo;
305
306	if ((dinfo = device_get_ivars(child)) == 0)
307		return (ENOENT);
308
309	switch (which) {
310	case NEXUS_IVAR_NAME:
311		return (EINVAL);
312
313	/* Identified devices may want to set these */
314	case NEXUS_IVAR_NODE:
315		dinfo->ndi_node = (phandle_t)value;
316		break;
317
318	case NEXUS_IVAR_DEVICE_TYPE:
319		dinfo->ndi_device_type = (char *)value;
320		break;
321
322	default:
323		return (ENOENT);
324	}
325	return 0;
326}
327
328static int
329nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
330    driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep)
331{
332	driver_t	*driver;
333	int		error;
334
335	/* somebody tried to setup an irq that failed to allocate! */
336	if (res == NULL)
337		panic("nexus_setup_intr: NULL irq resource!");
338
339	*cookiep = 0;
340	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
341		flags |= INTR_EXCL;
342
343	driver = device_get_driver(child);
344
345	/*
346	 * We depend here on rman_activate_resource() being idempotent.
347	 */
348	error = rman_activate_resource(res);
349	if (error)
350		return (error);
351
352	error = powerpc_setup_intr(device_get_nameunit(child),
353	    rman_get_start(res), filter, ihand, arg, flags, cookiep);
354
355	return (error);
356}
357
358static int
359nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
360    void *cookie)
361{
362
363	return (powerpc_teardown_intr(cookie));
364}
365
366/*
367 * Allocate resources at the behest of a child.  This only handles interrupts,
368 * since I/O resources are handled by child busses.
369 */
370static struct resource *
371nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
372    u_long start, u_long end, u_long count, u_int flags)
373{
374	struct nexus_softc *sc;
375	struct resource *rv;
376
377	if (type != SYS_RES_IRQ) {
378		device_printf(bus, "unknown resource request from %s\n",
379		    device_get_nameunit(child));
380		return (NULL);
381	}
382
383	if (count == 0 || start + count - 1 != end) {
384		device_printf(bus, "invalid IRQ allocation from %s\n",
385		    device_get_nameunit(child));
386		return (NULL);
387	}
388
389	sc = device_get_softc(bus);
390
391	rv = rman_reserve_resource(&sc->sc_rman, start, end, count,
392	    flags, child);
393	if (rv == NULL) {
394		device_printf(bus, "IRQ allocation failed for %s\n",
395		    device_get_nameunit(child));
396	} else
397		rman_set_rid(rv, *rid);
398
399	return (rv);
400}
401
402static int
403nexus_activate_resource(device_t bus, device_t child, int type, int rid,
404    struct resource *res)
405{
406
407	/* Not much to be done yet... */
408	return (rman_activate_resource(res));
409}
410
411static int
412nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
413    struct resource *res)
414{
415
416	/* Not much to be done yet... */
417	return (rman_deactivate_resource(res));
418}
419
420static int
421nexus_release_resource(device_t bus, device_t child, int type, int rid,
422    struct resource *res)
423{
424
425	if (type != SYS_RES_IRQ) {
426		device_printf(bus, "unknown resource request from %s\n",
427		    device_get_nameunit(child));
428		return (EINVAL);
429	}
430
431	return (rman_release_resource(res));
432}
433
434static device_t
435nexus_device_from_node(device_t parent, phandle_t node)
436{
437	device_t	cdev;
438	struct		nexus_devinfo *dinfo;
439	char		*name, *type, *compatible;
440
441	OF_getprop_alloc(node, "name", 1, (void **)&name);
442	OF_getprop_alloc(node, "device_type", 1, (void **)&type);
443	OF_getprop_alloc(node, "compatible", 1, (void **)&compatible);
444	cdev = device_add_child(parent, NULL, -1);
445	if (cdev != NULL) {
446		dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
447		dinfo->ndi_node = node;
448		dinfo->ndi_name = name;
449		dinfo->ndi_device_type = type;
450		dinfo->ndi_compatible = compatible;
451		device_set_ivars(cdev, dinfo);
452	} else
453		free(name, M_OFWPROP);
454
455	return (cdev);
456}
457
458static const char *
459nexus_ofw_get_name(device_t bus, device_t dev)
460{
461	struct nexus_devinfo *dinfo;
462
463	if ((dinfo = device_get_ivars(dev)) == NULL)
464		return (NULL);
465
466	return (dinfo->ndi_name);
467}
468
469static phandle_t
470nexus_ofw_get_node(device_t bus, device_t dev)
471{
472	struct nexus_devinfo *dinfo;
473
474	if ((dinfo = device_get_ivars(dev)) == NULL)
475		return (0);
476
477	return (dinfo->ndi_node);
478}
479
480static const char *
481nexus_ofw_get_type(device_t bus, device_t dev)
482{
483	struct nexus_devinfo *dinfo;
484
485	if ((dinfo = device_get_ivars(dev)) == NULL)
486		return (NULL);
487
488	return (dinfo->ndi_device_type);
489}
490
491static const char *
492nexus_ofw_get_compat(device_t bus, device_t dev)
493{
494	struct nexus_devinfo *dinfo;
495
496	if ((dinfo = device_get_ivars(dev)) == NULL)
497		return (NULL);
498
499	return (dinfo->ndi_compatible);
500}
501
502