ofw_pcib_pci.c revision 186128
1109001Sbenno/*-
2109001Sbenno * Copyright (c) 2000 Michael Smith
3109001Sbenno * Copyright (c) 2000 BSDi
4109001Sbenno * All rights reserved.
5109001Sbenno *
6109001Sbenno * Redistribution and use in source and binary forms, with or without
7109001Sbenno * modification, are permitted provided that the following conditions
8109001Sbenno * are met:
9109001Sbenno * 1. Redistributions of source code must retain the above copyright
10109001Sbenno *    notice, this list of conditions and the following disclaimer.
11109001Sbenno * 2. Redistributions in binary form must reproduce the above copyright
12109001Sbenno *    notice, this list of conditions and the following disclaimer in the
13109001Sbenno *    documentation and/or other materials provided with the distribution.
14109001Sbenno *
15109001Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16109001Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17109001Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18109001Sbenno * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19109001Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20109001Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21109001Sbenno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22109001Sbenno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23109001Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24109001Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25109001Sbenno * SUCH DAMAGE.
26109001Sbenno *
27109001Sbenno * $FreeBSD: head/sys/powerpc/ofw/ofw_pcib_pci.c 186128 2008-12-15 15:31:10Z nwhitehorn $
28109001Sbenno */
29109001Sbenno
30109001Sbenno#include <sys/param.h>
31131102Sgrehan#include <sys/module.h>
32109001Sbenno#include <sys/bus.h>
33109001Sbenno#include <sys/malloc.h>
34109001Sbenno#include <sys/kernel.h>
35109001Sbenno
36109001Sbenno#include <dev/ofw/openfirm.h>
37109001Sbenno#include <dev/ofw/ofw_pci.h>
38183882Snwhitehorn#include <dev/ofw/ofw_bus.h>
39186128Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
40109001Sbenno
41119291Simp#include <dev/pci/pcivar.h>
42119291Simp#include <dev/pci/pcireg.h>
43119291Simp#include <dev/pci/pcib_private.h>
44109001Sbenno
45109001Sbenno#include "pcib_if.h"
46109001Sbenno
47109001Sbennostatic int	ofw_pcib_pci_probe(device_t bus);
48109001Sbennostatic int	ofw_pcib_pci_attach(device_t bus);
49183882Snwhitehornstatic phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev);
50186128Snwhitehornstatic int	ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev,
51186128Snwhitehorn		    int intpin);
52109001Sbenno
53109001Sbennostatic device_method_t ofw_pcib_pci_methods[] = {
54109001Sbenno	/* Device interface */
55109001Sbenno	DEVMETHOD(device_probe,		ofw_pcib_pci_probe),
56109001Sbenno	DEVMETHOD(device_attach,		ofw_pcib_pci_attach),
57109001Sbenno	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
58109001Sbenno	DEVMETHOD(device_suspend,		bus_generic_suspend),
59109001Sbenno	DEVMETHOD(device_resume,		bus_generic_resume),
60109001Sbenno
61109001Sbenno	/* Bus interface */
62109001Sbenno	DEVMETHOD(bus_print_child,		bus_generic_print_child),
63109001Sbenno	DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
64109001Sbenno	DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
65109001Sbenno	DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
66109001Sbenno	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
67109001Sbenno	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
68109001Sbenno	DEVMETHOD(bus_deactivate_resource, 	bus_generic_deactivate_resource),
69109001Sbenno	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
70109001Sbenno	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
71109001Sbenno
72109001Sbenno	/* pcib interface */
73109001Sbenno	DEVMETHOD(pcib_maxslots,		pcib_maxslots),
74109001Sbenno	DEVMETHOD(pcib_read_config,		pcib_read_config),
75109001Sbenno	DEVMETHOD(pcib_write_config,	pcib_write_config),
76186128Snwhitehorn	DEVMETHOD(pcib_route_interrupt,	ofw_pcib_pci_route_interrupt),
77109001Sbenno
78183882Snwhitehorn	/* ofw_bus interface */
79183882Snwhitehorn	DEVMETHOD(ofw_bus_get_node,     ofw_pcib_pci_get_node),
80183882Snwhitehorn
81109001Sbenno	{0, 0}
82109001Sbenno};
83109001Sbenno
84154079Sjhbstatic devclass_t pcib_devclass;
85109001Sbenno
86186128Snwhitehornstruct ofw_pcib_softc {
87186128Snwhitehorn        /*
88186128Snwhitehorn         * This is here so that we can use pci bridge methods, too - the
89186128Snwhitehorn         * generic routines only need the dev, secbus and subbus members
90186128Snwhitehorn         * filled.
91186128Snwhitehorn         */
92186128Snwhitehorn        struct pcib_softc       ops_pcib_sc;
93186128Snwhitehorn	phandle_t		ops_node;
94186128Snwhitehorn        struct ofw_bus_iinfo    ops_iinfo;
95186128Snwhitehorn};
96186128Snwhitehorn
97186128Snwhitehorn
98154079SjhbDEFINE_CLASS_0(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods,
99186128Snwhitehorn    sizeof(struct ofw_pcib_softc));
100109001SbennoDRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, pcib_devclass, 0, 0);
101109001Sbenno
102109001Sbennostatic int
103109001Sbennoofw_pcib_pci_probe(device_t dev)
104109001Sbenno{
105109001Sbenno
106109001Sbenno	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
107109001Sbenno	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) {
108109001Sbenno		return (ENXIO);
109109001Sbenno	}
110183882Snwhitehorn
111183882Snwhitehorn	if (ofw_bus_get_node(dev) == 0)
112109001Sbenno		return (ENXIO);
113109001Sbenno
114183882Snwhitehorn	device_set_desc(dev, "OFW PCI-PCI bridge");
115183882Snwhitehorn	return (0);
116109001Sbenno}
117109001Sbenno
118109001Sbennostatic int
119109001Sbennoofw_pcib_pci_attach(device_t dev)
120109001Sbenno{
121186128Snwhitehorn	struct ofw_pcib_softc *sc;
122186128Snwhitehorn
123186128Snwhitehorn	sc = device_get_softc(dev);
124186128Snwhitehorn	sc->ops_pcib_sc.dev = dev;
125186128Snwhitehorn	sc->ops_node = ofw_bus_get_node(dev);
126186128Snwhitehorn
127186128Snwhitehorn	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
128186128Snwhitehorn	    sizeof(cell_t));
129186128Snwhitehorn
130109001Sbenno	pcib_attach_common(dev);
131109001Sbenno
132109001Sbenno	device_add_child(dev, "pci", -1);
133109001Sbenno
134109001Sbenno	return (bus_generic_attach(dev));
135109001Sbenno}
136183882Snwhitehorn
137186128Snwhitehornstatic phandle_t
138183882Snwhitehornofw_pcib_pci_get_node(device_t bridge, device_t dev)
139183882Snwhitehorn{
140183882Snwhitehorn	/* We have only one child, the PCI bus, so pass it our node */
141183882Snwhitehorn
142183882Snwhitehorn	return (ofw_bus_get_node(bridge));
143183882Snwhitehorn}
144183882Snwhitehorn
145186128Snwhitehornstatic int
146186128Snwhitehornofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin)
147186128Snwhitehorn{
148186128Snwhitehorn	struct ofw_pcib_softc *sc;
149186128Snwhitehorn	struct ofw_bus_iinfo *ii;
150186128Snwhitehorn	struct ofw_pci_register reg;
151186128Snwhitehorn	cell_t pintr, mintr;
152186128Snwhitehorn	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
153186128Snwhitehorn
154186128Snwhitehorn	sc = device_get_softc(bridge);
155186128Snwhitehorn	ii = &sc->ops_iinfo;
156186128Snwhitehorn	if (ii->opi_imapsz > 0) {
157186128Snwhitehorn		pintr = intpin;
158186128Snwhitehorn		if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
159186128Snwhitehorn		    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
160186128Snwhitehorn		    maskbuf)) {
161186128Snwhitehorn			/*
162186128Snwhitehorn			 * If we've found a mapping, return it and don't map
163186128Snwhitehorn			 * it again on higher levels - that causes problems
164186128Snwhitehorn			 * in some cases, and never seems to be required.
165186128Snwhitehorn			 */
166186128Snwhitehorn			return (mintr);
167186128Snwhitehorn		}
168186128Snwhitehorn	} else if (intpin >= 1 && intpin <= 4) {
169186128Snwhitehorn		/*
170186128Snwhitehorn		 * When an interrupt map is missing, we need to do the
171186128Snwhitehorn		 * standard PCI swizzle and continue mapping at the parent.
172186128Snwhitehorn		 */
173186128Snwhitehorn		return (pcib_route_interrupt(bridge, dev, intpin));
174186128Snwhitehorn	}
175186128Snwhitehorn	return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
176186128Snwhitehorn	    bridge)), bridge, intpin));
177186128Snwhitehorn}
178186128Snwhitehorn
179