1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
8 #include <linux/kernel.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio.h>
13 #include <linux/init.h>
14 #include <linux/mbus.h>
15 #include <linux/msi.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_pci.h>
22 #include <linux/of_platform.h>
25 #include "../pci-bridge-emul.h"
28 * PCIe unit register offsets.
30 #define PCIE_DEV_ID_OFF 0x0000
31 #define PCIE_CMD_OFF 0x0004
32 #define PCIE_DEV_REV_OFF 0x0008
33 #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
34 #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
35 #define PCIE_CAP_PCIEXP 0x0060
36 #define PCIE_HEADER_LOG_4_OFF 0x0128
37 #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
38 #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
39 #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
40 #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
41 #define PCIE_WIN5_CTRL_OFF 0x1880
42 #define PCIE_WIN5_BASE_OFF 0x1884
43 #define PCIE_WIN5_REMAP_OFF 0x188c
44 #define PCIE_CONF_ADDR_OFF 0x18f8
45 #define PCIE_CONF_ADDR_EN 0x80000000
46 #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
47 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
48 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
49 #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
50 #define PCIE_CONF_ADDR(bus, devfn, where) \
51 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
52 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
54 #define PCIE_CONF_DATA_OFF 0x18fc
55 #define PCIE_MASK_OFF 0x1910
56 #define PCIE_MASK_ENABLE_INTS 0x0f000000
57 #define PCIE_CTRL_OFF 0x1a00
58 #define PCIE_CTRL_X1_MODE 0x0001
59 #define PCIE_STAT_OFF 0x1a04
60 #define PCIE_STAT_BUS 0xff00
61 #define PCIE_STAT_DEV 0x1f0000
62 #define PCIE_STAT_LINK_DOWN BIT(0)
63 #define PCIE_RC_RTSTA 0x1a14
64 #define PCIE_DEBUG_CTRL 0x1a60
65 #define PCIE_DEBUG_SOFT_RESET BIT(20)
67 struct mvebu_pcie_port;
69 /* Structure representing all PCIe interfaces */
71 struct platform_device *pdev;
72 struct mvebu_pcie_port *ports;
73 struct msi_controller *msi;
75 struct resource realio;
81 struct mvebu_pcie_window {
87 /* Structure representing one PCIe interface */
88 struct mvebu_pcie_port {
94 unsigned int mem_target;
95 unsigned int mem_attr;
96 unsigned int io_target;
99 struct gpio_desc *reset_gpio;
101 struct pci_bridge_emul bridge;
102 struct device_node *dn;
103 struct mvebu_pcie *pcie;
104 struct mvebu_pcie_window memwin;
105 struct mvebu_pcie_window iowin;
107 struct resource regs;
110 static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
112 writel(val, port->base + reg);
115 static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
117 return readl(port->base + reg);
120 static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
122 return port->io_target != -1 && port->io_attr != -1;
125 static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
127 return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
130 static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
134 stat = mvebu_readl(port, PCIE_STAT_OFF);
135 stat &= ~PCIE_STAT_BUS;
137 mvebu_writel(port, stat, PCIE_STAT_OFF);
140 static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
144 stat = mvebu_readl(port, PCIE_STAT_OFF);
145 stat &= ~PCIE_STAT_DEV;
147 mvebu_writel(port, stat, PCIE_STAT_OFF);
151 * Setup PCIE BARs and Address Decode Wins:
152 * BAR[0] -> internal registers (needed for MSI)
153 * BAR[1] -> covers all DRAM banks
155 * WIN[0-3] -> DRAM bank[0-3]
157 static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
159 const struct mbus_dram_target_info *dram;
163 dram = mv_mbus_dram_info();
165 /* First, disable and clear BARs and windows. */
166 for (i = 1; i < 3; i++) {
167 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
168 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
169 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
172 for (i = 0; i < 5; i++) {
173 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
174 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
175 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
178 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
179 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
180 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
182 /* Setup windows for DDR banks. Count total DDR size on the fly. */
184 for (i = 0; i < dram->num_cs; i++) {
185 const struct mbus_dram_window *cs = dram->cs + i;
187 mvebu_writel(port, cs->base & 0xffff0000,
188 PCIE_WIN04_BASE_OFF(i));
189 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
191 ((cs->size - 1) & 0xffff0000) |
192 (cs->mbus_attr << 8) |
193 (dram->mbus_dram_target_id << 4) | 1,
194 PCIE_WIN04_CTRL_OFF(i));
199 /* Round up 'size' to the nearest power of two. */
200 if ((size & (size - 1)) != 0)
201 size = 1 << fls(size);
203 /* Setup BAR[1] to all DRAM banks. */
204 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
205 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
206 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
207 PCIE_BAR_CTRL_OFF(1));
210 * Point BAR[0] to the device's internal registers.
212 mvebu_writel(port, round_down(port->regs.start, SZ_1M), PCIE_BAR_LO_OFF(0));
213 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0));
216 static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
220 /* Point PCIe unit MBUS decode windows to DRAM space. */
221 mvebu_pcie_setup_wins(port);
223 /* Master + slave enable. */
224 cmd = mvebu_readl(port, PCIE_CMD_OFF);
225 cmd |= PCI_COMMAND_IO;
226 cmd |= PCI_COMMAND_MEMORY;
227 cmd |= PCI_COMMAND_MASTER;
228 mvebu_writel(port, cmd, PCIE_CMD_OFF);
230 /* Enable interrupt lines A-D. */
231 mask = mvebu_readl(port, PCIE_MASK_OFF);
232 mask |= PCIE_MASK_ENABLE_INTS;
233 mvebu_writel(port, mask, PCIE_MASK_OFF);
236 static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
238 u32 devfn, int where, int size, u32 *val)
240 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
242 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
247 *val = readb_relaxed(conf_data + (where & 3));
250 *val = readw_relaxed(conf_data + (where & 2));
253 *val = readl_relaxed(conf_data);
257 return PCIBIOS_SUCCESSFUL;
260 static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
262 u32 devfn, int where, int size, u32 val)
264 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
266 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
271 writeb(val, conf_data + (where & 3));
274 writew(val, conf_data + (where & 2));
277 writel(val, conf_data);
280 return PCIBIOS_BAD_REGISTER_NUMBER;
283 return PCIBIOS_SUCCESSFUL;
287 * Remove windows, starting from the largest ones to the smallest
290 static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
291 phys_addr_t base, size_t size)
294 size_t sz = 1 << (fls(size) - 1);
296 mvebu_mbus_del_window(base, sz);
303 * MBus windows can only have a power of two size, but PCI BARs do not
304 * have this constraint. Therefore, we have to split the PCI BAR into
305 * areas each having a power of two size. We start from the largest
306 * one (i.e highest order bit set in the size).
308 static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
309 unsigned int target, unsigned int attribute,
310 phys_addr_t base, size_t size,
313 size_t size_mapped = 0;
316 size_t sz = 1 << (fls(size) - 1);
319 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
322 phys_addr_t end = base + sz - 1;
324 dev_err(&port->pcie->pdev->dev,
325 "Could not create MBus window at [mem %pa-%pa]: %d\n",
327 mvebu_pcie_del_windows(port, base - size_mapped,
335 if (remap != MVEBU_MBUS_NO_REMAP)
340 static void mvebu_pcie_set_window(struct mvebu_pcie_port *port,
341 unsigned int target, unsigned int attribute,
342 const struct mvebu_pcie_window *desired,
343 struct mvebu_pcie_window *cur)
345 if (desired->base == cur->base && desired->remap == cur->remap &&
346 desired->size == cur->size)
349 if (cur->size != 0) {
350 mvebu_pcie_del_windows(port, cur->base, cur->size);
355 * If something tries to change the window while it is enabled
356 * the change will not be done atomically. That would be
357 * difficult to do in the general case.
361 if (desired->size == 0)
364 mvebu_pcie_add_windows(port, target, attribute, desired->base,
365 desired->size, desired->remap);
369 static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
371 struct mvebu_pcie_window desired = {};
372 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
374 /* Are the new iobase/iolimit values invalid? */
375 if (conf->iolimit < conf->iobase ||
376 conf->iolimitupper < conf->iobaseupper ||
377 !(conf->command & PCI_COMMAND_IO)) {
378 mvebu_pcie_set_window(port, port->io_target, port->io_attr,
379 &desired, &port->iowin);
383 if (!mvebu_has_ioport(port)) {
384 dev_WARN(&port->pcie->pdev->dev,
385 "Attempt to set IO when IO is disabled\n");
390 * We read the PCI-to-PCI bridge emulated registers, and
391 * calculate the base address and size of the address decoding
392 * window to setup, according to the PCI-to-PCI bridge
393 * specifications. iobase is the bus address, port->iowin_base
394 * is the CPU address.
396 desired.remap = ((conf->iobase & 0xF0) << 8) |
397 (conf->iobaseupper << 16);
398 desired.base = port->pcie->io.start + desired.remap;
399 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) |
400 (conf->iolimitupper << 16)) -
404 mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
408 static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
410 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
411 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
413 /* Are the new membase/memlimit values invalid? */
414 if (conf->memlimit < conf->membase ||
415 !(conf->command & PCI_COMMAND_MEMORY)) {
416 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
417 &desired, &port->memwin);
422 * We read the PCI-to-PCI bridge emulated registers, and
423 * calculate the base address and size of the address decoding
424 * window to setup, according to the PCI-to-PCI bridge
427 desired.base = ((conf->membase & 0xFFF0) << 16);
428 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) -
431 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
435 static pci_bridge_emul_read_status_t
436 mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
439 struct mvebu_pcie_port *port = bridge->data;
443 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
447 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL) &
448 ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
449 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
454 * PCIe requires the clock power management capability to be
455 * hard-wired to zero for downstream ports
457 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
458 ~PCI_EXP_LNKCAP_CLKPM;
462 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
466 *value = PCI_EXP_SLTSTA_PDS << 16;
470 *value = mvebu_readl(port, PCIE_RC_RTSTA);
474 return PCI_BRIDGE_EMUL_NOT_HANDLED;
477 return PCI_BRIDGE_EMUL_HANDLED;
481 mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
482 int reg, u32 old, u32 new, u32 mask)
484 struct mvebu_pcie_port *port = bridge->data;
485 struct pci_bridge_emul_conf *conf = &bridge->conf;
490 if (!mvebu_has_ioport(port))
491 conf->command &= ~PCI_COMMAND_IO;
493 if ((old ^ new) & PCI_COMMAND_IO)
494 mvebu_pcie_handle_iobase_change(port);
495 if ((old ^ new) & PCI_COMMAND_MEMORY)
496 mvebu_pcie_handle_membase_change(port);
503 * We keep bit 1 set, it is a read-only bit that
504 * indicates we support 32 bits addressing for the
507 conf->iobase |= PCI_IO_RANGE_TYPE_32;
508 conf->iolimit |= PCI_IO_RANGE_TYPE_32;
509 mvebu_pcie_handle_iobase_change(port);
512 case PCI_MEMORY_BASE:
513 mvebu_pcie_handle_membase_change(port);
516 case PCI_IO_BASE_UPPER16:
517 mvebu_pcie_handle_iobase_change(port);
520 case PCI_PRIMARY_BUS:
521 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus);
530 mvebu_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
531 int reg, u32 old, u32 new, u32 mask)
533 struct mvebu_pcie_port *port = bridge->data;
538 * Armada370 data says these bits must always
539 * be zero when in root complex mode.
541 new &= ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
542 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
544 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
549 * If we don't support CLKREQ, we must ensure that the
550 * CLKREQ enable bit always reads zero. Since we haven't
551 * had this capability, and it's dependent on board wiring,
552 * disable it for the time being.
554 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
556 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
560 mvebu_writel(port, new, PCIE_RC_RTSTA);
565 static struct pci_bridge_emul_ops mvebu_pci_bridge_emul_ops = {
566 .write_base = mvebu_pci_bridge_emul_base_conf_write,
567 .read_pcie = mvebu_pci_bridge_emul_pcie_conf_read,
568 .write_pcie = mvebu_pci_bridge_emul_pcie_conf_write,
572 * Initialize the configuration space of the PCI-to-PCI bridge
573 * associated with the given PCIe interface.
575 static void mvebu_pci_bridge_emul_init(struct mvebu_pcie_port *port)
577 struct pci_bridge_emul *bridge = &port->bridge;
579 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL;
580 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
581 bridge->conf.class_revision =
582 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
584 if (mvebu_has_ioport(port)) {
585 /* We support 32 bits I/O addressing */
586 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
587 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
590 bridge->has_pcie = true;
592 bridge->ops = &mvebu_pci_bridge_emul_ops;
594 pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR);
597 static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
599 return sys->private_data;
602 static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
608 for (i = 0; i < pcie->nports; i++) {
609 struct mvebu_pcie_port *port = &pcie->ports[i];
611 if (bus->number == 0 && port->devfn == devfn)
613 if (bus->number != 0 &&
614 bus->number >= port->bridge.conf.secondary_bus &&
615 bus->number <= port->bridge.conf.subordinate_bus)
622 /* PCI configuration space write function */
623 static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
624 int where, int size, u32 val)
626 struct mvebu_pcie *pcie = bus->sysdata;
627 struct mvebu_pcie_port *port;
630 port = mvebu_pcie_find_port(pcie, bus, devfn);
632 return PCIBIOS_DEVICE_NOT_FOUND;
634 /* Access the emulated PCI-to-PCI bridge */
635 if (bus->number == 0)
636 return pci_bridge_emul_conf_write(&port->bridge, where,
639 if (!mvebu_pcie_link_up(port))
640 return PCIBIOS_DEVICE_NOT_FOUND;
642 /* Access the real PCIe interface */
643 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
649 /* PCI configuration space read function */
650 static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
653 struct mvebu_pcie *pcie = bus->sysdata;
654 struct mvebu_pcie_port *port;
657 port = mvebu_pcie_find_port(pcie, bus, devfn);
660 return PCIBIOS_DEVICE_NOT_FOUND;
663 /* Access the emulated PCI-to-PCI bridge */
664 if (bus->number == 0)
665 return pci_bridge_emul_conf_read(&port->bridge, where,
668 if (!mvebu_pcie_link_up(port)) {
670 return PCIBIOS_DEVICE_NOT_FOUND;
673 /* Access the real PCIe interface */
674 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
680 static struct pci_ops mvebu_pcie_ops = {
681 .read = mvebu_pcie_rd_conf,
682 .write = mvebu_pcie_wr_conf,
685 static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
686 const struct resource *res,
687 resource_size_t start,
688 resource_size_t size,
689 resource_size_t align)
691 if (dev->bus->number != 0)
695 * On the PCI-to-PCI bridge side, the I/O windows must have at
696 * least a 64 KB size and the memory windows must have at
697 * least a 1 MB size. Moreover, MBus windows need to have a
698 * base address aligned on their size, and their size must be
699 * a power of two. This means that if the BAR doesn't have a
700 * power of two size, several MBus windows will actually be
701 * created. We need to ensure that the biggest MBus window
702 * (which will be the first one) is aligned on its size, which
703 * explains the rounddown_pow_of_two() being done here.
705 if (res->flags & IORESOURCE_IO)
706 return round_up(start, max_t(resource_size_t, SZ_64K,
707 rounddown_pow_of_two(size)));
708 else if (res->flags & IORESOURCE_MEM)
709 return round_up(start, max_t(resource_size_t, SZ_1M,
710 rounddown_pow_of_two(size)));
715 static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
716 struct device_node *np,
717 struct mvebu_pcie_port *port)
721 ret = of_address_to_resource(np, 0, &port->regs);
723 return (void __iomem *)ERR_PTR(ret);
725 return devm_ioremap_resource(&pdev->dev, &port->regs);
728 #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
729 #define DT_TYPE_IO 0x1
730 #define DT_TYPE_MEM32 0x2
731 #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
732 #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
734 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
739 const int na = 3, ns = 2;
741 int rlen, nranges, rangesz, pna, i;
746 range = of_get_property(np, "ranges", &rlen);
750 pna = of_n_addr_cells(np);
751 rangesz = pna + na + ns;
752 nranges = rlen / sizeof(__be32) / rangesz;
754 for (i = 0; i < nranges; i++, range += rangesz) {
755 u32 flags = of_read_number(range, 1);
756 u32 slot = of_read_number(range + 1, 1);
757 u64 cpuaddr = of_read_number(range + na, pna);
760 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
761 rtype = IORESOURCE_IO;
762 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
763 rtype = IORESOURCE_MEM;
767 if (slot == PCI_SLOT(devfn) && type == rtype) {
768 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
769 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
777 #ifdef CONFIG_PM_SLEEP
778 static int mvebu_pcie_suspend(struct device *dev)
780 struct mvebu_pcie *pcie;
783 pcie = dev_get_drvdata(dev);
784 for (i = 0; i < pcie->nports; i++) {
785 struct mvebu_pcie_port *port = pcie->ports + i;
786 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
792 static int mvebu_pcie_resume(struct device *dev)
794 struct mvebu_pcie *pcie;
797 pcie = dev_get_drvdata(dev);
798 for (i = 0; i < pcie->nports; i++) {
799 struct mvebu_pcie_port *port = pcie->ports + i;
800 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
801 mvebu_pcie_setup_hw(port);
808 static void mvebu_pcie_port_clk_put(void *data)
810 struct mvebu_pcie_port *port = data;
815 static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
816 struct mvebu_pcie_port *port, struct device_node *child)
818 struct device *dev = &pcie->pdev->dev;
819 enum of_gpio_flags flags;
824 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
825 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
830 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
833 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
840 port->devfn = of_pci_get_devfn(child);
844 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
845 &port->mem_target, &port->mem_attr);
847 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
852 if (resource_size(&pcie->io) != 0) {
853 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
854 &port->io_target, &port->io_attr);
856 port->io_target = -1;
860 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
861 if (reset_gpio == -EPROBE_DEFER) {
866 if (gpio_is_valid(reset_gpio)) {
867 unsigned long gpio_flags;
869 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
871 if (!port->reset_name) {
876 if (flags & OF_GPIO_ACTIVE_LOW) {
877 dev_info(dev, "%pOF: reset gpio is active low\n",
879 gpio_flags = GPIOF_ACTIVE_LOW |
882 gpio_flags = GPIOF_OUT_INIT_HIGH;
885 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
888 if (ret == -EPROBE_DEFER)
893 port->reset_gpio = gpio_to_desc(reset_gpio);
896 port->clk = of_clk_get_by_name(child, NULL);
897 if (IS_ERR(port->clk)) {
898 dev_err(dev, "%s: cannot get clock\n", port->name);
902 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
913 /* In the case of skipping, we need to free these */
914 devm_kfree(dev, port->reset_name);
915 port->reset_name = NULL;
916 devm_kfree(dev, port->name);
924 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
925 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
926 * of the PCI Express Card Electromechanical Specification, 1.1.
928 static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
932 ret = clk_prepare_enable(port->clk);
936 if (port->reset_gpio) {
937 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
939 of_property_read_u32(port->dn, "reset-delay-us",
944 gpiod_set_value_cansleep(port->reset_gpio, 0);
945 msleep(reset_udelay / 1000);
952 * Power down a PCIe port. Strictly, PCIe requires us to place the card
953 * in D3hot state before asserting PERST#.
955 static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
957 gpiod_set_value_cansleep(port->reset_gpio, 1);
959 clk_disable_unprepare(port->clk);
963 * We can't use devm_of_pci_get_host_bridge_resources() because we
964 * need to parse our special DT properties encoding the MEM and IO
967 static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
969 struct device *dev = &pcie->pdev->dev;
970 struct device_node *np = dev->of_node;
971 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
974 /* Get the bus range */
975 ret = of_pci_parse_bus_range(np, &pcie->busn);
977 dev_err(dev, "failed to parse bus-range property: %d\n", ret);
980 pci_add_resource(&bridge->windows, &pcie->busn);
982 /* Get the PCIe memory aperture */
983 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
984 if (resource_size(&pcie->mem) == 0) {
985 dev_err(dev, "invalid memory aperture size\n");
989 pcie->mem.name = "PCI MEM";
990 pci_add_resource(&bridge->windows, &pcie->mem);
992 /* Get the PCIe IO aperture */
993 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
995 if (resource_size(&pcie->io) != 0) {
996 pcie->realio.flags = pcie->io.flags;
997 pcie->realio.start = PCIBIOS_MIN_IO;
998 pcie->realio.end = min_t(resource_size_t,
999 IO_SPACE_LIMIT - SZ_64K,
1000 resource_size(&pcie->io) - 1);
1001 pcie->realio.name = "PCI I/O";
1003 pci_add_resource(&bridge->windows, &pcie->realio);
1006 return devm_request_pci_bus_resources(dev, &bridge->windows);
1010 * This is a copy of pci_host_probe(), except that it does the I/O
1011 * remap as the last step, once we are sure we won't fail.
1013 * It should be removed once the I/O remap error handling issue has
1016 static int mvebu_pci_host_probe(struct pci_host_bridge *bridge)
1018 struct mvebu_pcie *pcie;
1019 struct pci_bus *bus, *child;
1022 ret = pci_scan_root_bus_bridge(bridge);
1024 dev_err(bridge->dev.parent, "Scanning root bridge failed");
1028 pcie = pci_host_bridge_priv(bridge);
1029 if (resource_size(&pcie->io) != 0) {
1032 for (i = 0; i < resource_size(&pcie->realio); i += SZ_64K)
1033 pci_ioremap_io(i, pcie->io.start + i);
1039 * We insert PCI resources into the iomem_resource and
1040 * ioport_resource trees in either pci_bus_claim_resources()
1041 * or pci_bus_assign_resources().
1043 if (pci_has_flag(PCI_PROBE_ONLY)) {
1044 pci_bus_claim_resources(bus);
1046 pci_bus_size_bridges(bus);
1047 pci_bus_assign_resources(bus);
1049 list_for_each_entry(child, &bus->children, node)
1050 pcie_bus_configure_settings(child);
1053 pci_bus_add_devices(bus);
1057 static int mvebu_pcie_probe(struct platform_device *pdev)
1059 struct device *dev = &pdev->dev;
1060 struct mvebu_pcie *pcie;
1061 struct pci_host_bridge *bridge;
1062 struct device_node *np = dev->of_node;
1063 struct device_node *child;
1066 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
1070 pcie = pci_host_bridge_priv(bridge);
1072 platform_set_drvdata(pdev, pcie);
1074 ret = mvebu_pcie_parse_request_resources(pcie);
1078 num = of_get_available_child_count(np);
1080 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
1085 for_each_available_child_of_node(np, child) {
1086 struct mvebu_pcie_port *port = &pcie->ports[i];
1088 ret = mvebu_pcie_parse_port(pcie, port, child);
1092 } else if (ret == 0) {
1101 for (i = 0; i < pcie->nports; i++) {
1102 struct mvebu_pcie_port *port = &pcie->ports[i];
1108 ret = mvebu_pcie_powerup(port);
1112 port->base = mvebu_pcie_map_registers(pdev, child, port);
1113 if (IS_ERR(port->base)) {
1114 dev_err(dev, "%s: cannot map registers\n", port->name);
1116 mvebu_pcie_powerdown(port);
1120 mvebu_pcie_setup_hw(port);
1121 mvebu_pcie_set_local_dev_nr(port, 1);
1122 mvebu_pci_bridge_emul_init(port);
1127 bridge->sysdata = pcie;
1128 bridge->ops = &mvebu_pcie_ops;
1129 bridge->align_resource = mvebu_pcie_align_resource;
1130 bridge->msi = pcie->msi;
1132 return mvebu_pci_host_probe(bridge);
1135 static const struct of_device_id mvebu_pcie_of_match_table[] = {
1136 { .compatible = "marvell,armada-xp-pcie", },
1137 { .compatible = "marvell,armada-370-pcie", },
1138 { .compatible = "marvell,dove-pcie", },
1139 { .compatible = "marvell,kirkwood-pcie", },
1143 static const struct dev_pm_ops mvebu_pcie_pm_ops = {
1144 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
1147 static struct platform_driver mvebu_pcie_driver = {
1149 .name = "mvebu-pcie",
1150 .of_match_table = mvebu_pcie_of_match_table,
1151 /* driver unloading/unbinding currently not supported */
1152 .suppress_bind_attrs = true,
1153 .pm = &mvebu_pcie_pm_ops,
1155 .probe = mvebu_pcie_probe,
1157 builtin_platform_driver(mvebu_pcie_driver);