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