Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Zhou Wang | 500a1d9 | 2015-10-29 20:02:51 -0500 | [diff] [blame] | 2 | /* |
Gabriele Paoloni | 5930fe4 | 2015-11-27 01:17:05 +0800 | [diff] [blame] | 3 | * PCIe host controller driver for HiSilicon SoCs |
Zhou Wang | 500a1d9 | 2015-10-29 20:02:51 -0500 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com |
| 6 | * |
Gabriele Paoloni | 5930fe4 | 2015-11-27 01:17:05 +0800 | [diff] [blame] | 7 | * Authors: Zhou Wang <wangzhou1@hisilicon.com> |
| 8 | * Dacai Zhu <zhudacai@hisilicon.com> |
| 9 | * Gabriele Paoloni <gabriele.paoloni@huawei.com> |
Zhou Wang | 500a1d9 | 2015-10-29 20:02:51 -0500 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/interrupt.h> |
Paul Gortmaker | fb38118 | 2016-07-02 19:13:25 -0400 | [diff] [blame] | 12 | #include <linux/init.h> |
Zhou Wang | 500a1d9 | 2015-10-29 20:02:51 -0500 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 14 | #include <linux/pci.h> |
| 15 | #include <linux/pci-acpi.h> |
| 16 | #include <linux/pci-ecam.h> |
Shawn Lin | 6e0832f | 2018-05-31 09:12:37 +0800 | [diff] [blame] | 17 | #include "../../pci.h" |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 18 | |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 19 | #if defined(CONFIG_PCI_HISI) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)) |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 20 | |
Bjorn Helgaas | 4788316 | 2017-02-07 08:41:09 -0600 | [diff] [blame] | 21 | static int hisi_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 22 | int size, u32 *val) |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 23 | { |
| 24 | struct pci_config_window *cfg = bus->sysdata; |
| 25 | int dev = PCI_SLOT(devfn); |
| 26 | |
| 27 | if (bus->number == cfg->busr.start) { |
| 28 | /* access only one slot on each root port */ |
| 29 | if (dev > 0) |
| 30 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 31 | else |
| 32 | return pci_generic_config_read32(bus, devfn, where, |
| 33 | size, val); |
| 34 | } |
| 35 | |
| 36 | return pci_generic_config_read(bus, devfn, where, size, val); |
| 37 | } |
| 38 | |
Bjorn Helgaas | 4788316 | 2017-02-07 08:41:09 -0600 | [diff] [blame] | 39 | static int hisi_pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 40 | int where, int size, u32 val) |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 41 | { |
| 42 | struct pci_config_window *cfg = bus->sysdata; |
| 43 | int dev = PCI_SLOT(devfn); |
| 44 | |
| 45 | if (bus->number == cfg->busr.start) { |
| 46 | /* access only one slot on each root port */ |
| 47 | if (dev > 0) |
| 48 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 49 | else |
| 50 | return pci_generic_config_write32(bus, devfn, where, |
| 51 | size, val); |
| 52 | } |
| 53 | |
| 54 | return pci_generic_config_write(bus, devfn, where, size, val); |
| 55 | } |
| 56 | |
| 57 | static void __iomem *hisi_pcie_map_bus(struct pci_bus *bus, unsigned int devfn, |
| 58 | int where) |
| 59 | { |
| 60 | struct pci_config_window *cfg = bus->sysdata; |
| 61 | void __iomem *reg_base = cfg->priv; |
| 62 | |
| 63 | if (bus->number == cfg->busr.start) |
| 64 | return reg_base + where; |
| 65 | else |
| 66 | return pci_ecam_map_bus(bus, devfn, where); |
| 67 | } |
| 68 | |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 69 | #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS) |
| 70 | |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 71 | static int hisi_pcie_init(struct pci_config_window *cfg) |
| 72 | { |
| 73 | struct device *dev = cfg->parent; |
| 74 | struct acpi_device *adev = to_acpi_device(dev); |
| 75 | struct acpi_pci_root *root = acpi_driver_data(adev); |
| 76 | struct resource *res; |
| 77 | void __iomem *reg_base; |
| 78 | int ret; |
| 79 | |
| 80 | /* |
| 81 | * Retrieve RC base and size from a HISI0081 device with _UID |
| 82 | * matching our segment. |
| 83 | */ |
| 84 | res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL); |
| 85 | if (!res) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | ret = acpi_get_rc_resources(dev, "HISI0081", root->segment, res); |
| 89 | if (ret) { |
| 90 | dev_err(dev, "can't get rc base address\n"); |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | |
Lorenzo Pieralisi | e313a44 | 2017-04-19 17:49:07 +0100 | [diff] [blame] | 94 | reg_base = devm_pci_remap_cfgspace(dev, res->start, resource_size(res)); |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 95 | if (!reg_base) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | cfg->priv = reg_base; |
| 99 | return 0; |
| 100 | } |
| 101 | |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 102 | const struct pci_ecam_ops hisi_pcie_ops = { |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 103 | .init = hisi_pcie_init, |
| 104 | .pci_ops = { |
| 105 | .map_bus = hisi_pcie_map_bus, |
Bjorn Helgaas | 4788316 | 2017-02-07 08:41:09 -0600 | [diff] [blame] | 106 | .read = hisi_pcie_rd_conf, |
| 107 | .write = hisi_pcie_wr_conf, |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
| 111 | #endif |
| 112 | |
| 113 | #ifdef CONFIG_PCI_HISI |
Zhou Wang | 500a1d9 | 2015-10-29 20:02:51 -0500 | [diff] [blame] | 114 | |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 115 | static int hisi_pcie_platform_init(struct pci_config_window *cfg) |
| 116 | { |
| 117 | struct device *dev = cfg->parent; |
| 118 | struct platform_device *pdev = to_platform_device(dev); |
| 119 | struct resource *res; |
| 120 | void __iomem *reg_base; |
| 121 | |
| 122 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 123 | if (!res) { |
| 124 | dev_err(dev, "missing \"reg[1]\"property\n"); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
Lorenzo Pieralisi | e313a44 | 2017-04-19 17:49:07 +0100 | [diff] [blame] | 128 | reg_base = devm_pci_remap_cfgspace(dev, res->start, resource_size(res)); |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 129 | if (!reg_base) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | cfg->priv = reg_base; |
| 133 | return 0; |
| 134 | } |
| 135 | |
Bjorn Helgaas | d388e54 | 2020-06-04 12:59:16 -0500 | [diff] [blame] | 136 | static const struct pci_ecam_ops hisi_pcie_platform_ops = { |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 137 | .init = hisi_pcie_platform_init, |
| 138 | .pci_ops = { |
| 139 | .map_bus = hisi_pcie_map_bus, |
Bjorn Helgaas | 4788316 | 2017-02-07 08:41:09 -0600 | [diff] [blame] | 140 | .read = hisi_pcie_rd_conf, |
| 141 | .write = hisi_pcie_wr_conf, |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | static const struct of_device_id hisi_pcie_almost_ecam_of_match[] = { |
| 146 | { |
Dongdong Liu | b9c1153 | 2017-03-23 21:18:17 +0800 | [diff] [blame] | 147 | .compatible = "hisilicon,hip06-pcie-ecam", |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 148 | .data = &hisi_pcie_platform_ops, |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 149 | }, |
Dongdong Liu | b9c1153 | 2017-03-23 21:18:17 +0800 | [diff] [blame] | 150 | { |
| 151 | .compatible = "hisilicon,hip07-pcie-ecam", |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 152 | .data = &hisi_pcie_platform_ops, |
Dongdong Liu | b9c1153 | 2017-03-23 21:18:17 +0800 | [diff] [blame] | 153 | }, |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 154 | {}, |
| 155 | }; |
| 156 | |
| 157 | static struct platform_driver hisi_pcie_almost_ecam_driver = { |
Rob Herring | b2f75a4 | 2020-04-09 17:49:23 -0600 | [diff] [blame] | 158 | .probe = pci_host_common_probe, |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 159 | .driver = { |
| 160 | .name = "hisi-pcie-almost-ecam", |
| 161 | .of_match_table = hisi_pcie_almost_ecam_of_match, |
Brian Norris | a5f40e8 | 2017-04-20 15:36:25 -0500 | [diff] [blame] | 162 | .suppress_bind_attrs = true, |
Dongdong Liu | a2ec199609 | 2017-02-06 14:25:04 +0800 | [diff] [blame] | 163 | }, |
| 164 | }; |
| 165 | builtin_platform_driver(hisi_pcie_almost_ecam_driver); |
| 166 | |
| 167 | #endif |
Dongdong Liu | 5f00f1a | 2016-12-01 00:45:35 -0600 | [diff] [blame] | 168 | #endif |