1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 */
5 #include <linux/kernel.h>
6 #include <linux/init.h>
7 #include <linux/acpi.h>
8 #include <linux/types.h>
9 #include <linux/pci.h>
10 #include <linux/vgaarb.h>
11 #include <asm/cacheflush.h>
12 #include <asm/loongson.h>
13
14 #define PCI_DEVICE_ID_LOONGSON_HOST 0x7a00
15 #define PCI_DEVICE_ID_LOONGSON_DC1 0x7a06
16 #define PCI_DEVICE_ID_LOONGSON_DC2 0x7a36
17
raw_pci_read(unsigned int domain,unsigned int bus,unsigned int devfn,int reg,int len,u32 * val)18 int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
19 int reg, int len, u32 *val)
20 {
21 struct pci_bus *bus_tmp = pci_find_bus(domain, bus);
22
23 if (bus_tmp)
24 return bus_tmp->ops->read(bus_tmp, devfn, reg, len, val);
25 return -EINVAL;
26 }
27
raw_pci_write(unsigned int domain,unsigned int bus,unsigned int devfn,int reg,int len,u32 val)28 int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
29 int reg, int len, u32 val)
30 {
31 struct pci_bus *bus_tmp = pci_find_bus(domain, bus);
32
33 if (bus_tmp)
34 return bus_tmp->ops->write(bus_tmp, devfn, reg, len, val);
35 return -EINVAL;
36 }
37
mcfg_addr_init(int node)38 phys_addr_t mcfg_addr_init(int node)
39 {
40 return (((u64)node << 44) | MCFG_EXT_PCICFG_BASE);
41 }
42
pcibios_init(void)43 static int __init pcibios_init(void)
44 {
45 unsigned int lsize;
46
47 /*
48 * Set PCI cacheline size to that of the last level in the
49 * cache hierarchy.
50 */
51 lsize = cpu_last_level_cache_line_size();
52
53 BUG_ON(!lsize);
54
55 pci_dfl_cache_line_size = lsize >> 2;
56
57 pr_debug("PCI: pci_cache_line_size set to %d bytes\n", lsize);
58
59 return 0;
60 }
61
62 subsys_initcall(pcibios_init);
63
pcibios_device_add(struct pci_dev * dev)64 int pcibios_device_add(struct pci_dev *dev)
65 {
66 int id;
67 struct irq_domain *dom;
68
69 id = pci_domain_nr(dev->bus);
70 dom = irq_find_matching_fwnode(get_pch_msi_handle(id), DOMAIN_BUS_PCI_MSI);
71 dev_set_msi_domain(&dev->dev, dom);
72
73 return 0;
74 }
75
pcibios_alloc_irq(struct pci_dev * dev)76 int pcibios_alloc_irq(struct pci_dev *dev)
77 {
78 if (acpi_disabled)
79 return 0;
80 if (pci_dev_msi_enabled(dev))
81 return 0;
82 return acpi_pci_irq_enable(dev);
83 }
84
pci_fixup_vgadev(struct pci_dev * pdev)85 static void pci_fixup_vgadev(struct pci_dev *pdev)
86 {
87 struct pci_dev *devp = NULL;
88
89 while ((devp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, devp))) {
90 if (devp->vendor != PCI_VENDOR_ID_LOONGSON) {
91 vga_set_default_device(devp);
92 dev_info(&pdev->dev,
93 "Overriding boot device as %X:%X\n",
94 devp->vendor, devp->device);
95 }
96 }
97 }
98 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_DC1, pci_fixup_vgadev);
99 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_DC2, pci_fixup_vgadev);
100