1 // SPDX-License-Identifier: GPL-2.0+
3 * APM X-Gene PCIe Driver
5 * Copyright (c) 2014 Applied Micro Circuits Corporation.
10 #include <linux/delay.h>
12 #include <linux/jiffies.h>
13 #include <linux/memblock.h>
14 #include <linux/init.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_pci.h>
19 #include <linux/pci.h>
20 #include <linux/pci-acpi.h>
21 #include <linux/pci-ecam.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
27 #define PCIECORE_CTLANDSTATUS 0x50
35 #define OMR1BARL 0x100
36 #define OMR2BARL 0x118
37 #define OMR3BARL 0x130
42 #define BRIDGE_CFG_0 0x2000
43 #define BRIDGE_CFG_4 0x2010
44 #define BRIDGE_STATUS_0 0x2600
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
51 #define XGENE_PCIE_VENDORID 0x10E8
52 #define XGENE_PCIE_DEVICEID 0xE004
53 #define SZ_1T (SZ_1G*1024ULL)
54 #define PIPE_PHY_RATE_RD(src) ((0xc000 & (u32)(src)) >> 0xe)
56 #define XGENE_V1_PCI_EXP_CAP 0x40
59 #define XGENE_PCIE_IP_VER_UNKN 0
60 #define XGENE_PCIE_IP_VER_1 1
61 #define XGENE_PCIE_IP_VER_2 2
63 #if defined(CONFIG_PCI_XGENE) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
64 struct xgene_pcie_port {
65 struct device_node *node;
68 void __iomem *csr_base;
69 void __iomem *cfg_base;
70 unsigned long cfg_addr;
75 static u32 xgene_pcie_readl(struct xgene_pcie_port *port, u32 reg)
77 return readl(port->csr_base + reg);
80 static void xgene_pcie_writel(struct xgene_pcie_port *port, u32 reg, u32 val)
82 writel(val, port->csr_base + reg);
85 static inline u32 pcie_bar_low_val(u32 addr, u32 flags)
87 return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags;
90 static inline struct xgene_pcie_port *pcie_bus_to_port(struct pci_bus *bus)
92 struct pci_config_window *cfg;
95 return (struct xgene_pcie_port *)(bus->sysdata);
98 return (struct xgene_pcie_port *)(cfg->priv);
102 * When the address bit [17:16] is 2'b01, the Configuration access will be
103 * treated as Type 1 and it will be forwarded to external PCIe device.
105 static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus)
107 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
109 if (bus->number >= (bus->primary + 1))
110 return port->cfg_base + AXI_EP_CFG_ACCESS;
112 return port->cfg_base;
116 * For Configuration request, RTDID register is used as Bus Number,
117 * Device Number and Function number of the header fields.
119 static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
121 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
122 unsigned int b, d, f;
129 if (!pci_is_root_bus(bus))
130 rtdid_val = (b << 8) | (d << 3) | f;
132 xgene_pcie_writel(port, RTDID, rtdid_val);
133 /* read the register back to ensure flush */
134 xgene_pcie_readl(port, RTDID);
138 * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
139 * the translation from PCI bus to native BUS. Entire DDR region
140 * is mapped into PCIe space using these registers, so it can be
141 * reached by DMA from EP devices. The BAR0/1 of bridge should be
142 * hidden during enumeration to avoid the sizing and resource allocation
145 static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
147 if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
148 (offset == PCI_BASE_ADDRESS_1)))
154 static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
157 if ((pci_is_root_bus(bus) && devfn != 0) ||
158 xgene_pcie_hide_rc_bars(bus, offset))
161 xgene_pcie_set_rtdid_reg(bus, devfn);
162 return xgene_pcie_get_cfg_base(bus) + offset;
165 static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
166 int where, int size, u32 *val)
168 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
170 if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
172 return PCIBIOS_DEVICE_NOT_FOUND;
175 * The v1 controller has a bug in its Configuration Request
176 * Retry Status (CRS) logic: when CRS is enabled and we read the
177 * Vendor and Device ID of a non-existent device, the controller
178 * fabricates return data of 0xFFFF0001 ("device exists but is not
179 * ready") instead of 0xFFFFFFFF ("device does not exist"). This
180 * causes the PCI core to retry the read until it times out.
181 * Avoid this by not claiming to support CRS.
183 if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
184 ((where & ~0x3) == XGENE_V1_PCI_EXP_CAP + PCI_EXP_RTCTL))
185 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
188 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
190 return PCIBIOS_SUCCESSFUL;
194 #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
195 static int xgene_get_csr_resource(struct acpi_device *adev,
196 struct resource *res)
198 struct device *dev = &adev->dev;
199 struct resource_entry *entry;
200 struct list_head list;
204 INIT_LIST_HEAD(&list);
205 flags = IORESOURCE_MEM;
206 ret = acpi_dev_get_resources(adev, &list,
207 acpi_dev_filter_resource_type_cb,
210 dev_err(dev, "failed to parse _CRS method, error code %d\n",
216 dev_err(dev, "no IO and memory resources present in _CRS\n");
220 entry = list_first_entry(&list, struct resource_entry, node);
222 acpi_dev_free_resource_list(&list);
226 static int xgene_pcie_ecam_init(struct pci_config_window *cfg, u32 ipversion)
228 struct device *dev = cfg->parent;
229 struct acpi_device *adev = to_acpi_device(dev);
230 struct xgene_pcie_port *port;
234 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
238 ret = xgene_get_csr_resource(adev, &csr);
240 dev_err(dev, "can't get CSR resource\n");
243 port->csr_base = devm_pci_remap_cfg_resource(dev, &csr);
244 if (IS_ERR(port->csr_base))
245 return PTR_ERR(port->csr_base);
247 port->cfg_base = cfg->win;
248 port->version = ipversion;
254 static int xgene_v1_pcie_ecam_init(struct pci_config_window *cfg)
256 return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_1);
259 struct pci_ecam_ops xgene_v1_pcie_ecam_ops = {
261 .init = xgene_v1_pcie_ecam_init,
263 .map_bus = xgene_pcie_map_bus,
264 .read = xgene_pcie_config_read32,
265 .write = pci_generic_config_write,
269 static int xgene_v2_pcie_ecam_init(struct pci_config_window *cfg)
271 return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_2);
274 struct pci_ecam_ops xgene_v2_pcie_ecam_ops = {
276 .init = xgene_v2_pcie_ecam_init,
278 .map_bus = xgene_pcie_map_bus,
279 .read = xgene_pcie_config_read32,
280 .write = pci_generic_config_write,
285 #if defined(CONFIG_PCI_XGENE)
286 static u64 xgene_pcie_set_ib_mask(struct xgene_pcie_port *port, u32 addr,
289 u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags;
293 val32 = xgene_pcie_readl(port, addr);
294 val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16);
295 xgene_pcie_writel(port, addr, val);
297 val32 = xgene_pcie_readl(port, addr + 0x04);
298 val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16);
299 xgene_pcie_writel(port, addr + 0x04, val);
301 val32 = xgene_pcie_readl(port, addr + 0x04);
302 val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16);
303 xgene_pcie_writel(port, addr + 0x04, val);
305 val32 = xgene_pcie_readl(port, addr + 0x08);
306 val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16);
307 xgene_pcie_writel(port, addr + 0x08, val);
312 static void xgene_pcie_linkup(struct xgene_pcie_port *port,
313 u32 *lanes, u32 *speed)
317 port->link_up = false;
318 val32 = xgene_pcie_readl(port, PCIECORE_CTLANDSTATUS);
319 if (val32 & LINK_UP_MASK) {
320 port->link_up = true;
321 *speed = PIPE_PHY_RATE_RD(val32);
322 val32 = xgene_pcie_readl(port, BRIDGE_STATUS_0);
323 *lanes = val32 >> 26;
327 static int xgene_pcie_init_port(struct xgene_pcie_port *port)
329 struct device *dev = port->dev;
332 port->clk = clk_get(dev, NULL);
333 if (IS_ERR(port->clk)) {
334 dev_err(dev, "clock not available\n");
338 rc = clk_prepare_enable(port->clk);
340 dev_err(dev, "clock enable failed\n");
347 static int xgene_pcie_map_reg(struct xgene_pcie_port *port,
348 struct platform_device *pdev)
350 struct device *dev = port->dev;
351 struct resource *res;
353 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
354 port->csr_base = devm_pci_remap_cfg_resource(dev, res);
355 if (IS_ERR(port->csr_base))
356 return PTR_ERR(port->csr_base);
358 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
359 port->cfg_base = devm_ioremap_resource(dev, res);
360 if (IS_ERR(port->cfg_base))
361 return PTR_ERR(port->cfg_base);
362 port->cfg_addr = res->start;
367 static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port *port,
368 struct resource *res, u32 offset,
369 u64 cpu_addr, u64 pci_addr)
371 struct device *dev = port->dev;
372 resource_size_t size = resource_size(res);
373 u64 restype = resource_type(res);
378 if (restype == IORESOURCE_MEM) {
385 if (size >= min_size)
386 mask = ~(size - 1) | flag;
388 dev_warn(dev, "res size 0x%llx less than minimum 0x%x\n",
389 (u64)size, min_size);
391 xgene_pcie_writel(port, offset, lower_32_bits(cpu_addr));
392 xgene_pcie_writel(port, offset + 0x04, upper_32_bits(cpu_addr));
393 xgene_pcie_writel(port, offset + 0x08, lower_32_bits(mask));
394 xgene_pcie_writel(port, offset + 0x0c, upper_32_bits(mask));
395 xgene_pcie_writel(port, offset + 0x10, lower_32_bits(pci_addr));
396 xgene_pcie_writel(port, offset + 0x14, upper_32_bits(pci_addr));
399 static void xgene_pcie_setup_cfg_reg(struct xgene_pcie_port *port)
401 u64 addr = port->cfg_addr;
403 xgene_pcie_writel(port, CFGBARL, lower_32_bits(addr));
404 xgene_pcie_writel(port, CFGBARH, upper_32_bits(addr));
405 xgene_pcie_writel(port, CFGCTL, EN_REG);
408 static int xgene_pcie_map_ranges(struct xgene_pcie_port *port,
409 struct list_head *res,
410 resource_size_t io_base)
412 struct resource_entry *window;
413 struct device *dev = port->dev;
416 resource_list_for_each_entry(window, res) {
417 struct resource *res = window->res;
418 u64 restype = resource_type(res);
420 dev_dbg(dev, "%pR\n", res);
424 xgene_pcie_setup_ob_reg(port, res, OMR3BARL, io_base,
425 res->start - window->offset);
426 ret = devm_pci_remap_iospace(dev, res, io_base);
431 if (res->flags & IORESOURCE_PREFETCH)
432 xgene_pcie_setup_ob_reg(port, res, OMR2BARL,
437 xgene_pcie_setup_ob_reg(port, res, OMR1BARL,
445 dev_err(dev, "invalid resource %pR\n", res);
449 xgene_pcie_setup_cfg_reg(port);
453 static void xgene_pcie_setup_pims(struct xgene_pcie_port *port, u32 pim_reg,
456 xgene_pcie_writel(port, pim_reg, lower_32_bits(pim));
457 xgene_pcie_writel(port, pim_reg + 0x04,
458 upper_32_bits(pim) | EN_COHERENCY);
459 xgene_pcie_writel(port, pim_reg + 0x10, lower_32_bits(size));
460 xgene_pcie_writel(port, pim_reg + 0x14, upper_32_bits(size));
464 * X-Gene PCIe support maximum 3 inbound memory regions
465 * This function helps to select a region based on size of region
467 static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size)
469 if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) {
470 *ib_reg_mask |= (1 << 1);
474 if ((size > SZ_1K) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 0))) {
475 *ib_reg_mask |= (1 << 0);
479 if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) {
480 *ib_reg_mask |= (1 << 2);
487 static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port *port,
488 struct of_pci_range *range, u8 *ib_reg_mask)
490 void __iomem *cfg_base = port->cfg_base;
491 struct device *dev = port->dev;
494 u64 cpu_addr = range->cpu_addr;
495 u64 pci_addr = range->pci_addr;
496 u64 size = range->size;
497 u64 mask = ~(size - 1) | EN_REG;
498 u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64;
502 region = xgene_pcie_select_ib_reg(ib_reg_mask, range->size);
504 dev_warn(dev, "invalid pcie dma-range config\n");
508 if (range->flags & IORESOURCE_PREFETCH)
509 flags |= PCI_BASE_ADDRESS_MEM_PREFETCH;
511 bar_low = pcie_bar_low_val((u32)cpu_addr, flags);
514 xgene_pcie_set_ib_mask(port, BRIDGE_CFG_4, flags, size);
515 bar_addr = cfg_base + PCI_BASE_ADDRESS_0;
516 writel(bar_low, bar_addr);
517 writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
521 xgene_pcie_writel(port, IBAR2, bar_low);
522 xgene_pcie_writel(port, IR2MSK, lower_32_bits(mask));
526 xgene_pcie_writel(port, IBAR3L, bar_low);
527 xgene_pcie_writel(port, IBAR3L + 0x4, upper_32_bits(cpu_addr));
528 xgene_pcie_writel(port, IR3MSKL, lower_32_bits(mask));
529 xgene_pcie_writel(port, IR3MSKL + 0x4, upper_32_bits(mask));
534 xgene_pcie_setup_pims(port, pim_reg, pci_addr, ~(size - 1));
537 static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port *port)
539 struct device_node *np = port->node;
540 struct of_pci_range range;
541 struct of_pci_range_parser parser;
542 struct device *dev = port->dev;
545 if (of_pci_dma_range_parser_init(&parser, np)) {
546 dev_err(dev, "missing dma-ranges property\n");
550 /* Get the dma-ranges from DT */
551 for_each_of_pci_range(&parser, &range) {
552 u64 end = range.cpu_addr + range.size - 1;
554 dev_dbg(dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
555 range.flags, range.cpu_addr, end, range.pci_addr);
556 xgene_pcie_setup_ib_reg(port, &range, &ib_reg_mask);
561 /* clear BAR configuration which was done by firmware */
562 static void xgene_pcie_clear_config(struct xgene_pcie_port *port)
566 for (i = PIM1_1L; i <= CFGCTL; i += 4)
567 xgene_pcie_writel(port, i, 0);
570 static int xgene_pcie_setup(struct xgene_pcie_port *port, struct list_head *res,
571 resource_size_t io_base)
573 struct device *dev = port->dev;
574 u32 val, lanes = 0, speed = 0;
577 xgene_pcie_clear_config(port);
579 /* setup the vendor and device IDs correctly */
580 val = (XGENE_PCIE_DEVICEID << 16) | XGENE_PCIE_VENDORID;
581 xgene_pcie_writel(port, BRIDGE_CFG_0, val);
583 ret = xgene_pcie_map_ranges(port, res, io_base);
587 ret = xgene_pcie_parse_map_dma_ranges(port);
591 xgene_pcie_linkup(port, &lanes, &speed);
593 dev_info(dev, "(rc) link down\n");
595 dev_info(dev, "(rc) x%d gen-%d link up\n", lanes, speed + 1);
599 static struct pci_ops xgene_pcie_ops = {
600 .map_bus = xgene_pcie_map_bus,
601 .read = xgene_pcie_config_read32,
602 .write = pci_generic_config_write32,
605 static int xgene_pcie_probe(struct platform_device *pdev)
607 struct device *dev = &pdev->dev;
608 struct device_node *dn = dev->of_node;
609 struct xgene_pcie_port *port;
610 resource_size_t iobase = 0;
611 struct pci_bus *bus, *child;
612 struct pci_host_bridge *bridge;
616 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
620 port = pci_host_bridge_priv(bridge);
622 port->node = of_node_get(dn);
625 port->version = XGENE_PCIE_IP_VER_UNKN;
626 if (of_device_is_compatible(port->node, "apm,xgene-pcie"))
627 port->version = XGENE_PCIE_IP_VER_1;
629 ret = xgene_pcie_map_reg(port, pdev);
633 ret = xgene_pcie_init_port(port);
637 ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &res,
642 ret = devm_request_pci_bus_resources(dev, &res);
646 ret = xgene_pcie_setup(port, &res, iobase);
650 list_splice_init(&res, &bridge->windows);
651 bridge->dev.parent = dev;
652 bridge->sysdata = port;
654 bridge->ops = &xgene_pcie_ops;
655 bridge->map_irq = of_irq_parse_and_map_pci;
656 bridge->swizzle_irq = pci_common_swizzle;
658 ret = pci_scan_root_bus_bridge(bridge);
664 pci_assign_unassigned_bus_resources(bus);
665 list_for_each_entry(child, &bus->children, node)
666 pcie_bus_configure_settings(child);
667 pci_bus_add_devices(bus);
671 pci_free_resource_list(&res);
675 static const struct of_device_id xgene_pcie_match_table[] = {
676 {.compatible = "apm,xgene-pcie",},
680 static struct platform_driver xgene_pcie_driver = {
682 .name = "xgene-pcie",
683 .of_match_table = of_match_ptr(xgene_pcie_match_table),
684 .suppress_bind_attrs = true,
686 .probe = xgene_pcie_probe,
688 builtin_platform_driver(xgene_pcie_driver);