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;
58 int (*phy_init_fn)(struct rcar_pcie_host *host);
61 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
63 unsigned int shift = BITS_PER_BYTE * (where & 3);
64 u32 val = rcar_pci_read_reg(pcie, where & ~3);
69 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
70 static int rcar_pcie_config_access(struct rcar_pcie_host *host,
71 unsigned char access_type, struct pci_bus *bus,
72 unsigned int devfn, int where, u32 *data)
74 struct rcar_pcie *pcie = &host->pcie;
75 unsigned int dev, func, reg, index;
77 dev = PCI_SLOT(devfn);
78 func = PCI_FUNC(devfn);
83 * While each channel has its own memory-mapped extended config
84 * space, it's generally only accessible when in endpoint mode.
85 * When in root complex mode, the controller is unable to target
86 * itself with either type 0 or type 1 accesses, and indeed, any
87 * controller initiated target transfer to its own config space
88 * result in a completer abort.
90 * Each channel effectively only supports a single device, but as
91 * the same channel <-> device access works for any PCI_SLOT()
92 * value, we cheat a bit here and bind the controller's config
93 * space to devfn 0 in order to enable self-enumeration. In this
94 * case the regular ECAR/ECDR path is sidelined and the mangled
95 * config access itself is initiated as an internal bus transaction.
97 if (pci_is_root_bus(bus)) {
99 return PCIBIOS_DEVICE_NOT_FOUND;
101 if (access_type == RCAR_PCI_ACCESS_READ)
102 *data = rcar_pci_read_reg(pcie, PCICONF(index));
104 rcar_pci_write_reg(pcie, *data, PCICONF(index));
106 return PCIBIOS_SUCCESSFUL;
110 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
112 /* Set the PIO address */
113 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
114 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
116 /* Enable the configuration access */
117 if (pci_is_root_bus(bus->parent))
118 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
120 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
122 /* Check for errors */
123 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
124 return PCIBIOS_DEVICE_NOT_FOUND;
126 /* Check for master and target aborts */
127 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
128 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
129 return PCIBIOS_DEVICE_NOT_FOUND;
131 if (access_type == RCAR_PCI_ACCESS_READ)
132 *data = rcar_pci_read_reg(pcie, PCIECDR);
134 rcar_pci_write_reg(pcie, *data, PCIECDR);
136 /* Disable the configuration access */
137 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
139 return PCIBIOS_SUCCESSFUL;
142 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
143 int where, int size, u32 *val)
145 struct rcar_pcie_host *host = bus->sysdata;
148 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
149 bus, devfn, where, val);
150 if (ret != PCIBIOS_SUCCESSFUL) {
156 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
158 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
160 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
161 bus->number, devfn, where, size, *val);
166 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
167 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
168 int where, int size, u32 val)
170 struct rcar_pcie_host *host = bus->sysdata;
175 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
176 bus, devfn, where, &data);
177 if (ret != PCIBIOS_SUCCESSFUL)
180 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
181 bus->number, devfn, where, size, val);
184 shift = BITS_PER_BYTE * (where & 3);
185 data &= ~(0xff << shift);
186 data |= ((val & 0xff) << shift);
187 } else if (size == 2) {
188 shift = BITS_PER_BYTE * (where & 2);
189 data &= ~(0xffff << shift);
190 data |= ((val & 0xffff) << shift);
194 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
195 bus, devfn, where, &data);
200 static struct pci_ops rcar_pcie_ops = {
201 .read = rcar_pcie_read_conf,
202 .write = rcar_pcie_write_conf,
205 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
207 struct device *dev = pcie->dev;
208 unsigned int timeout = 1000;
211 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
214 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
215 dev_err(dev, "Speed change already in progress\n");
219 macsr = rcar_pci_read_reg(pcie, MACSR);
220 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
223 /* Set target link speed to 5.0 GT/s */
224 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
225 PCI_EXP_LNKSTA_CLS_5_0GB);
227 /* Set speed change reason as intentional factor */
228 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
230 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
231 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
232 rcar_pci_write_reg(pcie, macsr, MACSR);
234 /* Start link speed change */
235 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
238 macsr = rcar_pci_read_reg(pcie, MACSR);
239 if (macsr & SPCHGFIN) {
240 /* Clear the interrupt bits */
241 rcar_pci_write_reg(pcie, macsr, MACSR);
243 if (macsr & SPCHGFAIL)
244 dev_err(dev, "Speed change failed\n");
252 dev_err(dev, "Speed change timed out\n");
255 dev_info(dev, "Current link speed is %s GT/s\n",
256 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
259 static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
261 struct rcar_pcie *pcie = &host->pcie;
262 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
263 struct resource_entry *win;
267 /* Try setting 5 GT/s link speed */
268 rcar_pcie_force_speedup(pcie);
270 /* Setup PCI resources */
271 resource_list_for_each_entry(win, &bridge->windows) {
272 struct resource *res = win->res;
277 switch (resource_type(res)) {
280 rcar_pcie_set_outbound(pcie, i, win);
287 static int rcar_pcie_enable(struct rcar_pcie_host *host)
289 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
291 rcar_pcie_hw_enable(host);
293 pci_add_flags(PCI_REASSIGN_ALL_BUS);
295 bridge->sysdata = host;
296 bridge->ops = &rcar_pcie_ops;
297 if (IS_ENABLED(CONFIG_PCI_MSI))
298 bridge->msi = &host->msi.chip;
300 return pci_host_probe(bridge);
303 static int phy_wait_for_ack(struct rcar_pcie *pcie)
305 struct device *dev = pcie->dev;
306 unsigned int timeout = 100;
309 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
315 dev_err(dev, "Access to PCIe phy timed out\n");
320 static void phy_write_reg(struct rcar_pcie *pcie,
321 unsigned int rate, u32 addr,
322 unsigned int lane, u32 data)
326 phyaddr = WRITE_CMD |
327 ((rate & 1) << RATE_POS) |
328 ((lane & 0xf) << LANE_POS) |
329 ((addr & 0xff) << ADR_POS);
332 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
333 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
335 /* Ignore errors as they will be dealt with if the data link is down */
336 phy_wait_for_ack(pcie);
339 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
340 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
342 /* Ignore errors as they will be dealt with if the data link is down */
343 phy_wait_for_ack(pcie);
346 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
350 /* Begin initialization */
351 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
354 rcar_pci_write_reg(pcie, 1, PCIEMSR);
356 err = rcar_pcie_wait_for_phyrdy(pcie);
361 * Initial header for port config space is type 1, set the device
362 * class to match. Hardware takes care of propagating the IDSETR
363 * settings, so there is no need to bother with a quirk.
365 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
368 * Setup Secondary Bus Number & Subordinate Bus Number, even though
369 * they aren't used, to avoid bridge being detected as broken.
371 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
372 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
374 /* Initialize default capabilities. */
375 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
376 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
377 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
378 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
379 PCI_HEADER_TYPE_BRIDGE);
381 /* Enable data link layer active state reporting */
382 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
383 PCI_EXP_LNKCAP_DLLLARC);
385 /* Write out the physical slot number = 0 */
386 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
388 /* Set the completion timer timeout to the maximum 50ms. */
389 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
391 /* Terminate list of capabilities (Next Capability Offset=0) */
392 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
395 if (IS_ENABLED(CONFIG_PCI_MSI))
396 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
398 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
400 /* Finish initialization - establish a PCI Express link */
401 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
403 /* This will timeout if we don't have a link. */
404 err = rcar_pcie_wait_for_dl(pcie);
408 /* Enable INTx interrupts */
409 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
416 static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
418 struct rcar_pcie *pcie = &host->pcie;
420 /* Initialize the phy */
421 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
422 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
423 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
424 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
425 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
426 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
427 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
428 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
429 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
430 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
431 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
432 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
434 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
435 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
436 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
441 static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
443 struct rcar_pcie *pcie = &host->pcie;
446 * These settings come from the R-Car Series, 2nd Generation User's
447 * Manual, section 50.3.1 (2) Initialization of the physical layer.
449 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
450 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
451 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
452 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
454 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
455 /* The following value is for DC connection, no termination resistor */
456 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
457 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
458 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
463 static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
467 err = phy_init(host->phy);
471 err = phy_power_on(host->phy);
478 static int rcar_msi_alloc(struct rcar_msi *chip)
482 mutex_lock(&chip->lock);
484 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
485 if (msi < INT_PCI_MSI_NR)
486 set_bit(msi, chip->used);
490 mutex_unlock(&chip->lock);
495 static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
499 mutex_lock(&chip->lock);
500 msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
501 order_base_2(no_irqs));
502 mutex_unlock(&chip->lock);
507 static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
509 mutex_lock(&chip->lock);
510 clear_bit(irq, chip->used);
511 mutex_unlock(&chip->lock);
514 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
516 struct rcar_pcie_host *host = data;
517 struct rcar_pcie *pcie = &host->pcie;
518 struct rcar_msi *msi = &host->msi;
519 struct device *dev = pcie->dev;
522 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
524 /* MSI & INTx share an interrupt - we only handle MSI here */
529 unsigned int index = find_first_bit(®, 32);
530 unsigned int msi_irq;
532 /* clear the interrupt */
533 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
535 msi_irq = irq_find_mapping(msi->domain, index);
537 if (test_bit(index, msi->used))
538 generic_handle_irq(msi_irq);
540 dev_info(dev, "unhandled MSI\n");
542 /* Unknown MSI, just clear it */
543 dev_dbg(dev, "unexpected MSI\n");
546 /* see if there's any more pending in this vector */
547 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
553 static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
554 struct msi_desc *desc)
556 struct rcar_msi *msi = to_rcar_msi(chip);
557 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
559 struct rcar_pcie *pcie = &host->pcie;
564 hwirq = rcar_msi_alloc(msi);
568 irq = irq_find_mapping(msi->domain, hwirq);
570 rcar_msi_free(msi, hwirq);
574 irq_set_msi_desc(irq, desc);
576 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
577 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
580 pci_write_msi_msg(irq, &msg);
585 static int rcar_msi_setup_irqs(struct msi_controller *chip,
586 struct pci_dev *pdev, int nvec, int type)
588 struct rcar_msi *msi = to_rcar_msi(chip);
589 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
591 struct rcar_pcie *pcie = &host->pcie;
592 struct msi_desc *desc;
598 /* MSI-X interrupts are not supported */
599 if (type == PCI_CAP_ID_MSIX)
602 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
603 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
605 hwirq = rcar_msi_alloc_region(msi, nvec);
609 irq = irq_find_mapping(msi->domain, hwirq);
613 for (i = 0; i < nvec; i++) {
615 * irq_create_mapping() called from rcar_pcie_probe() pre-
616 * allocates descs, so there is no need to allocate descs here.
617 * We can therefore assume that if irq_find_mapping() above
618 * returns non-zero, then the descs are also successfully
621 if (irq_set_msi_desc_off(irq, i, desc)) {
627 desc->nvec_used = nvec;
628 desc->msi_attrib.multiple = order_base_2(nvec);
630 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
631 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
634 pci_write_msi_msg(irq, &msg);
639 static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
641 struct rcar_msi *msi = to_rcar_msi(chip);
642 struct irq_data *d = irq_get_irq_data(irq);
644 rcar_msi_free(msi, d->hwirq);
647 static struct irq_chip rcar_msi_irq_chip = {
648 .name = "R-Car PCIe MSI",
649 .irq_enable = pci_msi_unmask_irq,
650 .irq_disable = pci_msi_mask_irq,
651 .irq_mask = pci_msi_mask_irq,
652 .irq_unmask = pci_msi_unmask_irq,
655 static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
656 irq_hw_number_t hwirq)
658 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
659 irq_set_chip_data(irq, domain->host_data);
664 static const struct irq_domain_ops msi_domain_ops = {
668 static void rcar_pcie_unmap_msi(struct rcar_pcie_host *host)
670 struct rcar_msi *msi = &host->msi;
673 for (i = 0; i < INT_PCI_MSI_NR; i++) {
674 irq = irq_find_mapping(msi->domain, i);
676 irq_dispose_mapping(irq);
679 irq_domain_remove(msi->domain);
682 static void rcar_pcie_hw_enable_msi(struct rcar_pcie_host *host)
684 struct rcar_pcie *pcie = &host->pcie;
685 struct rcar_msi *msi = &host->msi;
688 /* setup MSI data target */
689 base = virt_to_phys((void *)msi->pages);
691 rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
692 rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);
694 /* enable all MSI interrupts */
695 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
698 static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
700 struct rcar_pcie *pcie = &host->pcie;
701 struct device *dev = pcie->dev;
702 struct rcar_msi *msi = &host->msi;
705 mutex_init(&msi->lock);
708 msi->chip.setup_irq = rcar_msi_setup_irq;
709 msi->chip.setup_irqs = rcar_msi_setup_irqs;
710 msi->chip.teardown_irq = rcar_msi_teardown_irq;
712 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
713 &msi_domain_ops, &msi->chip);
715 dev_err(dev, "failed to create IRQ domain\n");
719 for (i = 0; i < INT_PCI_MSI_NR; i++)
720 irq_create_mapping(msi->domain, i);
722 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
723 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
724 IRQF_SHARED | IRQF_NO_THREAD,
725 rcar_msi_irq_chip.name, host);
727 dev_err(dev, "failed to request IRQ: %d\n", err);
731 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
732 IRQF_SHARED | IRQF_NO_THREAD,
733 rcar_msi_irq_chip.name, host);
735 dev_err(dev, "failed to request IRQ: %d\n", err);
739 /* setup MSI data target */
740 msi->pages = __get_free_pages(GFP_KERNEL, 0);
741 rcar_pcie_hw_enable_msi(host);
746 rcar_pcie_unmap_msi(host);
750 static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
752 struct rcar_pcie *pcie = &host->pcie;
753 struct rcar_msi *msi = &host->msi;
755 /* Disable all MSI interrupts */
756 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
758 /* Disable address decoding of the MSI interrupt, MSIFE */
759 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
761 free_pages(msi->pages, 0);
763 rcar_pcie_unmap_msi(host);
766 static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
768 struct rcar_pcie *pcie = &host->pcie;
769 struct device *dev = pcie->dev;
773 host->phy = devm_phy_optional_get(dev, "pcie");
774 if (IS_ERR(host->phy))
775 return PTR_ERR(host->phy);
777 err = of_address_to_resource(dev->of_node, 0, &res);
781 pcie->base = devm_ioremap_resource(dev, &res);
782 if (IS_ERR(pcie->base))
783 return PTR_ERR(pcie->base);
785 host->bus_clk = devm_clk_get(dev, "pcie_bus");
786 if (IS_ERR(host->bus_clk)) {
787 dev_err(dev, "cannot get pcie bus clock\n");
788 return PTR_ERR(host->bus_clk);
791 i = irq_of_parse_and_map(dev->of_node, 0);
793 dev_err(dev, "cannot get platform resources for msi interrupt\n");
799 i = irq_of_parse_and_map(dev->of_node, 1);
801 dev_err(dev, "cannot get platform resources for msi interrupt\n");
810 irq_dispose_mapping(host->msi.irq1);
815 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
816 struct resource_entry *entry,
819 u64 restype = entry->res->flags;
820 u64 cpu_addr = entry->res->start;
821 u64 cpu_end = entry->res->end;
822 u64 pci_addr = entry->res->start - entry->offset;
823 u32 flags = LAM_64BIT | LAR_ENABLE;
825 u64 size = resource_size(entry->res);
828 if (restype & IORESOURCE_PREFETCH)
829 flags |= LAM_PREFETCH;
831 while (cpu_addr < cpu_end) {
832 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
833 dev_err(pcie->dev, "Failed to map inbound regions!\n");
837 * If the size of the range is larger than the alignment of
838 * the start address, we have to use multiple entries to
839 * perform the mapping.
842 unsigned long nr_zeros = __ffs64(cpu_addr);
843 u64 alignment = 1ULL << nr_zeros;
845 size = min(size, alignment);
847 /* Hardware supports max 4GiB inbound region */
848 size = min(size, 1ULL << 32);
850 mask = roundup_pow_of_two(size) - 1;
853 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
854 lower_32_bits(mask) | flags, idx, true);
865 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
867 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
868 struct resource_entry *entry;
869 int index = 0, err = 0;
871 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
872 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
880 static const struct of_device_id rcar_pcie_of_match[] = {
881 { .compatible = "renesas,pcie-r8a7779",
882 .data = rcar_pcie_phy_init_h1 },
883 { .compatible = "renesas,pcie-r8a7790",
884 .data = rcar_pcie_phy_init_gen2 },
885 { .compatible = "renesas,pcie-r8a7791",
886 .data = rcar_pcie_phy_init_gen2 },
887 { .compatible = "renesas,pcie-rcar-gen2",
888 .data = rcar_pcie_phy_init_gen2 },
889 { .compatible = "renesas,pcie-r8a7795",
890 .data = rcar_pcie_phy_init_gen3 },
891 { .compatible = "renesas,pcie-rcar-gen3",
892 .data = rcar_pcie_phy_init_gen3 },
896 static int rcar_pcie_probe(struct platform_device *pdev)
898 struct device *dev = &pdev->dev;
899 struct rcar_pcie_host *host;
900 struct rcar_pcie *pcie;
903 struct pci_host_bridge *bridge;
905 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
909 host = pci_host_bridge_priv(bridge);
912 platform_set_drvdata(pdev, host);
914 pm_runtime_enable(pcie->dev);
915 err = pm_runtime_get_sync(pcie->dev);
917 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
921 err = rcar_pcie_get_resources(host);
923 dev_err(dev, "failed to request resources: %d\n", err);
927 err = clk_prepare_enable(host->bus_clk);
929 dev_err(dev, "failed to enable bus clock: %d\n", err);
930 goto err_unmap_msi_irqs;
933 err = rcar_pcie_parse_map_dma_ranges(host);
935 goto err_clk_disable;
937 host->phy_init_fn = of_device_get_match_data(dev);
938 err = host->phy_init_fn(host);
940 dev_err(dev, "failed to init PCIe PHY\n");
941 goto err_clk_disable;
944 /* Failure to get a link might just be that no cards are inserted */
945 if (rcar_pcie_hw_init(pcie)) {
946 dev_info(dev, "PCIe link down\n");
948 goto err_phy_shutdown;
951 data = rcar_pci_read_reg(pcie, MACSR);
952 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
954 if (IS_ENABLED(CONFIG_PCI_MSI)) {
955 err = rcar_pcie_enable_msi(host);
958 "failed to enable MSI support: %d\n",
960 goto err_phy_shutdown;
964 err = rcar_pcie_enable(host);
966 goto err_msi_teardown;
971 if (IS_ENABLED(CONFIG_PCI_MSI))
972 rcar_pcie_teardown_msi(host);
976 phy_power_off(host->phy);
981 clk_disable_unprepare(host->bus_clk);
984 irq_dispose_mapping(host->msi.irq2);
985 irq_dispose_mapping(host->msi.irq1);
989 pm_runtime_disable(dev);
994 static int __maybe_unused rcar_pcie_resume(struct device *dev)
996 struct rcar_pcie_host *host = dev_get_drvdata(dev);
997 struct rcar_pcie *pcie = &host->pcie;
1001 err = rcar_pcie_parse_map_dma_ranges(host);
1005 /* Failure to get a link might just be that no cards are inserted */
1006 err = host->phy_init_fn(host);
1008 dev_info(dev, "PCIe link down\n");
1012 data = rcar_pci_read_reg(pcie, MACSR);
1013 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1016 if (IS_ENABLED(CONFIG_PCI_MSI))
1017 rcar_pcie_hw_enable_msi(host);
1019 rcar_pcie_hw_enable(host);
1024 static int rcar_pcie_resume_noirq(struct device *dev)
1026 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1027 struct rcar_pcie *pcie = &host->pcie;
1029 if (rcar_pci_read_reg(pcie, PMSR) &&
1030 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1033 /* Re-establish the PCIe link */
1034 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1035 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1036 return rcar_pcie_wait_for_dl(pcie);
1039 static const struct dev_pm_ops rcar_pcie_pm_ops = {
1040 SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1041 .resume_noirq = rcar_pcie_resume_noirq,
1044 static struct platform_driver rcar_pcie_driver = {
1046 .name = "rcar-pcie",
1047 .of_match_table = rcar_pcie_of_match,
1048 .pm = &rcar_pcie_pm_ops,
1049 .suppress_bind_attrs = true,
1051 .probe = rcar_pcie_probe,
1053 builtin_platform_driver(rcar_pcie_driver);