1 From cf7b5cb15e46b5357c60188b75b213a7f0b5fd32 Mon Sep 17 00:00:00 2001
2 From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
3 Date: Fri, 18 Jan 2013 17:42:58 +0100
4 Subject: [PATCH 009/203] pci: PCIe driver for Marvell Armada 370/XP systems
6 This driver implements the support for the PCIe interfaces on the
7 Marvell Armada 370/XP ARM SoCs. In the future, it might be extended to
8 cover earlier families of Marvell SoCs, such as Dove, Orion and
11 The driver implements the hw_pci operations needed by the core ARM PCI
12 code to setup PCI devices and get their corresponding IRQs, and the
13 pci_ops operations that are used by the PCI core to read/write the
14 configuration space of PCI devices.
16 Since the PCIe interfaces of Marvell SoCs are completely separate and
17 not linked together in a bus, this driver sets up an emulated PCI host
18 bridge, with one PCI-to-PCI bridge as child for each hardware PCIe
21 In addition, this driver enumerates the different PCIe slots, and for
22 those having a device plugged in, it sets up the necessary address
23 decoding windows, using the mvebu-mbus driver.
25 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
26 Acked-by: Bjorn Helgaas <bhelgaas@google.com>
28 .../devicetree/bindings/pci/mvebu-pci.txt | 220 ++++++
29 drivers/pci/Kconfig | 2 +
30 drivers/pci/Makefile | 3 +
31 drivers/pci/host/Kconfig | 8 +
32 drivers/pci/host/Makefile | 1 +
33 drivers/pci/host/pci-mvebu.c | 880 +++++++++++++++++++++
34 6 files changed, 1114 insertions(+)
35 create mode 100644 Documentation/devicetree/bindings/pci/mvebu-pci.txt
36 create mode 100644 drivers/pci/host/Kconfig
37 create mode 100644 drivers/pci/host/Makefile
38 create mode 100644 drivers/pci/host/pci-mvebu.c
41 +++ b/Documentation/devicetree/bindings/pci/mvebu-pci.txt
43 +* Marvell EBU PCIe interfaces
45 +Mandatory properties:
46 +- compatible: one of the following values:
47 + marvell,armada-370-pcie
48 + marvell,armada-xp-pcie
49 +- #address-cells, set to <3>
50 +- #size-cells, set to <2>
51 +- #interrupt-cells, set to <1>
52 +- bus-range: PCI bus numbers covered
53 +- device_type, set to "pci"
54 +- ranges: ranges for the PCI memory and I/O regions, as well as the
55 + MMIO registers to control the PCIe interfaces.
57 +In addition, the Device Tree node must have sub-nodes describing each
58 +PCIe interface, having the following mandatory properties:
59 +- reg: used only for interrupt mapping, so only the first four bytes
60 + are used to refer to the correct bus number and device number.
61 +- assigned-addresses: reference to the MMIO registers used to control
62 + this PCIe interface.
63 +- clocks: the clock associated to this PCIe interface
64 +- marvell,pcie-port: the physical PCIe port number
65 +- status: either "disabled" or "okay"
66 +- device_type, set to "pci"
67 +- #address-cells, set to <3>
68 +- #size-cells, set to <2>
69 +- #interrupt-cells, set to <1>
70 +- ranges, empty property.
71 +- interrupt-map-mask and interrupt-map, standard PCI properties to
72 + define the mapping of the PCIe interface to interrupt numbers.
74 +and the following optional properties:
75 +- marvell,pcie-lane: the physical PCIe lane number, for ports having
76 + multiple lanes. If this property is not found, we assume that the
82 + compatible = "marvell,armada-xp-pcie";
83 + status = "disabled";
84 + device_type = "pci";
86 + #address-cells = <3>;
89 + bus-range = <0x00 0xff>;
91 + ranges = <0x82000000 0 0xd0040000 0xd0040000 0 0x00002000 /* Port 0.0 registers */
92 + 0x82000000 0 0xd0042000 0xd0042000 0 0x00002000 /* Port 2.0 registers */
93 + 0x82000000 0 0xd0044000 0xd0044000 0 0x00002000 /* Port 0.1 registers */
94 + 0x82000000 0 0xd0048000 0xd0048000 0 0x00002000 /* Port 0.2 registers */
95 + 0x82000000 0 0xd004c000 0xd004c000 0 0x00002000 /* Port 0.3 registers */
96 + 0x82000000 0 0xd0080000 0xd0080000 0 0x00002000 /* Port 1.0 registers */
97 + 0x82000000 0 0xd0082000 0xd0082000 0 0x00002000 /* Port 3.0 registers */
98 + 0x82000000 0 0xd0084000 0xd0084000 0 0x00002000 /* Port 1.1 registers */
99 + 0x82000000 0 0xd0088000 0xd0088000 0 0x00002000 /* Port 1.2 registers */
100 + 0x82000000 0 0xd008c000 0xd008c000 0 0x00002000 /* Port 1.3 registers */
101 + 0x82000000 0 0xe0000000 0xe0000000 0 0x08000000 /* non-prefetchable memory */
102 + 0x81000000 0 0 0xe8000000 0 0x00100000>; /* downstream I/O */
105 + device_type = "pci";
106 + assigned-addresses = <0x82000800 0 0xd0040000 0 0x2000>;
107 + reg = <0x0800 0 0 0 0>;
108 + #address-cells = <3>;
110 + #interrupt-cells = <1>;
112 + interrupt-map-mask = <0 0 0 0>;
113 + interrupt-map = <0 0 0 0 &mpic 58>;
114 + marvell,pcie-port = <0>;
115 + marvell,pcie-lane = <0>;
116 + clocks = <&gateclk 5>;
117 + status = "disabled";
121 + device_type = "pci";
122 + assigned-addresses = <0x82001000 0 0xd0044000 0 0x2000>;
123 + reg = <0x1000 0 0 0 0>;
124 + #address-cells = <3>;
126 + #interrupt-cells = <1>;
128 + interrupt-map-mask = <0 0 0 0>;
129 + interrupt-map = <0 0 0 0 &mpic 59>;
130 + marvell,pcie-port = <0>;
131 + marvell,pcie-lane = <1>;
132 + clocks = <&gateclk 6>;
133 + status = "disabled";
137 + device_type = "pci";
138 + assigned-addresses = <0x82001800 0 0xd0048000 0 0x2000>;
139 + reg = <0x1800 0 0 0 0>;
140 + #address-cells = <3>;
142 + #interrupt-cells = <1>;
144 + interrupt-map-mask = <0 0 0 0>;
145 + interrupt-map = <0 0 0 0 &mpic 60>;
146 + marvell,pcie-port = <0>;
147 + marvell,pcie-lane = <2>;
148 + clocks = <&gateclk 7>;
149 + status = "disabled";
153 + device_type = "pci";
154 + assigned-addresses = <0x82002000 0 0xd004c000 0 0x2000>;
155 + reg = <0x2000 0 0 0 0>;
156 + #address-cells = <3>;
158 + #interrupt-cells = <1>;
160 + interrupt-map-mask = <0 0 0 0>;
161 + interrupt-map = <0 0 0 0 &mpic 61>;
162 + marvell,pcie-port = <0>;
163 + marvell,pcie-lane = <3>;
164 + clocks = <&gateclk 8>;
165 + status = "disabled";
169 + device_type = "pci";
170 + assigned-addresses = <0x82002800 0 0xd0080000 0 0x2000>;
171 + reg = <0x2800 0 0 0 0>;
172 + #address-cells = <3>;
174 + #interrupt-cells = <1>;
176 + interrupt-map-mask = <0 0 0 0>;
177 + interrupt-map = <0 0 0 0 &mpic 62>;
178 + marvell,pcie-port = <1>;
179 + marvell,pcie-lane = <0>;
180 + clocks = <&gateclk 9>;
181 + status = "disabled";
185 + device_type = "pci";
186 + assigned-addresses = <0x82003000 0 0xd0084000 0 0x2000>;
187 + reg = <0x3000 0 0 0 0>;
188 + #address-cells = <3>;
190 + #interrupt-cells = <1>;
192 + interrupt-map-mask = <0 0 0 0>;
193 + interrupt-map = <0 0 0 0 &mpic 63>;
194 + marvell,pcie-port = <1>;
195 + marvell,pcie-lane = <1>;
196 + clocks = <&gateclk 10>;
197 + status = "disabled";
201 + device_type = "pci";
202 + assigned-addresses = <0x82003800 0 0xd0088000 0 0x2000>;
203 + reg = <0x3800 0 0 0 0>;
204 + #address-cells = <3>;
206 + #interrupt-cells = <1>;
208 + interrupt-map-mask = <0 0 0 0>;
209 + interrupt-map = <0 0 0 0 &mpic 64>;
210 + marvell,pcie-port = <1>;
211 + marvell,pcie-lane = <2>;
212 + clocks = <&gateclk 11>;
213 + status = "disabled";
217 + device_type = "pci";
218 + assigned-addresses = <0x82004000 0 0xd008c000 0 0x2000>;
219 + reg = <0x4000 0 0 0 0>;
220 + #address-cells = <3>;
222 + #interrupt-cells = <1>;
224 + interrupt-map-mask = <0 0 0 0>;
225 + interrupt-map = <0 0 0 0 &mpic 65>;
226 + marvell,pcie-port = <1>;
227 + marvell,pcie-lane = <3>;
228 + clocks = <&gateclk 12>;
229 + status = "disabled";
232 + device_type = "pci";
233 + assigned-addresses = <0x82004800 0 0xd0042000 0 0x2000>;
234 + reg = <0x4800 0 0 0 0>;
235 + #address-cells = <3>;
237 + #interrupt-cells = <1>;
239 + interrupt-map-mask = <0 0 0 0>;
240 + interrupt-map = <0 0 0 0 &mpic 99>;
241 + marvell,pcie-port = <2>;
242 + marvell,pcie-lane = <0>;
243 + clocks = <&gateclk 26>;
244 + status = "disabled";
248 + device_type = "pci";
249 + assigned-addresses = <0x82005000 0 0xd0082000 0 0x2000>;
250 + reg = <0x5000 0 0 0 0>;
251 + #address-cells = <3>;
253 + #interrupt-cells = <1>;
255 + interrupt-map-mask = <0 0 0 0>;
256 + interrupt-map = <0 0 0 0 &mpic 103>;
257 + marvell,pcie-port = <3>;
258 + marvell,pcie-lane = <0>;
259 + clocks = <&gateclk 27>;
260 + status = "disabled";
263 --- a/drivers/pci/Kconfig
264 +++ b/drivers/pci/Kconfig
265 @@ -125,3 +125,5 @@ config PCI_IOAPIC
267 def_bool y if (DMI || ACPI)
270 +source "drivers/pci/host/Kconfig"
271 --- a/drivers/pci/Makefile
272 +++ b/drivers/pci/Makefile
273 @@ -67,3 +67,6 @@ obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen
274 obj-$(CONFIG_OF) += of.o
276 ccflags-$(CONFIG_PCI_DEBUG) := -DDEBUG
278 +# PCI host controller drivers
281 +++ b/drivers/pci/host/Kconfig
283 +menu "PCI host controller drivers"
287 + bool "Marvell EBU PCIe controller"
288 + depends on ARCH_MVEBU
292 +++ b/drivers/pci/host/Makefile
294 +obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o
296 +++ b/drivers/pci/host/pci-mvebu.c
299 + * PCIe driver for Marvell Armada 370 and Armada XP SoCs
301 + * This file is licensed under the terms of the GNU General Public
302 + * License version 2. This program is licensed "as is" without any
303 + * warranty of any kind, whether express or implied.
306 +#include <linux/kernel.h>
307 +#include <linux/pci.h>
308 +#include <linux/clk.h>
309 +#include <linux/module.h>
310 +#include <linux/mbus.h>
311 +#include <linux/slab.h>
312 +#include <linux/platform_device.h>
313 +#include <linux/of_address.h>
314 +#include <linux/of_pci.h>
315 +#include <linux/of_irq.h>
316 +#include <linux/of_platform.h>
319 + * PCIe unit register offsets.
321 +#define PCIE_DEV_ID_OFF 0x0000
322 +#define PCIE_CMD_OFF 0x0004
323 +#define PCIE_DEV_REV_OFF 0x0008
324 +#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
325 +#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
326 +#define PCIE_HEADER_LOG_4_OFF 0x0128
327 +#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
328 +#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
329 +#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
330 +#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
331 +#define PCIE_WIN5_CTRL_OFF 0x1880
332 +#define PCIE_WIN5_BASE_OFF 0x1884
333 +#define PCIE_WIN5_REMAP_OFF 0x188c
334 +#define PCIE_CONF_ADDR_OFF 0x18f8
335 +#define PCIE_CONF_ADDR_EN 0x80000000
336 +#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
337 +#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
338 +#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
339 +#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
340 +#define PCIE_CONF_ADDR(bus, devfn, where) \
341 + (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
342 + PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
344 +#define PCIE_CONF_DATA_OFF 0x18fc
345 +#define PCIE_MASK_OFF 0x1910
346 +#define PCIE_MASK_ENABLE_INTS 0x0f000000
347 +#define PCIE_CTRL_OFF 0x1a00
348 +#define PCIE_CTRL_X1_MODE 0x0001
349 +#define PCIE_STAT_OFF 0x1a04
350 +#define PCIE_STAT_BUS 0xff00
351 +#define PCIE_STAT_LINK_DOWN BIT(0)
352 +#define PCIE_DEBUG_CTRL 0x1a60
353 +#define PCIE_DEBUG_SOFT_RESET BIT(20)
356 + * This product ID is registered by Marvell, and used when the Marvell
357 + * SoC is not the root complex, but an endpoint on the PCIe bus. It is
358 + * therefore safe to re-use this PCI ID for our emulated PCI-to-PCI
361 +#define MARVELL_EMULATED_PCI_PCI_BRIDGE_ID 0x7846
363 +/* PCI configuration space of a PCI-to-PCI bridge */
364 +struct mvebu_sw_pci_bridge {
375 + u8 cache_line_size;
379 + u8 subordinate_bus;
380 + u8 secondary_latency_timer;
383 + u16 secondary_status;
389 + u32 preflimitupper;
401 +struct mvebu_pcie_port;
403 +/* Structure representing all PCIe interfaces */
405 + struct platform_device *pdev;
406 + struct mvebu_pcie_port *ports;
407 + struct resource io;
408 + struct resource realio;
409 + struct resource mem;
410 + struct resource busn;
414 +/* Structure representing one PCIe interface */
415 +struct mvebu_pcie_port {
417 + void __iomem *base;
418 + spinlock_t conf_lock;
424 + struct mvebu_sw_pci_bridge bridge;
425 + struct device_node *dn;
426 + struct mvebu_pcie *pcie;
427 + phys_addr_t memwin_base;
428 + size_t memwin_size;
429 + phys_addr_t iowin_base;
433 +static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
435 + return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
438 +static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
442 + stat = readl(port->base + PCIE_STAT_OFF);
443 + stat &= ~PCIE_STAT_BUS;
445 + writel(stat, port->base + PCIE_STAT_OFF);
449 + * Setup PCIE BARs and Address Decode Wins:
450 + * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
451 + * WIN[0-3] -> DRAM bank[0-3]
453 +static void __init mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
455 + const struct mbus_dram_target_info *dram;
459 + dram = mv_mbus_dram_info();
461 + /* First, disable and clear BARs and windows. */
462 + for (i = 1; i < 3; i++) {
463 + writel(0, port->base + PCIE_BAR_CTRL_OFF(i));
464 + writel(0, port->base + PCIE_BAR_LO_OFF(i));
465 + writel(0, port->base + PCIE_BAR_HI_OFF(i));
468 + for (i = 0; i < 5; i++) {
469 + writel(0, port->base + PCIE_WIN04_CTRL_OFF(i));
470 + writel(0, port->base + PCIE_WIN04_BASE_OFF(i));
471 + writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
474 + writel(0, port->base + PCIE_WIN5_CTRL_OFF);
475 + writel(0, port->base + PCIE_WIN5_BASE_OFF);
476 + writel(0, port->base + PCIE_WIN5_REMAP_OFF);
478 + /* Setup windows for DDR banks. Count total DDR size on the fly. */
480 + for (i = 0; i < dram->num_cs; i++) {
481 + const struct mbus_dram_window *cs = dram->cs + i;
483 + writel(cs->base & 0xffff0000,
484 + port->base + PCIE_WIN04_BASE_OFF(i));
485 + writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
486 + writel(((cs->size - 1) & 0xffff0000) |
487 + (cs->mbus_attr << 8) |
488 + (dram->mbus_dram_target_id << 4) | 1,
489 + port->base + PCIE_WIN04_CTRL_OFF(i));
494 + /* Round up 'size' to the nearest power of two. */
495 + if ((size & (size - 1)) != 0)
496 + size = 1 << fls(size);
498 + /* Setup BAR[1] to all DRAM banks. */
499 + writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1));
500 + writel(0, port->base + PCIE_BAR_HI_OFF(1));
501 + writel(((size - 1) & 0xffff0000) | 1,
502 + port->base + PCIE_BAR_CTRL_OFF(1));
505 +static void __init mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
510 + /* Point PCIe unit MBUS decode windows to DRAM space. */
511 + mvebu_pcie_setup_wins(port);
513 + /* Master + slave enable. */
514 + cmd = readw(port->base + PCIE_CMD_OFF);
515 + cmd |= PCI_COMMAND_IO;
516 + cmd |= PCI_COMMAND_MEMORY;
517 + cmd |= PCI_COMMAND_MASTER;
518 + writew(cmd, port->base + PCIE_CMD_OFF);
520 + /* Enable interrupt lines A-D. */
521 + mask = readl(port->base + PCIE_MASK_OFF);
522 + mask |= PCIE_MASK_ENABLE_INTS;
523 + writel(mask, port->base + PCIE_MASK_OFF);
526 +static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
527 + struct pci_bus *bus,
528 + u32 devfn, int where, int size, u32 *val)
530 + writel(PCIE_CONF_ADDR(bus->number, devfn, where),
531 + port->base + PCIE_CONF_ADDR_OFF);
533 + *val = readl(port->base + PCIE_CONF_DATA_OFF);
536 + *val = (*val >> (8 * (where & 3))) & 0xff;
537 + else if (size == 2)
538 + *val = (*val >> (8 * (where & 3))) & 0xffff;
540 + return PCIBIOS_SUCCESSFUL;
543 +static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
544 + struct pci_bus *bus,
545 + u32 devfn, int where, int size, u32 val)
547 + int ret = PCIBIOS_SUCCESSFUL;
549 + writel(PCIE_CONF_ADDR(bus->number, devfn, where),
550 + port->base + PCIE_CONF_ADDR_OFF);
553 + writel(val, port->base + PCIE_CONF_DATA_OFF);
554 + else if (size == 2)
555 + writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
556 + else if (size == 1)
557 + writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
559 + ret = PCIBIOS_BAD_REGISTER_NUMBER;
564 +static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
566 + phys_addr_t iobase;
568 + /* Are the new iobase/iolimit values invalid? */
569 + if (port->bridge.iolimit < port->bridge.iobase ||
570 + port->bridge.iolimitupper < port->bridge.iobaseupper) {
572 + /* If a window was configured, remove it */
573 + if (port->iowin_base) {
574 + mvebu_mbus_del_window(port->iowin_base,
576 + port->iowin_base = 0;
577 + port->iowin_size = 0;
584 + * We read the PCI-to-PCI bridge emulated registers, and
585 + * calculate the base address and size of the address decoding
586 + * window to setup, according to the PCI-to-PCI bridge
587 + * specifications. iobase is the bus address, port->iowin_base
588 + * is the CPU address.
590 + iobase = ((port->bridge.iobase & 0xF0) << 8) |
591 + (port->bridge.iobaseupper << 16);
592 + port->iowin_base = port->pcie->io.start + iobase;
593 + port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
594 + (port->bridge.iolimitupper << 16)) -
597 + mvebu_mbus_add_window_remap_flags(port->name, port->iowin_base,
600 + MVEBU_MBUS_PCI_IO);
602 + pci_ioremap_io(iobase, port->iowin_base);
605 +static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
607 + /* Are the new membase/memlimit values invalid? */
608 + if (port->bridge.memlimit < port->bridge.membase) {
610 + /* If a window was configured, remove it */
611 + if (port->memwin_base) {
612 + mvebu_mbus_del_window(port->memwin_base,
613 + port->memwin_size);
614 + port->memwin_base = 0;
615 + port->memwin_size = 0;
622 + * We read the PCI-to-PCI bridge emulated registers, and
623 + * calculate the base address and size of the address decoding
624 + * window to setup, according to the PCI-to-PCI bridge
627 + port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
628 + port->memwin_size =
629 + (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
632 + mvebu_mbus_add_window_remap_flags(port->name, port->memwin_base,
634 + MVEBU_MBUS_NO_REMAP,
635 + MVEBU_MBUS_PCI_MEM);
639 + * Initialize the configuration space of the PCI-to-PCI bridge
640 + * associated with the given PCIe interface.
642 +static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
644 + struct mvebu_sw_pci_bridge *bridge = &port->bridge;
646 + memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
648 + bridge->status = PCI_STATUS_CAP_LIST;
649 + bridge->class = PCI_CLASS_BRIDGE_PCI;
650 + bridge->vendor = PCI_VENDOR_ID_MARVELL;
651 + bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID;
652 + bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
653 + bridge->cache_line_size = 0x10;
655 + /* We support 32 bits I/O addressing */
656 + bridge->iobase = PCI_IO_RANGE_TYPE_32;
657 + bridge->iolimit = PCI_IO_RANGE_TYPE_32;
661 + * Read the configuration space of the PCI-to-PCI bridge associated to
662 + * the given PCIe interface.
664 +static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
665 + unsigned int where, int size, u32 *value)
667 + struct mvebu_sw_pci_bridge *bridge = &port->bridge;
669 + switch (where & ~3) {
670 + case PCI_VENDOR_ID:
671 + *value = bridge->device << 16 | bridge->vendor;
675 + *value = bridge->status << 16 | bridge->command;
678 + case PCI_CLASS_REVISION:
679 + *value = bridge->class << 16 | bridge->interface << 8 |
683 + case PCI_CACHE_LINE_SIZE:
684 + *value = bridge->bist << 24 | bridge->header_type << 16 |
685 + bridge->latency_timer << 8 | bridge->cache_line_size;
688 + case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
689 + *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
692 + case PCI_PRIMARY_BUS:
693 + *value = (bridge->secondary_latency_timer << 24 |
694 + bridge->subordinate_bus << 16 |
695 + bridge->secondary_bus << 8 |
696 + bridge->primary_bus);
700 + *value = (bridge->secondary_status << 16 |
701 + bridge->iolimit << 8 |
705 + case PCI_MEMORY_BASE:
706 + *value = (bridge->memlimit << 16 | bridge->membase);
709 + case PCI_PREF_MEMORY_BASE:
710 + *value = (bridge->prefmemlimit << 16 | bridge->prefmembase);
713 + case PCI_PREF_BASE_UPPER32:
714 + *value = bridge->prefbaseupper;
717 + case PCI_PREF_LIMIT_UPPER32:
718 + *value = bridge->preflimitupper;
721 + case PCI_IO_BASE_UPPER16:
722 + *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
725 + case PCI_ROM_ADDRESS1:
730 + *value = 0xffffffff;
731 + return PCIBIOS_BAD_REGISTER_NUMBER;
735 + *value = (*value >> (8 * (where & 3))) & 0xffff;
736 + else if (size == 1)
737 + *value = (*value >> (8 * (where & 3))) & 0xff;
739 + return PCIBIOS_SUCCESSFUL;
742 +/* Write to the PCI-to-PCI bridge configuration space */
743 +static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
744 + unsigned int where, int size, u32 value)
746 + struct mvebu_sw_pci_bridge *bridge = &port->bridge;
752 + else if (size == 2)
753 + mask = ~(0xffff << ((where & 3) * 8));
754 + else if (size == 1)
755 + mask = ~(0xff << ((where & 3) * 8));
757 + return PCIBIOS_BAD_REGISTER_NUMBER;
759 + err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, ®);
763 + value = (reg & mask) | value << ((where & 3) * 8);
765 + switch (where & ~3) {
767 + bridge->command = value & 0xffff;
768 + bridge->status = value >> 16;
771 + case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
772 + bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
777 + * We also keep bit 1 set, it is a read-only bit that
778 + * indicates we support 32 bits addressing for the
781 + bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
782 + bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
783 + bridge->secondary_status = value >> 16;
784 + mvebu_pcie_handle_iobase_change(port);
787 + case PCI_MEMORY_BASE:
788 + bridge->membase = value & 0xffff;
789 + bridge->memlimit = value >> 16;
790 + mvebu_pcie_handle_membase_change(port);
793 + case PCI_PREF_MEMORY_BASE:
794 + bridge->prefmembase = value & 0xffff;
795 + bridge->prefmemlimit = value >> 16;
798 + case PCI_PREF_BASE_UPPER32:
799 + bridge->prefbaseupper = value;
802 + case PCI_PREF_LIMIT_UPPER32:
803 + bridge->preflimitupper = value;
806 + case PCI_IO_BASE_UPPER16:
807 + bridge->iobaseupper = value & 0xffff;
808 + bridge->iolimitupper = value >> 16;
809 + mvebu_pcie_handle_iobase_change(port);
812 + case PCI_PRIMARY_BUS:
813 + bridge->primary_bus = value & 0xff;
814 + bridge->secondary_bus = (value >> 8) & 0xff;
815 + bridge->subordinate_bus = (value >> 16) & 0xff;
816 + bridge->secondary_latency_timer = (value >> 24) & 0xff;
817 + mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
824 + return PCIBIOS_SUCCESSFUL;
827 +static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
829 + return sys->private_data;
832 +static struct mvebu_pcie_port *
833 +mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus,
838 + for (i = 0; i < pcie->nports; i++) {
839 + struct mvebu_pcie_port *port = &pcie->ports[i];
840 + if (bus->number == 0 && port->devfn == devfn)
842 + if (bus->number != 0 &&
843 + port->bridge.secondary_bus == bus->number)
850 +/* PCI configuration space write function */
851 +static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
852 + int where, int size, u32 val)
854 + struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
855 + struct mvebu_pcie_port *port;
856 + unsigned long flags;
859 + port = mvebu_pcie_find_port(pcie, bus, devfn);
861 + return PCIBIOS_DEVICE_NOT_FOUND;
863 + /* Access the emulated PCI-to-PCI bridge */
864 + if (bus->number == 0)
865 + return mvebu_sw_pci_bridge_write(port, where, size, val);
867 + if (!port->haslink || PCI_SLOT(devfn) != 0)
868 + return PCIBIOS_DEVICE_NOT_FOUND;
870 + /* Access the real PCIe interface */
871 + spin_lock_irqsave(&port->conf_lock, flags);
872 + ret = mvebu_pcie_hw_wr_conf(port, bus,
873 + PCI_DEVFN(1, PCI_FUNC(devfn)),
875 + spin_unlock_irqrestore(&port->conf_lock, flags);
880 +/* PCI configuration space read function */
881 +static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
882 + int size, u32 *val)
884 + struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
885 + struct mvebu_pcie_port *port;
886 + unsigned long flags;
889 + port = mvebu_pcie_find_port(pcie, bus, devfn);
892 + return PCIBIOS_DEVICE_NOT_FOUND;
895 + /* Access the emulated PCI-to-PCI bridge */
896 + if (bus->number == 0)
897 + return mvebu_sw_pci_bridge_read(port, where, size, val);
899 + if (!port->haslink || PCI_SLOT(devfn) != 0) {
901 + return PCIBIOS_DEVICE_NOT_FOUND;
904 + /* Access the real PCIe interface */
905 + spin_lock_irqsave(&port->conf_lock, flags);
906 + ret = mvebu_pcie_hw_rd_conf(port, bus,
907 + PCI_DEVFN(1, PCI_FUNC(devfn)),
909 + spin_unlock_irqrestore(&port->conf_lock, flags);
914 +static struct pci_ops mvebu_pcie_ops = {
915 + .read = mvebu_pcie_rd_conf,
916 + .write = mvebu_pcie_wr_conf,
919 +static int __init mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
921 + struct mvebu_pcie *pcie = sys_to_pcie(sys);
924 + pci_add_resource_offset(&sys->resources, &pcie->realio, sys->io_offset);
925 + pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
926 + pci_add_resource(&sys->resources, &pcie->busn);
928 + for (i = 0; i < pcie->nports; i++) {
929 + struct mvebu_pcie_port *port = &pcie->ports[i];
930 + mvebu_pcie_setup_hw(port);
936 +static int __init mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
938 + struct of_irq oirq;
941 + ret = of_irq_map_pci(dev, &oirq);
945 + return irq_create_of_mapping(oirq.controller, oirq.specifier,
949 +static struct pci_bus *mvebu_pcie_scan_bus(int nr, struct pci_sys_data *sys)
951 + struct mvebu_pcie *pcie = sys_to_pcie(sys);
952 + struct pci_bus *bus;
954 + bus = pci_create_root_bus(&pcie->pdev->dev, sys->busnr,
955 + &mvebu_pcie_ops, sys, &sys->resources);
959 + pci_scan_child_bus(bus);
964 +resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
965 + const struct resource *res,
966 + resource_size_t start,
967 + resource_size_t size,
968 + resource_size_t align)
970 + if (dev->bus->number != 0)
974 + * On the PCI-to-PCI bridge side, the I/O windows must have at
975 + * least a 64 KB size and be aligned on their size, and the
976 + * memory windows must have at least a 1 MB size and be
977 + * aligned on their size
979 + if (res->flags & IORESOURCE_IO)
980 + return round_up(start, max((resource_size_t)SZ_64K, size));
981 + else if (res->flags & IORESOURCE_MEM)
982 + return round_up(start, max((resource_size_t)SZ_1M, size));
987 +static void __init mvebu_pcie_enable(struct mvebu_pcie *pcie)
991 + memset(&hw, 0, sizeof(hw));
993 + hw.nr_controllers = 1;
994 + hw.private_data = (void **)&pcie;
995 + hw.setup = mvebu_pcie_setup;
996 + hw.scan = mvebu_pcie_scan_bus;
997 + hw.map_irq = mvebu_pcie_map_irq;
998 + hw.ops = &mvebu_pcie_ops;
999 + hw.align_resource = mvebu_pcie_align_resource;
1001 + pci_common_init(&hw);
1005 + * Looks up the list of register addresses encoded into the reg =
1006 + * <...> property for one that matches the given port/lane. Once
1009 +static void __iomem * __init
1010 +mvebu_pcie_map_registers(struct platform_device *pdev,
1011 + struct device_node *np,
1012 + struct mvebu_pcie_port *port)
1014 + struct resource regs;
1017 + ret = of_address_to_resource(np, 0, ®s);
1021 + return devm_request_and_ioremap(&pdev->dev, ®s);
1024 +static int __init mvebu_pcie_probe(struct platform_device *pdev)
1026 + struct mvebu_pcie *pcie;
1027 + struct device_node *np = pdev->dev.of_node;
1028 + struct of_pci_range range;
1029 + struct of_pci_range_parser parser;
1030 + struct device_node *child;
1033 + pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
1038 + pcie->pdev = pdev;
1040 + if (of_pci_range_parser_init(&parser, np))
1043 + /* Get the I/O and memory ranges from DT */
1044 + for_each_of_pci_range(&parser, &range) {
1045 + unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
1046 + if (restype == IORESOURCE_IO) {
1047 + of_pci_range_to_resource(&range, np, &pcie->io);
1048 + of_pci_range_to_resource(&range, np, &pcie->realio);
1049 + pcie->io.name = "I/O";
1050 + pcie->realio.start = max_t(resource_size_t,
1053 + pcie->realio.end = min_t(resource_size_t,
1055 + range.pci_addr + range.size);
1057 + if (restype == IORESOURCE_MEM) {
1058 + of_pci_range_to_resource(&range, np, &pcie->mem);
1059 + pcie->mem.name = "MEM";
1063 + /* Get the bus range */
1064 + ret = of_pci_parse_bus_range(np, &pcie->busn);
1066 + dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
1071 + for_each_child_of_node(pdev->dev.of_node, child) {
1072 + if (!of_device_is_available(child))
1077 + pcie->ports = devm_kzalloc(&pdev->dev, pcie->nports *
1078 + sizeof(struct mvebu_pcie_port),
1084 + for_each_child_of_node(pdev->dev.of_node, child) {
1085 + struct mvebu_pcie_port *port = &pcie->ports[i];
1087 + if (!of_device_is_available(child))
1090 + port->pcie = pcie;
1092 + if (of_property_read_u32(child, "marvell,pcie-port",
1094 + dev_warn(&pdev->dev,
1095 + "ignoring PCIe DT node, missing pcie-port property\n");
1099 + if (of_property_read_u32(child, "marvell,pcie-lane",
1103 + port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
1104 + port->port, port->lane);
1106 + port->devfn = of_pci_get_devfn(child);
1107 + if (port->devfn < 0)
1110 + port->base = mvebu_pcie_map_registers(pdev, child, port);
1111 + if (!port->base) {
1112 + dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
1113 + port->port, port->lane);
1117 + if (mvebu_pcie_link_up(port)) {
1118 + port->haslink = 1;
1119 + dev_info(&pdev->dev, "PCIe%d.%d: link up\n",
1120 + port->port, port->lane);
1122 + port->haslink = 0;
1123 + dev_info(&pdev->dev, "PCIe%d.%d: link down\n",
1124 + port->port, port->lane);
1127 + port->clk = of_clk_get_by_name(child, NULL);
1129 + dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
1130 + port->port, port->lane);
1131 + iounmap(port->base);
1132 + port->haslink = 0;
1138 + clk_prepare_enable(port->clk);
1139 + spin_lock_init(&port->conf_lock);
1141 + mvebu_sw_pci_bridge_init(port);
1146 + mvebu_pcie_enable(pcie);
1151 +static const struct of_device_id mvebu_pcie_of_match_table[] = {
1152 + { .compatible = "marvell,armada-xp-pcie", },
1153 + { .compatible = "marvell,armada-370-pcie", },
1156 +MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
1158 +static struct platform_driver mvebu_pcie_driver = {
1160 + .owner = THIS_MODULE,
1161 + .name = "mvebu-pcie",
1163 + of_match_ptr(mvebu_pcie_of_match_table),
1167 +static int __init mvebu_pcie_init(void)
1169 + return platform_driver_probe(&mvebu_pcie_driver,
1170 + mvebu_pcie_probe);
1173 +subsys_initcall(mvebu_pcie_init);
1175 +MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
1176 +MODULE_DESCRIPTION("Marvell EBU PCIe driver");
1177 +MODULE_LICENSE("GPLv2");