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/clk-provider.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/irqdomain.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/iopoll.h>
24 #include <linux/msi.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/pci.h>
29 #include <linux/phy/phy.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
33 #include "pcie-rcar.h"
36 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
37 struct irq_domain *domain;
38 struct mutex map_lock;
46 * Here we keep a static copy of the remapped PCIe controller address.
47 * This is only used on aarch32 systems, all of which have one single
48 * PCIe controller, to provide quick access to the PCIe controller in
49 * the L1 link state fixup function, called from the ARM fault handler.
51 static void __iomem *pcie_base;
53 * Static copy of bus clock pointer, so we can check whether the clock
56 static struct clk *pcie_bus_clk;
59 /* Structure representing the PCIe interface */
60 struct rcar_pcie_host {
61 struct rcar_pcie pcie;
65 int (*phy_init_fn)(struct rcar_pcie_host *host);
68 static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi)
70 return container_of(msi, struct rcar_pcie_host, msi);
73 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
75 unsigned int shift = BITS_PER_BYTE * (where & 3);
76 u32 val = rcar_pci_read_reg(pcie, where & ~3);
81 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
82 static int rcar_pcie_config_access(struct rcar_pcie_host *host,
83 unsigned char access_type, struct pci_bus *bus,
84 unsigned int devfn, int where, u32 *data)
86 struct rcar_pcie *pcie = &host->pcie;
87 unsigned int dev, func, reg, index;
89 dev = PCI_SLOT(devfn);
90 func = PCI_FUNC(devfn);
95 * While each channel has its own memory-mapped extended config
96 * space, it's generally only accessible when in endpoint mode.
97 * When in root complex mode, the controller is unable to target
98 * itself with either type 0 or type 1 accesses, and indeed, any
99 * controller initiated target transfer to its own config space
100 * result in a completer abort.
102 * Each channel effectively only supports a single device, but as
103 * the same channel <-> device access works for any PCI_SLOT()
104 * value, we cheat a bit here and bind the controller's config
105 * space to devfn 0 in order to enable self-enumeration. In this
106 * case the regular ECAR/ECDR path is sidelined and the mangled
107 * config access itself is initiated as an internal bus transaction.
109 if (pci_is_root_bus(bus)) {
111 return PCIBIOS_DEVICE_NOT_FOUND;
113 if (access_type == RCAR_PCI_ACCESS_READ)
114 *data = rcar_pci_read_reg(pcie, PCICONF(index));
116 rcar_pci_write_reg(pcie, *data, PCICONF(index));
118 return PCIBIOS_SUCCESSFUL;
122 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
124 /* Set the PIO address */
125 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
126 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
128 /* Enable the configuration access */
129 if (pci_is_root_bus(bus->parent))
130 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
132 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
134 /* Check for errors */
135 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
136 return PCIBIOS_DEVICE_NOT_FOUND;
138 /* Check for master and target aborts */
139 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
140 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
141 return PCIBIOS_DEVICE_NOT_FOUND;
143 if (access_type == RCAR_PCI_ACCESS_READ)
144 *data = rcar_pci_read_reg(pcie, PCIECDR);
146 rcar_pci_write_reg(pcie, *data, PCIECDR);
148 /* Disable the configuration access */
149 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
151 return PCIBIOS_SUCCESSFUL;
154 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
155 int where, int size, u32 *val)
157 struct rcar_pcie_host *host = bus->sysdata;
160 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
161 bus, devfn, where, val);
162 if (ret != PCIBIOS_SUCCESSFUL) {
168 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
170 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
172 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
173 bus->number, devfn, where, size, *val);
178 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
179 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
180 int where, int size, u32 val)
182 struct rcar_pcie_host *host = bus->sysdata;
187 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
188 bus, devfn, where, &data);
189 if (ret != PCIBIOS_SUCCESSFUL)
192 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
193 bus->number, devfn, where, size, val);
196 shift = BITS_PER_BYTE * (where & 3);
197 data &= ~(0xff << shift);
198 data |= ((val & 0xff) << shift);
199 } else if (size == 2) {
200 shift = BITS_PER_BYTE * (where & 2);
201 data &= ~(0xffff << shift);
202 data |= ((val & 0xffff) << shift);
206 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
207 bus, devfn, where, &data);
212 static struct pci_ops rcar_pcie_ops = {
213 .read = rcar_pcie_read_conf,
214 .write = rcar_pcie_write_conf,
217 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
219 struct device *dev = pcie->dev;
220 unsigned int timeout = 1000;
223 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
226 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
227 dev_err(dev, "Speed change already in progress\n");
231 macsr = rcar_pci_read_reg(pcie, MACSR);
232 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
235 /* Set target link speed to 5.0 GT/s */
236 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
237 PCI_EXP_LNKSTA_CLS_5_0GB);
239 /* Set speed change reason as intentional factor */
240 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
242 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
243 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
244 rcar_pci_write_reg(pcie, macsr, MACSR);
246 /* Start link speed change */
247 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
250 macsr = rcar_pci_read_reg(pcie, MACSR);
251 if (macsr & SPCHGFIN) {
252 /* Clear the interrupt bits */
253 rcar_pci_write_reg(pcie, macsr, MACSR);
255 if (macsr & SPCHGFAIL)
256 dev_err(dev, "Speed change failed\n");
264 dev_err(dev, "Speed change timed out\n");
267 dev_info(dev, "Current link speed is %s GT/s\n",
268 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
271 static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
273 struct rcar_pcie *pcie = &host->pcie;
274 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
275 struct resource_entry *win;
279 /* Try setting 5 GT/s link speed */
280 rcar_pcie_force_speedup(pcie);
282 /* Setup PCI resources */
283 resource_list_for_each_entry(win, &bridge->windows) {
284 struct resource *res = win->res;
289 switch (resource_type(res)) {
292 rcar_pcie_set_outbound(pcie, i, win);
299 static int rcar_pcie_enable(struct rcar_pcie_host *host)
301 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
303 rcar_pcie_hw_enable(host);
305 pci_add_flags(PCI_REASSIGN_ALL_BUS);
307 bridge->sysdata = host;
308 bridge->ops = &rcar_pcie_ops;
310 return pci_host_probe(bridge);
313 static int phy_wait_for_ack(struct rcar_pcie *pcie)
315 struct device *dev = pcie->dev;
316 unsigned int timeout = 100;
319 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
325 dev_err(dev, "Access to PCIe phy timed out\n");
330 static void phy_write_reg(struct rcar_pcie *pcie,
331 unsigned int rate, u32 addr,
332 unsigned int lane, u32 data)
336 phyaddr = WRITE_CMD |
337 ((rate & 1) << RATE_POS) |
338 ((lane & 0xf) << LANE_POS) |
339 ((addr & 0xff) << ADR_POS);
342 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
343 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
345 /* Ignore errors as they will be dealt with if the data link is down */
346 phy_wait_for_ack(pcie);
349 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
350 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
352 /* Ignore errors as they will be dealt with if the data link is down */
353 phy_wait_for_ack(pcie);
356 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
360 /* Begin initialization */
361 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
364 rcar_pci_write_reg(pcie, 1, PCIEMSR);
366 err = rcar_pcie_wait_for_phyrdy(pcie);
371 * Initial header for port config space is type 1, set the device
372 * class to match. Hardware takes care of propagating the IDSETR
373 * settings, so there is no need to bother with a quirk.
375 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
378 * Setup Secondary Bus Number & Subordinate Bus Number, even though
379 * they aren't used, to avoid bridge being detected as broken.
381 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
382 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
384 /* Initialize default capabilities. */
385 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
386 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
387 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
388 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
389 PCI_HEADER_TYPE_BRIDGE);
391 /* Enable data link layer active state reporting */
392 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
393 PCI_EXP_LNKCAP_DLLLARC);
395 /* Write out the physical slot number = 0 */
396 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
398 /* Set the completion timer timeout to the maximum 50ms. */
399 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
401 /* Terminate list of capabilities (Next Capability Offset=0) */
402 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
405 if (IS_ENABLED(CONFIG_PCI_MSI))
406 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
408 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
410 /* Finish initialization - establish a PCI Express link */
411 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
413 /* This will timeout if we don't have a link. */
414 err = rcar_pcie_wait_for_dl(pcie);
418 /* Enable INTx interrupts */
419 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
426 static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
428 struct rcar_pcie *pcie = &host->pcie;
430 /* Initialize the phy */
431 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
432 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
433 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
434 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
435 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
436 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
437 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
438 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
439 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
440 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
441 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
442 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
444 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
445 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
446 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
451 static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
453 struct rcar_pcie *pcie = &host->pcie;
456 * These settings come from the R-Car Series, 2nd Generation User's
457 * Manual, section 50.3.1 (2) Initialization of the physical layer.
459 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
460 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
461 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
462 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
464 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
465 /* The following value is for DC connection, no termination resistor */
466 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
467 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
468 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
473 static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
477 err = phy_init(host->phy);
481 err = phy_power_on(host->phy);
488 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
490 struct rcar_pcie_host *host = data;
491 struct rcar_pcie *pcie = &host->pcie;
492 struct rcar_msi *msi = &host->msi;
493 struct device *dev = pcie->dev;
496 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
498 /* MSI & INTx share an interrupt - we only handle MSI here */
503 unsigned int index = find_first_bit(®, 32);
506 ret = generic_handle_domain_irq(msi->domain->parent, index);
508 /* Unknown MSI, just clear it */
509 dev_dbg(dev, "unexpected MSI\n");
510 rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR);
513 /* see if there's any more pending in this vector */
514 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
520 static void rcar_msi_top_irq_ack(struct irq_data *d)
522 irq_chip_ack_parent(d);
525 static void rcar_msi_top_irq_mask(struct irq_data *d)
528 irq_chip_mask_parent(d);
531 static void rcar_msi_top_irq_unmask(struct irq_data *d)
533 pci_msi_unmask_irq(d);
534 irq_chip_unmask_parent(d);
537 static struct irq_chip rcar_msi_top_chip = {
539 .irq_ack = rcar_msi_top_irq_ack,
540 .irq_mask = rcar_msi_top_irq_mask,
541 .irq_unmask = rcar_msi_top_irq_unmask,
544 static void rcar_msi_irq_ack(struct irq_data *d)
546 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
547 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
549 /* clear the interrupt */
550 rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR);
553 static void rcar_msi_irq_mask(struct irq_data *d)
555 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
556 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
560 spin_lock_irqsave(&msi->mask_lock, flags);
561 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
562 value &= ~BIT(d->hwirq);
563 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
564 spin_unlock_irqrestore(&msi->mask_lock, flags);
567 static void rcar_msi_irq_unmask(struct irq_data *d)
569 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
570 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
574 spin_lock_irqsave(&msi->mask_lock, flags);
575 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
576 value |= BIT(d->hwirq);
577 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
578 spin_unlock_irqrestore(&msi->mask_lock, flags);
581 static int rcar_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force)
586 static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
588 struct rcar_msi *msi = irq_data_get_irq_chip_data(data);
589 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
591 msg->address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
592 msg->address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
593 msg->data = data->hwirq;
596 static struct irq_chip rcar_msi_bottom_chip = {
598 .irq_ack = rcar_msi_irq_ack,
599 .irq_mask = rcar_msi_irq_mask,
600 .irq_unmask = rcar_msi_irq_unmask,
601 .irq_set_affinity = rcar_msi_set_affinity,
602 .irq_compose_msi_msg = rcar_compose_msi_msg,
605 static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
606 unsigned int nr_irqs, void *args)
608 struct rcar_msi *msi = domain->host_data;
612 mutex_lock(&msi->map_lock);
614 hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
616 mutex_unlock(&msi->map_lock);
621 for (i = 0; i < nr_irqs; i++)
622 irq_domain_set_info(domain, virq + i, hwirq + i,
623 &rcar_msi_bottom_chip, domain->host_data,
624 handle_edge_irq, NULL, NULL);
629 static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq,
630 unsigned int nr_irqs)
632 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
633 struct rcar_msi *msi = domain->host_data;
635 mutex_lock(&msi->map_lock);
637 bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
639 mutex_unlock(&msi->map_lock);
642 static const struct irq_domain_ops rcar_msi_domain_ops = {
643 .alloc = rcar_msi_domain_alloc,
644 .free = rcar_msi_domain_free,
647 static struct msi_domain_info rcar_msi_info = {
648 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
649 MSI_FLAG_MULTI_PCI_MSI),
650 .chip = &rcar_msi_top_chip,
653 static int rcar_allocate_domains(struct rcar_msi *msi)
655 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
656 struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
657 struct irq_domain *parent;
659 parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR,
660 &rcar_msi_domain_ops, msi);
662 dev_err(pcie->dev, "failed to create IRQ domain\n");
665 irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
667 msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent);
669 dev_err(pcie->dev, "failed to create MSI domain\n");
670 irq_domain_remove(parent);
677 static void rcar_free_domains(struct rcar_msi *msi)
679 struct irq_domain *parent = msi->domain->parent;
681 irq_domain_remove(msi->domain);
682 irq_domain_remove(parent);
685 static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
687 struct rcar_pcie *pcie = &host->pcie;
688 struct device *dev = pcie->dev;
689 struct rcar_msi *msi = &host->msi;
693 mutex_init(&msi->map_lock);
694 spin_lock_init(&msi->mask_lock);
696 err = of_address_to_resource(dev->of_node, 0, &res);
700 err = rcar_allocate_domains(msi);
704 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
705 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
706 IRQF_SHARED | IRQF_NO_THREAD,
707 rcar_msi_bottom_chip.name, host);
709 dev_err(dev, "failed to request IRQ: %d\n", err);
713 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
714 IRQF_SHARED | IRQF_NO_THREAD,
715 rcar_msi_bottom_chip.name, host);
717 dev_err(dev, "failed to request IRQ: %d\n", err);
721 /* disable all MSIs */
722 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
725 * Setup MSI data target using RC base address address, which
726 * is guaranteed to be in the low 32bit range on any RCar HW.
728 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
729 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
734 rcar_free_domains(msi);
738 static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
740 struct rcar_pcie *pcie = &host->pcie;
742 /* Disable all MSI interrupts */
743 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
745 /* Disable address decoding of the MSI interrupt, MSIFE */
746 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
748 rcar_free_domains(&host->msi);
751 static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
753 struct rcar_pcie *pcie = &host->pcie;
754 struct device *dev = pcie->dev;
758 host->phy = devm_phy_optional_get(dev, "pcie");
759 if (IS_ERR(host->phy))
760 return PTR_ERR(host->phy);
762 err = of_address_to_resource(dev->of_node, 0, &res);
766 pcie->base = devm_ioremap_resource(dev, &res);
767 if (IS_ERR(pcie->base))
768 return PTR_ERR(pcie->base);
770 host->bus_clk = devm_clk_get(dev, "pcie_bus");
771 if (IS_ERR(host->bus_clk)) {
772 dev_err(dev, "cannot get pcie bus clock\n");
773 return PTR_ERR(host->bus_clk);
776 i = irq_of_parse_and_map(dev->of_node, 0);
778 dev_err(dev, "cannot get platform resources for msi interrupt\n");
784 i = irq_of_parse_and_map(dev->of_node, 1);
786 dev_err(dev, "cannot get platform resources for msi interrupt\n");
793 /* Cache static copy for L1 link state fixup hook on aarch32 */
794 pcie_base = pcie->base;
795 pcie_bus_clk = host->bus_clk;
801 irq_dispose_mapping(host->msi.irq1);
806 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
807 struct resource_entry *entry,
810 u64 restype = entry->res->flags;
811 u64 cpu_addr = entry->res->start;
812 u64 cpu_end = entry->res->end;
813 u64 pci_addr = entry->res->start - entry->offset;
814 u32 flags = LAM_64BIT | LAR_ENABLE;
816 u64 size = resource_size(entry->res);
819 if (restype & IORESOURCE_PREFETCH)
820 flags |= LAM_PREFETCH;
822 while (cpu_addr < cpu_end) {
823 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
824 dev_err(pcie->dev, "Failed to map inbound regions!\n");
828 * If the size of the range is larger than the alignment of
829 * the start address, we have to use multiple entries to
830 * perform the mapping.
833 unsigned long nr_zeros = __ffs64(cpu_addr);
834 u64 alignment = 1ULL << nr_zeros;
836 size = min(size, alignment);
838 /* Hardware supports max 4GiB inbound region */
839 size = min(size, 1ULL << 32);
841 mask = roundup_pow_of_two(size) - 1;
844 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
845 lower_32_bits(mask) | flags, idx, true);
856 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
858 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
859 struct resource_entry *entry;
860 int index = 0, err = 0;
862 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
863 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
871 static const struct of_device_id rcar_pcie_of_match[] = {
872 { .compatible = "renesas,pcie-r8a7779",
873 .data = rcar_pcie_phy_init_h1 },
874 { .compatible = "renesas,pcie-r8a7790",
875 .data = rcar_pcie_phy_init_gen2 },
876 { .compatible = "renesas,pcie-r8a7791",
877 .data = rcar_pcie_phy_init_gen2 },
878 { .compatible = "renesas,pcie-rcar-gen2",
879 .data = rcar_pcie_phy_init_gen2 },
880 { .compatible = "renesas,pcie-r8a7795",
881 .data = rcar_pcie_phy_init_gen3 },
882 { .compatible = "renesas,pcie-rcar-gen3",
883 .data = rcar_pcie_phy_init_gen3 },
887 static int rcar_pcie_probe(struct platform_device *pdev)
889 struct device *dev = &pdev->dev;
890 struct rcar_pcie_host *host;
891 struct rcar_pcie *pcie;
894 struct pci_host_bridge *bridge;
896 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
900 host = pci_host_bridge_priv(bridge);
903 platform_set_drvdata(pdev, host);
905 pm_runtime_enable(pcie->dev);
906 err = pm_runtime_get_sync(pcie->dev);
908 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
912 err = rcar_pcie_get_resources(host);
914 dev_err(dev, "failed to request resources: %d\n", err);
918 err = clk_prepare_enable(host->bus_clk);
920 dev_err(dev, "failed to enable bus clock: %d\n", err);
921 goto err_unmap_msi_irqs;
924 err = rcar_pcie_parse_map_dma_ranges(host);
926 goto err_clk_disable;
928 host->phy_init_fn = of_device_get_match_data(dev);
929 err = host->phy_init_fn(host);
931 dev_err(dev, "failed to init PCIe PHY\n");
932 goto err_clk_disable;
935 /* Failure to get a link might just be that no cards are inserted */
936 if (rcar_pcie_hw_init(pcie)) {
937 dev_info(dev, "PCIe link down\n");
939 goto err_phy_shutdown;
942 data = rcar_pci_read_reg(pcie, MACSR);
943 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
945 if (IS_ENABLED(CONFIG_PCI_MSI)) {
946 err = rcar_pcie_enable_msi(host);
949 "failed to enable MSI support: %d\n",
951 goto err_phy_shutdown;
955 err = rcar_pcie_enable(host);
957 goto err_msi_teardown;
962 if (IS_ENABLED(CONFIG_PCI_MSI))
963 rcar_pcie_teardown_msi(host);
967 phy_power_off(host->phy);
972 clk_disable_unprepare(host->bus_clk);
975 irq_dispose_mapping(host->msi.irq2);
976 irq_dispose_mapping(host->msi.irq1);
980 pm_runtime_disable(dev);
985 static int __maybe_unused rcar_pcie_resume(struct device *dev)
987 struct rcar_pcie_host *host = dev_get_drvdata(dev);
988 struct rcar_pcie *pcie = &host->pcie;
992 err = rcar_pcie_parse_map_dma_ranges(host);
996 /* Failure to get a link might just be that no cards are inserted */
997 err = host->phy_init_fn(host);
999 dev_info(dev, "PCIe link down\n");
1003 data = rcar_pci_read_reg(pcie, MACSR);
1004 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1007 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1008 struct resource res;
1011 of_address_to_resource(dev->of_node, 0, &res);
1012 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
1013 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
1015 bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR);
1016 rcar_pci_write_reg(pcie, val, PCIEMSIIER);
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,
1055 static DEFINE_SPINLOCK(pmsr_lock);
1056 static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
1057 unsigned int fsr, struct pt_regs *regs)
1059 unsigned long flags;
1063 spin_lock_irqsave(&pmsr_lock, flags);
1065 if (!pcie_base || !__clk_is_enabled(pcie_bus_clk)) {
1070 pmsr = readl(pcie_base + PMSR);
1073 * Test if the PCIe controller received PM_ENTER_L1 DLLP and
1074 * the PCIe controller is not in L1 link state. If true, apply
1075 * fix, which will put the controller into L1 link state, from
1076 * which it can return to L0s/L0 on its own.
1078 if ((pmsr & PMEL1RX) && ((pmsr & PMSTATE) != PMSTATE_L1)) {
1079 writel(L1IATN, pcie_base + PMCTLR);
1080 ret = readl_poll_timeout_atomic(pcie_base + PMSR, val,
1081 val & L1FAEG, 10, 1000);
1082 WARN(ret, "Timeout waiting for L1 link state, ret=%d\n", ret);
1083 writel(L1FAEG | PMEL1RX, pcie_base + PMSR);
1087 spin_unlock_irqrestore(&pmsr_lock, flags);
1091 static const struct of_device_id rcar_pcie_abort_handler_of_match[] __initconst = {
1092 { .compatible = "renesas,pcie-r8a7779" },
1093 { .compatible = "renesas,pcie-r8a7790" },
1094 { .compatible = "renesas,pcie-r8a7791" },
1095 { .compatible = "renesas,pcie-rcar-gen2" },
1099 static int __init rcar_pcie_init(void)
1101 if (of_find_matching_node(NULL, rcar_pcie_abort_handler_of_match)) {
1102 #ifdef CONFIG_ARM_LPAE
1103 hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1104 "asynchronous external abort");
1106 hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1107 "imprecise external abort");
1111 return platform_driver_register(&rcar_pcie_driver);
1113 device_initcall(rcar_pcie_init);
1115 builtin_platform_driver(rcar_pcie_driver);