1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe driver for Renesas R-Car SoCs
4 * Copyright (C) 2014-2020 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
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>
33 #include "pcie-rcar.h"
36 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
37 struct irq_domain *domain;
38 struct msi_controller chip;
45 static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
47 return container_of(chip, struct rcar_msi, chip);
50 /* Structure representing the PCIe interface */
51 struct rcar_pcie_host {
52 struct rcar_pcie pcie;
56 struct list_head resources;
60 int (*phy_init_fn)(struct rcar_pcie_host *host);
63 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
65 unsigned int shift = BITS_PER_BYTE * (where & 3);
66 u32 val = rcar_pci_read_reg(pcie, where & ~3);
71 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
72 static int rcar_pcie_config_access(struct rcar_pcie_host *host,
73 unsigned char access_type, struct pci_bus *bus,
74 unsigned int devfn, int where, u32 *data)
76 struct rcar_pcie *pcie = &host->pcie;
77 unsigned int dev, func, reg, index;
79 dev = PCI_SLOT(devfn);
80 func = PCI_FUNC(devfn);
85 * While each channel has its own memory-mapped extended config
86 * space, it's generally only accessible when in endpoint mode.
87 * When in root complex mode, the controller is unable to target
88 * itself with either type 0 or type 1 accesses, and indeed, any
89 * controller initiated target transfer to its own config space
90 * result in a completer abort.
92 * Each channel effectively only supports a single device, but as
93 * the same channel <-> device access works for any PCI_SLOT()
94 * value, we cheat a bit here and bind the controller's config
95 * space to devfn 0 in order to enable self-enumeration. In this
96 * case the regular ECAR/ECDR path is sidelined and the mangled
97 * config access itself is initiated as an internal bus transaction.
99 if (pci_is_root_bus(bus)) {
101 return PCIBIOS_DEVICE_NOT_FOUND;
103 if (access_type == RCAR_PCI_ACCESS_READ) {
104 *data = rcar_pci_read_reg(pcie, PCICONF(index));
106 /* Keep an eye out for changes to the root bus number */
107 if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
108 host->root_bus_nr = *data & 0xff;
110 rcar_pci_write_reg(pcie, *data, PCICONF(index));
113 return PCIBIOS_SUCCESSFUL;
116 if (host->root_bus_nr < 0)
117 return PCIBIOS_DEVICE_NOT_FOUND;
120 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
122 /* Set the PIO address */
123 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
124 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
126 /* Enable the configuration access */
127 if (bus->parent->number == host->root_bus_nr)
128 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
130 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
132 /* Check for errors */
133 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
134 return PCIBIOS_DEVICE_NOT_FOUND;
136 /* Check for master and target aborts */
137 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
138 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
139 return PCIBIOS_DEVICE_NOT_FOUND;
141 if (access_type == RCAR_PCI_ACCESS_READ)
142 *data = rcar_pci_read_reg(pcie, PCIECDR);
144 rcar_pci_write_reg(pcie, *data, PCIECDR);
146 /* Disable the configuration access */
147 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
149 return PCIBIOS_SUCCESSFUL;
152 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
153 int where, int size, u32 *val)
155 struct rcar_pcie_host *host = bus->sysdata;
158 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
159 bus, devfn, where, val);
160 if (ret != PCIBIOS_SUCCESSFUL) {
166 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
168 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
170 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
171 bus->number, devfn, where, size, *val);
176 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
177 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
178 int where, int size, u32 val)
180 struct rcar_pcie_host *host = bus->sysdata;
185 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
186 bus, devfn, where, &data);
187 if (ret != PCIBIOS_SUCCESSFUL)
190 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
191 bus->number, devfn, where, size, val);
194 shift = BITS_PER_BYTE * (where & 3);
195 data &= ~(0xff << shift);
196 data |= ((val & 0xff) << shift);
197 } else if (size == 2) {
198 shift = BITS_PER_BYTE * (where & 2);
199 data &= ~(0xffff << shift);
200 data |= ((val & 0xffff) << shift);
204 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
205 bus, devfn, where, &data);
210 static struct pci_ops rcar_pcie_ops = {
211 .read = rcar_pcie_read_conf,
212 .write = rcar_pcie_write_conf,
215 static int rcar_pcie_setup(struct list_head *resource,
216 struct rcar_pcie_host *host)
218 struct resource_entry *win;
221 /* Setup PCI resources */
222 resource_list_for_each_entry(win, &host->resources) {
223 struct resource *res = win->res;
228 switch (resource_type(res)) {
231 rcar_pcie_set_outbound(&host->pcie, i, win);
235 host->root_bus_nr = res->start;
241 pci_add_resource(resource, res);
247 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
249 struct device *dev = pcie->dev;
250 unsigned int timeout = 1000;
253 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
256 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
257 dev_err(dev, "Speed change already in progress\n");
261 macsr = rcar_pci_read_reg(pcie, MACSR);
262 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
265 /* Set target link speed to 5.0 GT/s */
266 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
267 PCI_EXP_LNKSTA_CLS_5_0GB);
269 /* Set speed change reason as intentional factor */
270 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
272 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
273 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
274 rcar_pci_write_reg(pcie, macsr, MACSR);
276 /* Start link speed change */
277 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
280 macsr = rcar_pci_read_reg(pcie, MACSR);
281 if (macsr & SPCHGFIN) {
282 /* Clear the interrupt bits */
283 rcar_pci_write_reg(pcie, macsr, MACSR);
285 if (macsr & SPCHGFAIL)
286 dev_err(dev, "Speed change failed\n");
294 dev_err(dev, "Speed change timed out\n");
297 dev_info(dev, "Current link speed is %s GT/s\n",
298 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
301 static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
303 struct rcar_pcie *pcie = &host->pcie;
304 struct resource_entry *win;
308 /* Try setting 5 GT/s link speed */
309 rcar_pcie_force_speedup(pcie);
311 /* Setup PCI resources */
312 resource_list_for_each_entry(win, &host->resources) {
313 struct resource *res = win->res;
318 switch (resource_type(res)) {
321 rcar_pcie_set_outbound(pcie, i, win);
328 static int rcar_pcie_enable(struct rcar_pcie_host *host)
330 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
331 struct rcar_pcie *pcie = &host->pcie;
332 struct device *dev = pcie->dev;
333 struct pci_bus *bus, *child;
336 /* Try setting 5 GT/s link speed */
337 rcar_pcie_force_speedup(pcie);
339 rcar_pcie_setup(&bridge->windows, host);
341 pci_add_flags(PCI_REASSIGN_ALL_BUS);
343 bridge->dev.parent = dev;
344 bridge->sysdata = host;
345 bridge->busnr = host->root_bus_nr;
346 bridge->ops = &rcar_pcie_ops;
347 bridge->map_irq = of_irq_parse_and_map_pci;
348 bridge->swizzle_irq = pci_common_swizzle;
349 if (IS_ENABLED(CONFIG_PCI_MSI))
350 bridge->msi = &host->msi.chip;
352 ret = pci_scan_root_bus_bridge(bridge);
358 pci_bus_size_bridges(bus);
359 pci_bus_assign_resources(bus);
361 list_for_each_entry(child, &bus->children, node)
362 pcie_bus_configure_settings(child);
364 pci_bus_add_devices(bus);
369 static int phy_wait_for_ack(struct rcar_pcie *pcie)
371 struct device *dev = pcie->dev;
372 unsigned int timeout = 100;
375 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
381 dev_err(dev, "Access to PCIe phy timed out\n");
386 static void phy_write_reg(struct rcar_pcie *pcie,
387 unsigned int rate, u32 addr,
388 unsigned int lane, u32 data)
392 phyaddr = WRITE_CMD |
393 ((rate & 1) << RATE_POS) |
394 ((lane & 0xf) << LANE_POS) |
395 ((addr & 0xff) << ADR_POS);
398 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
399 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
401 /* Ignore errors as they will be dealt with if the data link is down */
402 phy_wait_for_ack(pcie);
405 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
406 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
408 /* Ignore errors as they will be dealt with if the data link is down */
409 phy_wait_for_ack(pcie);
412 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
416 /* Begin initialization */
417 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
420 rcar_pci_write_reg(pcie, 1, PCIEMSR);
422 err = rcar_pcie_wait_for_phyrdy(pcie);
427 * Initial header for port config space is type 1, set the device
428 * class to match. Hardware takes care of propagating the IDSETR
429 * settings, so there is no need to bother with a quirk.
431 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
434 * Setup Secondary Bus Number & Subordinate Bus Number, even though
435 * they aren't used, to avoid bridge being detected as broken.
437 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
438 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
440 /* Initialize default capabilities. */
441 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
442 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
443 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
444 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
445 PCI_HEADER_TYPE_BRIDGE);
447 /* Enable data link layer active state reporting */
448 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
449 PCI_EXP_LNKCAP_DLLLARC);
451 /* Write out the physical slot number = 0 */
452 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
454 /* Set the completion timer timeout to the maximum 50ms. */
455 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
457 /* Terminate list of capabilities (Next Capability Offset=0) */
458 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
461 if (IS_ENABLED(CONFIG_PCI_MSI))
462 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
464 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
466 /* Finish initialization - establish a PCI Express link */
467 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
469 /* This will timeout if we don't have a link. */
470 err = rcar_pcie_wait_for_dl(pcie);
474 /* Enable INTx interrupts */
475 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
482 static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
484 struct rcar_pcie *pcie = &host->pcie;
486 /* Initialize the phy */
487 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
488 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
489 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
490 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
491 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
492 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
493 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
494 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
495 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
496 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
497 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
498 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
500 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
501 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
502 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
507 static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
509 struct rcar_pcie *pcie = &host->pcie;
512 * These settings come from the R-Car Series, 2nd Generation User's
513 * Manual, section 50.3.1 (2) Initialization of the physical layer.
515 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
516 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
517 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
518 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
520 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
521 /* The following value is for DC connection, no termination resistor */
522 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
523 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
524 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
529 static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
533 err = phy_init(host->phy);
537 err = phy_power_on(host->phy);
544 static int rcar_msi_alloc(struct rcar_msi *chip)
548 mutex_lock(&chip->lock);
550 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
551 if (msi < INT_PCI_MSI_NR)
552 set_bit(msi, chip->used);
556 mutex_unlock(&chip->lock);
561 static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
565 mutex_lock(&chip->lock);
566 msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
567 order_base_2(no_irqs));
568 mutex_unlock(&chip->lock);
573 static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
575 mutex_lock(&chip->lock);
576 clear_bit(irq, chip->used);
577 mutex_unlock(&chip->lock);
580 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
582 struct rcar_pcie_host *host = data;
583 struct rcar_pcie *pcie = &host->pcie;
584 struct rcar_msi *msi = &host->msi;
585 struct device *dev = pcie->dev;
588 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
590 /* MSI & INTx share an interrupt - we only handle MSI here */
595 unsigned int index = find_first_bit(®, 32);
596 unsigned int msi_irq;
598 /* clear the interrupt */
599 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
601 msi_irq = irq_find_mapping(msi->domain, index);
603 if (test_bit(index, msi->used))
604 generic_handle_irq(msi_irq);
606 dev_info(dev, "unhandled MSI\n");
608 /* Unknown MSI, just clear it */
609 dev_dbg(dev, "unexpected MSI\n");
612 /* see if there's any more pending in this vector */
613 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
619 static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
620 struct msi_desc *desc)
622 struct rcar_msi *msi = to_rcar_msi(chip);
623 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
625 struct rcar_pcie *pcie = &host->pcie;
630 hwirq = rcar_msi_alloc(msi);
634 irq = irq_find_mapping(msi->domain, hwirq);
636 rcar_msi_free(msi, hwirq);
640 irq_set_msi_desc(irq, desc);
642 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
643 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
646 pci_write_msi_msg(irq, &msg);
651 static int rcar_msi_setup_irqs(struct msi_controller *chip,
652 struct pci_dev *pdev, int nvec, int type)
654 struct rcar_msi *msi = to_rcar_msi(chip);
655 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
657 struct rcar_pcie *pcie = &host->pcie;
658 struct msi_desc *desc;
664 /* MSI-X interrupts are not supported */
665 if (type == PCI_CAP_ID_MSIX)
668 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
669 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
671 hwirq = rcar_msi_alloc_region(msi, nvec);
675 irq = irq_find_mapping(msi->domain, hwirq);
679 for (i = 0; i < nvec; i++) {
681 * irq_create_mapping() called from rcar_pcie_probe() pre-
682 * allocates descs, so there is no need to allocate descs here.
683 * We can therefore assume that if irq_find_mapping() above
684 * returns non-zero, then the descs are also successfully
687 if (irq_set_msi_desc_off(irq, i, desc)) {
693 desc->nvec_used = nvec;
694 desc->msi_attrib.multiple = order_base_2(nvec);
696 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
697 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
700 pci_write_msi_msg(irq, &msg);
705 static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
707 struct rcar_msi *msi = to_rcar_msi(chip);
708 struct irq_data *d = irq_get_irq_data(irq);
710 rcar_msi_free(msi, d->hwirq);
713 static struct irq_chip rcar_msi_irq_chip = {
714 .name = "R-Car PCIe MSI",
715 .irq_enable = pci_msi_unmask_irq,
716 .irq_disable = pci_msi_mask_irq,
717 .irq_mask = pci_msi_mask_irq,
718 .irq_unmask = pci_msi_unmask_irq,
721 static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
722 irq_hw_number_t hwirq)
724 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
725 irq_set_chip_data(irq, domain->host_data);
730 static const struct irq_domain_ops msi_domain_ops = {
734 static void rcar_pcie_unmap_msi(struct rcar_pcie_host *host)
736 struct rcar_msi *msi = &host->msi;
739 for (i = 0; i < INT_PCI_MSI_NR; i++) {
740 irq = irq_find_mapping(msi->domain, i);
742 irq_dispose_mapping(irq);
745 irq_domain_remove(msi->domain);
748 static void rcar_pcie_hw_enable_msi(struct rcar_pcie_host *host)
750 struct rcar_pcie *pcie = &host->pcie;
751 struct rcar_msi *msi = &host->msi;
754 /* setup MSI data target */
755 base = virt_to_phys((void *)msi->pages);
757 rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
758 rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);
760 /* enable all MSI interrupts */
761 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
764 static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
766 struct rcar_pcie *pcie = &host->pcie;
767 struct device *dev = pcie->dev;
768 struct rcar_msi *msi = &host->msi;
771 mutex_init(&msi->lock);
774 msi->chip.setup_irq = rcar_msi_setup_irq;
775 msi->chip.setup_irqs = rcar_msi_setup_irqs;
776 msi->chip.teardown_irq = rcar_msi_teardown_irq;
778 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
779 &msi_domain_ops, &msi->chip);
781 dev_err(dev, "failed to create IRQ domain\n");
785 for (i = 0; i < INT_PCI_MSI_NR; i++)
786 irq_create_mapping(msi->domain, i);
788 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
789 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
790 IRQF_SHARED | IRQF_NO_THREAD,
791 rcar_msi_irq_chip.name, host);
793 dev_err(dev, "failed to request IRQ: %d\n", err);
797 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
798 IRQF_SHARED | IRQF_NO_THREAD,
799 rcar_msi_irq_chip.name, host);
801 dev_err(dev, "failed to request IRQ: %d\n", err);
805 /* setup MSI data target */
806 msi->pages = __get_free_pages(GFP_KERNEL, 0);
807 rcar_pcie_hw_enable_msi(host);
812 rcar_pcie_unmap_msi(host);
816 static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
818 struct rcar_pcie *pcie = &host->pcie;
819 struct rcar_msi *msi = &host->msi;
821 /* Disable all MSI interrupts */
822 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
824 /* Disable address decoding of the MSI interrupt, MSIFE */
825 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
827 free_pages(msi->pages, 0);
829 rcar_pcie_unmap_msi(host);
832 static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
834 struct rcar_pcie *pcie = &host->pcie;
835 struct device *dev = pcie->dev;
839 host->phy = devm_phy_optional_get(dev, "pcie");
840 if (IS_ERR(host->phy))
841 return PTR_ERR(host->phy);
843 err = of_address_to_resource(dev->of_node, 0, &res);
847 pcie->base = devm_ioremap_resource(dev, &res);
848 if (IS_ERR(pcie->base))
849 return PTR_ERR(pcie->base);
851 host->bus_clk = devm_clk_get(dev, "pcie_bus");
852 if (IS_ERR(host->bus_clk)) {
853 dev_err(dev, "cannot get pcie bus clock\n");
854 return PTR_ERR(host->bus_clk);
857 i = irq_of_parse_and_map(dev->of_node, 0);
859 dev_err(dev, "cannot get platform resources for msi interrupt\n");
865 i = irq_of_parse_and_map(dev->of_node, 1);
867 dev_err(dev, "cannot get platform resources for msi interrupt\n");
876 irq_dispose_mapping(host->msi.irq1);
881 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
882 struct resource_entry *entry,
885 u64 restype = entry->res->flags;
886 u64 cpu_addr = entry->res->start;
887 u64 cpu_end = entry->res->end;
888 u64 pci_addr = entry->res->start - entry->offset;
889 u32 flags = LAM_64BIT | LAR_ENABLE;
891 u64 size = resource_size(entry->res);
894 if (restype & IORESOURCE_PREFETCH)
895 flags |= LAM_PREFETCH;
897 while (cpu_addr < cpu_end) {
898 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
899 dev_err(pcie->dev, "Failed to map inbound regions!\n");
903 * If the size of the range is larger than the alignment of
904 * the start address, we have to use multiple entries to
905 * perform the mapping.
908 unsigned long nr_zeros = __ffs64(cpu_addr);
909 u64 alignment = 1ULL << nr_zeros;
911 size = min(size, alignment);
913 /* Hardware supports max 4GiB inbound region */
914 size = min(size, 1ULL << 32);
916 mask = roundup_pow_of_two(size) - 1;
919 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
920 lower_32_bits(mask) | flags, idx, true);
931 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
933 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
934 struct resource_entry *entry;
935 int index = 0, err = 0;
937 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
938 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
946 static const struct of_device_id rcar_pcie_of_match[] = {
947 { .compatible = "renesas,pcie-r8a7779",
948 .data = rcar_pcie_phy_init_h1 },
949 { .compatible = "renesas,pcie-r8a7790",
950 .data = rcar_pcie_phy_init_gen2 },
951 { .compatible = "renesas,pcie-r8a7791",
952 .data = rcar_pcie_phy_init_gen2 },
953 { .compatible = "renesas,pcie-rcar-gen2",
954 .data = rcar_pcie_phy_init_gen2 },
955 { .compatible = "renesas,pcie-r8a7795",
956 .data = rcar_pcie_phy_init_gen3 },
957 { .compatible = "renesas,pcie-rcar-gen3",
958 .data = rcar_pcie_phy_init_gen3 },
962 static int rcar_pcie_probe(struct platform_device *pdev)
964 struct device *dev = &pdev->dev;
965 struct rcar_pcie_host *host;
966 struct rcar_pcie *pcie;
969 struct pci_host_bridge *bridge;
971 bridge = pci_alloc_host_bridge(sizeof(*host));
975 host = pci_host_bridge_priv(bridge);
978 platform_set_drvdata(pdev, host);
980 err = pci_parse_request_of_pci_ranges(dev, &host->resources,
981 &bridge->dma_ranges, NULL);
983 goto err_free_bridge;
985 pm_runtime_enable(pcie->dev);
986 err = pm_runtime_get_sync(pcie->dev);
988 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
992 err = rcar_pcie_get_resources(host);
994 dev_err(dev, "failed to request resources: %d\n", err);
998 err = clk_prepare_enable(host->bus_clk);
1000 dev_err(dev, "failed to enable bus clock: %d\n", err);
1001 goto err_unmap_msi_irqs;
1004 err = rcar_pcie_parse_map_dma_ranges(host);
1006 goto err_clk_disable;
1008 host->phy_init_fn = of_device_get_match_data(dev);
1009 err = host->phy_init_fn(host);
1011 dev_err(dev, "failed to init PCIe PHY\n");
1012 goto err_clk_disable;
1015 /* Failure to get a link might just be that no cards are inserted */
1016 if (rcar_pcie_hw_init(pcie)) {
1017 dev_info(dev, "PCIe link down\n");
1019 goto err_phy_shutdown;
1022 data = rcar_pci_read_reg(pcie, MACSR);
1023 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1025 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1026 err = rcar_pcie_enable_msi(host);
1029 "failed to enable MSI support: %d\n",
1031 goto err_phy_shutdown;
1035 err = rcar_pcie_enable(host);
1037 goto err_msi_teardown;
1042 if (IS_ENABLED(CONFIG_PCI_MSI))
1043 rcar_pcie_teardown_msi(host);
1047 phy_power_off(host->phy);
1048 phy_exit(host->phy);
1052 clk_disable_unprepare(host->bus_clk);
1055 irq_dispose_mapping(host->msi.irq2);
1056 irq_dispose_mapping(host->msi.irq1);
1059 pm_runtime_put(dev);
1062 pm_runtime_disable(dev);
1063 pci_free_resource_list(&host->resources);
1066 pci_free_host_bridge(bridge);
1071 static int __maybe_unused rcar_pcie_resume(struct device *dev)
1073 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1074 struct rcar_pcie *pcie = &host->pcie;
1078 err = rcar_pcie_parse_map_dma_ranges(host);
1082 /* Failure to get a link might just be that no cards are inserted */
1083 err = host->phy_init_fn(host);
1085 dev_info(dev, "PCIe link down\n");
1089 data = rcar_pci_read_reg(pcie, MACSR);
1090 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1093 if (IS_ENABLED(CONFIG_PCI_MSI))
1094 rcar_pcie_hw_enable_msi(host);
1096 rcar_pcie_hw_enable(host);
1101 static int rcar_pcie_resume_noirq(struct device *dev)
1103 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1104 struct rcar_pcie *pcie = &host->pcie;
1106 if (rcar_pci_read_reg(pcie, PMSR) &&
1107 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1110 /* Re-establish the PCIe link */
1111 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1112 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1113 return rcar_pcie_wait_for_dl(pcie);
1116 static const struct dev_pm_ops rcar_pcie_pm_ops = {
1117 SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1118 .resume_noirq = rcar_pcie_resume_noirq,
1121 static struct platform_driver rcar_pcie_driver = {
1123 .name = "rcar-pcie",
1124 .of_match_table = rcar_pcie_of_match,
1125 .pm = &rcar_pcie_pm_ops,
1126 .suppress_bind_attrs = true,
1128 .probe = rcar_pcie_probe,
1130 builtin_platform_driver(rcar_pcie_driver);