18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Loongson PCI Host Controller Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/of_device.h>
98c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
108c2ecf20Sopenharmony_ci#include <linux/pci.h>
118c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
128c2ecf20Sopenharmony_ci#include <linux/pci-acpi.h>
138c2ecf20Sopenharmony_ci#include <linux/pci-ecam.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "../pci.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Device IDs */
188c2ecf20Sopenharmony_ci#define DEV_LS2K_PCIE_PORT0	0x1a05
198c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT0	0x7a09
208c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT1	0x7a19
218c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT2	0x7a29
228c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT3	0x7a39
238c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT4	0x7a49
248c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT5	0x7a59
258c2ecf20Sopenharmony_ci#define DEV_LS7A_PCIE_PORT6	0x7a69
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DEV_LS2K_APB	0x7a02
288c2ecf20Sopenharmony_ci#define DEV_LS7A_CONF	0x7a10
298c2ecf20Sopenharmony_ci#define DEV_LS7A_LPC	0x7a0c
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define FLAG_CFG0	BIT(0)
328c2ecf20Sopenharmony_ci#define FLAG_CFG1	BIT(1)
338c2ecf20Sopenharmony_ci#define FLAG_DEV_FIX	BIT(2)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct pci_controller_data {
368c2ecf20Sopenharmony_ci	u32 flags;
378c2ecf20Sopenharmony_ci	struct pci_ops *ops;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct loongson_pci {
418c2ecf20Sopenharmony_ci	void __iomem *cfg0_base;
428c2ecf20Sopenharmony_ci	void __iomem *cfg1_base;
438c2ecf20Sopenharmony_ci	struct platform_device *pdev;
448c2ecf20Sopenharmony_ci	struct pci_controller_data *data;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* Fixup wrong class code in PCIe bridges */
488c2ecf20Sopenharmony_cistatic void bridge_class_quirk(struct pci_dev *dev)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	dev->class = PCI_CLASS_BRIDGE_PCI << 8;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
538c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT0, bridge_class_quirk);
548c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
558c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT1, bridge_class_quirk);
568c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
578c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT2, bridge_class_quirk);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic void system_bus_quirk(struct pci_dev *pdev)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	/*
628c2ecf20Sopenharmony_ci	 * The address space consumed by these devices is outside the
638c2ecf20Sopenharmony_ci	 * resources of the host bridge.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	pdev->mmio_always_on = 1;
668c2ecf20Sopenharmony_ci	pdev->non_compliant_bars = 1;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
698c2ecf20Sopenharmony_ci			DEV_LS2K_APB, system_bus_quirk);
708c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
718c2ecf20Sopenharmony_ci			DEV_LS7A_CONF, system_bus_quirk);
728c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
738c2ecf20Sopenharmony_ci			DEV_LS7A_LPC, system_bus_quirk);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/*
768c2ecf20Sopenharmony_ci * Some Loongson PCIe ports have hardware limitations on their Maximum Read
778c2ecf20Sopenharmony_ci * Request Size. They can't handle anything larger than this.  Sane
788c2ecf20Sopenharmony_ci * firmware will set proper MRRS at boot, so we only need no_inc_mrrs for
798c2ecf20Sopenharmony_ci * bridges. However, some MIPS Loongson firmware doesn't set MRRS properly,
808c2ecf20Sopenharmony_ci * so we have to enforce maximum safe MRRS, which is 256 bytes.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#ifdef CONFIG_MIPS
838c2ecf20Sopenharmony_cistatic void loongson_set_min_mrrs_quirk(struct pci_dev *pdev)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct pci_bus *bus = pdev->bus;
868c2ecf20Sopenharmony_ci	struct pci_dev *bridge;
878c2ecf20Sopenharmony_ci	static const struct pci_device_id bridge_devids[] = {
888c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS2K_PCIE_PORT0) },
898c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT0) },
908c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT1) },
918c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT2) },
928c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT3) },
938c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT4) },
948c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT5) },
958c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT6) },
968c2ecf20Sopenharmony_ci		{ 0, },
978c2ecf20Sopenharmony_ci	};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* look for the matching bridge */
1008c2ecf20Sopenharmony_ci	while (!pci_is_root_bus(bus)) {
1018c2ecf20Sopenharmony_ci		bridge = bus->self;
1028c2ecf20Sopenharmony_ci		bus = bus->parent;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		if (pci_match_id(bridge_devids, bridge)) {
1058c2ecf20Sopenharmony_ci			if (pcie_get_readrq(pdev) > 256) {
1068c2ecf20Sopenharmony_ci				pci_info(pdev, "limiting MRRS to 256\n");
1078c2ecf20Sopenharmony_ci				pcie_set_readrq(pdev, 256);
1088c2ecf20Sopenharmony_ci			}
1098c2ecf20Sopenharmony_ci			break;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, loongson_set_min_mrrs_quirk);
1148c2ecf20Sopenharmony_ci#endif
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void loongson_mrrs_quirk(struct pci_dev *pdev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	bridge->no_inc_mrrs = 1;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1238c2ecf20Sopenharmony_ci			DEV_LS2K_PCIE_PORT0, loongson_mrrs_quirk);
1248c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1258c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT0, loongson_mrrs_quirk);
1268c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1278c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT1, loongson_mrrs_quirk);
1288c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1298c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT2, loongson_mrrs_quirk);
1308c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1318c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT3, loongson_mrrs_quirk);
1328c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1338c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT4, loongson_mrrs_quirk);
1348c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1358c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT5, loongson_mrrs_quirk);
1368c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
1378c2ecf20Sopenharmony_ci			DEV_LS7A_PCIE_PORT6, loongson_mrrs_quirk);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic void loongson_pci_pin_quirk(struct pci_dev *pdev)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	pdev->pin = 1 + (PCI_FUNC(pdev->devfn) & 3);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1448c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_AHCI, loongson_pci_pin_quirk);
1458c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1468c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_EHCI, loongson_pci_pin_quirk);
1478c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1488c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_OHCI, loongson_pci_pin_quirk);
1498c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1508c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_DC1, loongson_pci_pin_quirk);
1518c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1528c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_DC2, loongson_pci_pin_quirk);
1538c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1548c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_GPU, loongson_pci_pin_quirk);
1558c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1568c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_HDMI, loongson_pci_pin_quirk);
1578c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1588c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_GMAC, loongson_pci_pin_quirk);
1598c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
1608c2ecf20Sopenharmony_ci			PCI_DEVICE_ID_LOONGSON_GNET, loongson_pci_pin_quirk);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct loongson_pci *pci_bus_to_loongson_pci(struct pci_bus *bus)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct pci_config_window *cfg;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (acpi_disabled)
1678c2ecf20Sopenharmony_ci		return (struct loongson_pci *)(bus->sysdata);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	cfg = bus->sysdata;
1708c2ecf20Sopenharmony_ci	return (struct loongson_pci *)(cfg->priv);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void __iomem *cfg0_map(struct loongson_pci *priv,
1748c2ecf20Sopenharmony_ci			      struct pci_bus *bus, unsigned int devfn, int where)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	unsigned long addroff = 0x0;
1778c2ecf20Sopenharmony_ci	unsigned char busnum = bus->number;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (!pci_is_root_bus(bus)) {
1808c2ecf20Sopenharmony_ci		addroff |= BIT(24); /* Type 1 Access */
1818c2ecf20Sopenharmony_ci		addroff |= (busnum << 16);
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	addroff |= (devfn << 8) | where;
1848c2ecf20Sopenharmony_ci	return priv->cfg0_base + addroff;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic void __iomem *cfg1_map(struct loongson_pci *priv,
1888c2ecf20Sopenharmony_ci			      struct pci_bus *bus, unsigned int devfn, int where)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	unsigned long addroff = 0x0;
1918c2ecf20Sopenharmony_ci	unsigned char busnum = bus->number;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!pci_is_root_bus(bus)) {
1948c2ecf20Sopenharmony_ci		addroff |= BIT(28); /* Type 1 Access */
1958c2ecf20Sopenharmony_ci		addroff |= (busnum << 16);
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	addroff |= (devfn << 8) | (where & 0xff) | ((where & 0xf00) << 16);
1988c2ecf20Sopenharmony_ci	return priv->cfg1_base + addroff;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic void __iomem *pci_loongson_map_bus(struct pci_bus *bus, unsigned int devfn,
2028c2ecf20Sopenharmony_ci			       int where)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	unsigned int device = PCI_SLOT(devfn);
2058c2ecf20Sopenharmony_ci	unsigned int function = PCI_FUNC(devfn);
2068c2ecf20Sopenharmony_ci	struct loongson_pci *priv = pci_bus_to_loongson_pci(bus);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/*
2098c2ecf20Sopenharmony_ci	 * Do not read more than one device on the bus other than
2108c2ecf20Sopenharmony_ci	 * the host bus. For our hardware the root bus is always bus 0.
2118c2ecf20Sopenharmony_ci	 */
2128c2ecf20Sopenharmony_ci	if ((priv->data->flags & FLAG_DEV_FIX) && bus->self) {
2138c2ecf20Sopenharmony_ci		if (!pci_is_root_bus(bus) && (device > 0))
2148c2ecf20Sopenharmony_ci			return NULL;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Don't access unexisting devices */
2188c2ecf20Sopenharmony_ci	if (pci_is_root_bus(bus) && (device >= 9 && device <= 20 && function > 0))
2198c2ecf20Sopenharmony_ci		return NULL;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* CFG0 can only access standard space */
2228c2ecf20Sopenharmony_ci	if (where < PCI_CFG_SPACE_SIZE && priv->cfg0_base)
2238c2ecf20Sopenharmony_ci		return cfg0_map(priv, bus, devfn, where);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* CFG1 can access extended space */
2268c2ecf20Sopenharmony_ci	if (where < PCI_CFG_SPACE_EXP_SIZE && priv->cfg1_base)
2278c2ecf20Sopenharmony_ci		return cfg1_map(priv, bus, devfn, where);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	return NULL;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int loongson_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	int irq;
2378c2ecf20Sopenharmony_ci	u8 val;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	irq = of_irq_parse_and_map_pci(dev, slot, pin);
2408c2ecf20Sopenharmony_ci	if (irq > 0)
2418c2ecf20Sopenharmony_ci		return irq;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* Care i8259 legacy systems */
2448c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
2458c2ecf20Sopenharmony_ci	/* i8259 only have 15 IRQs */
2468c2ecf20Sopenharmony_ci	if (val > 15)
2478c2ecf20Sopenharmony_ci		return 0;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return val;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* LS2K/LS7A accept 8/16/32-bit PCI config operations */
2538c2ecf20Sopenharmony_cistatic struct pci_ops loongson_pci_ops = {
2548c2ecf20Sopenharmony_ci	.map_bus = pci_loongson_map_bus,
2558c2ecf20Sopenharmony_ci	.read	= pci_generic_config_read,
2568c2ecf20Sopenharmony_ci	.write	= pci_generic_config_write,
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/* RS780/SR5690 only accept 32-bit PCI config operations */
2608c2ecf20Sopenharmony_cistatic struct pci_ops loongson_pci_ops32 = {
2618c2ecf20Sopenharmony_ci	.map_bus = pci_loongson_map_bus,
2628c2ecf20Sopenharmony_ci	.read	= pci_generic_config_read32,
2638c2ecf20Sopenharmony_ci	.write	= pci_generic_config_write32,
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic const struct pci_controller_data ls2k_pci_data = {
2678c2ecf20Sopenharmony_ci	.flags = FLAG_CFG1 | FLAG_DEV_FIX,
2688c2ecf20Sopenharmony_ci	.ops = &loongson_pci_ops,
2698c2ecf20Sopenharmony_ci};
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic const struct pci_controller_data ls7a_pci_data = {
2728c2ecf20Sopenharmony_ci	.flags = FLAG_CFG1 | FLAG_DEV_FIX,
2738c2ecf20Sopenharmony_ci	.ops = &loongson_pci_ops,
2748c2ecf20Sopenharmony_ci};
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic const struct pci_controller_data rs780e_pci_data = {
2778c2ecf20Sopenharmony_ci	.flags = FLAG_CFG0,
2788c2ecf20Sopenharmony_ci	.ops = &loongson_pci_ops32,
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic const struct of_device_id loongson_pci_of_match[] = {
2828c2ecf20Sopenharmony_ci	{ .compatible = "loongson,ls2k-pci",
2838c2ecf20Sopenharmony_ci		.data = (void *)&ls2k_pci_data, },
2848c2ecf20Sopenharmony_ci	{ .compatible = "loongson,ls7a-pci",
2858c2ecf20Sopenharmony_ci		.data = (void *)&ls7a_pci_data, },
2868c2ecf20Sopenharmony_ci	{ .compatible = "loongson,rs780e-pci",
2878c2ecf20Sopenharmony_ci		.data = (void *)&rs780e_pci_data, },
2888c2ecf20Sopenharmony_ci	{}
2898c2ecf20Sopenharmony_ci};
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int loongson_pci_probe(struct platform_device *pdev)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct loongson_pci *priv;
2948c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2958c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
2968c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge;
2978c2ecf20Sopenharmony_ci	struct resource *regs;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (!node)
3008c2ecf20Sopenharmony_ci		return -ENODEV;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*priv));
3038c2ecf20Sopenharmony_ci	if (!bridge)
3048c2ecf20Sopenharmony_ci		return -ENODEV;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	priv = pci_host_bridge_priv(bridge);
3078c2ecf20Sopenharmony_ci	priv->pdev = pdev;
3088c2ecf20Sopenharmony_ci	priv->data = (struct pci_controller_data *)of_device_get_match_data(dev);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (priv->data->flags & FLAG_CFG0) {
3118c2ecf20Sopenharmony_ci		regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3128c2ecf20Sopenharmony_ci		if (!regs)
3138c2ecf20Sopenharmony_ci			dev_err(dev, "missing mem resources for cfg0\n");
3148c2ecf20Sopenharmony_ci		else {
3158c2ecf20Sopenharmony_ci			priv->cfg0_base = devm_pci_remap_cfg_resource(dev, regs);
3168c2ecf20Sopenharmony_ci			if (IS_ERR(priv->cfg0_base))
3178c2ecf20Sopenharmony_ci				return PTR_ERR(priv->cfg0_base);
3188c2ecf20Sopenharmony_ci		}
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (priv->data->flags & FLAG_CFG1) {
3228c2ecf20Sopenharmony_ci		regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3238c2ecf20Sopenharmony_ci		if (!regs)
3248c2ecf20Sopenharmony_ci			dev_info(dev, "missing mem resource for cfg1\n");
3258c2ecf20Sopenharmony_ci		else {
3268c2ecf20Sopenharmony_ci			priv->cfg1_base = devm_pci_remap_cfg_resource(dev, regs);
3278c2ecf20Sopenharmony_ci			if (IS_ERR(priv->cfg1_base))
3288c2ecf20Sopenharmony_ci				priv->cfg1_base = NULL;
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	bridge->sysdata = priv;
3338c2ecf20Sopenharmony_ci	bridge->ops = priv->data->ops;
3348c2ecf20Sopenharmony_ci	bridge->map_irq = loongson_map_irq;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return pci_host_probe(bridge);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic struct platform_driver loongson_pci_driver = {
3408c2ecf20Sopenharmony_ci	.driver = {
3418c2ecf20Sopenharmony_ci		.name = "loongson-pci",
3428c2ecf20Sopenharmony_ci		.of_match_table = loongson_pci_of_match,
3438c2ecf20Sopenharmony_ci	},
3448c2ecf20Sopenharmony_ci	.probe = loongson_pci_probe,
3458c2ecf20Sopenharmony_ci};
3468c2ecf20Sopenharmony_cibuiltin_platform_driver(loongson_pci_driver);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci#endif
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic int loongson_pci_ecam_init(struct pci_config_window *cfg)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct device *dev = cfg->parent;
3558c2ecf20Sopenharmony_ci	struct loongson_pci *priv;
3568c2ecf20Sopenharmony_ci	struct pci_controller_data *data;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3598c2ecf20Sopenharmony_ci	if (!priv)
3608c2ecf20Sopenharmony_ci		return -ENOMEM;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
3638c2ecf20Sopenharmony_ci	if (!data)
3648c2ecf20Sopenharmony_ci		return -ENOMEM;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	cfg->priv = priv;
3678c2ecf20Sopenharmony_ci	data->flags = FLAG_CFG1;
3688c2ecf20Sopenharmony_ci	priv->data = data;
3698c2ecf20Sopenharmony_ci	priv->cfg1_base = cfg->win - (cfg->busr.start << 16);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	return 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ciconst struct pci_ecam_ops loongson_pci_ecam_ops = {
3758c2ecf20Sopenharmony_ci	.bus_shift = 16,
3768c2ecf20Sopenharmony_ci	.init	   = loongson_pci_ecam_init,
3778c2ecf20Sopenharmony_ci	.pci_ops   = {
3788c2ecf20Sopenharmony_ci		.map_bus = pci_loongson_map_bus,
3798c2ecf20Sopenharmony_ci		.read	 = pci_generic_config_read,
3808c2ecf20Sopenharmony_ci		.write	 = pci_generic_config_write,
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci};
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci#endif
385