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