1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/init.h>
15 #include <linux/mbus.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_INT_CAUSE_OFF 0x1900
56 #define PCIE_INT_PM_PME BIT(28)
57 #define PCIE_MASK_OFF 0x1910
58 #define PCIE_MASK_ENABLE_INTS 0x0f000000
59 #define PCIE_CTRL_OFF 0x1a00
60 #define PCIE_CTRL_X1_MODE 0x0001
61 #define PCIE_CTRL_RC_MODE BIT(1)
62 #define PCIE_CTRL_MASTER_HOT_RESET BIT(24)
63 #define PCIE_STAT_OFF 0x1a04
64 #define PCIE_STAT_BUS 0xff00
65 #define PCIE_STAT_DEV 0x1f0000
66 #define PCIE_STAT_LINK_DOWN BIT(0)
67 #define PCIE_RC_RTSTA 0x1a14
68 #define PCIE_DEBUG_CTRL 0x1a60
69 #define PCIE_DEBUG_SOFT_RESET BIT(20)
71 struct mvebu_pcie_port;
73 /* Structure representing all PCIe interfaces */
75 struct platform_device *pdev;
76 struct mvebu_pcie_port *ports;
78 struct resource realio;
84 struct mvebu_pcie_window {
90 /* Structure representing one PCIe interface */
91 struct mvebu_pcie_port {
97 unsigned int mem_target;
98 unsigned int mem_attr;
99 unsigned int io_target;
100 unsigned int io_attr;
102 struct gpio_desc *reset_gpio;
104 struct pci_bridge_emul bridge;
105 struct device_node *dn;
106 struct mvebu_pcie *pcie;
107 struct mvebu_pcie_window memwin;
108 struct mvebu_pcie_window iowin;
110 struct resource regs;
113 static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
115 writel(val, port->base + reg);
118 static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
120 return readl(port->base + reg);
123 static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
125 return port->io_target != -1 && port->io_attr != -1;
128 static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
130 return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
133 static u8 mvebu_pcie_get_local_bus_nr(struct mvebu_pcie_port *port)
135 return (mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_BUS) >> 8;
138 static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
142 stat = mvebu_readl(port, PCIE_STAT_OFF);
143 stat &= ~PCIE_STAT_BUS;
145 mvebu_writel(port, stat, PCIE_STAT_OFF);
148 static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
152 stat = mvebu_readl(port, PCIE_STAT_OFF);
153 stat &= ~PCIE_STAT_DEV;
155 mvebu_writel(port, stat, PCIE_STAT_OFF);
158 static void mvebu_pcie_disable_wins(struct mvebu_pcie_port *port)
162 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(0));
163 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0));
165 for (i = 1; i < 3; i++) {
166 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
167 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
168 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
171 for (i = 0; i < 5; i++) {
172 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
173 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
174 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
177 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
178 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
179 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
183 * Setup PCIE BARs and Address Decode Wins:
184 * BAR[0] -> internal registers (needed for MSI)
185 * BAR[1] -> covers all DRAM banks
187 * WIN[0-3] -> DRAM bank[0-3]
189 static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
191 const struct mbus_dram_target_info *dram;
195 dram = mv_mbus_dram_info();
197 /* First, disable and clear BARs and windows. */
198 mvebu_pcie_disable_wins(port);
200 /* Setup windows for DDR banks. Count total DDR size on the fly. */
202 for (i = 0; i < dram->num_cs; i++) {
203 const struct mbus_dram_window *cs = dram->cs + i;
205 mvebu_writel(port, cs->base & 0xffff0000,
206 PCIE_WIN04_BASE_OFF(i));
207 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
209 ((cs->size - 1) & 0xffff0000) |
210 (cs->mbus_attr << 8) |
211 (dram->mbus_dram_target_id << 4) | 1,
212 PCIE_WIN04_CTRL_OFF(i));
217 /* Round up 'size' to the nearest power of two. */
218 if ((size & (size - 1)) != 0)
219 size = 1 << fls(size);
221 /* Setup BAR[1] to all DRAM banks. */
222 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
223 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
224 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
225 PCIE_BAR_CTRL_OFF(1));
228 * Point BAR[0] to the device's internal registers.
230 mvebu_writel(port, round_down(port->regs.start, SZ_1M), PCIE_BAR_LO_OFF(0));
231 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0));
234 static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
236 u32 ctrl, cmd, dev_rev, mask;
238 /* Setup PCIe controller to Root Complex mode. */
239 ctrl = mvebu_readl(port, PCIE_CTRL_OFF);
240 ctrl |= PCIE_CTRL_RC_MODE;
241 mvebu_writel(port, ctrl, PCIE_CTRL_OFF);
243 /* Disable Root Bridge I/O space, memory space and bus mastering. */
244 cmd = mvebu_readl(port, PCIE_CMD_OFF);
245 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
246 mvebu_writel(port, cmd, PCIE_CMD_OFF);
249 * Change Class Code of PCI Bridge device to PCI Bridge (0x6004)
250 * because default value is Memory controller (0x5080).
252 * Note that this mvebu PCI Bridge does not have compliant Type 1
253 * Configuration Space. Header Type is reported as Type 0 and it
254 * has format of Type 0 config space.
256 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
257 * have the same format in Marvell's specification as in PCIe
258 * specification, but their meaning is totally different and they do
259 * different things: they are aliased into internal mvebu registers
260 * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or
261 * reconfigured by pci device drivers.
263 * Therefore driver uses emulation of PCI Bridge which emulates
264 * access to configuration space via internal mvebu registers or
265 * emulated configuration buffer. Driver access these PCI Bridge
266 * directly for simplification, but these registers can be accessed
267 * also via standard mvebu way for accessing PCI config space.
269 dev_rev = mvebu_readl(port, PCIE_DEV_REV_OFF);
270 dev_rev &= ~0xffffff00;
271 dev_rev |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
272 mvebu_writel(port, dev_rev, PCIE_DEV_REV_OFF);
274 /* Point PCIe unit MBUS decode windows to DRAM space. */
275 mvebu_pcie_setup_wins(port);
277 /* Enable interrupt lines A-D. */
278 mask = mvebu_readl(port, PCIE_MASK_OFF);
279 mask |= PCIE_MASK_ENABLE_INTS;
280 mvebu_writel(port, mask, PCIE_MASK_OFF);
283 static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
285 u32 devfn, int where, int size, u32 *val)
287 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
289 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
294 *val = readb_relaxed(conf_data + (where & 3));
297 *val = readw_relaxed(conf_data + (where & 2));
300 *val = readl_relaxed(conf_data);
304 return PCIBIOS_BAD_REGISTER_NUMBER;
307 return PCIBIOS_SUCCESSFUL;
310 static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
312 u32 devfn, int where, int size, u32 val)
314 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
316 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
321 writeb(val, conf_data + (where & 3));
324 writew(val, conf_data + (where & 2));
327 writel(val, conf_data);
330 return PCIBIOS_BAD_REGISTER_NUMBER;
333 return PCIBIOS_SUCCESSFUL;
337 * Remove windows, starting from the largest ones to the smallest
340 static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
341 phys_addr_t base, size_t size)
344 size_t sz = 1 << (fls(size) - 1);
346 mvebu_mbus_del_window(base, sz);
353 * MBus windows can only have a power of two size, but PCI BARs do not
354 * have this constraint. Therefore, we have to split the PCI BAR into
355 * areas each having a power of two size. We start from the largest
356 * one (i.e highest order bit set in the size).
358 static int mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
359 unsigned int target, unsigned int attribute,
360 phys_addr_t base, size_t size,
363 size_t size_mapped = 0;
366 size_t sz = 1 << (fls(size) - 1);
369 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
372 phys_addr_t end = base + sz - 1;
374 dev_err(&port->pcie->pdev->dev,
375 "Could not create MBus window at [mem %pa-%pa]: %d\n",
377 mvebu_pcie_del_windows(port, base - size_mapped,
385 if (remap != MVEBU_MBUS_NO_REMAP)
392 static int mvebu_pcie_set_window(struct mvebu_pcie_port *port,
393 unsigned int target, unsigned int attribute,
394 const struct mvebu_pcie_window *desired,
395 struct mvebu_pcie_window *cur)
399 if (desired->base == cur->base && desired->remap == cur->remap &&
400 desired->size == cur->size)
403 if (cur->size != 0) {
404 mvebu_pcie_del_windows(port, cur->base, cur->size);
409 * If something tries to change the window while it is enabled
410 * the change will not be done atomically. That would be
411 * difficult to do in the general case.
415 if (desired->size == 0)
418 ret = mvebu_pcie_add_windows(port, target, attribute, desired->base,
419 desired->size, desired->remap);
430 static int mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
432 struct mvebu_pcie_window desired = {};
433 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
435 /* Are the new iobase/iolimit values invalid? */
436 if (conf->iolimit < conf->iobase ||
437 conf->iolimitupper < conf->iobaseupper)
438 return mvebu_pcie_set_window(port, port->io_target, port->io_attr,
439 &desired, &port->iowin);
441 if (!mvebu_has_ioport(port)) {
442 dev_WARN(&port->pcie->pdev->dev,
443 "Attempt to set IO when IO is disabled\n");
448 * We read the PCI-to-PCI bridge emulated registers, and
449 * calculate the base address and size of the address decoding
450 * window to setup, according to the PCI-to-PCI bridge
451 * specifications. iobase is the bus address, port->iowin_base
452 * is the CPU address.
454 desired.remap = ((conf->iobase & 0xF0) << 8) |
455 (conf->iobaseupper << 16);
456 desired.base = port->pcie->io.start + desired.remap;
457 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) |
458 (conf->iolimitupper << 16)) -
462 return mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
466 static int mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
468 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
469 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
471 /* Are the new membase/memlimit values invalid? */
472 if (conf->memlimit < conf->membase)
473 return mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
474 &desired, &port->memwin);
477 * We read the PCI-to-PCI bridge emulated registers, and
478 * calculate the base address and size of the address decoding
479 * window to setup, according to the PCI-to-PCI bridge
482 desired.base = ((conf->membase & 0xFFF0) << 16);
483 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) -
486 return mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
490 static pci_bridge_emul_read_status_t
491 mvebu_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
494 struct mvebu_pcie_port *port = bridge->data;
498 *value = mvebu_readl(port, PCIE_CMD_OFF);
501 case PCI_PRIMARY_BUS: {
503 * From the whole 32bit register we support reading from HW only
504 * secondary bus number which is mvebu local bus number.
505 * Other bits are retrieved only from emulated config buffer.
507 __le32 *cfgspace = (__le32 *)&bridge->conf;
508 u32 val = le32_to_cpu(cfgspace[PCI_PRIMARY_BUS / 4]);
510 val |= mvebu_pcie_get_local_bus_nr(port) << 8;
515 case PCI_INTERRUPT_LINE: {
517 * From the whole 32bit register we support reading from HW only
518 * one bit: PCI_BRIDGE_CTL_BUS_RESET.
519 * Other bits are retrieved only from emulated config buffer.
521 __le32 *cfgspace = (__le32 *)&bridge->conf;
522 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
523 if (mvebu_readl(port, PCIE_CTRL_OFF) & PCIE_CTRL_MASTER_HOT_RESET)
524 val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
526 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
532 return PCI_BRIDGE_EMUL_NOT_HANDLED;
535 return PCI_BRIDGE_EMUL_HANDLED;
538 static pci_bridge_emul_read_status_t
539 mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
542 struct mvebu_pcie_port *port = bridge->data;
546 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
550 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
555 * PCIe requires the clock power management capability to be
556 * hard-wired to zero for downstream ports
558 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
559 ~PCI_EXP_LNKCAP_CLKPM;
563 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
567 *value = PCI_EXP_SLTSTA_PDS << 16;
571 *value = mvebu_readl(port, PCIE_RC_RTSTA);
574 case PCI_EXP_DEVCAP2:
575 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP2);
578 case PCI_EXP_DEVCTL2:
579 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL2);
582 case PCI_EXP_LNKCTL2:
583 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL2);
587 return PCI_BRIDGE_EMUL_NOT_HANDLED;
590 return PCI_BRIDGE_EMUL_HANDLED;
594 mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
595 int reg, u32 old, u32 new, u32 mask)
597 struct mvebu_pcie_port *port = bridge->data;
598 struct pci_bridge_emul_conf *conf = &bridge->conf;
602 if (!mvebu_has_ioport(port)) {
603 conf->command = cpu_to_le16(
604 le16_to_cpu(conf->command) & ~PCI_COMMAND_IO);
605 new &= ~PCI_COMMAND_IO;
608 mvebu_writel(port, new, PCIE_CMD_OFF);
612 if ((mask & 0xffff) && mvebu_pcie_handle_iobase_change(port)) {
613 /* On error disable IO range */
614 conf->iobase &= ~0xf0;
615 conf->iolimit &= ~0xf0;
616 conf->iobaseupper = cpu_to_le16(0x0000);
617 conf->iolimitupper = cpu_to_le16(0x0000);
618 if (mvebu_has_ioport(port))
619 conf->iobase |= 0xf0;
623 case PCI_MEMORY_BASE:
624 if (mvebu_pcie_handle_membase_change(port)) {
625 /* On error disable mem range */
626 conf->membase = cpu_to_le16(le16_to_cpu(conf->membase) & ~0xfff0);
627 conf->memlimit = cpu_to_le16(le16_to_cpu(conf->memlimit) & ~0xfff0);
628 conf->membase = cpu_to_le16(le16_to_cpu(conf->membase) | 0xfff0);
632 case PCI_IO_BASE_UPPER16:
633 if (mvebu_pcie_handle_iobase_change(port)) {
634 /* On error disable IO range */
635 conf->iobase &= ~0xf0;
636 conf->iolimit &= ~0xf0;
637 conf->iobaseupper = cpu_to_le16(0x0000);
638 conf->iolimitupper = cpu_to_le16(0x0000);
639 if (mvebu_has_ioport(port))
640 conf->iobase |= 0xf0;
644 case PCI_PRIMARY_BUS:
646 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus);
649 case PCI_INTERRUPT_LINE:
650 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
651 u32 ctrl = mvebu_readl(port, PCIE_CTRL_OFF);
652 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
653 ctrl |= PCIE_CTRL_MASTER_HOT_RESET;
655 ctrl &= ~PCIE_CTRL_MASTER_HOT_RESET;
656 mvebu_writel(port, ctrl, PCIE_CTRL_OFF);
666 mvebu_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
667 int reg, u32 old, u32 new, u32 mask)
669 struct mvebu_pcie_port *port = bridge->data;
673 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
678 * If we don't support CLKREQ, we must ensure that the
679 * CLKREQ enable bit always reads zero. Since we haven't
680 * had this capability, and it's dependent on board wiring,
681 * disable it for the time being.
683 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
685 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
690 * PME Status bit in Root Status Register (PCIE_RC_RTSTA)
691 * is read-only and can be cleared only by writing 0b to the
692 * Interrupt Cause RW0C register (PCIE_INT_CAUSE_OFF). So
693 * clear PME via Interrupt Cause.
695 if (new & PCI_EXP_RTSTA_PME)
696 mvebu_writel(port, ~PCIE_INT_PM_PME, PCIE_INT_CAUSE_OFF);
699 case PCI_EXP_DEVCTL2:
700 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL2);
703 case PCI_EXP_LNKCTL2:
704 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL2);
712 static struct pci_bridge_emul_ops mvebu_pci_bridge_emul_ops = {
713 .read_base = mvebu_pci_bridge_emul_base_conf_read,
714 .write_base = mvebu_pci_bridge_emul_base_conf_write,
715 .read_pcie = mvebu_pci_bridge_emul_pcie_conf_read,
716 .write_pcie = mvebu_pci_bridge_emul_pcie_conf_write,
720 * Initialize the configuration space of the PCI-to-PCI bridge
721 * associated with the given PCIe interface.
723 static int mvebu_pci_bridge_emul_init(struct mvebu_pcie_port *port)
725 struct pci_bridge_emul *bridge = &port->bridge;
726 u32 pcie_cap = mvebu_readl(port, PCIE_CAP_PCIEXP);
727 u8 pcie_cap_ver = ((pcie_cap >> 16) & PCI_EXP_FLAGS_VERS);
729 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL;
730 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
731 bridge->conf.class_revision =
732 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
734 if (mvebu_has_ioport(port)) {
735 /* We support 32 bits I/O addressing */
736 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
737 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
741 * Older mvebu hardware provides PCIe Capability structure only in
742 * version 1. New hardware provides it in version 2.
744 bridge->pcie_conf.cap = cpu_to_le16(pcie_cap_ver);
746 bridge->has_pcie = true;
748 bridge->ops = &mvebu_pci_bridge_emul_ops;
750 return pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR);
753 static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
755 return sys->private_data;
758 static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
764 for (i = 0; i < pcie->nports; i++) {
765 struct mvebu_pcie_port *port = &pcie->ports[i];
770 if (bus->number == 0 && port->devfn == devfn)
772 if (bus->number != 0 &&
773 bus->number >= port->bridge.conf.secondary_bus &&
774 bus->number <= port->bridge.conf.subordinate_bus)
781 /* PCI configuration space write function */
782 static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
783 int where, int size, u32 val)
785 struct mvebu_pcie *pcie = bus->sysdata;
786 struct mvebu_pcie_port *port;
789 port = mvebu_pcie_find_port(pcie, bus, devfn);
791 return PCIBIOS_DEVICE_NOT_FOUND;
793 /* Access the emulated PCI-to-PCI bridge */
794 if (bus->number == 0)
795 return pci_bridge_emul_conf_write(&port->bridge, where,
798 if (!mvebu_pcie_link_up(port))
799 return PCIBIOS_DEVICE_NOT_FOUND;
801 /* Access the real PCIe interface */
802 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
808 /* PCI configuration space read function */
809 static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
812 struct mvebu_pcie *pcie = bus->sysdata;
813 struct mvebu_pcie_port *port;
816 port = mvebu_pcie_find_port(pcie, bus, devfn);
818 return PCIBIOS_DEVICE_NOT_FOUND;
820 /* Access the emulated PCI-to-PCI bridge */
821 if (bus->number == 0)
822 return pci_bridge_emul_conf_read(&port->bridge, where,
825 if (!mvebu_pcie_link_up(port))
826 return PCIBIOS_DEVICE_NOT_FOUND;
828 /* Access the real PCIe interface */
829 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
835 static struct pci_ops mvebu_pcie_ops = {
836 .read = mvebu_pcie_rd_conf,
837 .write = mvebu_pcie_wr_conf,
840 static int mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
842 /* Interrupt support on mvebu emulated bridges is not implemented yet */
843 if (dev->bus->number == 0)
844 return 0; /* Proper return code 0 == NO_IRQ */
846 return of_irq_parse_and_map_pci(dev, slot, pin);
849 static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
850 const struct resource *res,
851 resource_size_t start,
852 resource_size_t size,
853 resource_size_t align)
855 if (dev->bus->number != 0)
859 * On the PCI-to-PCI bridge side, the I/O windows must have at
860 * least a 64 KB size and the memory windows must have at
861 * least a 1 MB size. Moreover, MBus windows need to have a
862 * base address aligned on their size, and their size must be
863 * a power of two. This means that if the BAR doesn't have a
864 * power of two size, several MBus windows will actually be
865 * created. We need to ensure that the biggest MBus window
866 * (which will be the first one) is aligned on its size, which
867 * explains the rounddown_pow_of_two() being done here.
869 if (res->flags & IORESOURCE_IO)
870 return round_up(start, max_t(resource_size_t, SZ_64K,
871 rounddown_pow_of_two(size)));
872 else if (res->flags & IORESOURCE_MEM)
873 return round_up(start, max_t(resource_size_t, SZ_1M,
874 rounddown_pow_of_two(size)));
879 static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
880 struct device_node *np,
881 struct mvebu_pcie_port *port)
885 ret = of_address_to_resource(np, 0, &port->regs);
887 return (void __iomem *)ERR_PTR(ret);
889 return devm_ioremap_resource(&pdev->dev, &port->regs);
892 #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
893 #define DT_TYPE_IO 0x1
894 #define DT_TYPE_MEM32 0x2
895 #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
896 #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
898 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
903 const int na = 3, ns = 2;
905 int rlen, nranges, rangesz, pna, i;
910 range = of_get_property(np, "ranges", &rlen);
914 pna = of_n_addr_cells(np);
915 rangesz = pna + na + ns;
916 nranges = rlen / sizeof(__be32) / rangesz;
918 for (i = 0; i < nranges; i++, range += rangesz) {
919 u32 flags = of_read_number(range, 1);
920 u32 slot = of_read_number(range + 1, 1);
921 u64 cpuaddr = of_read_number(range + na, pna);
924 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
925 rtype = IORESOURCE_IO;
926 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
927 rtype = IORESOURCE_MEM;
931 if (slot == PCI_SLOT(devfn) && type == rtype) {
932 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
933 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
941 #ifdef CONFIG_PM_SLEEP
942 static int mvebu_pcie_suspend(struct device *dev)
944 struct mvebu_pcie *pcie;
947 pcie = dev_get_drvdata(dev);
948 for (i = 0; i < pcie->nports; i++) {
949 struct mvebu_pcie_port *port = pcie->ports + i;
952 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
958 static int mvebu_pcie_resume(struct device *dev)
960 struct mvebu_pcie *pcie;
963 pcie = dev_get_drvdata(dev);
964 for (i = 0; i < pcie->nports; i++) {
965 struct mvebu_pcie_port *port = pcie->ports + i;
968 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
969 mvebu_pcie_setup_hw(port);
976 static void mvebu_pcie_port_clk_put(void *data)
978 struct mvebu_pcie_port *port = data;
983 static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
984 struct mvebu_pcie_port *port, struct device_node *child)
986 struct device *dev = &pcie->pdev->dev;
987 enum of_gpio_flags flags;
992 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
993 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
998 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
1001 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
1008 port->devfn = of_pci_get_devfn(child);
1009 if (port->devfn < 0)
1011 if (PCI_FUNC(port->devfn) != 0) {
1012 dev_err(dev, "%s: invalid function number, must be zero\n",
1017 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
1018 &port->mem_target, &port->mem_attr);
1020 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
1025 if (resource_size(&pcie->io) != 0) {
1026 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
1027 &port->io_target, &port->io_attr);
1029 port->io_target = -1;
1033 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
1034 if (reset_gpio == -EPROBE_DEFER) {
1039 if (gpio_is_valid(reset_gpio)) {
1040 unsigned long gpio_flags;
1042 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
1044 if (!port->reset_name) {
1049 if (flags & OF_GPIO_ACTIVE_LOW) {
1050 dev_info(dev, "%pOF: reset gpio is active low\n",
1052 gpio_flags = GPIOF_ACTIVE_LOW |
1055 gpio_flags = GPIOF_OUT_INIT_HIGH;
1058 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
1061 if (ret == -EPROBE_DEFER)
1066 port->reset_gpio = gpio_to_desc(reset_gpio);
1069 port->clk = of_clk_get_by_name(child, NULL);
1070 if (IS_ERR(port->clk)) {
1071 dev_err(dev, "%s: cannot get clock\n", port->name);
1075 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
1086 /* In the case of skipping, we need to free these */
1087 devm_kfree(dev, port->reset_name);
1088 port->reset_name = NULL;
1089 devm_kfree(dev, port->name);
1097 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
1098 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
1099 * of the PCI Express Card Electromechanical Specification, 1.1.
1101 static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
1105 ret = clk_prepare_enable(port->clk);
1109 if (port->reset_gpio) {
1110 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
1112 of_property_read_u32(port->dn, "reset-delay-us",
1117 gpiod_set_value_cansleep(port->reset_gpio, 0);
1118 msleep(reset_udelay / 1000);
1125 * Power down a PCIe port. Strictly, PCIe requires us to place the card
1126 * in D3hot state before asserting PERST#.
1128 static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
1130 gpiod_set_value_cansleep(port->reset_gpio, 1);
1132 clk_disable_unprepare(port->clk);
1136 * devm_of_pci_get_host_bridge_resources() only sets up translateable resources,
1137 * so we need extra resource setup parsing our special DT properties encoding
1138 * the MEM and IO apertures.
1140 static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
1142 struct device *dev = &pcie->pdev->dev;
1143 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1146 /* Get the PCIe memory aperture */
1147 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
1148 if (resource_size(&pcie->mem) == 0) {
1149 dev_err(dev, "invalid memory aperture size\n");
1153 pcie->mem.name = "PCI MEM";
1154 pci_add_resource(&bridge->windows, &pcie->mem);
1155 ret = devm_request_resource(dev, &iomem_resource, &pcie->mem);
1159 /* Get the PCIe IO aperture */
1160 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
1162 if (resource_size(&pcie->io) != 0) {
1163 pcie->realio.flags = pcie->io.flags;
1164 pcie->realio.start = PCIBIOS_MIN_IO;
1165 pcie->realio.end = min_t(resource_size_t,
1166 IO_SPACE_LIMIT - SZ_64K,
1167 resource_size(&pcie->io) - 1);
1168 pcie->realio.name = "PCI I/O";
1170 ret = devm_pci_remap_iospace(dev, &pcie->realio, pcie->io.start);
1174 pci_add_resource(&bridge->windows, &pcie->realio);
1175 ret = devm_request_resource(dev, &ioport_resource, &pcie->realio);
1183 static int mvebu_pcie_probe(struct platform_device *pdev)
1185 struct device *dev = &pdev->dev;
1186 struct mvebu_pcie *pcie;
1187 struct pci_host_bridge *bridge;
1188 struct device_node *np = dev->of_node;
1189 struct device_node *child;
1192 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
1196 pcie = pci_host_bridge_priv(bridge);
1198 platform_set_drvdata(pdev, pcie);
1200 ret = mvebu_pcie_parse_request_resources(pcie);
1204 num = of_get_available_child_count(np);
1206 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
1211 for_each_available_child_of_node(np, child) {
1212 struct mvebu_pcie_port *port = &pcie->ports[i];
1214 ret = mvebu_pcie_parse_port(pcie, port, child);
1218 } else if (ret == 0) {
1227 for (i = 0; i < pcie->nports; i++) {
1228 struct mvebu_pcie_port *port = &pcie->ports[i];
1234 ret = mvebu_pcie_powerup(port);
1238 port->base = mvebu_pcie_map_registers(pdev, child, port);
1239 if (IS_ERR(port->base)) {
1240 dev_err(dev, "%s: cannot map registers\n", port->name);
1242 mvebu_pcie_powerdown(port);
1246 ret = mvebu_pci_bridge_emul_init(port);
1248 dev_err(dev, "%s: cannot init emulated bridge\n",
1250 devm_iounmap(dev, port->base);
1252 mvebu_pcie_powerdown(port);
1257 * PCIe topology exported by mvebu hw is quite complicated. In
1258 * reality has something like N fully independent host bridges
1259 * where each host bridge has one PCIe Root Port (which acts as
1260 * PCI Bridge device). Each host bridge has its own independent
1261 * internal registers, independent access to PCI config space,
1262 * independent interrupt lines, independent window and memory
1263 * access configuration. But additionally there is some kind of
1264 * peer-to-peer support between PCIe devices behind different
1265 * host bridges limited just to forwarding of memory and I/O
1266 * transactions (forwarding of error messages and config cycles
1267 * is not supported). So we could say there are N independent
1268 * PCIe Root Complexes.
1270 * For this kind of setup DT should have been structured into
1271 * N independent PCIe controllers / host bridges. But instead
1272 * structure in past was defined to put PCIe Root Ports of all
1273 * host bridges into one bus zero, like in classic multi-port
1274 * Root Complex setup with just one host bridge.
1276 * This means that pci-mvebu.c driver provides "virtual" bus 0
1277 * on which registers all PCIe Root Ports (PCI Bridge devices)
1278 * specified in DT by their BDF addresses and virtually routes
1279 * PCI config access of each PCI bridge device to specific PCIe
1282 * Normally PCI Bridge should choose between Type 0 and Type 1
1283 * config requests based on primary and secondary bus numbers
1284 * configured on the bridge itself. But because mvebu PCI Bridge
1285 * does not have registers for primary and secondary bus numbers
1286 * in its config space, it determinates type of config requests
1287 * via its own custom way.
1289 * There are two options how mvebu determinate type of config
1292 * 1. If Secondary Bus Number Enable bit is not set or is not
1293 * available (applies for pre-XP PCIe controllers) then Type 0
1294 * is used if target bus number equals Local Bus Number (bits
1295 * [15:8] in register 0x1a04) and target device number differs
1296 * from Local Device Number (bits [20:16] in register 0x1a04).
1297 * Type 1 is used if target bus number differs from Local Bus
1298 * Number. And when target bus number equals Local Bus Number
1299 * and target device equals Local Device Number then request is
1300 * routed to Local PCI Bridge (PCIe Root Port).
1302 * 2. If Secondary Bus Number Enable bit is set (bit 7 in
1303 * register 0x1a2c) then mvebu hw determinate type of config
1304 * request like compliant PCI Bridge based on primary bus number
1305 * which is configured via Local Bus Number (bits [15:8] in
1306 * register 0x1a04) and secondary bus number which is configured
1307 * via Secondary Bus Number (bits [7:0] in register 0x1a2c).
1308 * Local PCI Bridge (PCIe Root Port) is available on primary bus
1309 * as device with Local Device Number (bits [20:16] in register
1312 * Secondary Bus Number Enable bit is disabled by default and
1313 * option 2. is not available on pre-XP PCIe controllers. Hence
1314 * this driver always use option 1.
1316 * Basically it means that primary and secondary buses shares
1317 * one virtual number configured via Local Bus Number bits and
1318 * Local Device Number bits determinates if accessing primary
1319 * or secondary bus. Set Local Device Number to 1 and redirect
1320 * all writes of PCI Bridge Secondary Bus Number register to
1321 * Local Bus Number (bits [15:8] in register 0x1a04).
1323 * So when accessing devices on buses behind secondary bus
1324 * number it would work correctly. And also when accessing
1325 * device 0 at secondary bus number via config space would be
1326 * correctly routed to secondary bus. Due to issues described
1327 * in mvebu_pcie_setup_hw(), PCI Bridges at primary bus (zero)
1328 * are not accessed directly via PCI config space but rarher
1329 * indirectly via kernel emulated PCI bridge driver.
1331 mvebu_pcie_setup_hw(port);
1332 mvebu_pcie_set_local_dev_nr(port, 0);
1337 bridge->sysdata = pcie;
1338 bridge->ops = &mvebu_pcie_ops;
1339 bridge->align_resource = mvebu_pcie_align_resource;
1340 bridge->map_irq = mvebu_pcie_map_irq;
1342 return pci_host_probe(bridge);
1345 static int mvebu_pcie_remove(struct platform_device *pdev)
1347 struct mvebu_pcie *pcie = platform_get_drvdata(pdev);
1348 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1352 /* Remove PCI bus with all devices. */
1353 pci_lock_rescan_remove();
1354 pci_stop_root_bus(bridge->bus);
1355 pci_remove_root_bus(bridge->bus);
1356 pci_unlock_rescan_remove();
1358 for (i = 0; i < pcie->nports; i++) {
1359 struct mvebu_pcie_port *port = &pcie->ports[i];
1364 /* Disable Root Bridge I/O space, memory space and bus mastering. */
1365 cmd = mvebu_readl(port, PCIE_CMD_OFF);
1366 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1367 mvebu_writel(port, cmd, PCIE_CMD_OFF);
1369 /* Mask all interrupt sources. */
1370 mvebu_writel(port, 0, PCIE_MASK_OFF);
1372 /* Free config space for emulated root bridge. */
1373 pci_bridge_emul_cleanup(&port->bridge);
1375 /* Disable and clear BARs and windows. */
1376 mvebu_pcie_disable_wins(port);
1378 /* Delete PCIe IO and MEM windows. */
1379 if (port->iowin.size)
1380 mvebu_pcie_del_windows(port, port->iowin.base, port->iowin.size);
1381 if (port->memwin.size)
1382 mvebu_pcie_del_windows(port, port->memwin.base, port->memwin.size);
1384 /* Power down card and disable clocks. Must be the last step. */
1385 mvebu_pcie_powerdown(port);
1391 static const struct of_device_id mvebu_pcie_of_match_table[] = {
1392 { .compatible = "marvell,armada-xp-pcie", },
1393 { .compatible = "marvell,armada-370-pcie", },
1394 { .compatible = "marvell,dove-pcie", },
1395 { .compatible = "marvell,kirkwood-pcie", },
1399 static const struct dev_pm_ops mvebu_pcie_pm_ops = {
1400 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
1403 static struct platform_driver mvebu_pcie_driver = {
1405 .name = "mvebu-pcie",
1406 .of_match_table = mvebu_pcie_of_match_table,
1407 .pm = &mvebu_pcie_pm_ops,
1409 .probe = mvebu_pcie_probe,
1410 .remove = mvebu_pcie_remove,
1412 module_platform_driver(mvebu_pcie_driver);
1416 MODULE_DESCRIPTION("Marvell EBU PCIe controller");
1417 MODULE_LICENSE("GPL v2");