xref: /freebsd/sys/powerpc/powermac/grackle.c (revision 64a0982bee3db2236df43357e70ce8dddbc21d48)
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/rman.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_pci.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/ofw/ofwpci.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 
49 #include <machine/bus.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/md_var.h>
52 #include <machine/pio.h>
53 #include <machine/resource.h>
54 
55 #include <powerpc/powermac/gracklevar.h>
56 
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 
60 #include "pcib_if.h"
61 
62 /*
63  * Device interface.
64  */
65 static int		grackle_probe(device_t);
66 static int		grackle_attach(device_t);
67 
68 /*
69  * pcib interface.
70  */
71 static u_int32_t	grackle_read_config(device_t, u_int, u_int, u_int,
72 			    u_int, int);
73 static void		grackle_write_config(device_t, u_int, u_int, u_int,
74 			    u_int, u_int32_t, int);
75 
76 /*
77  * Local routines.
78  */
79 static int		grackle_enable_config(struct grackle_softc *, u_int,
80 			    u_int, u_int, u_int);
81 static void		grackle_disable_config(struct grackle_softc *);
82 static int		badaddr(void *, size_t);
83 
84 /*
85  * Driver methods.
86  */
87 static device_method_t	grackle_methods[] = {
88 	/* Device interface */
89 	DEVMETHOD(device_probe,		grackle_probe),
90 	DEVMETHOD(device_attach,	grackle_attach),
91 
92 	/* pcib interface */
93 	DEVMETHOD(pcib_read_config,	grackle_read_config),
94 	DEVMETHOD(pcib_write_config,	grackle_write_config),
95 
96 	DEVMETHOD_END
97 };
98 
99 static devclass_t	grackle_devclass;
100 DEFINE_CLASS_1(pcib, grackle_driver, grackle_methods,
101     sizeof(struct grackle_softc), ofw_pci_driver);
102 DRIVER_MODULE(grackle, ofwbus, grackle_driver, grackle_devclass, 0, 0);
103 
104 static int
105 grackle_probe(device_t dev)
106 {
107 	const char	*type, *compatible;
108 
109 	type = ofw_bus_get_type(dev);
110 	compatible = ofw_bus_get_compat(dev);
111 
112 	if (type == NULL || compatible == NULL)
113 		return (ENXIO);
114 
115 	if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
116 		return (ENXIO);
117 
118 	device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
119 	return (0);
120 }
121 
122 static int
123 grackle_attach(device_t dev)
124 {
125 	struct		grackle_softc *sc;
126 
127 	sc = device_get_softc(dev);
128 
129 	/*
130 	 * The Grackle PCI config addr/data registers are actually in
131 	 * PCI space, but since they are needed to actually probe the
132 	 * PCI bus, use the fact that they are also available directly
133 	 * on the processor bus and map them
134 	 */
135 	sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
136 	sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
137 
138 	return (ofw_pci_attach(dev));
139 }
140 
141 static u_int32_t
142 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
143     int width)
144 {
145 	struct		grackle_softc *sc;
146 	vm_offset_t	caoff;
147 	u_int32_t	retval = 0xffffffff;
148 
149 	sc = device_get_softc(dev);
150 	caoff = sc->sc_data + (reg & 0x03);
151 
152 	if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
153 
154 		/*
155 		 * Config probes to non-existent devices on the
156 		 * secondary bus generates machine checks. Be sure
157 		 * to catch these.
158 		 */
159 		if (bus > 0) {
160 		  if (badaddr((void *)sc->sc_data, 4)) {
161 			  return (retval);
162 		  }
163 		}
164 
165 		switch (width) {
166 		case 1:
167 			retval = (in8rb(caoff));
168 			break;
169 		case 2:
170 			retval = (in16rb(caoff));
171 			break;
172 		case 4:
173 			retval = (in32rb(caoff));
174 			break;
175 		}
176 	}
177 	grackle_disable_config(sc);
178 
179 	return (retval);
180 }
181 
182 static void
183 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
184     u_int reg, u_int32_t val, int width)
185 {
186 	struct		grackle_softc *sc;
187 	vm_offset_t	caoff;
188 
189 	sc = device_get_softc(dev);
190 	caoff = sc->sc_data + (reg & 0x03);
191 
192 	if (grackle_enable_config(sc, bus, slot, func, reg)) {
193 		switch (width) {
194 		case 1:
195 			out8rb(caoff, val);
196 			(void)in8rb(caoff);
197 			break;
198 		case 2:
199 			out16rb(caoff, val);
200 			(void)in16rb(caoff);
201 			break;
202 		case 4:
203 			out32rb(caoff, val);
204 			(void)in32rb(caoff);
205 			break;
206 		}
207 	}
208 	grackle_disable_config(sc);
209 }
210 
211 static int
212 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
213     u_int func, u_int reg)
214 {
215 	u_int32_t	cfgval;
216 
217 	/*
218 	 * Unlike UniNorth, the format of the config word is the same
219 	 * for local (0) and remote busses.
220 	 */
221 	cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
222 	    | GRACKLE_CFG_ENABLE;
223 
224 	out32rb(sc->sc_addr, cfgval);
225 	(void) in32rb(sc->sc_addr);
226 
227 	return (1);
228 }
229 
230 static void
231 grackle_disable_config(struct grackle_softc *sc)
232 {
233 	/*
234 	 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
235 	 * accesses from causing config cycles
236 	 */
237 	out32rb(sc->sc_addr, 0);
238 }
239 
240 static int
241 badaddr(void *addr, size_t size)
242 {
243 	struct thread	*td;
244 	jmp_buf		env, *oldfaultbuf;
245 	int		x;
246 
247 	/* Get rid of any stale machine checks that have been waiting.  */
248 	__asm __volatile ("sync; isync");
249 
250 	td = curthread;
251 
252 	oldfaultbuf = td->td_pcb->pcb_onfault;
253 	td->td_pcb->pcb_onfault = &env;
254 	if (setjmp(env)) {
255 		td->td_pcb->pcb_onfault = oldfaultbuf;
256 		__asm __volatile ("sync");
257 		return 1;
258 	}
259 
260 	__asm __volatile ("sync");
261 
262 	switch (size) {
263 	case 1:
264 		x = *(volatile int8_t *)addr;
265 		break;
266 	case 2:
267 		x = *(volatile int16_t *)addr;
268 		break;
269 	case 4:
270 		x = *(volatile int32_t *)addr;
271 		break;
272 	default:
273 		panic("badaddr: invalid size (%zd)", size);
274 	}
275 
276 	/* Make sure we took the machine check, if we caused one. */
277 	__asm __volatile ("sync; isync");
278 
279 	td->td_pcb->pcb_onfault = oldfaultbuf;
280 	__asm __volatile ("sync");	/* To be sure. */
281 
282 	return (0);
283 }
284 
285 /*
286  * Driver to swallow Grackle host bridges from the PCI bus side.
287  */
288 static int
289 grackle_hb_probe(device_t dev)
290 {
291 
292 	if (pci_get_devid(dev) == 0x00021057) {
293 		device_set_desc(dev, "Grackle Host to PCI bridge");
294 		device_quiet(dev);
295 		return (0);
296 	}
297 
298 	return (ENXIO);
299 }
300 
301 static int
302 grackle_hb_attach(device_t dev)
303 {
304 
305 	return (0);
306 }
307 
308 static device_method_t grackle_hb_methods[] = {
309 	/* Device interface */
310 	DEVMETHOD(device_probe,         grackle_hb_probe),
311 	DEVMETHOD(device_attach,        grackle_hb_attach),
312 
313 	{ 0, 0 }
314 };
315 
316 static driver_t grackle_hb_driver = {
317 	"grackle_hb",
318 	grackle_hb_methods,
319 	1,
320 };
321 static devclass_t grackle_hb_devclass;
322 
323 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);
324