1/*-
2 * Copyright 2015 Justin Hibbits
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * From: FreeBSD: src/sys/powerpc/mpc85xx/pci_ocp.c,v 1.9 2010/03/23 23:46:28 marcel
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/ktr.h>
37#include <sys/sockio.h>
38#include <sys/mbuf.h>
39#include <sys/malloc.h>
40#include <sys/kernel.h>
41#include <sys/module.h>
42#include <sys/socket.h>
43#include <sys/queue.h>
44#include <sys/bus.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/rman.h>
48#include <sys/endian.h>
49
50#include <vm/vm.h>
51#include <vm/pmap.h>
52
53#include <dev/ofw/openfirm.h>
54#include <dev/ofw/ofw_pci.h>
55#include <dev/ofw/ofw_bus.h>
56#include <dev/ofw/ofw_bus_subr.h>
57
58#include <dev/pci/pcivar.h>
59#include <dev/pci/pcireg.h>
60#include <dev/pci/pcib_private.h>
61
62#include <machine/intr_machdep.h>
63
64#include "pcib_if.h"
65
66struct fsl_pcib_softc {
67        /*
68         * This is here so that we can use pci bridge methods, too - the
69         * generic routines only need the dev, secbus and subbus members
70         * filled.
71         */
72        struct pcib_softc       ops_pcib_sc;
73	phandle_t		ops_node;
74        struct ofw_bus_iinfo    ops_iinfo;
75};
76
77static int
78fsl_pcib_rc_probe(device_t dev)
79{
80
81	if (pci_get_vendor(dev) != 0x1957)
82		return (ENXIO);
83	if (pci_get_progif(dev) != 0)
84		return (ENXIO);
85	if (pci_get_class(dev) != PCIC_PROCESSOR)
86		return (ENXIO);
87	if (pci_get_subclass(dev) != PCIS_PROCESSOR_POWERPC)
88		return (ENXIO);
89
90	return (BUS_PROBE_DEFAULT);
91}
92
93static int
94fsl_pcib_rc_attach(device_t dev)
95{
96	struct fsl_pcib_softc *sc;
97
98	sc = device_get_softc(dev);
99	sc->ops_pcib_sc.dev = dev;
100	sc->ops_node = ofw_bus_get_node(dev);
101
102	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
103	    sizeof(cell_t));
104
105	pcib_attach_common(dev);
106	return (pcib_attach_child(dev));
107}
108
109static phandle_t
110fsl_pcib_rc_get_node(device_t bridge, device_t dev)
111{
112	/* We have only one child, the PCI bus, so pass it our node */
113
114	return (ofw_bus_get_node(bridge));
115}
116
117static int
118fsl_pcib_rc_route_interrupt(device_t bridge, device_t dev, int intpin)
119{
120	struct fsl_pcib_softc *sc;
121	struct ofw_bus_iinfo *ii;
122	struct ofw_pci_register reg;
123	cell_t pintr, mintr[2];
124	int intrcells;
125	phandle_t iparent;
126
127	sc = device_get_softc(bridge);
128	ii = &sc->ops_iinfo;
129	if (ii->opi_imapsz > 0) {
130		pintr = intpin;
131
132		/* Fabricate imap information if this isn't an OFW device */
133		bzero(&reg, sizeof(reg));
134		reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
135		    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
136		    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
137
138		intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
139		    sizeof(reg), &pintr, sizeof(pintr), mintr, sizeof(mintr),
140		    &iparent);
141		if (intrcells) {
142			/*
143			 * If we've found a mapping, return it and don't map
144			 * it again on higher levels - that causes problems
145			 * in some cases, and never seems to be required.
146			 */
147			mintr[0] = ofw_bus_map_intr(dev, iparent, intrcells,
148			    mintr);
149			return (mintr[0]);
150		}
151	} else if (intpin >= 1 && intpin <= 4) {
152		/*
153		 * When an interrupt map is missing, we need to do the
154		 * standard PCI swizzle and continue mapping at the parent.
155		 */
156		return (pcib_route_interrupt(bridge, dev, intpin));
157	}
158	return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
159	    bridge)), bridge, intpin));
160}
161
162static device_method_t fsl_pcib_rc_methods[] = {
163	DEVMETHOD(device_probe,		fsl_pcib_rc_probe),
164	DEVMETHOD(device_attach,	fsl_pcib_rc_attach),
165
166	/* pcib interface */
167	DEVMETHOD(pcib_route_interrupt,	fsl_pcib_rc_route_interrupt),
168	DEVMETHOD(pcib_request_feature,	pcib_request_feature_allow),
169
170	/* ofw_bus interface */
171	DEVMETHOD(ofw_bus_get_node,	fsl_pcib_rc_get_node),
172	DEVMETHOD_END
173};
174
175static devclass_t fsl_pcib_rc_devclass;
176DEFINE_CLASS_1(pcib, fsl_pcib_rc_driver, fsl_pcib_rc_methods,
177    sizeof(struct fsl_pcib_softc), pcib_driver);
178DRIVER_MODULE(rcpcib, pci, fsl_pcib_rc_driver, fsl_pcib_rc_devclass, 0, 0);
179