openpic_ofw.c revision 127135
1155191Srwatson/*
2155191Srwatson * Copyright 2003 by Peter Grehan. All rights reserved.
3155191Srwatson *
4155191Srwatson * Redistribution and use in source and binary forms, with or without
5155191Srwatson * modification, are permitted provided that the following conditions
6155191Srwatson * are met:
7155191Srwatson * 1. Redistributions of source code must retain the above copyright
8155191Srwatson *    notice, this list of conditions and the following disclaimer.
9155191Srwatson * 2. Redistributions in binary form must reproduce the above copyright
10155191Srwatson *    notice, this list of conditions and the following disclaimer in the
11155191Srwatson *    documentation and/or other materials provided with the distribution.
12155191Srwatson * 3. The name of the author may not be used to endorse or promote products
13155191Srwatson *    derived from this software without specific prior written permission.
14155191Srwatson *
15155191Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16155191Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17155191Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18155191Srwatson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19155191Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20155191Srwatson * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21155191Srwatson * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22155191Srwatson * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23155191Srwatson * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24155191Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25155191Srwatson * SUCH DAMAGE.
26155191Srwatson *
27155191Srwatson */
28155191Srwatson
29155191Srwatson/*
30155191Srwatson * The macio attachment for the OpenPIC interrupt controller.
31155191Srwatson * A nexus driver is defined so the number of interrupts can be
32155191Srwatson * determined early in the boot sequence before the hardware
33155191Srwatson * is accessed - the interrupt i/f is installed at this time,
34155191Srwatson * and when h/w is finally accessed, interrupt sources allocated
35155191Srwatson * prior to this are activated
36155191Srwatson */
37155191Srwatson
38155191Srwatson#include <sys/cdefs.h>
39155191Srwatson__FBSDID("$FreeBSD: head/sys/powerpc/powermac/openpic_macio.c 127135 2004-03-17 17:50:55Z njl $");
40155191Srwatson
41155191Srwatson#include <sys/param.h>
42155191Srwatson#include <sys/systm.h>
43155191Srwatson#include <sys/bus.h>
44155191Srwatson#include <sys/conf.h>
45155191Srwatson#include <sys/kernel.h>
46155191Srwatson
47155191Srwatson#include <dev/ofw/openfirm.h>
48155191Srwatson
49155191Srwatson#include <machine/bus.h>
50155191Srwatson#include <machine/intr.h>
51155191Srwatson#include <machine/intr_machdep.h>
52155191Srwatson#include <machine/md_var.h>
53155191Srwatson#include <machine/nexusvar.h>
54155191Srwatson#include <machine/pio.h>
55155191Srwatson#include <machine/resource.h>
56155191Srwatson
57155191Srwatson#include <vm/vm.h>
58155191Srwatson#include <vm/pmap.h>
59155191Srwatson
60155191Srwatson#include <sys/rman.h>
61155191Srwatson
62155191Srwatson#include <machine/openpicvar.h>
63155191Srwatson#include <powerpc/powermac/maciovar.h>
64155191Srwatson
65155191Srwatson#include "pic_if.h"
66155191Srwatson
67155191Srwatsonstruct openpic_ofw_softc {
68155191Srwatson	struct openpic_softc osc;
69155191Srwatson	struct resource *sc_memr;	/* macio bus resource */
70155191Srwatson	device_t	sc_ndev;	/* nexus device */
71155191Srwatson};
72155191Srwatson
73155191Srwatsonstatic struct openpic_ofw_softc *ofwpicsoftc;
74155191Srwatson
75155191Srwatson/*
76155191Srwatson * MacIO interface
77155191Srwatson */
78155191Srwatsonstatic void	openpic_ofw_identify(driver_t *, device_t);
79155191Srwatsonstatic int	openpic_ofw_probe(device_t);
80155191Srwatsonstatic int	openpic_ofw_attach(device_t);
81155191Srwatsonstatic int	openpic_macio_probe(device_t);
82155191Srwatsonstatic int	openpic_macio_attach(device_t);
83155191Srwatson
84155191Srwatson/*
85155191Srwatson * Nexus attachment
86155191Srwatson */
87155191Srwatsonstatic device_method_t  openpic_ofw_methods[] = {
88155191Srwatson	/* Device interface */
89155191Srwatson	DEVMETHOD(device_identify,	openpic_ofw_identify),
90155191Srwatson	DEVMETHOD(device_probe,		openpic_ofw_probe),
91155191Srwatson	DEVMETHOD(device_attach,	openpic_ofw_attach),
92155191Srwatson
93155191Srwatson	/* PIC interface */
94155191Srwatson	DEVMETHOD(pic_allocate_intr,	openpic_allocate_intr),
95155191Srwatson	DEVMETHOD(pic_setup_intr,	openpic_setup_intr),
96155191Srwatson	DEVMETHOD(pic_teardown_intr,	openpic_teardown_intr),
97155191Srwatson	DEVMETHOD(pic_release_intr,	openpic_release_intr),
98155191Srwatson
99155191Srwatson	{ 0, 0 }
100155191Srwatson};
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 pic;
118	char type[40];
119
120	pic = OF_finddevice("mpic");
121	if (pic == -1)
122		return;
123
124	OF_getprop(pic, "device_type", type, sizeof(type));
125
126	if (strcmp(type, "open-pic") != 0)
127		return;
128
129	child = BUS_ADD_CHILD(parent, 0, "openpic", 0);
130	if (child != NULL)
131		nexus_set_device_type(child, "macio");
132}
133
134static int
135openpic_ofw_probe(device_t dev)
136{
137	char	*name;
138	char	*type;
139
140	name = nexus_get_name(dev);
141	type = nexus_get_device_type(dev);
142
143	if (strcmp(name, "openpic") != 0 ||
144	    strcmp(type, "macio") != 0)
145		return (ENXIO);
146
147	device_set_desc(dev, OPENPIC_DEVSTR);
148	return (0);
149}
150
151static int
152openpic_ofw_attach(device_t dev)
153{
154	KASSERT(ofwpicsoftc == NULL, ("ofw openpic: already probed"));
155	ofwpicsoftc = device_get_softc(dev);
156	ofwpicsoftc->sc_ndev = dev;
157
158	nexus_install_intcntlr(dev);
159	openpic_early_attach(dev);
160	return (0);
161}
162
163/*
164 * MacIO attachment
165 */
166static device_method_t  openpic_macio_methods[] = {
167	/* Device interface */
168	DEVMETHOD(device_probe,		openpic_macio_probe),
169	DEVMETHOD(device_attach,	openpic_macio_attach),
170
171	{ 0, 0 },
172};
173
174static driver_t openpic_macio_driver = {
175	"openpicmacio",
176	openpic_macio_methods,
177	0
178};
179
180static devclass_t openpic_macio_devclass;
181
182DRIVER_MODULE(openpicmacio, macio, openpic_macio_driver,
183    openpic_macio_devclass, 0, 0);
184
185static int
186openpic_macio_probe(device_t dev)
187{
188	char *type = macio_get_devtype(dev);
189
190	if (strcmp(type, "open-pic") != 0)
191                return (ENXIO);
192
193	/*
194	 * The description was already printed out in the nexus
195	 * probe, so don't do it again here
196	 */
197	device_set_desc(dev, "OpenPIC MacIO interrupt cell");
198	if (!bootverbose)
199		device_quiet(dev);
200	return (0);
201}
202
203static int
204openpic_macio_attach(device_t dev)
205{
206	struct openpic_ofw_softc *sc;
207	int rid;
208
209	sc = ofwpicsoftc;
210	KASSERT(sc != NULL, ("pic not nexus-probed\n"));
211
212	rid = 0;
213	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
214	     RF_ACTIVE);
215
216	if (sc->sc_memr == NULL) {
217		device_printf(dev, "Could not alloc mem resource!\n");
218		return (ENXIO);
219	}
220
221	sc->osc.sc_bt = rman_get_bustag(sc->sc_memr);
222	sc->osc.sc_bh = rman_get_bushandle(sc->sc_memr);
223	sc->osc.sc_altdev = dev;
224
225	return (openpic_attach(sc->sc_ndev));
226}
227
228
229