ofwbus.c revision 124468
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: head/sys/powerpc/aim/nexus.c 124468 2004-01-13 11:20:52Z grehan $
56 */
57#include "opt_psim.h"
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/bus.h>
62#include <sys/cons.h>
63#include <sys/kernel.h>
64#include <sys/malloc.h>
65
66#include <dev/ofw/openfirm.h>
67
68#include <machine/bus.h>
69#include <machine/frame.h>
70#include <machine/intr_machdep.h>
71#include <machine/nexusvar.h>
72#include <machine/resource.h>
73
74#include <sys/rman.h>
75
76#include "pic_if.h"
77
78/*
79 * The nexus (which is a pseudo-bus actually) iterates over the nodes that
80 * exist in OpenFirmware and adds them as devices to this bus so that drivers
81 * can be attached to them.
82 *
83 * Maybe this code should get into dev/ofw to some extent, as some of it should
84 * work for all OpenFirmware based machines...
85 */
86
87static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
88
89struct nexus_devinfo {
90	phandle_t	ndi_node;
91	/* Some common properties. */
92	char     	*ndi_name;
93	char     	*ndi_device_type;
94	char     	*ndi_compatible;
95};
96
97struct nexus_softc {
98	device_t	sc_pic;
99};
100
101/*
102 * Device interface
103 */
104static int	nexus_probe(device_t);
105static void	nexus_probe_nomatch(device_t, device_t);
106
107/*
108 * Bus interface
109 */
110static device_t nexus_add_child(device_t, int, const char *, int);
111static int	nexus_read_ivar(device_t, device_t, int, uintptr_t *);
112static int	nexus_write_ivar(device_t, device_t, int, uintptr_t);
113static int	nexus_setup_intr(device_t, device_t, struct resource *, int,
114		    driver_intr_t *, void *, void **);
115static int	nexus_teardown_intr(device_t, device_t, struct resource *,
116		    void *);
117static struct	resource *nexus_alloc_resource(device_t, device_t, int, int *,
118		    u_long, u_long, u_long, u_int);
119static int	nexus_activate_resource(device_t, device_t, int, int,
120		    struct resource *);
121static int	nexus_deactivate_resource(device_t, device_t, int, int,
122		    struct resource *);
123static int	nexus_release_resource(device_t, device_t, int, int,
124		    struct resource *);
125
126/*
127 * Local routines
128 */
129static device_t	nexus_device_from_node(device_t, phandle_t);
130
131static device_method_t nexus_methods[] = {
132	/* Device interface */
133	DEVMETHOD(device_probe,		nexus_probe),
134	DEVMETHOD(device_attach,	bus_generic_attach),
135	DEVMETHOD(device_detach,	bus_generic_detach),
136	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
137	DEVMETHOD(device_suspend,	bus_generic_suspend),
138	DEVMETHOD(device_resume,	bus_generic_resume),
139
140	/* Bus interface. Resource management is business of the children... */
141	DEVMETHOD(bus_add_child,	nexus_add_child),
142	DEVMETHOD(bus_print_child,	bus_generic_print_child),
143	DEVMETHOD(bus_probe_nomatch,	nexus_probe_nomatch),
144	DEVMETHOD(bus_read_ivar,	nexus_read_ivar),
145	DEVMETHOD(bus_write_ivar,	nexus_write_ivar),
146	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
147	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
148	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
149	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
150	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
151	DEVMETHOD(bus_release_resource,	nexus_release_resource),
152
153	{ 0, 0 }
154};
155
156static driver_t nexus_driver = {
157	"nexus",
158	nexus_methods,
159	sizeof(struct nexus_softc),
160};
161
162static devclass_t nexus_devclass;
163
164DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
165
166static int
167nexus_probe(device_t dev)
168{
169	phandle_t	root;
170	phandle_t	child;
171	struct		nexus_softc *sc;
172
173	if ((root = OF_peer(0)) == -1)
174		panic("nexus_probe: OF_peer failed.");
175
176	sc = device_get_softc(dev);
177
178	/*
179	 * Allow devices to identify
180	 */
181	bus_generic_probe(dev);
182
183	/*
184	 * Now walk the OFW tree to locate top-level devices
185	 */
186	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
187		if (child == -1)
188			panic("nexus_probe(): OF_child failed.");
189		(void)nexus_device_from_node(dev, child);
190
191	}
192	device_set_desc(dev, "OpenFirmware Nexus device");
193	return (0);
194}
195
196static void
197nexus_probe_nomatch(device_t dev, device_t child)
198{
199	char	*name, *type;
200
201	if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
202	    (uintptr_t *)&name) != 0 ||
203	    BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
204	    (uintptr_t *)&type) != 0)
205		return;
206
207	if (type == NULL)
208		type = "(unknown)";
209
210	if (bootverbose)
211		device_printf(dev, "<%s>, type %s (no driver attached)\n",
212		    name, type);
213}
214
215static device_t
216nexus_add_child(device_t dev, int order, const char *name, int unit)
217{
218	device_t child;
219	struct nexus_devinfo *dinfo;
220
221	child = device_add_child_ordered(dev, order, name, unit);
222	if (child == NULL)
223		return (NULL);
224
225	dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO);
226	if (dinfo == NULL)
227		return (NULL);
228
229	dinfo->ndi_node = -1;
230	dinfo->ndi_name = name;
231	device_set_ivars(child, dinfo);
232
233        return (child);
234}
235
236
237static int
238nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
239{
240	struct nexus_devinfo *dinfo;
241
242	if ((dinfo = device_get_ivars(child)) == 0)
243		return (ENOENT);
244	switch (which) {
245	case NEXUS_IVAR_NODE:
246		*result = dinfo->ndi_node;
247		break;
248	case NEXUS_IVAR_NAME:
249		*result = (uintptr_t)dinfo->ndi_name;
250		break;
251	case NEXUS_IVAR_DEVICE_TYPE:
252		*result = (uintptr_t)dinfo->ndi_device_type;
253		break;
254	case NEXUS_IVAR_COMPATIBLE:
255		*result = (uintptr_t)dinfo->ndi_compatible;
256		break;
257	default:
258		return (ENOENT);
259	}
260	return 0;
261}
262
263static int
264nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
265{
266	struct nexus_devinfo *dinfo;
267
268	if ((dinfo = device_get_ivars(child)) == 0)
269		return (ENOENT);
270
271	switch (which) {
272	case NEXUS_IVAR_NODE:
273	case NEXUS_IVAR_NAME:
274		return (EINVAL);
275
276	/* Identified devices will want to set this */
277	case NEXUS_IVAR_DEVICE_TYPE:
278		dinfo->ndi_device_type = (char *)value;
279		break;
280
281	default:
282		return (ENOENT);
283	}
284	return 0;
285}
286
287static int
288nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
289    driver_intr_t *intr, void *arg, void **cookiep)
290{
291	struct	nexus_softc *sc;
292
293	sc = device_get_softc(dev);
294
295	if (device_get_state(sc->sc_pic) != DS_ATTACHED)
296		panic("nexus_setup_intr: no pic attached\n");
297
298	return (PIC_SETUP_INTR(sc->sc_pic, child, res, flags, intr, arg,
299	    cookiep));
300}
301
302static int
303nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
304    void *ih)
305{
306	struct	nexus_softc *sc;
307
308	sc = device_get_softc(dev);
309
310	if (device_get_state(sc->sc_pic) != DS_ATTACHED)
311		panic("nexus_teardown_intr: no pic attached\n");
312
313	return (PIC_TEARDOWN_INTR(sc->sc_pic, child, res, ih));
314}
315
316/*
317 * Allocate resources at the behest of a child.  This only handles interrupts,
318 * since I/O resources are handled by child busses.
319 */
320static struct resource *
321nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
322    u_long start, u_long end, u_long count, u_int flags)
323{
324	struct	nexus_softc *sc;
325	struct	resource *rv;
326
327	sc = device_get_softc(bus);
328
329	if (type != SYS_RES_IRQ) {
330		device_printf(bus, "unknown resource request from %s\n",
331		    device_get_nameunit(child));
332		return (NULL);
333	}
334
335	if (device_get_state(sc->sc_pic) != DS_ATTACHED)
336		panic("nexus_alloc_resource: no pic attached\n");
337
338	rv = PIC_ALLOCATE_INTR(sc->sc_pic, child, rid, start, flags);
339
340	return (rv);
341}
342
343static int
344nexus_activate_resource(device_t bus, device_t child, int type, int rid,
345    struct resource *res)
346{
347
348	/* Not much to be done yet... */
349	return (rman_activate_resource(res));
350}
351
352static int
353nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
354    struct resource *res)
355{
356
357	/* Not much to be done yet... */
358	return (rman_deactivate_resource(res));
359}
360
361static int
362nexus_release_resource(device_t bus, device_t child, int type, int rid,
363    struct resource *res)
364{
365	struct	nexus_softc *sc;
366
367	sc = device_get_softc(bus);
368
369	if (type != SYS_RES_IRQ) {
370		device_printf(bus, "unknown resource request from %s\n",
371		    device_get_nameunit(child));
372		return (EINVAL);
373	}
374
375	if (device_get_state(sc->sc_pic) != DS_ATTACHED)
376		panic("nexus_release_resource: no pic attached\n");
377
378	return (PIC_RELEASE_INTR(sc->sc_pic, child, rid, res));
379}
380
381static device_t
382nexus_device_from_node(device_t parent, phandle_t node)
383{
384	device_t	cdev;
385	struct		nexus_devinfo *dinfo;
386	char		*name, *type, *compatible;
387
388	OF_getprop_alloc(node, "name", 1, (void **)&name);
389	OF_getprop_alloc(node, "device_type", 1, (void **)&type);
390	OF_getprop_alloc(node, "compatible", 1, (void **)&compatible);
391	cdev = device_add_child(parent, NULL, -1);
392	if (cdev != NULL) {
393		dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
394		dinfo->ndi_node = node;
395		dinfo->ndi_name = name;
396		dinfo->ndi_device_type = type;
397		dinfo->ndi_compatible = compatible;
398		device_set_ivars(cdev, dinfo);
399	} else
400		free(name, M_OFWPROP);
401
402	return (cdev);
403}
404
405int
406nexus_install_intcntlr(device_t dev)
407{
408	struct	nexus_softc *sc;
409
410	sc = device_get_softc(device_get_parent(dev));
411	sc->sc_pic = dev;
412
413	return (0);
414}
415