1294991Szbb/*
2294991Szbb * Copyright (C) 2016 Cavium Inc.
3294991Szbb * All rights reserved.
4294991Szbb *
5294991Szbb * Developed by Semihalf.
6294991Szbb *
7294991Szbb * Redistribution and use in source and binary forms, with or without
8294991Szbb * modification, are permitted provided that the following conditions
9294991Szbb * are met:
10294991Szbb * 1. Redistributions of source code must retain the above copyright
11294991Szbb *    notice, this list of conditions and the following disclaimer.
12294991Szbb * 2. Redistributions in binary form must reproduce the above copyright
13294991Szbb *    notice, this list of conditions and the following disclaimer in the
14294991Szbb *    documentation and/or other materials provided with the distribution.
15294991Szbb *
16294991Szbb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17294991Szbb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18294991Szbb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19294991Szbb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20294991Szbb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21294991Szbb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22294991Szbb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23294991Szbb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24294991Szbb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25294991Szbb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26294991Szbb * SUCH DAMAGE.
27294991Szbb */
28294991Szbb#include "opt_platform.h"
29294991Szbb
30294991Szbb#include <sys/cdefs.h>
31294991Szbb__FBSDID("$FreeBSD$");
32294991Szbb
33294991Szbb#include <sys/param.h>
34294991Szbb#include <sys/systm.h>
35294991Szbb#include <sys/malloc.h>
36294991Szbb#include <sys/types.h>
37294991Szbb#include <sys/sysctl.h>
38294991Szbb#include <sys/kernel.h>
39294991Szbb#include <sys/rman.h>
40294991Szbb#include <sys/module.h>
41294991Szbb#include <sys/bus.h>
42294991Szbb#include <sys/endian.h>
43294991Szbb#include <sys/cpuset.h>
44294991Szbb
45294991Szbb#include <dev/ofw/openfirm.h>
46294991Szbb#include <dev/ofw/ofw_bus.h>
47294991Szbb#include <dev/ofw/ofw_bus_subr.h>
48299932Sandrew
49299932Sandrew#include <dev/pci/pcireg.h>
50299932Sandrew#include <dev/pci/pcivar.h>
51295962Swma#include <dev/pci/pci_host_generic.h>
52299932Sandrew#include <dev/pci/pcib_private.h>
53294991Szbb
54294991Szbb#include "thunder_pcie_common.h"
55294991Szbb
56299932Sandrew#include "pcib_if.h"
57299932Sandrew
58296091Swma#ifdef THUNDERX_PASS_1_1_ERRATA
59296091Swmastatic struct resource * thunder_pcie_fdt_alloc_resource(device_t, device_t,
60296091Swma    int, int *, rman_res_t, rman_res_t, rman_res_t, u_int);
61296091Swma#endif
62295962Swmastatic int thunder_pcie_fdt_attach(device_t);
63294991Szbbstatic int thunder_pcie_fdt_probe(device_t);
64299932Sandrewstatic int thunder_pcie_fdt_get_id(device_t, device_t, enum pci_id_type,
65299932Sandrew    uintptr_t *);
66294991Szbb
67294991Szbbstatic device_method_t thunder_pcie_fdt_methods[] = {
68294991Szbb	/* Device interface */
69294991Szbb	DEVMETHOD(device_probe,		thunder_pcie_fdt_probe),
70294991Szbb	DEVMETHOD(device_attach,	thunder_pcie_fdt_attach),
71296091Swma#ifdef THUNDERX_PASS_1_1_ERRATA
72296091Swma	DEVMETHOD(bus_alloc_resource,	thunder_pcie_fdt_alloc_resource),
73296091Swma#endif
74294993Szbb
75299932Sandrew	/* pcib interface */
76299932Sandrew	DEVMETHOD(pcib_get_id,		thunder_pcie_fdt_get_id),
77299932Sandrew
78294991Szbb	/* End */
79294991Szbb	DEVMETHOD_END
80294991Szbb};
81294991Szbb
82294991SzbbDEFINE_CLASS_1(pcib, thunder_pcie_fdt_driver, thunder_pcie_fdt_methods,
83295962Swma    sizeof(struct generic_pcie_softc), generic_pcie_driver);
84294991Szbb
85294991Szbbstatic devclass_t thunder_pcie_fdt_devclass;
86294991Szbb
87294991SzbbDRIVER_MODULE(thunder_pcib, simplebus, thunder_pcie_fdt_driver,
88294991Szbb    thunder_pcie_fdt_devclass, 0, 0);
89294991SzbbDRIVER_MODULE(thunder_pcib, ofwbus, thunder_pcie_fdt_driver,
90294991Szbb    thunder_pcie_fdt_devclass, 0, 0);
91294991Szbb
92294991Szbb
93294991Szbbstatic int
94294991Szbbthunder_pcie_fdt_probe(device_t dev)
95294991Szbb{
96294991Szbb
97295962Swma	/* Check if we're running on Cavium ThunderX */
98295962Swma	if (!CPU_MATCH(CPU_IMPL_MASK | CPU_PART_MASK,
99295962Swma	    CPU_IMPL_CAVIUM, CPU_PART_THUNDER, 0, 0))
100295962Swma		return (ENXIO);
101295962Swma
102294991Szbb	if (!ofw_bus_status_okay(dev))
103294991Szbb		return (ENXIO);
104294991Szbb
105295962Swma	if (ofw_bus_is_compatible(dev, "pci-host-ecam-generic") ||
106295962Swma	    ofw_bus_is_compatible(dev, "cavium,thunder-pcie") ||
107294991Szbb	    ofw_bus_is_compatible(dev, "cavium,pci-host-thunder-ecam")) {
108294991Szbb		device_set_desc(dev, "Cavium Integrated PCI/PCI-E Controller");
109294991Szbb		return (BUS_PROBE_DEFAULT);
110294991Szbb	}
111294991Szbb
112294991Szbb	return (ENXIO);
113294991Szbb}
114294991Szbb
115294991Szbbstatic int
116294991Szbbthunder_pcie_fdt_attach(device_t dev)
117294991Szbb{
118295962Swma	struct generic_pcie_softc *sc;
119294991Szbb
120294991Szbb	sc = device_get_softc(dev);
121295962Swma	thunder_pcie_identify_ecam(dev, &sc->ecam);
122301031Sandrew	sc->coherent = 1;
123294991Szbb
124295962Swma	return (pci_host_generic_attach(dev));
125294991Szbb}
126294993Szbb
127299932Sandrewstatic int
128299932Sandrewthunder_pcie_fdt_get_id(device_t pci, device_t child, enum pci_id_type type,
129299932Sandrew    uintptr_t *id)
130299932Sandrew{
131299932Sandrew	phandle_t node;
132299932Sandrew	int bsf;
133299932Sandrew
134299932Sandrew	if (type != PCI_ID_MSI)
135299932Sandrew		return (pcib_get_id(pci, child, type, id));
136299932Sandrew
137299932Sandrew	node = ofw_bus_get_node(pci);
138299932Sandrew	if (OF_hasprop(node, "msi-map"))
139299932Sandrew		return (generic_pcie_get_id(pci, child, type, id));
140299932Sandrew
141299932Sandrew	bsf = pci_get_rid(child);
142299932Sandrew	*id = (pci_get_domain(child) << PCI_RID_DOMAIN_SHIFT) | bsf;
143299932Sandrew
144299932Sandrew	return (0);
145299932Sandrew}
146299932Sandrew
147296091Swma#ifdef THUNDERX_PASS_1_1_ERRATA
148296091Swmastatic struct resource *
149296091Swmathunder_pcie_fdt_alloc_resource(device_t dev, device_t child, int type, int *rid,
150296091Swma    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
151296091Swma{
152296091Swma
153296091Swma	if ((int)ofw_bus_get_node(child) > 0)
154296091Swma		return (pci_host_generic_alloc_resource(dev, child,
155296091Swma		    type, rid, start, end, count, flags));
156296091Swma
157296091Swma	return (thunder_pcie_alloc_resource(dev, child,
158296091Swma	    type, rid, start, end, count, flags));
159296091Swma}
160296091Swma#endif
161