apb.c revision 206019
186231Stmm/*-
286231Stmm * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
386231Stmm * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
486231Stmm * Copyright (c) 2000 BSDi
5117119Stmm * Copyright (c) 2001, 2003 Thomas Moestl <tmm@FreeBSD.org>
686231Stmm * All rights reserved.
786231Stmm *
886231Stmm * Redistribution and use in source and binary forms, with or without
986231Stmm * modification, are permitted provided that the following conditions
1086231Stmm * are met:
1186231Stmm * 1. Redistributions of source code must retain the above copyright
1286231Stmm *    notice, this list of conditions and the following disclaimer.
1386231Stmm * 2. Redistributions in binary form must reproduce the above copyright
1486231Stmm *    notice, this list of conditions and the following disclaimer in the
1586231Stmm *    documentation and/or other materials provided with the distribution.
1686231Stmm * 3. The name of the author may not be used to endorse or promote products
1786231Stmm *    derived from this software without specific prior written permission.
1886231Stmm *
1986231Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2086231Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2186231Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2286231Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2386231Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2486231Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2586231Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2686231Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2786231Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2886231Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2986231Stmm * SUCH DAMAGE.
3086231Stmm *
3186231Stmm *	from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
3286231Stmm */
3386231Stmm
34153057Smarius#include <sys/cdefs.h>
35153057Smarius__FBSDID("$FreeBSD: head/sys/sparc64/pci/apb.c 206019 2010-03-31 22:27:33Z marius $");
36153057Smarius
3786231Stmm/*
3886231Stmm * Support for the Sun APB (Advanced PCI Bridge) PCI-PCI bridge.
3986231Stmm * This bridge does not fully comply to the PCI bridge specification, and is
4086231Stmm * therefore not supported by the generic driver.
41129051Smarius * We can use some of the pcib methods anyway.
4286231Stmm */
4386231Stmm
44117119Stmm#include "opt_ofw_pci.h"
45117119Stmm
4686231Stmm#include <sys/param.h>
4786231Stmm#include <sys/systm.h>
4886231Stmm#include <sys/kernel.h>
49130025Sphk#include <sys/module.h>
5086231Stmm#include <sys/bus.h>
5186231Stmm
52133589Smarius#include <dev/ofw/ofw_bus.h>
5393067Stmm#include <dev/ofw/openfirm.h>
5493067Stmm
55117119Stmm#include <machine/bus.h>
5686231Stmm#include <machine/resource.h>
5786231Stmm
58119291Simp#include <dev/pci/pcireg.h>
59119291Simp#include <dev/pci/pcivar.h>
60119291Simp#include <dev/pci/pcib_private.h>
6186231Stmm
6286231Stmm#include "pcib_if.h"
6386231Stmm
64117119Stmm#include <sparc64/pci/ofw_pci.h>
65117119Stmm#include <sparc64/pci/ofw_pcib_subr.h>
66117119Stmm
6786231Stmm/*
6886231Stmm * Bridge-specific data.
6986231Stmm */
7086231Stmmstruct apb_softc {
71117119Stmm	struct ofw_pcib_gen_softc	sc_bsc;
72153057Smarius	uint8_t		sc_iomap;
73153057Smarius	uint8_t		sc_memmap;
7486231Stmm};
7586231Stmm
76117119Stmmstatic device_probe_t apb_probe;
77117119Stmmstatic device_attach_t apb_attach;
78117119Stmmstatic bus_alloc_resource_t apb_alloc_resource;
7986231Stmm
8086231Stmmstatic device_method_t apb_methods[] = {
8186231Stmm	/* Device interface */
8286231Stmm	DEVMETHOD(device_probe,		apb_probe),
8386231Stmm	DEVMETHOD(device_attach,	apb_attach),
8486231Stmm	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
8586231Stmm	DEVMETHOD(device_suspend,	bus_generic_suspend),
8686231Stmm	DEVMETHOD(device_resume,	bus_generic_resume),
8786231Stmm
8886231Stmm	/* Bus interface */
8986231Stmm	DEVMETHOD(bus_print_child,	bus_generic_print_child),
90117119Stmm	DEVMETHOD(bus_read_ivar,	pcib_read_ivar),
91117119Stmm	DEVMETHOD(bus_write_ivar,	pcib_write_ivar),
9286231Stmm	DEVMETHOD(bus_alloc_resource,	apb_alloc_resource),
9386231Stmm	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
9486231Stmm	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
95190099Smarius	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
9686231Stmm	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
9786231Stmm	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
9886231Stmm
9986231Stmm	/* pcib interface */
100117119Stmm	DEVMETHOD(pcib_maxslots,	pcib_maxslots),
101117119Stmm	DEVMETHOD(pcib_read_config,	pcib_read_config),
102117119Stmm	DEVMETHOD(pcib_write_config,	pcib_write_config),
103117119Stmm	DEVMETHOD(pcib_route_interrupt,	ofw_pcib_gen_route_interrupt),
10486231Stmm
105133589Smarius	/* ofw_bus interface */
106133589Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_pcib_gen_get_node),
107133589Smarius
108190099Smarius	KOBJMETHOD_END
10986231Stmm};
11086231Stmm
111154079Sjhbstatic devclass_t pcib_devclass;
11286231Stmm
113154079SjhbDEFINE_CLASS_0(pcib, apb_driver, apb_methods, sizeof(struct apb_softc));
114200874SmariusEARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS);
115200815SmariusMODULE_DEPEND(apb, pci, 1, 1, 1);
11686231Stmm
11786231Stmm/* APB specific registers */
11886231Stmm#define	APBR_IOMAP	0xde
11986231Stmm#define	APBR_MEMMAP	0xdf
12086231Stmm
12186231Stmm/* Definitions for the mapping registers */
12286231Stmm#define	APB_IO_SCALE	0x200000
12386231Stmm#define	APB_MEM_SCALE	0x20000000
12486231Stmm
12586231Stmm/*
12686231Stmm * Generic device interface
12786231Stmm */
12886231Stmmstatic int
12986231Stmmapb_probe(device_t dev)
13086231Stmm{
13186231Stmm
13286231Stmm	if (pci_get_vendor(dev) == 0x108e &&	/* Sun */
13386231Stmm	    pci_get_device(dev) == 0x5000)  {	/* APB */
13486231Stmm		device_set_desc(dev, "APB PCI-PCI bridge");
13586231Stmm		return (0);
13686231Stmm	}
13786231Stmm	return (ENXIO);
13886231Stmm}
13986231Stmm
14086231Stmmstatic void
141153057Smariusapb_map_print(uint8_t map, u_long scale)
14286231Stmm{
14386231Stmm	int i, first;
14486231Stmm
14586231Stmm	for (first = 1, i = 0; i < 8; i++) {
14686231Stmm		if ((map & (1 << i)) != 0) {
14786231Stmm			printf("%s0x%lx-0x%lx", first ? "" : ", ",
14886231Stmm			    i * scale, (i + 1) * scale - 1);
14986231Stmm			first = 0;
15086231Stmm		}
15186231Stmm	}
15286231Stmm}
15386231Stmm
15486231Stmmstatic int
155153057Smariusapb_checkrange(uint8_t map, u_long scale, u_long start, u_long end)
15686231Stmm{
15786231Stmm	int i, ei;
15886231Stmm
15986231Stmm	i = start / scale;
16086231Stmm	ei = end / scale;
16186231Stmm	if (i > 7 || ei > 7)
16286231Stmm		return (0);
16386231Stmm	for (; i <= ei; i++)
16486231Stmm		if ((map & (1 << i)) == 0)
16586231Stmm			return (0);
16686231Stmm	return (1);
16786231Stmm}
16886231Stmm
16986231Stmmstatic int
17086231Stmmapb_attach(device_t dev)
17186231Stmm{
17288369Stmm	struct apb_softc *sc;
17386231Stmm
17486231Stmm	sc = device_get_softc(dev);
17586231Stmm
17686231Stmm	/*
17786231Stmm	 * Get current bridge configuration.
17886231Stmm	 */
179178279Smarius	sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev);
180178279Smarius	sc->sc_bsc.ops_pcib_sc.secbus =
181178279Smarius	    pci_read_config(dev, PCIR_SECBUS_1, 1);
182178279Smarius	sc->sc_bsc.ops_pcib_sc.subbus =
183178279Smarius	    pci_read_config(dev, PCIR_SUBBUS_1, 1);
184117119Stmm	sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1);
185117119Stmm	sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1);
186117119Stmm	ofw_pcib_gen_setup(dev);
18786231Stmm
18886231Stmm	if (bootverbose) {
189172394Smarius		device_printf(dev, "  domain            %d\n",
190172394Smarius		    sc->sc_bsc.ops_pcib_sc.domain);
191117119Stmm		device_printf(dev, "  secondary bus     %d\n",
192117119Stmm		    sc->sc_bsc.ops_pcib_sc.secbus);
193117119Stmm		device_printf(dev, "  subordinate bus   %d\n",
194117119Stmm		    sc->sc_bsc.ops_pcib_sc.subbus);
19586231Stmm		device_printf(dev, "  I/O decode        ");
196117119Stmm		apb_map_print(sc->sc_iomap, APB_IO_SCALE);
19786231Stmm		printf("\n");
19886231Stmm		device_printf(dev, "  memory decode     ");
199117119Stmm		apb_map_print(sc->sc_memmap, APB_MEM_SCALE);
20086231Stmm		printf("\n");
20186231Stmm	}
20286231Stmm
203178279Smarius	device_add_child(dev, "pci", -1);
204117119Stmm	return (bus_generic_attach(dev));
20586231Stmm}
20686231Stmm
20786231Stmm/*
20886231Stmm * We have to trap resource allocation requests and ensure that the bridge
20986231Stmm * is set up to, or capable of handling them.
21086231Stmm */
21186231Stmmstatic struct resource *
212178279Smariusapb_alloc_resource(device_t dev, device_t child, int type, int *rid,
21386231Stmm    u_long start, u_long end, u_long count, u_int flags)
21486231Stmm{
21588369Stmm	struct apb_softc *sc;
21686231Stmm
21786231Stmm	sc = device_get_softc(dev);
218145610Smarcel
21986231Stmm	/*
22086231Stmm	 * If this is a "default" allocation against this rid, we can't work
221129051Smarius	 * out where it's coming from (we should actually never see these) so
222129051Smarius	 * we just have to punt.
22386231Stmm	 */
224145610Smarcel	if (start == 0 && end == ~0) {
22586231Stmm		device_printf(dev, "can't decode default resource id %d for "
226206019Smarius		    "%s, bypassing\n", *rid, device_get_nameunit(child));
227145610Smarcel		goto passup;
228145610Smarcel	}
22986231Stmm
230145610Smarcel	/*
231145610Smarcel	 * Fail the allocation for this range if it's not supported.
232145610Smarcel	 * XXX we should probably just fix up the bridge decode and
233145610Smarcel	 * soldier on.
234145610Smarcel	 */
235145610Smarcel	switch (type) {
236145610Smarcel	case SYS_RES_IOPORT:
237145610Smarcel		if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) {
238206019Smarius			device_printf(dev, "device %s requested unsupported "
239206019Smarius			    "I/O range 0x%lx-0x%lx\n",
240206019Smarius			    device_get_nameunit(child), start, end);
241145610Smarcel			return (NULL);
242145610Smarcel		}
243145610Smarcel		if (bootverbose)
244145610Smarcel			device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
245206019Smarius			    "%s requested decoded I/O range 0x%lx-0x%lx\n",
246206019Smarius			    device_get_nameunit(child), start, end);
247145610Smarcel		break;
24886231Stmm
249145610Smarcel	case SYS_RES_MEMORY:
250145610Smarcel		if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end)) {
251206019Smarius			device_printf(dev, "device %s requested unsupported "
252145610Smarcel			    "memory range 0x%lx-0x%lx\n",
253206019Smarius			    device_get_nameunit(child), start, end);
254145610Smarcel			return (NULL);
25586231Stmm		}
256145610Smarcel		if (bootverbose)
257145610Smarcel			device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
258206019Smarius			    "%s requested decoded memory range 0x%lx-0x%lx\n",
259206019Smarius			    device_get_nameunit(child), start, end);
260145610Smarcel		break;
261145610Smarcel
262145610Smarcel	default:
263145610Smarcel		break;
26486231Stmm	}
26586231Stmm
266145610Smarcel passup:
26786231Stmm	/*
26886231Stmm	 * Bridge is OK decoding this resource, so pass it up.
26986231Stmm	 */
27086231Stmm	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
27186231Stmm	    count, flags));
27286231Stmm}
273