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