xref: /freebsd/sys/powerpc/powermac/grackle.c (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2003 by Peter Grehan. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/rman.h>
41 
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_pci.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofwpci.h>
47 
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 
51 #include <machine/bus.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/md_var.h>
54 #include <machine/pio.h>
55 #include <machine/resource.h>
56 
57 #include <powerpc/powermac/gracklevar.h>
58 
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 
62 #include "pcib_if.h"
63 
64 /*
65  * Device interface.
66  */
67 static int		grackle_probe(device_t);
68 static int		grackle_attach(device_t);
69 
70 /*
71  * pcib interface.
72  */
73 static u_int32_t	grackle_read_config(device_t, u_int, u_int, u_int,
74 			    u_int, int);
75 static void		grackle_write_config(device_t, u_int, u_int, u_int,
76 			    u_int, u_int32_t, int);
77 
78 /*
79  * Local routines.
80  */
81 static int		grackle_enable_config(struct grackle_softc *, u_int,
82 			    u_int, u_int, u_int);
83 static void		grackle_disable_config(struct grackle_softc *);
84 static int		badaddr(void *, size_t);
85 
86 /*
87  * Driver methods.
88  */
89 static device_method_t	grackle_methods[] = {
90 	/* Device interface */
91 	DEVMETHOD(device_probe,		grackle_probe),
92 	DEVMETHOD(device_attach,	grackle_attach),
93 
94 	/* pcib interface */
95 	DEVMETHOD(pcib_read_config,	grackle_read_config),
96 	DEVMETHOD(pcib_write_config,	grackle_write_config),
97 
98 	DEVMETHOD_END
99 };
100 
101 static devclass_t	grackle_devclass;
102 DEFINE_CLASS_1(pcib, grackle_driver, grackle_methods,
103     sizeof(struct grackle_softc), ofw_pcib_driver);
104 DRIVER_MODULE(grackle, ofwbus, grackle_driver, grackle_devclass, 0, 0);
105 
106 static int
107 grackle_probe(device_t dev)
108 {
109 	const char	*type, *compatible;
110 
111 	type = ofw_bus_get_type(dev);
112 	compatible = ofw_bus_get_compat(dev);
113 
114 	if (type == NULL || compatible == NULL)
115 		return (ENXIO);
116 
117 	if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
118 		return (ENXIO);
119 
120 	device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
121 	return (0);
122 }
123 
124 static int
125 grackle_attach(device_t dev)
126 {
127 	struct		grackle_softc *sc;
128 
129 	sc = device_get_softc(dev);
130 
131 	/*
132 	 * The Grackle PCI config addr/data registers are actually in
133 	 * PCI space, but since they are needed to actually probe the
134 	 * PCI bus, use the fact that they are also available directly
135 	 * on the processor bus and map them
136 	 */
137 	sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
138 	sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
139 
140 	return (ofw_pcib_attach(dev));
141 }
142 
143 static u_int32_t
144 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
145     int width)
146 {
147 	struct		grackle_softc *sc;
148 	vm_offset_t	caoff;
149 	u_int32_t	retval = 0xffffffff;
150 
151 	sc = device_get_softc(dev);
152 	caoff = sc->sc_data + (reg & 0x03);
153 
154 	if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
155 		/*
156 		 * Config probes to non-existent devices on the
157 		 * secondary bus generates machine checks. Be sure
158 		 * to catch these.
159 		 */
160 		if (bus > 0) {
161 		  if (badaddr((void *)sc->sc_data, 4)) {
162 			  return (retval);
163 		  }
164 		}
165 
166 		switch (width) {
167 		case 1:
168 			retval = (in8rb(caoff));
169 			break;
170 		case 2:
171 			retval = (in16rb(caoff));
172 			break;
173 		case 4:
174 			retval = (in32rb(caoff));
175 			break;
176 		}
177 	}
178 	grackle_disable_config(sc);
179 
180 	return (retval);
181 }
182 
183 static void
184 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
185     u_int reg, u_int32_t val, int width)
186 {
187 	struct		grackle_softc *sc;
188 	vm_offset_t	caoff;
189 
190 	sc = device_get_softc(dev);
191 	caoff = sc->sc_data + (reg & 0x03);
192 
193 	if (grackle_enable_config(sc, bus, slot, func, reg)) {
194 		switch (width) {
195 		case 1:
196 			out8rb(caoff, val);
197 			(void)in8rb(caoff);
198 			break;
199 		case 2:
200 			out16rb(caoff, val);
201 			(void)in16rb(caoff);
202 			break;
203 		case 4:
204 			out32rb(caoff, val);
205 			(void)in32rb(caoff);
206 			break;
207 		}
208 	}
209 	grackle_disable_config(sc);
210 }
211 
212 static int
213 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
214     u_int func, u_int reg)
215 {
216 	u_int32_t	cfgval;
217 
218 	/*
219 	 * Unlike UniNorth, the format of the config word is the same
220 	 * for local (0) and remote busses.
221 	 */
222 	cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
223 	    | GRACKLE_CFG_ENABLE;
224 
225 	out32rb(sc->sc_addr, cfgval);
226 	(void) in32rb(sc->sc_addr);
227 
228 	return (1);
229 }
230 
231 static void
232 grackle_disable_config(struct grackle_softc *sc)
233 {
234 	/*
235 	 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
236 	 * accesses from causing config cycles
237 	 */
238 	out32rb(sc->sc_addr, 0);
239 }
240 
241 static int
242 badaddr(void *addr, size_t size)
243 {
244 	struct thread	*td;
245 	jmp_buf		env, *oldfaultbuf;
246 	int		x;
247 
248 	/* Get rid of any stale machine checks that have been waiting.  */
249 	__asm __volatile ("sync; isync");
250 
251 	td = curthread;
252 
253 	oldfaultbuf = td->td_pcb->pcb_onfault;
254 	td->td_pcb->pcb_onfault = &env;
255 	if (setjmp(env)) {
256 		td->td_pcb->pcb_onfault = oldfaultbuf;
257 		__asm __volatile ("sync");
258 		return 1;
259 	}
260 
261 	__asm __volatile ("sync");
262 
263 	switch (size) {
264 	case 1:
265 		x = *(volatile int8_t *)addr;
266 		break;
267 	case 2:
268 		x = *(volatile int16_t *)addr;
269 		break;
270 	case 4:
271 		x = *(volatile int32_t *)addr;
272 		break;
273 	default:
274 		panic("badaddr: invalid size (%zd)", size);
275 	}
276 
277 	/* Make sure we took the machine check, if we caused one. */
278 	__asm __volatile ("sync; isync");
279 
280 	td->td_pcb->pcb_onfault = oldfaultbuf;
281 	__asm __volatile ("sync");	/* To be sure. */
282 
283 	return (0);
284 }
285 
286 /*
287  * Driver to swallow Grackle host bridges from the PCI bus side.
288  */
289 static int
290 grackle_hb_probe(device_t dev)
291 {
292 
293 	if (pci_get_devid(dev) == 0x00021057) {
294 		device_set_desc(dev, "Grackle Host to PCI bridge");
295 		device_quiet(dev);
296 		return (0);
297 	}
298 
299 	return (ENXIO);
300 }
301 
302 static int
303 grackle_hb_attach(device_t dev)
304 {
305 
306 	return (0);
307 }
308 
309 static device_method_t grackle_hb_methods[] = {
310 	/* Device interface */
311 	DEVMETHOD(device_probe,         grackle_hb_probe),
312 	DEVMETHOD(device_attach,        grackle_hb_attach),
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