1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
5 * Author: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
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/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_gpio.h>
20 #include <linux/of_pci.h>
21 #include <linux/of_platform.h>
24 #include "../pci-bridge-emul.h"
27 * PCIe unit register offsets.
29 #define PCIE_DEV_ID_OFF 0x0000
30 #define PCIE_CMD_OFF 0x0004
31 #define PCIE_DEV_REV_OFF 0x0008
32 #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
33 #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
34 #define PCIE_CAP_PCIEXP 0x0060
35 #define PCIE_HEADER_LOG_4_OFF 0x0128
36 #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
37 #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
38 #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
39 #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
40 #define PCIE_WIN5_CTRL_OFF 0x1880
41 #define PCIE_WIN5_BASE_OFF 0x1884
42 #define PCIE_WIN5_REMAP_OFF 0x188c
43 #define PCIE_CONF_ADDR_OFF 0x18f8
44 #define PCIE_CONF_ADDR_EN 0x80000000
45 #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
46 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
47 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
48 #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
49 #define PCIE_CONF_ADDR(bus, devfn, where) \
50 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
51 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
53 #define PCIE_CONF_DATA_OFF 0x18fc
54 #define PCIE_MASK_OFF 0x1910
55 #define PCIE_MASK_ENABLE_INTS 0x0f000000
56 #define PCIE_CTRL_OFF 0x1a00
57 #define PCIE_CTRL_X1_MODE 0x0001
58 #define PCIE_CTRL_RC_MODE BIT(1)
59 #define PCIE_CTRL_MASTER_HOT_RESET BIT(24)
60 #define PCIE_STAT_OFF 0x1a04
61 #define PCIE_STAT_BUS 0xff00
62 #define PCIE_STAT_DEV 0x1f0000
63 #define PCIE_STAT_LINK_DOWN BIT(0)
64 #define PCIE_RC_RTSTA 0x1a14
65 #define PCIE_DEBUG_CTRL 0x1a60
66 #define PCIE_DEBUG_SOFT_RESET BIT(20)
68 struct mvebu_pcie_port;
70 /* Structure representing all PCIe interfaces */
72 struct platform_device *pdev;
73 struct mvebu_pcie_port *ports;
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 u8 mvebu_pcie_get_local_bus_nr(struct mvebu_pcie_port *port)
132 return (mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_BUS) >> 8;
135 static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
139 stat = mvebu_readl(port, PCIE_STAT_OFF);
140 stat &= ~PCIE_STAT_BUS;
142 mvebu_writel(port, stat, PCIE_STAT_OFF);
145 static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
149 stat = mvebu_readl(port, PCIE_STAT_OFF);
150 stat &= ~PCIE_STAT_DEV;
152 mvebu_writel(port, stat, PCIE_STAT_OFF);
156 * Setup PCIE BARs and Address Decode Wins:
157 * BAR[0] -> internal registers (needed for MSI)
158 * BAR[1] -> covers all DRAM banks
160 * WIN[0-3] -> DRAM bank[0-3]
162 static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
164 const struct mbus_dram_target_info *dram;
168 dram = mv_mbus_dram_info();
170 /* First, disable and clear BARs and windows. */
171 for (i = 1; i < 3; i++) {
172 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
173 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
174 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
177 for (i = 0; i < 5; i++) {
178 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
179 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
180 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
183 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
184 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
185 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
187 /* Setup windows for DDR banks. Count total DDR size on the fly. */
189 for (i = 0; i < dram->num_cs; i++) {
190 const struct mbus_dram_window *cs = dram->cs + i;
192 mvebu_writel(port, cs->base & 0xffff0000,
193 PCIE_WIN04_BASE_OFF(i));
194 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
196 ((cs->size - 1) & 0xffff0000) |
197 (cs->mbus_attr << 8) |
198 (dram->mbus_dram_target_id << 4) | 1,
199 PCIE_WIN04_CTRL_OFF(i));
204 /* Round up 'size' to the nearest power of two. */
205 if ((size & (size - 1)) != 0)
206 size = 1 << fls(size);
208 /* Setup BAR[1] to all DRAM banks. */
209 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
210 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
211 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
212 PCIE_BAR_CTRL_OFF(1));
215 * Point BAR[0] to the device's internal registers.
217 mvebu_writel(port, round_down(port->regs.start, SZ_1M), PCIE_BAR_LO_OFF(0));
218 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0));
221 static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
225 /* Setup PCIe controller to Root Complex mode. */
226 ctrl = mvebu_readl(port, PCIE_CTRL_OFF);
227 ctrl |= PCIE_CTRL_RC_MODE;
228 mvebu_writel(port, ctrl, PCIE_CTRL_OFF);
230 /* Disable Root Bridge I/O space, memory space and bus mastering. */
231 cmd = mvebu_readl(port, PCIE_CMD_OFF);
232 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
233 mvebu_writel(port, cmd, PCIE_CMD_OFF);
235 /* Point PCIe unit MBUS decode windows to DRAM space. */
236 mvebu_pcie_setup_wins(port);
238 /* Enable interrupt lines A-D. */
239 mask = mvebu_readl(port, PCIE_MASK_OFF);
240 mask |= PCIE_MASK_ENABLE_INTS;
241 mvebu_writel(port, mask, PCIE_MASK_OFF);
244 static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
246 u32 devfn, int where, int size, u32 *val)
248 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
250 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
255 *val = readb_relaxed(conf_data + (where & 3));
258 *val = readw_relaxed(conf_data + (where & 2));
261 *val = readl_relaxed(conf_data);
265 return PCIBIOS_SUCCESSFUL;
268 static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
270 u32 devfn, int where, int size, u32 val)
272 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
274 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
279 writeb(val, conf_data + (where & 3));
282 writew(val, conf_data + (where & 2));
285 writel(val, conf_data);
288 return PCIBIOS_BAD_REGISTER_NUMBER;
291 return PCIBIOS_SUCCESSFUL;
295 * Remove windows, starting from the largest ones to the smallest
298 static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
299 phys_addr_t base, size_t size)
302 size_t sz = 1 << (fls(size) - 1);
304 mvebu_mbus_del_window(base, sz);
311 * MBus windows can only have a power of two size, but PCI BARs do not
312 * have this constraint. Therefore, we have to split the PCI BAR into
313 * areas each having a power of two size. We start from the largest
314 * one (i.e highest order bit set in the size).
316 static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
317 unsigned int target, unsigned int attribute,
318 phys_addr_t base, size_t size,
321 size_t size_mapped = 0;
324 size_t sz = 1 << (fls(size) - 1);
327 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
330 phys_addr_t end = base + sz - 1;
332 dev_err(&port->pcie->pdev->dev,
333 "Could not create MBus window at [mem %pa-%pa]: %d\n",
335 mvebu_pcie_del_windows(port, base - size_mapped,
343 if (remap != MVEBU_MBUS_NO_REMAP)
348 static void mvebu_pcie_set_window(struct mvebu_pcie_port *port,
349 unsigned int target, unsigned int attribute,
350 const struct mvebu_pcie_window *desired,
351 struct mvebu_pcie_window *cur)
353 if (desired->base == cur->base && desired->remap == cur->remap &&
354 desired->size == cur->size)
357 if (cur->size != 0) {
358 mvebu_pcie_del_windows(port, cur->base, cur->size);
363 * If something tries to change the window while it is enabled
364 * the change will not be done atomically. That would be
365 * difficult to do in the general case.
369 if (desired->size == 0)
372 mvebu_pcie_add_windows(port, target, attribute, desired->base,
373 desired->size, desired->remap);
377 static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
379 struct mvebu_pcie_window desired = {};
380 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
382 /* Are the new iobase/iolimit values invalid? */
383 if (conf->iolimit < conf->iobase ||
384 conf->iolimitupper < conf->iobaseupper) {
385 mvebu_pcie_set_window(port, port->io_target, port->io_attr,
386 &desired, &port->iowin);
390 if (!mvebu_has_ioport(port)) {
391 dev_WARN(&port->pcie->pdev->dev,
392 "Attempt to set IO when IO is disabled\n");
397 * We read the PCI-to-PCI bridge emulated registers, and
398 * calculate the base address and size of the address decoding
399 * window to setup, according to the PCI-to-PCI bridge
400 * specifications. iobase is the bus address, port->iowin_base
401 * is the CPU address.
403 desired.remap = ((conf->iobase & 0xF0) << 8) |
404 (conf->iobaseupper << 16);
405 desired.base = port->pcie->io.start + desired.remap;
406 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) |
407 (conf->iolimitupper << 16)) -
411 mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
415 static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
417 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
418 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
420 /* Are the new membase/memlimit values invalid? */
421 if (conf->memlimit < conf->membase) {
422 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
423 &desired, &port->memwin);
428 * We read the PCI-to-PCI bridge emulated registers, and
429 * calculate the base address and size of the address decoding
430 * window to setup, according to the PCI-to-PCI bridge
433 desired.base = ((conf->membase & 0xFFF0) << 16);
434 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) -
437 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
441 static pci_bridge_emul_read_status_t
442 mvebu_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
445 struct mvebu_pcie_port *port = bridge->data;
449 *value = mvebu_readl(port, PCIE_CMD_OFF);
452 case PCI_PRIMARY_BUS: {
454 * From the whole 32bit register we support reading from HW only
455 * secondary bus number which is mvebu local bus number.
456 * Other bits are retrieved only from emulated config buffer.
458 __le32 *cfgspace = (__le32 *)&bridge->conf;
459 u32 val = le32_to_cpu(cfgspace[PCI_PRIMARY_BUS / 4]);
461 val |= mvebu_pcie_get_local_bus_nr(port) << 8;
466 case PCI_INTERRUPT_LINE: {
468 * From the whole 32bit register we support reading from HW only
469 * one bit: PCI_BRIDGE_CTL_BUS_RESET.
470 * Other bits are retrieved only from emulated config buffer.
472 __le32 *cfgspace = (__le32 *)&bridge->conf;
473 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
474 if (mvebu_readl(port, PCIE_CTRL_OFF) & PCIE_CTRL_MASTER_HOT_RESET)
475 val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
477 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
483 return PCI_BRIDGE_EMUL_NOT_HANDLED;
486 return PCI_BRIDGE_EMUL_HANDLED;
489 static pci_bridge_emul_read_status_t
490 mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
493 struct mvebu_pcie_port *port = bridge->data;
497 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
501 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
506 * PCIe requires the clock power management capability to be
507 * hard-wired to zero for downstream ports
509 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
510 ~PCI_EXP_LNKCAP_CLKPM;
514 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
518 *value = PCI_EXP_SLTSTA_PDS << 16;
522 *value = mvebu_readl(port, PCIE_RC_RTSTA);
526 return PCI_BRIDGE_EMUL_NOT_HANDLED;
529 return PCI_BRIDGE_EMUL_HANDLED;
533 mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
534 int reg, u32 old, u32 new, u32 mask)
536 struct mvebu_pcie_port *port = bridge->data;
537 struct pci_bridge_emul_conf *conf = &bridge->conf;
541 if (!mvebu_has_ioport(port)) {
542 conf->command = cpu_to_le16(
543 le16_to_cpu(conf->command) & ~PCI_COMMAND_IO);
544 new &= ~PCI_COMMAND_IO;
547 mvebu_writel(port, new, PCIE_CMD_OFF);
551 mvebu_pcie_handle_iobase_change(port);
554 case PCI_MEMORY_BASE:
555 mvebu_pcie_handle_membase_change(port);
558 case PCI_IO_BASE_UPPER16:
559 mvebu_pcie_handle_iobase_change(port);
562 case PCI_PRIMARY_BUS:
564 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus);
567 case PCI_INTERRUPT_LINE:
568 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
569 u32 ctrl = mvebu_readl(port, PCIE_CTRL_OFF);
570 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
571 ctrl |= PCIE_CTRL_MASTER_HOT_RESET;
573 ctrl &= ~PCIE_CTRL_MASTER_HOT_RESET;
574 mvebu_writel(port, ctrl, PCIE_CTRL_OFF);
584 mvebu_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
585 int reg, u32 old, u32 new, u32 mask)
587 struct mvebu_pcie_port *port = bridge->data;
591 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
596 * If we don't support CLKREQ, we must ensure that the
597 * CLKREQ enable bit always reads zero. Since we haven't
598 * had this capability, and it's dependent on board wiring,
599 * disable it for the time being.
601 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
603 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
607 mvebu_writel(port, new, PCIE_RC_RTSTA);
612 static struct pci_bridge_emul_ops mvebu_pci_bridge_emul_ops = {
613 .read_base = mvebu_pci_bridge_emul_base_conf_read,
614 .write_base = mvebu_pci_bridge_emul_base_conf_write,
615 .read_pcie = mvebu_pci_bridge_emul_pcie_conf_read,
616 .write_pcie = mvebu_pci_bridge_emul_pcie_conf_write,
620 * Initialize the configuration space of the PCI-to-PCI bridge
621 * associated with the given PCIe interface.
623 static int mvebu_pci_bridge_emul_init(struct mvebu_pcie_port *port)
625 struct pci_bridge_emul *bridge = &port->bridge;
626 u32 pcie_cap = mvebu_readl(port, PCIE_CAP_PCIEXP);
627 u8 pcie_cap_ver = ((pcie_cap >> 16) & PCI_EXP_FLAGS_VERS);
629 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL;
630 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
631 bridge->conf.class_revision =
632 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
634 if (mvebu_has_ioport(port)) {
635 /* We support 32 bits I/O addressing */
636 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
637 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
641 * Older mvebu hardware provides PCIe Capability structure only in
642 * version 1. New hardware provides it in version 2.
644 bridge->pcie_conf.cap = cpu_to_le16(pcie_cap_ver);
646 bridge->has_pcie = true;
648 bridge->ops = &mvebu_pci_bridge_emul_ops;
650 return pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR);
653 static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
655 return sys->private_data;
658 static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
664 for (i = 0; i < pcie->nports; i++) {
665 struct mvebu_pcie_port *port = &pcie->ports[i];
667 if (bus->number == 0 && port->devfn == devfn)
669 if (bus->number != 0 &&
670 bus->number >= port->bridge.conf.secondary_bus &&
671 bus->number <= port->bridge.conf.subordinate_bus)
678 /* PCI configuration space write function */
679 static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
680 int where, int size, u32 val)
682 struct mvebu_pcie *pcie = bus->sysdata;
683 struct mvebu_pcie_port *port;
686 port = mvebu_pcie_find_port(pcie, bus, devfn);
688 return PCIBIOS_DEVICE_NOT_FOUND;
690 /* Access the emulated PCI-to-PCI bridge */
691 if (bus->number == 0)
692 return pci_bridge_emul_conf_write(&port->bridge, where,
695 if (!mvebu_pcie_link_up(port))
696 return PCIBIOS_DEVICE_NOT_FOUND;
698 /* Access the real PCIe interface */
699 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
705 /* PCI configuration space read function */
706 static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
709 struct mvebu_pcie *pcie = bus->sysdata;
710 struct mvebu_pcie_port *port;
713 port = mvebu_pcie_find_port(pcie, bus, devfn);
716 return PCIBIOS_DEVICE_NOT_FOUND;
719 /* Access the emulated PCI-to-PCI bridge */
720 if (bus->number == 0)
721 return pci_bridge_emul_conf_read(&port->bridge, where,
724 if (!mvebu_pcie_link_up(port)) {
726 return PCIBIOS_DEVICE_NOT_FOUND;
729 /* Access the real PCIe interface */
730 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
736 static struct pci_ops mvebu_pcie_ops = {
737 .read = mvebu_pcie_rd_conf,
738 .write = mvebu_pcie_wr_conf,
741 static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
742 const struct resource *res,
743 resource_size_t start,
744 resource_size_t size,
745 resource_size_t align)
747 if (dev->bus->number != 0)
751 * On the PCI-to-PCI bridge side, the I/O windows must have at
752 * least a 64 KB size and the memory windows must have at
753 * least a 1 MB size. Moreover, MBus windows need to have a
754 * base address aligned on their size, and their size must be
755 * a power of two. This means that if the BAR doesn't have a
756 * power of two size, several MBus windows will actually be
757 * created. We need to ensure that the biggest MBus window
758 * (which will be the first one) is aligned on its size, which
759 * explains the rounddown_pow_of_two() being done here.
761 if (res->flags & IORESOURCE_IO)
762 return round_up(start, max_t(resource_size_t, SZ_64K,
763 rounddown_pow_of_two(size)));
764 else if (res->flags & IORESOURCE_MEM)
765 return round_up(start, max_t(resource_size_t, SZ_1M,
766 rounddown_pow_of_two(size)));
771 static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
772 struct device_node *np,
773 struct mvebu_pcie_port *port)
777 ret = of_address_to_resource(np, 0, &port->regs);
779 return (void __iomem *)ERR_PTR(ret);
781 return devm_ioremap_resource(&pdev->dev, &port->regs);
784 #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
785 #define DT_TYPE_IO 0x1
786 #define DT_TYPE_MEM32 0x2
787 #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
788 #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
790 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
795 const int na = 3, ns = 2;
797 int rlen, nranges, rangesz, pna, i;
802 range = of_get_property(np, "ranges", &rlen);
806 pna = of_n_addr_cells(np);
807 rangesz = pna + na + ns;
808 nranges = rlen / sizeof(__be32) / rangesz;
810 for (i = 0; i < nranges; i++, range += rangesz) {
811 u32 flags = of_read_number(range, 1);
812 u32 slot = of_read_number(range + 1, 1);
813 u64 cpuaddr = of_read_number(range + na, pna);
816 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
817 rtype = IORESOURCE_IO;
818 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
819 rtype = IORESOURCE_MEM;
823 if (slot == PCI_SLOT(devfn) && type == rtype) {
824 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
825 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
833 #ifdef CONFIG_PM_SLEEP
834 static int mvebu_pcie_suspend(struct device *dev)
836 struct mvebu_pcie *pcie;
839 pcie = dev_get_drvdata(dev);
840 for (i = 0; i < pcie->nports; i++) {
841 struct mvebu_pcie_port *port = pcie->ports + i;
842 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
848 static int mvebu_pcie_resume(struct device *dev)
850 struct mvebu_pcie *pcie;
853 pcie = dev_get_drvdata(dev);
854 for (i = 0; i < pcie->nports; i++) {
855 struct mvebu_pcie_port *port = pcie->ports + i;
856 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
857 mvebu_pcie_setup_hw(port);
864 static void mvebu_pcie_port_clk_put(void *data)
866 struct mvebu_pcie_port *port = data;
871 static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
872 struct mvebu_pcie_port *port, struct device_node *child)
874 struct device *dev = &pcie->pdev->dev;
875 enum of_gpio_flags flags;
880 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
881 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
886 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
889 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
896 port->devfn = of_pci_get_devfn(child);
900 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
901 &port->mem_target, &port->mem_attr);
903 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
908 if (resource_size(&pcie->io) != 0) {
909 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
910 &port->io_target, &port->io_attr);
912 port->io_target = -1;
916 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
917 if (reset_gpio == -EPROBE_DEFER) {
922 if (gpio_is_valid(reset_gpio)) {
923 unsigned long gpio_flags;
925 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
927 if (!port->reset_name) {
932 if (flags & OF_GPIO_ACTIVE_LOW) {
933 dev_info(dev, "%pOF: reset gpio is active low\n",
935 gpio_flags = GPIOF_ACTIVE_LOW |
938 gpio_flags = GPIOF_OUT_INIT_HIGH;
941 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
944 if (ret == -EPROBE_DEFER)
949 port->reset_gpio = gpio_to_desc(reset_gpio);
952 port->clk = of_clk_get_by_name(child, NULL);
953 if (IS_ERR(port->clk)) {
954 dev_err(dev, "%s: cannot get clock\n", port->name);
958 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
969 /* In the case of skipping, we need to free these */
970 devm_kfree(dev, port->reset_name);
971 port->reset_name = NULL;
972 devm_kfree(dev, port->name);
980 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
981 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
982 * of the PCI Express Card Electromechanical Specification, 1.1.
984 static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
988 ret = clk_prepare_enable(port->clk);
992 if (port->reset_gpio) {
993 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
995 of_property_read_u32(port->dn, "reset-delay-us",
1000 gpiod_set_value_cansleep(port->reset_gpio, 0);
1001 msleep(reset_udelay / 1000);
1008 * Power down a PCIe port. Strictly, PCIe requires us to place the card
1009 * in D3hot state before asserting PERST#.
1011 static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
1013 gpiod_set_value_cansleep(port->reset_gpio, 1);
1015 clk_disable_unprepare(port->clk);
1019 * devm_of_pci_get_host_bridge_resources() only sets up translateable resources,
1020 * so we need extra resource setup parsing our special DT properties encoding
1021 * the MEM and IO apertures.
1023 static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
1025 struct device *dev = &pcie->pdev->dev;
1026 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1029 /* Get the PCIe memory aperture */
1030 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
1031 if (resource_size(&pcie->mem) == 0) {
1032 dev_err(dev, "invalid memory aperture size\n");
1036 pcie->mem.name = "PCI MEM";
1037 pci_add_resource(&bridge->windows, &pcie->mem);
1038 ret = devm_request_resource(dev, &iomem_resource, &pcie->mem);
1042 /* Get the PCIe IO aperture */
1043 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
1045 if (resource_size(&pcie->io) != 0) {
1046 pcie->realio.flags = pcie->io.flags;
1047 pcie->realio.start = PCIBIOS_MIN_IO;
1048 pcie->realio.end = min_t(resource_size_t,
1049 IO_SPACE_LIMIT - SZ_64K,
1050 resource_size(&pcie->io) - 1);
1051 pcie->realio.name = "PCI I/O";
1053 pci_add_resource(&bridge->windows, &pcie->realio);
1054 ret = devm_request_resource(dev, &ioport_resource, &pcie->realio);
1063 * This is a copy of pci_host_probe(), except that it does the I/O
1064 * remap as the last step, once we are sure we won't fail.
1066 * It should be removed once the I/O remap error handling issue has
1069 static int mvebu_pci_host_probe(struct pci_host_bridge *bridge)
1071 struct mvebu_pcie *pcie;
1072 struct pci_bus *bus, *child;
1075 ret = pci_scan_root_bus_bridge(bridge);
1077 dev_err(bridge->dev.parent, "Scanning root bridge failed");
1081 pcie = pci_host_bridge_priv(bridge);
1082 if (resource_size(&pcie->io) != 0) {
1085 for (i = 0; i < resource_size(&pcie->realio); i += SZ_64K)
1086 pci_ioremap_io(i, pcie->io.start + i);
1092 * We insert PCI resources into the iomem_resource and
1093 * ioport_resource trees in either pci_bus_claim_resources()
1094 * or pci_bus_assign_resources().
1096 if (pci_has_flag(PCI_PROBE_ONLY)) {
1097 pci_bus_claim_resources(bus);
1099 pci_bus_size_bridges(bus);
1100 pci_bus_assign_resources(bus);
1102 list_for_each_entry(child, &bus->children, node)
1103 pcie_bus_configure_settings(child);
1106 pci_bus_add_devices(bus);
1110 static int mvebu_pcie_probe(struct platform_device *pdev)
1112 struct device *dev = &pdev->dev;
1113 struct mvebu_pcie *pcie;
1114 struct pci_host_bridge *bridge;
1115 struct device_node *np = dev->of_node;
1116 struct device_node *child;
1119 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
1123 pcie = pci_host_bridge_priv(bridge);
1125 platform_set_drvdata(pdev, pcie);
1127 ret = mvebu_pcie_parse_request_resources(pcie);
1131 num = of_get_available_child_count(np);
1133 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
1138 for_each_available_child_of_node(np, child) {
1139 struct mvebu_pcie_port *port = &pcie->ports[i];
1141 ret = mvebu_pcie_parse_port(pcie, port, child);
1145 } else if (ret == 0) {
1154 for (i = 0; i < pcie->nports; i++) {
1155 struct mvebu_pcie_port *port = &pcie->ports[i];
1161 ret = mvebu_pcie_powerup(port);
1165 port->base = mvebu_pcie_map_registers(pdev, child, port);
1166 if (IS_ERR(port->base)) {
1167 dev_err(dev, "%s: cannot map registers\n", port->name);
1169 mvebu_pcie_powerdown(port);
1173 ret = mvebu_pci_bridge_emul_init(port);
1175 dev_err(dev, "%s: cannot init emulated bridge\n",
1177 devm_iounmap(dev, port->base);
1179 mvebu_pcie_powerdown(port);
1184 * PCIe topology exported by mvebu hw is quite complicated. In
1185 * reality has something like N fully independent host bridges
1186 * where each host bridge has one PCIe Root Port (which acts as
1187 * PCI Bridge device). Each host bridge has its own independent
1188 * internal registers, independent access to PCI config space,
1189 * independent interrupt lines, independent window and memory
1190 * access configuration. But additionally there is some kind of
1191 * peer-to-peer support between PCIe devices behind different
1192 * host bridges limited just to forwarding of memory and I/O
1193 * transactions (forwarding of error messages and config cycles
1194 * is not supported). So we could say there are N independent
1195 * PCIe Root Complexes.
1197 * For this kind of setup DT should have been structured into
1198 * N independent PCIe controllers / host bridges. But instead
1199 * structure in past was defined to put PCIe Root Ports of all
1200 * host bridges into one bus zero, like in classic multi-port
1201 * Root Complex setup with just one host bridge.
1203 * This means that pci-mvebu.c driver provides "virtual" bus 0
1204 * on which registers all PCIe Root Ports (PCI Bridge devices)
1205 * specified in DT by their BDF addresses and virtually routes
1206 * PCI config access of each PCI bridge device to specific PCIe
1209 * Normally PCI Bridge should choose between Type 0 and Type 1
1210 * config requests based on primary and secondary bus numbers
1211 * configured on the bridge itself. But because mvebu PCI Bridge
1212 * does not have registers for primary and secondary bus numbers
1213 * in its config space, it determinates type of config requests
1214 * via its own custom way.
1216 * There are two options how mvebu determinate type of config
1219 * 1. If Secondary Bus Number Enable bit is not set or is not
1220 * available (applies for pre-XP PCIe controllers) then Type 0
1221 * is used if target bus number equals Local Bus Number (bits
1222 * [15:8] in register 0x1a04) and target device number differs
1223 * from Local Device Number (bits [20:16] in register 0x1a04).
1224 * Type 1 is used if target bus number differs from Local Bus
1225 * Number. And when target bus number equals Local Bus Number
1226 * and target device equals Local Device Number then request is
1227 * routed to Local PCI Bridge (PCIe Root Port).
1229 * 2. If Secondary Bus Number Enable bit is set (bit 7 in
1230 * register 0x1a2c) then mvebu hw determinate type of config
1231 * request like compliant PCI Bridge based on primary bus number
1232 * which is configured via Local Bus Number (bits [15:8] in
1233 * register 0x1a04) and secondary bus number which is configured
1234 * via Secondary Bus Number (bits [7:0] in register 0x1a2c).
1235 * Local PCI Bridge (PCIe Root Port) is available on primary bus
1236 * as device with Local Device Number (bits [20:16] in register
1239 * Secondary Bus Number Enable bit is disabled by default and
1240 * option 2. is not available on pre-XP PCIe controllers. Hence
1241 * this driver always use option 1.
1243 * Basically it means that primary and secondary buses shares
1244 * one virtual number configured via Local Bus Number bits and
1245 * Local Device Number bits determinates if accessing primary
1246 * or secondary bus. Set Local Device Number to 1 and redirect
1247 * all writes of PCI Bridge Secondary Bus Number register to
1248 * Local Bus Number (bits [15:8] in register 0x1a04).
1250 * So when accessing devices on buses behind secondary bus
1251 * number it would work correctly. And also when accessing
1252 * device 0 at secondary bus number via config space would be
1253 * correctly routed to secondary bus. Due to issues described
1254 * in mvebu_pcie_setup_hw(), PCI Bridges at primary bus (zero)
1255 * are not accessed directly via PCI config space but rarher
1256 * indirectly via kernel emulated PCI bridge driver.
1258 mvebu_pcie_setup_hw(port);
1259 mvebu_pcie_set_local_dev_nr(port, 0);
1264 bridge->sysdata = pcie;
1265 bridge->ops = &mvebu_pcie_ops;
1266 bridge->align_resource = mvebu_pcie_align_resource;
1268 return mvebu_pci_host_probe(bridge);
1271 static const struct of_device_id mvebu_pcie_of_match_table[] = {
1272 { .compatible = "marvell,armada-xp-pcie", },
1273 { .compatible = "marvell,armada-370-pcie", },
1274 { .compatible = "marvell,dove-pcie", },
1275 { .compatible = "marvell,kirkwood-pcie", },
1279 static const struct dev_pm_ops mvebu_pcie_pm_ops = {
1280 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
1283 static struct platform_driver mvebu_pcie_driver = {
1285 .name = "mvebu-pcie",
1286 .of_match_table = mvebu_pcie_of_match_table,
1287 /* driver unloading/unbinding currently not supported */
1288 .suppress_bind_attrs = true,
1289 .pm = &mvebu_pcie_pm_ops,
1291 .probe = mvebu_pcie_probe,
1293 builtin_platform_driver(mvebu_pcie_driver);