ofw_pcib.c revision 221393
1117119Stmm/*-
2117119Stmm * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3117119Stmm * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4117119Stmm * Copyright (c) 2000 BSDi
5117119Stmm * Copyright (c) 2001 - 2003 Thomas Moestl <tmm@FreeBSD.org>
6200987Smarius * Copyright (c) 2009 by Marius Strobl <marius@FreeBSD.org>
7117119Stmm * All rights reserved.
8117119Stmm *
9117119Stmm * Redistribution and use in source and binary forms, with or without
10117119Stmm * modification, are permitted provided that the following conditions
11117119Stmm * are met:
12117119Stmm * 1. Redistributions of source code must retain the above copyright
13117119Stmm *    notice, this list of conditions and the following disclaimer.
14117119Stmm * 2. Redistributions in binary form must reproduce the above copyright
15117119Stmm *    notice, this list of conditions and the following disclaimer in the
16117119Stmm *    documentation and/or other materials provided with the distribution.
17117119Stmm * 3. The name of the author may not be used to endorse or promote products
18117119Stmm *    derived from this software without specific prior written permission.
19117119Stmm *
20117119Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21117119Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22117119Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23117119Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24117119Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25117119Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26117119Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27117119Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28117119Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29117119Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30117119Stmm * SUCH DAMAGE.
31117119Stmm *
32117119Stmm *	from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
33117119Stmm */
34117119Stmm
35153059Smarius#include <sys/cdefs.h>
36153059Smarius__FBSDID("$FreeBSD: head/sys/sparc64/pci/ofw_pcib.c 221393 2011-05-03 17:37:24Z jhb $");
37153059Smarius
38117119Stmm#include "opt_ofw_pci.h"
39117119Stmm
40117119Stmm#include <sys/param.h>
41200987Smarius#include <sys/bus.h>
42117119Stmm#include <sys/kernel.h>
43200987Smarius#include <sys/libkern.h>
44117119Stmm#include <sys/module.h>
45221393Sjhb#include <sys/rman.h>
46117119Stmm
47133589Smarius#include <dev/ofw/ofw_bus.h>
48117119Stmm#include <dev/ofw/openfirm.h>
49117119Stmm
50117119Stmm#include <machine/bus.h>
51117119Stmm
52117119Stmm#include <dev/pci/pcireg.h>
53117119Stmm#include <dev/pci/pcivar.h>
54117119Stmm#include <dev/pci/pcib_private.h>
55117119Stmm
56117119Stmm#include "pcib_if.h"
57117119Stmm
58117119Stmm#include <sparc64/pci/ofw_pci.h>
59117119Stmm#include <sparc64/pci/ofw_pcib_subr.h>
60117119Stmm
61117119Stmmstatic device_probe_t ofw_pcib_probe;
62117119Stmmstatic device_attach_t ofw_pcib_attach;
63117119Stmm
64117119Stmmstatic device_method_t ofw_pcib_methods[] = {
65117119Stmm	/* Device interface */
66117119Stmm	DEVMETHOD(device_probe,		ofw_pcib_probe),
67117119Stmm	DEVMETHOD(device_attach,	ofw_pcib_attach),
68117119Stmm
69117119Stmm	/* Bus interface */
70117119Stmm
71117119Stmm	/* pcib interface */
72216962Smarius	DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
73117119Stmm
74133589Smarius	/* ofw_bus interface */
75133589Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_pcib_gen_get_node),
76133589Smarius
77190099Smarius	KOBJMETHOD_END
78117119Stmm};
79117119Stmm
80154079Sjhbstatic devclass_t pcib_devclass;
81117119Stmm
82216962SmariusDEFINE_CLASS_1(pcib, ofw_pcib_driver, ofw_pcib_methods,
83216962Smarius    sizeof(struct ofw_pcib_gen_softc), pcib_driver);
84200874SmariusEARLY_DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_driver, pcib_devclass, 0, 0,
85200874Smarius    BUS_PASS_BUS);
86200815SmariusMODULE_DEPEND(ofw_pcib, pci, 1, 1, 1);
87117119Stmm
88117119Stmmstatic int
89117119Stmmofw_pcib_probe(device_t dev)
90117119Stmm{
91200987Smarius	char desc[sizeof("OFW PCIe-PCIe bridge")];
92200987Smarius	const char *dtype, *pbdtype;
93170929Smarius
94200987Smarius#define	ISDTYPE(dtype, type)						\
95200987Smarius	(((dtype) != NULL) && strcmp((dtype), (type)) == 0)
96200987Smarius
97117119Stmm	if ((pci_get_class(dev) == PCIC_BRIDGE) &&
98117119Stmm	    (pci_get_subclass(dev) == PCIS_BRIDGE_PCI) &&
99133589Smarius	    ofw_bus_get_node(dev) != 0) {
100200987Smarius		dtype = ofw_bus_get_type(dev);
101200987Smarius		pbdtype = ofw_bus_get_type(device_get_parent(
102200987Smarius		    device_get_parent(dev)));
103200987Smarius		snprintf(desc, sizeof(desc), "OFW PCI%s-PCI%s bridge",
104200987Smarius		    ISDTYPE(pbdtype, OFW_TYPE_PCIE) ? "e" : "",
105200987Smarius		    ISDTYPE(dtype, OFW_TYPE_PCIE) ? "e" : "");
106200987Smarius		device_set_desc_copy(dev, desc);
107117119Stmm		return (0);
108117119Stmm	}
109200987Smarius
110200987Smarius#undef ISDTYPE
111200987Smarius
112117119Stmm	return (ENXIO);
113117119Stmm}
114117119Stmm
115117119Stmmstatic int
116117119Stmmofw_pcib_attach(device_t dev)
117117119Stmm{
118200987Smarius	struct ofw_pcib_gen_softc *sc;
119117119Stmm
120200987Smarius	sc = device_get_softc(dev);
121200987Smarius
122200987Smarius	/* Quirk handling */
123200987Smarius	switch (pci_get_devid(dev)) {
124200987Smarius	/*
125200987Smarius	 * The ALi M5249 found in Fire-based machines by definition must me
126200987Smarius	 * subtractive as they have a ISA bridge on their secondary side but
127200987Smarius	 * don't indicate this in the class code although the ISA I/O range
128200987Smarius	 * isn't included in their bridge decode.
129200987Smarius	 */
130200987Smarius	case 0x524910b9:
131200987Smarius		sc->ops_pcib_sc.flags |= PCIB_SUBTRACTIVE;
132200987Smarius		break;
133200987Smarius	}
134200987Smarius
135117119Stmm	ofw_pcib_gen_setup(dev);
136117119Stmm	pcib_attach_common(dev);
137163260Skmacy	device_add_child(dev, "pci", -1);
138117119Stmm	return (bus_generic_attach(dev));
139117119Stmm}
140