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