1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe driver for Renesas R-Car SoCs
4 * Copyright (C) 2014 Renesas Electronics Europe Ltd
7 * arch/sh/drivers/pci/pcie-sh7786.c
8 * arch/sh/drivers/pci/ops-sh7786.c
9 * Copyright (C) 2009 - 2011 Paul Mundt
11 * Author: Phil Edworthy <phil.edworthy@renesas.com>
14 #include <linux/bitops.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/msi.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_pci.h>
26 #include <linux/of_platform.h>
27 #include <linux/pci.h>
28 #include <linux/phy/phy.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/slab.h>
35 #define PCIECAR 0x000010
36 #define PCIECCTLR 0x000018
37 #define CONFIG_SEND_ENABLE BIT(31)
38 #define TYPE0 (0 << 8)
40 #define PCIECDR 0x000020
41 #define PCIEMSR 0x000028
42 #define PCIEINTXR 0x000400
43 #define PCIEPHYSR 0x0007f0
45 #define PCIEMSITXR 0x000840
47 /* Transfer control */
48 #define PCIETCTLR 0x02000
49 #define DL_DOWN BIT(3)
51 #define PCIETSTR 0x02004
52 #define DATA_LINK_ACTIVE 1
53 #define PCIEERRFR 0x02020
54 #define UNSUPPORTED_REQUEST BIT(4)
55 #define PCIEMSIFR 0x02044
56 #define PCIEMSIALR 0x02048
58 #define PCIEMSIAUR 0x0204c
59 #define PCIEMSIIER 0x02050
61 /* root port address */
62 #define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
64 /* local address reg & mask */
65 #define PCIELAR(x) (0x02200 + ((x) * 0x20))
66 #define PCIELAMR(x) (0x02208 + ((x) * 0x20))
67 #define LAM_PREFETCH BIT(3)
68 #define LAM_64BIT BIT(2)
69 #define LAR_ENABLE BIT(1)
71 /* PCIe address reg & mask */
72 #define PCIEPALR(x) (0x03400 + ((x) * 0x20))
73 #define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
74 #define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
75 #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
76 #define PAR_ENABLE BIT(31)
77 #define IO_SPACE BIT(8)
80 #define PCICONF(x) (0x010000 + ((x) * 0x4))
81 #define PMCAP(x) (0x010040 + ((x) * 0x4))
82 #define EXPCAP(x) (0x010070 + ((x) * 0x4))
83 #define VCCAP(x) (0x010100 + ((x) * 0x4))
86 #define IDSETR1 0x011004
87 #define TLCTLR 0x011048
88 #define MACSR 0x011054
89 #define SPCHGFIN BIT(4)
90 #define SPCHGFAIL BIT(6)
91 #define SPCHGSUC BIT(7)
92 #define LINK_SPEED (0xf << 16)
93 #define LINK_SPEED_2_5GTS (1 << 16)
94 #define LINK_SPEED_5_0GTS (2 << 16)
95 #define MACCTLR 0x011058
96 #define MACCTLR_NFTS_MASK GENMASK(23, 16) /* The name is from SH7786 */
97 #define SPEED_CHANGE BIT(24)
98 #define SCRAMBLE_DISABLE BIT(27)
99 #define LTSMDIS BIT(31)
100 #define MACCTLR_INIT_VAL (LTSMDIS | MACCTLR_NFTS_MASK)
101 #define PMSR 0x01105c
102 #define MACS2R 0x011078
103 #define MACCGSPSETR 0x011084
104 #define SPCNGRSN BIT(31)
107 #define H1_PCIEPHYADRR 0x04000c
108 #define WRITE_CMD BIT(16)
109 #define PHY_ACK BIT(24)
113 #define H1_PCIEPHYDOUTR 0x040014
116 #define GEN2_PCIEPHYADDR 0x780
117 #define GEN2_PCIEPHYDATA 0x784
118 #define GEN2_PCIEPHYCTRL 0x78c
120 #define INT_PCI_MSI_NR 32
122 #define RCONF(x) (PCICONF(0) + (x))
123 #define RPMCAP(x) (PMCAP(0) + (x))
124 #define REXPCAP(x) (EXPCAP(0) + (x))
125 #define RVCCAP(x) (VCCAP(0) + (x))
127 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
128 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
129 #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
131 #define RCAR_PCI_MAX_RESOURCES 4
132 #define MAX_NR_INBOUND_MAPS 6
135 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
136 struct irq_domain *domain;
137 struct msi_controller chip;
144 static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
146 return container_of(chip, struct rcar_msi, chip);
149 /* Structure representing the PCIe interface */
154 struct list_head resources;
160 static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
163 writel(val, pcie->base + reg);
166 static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
169 return readl(pcie->base + reg);
173 RCAR_PCI_ACCESS_READ,
174 RCAR_PCI_ACCESS_WRITE,
177 static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
179 int shift = 8 * (where & 3);
180 u32 val = rcar_pci_read_reg(pcie, where & ~3);
182 val &= ~(mask << shift);
183 val |= data << shift;
184 rcar_pci_write_reg(pcie, val, where & ~3);
187 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
189 int shift = 8 * (where & 3);
190 u32 val = rcar_pci_read_reg(pcie, where & ~3);
195 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
196 static int rcar_pcie_config_access(struct rcar_pcie *pcie,
197 unsigned char access_type, struct pci_bus *bus,
198 unsigned int devfn, int where, u32 *data)
200 int dev, func, reg, index;
202 dev = PCI_SLOT(devfn);
203 func = PCI_FUNC(devfn);
208 * While each channel has its own memory-mapped extended config
209 * space, it's generally only accessible when in endpoint mode.
210 * When in root complex mode, the controller is unable to target
211 * itself with either type 0 or type 1 accesses, and indeed, any
212 * controller initiated target transfer to its own config space
213 * result in a completer abort.
215 * Each channel effectively only supports a single device, but as
216 * the same channel <-> device access works for any PCI_SLOT()
217 * value, we cheat a bit here and bind the controller's config
218 * space to devfn 0 in order to enable self-enumeration. In this
219 * case the regular ECAR/ECDR path is sidelined and the mangled
220 * config access itself is initiated as an internal bus transaction.
222 if (pci_is_root_bus(bus)) {
224 return PCIBIOS_DEVICE_NOT_FOUND;
226 if (access_type == RCAR_PCI_ACCESS_READ) {
227 *data = rcar_pci_read_reg(pcie, PCICONF(index));
229 /* Keep an eye out for changes to the root bus number */
230 if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
231 pcie->root_bus_nr = *data & 0xff;
233 rcar_pci_write_reg(pcie, *data, PCICONF(index));
236 return PCIBIOS_SUCCESSFUL;
239 if (pcie->root_bus_nr < 0)
240 return PCIBIOS_DEVICE_NOT_FOUND;
243 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
245 /* Set the PIO address */
246 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
247 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
249 /* Enable the configuration access */
250 if (bus->parent->number == pcie->root_bus_nr)
251 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
253 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
255 /* Check for errors */
256 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
257 return PCIBIOS_DEVICE_NOT_FOUND;
259 /* Check for master and target aborts */
260 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
261 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
262 return PCIBIOS_DEVICE_NOT_FOUND;
264 if (access_type == RCAR_PCI_ACCESS_READ)
265 *data = rcar_pci_read_reg(pcie, PCIECDR);
267 rcar_pci_write_reg(pcie, *data, PCIECDR);
269 /* Disable the configuration access */
270 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
272 return PCIBIOS_SUCCESSFUL;
275 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
276 int where, int size, u32 *val)
278 struct rcar_pcie *pcie = bus->sysdata;
281 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
282 bus, devfn, where, val);
283 if (ret != PCIBIOS_SUCCESSFUL) {
289 *val = (*val >> (8 * (where & 3))) & 0xff;
291 *val = (*val >> (8 * (where & 2))) & 0xffff;
293 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
294 bus->number, devfn, where, size, (unsigned long)*val);
299 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
300 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
301 int where, int size, u32 val)
303 struct rcar_pcie *pcie = bus->sysdata;
307 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
308 bus, devfn, where, &data);
309 if (ret != PCIBIOS_SUCCESSFUL)
312 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
313 bus->number, devfn, where, size, (unsigned long)val);
316 shift = 8 * (where & 3);
317 data &= ~(0xff << shift);
318 data |= ((val & 0xff) << shift);
319 } else if (size == 2) {
320 shift = 8 * (where & 2);
321 data &= ~(0xffff << shift);
322 data |= ((val & 0xffff) << shift);
326 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
327 bus, devfn, where, &data);
332 static struct pci_ops rcar_pcie_ops = {
333 .read = rcar_pcie_read_conf,
334 .write = rcar_pcie_write_conf,
337 static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie,
338 struct resource_entry *window)
340 /* Setup PCIe address space mappings for each resource */
341 resource_size_t size;
342 resource_size_t res_start;
343 struct resource *res = window->res;
346 rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
349 * The PAMR mask is calculated in units of 128Bytes, which
350 * keeps things pretty simple.
352 size = resource_size(res);
353 mask = (roundup_pow_of_two(size) / SZ_128) - 1;
354 rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
356 if (res->flags & IORESOURCE_IO)
357 res_start = pci_pio_to_address(res->start) - window->offset;
359 res_start = res->start - window->offset;
361 rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
362 rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
365 /* First resource is for IO */
367 if (res->flags & IORESOURCE_IO)
370 rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
373 static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci)
375 struct resource_entry *win;
378 /* Setup PCI resources */
379 resource_list_for_each_entry(win, &pci->resources) {
380 struct resource *res = win->res;
385 switch (resource_type(res)) {
388 rcar_pcie_setup_window(i, pci, win);
392 pci->root_bus_nr = res->start;
398 pci_add_resource(resource, res);
404 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
406 struct device *dev = pcie->dev;
407 unsigned int timeout = 1000;
410 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
413 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
414 dev_err(dev, "Speed change already in progress\n");
418 macsr = rcar_pci_read_reg(pcie, MACSR);
419 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
422 /* Set target link speed to 5.0 GT/s */
423 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
424 PCI_EXP_LNKSTA_CLS_5_0GB);
426 /* Set speed change reason as intentional factor */
427 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
429 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
430 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
431 rcar_pci_write_reg(pcie, macsr, MACSR);
433 /* Start link speed change */
434 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
437 macsr = rcar_pci_read_reg(pcie, MACSR);
438 if (macsr & SPCHGFIN) {
439 /* Clear the interrupt bits */
440 rcar_pci_write_reg(pcie, macsr, MACSR);
442 if (macsr & SPCHGFAIL)
443 dev_err(dev, "Speed change failed\n");
451 dev_err(dev, "Speed change timed out\n");
454 dev_info(dev, "Current link speed is %s GT/s\n",
455 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
458 static int rcar_pcie_enable(struct rcar_pcie *pcie)
460 struct device *dev = pcie->dev;
461 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
462 struct pci_bus *bus, *child;
465 /* Try setting 5 GT/s link speed */
466 rcar_pcie_force_speedup(pcie);
468 rcar_pcie_setup(&bridge->windows, pcie);
470 pci_add_flags(PCI_REASSIGN_ALL_BUS);
472 bridge->dev.parent = dev;
473 bridge->sysdata = pcie;
474 bridge->busnr = pcie->root_bus_nr;
475 bridge->ops = &rcar_pcie_ops;
476 bridge->map_irq = of_irq_parse_and_map_pci;
477 bridge->swizzle_irq = pci_common_swizzle;
478 if (IS_ENABLED(CONFIG_PCI_MSI))
479 bridge->msi = &pcie->msi.chip;
481 ret = pci_scan_root_bus_bridge(bridge);
487 pci_bus_size_bridges(bus);
488 pci_bus_assign_resources(bus);
490 list_for_each_entry(child, &bus->children, node)
491 pcie_bus_configure_settings(child);
493 pci_bus_add_devices(bus);
498 static int phy_wait_for_ack(struct rcar_pcie *pcie)
500 struct device *dev = pcie->dev;
501 unsigned int timeout = 100;
504 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
510 dev_err(dev, "Access to PCIe phy timed out\n");
515 static void phy_write_reg(struct rcar_pcie *pcie,
516 unsigned int rate, unsigned int addr,
517 unsigned int lane, unsigned int data)
519 unsigned long phyaddr;
521 phyaddr = WRITE_CMD |
522 ((rate & 1) << RATE_POS) |
523 ((lane & 0xf) << LANE_POS) |
524 ((addr & 0xff) << ADR_POS);
527 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
528 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
530 /* Ignore errors as they will be dealt with if the data link is down */
531 phy_wait_for_ack(pcie);
534 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
535 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
537 /* Ignore errors as they will be dealt with if the data link is down */
538 phy_wait_for_ack(pcie);
541 static int rcar_pcie_wait_for_phyrdy(struct rcar_pcie *pcie)
543 unsigned int timeout = 10;
546 if (rcar_pci_read_reg(pcie, PCIEPHYSR) & PHYRDY)
555 static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
557 unsigned int timeout = 10000;
560 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
570 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
574 /* Begin initialization */
575 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
578 rcar_pci_write_reg(pcie, 1, PCIEMSR);
580 err = rcar_pcie_wait_for_phyrdy(pcie);
585 * Initial header for port config space is type 1, set the device
586 * class to match. Hardware takes care of propagating the IDSETR
587 * settings, so there is no need to bother with a quirk.
589 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
592 * Setup Secondary Bus Number & Subordinate Bus Number, even though
593 * they aren't used, to avoid bridge being detected as broken.
595 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
596 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
598 /* Initialize default capabilities. */
599 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
600 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
601 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
602 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
603 PCI_HEADER_TYPE_BRIDGE);
605 /* Enable data link layer active state reporting */
606 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
607 PCI_EXP_LNKCAP_DLLLARC);
609 /* Write out the physical slot number = 0 */
610 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
612 /* Set the completion timer timeout to the maximum 50ms. */
613 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
615 /* Terminate list of capabilities (Next Capability Offset=0) */
616 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
619 if (IS_ENABLED(CONFIG_PCI_MSI))
620 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
622 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
624 /* Finish initialization - establish a PCI Express link */
625 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
627 /* This will timeout if we don't have a link. */
628 err = rcar_pcie_wait_for_dl(pcie);
632 /* Enable INTx interrupts */
633 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
640 static int rcar_pcie_phy_init_h1(struct rcar_pcie *pcie)
642 /* Initialize the phy */
643 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
644 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
645 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
646 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
647 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
648 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
649 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
650 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
651 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
652 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
653 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
654 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
656 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
657 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
658 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
663 static int rcar_pcie_phy_init_gen2(struct rcar_pcie *pcie)
666 * These settings come from the R-Car Series, 2nd Generation User's
667 * Manual, section 50.3.1 (2) Initialization of the physical layer.
669 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
670 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
671 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
672 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
674 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
675 /* The following value is for DC connection, no termination resistor */
676 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
677 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
678 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
683 static int rcar_pcie_phy_init_gen3(struct rcar_pcie *pcie)
687 err = phy_init(pcie->phy);
691 err = phy_power_on(pcie->phy);
698 static int rcar_msi_alloc(struct rcar_msi *chip)
702 mutex_lock(&chip->lock);
704 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
705 if (msi < INT_PCI_MSI_NR)
706 set_bit(msi, chip->used);
710 mutex_unlock(&chip->lock);
715 static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
719 mutex_lock(&chip->lock);
720 msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
721 order_base_2(no_irqs));
722 mutex_unlock(&chip->lock);
727 static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
729 mutex_lock(&chip->lock);
730 clear_bit(irq, chip->used);
731 mutex_unlock(&chip->lock);
734 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
736 struct rcar_pcie *pcie = data;
737 struct rcar_msi *msi = &pcie->msi;
738 struct device *dev = pcie->dev;
741 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
743 /* MSI & INTx share an interrupt - we only handle MSI here */
748 unsigned int index = find_first_bit(®, 32);
751 /* clear the interrupt */
752 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
754 irq = irq_find_mapping(msi->domain, index);
756 if (test_bit(index, msi->used))
757 generic_handle_irq(irq);
759 dev_info(dev, "unhandled MSI\n");
761 /* Unknown MSI, just clear it */
762 dev_dbg(dev, "unexpected MSI\n");
765 /* see if there's any more pending in this vector */
766 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
772 static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
773 struct msi_desc *desc)
775 struct rcar_msi *msi = to_rcar_msi(chip);
776 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
781 hwirq = rcar_msi_alloc(msi);
785 irq = irq_find_mapping(msi->domain, hwirq);
787 rcar_msi_free(msi, hwirq);
791 irq_set_msi_desc(irq, desc);
793 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
794 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
797 pci_write_msi_msg(irq, &msg);
802 static int rcar_msi_setup_irqs(struct msi_controller *chip,
803 struct pci_dev *pdev, int nvec, int type)
805 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
806 struct rcar_msi *msi = to_rcar_msi(chip);
807 struct msi_desc *desc;
813 /* MSI-X interrupts are not supported */
814 if (type == PCI_CAP_ID_MSIX)
817 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
818 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
820 hwirq = rcar_msi_alloc_region(msi, nvec);
824 irq = irq_find_mapping(msi->domain, hwirq);
828 for (i = 0; i < nvec; i++) {
830 * irq_create_mapping() called from rcar_pcie_probe() pre-
831 * allocates descs, so there is no need to allocate descs here.
832 * We can therefore assume that if irq_find_mapping() above
833 * returns non-zero, then the descs are also successfully
836 if (irq_set_msi_desc_off(irq, i, desc)) {
842 desc->nvec_used = nvec;
843 desc->msi_attrib.multiple = order_base_2(nvec);
845 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
846 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
849 pci_write_msi_msg(irq, &msg);
854 static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
856 struct rcar_msi *msi = to_rcar_msi(chip);
857 struct irq_data *d = irq_get_irq_data(irq);
859 rcar_msi_free(msi, d->hwirq);
862 static struct irq_chip rcar_msi_irq_chip = {
863 .name = "R-Car PCIe MSI",
864 .irq_enable = pci_msi_unmask_irq,
865 .irq_disable = pci_msi_mask_irq,
866 .irq_mask = pci_msi_mask_irq,
867 .irq_unmask = pci_msi_unmask_irq,
870 static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
871 irq_hw_number_t hwirq)
873 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
874 irq_set_chip_data(irq, domain->host_data);
879 static const struct irq_domain_ops msi_domain_ops = {
883 static void rcar_pcie_unmap_msi(struct rcar_pcie *pcie)
885 struct rcar_msi *msi = &pcie->msi;
888 for (i = 0; i < INT_PCI_MSI_NR; i++) {
889 irq = irq_find_mapping(msi->domain, i);
891 irq_dispose_mapping(irq);
894 irq_domain_remove(msi->domain);
897 static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
899 struct device *dev = pcie->dev;
900 struct rcar_msi *msi = &pcie->msi;
904 mutex_init(&msi->lock);
907 msi->chip.setup_irq = rcar_msi_setup_irq;
908 msi->chip.setup_irqs = rcar_msi_setup_irqs;
909 msi->chip.teardown_irq = rcar_msi_teardown_irq;
911 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
912 &msi_domain_ops, &msi->chip);
914 dev_err(dev, "failed to create IRQ domain\n");
918 for (i = 0; i < INT_PCI_MSI_NR; i++)
919 irq_create_mapping(msi->domain, i);
921 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
922 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
923 IRQF_SHARED | IRQF_NO_THREAD,
924 rcar_msi_irq_chip.name, pcie);
926 dev_err(dev, "failed to request IRQ: %d\n", err);
930 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
931 IRQF_SHARED | IRQF_NO_THREAD,
932 rcar_msi_irq_chip.name, pcie);
934 dev_err(dev, "failed to request IRQ: %d\n", err);
938 /* setup MSI data target */
939 msi->pages = __get_free_pages(GFP_KERNEL, 0);
944 base = virt_to_phys((void *)msi->pages);
946 rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
947 rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);
949 /* enable all MSI interrupts */
950 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
955 rcar_pcie_unmap_msi(pcie);
959 static void rcar_pcie_teardown_msi(struct rcar_pcie *pcie)
961 struct rcar_msi *msi = &pcie->msi;
963 /* Disable all MSI interrupts */
964 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
966 /* Disable address decoding of the MSI interrupt, MSIFE */
967 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
969 free_pages(msi->pages, 0);
971 rcar_pcie_unmap_msi(pcie);
974 static int rcar_pcie_get_resources(struct rcar_pcie *pcie)
976 struct device *dev = pcie->dev;
980 pcie->phy = devm_phy_optional_get(dev, "pcie");
981 if (IS_ERR(pcie->phy))
982 return PTR_ERR(pcie->phy);
984 err = of_address_to_resource(dev->of_node, 0, &res);
988 pcie->base = devm_ioremap_resource(dev, &res);
989 if (IS_ERR(pcie->base))
990 return PTR_ERR(pcie->base);
992 pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
993 if (IS_ERR(pcie->bus_clk)) {
994 dev_err(dev, "cannot get pcie bus clock\n");
995 return PTR_ERR(pcie->bus_clk);
998 i = irq_of_parse_and_map(dev->of_node, 0);
1000 dev_err(dev, "cannot get platform resources for msi interrupt\n");
1006 i = irq_of_parse_and_map(dev->of_node, 1);
1008 dev_err(dev, "cannot get platform resources for msi interrupt\n");
1017 irq_dispose_mapping(pcie->msi.irq1);
1022 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
1023 struct of_pci_range *range,
1026 u64 restype = range->flags;
1027 u64 cpu_addr = range->cpu_addr;
1028 u64 cpu_end = range->cpu_addr + range->size;
1029 u64 pci_addr = range->pci_addr;
1030 u32 flags = LAM_64BIT | LAR_ENABLE;
1035 if (restype & IORESOURCE_PREFETCH)
1036 flags |= LAM_PREFETCH;
1039 * If the size of the range is larger than the alignment of the start
1040 * address, we have to use multiple entries to perform the mapping.
1043 unsigned long nr_zeros = __ffs64(cpu_addr);
1044 u64 alignment = 1ULL << nr_zeros;
1046 size = min(range->size, alignment);
1050 /* Hardware supports max 4GiB inbound region */
1051 size = min(size, 1ULL << 32);
1053 mask = roundup_pow_of_two(size) - 1;
1056 while (cpu_addr < cpu_end) {
1058 * Set up 64-bit inbound regions as the range parser doesn't
1059 * distinguish between 32 and 64-bit types.
1061 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr),
1063 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
1064 rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags,
1067 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr),
1069 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr),
1071 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
1077 if (idx > MAX_NR_INBOUND_MAPS) {
1078 dev_err(pcie->dev, "Failed to map inbound regions!\n");
1087 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
1088 struct device_node *np)
1090 struct of_pci_range range;
1091 struct of_pci_range_parser parser;
1095 if (of_pci_dma_range_parser_init(&parser, np))
1098 /* Get the dma-ranges from DT */
1099 for_each_of_pci_range(&parser, &range) {
1100 u64 end = range.cpu_addr + range.size - 1;
1102 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
1103 range.flags, range.cpu_addr, end, range.pci_addr);
1105 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
1113 static const struct of_device_id rcar_pcie_of_match[] = {
1114 { .compatible = "renesas,pcie-r8a7779",
1115 .data = rcar_pcie_phy_init_h1 },
1116 { .compatible = "renesas,pcie-r8a7790",
1117 .data = rcar_pcie_phy_init_gen2 },
1118 { .compatible = "renesas,pcie-r8a7791",
1119 .data = rcar_pcie_phy_init_gen2 },
1120 { .compatible = "renesas,pcie-rcar-gen2",
1121 .data = rcar_pcie_phy_init_gen2 },
1122 { .compatible = "renesas,pcie-r8a7795",
1123 .data = rcar_pcie_phy_init_gen3 },
1124 { .compatible = "renesas,pcie-rcar-gen3",
1125 .data = rcar_pcie_phy_init_gen3 },
1129 static int rcar_pcie_probe(struct platform_device *pdev)
1131 struct device *dev = &pdev->dev;
1132 struct rcar_pcie *pcie;
1135 int (*phy_init_fn)(struct rcar_pcie *);
1136 struct pci_host_bridge *bridge;
1138 bridge = pci_alloc_host_bridge(sizeof(*pcie));
1142 pcie = pci_host_bridge_priv(bridge);
1145 platform_set_drvdata(pdev, pcie);
1147 err = pci_parse_request_of_pci_ranges(dev, &pcie->resources, NULL);
1149 goto err_free_bridge;
1151 pm_runtime_enable(pcie->dev);
1152 err = pm_runtime_get_sync(pcie->dev);
1154 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
1155 goto err_pm_disable;
1158 err = rcar_pcie_get_resources(pcie);
1160 dev_err(dev, "failed to request resources: %d\n", err);
1164 err = clk_prepare_enable(pcie->bus_clk);
1166 dev_err(dev, "failed to enable bus clock: %d\n", err);
1167 goto err_unmap_msi_irqs;
1170 err = rcar_pcie_parse_map_dma_ranges(pcie, dev->of_node);
1172 goto err_clk_disable;
1174 phy_init_fn = of_device_get_match_data(dev);
1175 err = phy_init_fn(pcie);
1177 dev_err(dev, "failed to init PCIe PHY\n");
1178 goto err_clk_disable;
1181 /* Failure to get a link might just be that no cards are inserted */
1182 if (rcar_pcie_hw_init(pcie)) {
1183 dev_info(dev, "PCIe link down\n");
1185 goto err_phy_shutdown;
1188 data = rcar_pci_read_reg(pcie, MACSR);
1189 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1191 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1192 err = rcar_pcie_enable_msi(pcie);
1195 "failed to enable MSI support: %d\n",
1197 goto err_phy_shutdown;
1201 err = rcar_pcie_enable(pcie);
1203 goto err_msi_teardown;
1208 if (IS_ENABLED(CONFIG_PCI_MSI))
1209 rcar_pcie_teardown_msi(pcie);
1213 phy_power_off(pcie->phy);
1214 phy_exit(pcie->phy);
1218 clk_disable_unprepare(pcie->bus_clk);
1221 irq_dispose_mapping(pcie->msi.irq2);
1222 irq_dispose_mapping(pcie->msi.irq1);
1225 pm_runtime_put(dev);
1228 pm_runtime_disable(dev);
1229 pci_free_resource_list(&pcie->resources);
1232 pci_free_host_bridge(bridge);
1237 static int rcar_pcie_resume_noirq(struct device *dev)
1239 struct rcar_pcie *pcie = dev_get_drvdata(dev);
1241 if (rcar_pci_read_reg(pcie, PMSR) &&
1242 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1245 /* Re-establish the PCIe link */
1246 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1247 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1248 return rcar_pcie_wait_for_dl(pcie);
1251 static const struct dev_pm_ops rcar_pcie_pm_ops = {
1252 .resume_noirq = rcar_pcie_resume_noirq,
1255 static struct platform_driver rcar_pcie_driver = {
1257 .name = "rcar-pcie",
1258 .of_match_table = rcar_pcie_of_match,
1259 .pm = &rcar_pcie_pm_ops,
1260 .suppress_bind_attrs = true,
1262 .probe = rcar_pcie_probe,
1264 builtin_platform_driver(rcar_pcie_driver);