openpic_ofw.c revision 124469
1/*
2 * Copyright 2003 by Peter Grehan. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29/*
30 * The macio attachment for the OpenPIC interrupt controller.
31 * A nexus driver is defined so the number of interrupts can be
32 * determined early in the boot sequence before the hardware
33 * is accessed - the interrupt i/f is installed at this time,
34 * and when h/w is finally accessed, interrupt sources allocated
35 * prior to this are activated
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/powerpc/powermac/openpic_macio.c 124469 2004-01-13 11:24:36Z grehan $");
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/bus.h>
44#include <sys/conf.h>
45#include <sys/kernel.h>
46
47#include <dev/ofw/openfirm.h>
48
49#include <machine/bus.h>
50#include <machine/intr.h>
51#include <machine/intr_machdep.h>
52#include <machine/md_var.h>
53#include <machine/nexusvar.h>
54#include <machine/pio.h>
55#include <machine/resource.h>
56
57#include <vm/vm.h>
58#include <vm/pmap.h>
59
60#include <sys/rman.h>
61
62#include <machine/openpicvar.h>
63#include <powerpc/powermac/maciovar.h>
64
65#include "pic_if.h"
66
67struct openpic_ofw_softc {
68	struct openpic_softc osc;
69	struct resource *sc_memr;	/* macio bus resource */
70	device_t	sc_ndev;	/* nexus device */
71};
72
73static struct openpic_ofw_softc *ofwpicsoftc;
74
75/*
76 * MacIO interface
77 */
78static void	openpic_ofw_identify(driver_t *, device_t);
79static int	openpic_ofw_probe(device_t);
80static int	openpic_ofw_attach(device_t);
81static int	openpic_macio_probe(device_t);
82static int	openpic_macio_attach(device_t);
83
84/*
85 * Nexus attachment
86 */
87static device_method_t  openpic_ofw_methods[] = {
88	/* Device interface */
89	DEVMETHOD(device_identify,	openpic_ofw_identify),
90	DEVMETHOD(device_probe,		openpic_ofw_probe),
91	DEVMETHOD(device_attach,	openpic_ofw_attach),
92
93	/* PIC interface */
94	DEVMETHOD(pic_allocate_intr,	openpic_allocate_intr),
95	DEVMETHOD(pic_setup_intr,	openpic_setup_intr),
96	DEVMETHOD(pic_teardown_intr,	openpic_teardown_intr),
97	DEVMETHOD(pic_release_intr,	openpic_release_intr),
98
99	{ 0, 0 }
100};
101
102static driver_t openpic_ofw_driver = {
103	"openpic",
104	openpic_ofw_methods,
105	sizeof(struct openpic_ofw_softc)
106};
107
108static devclass_t openpic_ofw_devclass;
109
110DRIVER_MODULE(openpic_ofw, nexus, openpic_ofw_driver, openpic_ofw_devclass,
111    0, 0);
112
113static void
114openpic_ofw_identify(driver_t *driver, device_t parent)
115{
116	device_t child;
117	phandle_t chosen, pic;
118	char type[40];
119
120	chosen = OF_finddevice("/chosen");
121	if (chosen == -1)
122		return;
123
124	if (OF_getprop(chosen, "interrupt-controller", &pic, 4) != 4)
125		return;
126
127	OF_getprop(pic, "device_type", type, sizeof(type));
128
129	if (strcmp(type, "open-pic") != 0)
130		return;
131
132	child = BUS_ADD_CHILD(parent, 0, "openpic", 0);
133	if (child != NULL)
134		nexus_set_device_type(child, "macio");
135}
136
137static int
138openpic_ofw_probe(device_t dev)
139{
140	char	*name;
141	char	*type;
142
143	name = nexus_get_name(dev);
144	type = nexus_get_device_type(dev);
145
146	if (strcmp(name, "openpic") != 0 ||
147	    strcmp(type, "macio") != 0)
148		return (ENXIO);
149
150	device_set_desc(dev, OPENPIC_DEVSTR);
151	return (0);
152}
153
154static int
155openpic_ofw_attach(device_t dev)
156{
157	KASSERT(ofwpicsoftc == NULL, ("ofw openpic: already probed"));
158	ofwpicsoftc = device_get_softc(dev);
159	ofwpicsoftc->sc_ndev = dev;
160
161	nexus_install_intcntlr(dev);
162	openpic_early_attach(dev);
163	return (0);
164}
165
166/*
167 * MacIO attachment
168 */
169static device_method_t  openpic_macio_methods[] = {
170	/* Device interface */
171	DEVMETHOD(device_probe,		openpic_macio_probe),
172	DEVMETHOD(device_attach,	openpic_macio_attach),
173
174	{ 0, 0 },
175};
176
177static driver_t openpic_macio_driver = {
178	"openpicmacio",
179	openpic_macio_methods,
180	0
181};
182
183static devclass_t openpic_macio_devclass;
184
185DRIVER_MODULE(openpicmacio, macio, openpic_macio_driver,
186    openpic_macio_devclass, 0, 0);
187
188static int
189openpic_macio_probe(device_t dev)
190{
191	char *type = macio_get_devtype(dev);
192
193	if (strcmp(type, "open-pic") != 0)
194                return (ENXIO);
195
196	/*
197	 * The description was already printed out in the nexus
198	 * probe, so don't do it again here
199	 */
200	device_set_desc(dev, "OpenPIC MacIO interrupt cell");
201	if (!bootverbose)
202		device_quiet(dev);
203	return (0);
204}
205
206static int
207openpic_macio_attach(device_t dev)
208{
209	struct openpic_ofw_softc *sc;
210	int rid;
211
212	sc = ofwpicsoftc;
213	KASSERT(sc != NULL, ("pic not nexus-probed\n"));
214
215	rid = 0;
216	sc->sc_memr = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1,
217	     RF_ACTIVE);
218
219	if (sc->sc_memr == NULL) {
220		device_printf(dev, "Could not alloc mem resource!\n");
221		return (ENXIO);
222	}
223
224	sc->osc.sc_bt = rman_get_bustag(sc->sc_memr);
225	sc->osc.sc_bh = rman_get_bushandle(sc->sc_memr);
226	sc->osc.sc_altdev = dev;
227
228	return (openpic_attach(sc->sc_ndev));
229}
230
231
232