1 // SPDX-License-Identifier: GPL-2.0
3 * Loongson PCI Host Controller Driver
5 * Copyright (C) 2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
8 #include <linux/of_device.h>
9 #include <linux/of_pci.h>
10 #include <linux/pci.h>
11 #include <linux/pci_ids.h>
12 #include <linux/pci-acpi.h>
13 #include <linux/pci-ecam.h>
18 #define DEV_PCIE_PORT_0 0x7a09
19 #define DEV_PCIE_PORT_1 0x7a19
20 #define DEV_PCIE_PORT_2 0x7a29
22 #define DEV_LS2K_APB 0x7a02
23 #define DEV_LS7A_GMAC 0x7a03
24 #define DEV_LS7A_DC1 0x7a06
25 #define DEV_LS7A_LPC 0x7a0c
26 #define DEV_LS7A_AHCI 0x7a08
27 #define DEV_LS7A_CONF 0x7a10
28 #define DEV_LS7A_GNET 0x7a13
29 #define DEV_LS7A_EHCI 0x7a14
30 #define DEV_LS7A_DC2 0x7a36
31 #define DEV_LS7A_HDMI 0x7a37
33 #define FLAG_CFG0 BIT(0)
34 #define FLAG_CFG1 BIT(1)
35 #define FLAG_DEV_FIX BIT(2)
36 #define FLAG_DEV_HIDDEN BIT(3)
38 struct loongson_pci_data {
44 void __iomem *cfg0_base;
45 void __iomem *cfg1_base;
46 struct platform_device *pdev;
47 const struct loongson_pci_data *data;
50 /* Fixup wrong class code in PCIe bridges */
51 static void bridge_class_quirk(struct pci_dev *dev)
53 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
55 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
56 DEV_PCIE_PORT_0, bridge_class_quirk);
57 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
58 DEV_PCIE_PORT_1, bridge_class_quirk);
59 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
60 DEV_PCIE_PORT_2, bridge_class_quirk);
62 static void system_bus_quirk(struct pci_dev *pdev)
65 * The address space consumed by these devices is outside the
66 * resources of the host bridge.
68 pdev->mmio_always_on = 1;
69 pdev->non_compliant_bars = 1;
71 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
72 DEV_LS2K_APB, system_bus_quirk);
73 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
74 DEV_LS7A_CONF, system_bus_quirk);
75 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
76 DEV_LS7A_LPC, system_bus_quirk);
78 static void loongson_mrrs_quirk(struct pci_dev *dev)
80 struct pci_bus *bus = dev->bus;
81 struct pci_dev *bridge;
82 static const struct pci_device_id bridge_devids[] = {
83 { PCI_VDEVICE(LOONGSON, DEV_PCIE_PORT_0) },
84 { PCI_VDEVICE(LOONGSON, DEV_PCIE_PORT_1) },
85 { PCI_VDEVICE(LOONGSON, DEV_PCIE_PORT_2) },
89 /* look for the matching bridge */
90 while (!pci_is_root_bus(bus)) {
94 * Some Loongson PCIe ports have a h/w limitation of
95 * 256 bytes maximum read request size. They can't handle
96 * anything larger than this. So force this limit on
97 * any devices attached under these ports.
99 if (pci_match_id(bridge_devids, bridge)) {
100 if (pcie_get_readrq(dev) > 256) {
101 pci_info(dev, "limiting MRRS to 256\n");
102 pcie_set_readrq(dev, 256);
108 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, loongson_mrrs_quirk);
110 static void loongson_pci_pin_quirk(struct pci_dev *pdev)
112 pdev->pin = 1 + (PCI_FUNC(pdev->devfn) & 3);
114 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
115 DEV_LS7A_DC1, loongson_pci_pin_quirk);
116 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
117 DEV_LS7A_DC2, loongson_pci_pin_quirk);
118 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
119 DEV_LS7A_GMAC, loongson_pci_pin_quirk);
120 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
121 DEV_LS7A_AHCI, loongson_pci_pin_quirk);
122 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
123 DEV_LS7A_EHCI, loongson_pci_pin_quirk);
124 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
125 DEV_LS7A_GNET, loongson_pci_pin_quirk);
126 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
127 DEV_LS7A_HDMI, loongson_pci_pin_quirk);
129 static struct loongson_pci *pci_bus_to_loongson_pci(struct pci_bus *bus)
131 struct pci_config_window *cfg;
134 return (struct loongson_pci *)(bus->sysdata);
137 return (struct loongson_pci *)(cfg->priv);
140 static void __iomem *cfg0_map(struct loongson_pci *priv, struct pci_bus *bus,
141 unsigned int devfn, int where)
143 unsigned long addroff = 0x0;
144 unsigned char busnum = bus->number;
146 if (!pci_is_root_bus(bus)) {
147 addroff |= BIT(24); /* Type 1 Access */
148 addroff |= (busnum << 16);
150 addroff |= (devfn << 8) | where;
151 return priv->cfg0_base + addroff;
154 static void __iomem *cfg1_map(struct loongson_pci *priv, struct pci_bus *bus,
155 unsigned int devfn, int where)
157 unsigned long addroff = 0x0;
158 unsigned char busnum = bus->number;
160 if (!pci_is_root_bus(bus)) {
161 addroff |= BIT(28); /* Type 1 Access */
162 addroff |= (busnum << 16);
164 addroff |= (devfn << 8) | (where & 0xff) | ((where & 0xf00) << 16);
165 return priv->cfg1_base + addroff;
168 static bool pdev_may_exist(struct pci_bus *bus, unsigned int device,
169 unsigned int function)
171 return !(pci_is_root_bus(bus) &&
172 (device >= 9 && device <= 20) && (function > 0));
175 static void __iomem *pci_loongson_map_bus(struct pci_bus *bus,
176 unsigned int devfn, int where)
178 unsigned int device = PCI_SLOT(devfn);
179 unsigned int function = PCI_FUNC(devfn);
180 struct loongson_pci *priv = pci_bus_to_loongson_pci(bus);
183 * Do not read more than one device on the bus other than
186 if ((priv->data->flags & FLAG_DEV_FIX) && bus->self) {
187 if (!pci_is_root_bus(bus) && (device > 0))
191 /* Don't access non-existent devices */
192 if (priv->data->flags & FLAG_DEV_HIDDEN) {
193 if (!pdev_may_exist(bus, device, function))
197 /* CFG0 can only access standard space */
198 if (where < PCI_CFG_SPACE_SIZE && priv->cfg0_base)
199 return cfg0_map(priv, bus, devfn, where);
201 /* CFG1 can access extended space */
202 if (where < PCI_CFG_SPACE_EXP_SIZE && priv->cfg1_base)
203 return cfg1_map(priv, bus, devfn, where);
210 static int loongson_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
215 irq = of_irq_parse_and_map_pci(dev, slot, pin);
219 /* Care i8259 legacy systems */
220 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
221 /* i8259 only have 15 IRQs */
228 /* LS2K/LS7A accept 8/16/32-bit PCI config operations */
229 static struct pci_ops loongson_pci_ops = {
230 .map_bus = pci_loongson_map_bus,
231 .read = pci_generic_config_read,
232 .write = pci_generic_config_write,
235 /* RS780/SR5690 only accept 32-bit PCI config operations */
236 static struct pci_ops loongson_pci_ops32 = {
237 .map_bus = pci_loongson_map_bus,
238 .read = pci_generic_config_read32,
239 .write = pci_generic_config_write32,
242 static const struct loongson_pci_data ls2k_pci_data = {
243 .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
244 .ops = &loongson_pci_ops,
247 static const struct loongson_pci_data ls7a_pci_data = {
248 .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
249 .ops = &loongson_pci_ops,
252 static const struct loongson_pci_data rs780e_pci_data = {
254 .ops = &loongson_pci_ops32,
257 static const struct of_device_id loongson_pci_of_match[] = {
258 { .compatible = "loongson,ls2k-pci",
259 .data = &ls2k_pci_data, },
260 { .compatible = "loongson,ls7a-pci",
261 .data = &ls7a_pci_data, },
262 { .compatible = "loongson,rs780e-pci",
263 .data = &rs780e_pci_data, },
267 static int loongson_pci_probe(struct platform_device *pdev)
269 struct loongson_pci *priv;
270 struct device *dev = &pdev->dev;
271 struct device_node *node = dev->of_node;
272 struct pci_host_bridge *bridge;
273 struct resource *regs;
278 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*priv));
282 priv = pci_host_bridge_priv(bridge);
284 priv->data = of_device_get_match_data(dev);
286 if (priv->data->flags & FLAG_CFG0) {
287 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 dev_err(dev, "missing mem resources for cfg0\n");
291 priv->cfg0_base = devm_pci_remap_cfg_resource(dev, regs);
292 if (IS_ERR(priv->cfg0_base))
293 return PTR_ERR(priv->cfg0_base);
297 if (priv->data->flags & FLAG_CFG1) {
298 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
300 dev_info(dev, "missing mem resource for cfg1\n");
302 priv->cfg1_base = devm_pci_remap_cfg_resource(dev, regs);
303 if (IS_ERR(priv->cfg1_base))
304 priv->cfg1_base = NULL;
308 bridge->sysdata = priv;
309 bridge->ops = priv->data->ops;
310 bridge->map_irq = loongson_map_irq;
312 return pci_host_probe(bridge);
315 static struct platform_driver loongson_pci_driver = {
317 .name = "loongson-pci",
318 .of_match_table = loongson_pci_of_match,
320 .probe = loongson_pci_probe,
322 builtin_platform_driver(loongson_pci_driver);
328 static int loongson_pci_ecam_init(struct pci_config_window *cfg)
330 struct device *dev = cfg->parent;
331 struct loongson_pci *priv;
332 struct loongson_pci_data *data;
334 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
338 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
343 data->flags = FLAG_CFG1 | FLAG_DEV_HIDDEN;
345 priv->cfg1_base = cfg->win - (cfg->busr.start << 16);
350 const struct pci_ecam_ops loongson_pci_ecam_ops = {
352 .init = loongson_pci_ecam_init,
354 .map_bus = pci_loongson_map_bus,
355 .read = pci_generic_config_read,
356 .write = pci_generic_config_write,