Bjorn Helgaas | 736759e | 2018-01-26 14:22:04 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Krzysztof Wilczyński | 347269c | 2021-07-03 15:13:02 +0000 | [diff] [blame] | 2 | /* |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 3 | * APM X-Gene PCIe Driver |
| 4 | * |
| 5 | * Copyright (c) 2014 Applied Micro Circuits Corporation. |
| 6 | * |
| 7 | * Author: Tanmay Inamdar <tinamdar@apm.com>. |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 8 | */ |
Stephen Boyd | 29ef709 | 2015-01-22 11:25:54 -0800 | [diff] [blame] | 9 | #include <linux/clk.h> |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/jiffies.h> |
| 13 | #include <linux/memblock.h> |
Paul Gortmaker | 50dcd29 | 2016-07-02 19:13:34 -0400 | [diff] [blame] | 14 | #include <linux/init.h> |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_address.h> |
| 17 | #include <linux/of_irq.h> |
| 18 | #include <linux/of_pci.h> |
| 19 | #include <linux/pci.h> |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 20 | #include <linux/pci-acpi.h> |
| 21 | #include <linux/pci-ecam.h> |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/slab.h> |
| 24 | |
Rob Herring | 9e2aee8 | 2018-05-11 12:15:30 -0500 | [diff] [blame] | 25 | #include "../pci.h" |
| 26 | |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 27 | #define PCIECORE_CTLANDSTATUS 0x50 |
| 28 | #define PIM1_1L 0x80 |
| 29 | #define IBAR2 0x98 |
| 30 | #define IR2MSK 0x9c |
| 31 | #define PIM2_1L 0xa0 |
| 32 | #define IBAR3L 0xb4 |
| 33 | #define IR3MSKL 0xbc |
| 34 | #define PIM3_1L 0xc4 |
| 35 | #define OMR1BARL 0x100 |
| 36 | #define OMR2BARL 0x118 |
| 37 | #define OMR3BARL 0x130 |
| 38 | #define CFGBARL 0x154 |
| 39 | #define CFGBARH 0x158 |
| 40 | #define CFGCTL 0x15c |
| 41 | #define RTDID 0x160 |
| 42 | #define BRIDGE_CFG_0 0x2000 |
| 43 | #define BRIDGE_CFG_4 0x2010 |
| 44 | #define BRIDGE_STATUS_0 0x2600 |
| 45 | |
| 46 | #define LINK_UP_MASK 0x00000100 |
| 47 | #define AXI_EP_CFG_ACCESS 0x10000 |
| 48 | #define EN_COHERENCY 0xF0000000 |
| 49 | #define EN_REG 0x00000001 |
| 50 | #define OB_LO_IO 0x00000002 |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 51 | #define XGENE_PCIE_DEVICEID 0xE004 |
| 52 | #define SZ_1T (SZ_1G*1024ULL) |
| 53 | #define PIPE_PHY_RATE_RD(src) ((0xc000 & (u32)(src)) >> 0xe) |
| 54 | |
Bjorn Helgaas | 582ffae | 2017-09-05 12:58:03 -0500 | [diff] [blame] | 55 | #define XGENE_V1_PCI_EXP_CAP 0x40 |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 56 | |
| 57 | /* PCIe IP version */ |
| 58 | #define XGENE_PCIE_IP_VER_UNKN 0 |
| 59 | #define XGENE_PCIE_IP_VER_1 1 |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 60 | #define XGENE_PCIE_IP_VER_2 2 |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 61 | |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 62 | #if defined(CONFIG_PCI_XGENE) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)) |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 63 | struct xgene_pcie { |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 64 | struct device_node *node; |
| 65 | struct device *dev; |
| 66 | struct clk *clk; |
| 67 | void __iomem *csr_base; |
| 68 | void __iomem *cfg_base; |
| 69 | unsigned long cfg_addr; |
| 70 | bool link_up; |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 71 | u32 version; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 72 | }; |
| 73 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 74 | static u32 xgene_pcie_readl(struct xgene_pcie *port, u32 reg) |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 75 | { |
| 76 | return readl(port->csr_base + reg); |
| 77 | } |
| 78 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 79 | static void xgene_pcie_writel(struct xgene_pcie *port, u32 reg, u32 val) |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 80 | { |
| 81 | writel(val, port->csr_base + reg); |
| 82 | } |
| 83 | |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 84 | static inline u32 pcie_bar_low_val(u32 addr, u32 flags) |
| 85 | { |
| 86 | return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags; |
| 87 | } |
| 88 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 89 | static inline struct xgene_pcie *pcie_bus_to_port(struct pci_bus *bus) |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 90 | { |
| 91 | struct pci_config_window *cfg; |
| 92 | |
| 93 | if (acpi_disabled) |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 94 | return (struct xgene_pcie *)(bus->sysdata); |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 95 | |
| 96 | cfg = bus->sysdata; |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 97 | return (struct xgene_pcie *)(cfg->priv); |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 100 | /* |
| 101 | * When the address bit [17:16] is 2'b01, the Configuration access will be |
| 102 | * treated as Type 1 and it will be forwarded to external PCIe device. |
| 103 | */ |
| 104 | static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus) |
| 105 | { |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 106 | struct xgene_pcie *port = pcie_bus_to_port(bus); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 107 | |
| 108 | if (bus->number >= (bus->primary + 1)) |
| 109 | return port->cfg_base + AXI_EP_CFG_ACCESS; |
| 110 | |
| 111 | return port->cfg_base; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * For Configuration request, RTDID register is used as Bus Number, |
| 116 | * Device Number and Function number of the header fields. |
| 117 | */ |
| 118 | static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn) |
| 119 | { |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 120 | struct xgene_pcie *port = pcie_bus_to_port(bus); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 121 | unsigned int b, d, f; |
| 122 | u32 rtdid_val = 0; |
| 123 | |
| 124 | b = bus->number; |
| 125 | d = PCI_SLOT(devfn); |
| 126 | f = PCI_FUNC(devfn); |
| 127 | |
| 128 | if (!pci_is_root_bus(bus)) |
| 129 | rtdid_val = (b << 8) | (d << 3) | f; |
| 130 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 131 | xgene_pcie_writel(port, RTDID, rtdid_val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 132 | /* read the register back to ensure flush */ |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 133 | xgene_pcie_readl(port, RTDID); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as |
| 138 | * the translation from PCI bus to native BUS. Entire DDR region |
| 139 | * is mapped into PCIe space using these registers, so it can be |
| 140 | * reached by DMA from EP devices. The BAR0/1 of bridge should be |
| 141 | * hidden during enumeration to avoid the sizing and resource allocation |
| 142 | * by PCIe core. |
| 143 | */ |
| 144 | static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset) |
| 145 | { |
| 146 | if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) || |
| 147 | (offset == PCI_BASE_ADDRESS_1))) |
| 148 | return true; |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
Feng Kan | 085a68d | 2015-02-17 15:14:00 -0800 | [diff] [blame] | 153 | static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn, |
Bjorn Helgaas | fca4848 | 2017-09-05 13:09:05 -0500 | [diff] [blame] | 154 | int offset) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 155 | { |
Duc Dang | ae4fa5f | 2015-06-18 11:45:39 -0700 | [diff] [blame] | 156 | if ((pci_is_root_bus(bus) && devfn != 0) || |
Rob Herring | 350f8be | 2015-01-09 20:34:49 -0600 | [diff] [blame] | 157 | xgene_pcie_hide_rc_bars(bus, offset)) |
| 158 | return NULL; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 159 | |
| 160 | xgene_pcie_set_rtdid_reg(bus, devfn); |
Feng Kan | 085a68d | 2015-02-17 15:14:00 -0800 | [diff] [blame] | 161 | return xgene_pcie_get_cfg_base(bus) + offset; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 162 | } |
| 163 | |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 164 | static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn, |
| 165 | int where, int size, u32 *val) |
| 166 | { |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 167 | struct xgene_pcie *port = pcie_bus_to_port(bus); |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 168 | |
| 169 | if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) != |
| 170 | PCIBIOS_SUCCESSFUL) |
| 171 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 172 | |
| 173 | /* |
Naveen Naidu | c78b9a9 | 2021-11-18 19:33:35 +0530 | [diff] [blame] | 174 | * The v1 controller has a bug in its Configuration Request Retry |
| 175 | * Status (CRS) logic: when CRS Software Visibility is enabled and |
| 176 | * we read the Vendor and Device ID of a non-existent device, the |
| 177 | * controller fabricates return data of 0xFFFF0001 ("device exists |
| 178 | * but is not ready") instead of 0xFFFFFFFF (PCI_ERROR_RESPONSE) |
Bjorn Helgaas | cc4a08c | 2021-01-26 15:35:03 -0600 | [diff] [blame] | 179 | * ("device does not exist"). This causes the PCI core to retry |
| 180 | * the read until it times out. Avoid this by not claiming to |
| 181 | * support CRS SV. |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 182 | */ |
| 183 | if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) && |
Bjorn Helgaas | 582ffae | 2017-09-05 12:58:03 -0500 | [diff] [blame] | 184 | ((where & ~0x3) == XGENE_V1_PCI_EXP_CAP + PCI_EXP_RTCTL)) |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 185 | *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16); |
| 186 | |
| 187 | if (size <= 2) |
| 188 | *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 189 | |
| 190 | return PCIBIOS_SUCCESSFUL; |
| 191 | } |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 192 | #endif |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 193 | |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 194 | #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS) |
| 195 | static int xgene_get_csr_resource(struct acpi_device *adev, |
| 196 | struct resource *res) |
| 197 | { |
| 198 | struct device *dev = &adev->dev; |
| 199 | struct resource_entry *entry; |
| 200 | struct list_head list; |
| 201 | unsigned long flags; |
| 202 | int ret; |
| 203 | |
| 204 | INIT_LIST_HEAD(&list); |
| 205 | flags = IORESOURCE_MEM; |
| 206 | ret = acpi_dev_get_resources(adev, &list, |
| 207 | acpi_dev_filter_resource_type_cb, |
| 208 | (void *) flags); |
| 209 | if (ret < 0) { |
| 210 | dev_err(dev, "failed to parse _CRS method, error code %d\n", |
| 211 | ret); |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | if (ret == 0) { |
| 216 | dev_err(dev, "no IO and memory resources present in _CRS\n"); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | entry = list_first_entry(&list, struct resource_entry, node); |
| 221 | *res = *entry->res; |
| 222 | acpi_dev_free_resource_list(&list); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int xgene_pcie_ecam_init(struct pci_config_window *cfg, u32 ipversion) |
| 227 | { |
| 228 | struct device *dev = cfg->parent; |
| 229 | struct acpi_device *adev = to_acpi_device(dev); |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 230 | struct xgene_pcie *port; |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 231 | struct resource csr; |
| 232 | int ret; |
| 233 | |
| 234 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); |
| 235 | if (!port) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | ret = xgene_get_csr_resource(adev, &csr); |
| 239 | if (ret) { |
| 240 | dev_err(dev, "can't get CSR resource\n"); |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 241 | return ret; |
| 242 | } |
Lorenzo Pieralisi | 26b758f | 2017-04-19 17:49:05 +0100 | [diff] [blame] | 243 | port->csr_base = devm_pci_remap_cfg_resource(dev, &csr); |
Dan Carpenter | 1ded56d | 2017-01-21 07:49:49 +0300 | [diff] [blame] | 244 | if (IS_ERR(port->csr_base)) |
| 245 | return PTR_ERR(port->csr_base); |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 246 | |
| 247 | port->cfg_base = cfg->win; |
| 248 | port->version = ipversion; |
| 249 | |
| 250 | cfg->priv = port; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int xgene_v1_pcie_ecam_init(struct pci_config_window *cfg) |
| 255 | { |
| 256 | return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_1); |
| 257 | } |
| 258 | |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 259 | const struct pci_ecam_ops xgene_v1_pcie_ecam_ops = { |
Bjorn Helgaas | fca4848 | 2017-09-05 13:09:05 -0500 | [diff] [blame] | 260 | .init = xgene_v1_pcie_ecam_init, |
| 261 | .pci_ops = { |
| 262 | .map_bus = xgene_pcie_map_bus, |
| 263 | .read = xgene_pcie_config_read32, |
| 264 | .write = pci_generic_config_write, |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 265 | } |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 266 | }; |
| 267 | |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 268 | static int xgene_v2_pcie_ecam_init(struct pci_config_window *cfg) |
| 269 | { |
| 270 | return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_2); |
| 271 | } |
| 272 | |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 273 | const struct pci_ecam_ops xgene_v2_pcie_ecam_ops = { |
Bjorn Helgaas | fca4848 | 2017-09-05 13:09:05 -0500 | [diff] [blame] | 274 | .init = xgene_v2_pcie_ecam_init, |
| 275 | .pci_ops = { |
| 276 | .map_bus = xgene_pcie_map_bus, |
| 277 | .read = xgene_pcie_config_read32, |
| 278 | .write = pci_generic_config_write, |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 279 | } |
| 280 | }; |
| 281 | #endif |
| 282 | |
| 283 | #if defined(CONFIG_PCI_XGENE) |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 284 | static u64 xgene_pcie_set_ib_mask(struct xgene_pcie *port, u32 addr, |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 285 | u32 flags, u64 size) |
| 286 | { |
| 287 | u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags; |
| 288 | u32 val32 = 0; |
| 289 | u32 val; |
| 290 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 291 | val32 = xgene_pcie_readl(port, addr); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 292 | val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16); |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 293 | xgene_pcie_writel(port, addr, val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 294 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 295 | val32 = xgene_pcie_readl(port, addr + 0x04); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 296 | val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16); |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 297 | xgene_pcie_writel(port, addr + 0x04, val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 298 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 299 | val32 = xgene_pcie_readl(port, addr + 0x04); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 300 | val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16); |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 301 | xgene_pcie_writel(port, addr + 0x04, val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 302 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 303 | val32 = xgene_pcie_readl(port, addr + 0x08); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 304 | val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16); |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 305 | xgene_pcie_writel(port, addr + 0x08, val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 306 | |
| 307 | return mask; |
| 308 | } |
| 309 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 310 | static void xgene_pcie_linkup(struct xgene_pcie *port, |
Bjorn Helgaas | fca4848 | 2017-09-05 13:09:05 -0500 | [diff] [blame] | 311 | u32 *lanes, u32 *speed) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 312 | { |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 313 | u32 val32; |
| 314 | |
| 315 | port->link_up = false; |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 316 | val32 = xgene_pcie_readl(port, PCIECORE_CTLANDSTATUS); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 317 | if (val32 & LINK_UP_MASK) { |
| 318 | port->link_up = true; |
| 319 | *speed = PIPE_PHY_RATE_RD(val32); |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 320 | val32 = xgene_pcie_readl(port, BRIDGE_STATUS_0); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 321 | *lanes = val32 >> 26; |
| 322 | } |
| 323 | } |
| 324 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 325 | static int xgene_pcie_init_port(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 326 | { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 327 | struct device *dev = port->dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 328 | int rc; |
| 329 | |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 330 | port->clk = clk_get(dev, NULL); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 331 | if (IS_ERR(port->clk)) { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 332 | dev_err(dev, "clock not available\n"); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 333 | return -ENODEV; |
| 334 | } |
| 335 | |
| 336 | rc = clk_prepare_enable(port->clk); |
| 337 | if (rc) { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 338 | dev_err(dev, "clock enable failed\n"); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 345 | static int xgene_pcie_map_reg(struct xgene_pcie *port, |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 346 | struct platform_device *pdev) |
| 347 | { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 348 | struct device *dev = port->dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 349 | struct resource *res; |
| 350 | |
| 351 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr"); |
Lorenzo Pieralisi | 26b758f | 2017-04-19 17:49:05 +0100 | [diff] [blame] | 352 | port->csr_base = devm_pci_remap_cfg_resource(dev, res); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 353 | if (IS_ERR(port->csr_base)) |
| 354 | return PTR_ERR(port->csr_base); |
| 355 | |
Dejin Zheng | d4707d7 | 2021-03-28 22:41:18 +0800 | [diff] [blame] | 356 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); |
| 357 | port->cfg_base = devm_ioremap_resource(dev, res); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 358 | if (IS_ERR(port->cfg_base)) |
| 359 | return PTR_ERR(port->cfg_base); |
| 360 | port->cfg_addr = res->start; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 365 | static void xgene_pcie_setup_ob_reg(struct xgene_pcie *port, |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 366 | struct resource *res, u32 offset, |
| 367 | u64 cpu_addr, u64 pci_addr) |
| 368 | { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 369 | struct device *dev = port->dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 370 | resource_size_t size = resource_size(res); |
| 371 | u64 restype = resource_type(res); |
| 372 | u64 mask = 0; |
| 373 | u32 min_size; |
| 374 | u32 flag = EN_REG; |
| 375 | |
| 376 | if (restype == IORESOURCE_MEM) { |
| 377 | min_size = SZ_128M; |
| 378 | } else { |
| 379 | min_size = 128; |
| 380 | flag |= OB_LO_IO; |
| 381 | } |
| 382 | |
| 383 | if (size >= min_size) |
| 384 | mask = ~(size - 1) | flag; |
| 385 | else |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 386 | dev_warn(dev, "res size 0x%llx less than minimum 0x%x\n", |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 387 | (u64)size, min_size); |
| 388 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 389 | xgene_pcie_writel(port, offset, lower_32_bits(cpu_addr)); |
| 390 | xgene_pcie_writel(port, offset + 0x04, upper_32_bits(cpu_addr)); |
| 391 | xgene_pcie_writel(port, offset + 0x08, lower_32_bits(mask)); |
| 392 | xgene_pcie_writel(port, offset + 0x0c, upper_32_bits(mask)); |
| 393 | xgene_pcie_writel(port, offset + 0x10, lower_32_bits(pci_addr)); |
| 394 | xgene_pcie_writel(port, offset + 0x14, upper_32_bits(pci_addr)); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 395 | } |
| 396 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 397 | static void xgene_pcie_setup_cfg_reg(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 398 | { |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 399 | u64 addr = port->cfg_addr; |
| 400 | |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 401 | xgene_pcie_writel(port, CFGBARL, lower_32_bits(addr)); |
| 402 | xgene_pcie_writel(port, CFGBARH, upper_32_bits(addr)); |
| 403 | xgene_pcie_writel(port, CFGCTL, EN_REG); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 404 | } |
| 405 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 406 | static int xgene_pcie_map_ranges(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 407 | { |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 408 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port); |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 409 | struct resource_entry *window; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 410 | struct device *dev = port->dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 411 | |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 412 | resource_list_for_each_entry(window, &bridge->windows) { |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 413 | struct resource *res = window->res; |
| 414 | u64 restype = resource_type(res); |
| 415 | |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 416 | dev_dbg(dev, "%pR\n", res); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 417 | |
| 418 | switch (restype) { |
| 419 | case IORESOURCE_IO: |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 420 | xgene_pcie_setup_ob_reg(port, res, OMR3BARL, |
| 421 | pci_pio_to_address(res->start), |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 422 | res->start - window->offset); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 423 | break; |
| 424 | case IORESOURCE_MEM: |
Duc Dang | 8ef54f2 | 2015-07-09 14:20:12 -0700 | [diff] [blame] | 425 | if (res->flags & IORESOURCE_PREFETCH) |
| 426 | xgene_pcie_setup_ob_reg(port, res, OMR2BARL, |
| 427 | res->start, |
| 428 | res->start - |
| 429 | window->offset); |
| 430 | else |
| 431 | xgene_pcie_setup_ob_reg(port, res, OMR1BARL, |
| 432 | res->start, |
| 433 | res->start - |
| 434 | window->offset); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 435 | break; |
| 436 | case IORESOURCE_BUS: |
| 437 | break; |
| 438 | default: |
| 439 | dev_err(dev, "invalid resource %pR\n", res); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | } |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 443 | xgene_pcie_setup_cfg_reg(port); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 447 | static void xgene_pcie_setup_pims(struct xgene_pcie *port, u32 pim_reg, |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 448 | u64 pim, u64 size) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 449 | { |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 450 | xgene_pcie_writel(port, pim_reg, lower_32_bits(pim)); |
| 451 | xgene_pcie_writel(port, pim_reg + 0x04, |
| 452 | upper_32_bits(pim) | EN_COHERENCY); |
| 453 | xgene_pcie_writel(port, pim_reg + 0x10, lower_32_bits(size)); |
| 454 | xgene_pcie_writel(port, pim_reg + 0x14, upper_32_bits(size)); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | * X-Gene PCIe support maximum 3 inbound memory regions |
| 459 | * This function helps to select a region based on size of region |
| 460 | */ |
| 461 | static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size) |
| 462 | { |
| 463 | if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) { |
| 464 | *ib_reg_mask |= (1 << 1); |
| 465 | return 1; |
| 466 | } |
| 467 | |
Rob Herring | c7a75d0 | 2021-11-29 11:36:37 -0600 | [diff] [blame] | 468 | if ((size > SZ_1K) && (size < SZ_4G) && !(*ib_reg_mask & (1 << 0))) { |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 469 | *ib_reg_mask |= (1 << 0); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) { |
| 474 | *ib_reg_mask |= (1 << 2); |
| 475 | return 2; |
| 476 | } |
| 477 | |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 481 | static void xgene_pcie_setup_ib_reg(struct xgene_pcie *port, |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 482 | struct resource_entry *entry, |
| 483 | u8 *ib_reg_mask) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 484 | { |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 485 | void __iomem *cfg_base = port->cfg_base; |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 486 | struct device *dev = port->dev; |
Bjorn Helgaas | 662e4b03 | 2021-05-17 12:18:39 -0500 | [diff] [blame] | 487 | void __iomem *bar_addr; |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 488 | u32 pim_reg; |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 489 | u64 cpu_addr = entry->res->start; |
| 490 | u64 pci_addr = cpu_addr - entry->offset; |
| 491 | u64 size = resource_size(entry->res); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 492 | u64 mask = ~(size - 1) | EN_REG; |
| 493 | u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64; |
| 494 | u32 bar_low; |
| 495 | int region; |
| 496 | |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 497 | region = xgene_pcie_select_ib_reg(ib_reg_mask, size); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 498 | if (region < 0) { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 499 | dev_warn(dev, "invalid pcie dma-range config\n"); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 503 | if (entry->res->flags & IORESOURCE_PREFETCH) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 504 | flags |= PCI_BASE_ADDRESS_MEM_PREFETCH; |
| 505 | |
| 506 | bar_low = pcie_bar_low_val((u32)cpu_addr, flags); |
| 507 | switch (region) { |
| 508 | case 0: |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 509 | xgene_pcie_set_ib_mask(port, BRIDGE_CFG_4, flags, size); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 510 | bar_addr = cfg_base + PCI_BASE_ADDRESS_0; |
| 511 | writel(bar_low, bar_addr); |
| 512 | writel(upper_32_bits(cpu_addr), bar_addr + 0x4); |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 513 | pim_reg = PIM1_1L; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 514 | break; |
| 515 | case 1: |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 516 | xgene_pcie_writel(port, IBAR2, bar_low); |
| 517 | xgene_pcie_writel(port, IR2MSK, lower_32_bits(mask)); |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 518 | pim_reg = PIM2_1L; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 519 | break; |
| 520 | case 2: |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 521 | xgene_pcie_writel(port, IBAR3L, bar_low); |
| 522 | xgene_pcie_writel(port, IBAR3L + 0x4, upper_32_bits(cpu_addr)); |
| 523 | xgene_pcie_writel(port, IR3MSKL, lower_32_bits(mask)); |
| 524 | xgene_pcie_writel(port, IR3MSKL + 0x4, upper_32_bits(mask)); |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 525 | pim_reg = PIM3_1L; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | |
Bjorn Helgaas | 4ecf6b0 | 2016-10-06 13:43:41 -0500 | [diff] [blame] | 529 | xgene_pcie_setup_pims(port, pim_reg, pci_addr, ~(size - 1)); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 530 | } |
| 531 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 532 | static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 533 | { |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 534 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port); |
| 535 | struct resource_entry *entry; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 536 | u8 ib_reg_mask = 0; |
| 537 | |
Rob Herring | 6dce5aa5 | 2019-10-28 11:32:53 -0500 | [diff] [blame] | 538 | resource_list_for_each_entry(entry, &bridge->dma_ranges) |
| 539 | xgene_pcie_setup_ib_reg(port, entry, &ib_reg_mask); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 540 | |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /* clear BAR configuration which was done by firmware */ |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 545 | static void xgene_pcie_clear_config(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 546 | { |
| 547 | int i; |
| 548 | |
| 549 | for (i = PIM1_1L; i <= CFGCTL; i += 4) |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 550 | xgene_pcie_writel(port, i, 0); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 551 | } |
| 552 | |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 553 | static int xgene_pcie_setup(struct xgene_pcie *port) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 554 | { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 555 | struct device *dev = port->dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 556 | u32 val, lanes = 0, speed = 0; |
| 557 | int ret; |
| 558 | |
| 559 | xgene_pcie_clear_config(port); |
| 560 | |
| 561 | /* setup the vendor and device IDs correctly */ |
Pali Rohár | 894682f | 2021-09-27 15:43:56 +0200 | [diff] [blame] | 562 | val = (XGENE_PCIE_DEVICEID << 16) | PCI_VENDOR_ID_AMCC; |
Bjorn Helgaas | 8e93c51 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 563 | xgene_pcie_writel(port, BRIDGE_CFG_0, val); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 564 | |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 565 | ret = xgene_pcie_map_ranges(port); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 566 | if (ret) |
| 567 | return ret; |
| 568 | |
| 569 | ret = xgene_pcie_parse_map_dma_ranges(port); |
| 570 | if (ret) |
| 571 | return ret; |
| 572 | |
| 573 | xgene_pcie_linkup(port, &lanes, &speed); |
| 574 | if (!port->link_up) |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 575 | dev_info(dev, "(rc) link down\n"); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 576 | else |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 577 | dev_info(dev, "(rc) x%d gen-%d link up\n", lanes, speed + 1); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 581 | static struct pci_ops xgene_pcie_ops = { |
| 582 | .map_bus = xgene_pcie_map_bus, |
| 583 | .read = xgene_pcie_config_read32, |
| 584 | .write = pci_generic_config_write32, |
| 585 | }; |
| 586 | |
Bjorn Helgaas | 92e3145 | 2017-11-09 18:12:01 -0600 | [diff] [blame] | 587 | static int xgene_pcie_probe(struct platform_device *pdev) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 588 | { |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 589 | struct device *dev = &pdev->dev; |
| 590 | struct device_node *dn = dev->of_node; |
Fan Fei | 24d174a1 | 2021-12-22 19:10:52 -0600 | [diff] [blame] | 591 | struct xgene_pcie *port; |
Lorenzo Pieralisi | 9af275b | 2017-06-28 15:13:59 -0500 | [diff] [blame] | 592 | struct pci_host_bridge *bridge; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 593 | int ret; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 594 | |
Lorenzo Pieralisi | 9af275b | 2017-06-28 15:13:59 -0500 | [diff] [blame] | 595 | bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port)); |
| 596 | if (!bridge) |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 597 | return -ENOMEM; |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 598 | |
Lorenzo Pieralisi | 9af275b | 2017-06-28 15:13:59 -0500 | [diff] [blame] | 599 | port = pci_host_bridge_priv(bridge); |
| 600 | |
Bjorn Helgaas | d963ab2 | 2016-10-06 13:43:42 -0500 | [diff] [blame] | 601 | port->node = of_node_get(dn); |
| 602 | port->dev = dev; |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 603 | |
Duc Dang | f09f873 | 2015-06-12 17:35:57 -0700 | [diff] [blame] | 604 | port->version = XGENE_PCIE_IP_VER_UNKN; |
| 605 | if (of_device_is_compatible(port->node, "apm,xgene-pcie")) |
| 606 | port->version = XGENE_PCIE_IP_VER_1; |
| 607 | |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 608 | ret = xgene_pcie_map_reg(port, pdev); |
| 609 | if (ret) |
| 610 | return ret; |
| 611 | |
| 612 | ret = xgene_pcie_init_port(port); |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 616 | ret = xgene_pcie_setup(port); |
Bjorn Helgaas | 0ccb7eef | 2016-05-28 18:14:24 -0500 | [diff] [blame] | 617 | if (ret) |
Rob Herring | 83083e2 | 2019-10-28 11:32:44 -0500 | [diff] [blame] | 618 | return ret; |
Bjorn Helgaas | 0ccb7eef | 2016-05-28 18:14:24 -0500 | [diff] [blame] | 619 | |
Lorenzo Pieralisi | 9af275b | 2017-06-28 15:13:59 -0500 | [diff] [blame] | 620 | bridge->sysdata = port; |
Lorenzo Pieralisi | 9af275b | 2017-06-28 15:13:59 -0500 | [diff] [blame] | 621 | bridge->ops = &xgene_pcie_ops; |
| 622 | |
Rob Herring | 97c5372 | 2020-05-22 17:48:26 -0600 | [diff] [blame] | 623 | return pci_host_probe(bridge); |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static const struct of_device_id xgene_pcie_match_table[] = { |
| 627 | {.compatible = "apm,xgene-pcie",}, |
| 628 | {}, |
| 629 | }; |
| 630 | |
| 631 | static struct platform_driver xgene_pcie_driver = { |
| 632 | .driver = { |
Bjorn Helgaas | fca4848 | 2017-09-05 13:09:05 -0500 | [diff] [blame] | 633 | .name = "xgene-pcie", |
| 634 | .of_match_table = of_match_ptr(xgene_pcie_match_table), |
| 635 | .suppress_bind_attrs = true, |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 636 | }, |
Bjorn Helgaas | 92e3145 | 2017-11-09 18:12:01 -0600 | [diff] [blame] | 637 | .probe = xgene_pcie_probe, |
Tanmay Inamdar | 5f6b6cc | 2014-10-01 13:01:35 -0600 | [diff] [blame] | 638 | }; |
Paul Gortmaker | 50dcd29 | 2016-07-02 19:13:34 -0400 | [diff] [blame] | 639 | builtin_platform_driver(xgene_pcie_driver); |
Duc Dang | c5d4603 | 2016-12-01 18:27:07 -0800 | [diff] [blame] | 640 | #endif |