xref: /freebsd/sys/powerpc/psim/openpic_iobus.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 psim iobus attachment for the OpenPIC interrupt controller.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #define __RMAN_RESOURCE_VISIBLE
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/kernel.h>
43 
44 #include <dev/ofw/openfirm.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/md_var.h>
50 #include <machine/nexusvar.h>
51 #include <machine/pio.h>
52 #include <machine/resource.h>
53 
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 
57 #include <sys/rman.h>
58 
59 #include <machine/openpicvar.h>
60 #include <powerpc/psim/iobusvar.h>
61 
62 #include "pic_if.h"
63 
64 struct openpic_iobus_softc {
65 	struct openpic_softc osc;
66 	struct resource *sc_memr;	/* iobus mem resource */
67 	device_t	sc_ndev;	/* nexus device */
68 };
69 
70 static struct openpic_iobus_softc *ppicsoftc;
71 
72 /*
73  * MacIO interface
74  */
75 static void	openpic_psim_identify(driver_t *, device_t);
76 static int	openpic_psim_probe(device_t);
77 static int	openpic_psim_attach(device_t);
78 static int	openpic_iobus_probe(device_t);
79 static int	openpic_iobus_attach(device_t);
80 
81 /*
82  * Nexus attachment
83  */
84 static device_method_t  openpic_psim_methods[] = {
85 	/* Device interface */
86 	DEVMETHOD(device_identify,	openpic_psim_identify),
87 	DEVMETHOD(device_probe,		openpic_psim_probe),
88 	DEVMETHOD(device_attach,	openpic_psim_attach),
89 
90 	/* PIC interface */
91 	DEVMETHOD(pic_allocate_intr,	openpic_allocate_intr),
92 	DEVMETHOD(pic_setup_intr,	openpic_setup_intr),
93 	DEVMETHOD(pic_teardown_intr,	openpic_teardown_intr),
94 	DEVMETHOD(pic_release_intr,	openpic_release_intr),
95 
96 	{ 0, 0 }
97 };
98 
99 static driver_t openpic_psim_driver = {
100 	"openpic",
101 	openpic_psim_methods,
102 	sizeof(struct openpic_iobus_softc)
103 };
104 
105 static devclass_t openpic_psim_devclass;
106 
107 DRIVER_MODULE(openpic_psim, nexus, openpic_psim_driver, openpic_psim_devclass,
108     0, 0);
109 
110 static void
111 openpic_psim_identify(driver_t *driver, device_t parent)
112 {
113 	device_t child;
114 	phandle_t pic;
115 
116 	pic = OF_finddevice("/iobus/opic");
117 	if (pic == -1)
118 		return;
119 
120 	child = BUS_ADD_CHILD(parent, 0, "openpic", 0);
121 	if (child != NULL)
122 		nexus_set_device_type(child, "psim");
123 }
124 
125 static int
126 openpic_psim_probe(device_t dev)
127 {
128 	char    *name;
129 	char	*type;
130 
131 	name = nexus_get_name(dev);
132 	type = nexus_get_device_type(dev);
133 
134 	if (strcmp(name, "openpic") != 0 ||
135 	    strcmp(type, "psim") != 0)
136 		return (ENXIO);
137 
138 	device_set_desc(dev, OPENPIC_DEVSTR);
139 	return (0);
140 }
141 
142 static int
143 openpic_psim_attach(device_t dev)
144 {
145 	KASSERT(ppicsoftc == NULL, ("iobus openpic: already probed"));
146 	ppicsoftc = device_get_softc(dev);
147 	ppicsoftc->sc_ndev = dev;
148 
149 	nexus_install_intcntlr(dev);
150 	openpic_early_attach(dev);
151 	return (0);
152 }
153 
154 /*
155  * PSIM IOBus attachment
156  */
157 static device_method_t  openpic_iobus_methods[] = {
158 	/* Device interface */
159 	DEVMETHOD(device_probe,		openpic_iobus_probe),
160 	DEVMETHOD(device_attach,	openpic_iobus_attach),
161 
162 	{ 0, 0 },
163 };
164 
165 static driver_t openpic_iobus_driver = {
166 	"openpiciobus",
167 	openpic_iobus_methods,
168 	0
169 };
170 
171 static devclass_t openpic_iobus_devclass;
172 
173 DRIVER_MODULE(openpiciobus, iobus, openpic_iobus_driver,
174     openpic_iobus_devclass, 0, 0);
175 
176 static int
177 openpic_iobus_probe(device_t dev)
178 {
179 	char *name;
180 
181 	name = iobus_get_name(dev);
182 	if (strcmp(name, "interrupt-controller") != 0)
183                 return (ENXIO);
184 
185 	/*
186 	 * The description was already printed out in the nexus
187 	 * probe, so don't do it again here
188 	 */
189 	device_set_desc(dev, "OpenPIC IOBus interrupt cell");
190 	if (!bootverbose)
191 		device_quiet(dev);
192 	return (0);
193 }
194 
195 static int
196 openpic_iobus_attach(device_t dev)
197 {
198 	struct openpic_iobus_softc *sc;
199 	int rid;
200 
201 	sc = ppicsoftc;
202 	KASSERT(sc != NULL, ("pic not nexus-probed\n"));
203 
204 	rid = 0;
205 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
206 	     RF_ACTIVE);
207 
208 	if (sc->sc_memr == NULL) {
209 		device_printf(dev, "Could not alloc mem resource!\n");
210 		return (ENXIO);
211 	}
212 
213 	sc->osc.sc_psim = 1;
214 	sc->osc.sc_bt = rman_get_bustag(sc->sc_memr);
215 	sc->osc.sc_bh = rman_get_bushandle(sc->sc_memr);
216 	sc->osc.sc_altdev = dev;
217 
218 	return (openpic_attach(sc->sc_ndev));
219 }
220 
221 
222