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