xref: /freebsd/sys/powerpc/ofw/ofw_pcib_pci.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1a65a2806SBenno Rice /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
4a65a2806SBenno Rice  * Copyright (c) 2000 Michael Smith
5a65a2806SBenno Rice  * Copyright (c) 2000 BSDi
6a65a2806SBenno Rice  * All rights reserved.
7a65a2806SBenno Rice  *
8a65a2806SBenno Rice  * Redistribution and use in source and binary forms, with or without
9a65a2806SBenno Rice  * modification, are permitted provided that the following conditions
10a65a2806SBenno Rice  * are met:
11a65a2806SBenno Rice  * 1. Redistributions of source code must retain the above copyright
12a65a2806SBenno Rice  *    notice, this list of conditions and the following disclaimer.
13a65a2806SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
14a65a2806SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
15a65a2806SBenno Rice  *    documentation and/or other materials provided with the distribution.
16a65a2806SBenno Rice  *
17a65a2806SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a65a2806SBenno Rice  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a65a2806SBenno Rice  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a65a2806SBenno Rice  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a65a2806SBenno Rice  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a65a2806SBenno Rice  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a65a2806SBenno Rice  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a65a2806SBenno Rice  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a65a2806SBenno Rice  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a65a2806SBenno Rice  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a65a2806SBenno Rice  * SUCH DAMAGE.
28a65a2806SBenno Rice  */
29a65a2806SBenno Rice 
30a65a2806SBenno Rice #include <sys/param.h>
316064b6acSNathan Whitehorn #include <sys/systm.h>
3240cdee9dSPeter Grehan #include <sys/module.h>
33a65a2806SBenno Rice #include <sys/bus.h>
34a65a2806SBenno Rice #include <sys/malloc.h>
35a65a2806SBenno Rice #include <sys/kernel.h>
36a65a2806SBenno Rice 
37a65a2806SBenno Rice #include <dev/ofw/openfirm.h>
38a65a2806SBenno Rice #include <dev/ofw/ofw_pci.h>
3951d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
4094b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
41a65a2806SBenno Rice 
42d2c5276dSWarner Losh #include <dev/pci/pcivar.h>
43d2c5276dSWarner Losh #include <dev/pci/pcireg.h>
44d2c5276dSWarner Losh #include <dev/pci/pcib_private.h>
45a65a2806SBenno Rice 
46eaef5f0aSNathan Whitehorn #include <machine/intr_machdep.h>
47eaef5f0aSNathan Whitehorn 
48a65a2806SBenno Rice #include "pcib_if.h"
49a65a2806SBenno Rice 
50a65a2806SBenno Rice static int	ofw_pcib_pci_probe(device_t bus);
51a65a2806SBenno Rice static int	ofw_pcib_pci_attach(device_t bus);
5251d163d3SNathan Whitehorn static phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev);
5394b4a038SNathan Whitehorn static int	ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev,
5494b4a038SNathan Whitehorn 		    int intpin);
55a65a2806SBenno Rice 
56a65a2806SBenno Rice static device_method_t ofw_pcib_pci_methods[] = {
57a65a2806SBenno Rice 	/* Device interface */
58a65a2806SBenno Rice 	DEVMETHOD(device_probe,		ofw_pcib_pci_probe),
59a65a2806SBenno Rice 	DEVMETHOD(device_attach,	ofw_pcib_pci_attach),
60a65a2806SBenno Rice 
61a65a2806SBenno Rice 	/* pcib interface */
6294b4a038SNathan Whitehorn 	DEVMETHOD(pcib_route_interrupt,	ofw_pcib_pci_route_interrupt),
6328586889SWarner Losh 	DEVMETHOD(pcib_request_feature,	pcib_request_feature_allow),
64a65a2806SBenno Rice 
6551d163d3SNathan Whitehorn 	/* ofw_bus interface */
6651d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_node,	ofw_pcib_pci_get_node),
6751d163d3SNathan Whitehorn 
684b7ec270SMarius Strobl 	DEVMETHOD_END
69a65a2806SBenno Rice };
70a65a2806SBenno Rice 
7194b4a038SNathan Whitehorn struct ofw_pcib_softc {
7294b4a038SNathan Whitehorn         /*
7394b4a038SNathan Whitehorn          * This is here so that we can use pci bridge methods, too - the
7494b4a038SNathan Whitehorn          * generic routines only need the dev, secbus and subbus members
7594b4a038SNathan Whitehorn          * filled.
7694b4a038SNathan Whitehorn          */
7794b4a038SNathan Whitehorn         struct pcib_softc       ops_pcib_sc;
7894b4a038SNathan Whitehorn 	phandle_t		ops_node;
7994b4a038SNathan Whitehorn         struct ofw_bus_iinfo    ops_iinfo;
8094b4a038SNathan Whitehorn };
8194b4a038SNathan Whitehorn 
82c3b91598SNathan Whitehorn DEFINE_CLASS_1(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods,
83c3b91598SNathan Whitehorn     sizeof(struct ofw_pcib_softc), pcib_driver);
8403f6459cSJohn Baldwin EARLY_DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, 0, 0, BUS_PASS_BUS);
85a65a2806SBenno Rice 
86a65a2806SBenno Rice static int
ofw_pcib_pci_probe(device_t dev)87a65a2806SBenno Rice ofw_pcib_pci_probe(device_t dev)
88a65a2806SBenno Rice {
89a65a2806SBenno Rice 
90a65a2806SBenno Rice 	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
91a65a2806SBenno Rice 	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) {
92a65a2806SBenno Rice 		return (ENXIO);
93a65a2806SBenno Rice 	}
94a65a2806SBenno Rice 
950d8d9edaSNathan Whitehorn 	if (ofw_bus_get_node(dev) == -1)
9651d163d3SNathan Whitehorn 		return (ENXIO);
9751d163d3SNathan Whitehorn 
9851d163d3SNathan Whitehorn 	device_set_desc(dev, "OFW PCI-PCI bridge");
9951d163d3SNathan Whitehorn 	return (0);
100a65a2806SBenno Rice }
101a65a2806SBenno Rice 
102a65a2806SBenno Rice static int
ofw_pcib_pci_attach(device_t dev)103a65a2806SBenno Rice ofw_pcib_pci_attach(device_t dev)
104a65a2806SBenno Rice {
10594b4a038SNathan Whitehorn 	struct ofw_pcib_softc *sc;
10694b4a038SNathan Whitehorn 
10794b4a038SNathan Whitehorn 	sc = device_get_softc(dev);
10894b4a038SNathan Whitehorn 	sc->ops_pcib_sc.dev = dev;
10994b4a038SNathan Whitehorn 	sc->ops_node = ofw_bus_get_node(dev);
11094b4a038SNathan Whitehorn 
11194b4a038SNathan Whitehorn 	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
11294b4a038SNathan Whitehorn 	    sizeof(cell_t));
11394b4a038SNathan Whitehorn 
114a65a2806SBenno Rice 	pcib_attach_common(dev);
11567e7d085SJohn Baldwin 	return (pcib_attach_child(dev));
116a65a2806SBenno Rice }
11751d163d3SNathan Whitehorn 
11894b4a038SNathan Whitehorn static phandle_t
ofw_pcib_pci_get_node(device_t bridge,device_t dev)11951d163d3SNathan Whitehorn ofw_pcib_pci_get_node(device_t bridge, device_t dev)
12051d163d3SNathan Whitehorn {
12151d163d3SNathan Whitehorn 	/* We have only one child, the PCI bus, so pass it our node */
12251d163d3SNathan Whitehorn 
12351d163d3SNathan Whitehorn 	return (ofw_bus_get_node(bridge));
12451d163d3SNathan Whitehorn }
12551d163d3SNathan Whitehorn 
12694b4a038SNathan Whitehorn static int
ofw_pcib_pci_route_interrupt(device_t bridge,device_t dev,int intpin)12794b4a038SNathan Whitehorn ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin)
12894b4a038SNathan Whitehorn {
12994b4a038SNathan Whitehorn 	struct ofw_pcib_softc *sc;
13094b4a038SNathan Whitehorn 	struct ofw_bus_iinfo *ii;
13194b4a038SNathan Whitehorn 	struct ofw_pci_register reg;
13295e3bfe8SNathan Whitehorn 	cell_t pintr, mintr[2];
13395e3bfe8SNathan Whitehorn 	int intrcells;
134eaef5f0aSNathan Whitehorn 	phandle_t iparent;
13594b4a038SNathan Whitehorn 
13694b4a038SNathan Whitehorn 	sc = device_get_softc(bridge);
13794b4a038SNathan Whitehorn 	ii = &sc->ops_iinfo;
13894b4a038SNathan Whitehorn 	if (ii->opi_imapsz > 0) {
13994b4a038SNathan Whitehorn 		pintr = intpin;
1406064b6acSNathan Whitehorn 
1416064b6acSNathan Whitehorn 		/* Fabricate imap information if this isn't an OFW device */
1426064b6acSNathan Whitehorn 		bzero(&reg, sizeof(reg));
1436064b6acSNathan Whitehorn 		reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
1446064b6acSNathan Whitehorn 		    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
1456064b6acSNathan Whitehorn 		    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
1466064b6acSNathan Whitehorn 
14795e3bfe8SNathan Whitehorn 		intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
14895e3bfe8SNathan Whitehorn 		    sizeof(reg), &pintr, sizeof(pintr), mintr, sizeof(mintr),
14995e3bfe8SNathan Whitehorn 		    &iparent);
15095e3bfe8SNathan Whitehorn 		if (intrcells) {
15194b4a038SNathan Whitehorn 			/*
15294b4a038SNathan Whitehorn 			 * If we've found a mapping, return it and don't map
15394b4a038SNathan Whitehorn 			 * it again on higher levels - that causes problems
15494b4a038SNathan Whitehorn 			 * in some cases, and never seems to be required.
15594b4a038SNathan Whitehorn 			 */
156bbc6da03SNathan Whitehorn 			mintr[0] = ofw_bus_map_intr(dev, iparent, intrcells,
157bbc6da03SNathan Whitehorn 			    mintr);
15895e3bfe8SNathan Whitehorn 			return (mintr[0]);
15994b4a038SNathan Whitehorn 		}
16094b4a038SNathan Whitehorn 	} else if (intpin >= 1 && intpin <= 4) {
16194b4a038SNathan Whitehorn 		/*
16294b4a038SNathan Whitehorn 		 * When an interrupt map is missing, we need to do the
16394b4a038SNathan Whitehorn 		 * standard PCI swizzle and continue mapping at the parent.
16494b4a038SNathan Whitehorn 		 */
16594b4a038SNathan Whitehorn 		return (pcib_route_interrupt(bridge, dev, intpin));
16694b4a038SNathan Whitehorn 	}
16794b4a038SNathan Whitehorn 	return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
16894b4a038SNathan Whitehorn 	    bridge)), bridge, intpin));
16994b4a038SNathan Whitehorn }
170