openpic_ofw.c revision 133521
186231Stmm/*
286231Stmm * Copyright 2003 by Peter Grehan. All rights reserved.
386231Stmm *
486231Stmm * Redistribution and use in source and binary forms, with or without
586231Stmm * modification, are permitted provided that the following conditions
686231Stmm * are met:
786231Stmm * 1. Redistributions of source code must retain the above copyright
886231Stmm *    notice, this list of conditions and the following disclaimer.
986231Stmm * 2. Redistributions in binary form must reproduce the above copyright
1086231Stmm *    notice, this list of conditions and the following disclaimer in the
1186231Stmm *    documentation and/or other materials provided with the distribution.
1286231Stmm * 3. The name of the author may not be used to endorse or promote products
1386231Stmm *    derived from this software without specific prior written permission.
1486231Stmm *
1586231Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1686231Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1786231Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1886231Stmm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1986231Stmm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2086231Stmm * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2186231Stmm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2286231Stmm * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2386231Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2486231Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2586231Stmm * SUCH DAMAGE.
2686231Stmm *
2786231Stmm */
2886231Stmm
2986231Stmm/*
3086231Stmm * The macio attachment for the OpenPIC interrupt controller.
3186231Stmm * A nexus driver is defined so the number of interrupts can be
3286231Stmm * determined early in the boot sequence before the hardware
3386231Stmm * is accessed - the interrupt i/f is installed at this time,
3486231Stmm * and when h/w is finally accessed, interrupt sources allocated
3586231Stmm * prior to this are activated
3686231Stmm */
3786231Stmm
3886231Stmm#include <sys/cdefs.h>
3986231Stmm__FBSDID("$FreeBSD: head/sys/powerpc/powermac/openpic_macio.c 133521 2004-08-11 21:09:40Z marius $");
4086231Stmm
4186231Stmm#include <sys/param.h>
4286231Stmm#include <sys/systm.h>
4388823Stmm#include <sys/module.h>
4486231Stmm#include <sys/bus.h>
4586231Stmm#include <sys/conf.h>
46113238Sjake#include <sys/kernel.h>
4786231Stmm
4886231Stmm#include <dev/ofw/openfirm.h>
4986231Stmm
5086231Stmm#include <machine/bus.h>
5186231Stmm#include <machine/intr.h>
5286231Stmm#include <machine/intr_machdep.h>
5388823Stmm#include <machine/md_var.h>
5488823Stmm#include <machine/nexusvar.h>
5586231Stmm#include <machine/pio.h>
5690617Stmm#include <machine/resource.h>
5790617Stmm
5886231Stmm#include <vm/vm.h>
5986231Stmm#include <vm/pmap.h>
6086231Stmm
6186231Stmm#include <sys/rman.h>
6286231Stmm
6386231Stmm#include <machine/openpicvar.h>
64115417Stmm#include <powerpc/powermac/maciovar.h>
65115417Stmm
66115417Stmm#include "pic_if.h"
6786231Stmm
68108800Stmmstruct openpic_ofw_softc {
6986231Stmm	struct openpic_softc osc;
7088823Stmm	struct resource *sc_memr;	/* macio bus resource */
7188823Stmm	device_t	sc_ndev;	/* nexus device */
7286231Stmm};
7386231Stmm
7486231Stmmstatic struct openpic_ofw_softc *ofwpicsoftc;
7586231Stmm
7686231Stmm/*
7786231Stmm * MacIO interface
7886231Stmm */
7986231Stmmstatic void	openpic_ofw_identify(driver_t *, device_t);
8086231Stmmstatic int	openpic_ofw_probe(device_t);
8186231Stmmstatic int	openpic_ofw_attach(device_t);
8286231Stmmstatic int	openpic_macio_probe(device_t);
8386231Stmmstatic int	openpic_macio_attach(device_t);
8486231Stmm
8586231Stmm/*
8686231Stmm * Nexus attachment
8786231Stmm */
8886231Stmmstatic device_method_t  openpic_ofw_methods[] = {
8986231Stmm	/* Device interface */
9086231Stmm	DEVMETHOD(device_identify,	openpic_ofw_identify),
9186231Stmm	DEVMETHOD(device_probe,		openpic_ofw_probe),
9286231Stmm	DEVMETHOD(device_attach,	openpic_ofw_attach),
9386231Stmm
94105274Stmm	/* PIC interface */
95105274Stmm	DEVMETHOD(pic_allocate_intr,	openpic_allocate_intr),
9686231Stmm	DEVMETHOD(pic_setup_intr,	openpic_setup_intr),
9786231Stmm	DEVMETHOD(pic_teardown_intr,	openpic_teardown_intr),
9886231Stmm	DEVMETHOD(pic_release_intr,	openpic_release_intr),
99
100	{ 0, 0 }
101};
102
103static driver_t openpic_ofw_driver = {
104	"openpic",
105	openpic_ofw_methods,
106	sizeof(struct openpic_ofw_softc)
107};
108
109static devclass_t openpic_ofw_devclass;
110
111DRIVER_MODULE(openpic_ofw, nexus, openpic_ofw_driver, openpic_ofw_devclass,
112    0, 0);
113
114static void
115openpic_ofw_identify(driver_t *driver, device_t parent)
116{
117	device_t child;
118	phandle_t pic;
119	char type[40];
120
121	pic = OF_finddevice("mpic");
122	if (pic == -1)
123		return;
124
125	OF_getprop(pic, "device_type", type, sizeof(type));
126
127	if (strcmp(type, "open-pic") != 0)
128		return;
129
130	child = BUS_ADD_CHILD(parent, 0, "openpic", 0);
131	if (child != NULL)
132		nexus_set_device_type(child, "macio");
133}
134
135static int
136openpic_ofw_probe(device_t dev)
137{
138	char	*name;
139	char	*type;
140
141	name = nexus_get_name(dev);
142	type = nexus_get_device_type(dev);
143
144	if (strcmp(name, "openpic") != 0 ||
145	    strcmp(type, "macio") != 0)
146		return (ENXIO);
147
148	device_set_desc(dev, OPENPIC_DEVSTR);
149	return (0);
150}
151
152static int
153openpic_ofw_attach(device_t dev)
154{
155	KASSERT(ofwpicsoftc == NULL, ("ofw openpic: already probed"));
156	ofwpicsoftc = device_get_softc(dev);
157	ofwpicsoftc->sc_ndev = dev;
158
159	nexus_install_intcntlr(dev);
160	openpic_early_attach(dev);
161	return (0);
162}
163
164/*
165 * MacIO attachment
166 */
167static device_method_t  openpic_macio_methods[] = {
168	/* Device interface */
169	DEVMETHOD(device_probe,		openpic_macio_probe),
170	DEVMETHOD(device_attach,	openpic_macio_attach),
171
172	{ 0, 0 },
173};
174
175static driver_t openpic_macio_driver = {
176	"openpicmacio",
177	openpic_macio_methods,
178	0
179};
180
181static devclass_t openpic_macio_devclass;
182
183DRIVER_MODULE(openpicmacio, macio, openpic_macio_driver,
184    openpic_macio_devclass, 0, 0);
185
186static int
187openpic_macio_probe(device_t dev)
188{
189	char *type = macio_get_devtype(dev);
190
191	if (strcmp(type, "open-pic") != 0)
192                return (ENXIO);
193
194	/*
195	 * The description was already printed out in the nexus
196	 * probe, so don't do it again here
197	 */
198	device_set_desc(dev, "OpenPIC MacIO interrupt cell");
199	if (!bootverbose)
200		device_quiet(dev);
201	return (0);
202}
203
204static int
205openpic_macio_attach(device_t dev)
206{
207	struct openpic_ofw_softc *sc;
208	int rid;
209
210	sc = ofwpicsoftc;
211	KASSERT(sc != NULL, ("pic not nexus-probed\n"));
212
213	rid = 0;
214	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
215	     RF_ACTIVE);
216
217	if (sc->sc_memr == NULL) {
218		device_printf(dev, "Could not alloc mem resource!\n");
219		return (ENXIO);
220	}
221
222	sc->osc.sc_bt = rman_get_bustag(sc->sc_memr);
223	sc->osc.sc_bh = rman_get_bushandle(sc->sc_memr);
224	sc->osc.sc_altdev = dev;
225
226	return (openpic_attach(sc->sc_ndev));
227}
228
229
230