xref: /freebsd/sys/powerpc/powermac/grackle.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 
45 #include <machine/bus.h>
46 #include <machine/md_var.h>
47 #include <machine/pio.h>
48 #include <machine/resource.h>
49 
50 #include <sys/rman.h>
51 
52 #include <powerpc/powermac/gracklevar.h>
53 
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 
57 #include "pcib_if.h"
58 
59 int      badaddr(void *, size_t);  /* XXX */
60 
61 /*
62  * Device interface.
63  */
64 static int		grackle_probe(device_t);
65 static int		grackle_attach(device_t);
66 
67 /*
68  * Bus interface.
69  */
70 static int		grackle_read_ivar(device_t, device_t, int,
71 			    uintptr_t *);
72 static struct		resource * grackle_alloc_resource(device_t bus,
73 			    device_t child, int type, int *rid, u_long start,
74 			    u_long end, u_long count, u_int flags);
75 static int		grackle_release_resource(device_t bus, device_t child,
76     			    int type, int rid, struct resource *res);
77 static int		grackle_activate_resource(device_t bus, device_t child,
78 			    int type, int rid, struct resource *res);
79 static int		grackle_deactivate_resource(device_t bus,
80     			    device_t child, int type, int rid,
81     			    struct resource *res);
82 
83 
84 /*
85  * pcib interface.
86  */
87 static int		grackle_maxslots(device_t);
88 static u_int32_t	grackle_read_config(device_t, u_int, u_int, u_int,
89 			    u_int, int);
90 static void		grackle_write_config(device_t, u_int, u_int, u_int,
91 			    u_int, u_int32_t, int);
92 static int		grackle_route_interrupt(device_t, device_t, int);
93 
94 /*
95  * ofw_bus interface
96  */
97 static phandle_t grackle_get_node(device_t bus, device_t dev);
98 
99 /*
100  * Local routines.
101  */
102 static int		grackle_enable_config(struct grackle_softc *, u_int,
103 			    u_int, u_int, u_int);
104 static void		grackle_disable_config(struct grackle_softc *);
105 
106 /*
107  * Driver methods.
108  */
109 static device_method_t	grackle_methods[] = {
110 	/* Device interface */
111 	DEVMETHOD(device_probe,		grackle_probe),
112 	DEVMETHOD(device_attach,	grackle_attach),
113 
114 	/* Bus interface */
115 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
116 	DEVMETHOD(bus_read_ivar,	grackle_read_ivar),
117 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
118 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
119 	DEVMETHOD(bus_alloc_resource,	grackle_alloc_resource),
120 	DEVMETHOD(bus_release_resource,	grackle_release_resource),
121 	DEVMETHOD(bus_activate_resource,	grackle_activate_resource),
122 	DEVMETHOD(bus_deactivate_resource,	grackle_deactivate_resource),
123 
124 	/* pcib interface */
125 	DEVMETHOD(pcib_maxslots,	grackle_maxslots),
126 	DEVMETHOD(pcib_read_config,	grackle_read_config),
127 	DEVMETHOD(pcib_write_config,	grackle_write_config),
128 	DEVMETHOD(pcib_route_interrupt,	grackle_route_interrupt),
129 
130 	/* ofw_bus interface */
131 	DEVMETHOD(ofw_bus_get_node,     grackle_get_node),
132 
133 	{ 0, 0 }
134 };
135 
136 static driver_t	grackle_driver = {
137 	"pcib",
138 	grackle_methods,
139 	sizeof(struct grackle_softc)
140 };
141 
142 static devclass_t	grackle_devclass;
143 
144 DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0);
145 
146 static int
147 grackle_probe(device_t dev)
148 {
149 	const char	*type, *compatible;
150 
151 	type = ofw_bus_get_type(dev);
152 	compatible = ofw_bus_get_compat(dev);
153 
154 	if (type == NULL || compatible == NULL)
155 		return (ENXIO);
156 
157 	if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
158 		return (ENXIO);
159 
160 	device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
161 	return (0);
162 }
163 
164 static int
165 grackle_attach(device_t dev)
166 {
167 	struct		grackle_softc *sc;
168 	phandle_t	node;
169 	u_int32_t	busrange[2];
170 	struct		grackle_range *rp, *io, *mem[2];
171 	int		nmem, i, error;
172 
173 	node = ofw_bus_get_node(dev);
174 	sc = device_get_softc(dev);
175 
176 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
177 		return (ENXIO);
178 
179 	sc->sc_dev = dev;
180 	sc->sc_node = node;
181 	sc->sc_bus = busrange[0];
182 
183 	/*
184 	 * The Grackle PCI config addr/data registers are actually in
185 	 * PCI space, but since they are needed to actually probe the
186 	 * PCI bus, use the fact that they are also available directly
187 	 * on the processor bus and map them
188 	 */
189 	sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
190 	sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
191 
192 	bzero(sc->sc_range, sizeof(sc->sc_range));
193 	sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
194 	    sizeof(sc->sc_range));
195 
196 	if (sc->sc_nrange == -1) {
197 		device_printf(dev, "could not get ranges\n");
198 		return (ENXIO);
199 	}
200 
201 	sc->sc_range[6].pci_hi = 0;
202 	io = NULL;
203 	nmem = 0;
204 
205 	for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
206 		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
207 		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
208 			break;
209 		case OFW_PCI_PHYS_HI_SPACE_IO:
210 			io = rp;
211 			break;
212 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
213 			mem[nmem] = rp;
214 			nmem++;
215 			break;
216 		case OFW_PCI_PHYS_HI_SPACE_MEM64:
217 			break;
218 		}
219 	}
220 
221 	if (io == NULL) {
222 		device_printf(dev, "can't find io range\n");
223 		return (ENXIO);
224 	}
225 	sc->sc_io_rman.rm_type = RMAN_ARRAY;
226 	sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports";
227 	sc->sc_iostart = io->pci_iospace;
228 	if (rman_init(&sc->sc_io_rman) != 0 ||
229 	    rman_manage_region(&sc->sc_io_rman, io->pci_lo,
230 	    io->pci_lo + io->size_lo) != 0) {
231 		panic("grackle_attach: failed to set up I/O rman");
232 	}
233 
234 	if (nmem == 0) {
235 		device_printf(dev, "can't find mem ranges\n");
236 		return (ENXIO);
237 	}
238 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
239 	sc->sc_mem_rman.rm_descr = "Grackle PCI Memory";
240 	error = rman_init(&sc->sc_mem_rman);
241 	if (error) {
242 		device_printf(dev, "rman_init() failed. error = %d\n", error);
243 		return (error);
244 	}
245 	for (i = 0; i < nmem; i++) {
246 		error = rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
247 		    mem[i]->pci_lo + mem[i]->size_lo);
248 		if (error) {
249 			device_printf(dev,
250 			    "rman_manage_region() failed. error = %d\n", error);
251 			return (error);
252 		}
253 	}
254 
255 	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
256 
257 	device_add_child(dev, "pci", device_get_unit(dev));
258 	return (bus_generic_attach(dev));
259 }
260 
261 static int
262 grackle_maxslots(device_t dev)
263 {
264 
265 	return (PCI_SLOTMAX);
266 }
267 
268 static u_int32_t
269 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
270     int width)
271 {
272 	struct		grackle_softc *sc;
273 	vm_offset_t	caoff;
274 	u_int32_t	retval = 0xffffffff;
275 
276 	sc = device_get_softc(dev);
277 	caoff = sc->sc_data + (reg & 0x03);
278 
279 	if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
280 
281 		/*
282 		 * Config probes to non-existent devices on the
283 		 * secondary bus generates machine checks. Be sure
284 		 * to catch these.
285 		 */
286 		if (bus > 0) {
287 		  if (badaddr((void *)sc->sc_data, 4)) {
288 			  return (retval);
289 		  }
290 		}
291 
292 		switch (width) {
293 		case 1:
294 			retval = (in8rb(caoff));
295 			break;
296 		case 2:
297 			retval = (in16rb(caoff));
298 			break;
299 		case 4:
300 			retval = (in32rb(caoff));
301 			break;
302 		}
303 	}
304 	grackle_disable_config(sc);
305 
306 	return (retval);
307 }
308 
309 static void
310 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
311     u_int reg, u_int32_t val, int width)
312 {
313 	struct		grackle_softc *sc;
314 	vm_offset_t	caoff;
315 
316 	sc = device_get_softc(dev);
317 	caoff = sc->sc_data + (reg & 0x03);
318 
319 	if (grackle_enable_config(sc, bus, slot, func, reg)) {
320 		switch (width) {
321 		case 1:
322 			out8rb(caoff, val);
323 			(void)in8rb(caoff);
324 			break;
325 		case 2:
326 			out16rb(caoff, val);
327 			(void)in16rb(caoff);
328 			break;
329 		case 4:
330 			out32rb(caoff, val);
331 			(void)in32rb(caoff);
332 			break;
333 		}
334 	}
335 	grackle_disable_config(sc);
336 }
337 
338 static int
339 grackle_route_interrupt(device_t bus, device_t dev, int pin)
340 {
341 	struct grackle_softc *sc;
342 	struct ofw_pci_register reg;
343 	uint32_t pintr, mintr;
344 	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
345 
346 	sc = device_get_softc(bus);
347 	pintr = pin;
348 	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, &reg,
349 	    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr), maskbuf))
350 		return (mintr);
351 
352 	/* Maybe it's a real interrupt, not an intpin */
353 	if (pin > 4)
354 		return (pin);
355 
356 	device_printf(bus, "could not route pin %d for device %d.%d\n",
357 	    pin, pci_get_slot(dev), pci_get_function(dev));
358 	return (PCI_INVALID_IRQ);
359 }
360 
361 static int
362 grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
363 {
364 	struct	grackle_softc *sc;
365 
366 	sc = device_get_softc(dev);
367 
368 	switch (which) {
369 	case PCIB_IVAR_DOMAIN:
370 		*result = 0;
371 		return (0);
372 	case PCIB_IVAR_BUS:
373 		*result = sc->sc_bus;
374 		return (0);
375 	}
376 
377 	return (ENOENT);
378 }
379 
380 static struct resource *
381 grackle_alloc_resource(device_t bus, device_t child, int type, int *rid,
382     u_long start, u_long end, u_long count, u_int flags)
383 {
384 	struct			grackle_softc *sc;
385 	struct			resource *rv;
386 	struct			rman *rm;
387 	int			needactivate;
388 
389 	needactivate = flags & RF_ACTIVE;
390 	flags &= ~RF_ACTIVE;
391 
392 	sc = device_get_softc(bus);
393 
394 	switch (type) {
395 	case SYS_RES_MEMORY:
396 		rm = &sc->sc_mem_rman;
397 		break;
398 
399 	case SYS_RES_IOPORT:
400 		rm = &sc->sc_io_rman;
401 		break;
402 
403 	case SYS_RES_IRQ:
404 		return (bus_alloc_resource(bus, type, rid, start, end, count,
405 		    flags));
406 
407 	default:
408 		device_printf(bus, "unknown resource request from %s\n",
409 		    device_get_nameunit(child));
410 		return (NULL);
411 	}
412 
413 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
414 	if (rv == NULL) {
415 		device_printf(bus, "failed to reserve resource for %s\n",
416 		    device_get_nameunit(child));
417 		return (NULL);
418 	}
419 
420 	rman_set_rid(rv, *rid);
421 
422 	if (needactivate) {
423 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
424 			device_printf(bus,
425 			    "failed to activate resource for %s\n",
426 			    device_get_nameunit(child));
427 			rman_release_resource(rv);
428 			return (NULL);
429 		}
430 	}
431 
432 	return (rv);
433 }
434 
435 static int
436 grackle_release_resource(device_t bus, device_t child, int type, int rid,
437     struct resource *res)
438 {
439 	if (rman_get_flags(res) & RF_ACTIVE) {
440 		int error = bus_deactivate_resource(child, type, rid, res);
441 		if (error)
442 			return error;
443 	}
444 
445 	return (rman_release_resource(res));
446 }
447 
448 static int
449 grackle_activate_resource(device_t bus, device_t child, int type, int rid,
450     struct resource *res)
451 {
452 	struct grackle_softc *sc;
453 	void	*p;
454 
455 	sc = device_get_softc(bus);
456 
457 	if (type == SYS_RES_IRQ) {
458 		return (bus_activate_resource(bus, type, rid, res));
459 	}
460 	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
461 		vm_offset_t start;
462 
463 		start = (vm_offset_t)rman_get_start(res);
464 		/*
465 		 * For i/o-ports, convert the start address to the
466 		 * MPC106 PCI i/o window
467 		 */
468 		if (type == SYS_RES_IOPORT)
469 			start += sc->sc_iostart;
470 
471 		if (bootverbose)
472 			printf("grackle mapdev: start %x, len %ld\n", start,
473 			    rman_get_size(res));
474 
475 		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
476 		if (p == NULL)
477 			return (ENOMEM);
478 
479 		rman_set_virtual(res, p);
480 		rman_set_bustag(res, &bs_le_tag);
481 		rman_set_bushandle(res, (u_long)p);
482 	}
483 
484 	return (rman_activate_resource(res));
485 }
486 
487 static int
488 grackle_deactivate_resource(device_t bus, device_t child, int type, int rid,
489     struct resource *res)
490 {
491 	/*
492 	 * If this is a memory resource, unmap it.
493 	 */
494 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
495 		u_int32_t psize;
496 
497 		psize = rman_get_size(res);
498 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
499 	}
500 
501 	return (rman_deactivate_resource(res));
502 }
503 
504 
505 static int
506 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
507     u_int func, u_int reg)
508 {
509 	u_int32_t	cfgval;
510 
511 	/*
512 	 * Unlike UniNorth, the format of the config word is the same
513 	 * for local (0) and remote busses.
514 	 */
515 	cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
516 	    | GRACKLE_CFG_ENABLE;
517 
518 	out32rb(sc->sc_addr, cfgval);
519 	(void) in32rb(sc->sc_addr);
520 
521 	return (1);
522 }
523 
524 static void
525 grackle_disable_config(struct grackle_softc *sc)
526 {
527 	/*
528 	 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
529 	 * accesses from causing config cycles
530 	 */
531 	out32rb(sc->sc_addr, 0);
532 }
533 
534 static phandle_t
535 grackle_get_node(device_t bus, device_t dev)
536 {
537 	struct grackle_softc *sc;
538 
539 	sc = device_get_softc(bus);
540 	/* We only have one child, the PCI bus, which needs our own node. */
541 
542 	return sc->sc_node;
543 }
544 
545 /*
546  * Driver to swallow Grackle host bridges from the PCI bus side.
547  */
548 static int
549 grackle_hb_probe(device_t dev)
550 {
551 
552 	if (pci_get_devid(dev) == 0x00021057) {
553 		device_set_desc(dev, "Grackle Host to PCI bridge");
554 		device_quiet(dev);
555 		return (0);
556 	}
557 
558 	return (ENXIO);
559 }
560 
561 static int
562 grackle_hb_attach(device_t dev)
563 {
564 
565 	return (0);
566 }
567 
568 static device_method_t grackle_hb_methods[] = {
569 	/* Device interface */
570 	DEVMETHOD(device_probe,         grackle_hb_probe),
571 	DEVMETHOD(device_attach,        grackle_hb_attach),
572 
573 	{ 0, 0 }
574 };
575 
576 static driver_t grackle_hb_driver = {
577 	"grackle_hb",
578 	grackle_hb_methods,
579 	1,
580 };
581 static devclass_t grackle_hb_devclass;
582 
583 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);
584