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 PCIe device pointer, so we can check whether the
54 * device is runtime suspended or not.
56 static struct device *pcie_dev;
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 DEFINE_SPINLOCK(pmsr_lock);
70 static int rcar_pcie_wakeup(struct device *pcie_dev, void __iomem *pcie_base)
76 spin_lock_irqsave(&pmsr_lock, flags);
78 if (!pcie_base || pm_runtime_suspended(pcie_dev)) {
83 pmsr = readl(pcie_base + PMSR);
86 * Test if the PCIe controller received PM_ENTER_L1 DLLP and
87 * the PCIe controller is not in L1 link state. If true, apply
88 * fix, which will put the controller into L1 link state, from
89 * which it can return to L0s/L0 on its own.
91 if ((pmsr & PMEL1RX) && ((pmsr & PMSTATE) != PMSTATE_L1)) {
92 writel(L1IATN, pcie_base + PMCTLR);
93 ret = readl_poll_timeout_atomic(pcie_base + PMSR, val,
94 val & L1FAEG, 10, 1000);
95 WARN(ret, "Timeout waiting for L1 link state, ret=%d\n", ret);
96 writel(L1FAEG | PMEL1RX, pcie_base + PMSR);
100 spin_unlock_irqrestore(&pmsr_lock, flags);
104 static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi)
106 return container_of(msi, struct rcar_pcie_host, msi);
109 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
111 unsigned int shift = BITS_PER_BYTE * (where & 3);
112 u32 val = rcar_pci_read_reg(pcie, where & ~3);
118 #define __rcar_pci_rw_reg_workaround(instr) \
120 "1: " instr " %1, [%2]\n" \
122 "3: .pushsection .text.fixup,\"ax\"\n" \
124 "4: mov %0, #" __stringify(PCIBIOS_SET_FAILED) "\n" \
127 " .pushsection __ex_table,\"a\"\n" \
134 static int rcar_pci_write_reg_workaround(struct rcar_pcie *pcie, u32 val,
137 int error = PCIBIOS_SUCCESSFUL;
140 __rcar_pci_rw_reg_workaround("str")
141 : "+r"(error):"r"(val), "r"(pcie->base + reg) : "memory");
143 rcar_pci_write_reg(pcie, val, reg);
148 static int rcar_pci_read_reg_workaround(struct rcar_pcie *pcie, u32 *val,
151 int error = PCIBIOS_SUCCESSFUL;
154 __rcar_pci_rw_reg_workaround("ldr")
155 : "+r"(error), "=r"(*val) : "r"(pcie->base + reg) : "memory");
157 if (error != PCIBIOS_SUCCESSFUL)
158 PCI_SET_ERROR_RESPONSE(val);
160 *val = rcar_pci_read_reg(pcie, reg);
165 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
166 static int rcar_pcie_config_access(struct rcar_pcie_host *host,
167 unsigned char access_type, struct pci_bus *bus,
168 unsigned int devfn, int where, u32 *data)
170 struct rcar_pcie *pcie = &host->pcie;
171 unsigned int dev, func, reg, index;
174 /* Wake the bus up in case it is in L1 state. */
175 ret = rcar_pcie_wakeup(pcie->dev, pcie->base);
177 PCI_SET_ERROR_RESPONSE(data);
178 return PCIBIOS_SET_FAILED;
181 dev = PCI_SLOT(devfn);
182 func = PCI_FUNC(devfn);
187 * While each channel has its own memory-mapped extended config
188 * space, it's generally only accessible when in endpoint mode.
189 * When in root complex mode, the controller is unable to target
190 * itself with either type 0 or type 1 accesses, and indeed, any
191 * controller initiated target transfer to its own config space
192 * result in a completer abort.
194 * Each channel effectively only supports a single device, but as
195 * the same channel <-> device access works for any PCI_SLOT()
196 * value, we cheat a bit here and bind the controller's config
197 * space to devfn 0 in order to enable self-enumeration. In this
198 * case the regular ECAR/ECDR path is sidelined and the mangled
199 * config access itself is initiated as an internal bus transaction.
201 if (pci_is_root_bus(bus)) {
203 return PCIBIOS_DEVICE_NOT_FOUND;
205 if (access_type == RCAR_PCI_ACCESS_READ)
206 *data = rcar_pci_read_reg(pcie, PCICONF(index));
208 rcar_pci_write_reg(pcie, *data, PCICONF(index));
210 return PCIBIOS_SUCCESSFUL;
214 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
216 /* Set the PIO address */
217 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
218 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
220 /* Enable the configuration access */
221 if (pci_is_root_bus(bus->parent))
222 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
224 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
226 /* Check for errors */
227 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
228 return PCIBIOS_DEVICE_NOT_FOUND;
230 /* Check for master and target aborts */
231 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
232 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
233 return PCIBIOS_DEVICE_NOT_FOUND;
235 if (access_type == RCAR_PCI_ACCESS_READ)
236 ret = rcar_pci_read_reg_workaround(pcie, data, PCIECDR);
238 ret = rcar_pci_write_reg_workaround(pcie, *data, PCIECDR);
240 /* Disable the configuration access */
241 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
246 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
247 int where, int size, u32 *val)
249 struct rcar_pcie_host *host = bus->sysdata;
252 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
253 bus, devfn, where, val);
254 if (ret != PCIBIOS_SUCCESSFUL)
258 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
260 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
262 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
263 bus->number, devfn, where, size, *val);
268 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
269 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
270 int where, int size, u32 val)
272 struct rcar_pcie_host *host = bus->sysdata;
277 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
278 bus, devfn, where, &data);
279 if (ret != PCIBIOS_SUCCESSFUL)
282 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
283 bus->number, devfn, where, size, val);
286 shift = BITS_PER_BYTE * (where & 3);
287 data &= ~(0xff << shift);
288 data |= ((val & 0xff) << shift);
289 } else if (size == 2) {
290 shift = BITS_PER_BYTE * (where & 2);
291 data &= ~(0xffff << shift);
292 data |= ((val & 0xffff) << shift);
296 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
297 bus, devfn, where, &data);
302 static struct pci_ops rcar_pcie_ops = {
303 .read = rcar_pcie_read_conf,
304 .write = rcar_pcie_write_conf,
307 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
309 struct device *dev = pcie->dev;
310 unsigned int timeout = 1000;
313 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
316 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
317 dev_err(dev, "Speed change already in progress\n");
321 macsr = rcar_pci_read_reg(pcie, MACSR);
322 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
325 /* Set target link speed to 5.0 GT/s */
326 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
327 PCI_EXP_LNKSTA_CLS_5_0GB);
329 /* Set speed change reason as intentional factor */
330 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
332 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
333 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
334 rcar_pci_write_reg(pcie, macsr, MACSR);
336 /* Start link speed change */
337 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
340 macsr = rcar_pci_read_reg(pcie, MACSR);
341 if (macsr & SPCHGFIN) {
342 /* Clear the interrupt bits */
343 rcar_pci_write_reg(pcie, macsr, MACSR);
345 if (macsr & SPCHGFAIL)
346 dev_err(dev, "Speed change failed\n");
354 dev_err(dev, "Speed change timed out\n");
357 dev_info(dev, "Current link speed is %s GT/s\n",
358 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
361 static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
363 struct rcar_pcie *pcie = &host->pcie;
364 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
365 struct resource_entry *win;
369 /* Try setting 5 GT/s link speed */
370 rcar_pcie_force_speedup(pcie);
372 /* Setup PCI resources */
373 resource_list_for_each_entry(win, &bridge->windows) {
374 struct resource *res = win->res;
379 switch (resource_type(res)) {
382 rcar_pcie_set_outbound(pcie, i, win);
389 static int rcar_pcie_enable(struct rcar_pcie_host *host)
391 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
393 rcar_pcie_hw_enable(host);
395 pci_add_flags(PCI_REASSIGN_ALL_BUS);
397 bridge->sysdata = host;
398 bridge->ops = &rcar_pcie_ops;
400 return pci_host_probe(bridge);
403 static int phy_wait_for_ack(struct rcar_pcie *pcie)
405 struct device *dev = pcie->dev;
406 unsigned int timeout = 100;
409 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
415 dev_err(dev, "Access to PCIe phy timed out\n");
420 static void phy_write_reg(struct rcar_pcie *pcie,
421 unsigned int rate, u32 addr,
422 unsigned int lane, u32 data)
426 phyaddr = WRITE_CMD |
427 ((rate & 1) << RATE_POS) |
428 ((lane & 0xf) << LANE_POS) |
429 ((addr & 0xff) << ADR_POS);
432 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
433 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
435 /* Ignore errors as they will be dealt with if the data link is down */
436 phy_wait_for_ack(pcie);
439 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
440 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
442 /* Ignore errors as they will be dealt with if the data link is down */
443 phy_wait_for_ack(pcie);
446 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
450 /* Begin initialization */
451 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
454 rcar_pci_write_reg(pcie, 1, PCIEMSR);
456 err = rcar_pcie_wait_for_phyrdy(pcie);
461 * Initial header for port config space is type 1, set the device
462 * class to match. Hardware takes care of propagating the IDSETR
463 * settings, so there is no need to bother with a quirk.
465 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI_NORMAL << 8, IDSETR1);
468 * Setup Secondary Bus Number & Subordinate Bus Number, even though
469 * they aren't used, to avoid bridge being detected as broken.
471 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
472 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
474 /* Initialize default capabilities. */
475 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
476 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
477 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
478 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
479 PCI_HEADER_TYPE_BRIDGE);
481 /* Enable data link layer active state reporting */
482 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
483 PCI_EXP_LNKCAP_DLLLARC);
485 /* Write out the physical slot number = 0 */
486 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
488 /* Set the completion timer timeout to the maximum 50ms. */
489 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
491 /* Terminate list of capabilities (Next Capability Offset=0) */
492 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
495 if (IS_ENABLED(CONFIG_PCI_MSI))
496 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
498 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
500 /* Finish initialization - establish a PCI Express link */
501 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
503 /* This will timeout if we don't have a link. */
504 err = rcar_pcie_wait_for_dl(pcie);
508 /* Enable INTx interrupts */
509 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
516 static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
518 struct rcar_pcie *pcie = &host->pcie;
520 /* Initialize the phy */
521 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
522 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
523 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
524 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
525 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
526 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
527 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
528 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
529 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
530 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
531 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
532 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
534 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
535 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
536 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
541 static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
543 struct rcar_pcie *pcie = &host->pcie;
546 * These settings come from the R-Car Series, 2nd Generation User's
547 * Manual, section 50.3.1 (2) Initialization of the physical layer.
549 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
550 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
551 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
552 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
554 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
555 /* The following value is for DC connection, no termination resistor */
556 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
557 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
558 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
563 static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
567 err = phy_init(host->phy);
571 err = phy_power_on(host->phy);
578 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
580 struct rcar_pcie_host *host = data;
581 struct rcar_pcie *pcie = &host->pcie;
582 struct rcar_msi *msi = &host->msi;
583 struct device *dev = pcie->dev;
586 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
588 /* MSI & INTx share an interrupt - we only handle MSI here */
593 unsigned int index = find_first_bit(®, 32);
596 ret = generic_handle_domain_irq(msi->domain->parent, index);
598 /* Unknown MSI, just clear it */
599 dev_dbg(dev, "unexpected MSI\n");
600 rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR);
603 /* see if there's any more pending in this vector */
604 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
610 static void rcar_msi_top_irq_ack(struct irq_data *d)
612 irq_chip_ack_parent(d);
615 static void rcar_msi_top_irq_mask(struct irq_data *d)
618 irq_chip_mask_parent(d);
621 static void rcar_msi_top_irq_unmask(struct irq_data *d)
623 pci_msi_unmask_irq(d);
624 irq_chip_unmask_parent(d);
627 static struct irq_chip rcar_msi_top_chip = {
629 .irq_ack = rcar_msi_top_irq_ack,
630 .irq_mask = rcar_msi_top_irq_mask,
631 .irq_unmask = rcar_msi_top_irq_unmask,
634 static void rcar_msi_irq_ack(struct irq_data *d)
636 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
637 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
639 /* clear the interrupt */
640 rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR);
643 static void rcar_msi_irq_mask(struct irq_data *d)
645 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
646 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
650 spin_lock_irqsave(&msi->mask_lock, flags);
651 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
652 value &= ~BIT(d->hwirq);
653 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
654 spin_unlock_irqrestore(&msi->mask_lock, flags);
657 static void rcar_msi_irq_unmask(struct irq_data *d)
659 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
660 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
664 spin_lock_irqsave(&msi->mask_lock, flags);
665 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
666 value |= BIT(d->hwirq);
667 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
668 spin_unlock_irqrestore(&msi->mask_lock, flags);
671 static int rcar_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force)
676 static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
678 struct rcar_msi *msi = irq_data_get_irq_chip_data(data);
679 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
681 msg->address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
682 msg->address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
683 msg->data = data->hwirq;
686 static struct irq_chip rcar_msi_bottom_chip = {
688 .irq_ack = rcar_msi_irq_ack,
689 .irq_mask = rcar_msi_irq_mask,
690 .irq_unmask = rcar_msi_irq_unmask,
691 .irq_set_affinity = rcar_msi_set_affinity,
692 .irq_compose_msi_msg = rcar_compose_msi_msg,
695 static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
696 unsigned int nr_irqs, void *args)
698 struct rcar_msi *msi = domain->host_data;
702 mutex_lock(&msi->map_lock);
704 hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
706 mutex_unlock(&msi->map_lock);
711 for (i = 0; i < nr_irqs; i++)
712 irq_domain_set_info(domain, virq + i, hwirq + i,
713 &rcar_msi_bottom_chip, domain->host_data,
714 handle_edge_irq, NULL, NULL);
719 static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq,
720 unsigned int nr_irqs)
722 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
723 struct rcar_msi *msi = domain->host_data;
725 mutex_lock(&msi->map_lock);
727 bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
729 mutex_unlock(&msi->map_lock);
732 static const struct irq_domain_ops rcar_msi_domain_ops = {
733 .alloc = rcar_msi_domain_alloc,
734 .free = rcar_msi_domain_free,
737 static struct msi_domain_info rcar_msi_info = {
738 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
739 MSI_FLAG_MULTI_PCI_MSI),
740 .chip = &rcar_msi_top_chip,
743 static int rcar_allocate_domains(struct rcar_msi *msi)
745 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
746 struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
747 struct irq_domain *parent;
749 parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR,
750 &rcar_msi_domain_ops, msi);
752 dev_err(pcie->dev, "failed to create IRQ domain\n");
755 irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
757 msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent);
759 dev_err(pcie->dev, "failed to create MSI domain\n");
760 irq_domain_remove(parent);
767 static void rcar_free_domains(struct rcar_msi *msi)
769 struct irq_domain *parent = msi->domain->parent;
771 irq_domain_remove(msi->domain);
772 irq_domain_remove(parent);
775 static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
777 struct rcar_pcie *pcie = &host->pcie;
778 struct device *dev = pcie->dev;
779 struct rcar_msi *msi = &host->msi;
783 mutex_init(&msi->map_lock);
784 spin_lock_init(&msi->mask_lock);
786 err = of_address_to_resource(dev->of_node, 0, &res);
790 err = rcar_allocate_domains(msi);
794 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
795 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
796 IRQF_SHARED | IRQF_NO_THREAD,
797 rcar_msi_bottom_chip.name, host);
799 dev_err(dev, "failed to request IRQ: %d\n", err);
803 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
804 IRQF_SHARED | IRQF_NO_THREAD,
805 rcar_msi_bottom_chip.name, host);
807 dev_err(dev, "failed to request IRQ: %d\n", err);
811 /* disable all MSIs */
812 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
815 * Setup MSI data target using RC base address address, which
816 * is guaranteed to be in the low 32bit range on any RCar HW.
818 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
819 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
824 rcar_free_domains(msi);
828 static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
830 struct rcar_pcie *pcie = &host->pcie;
832 /* Disable all MSI interrupts */
833 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
835 /* Disable address decoding of the MSI interrupt, MSIFE */
836 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
838 rcar_free_domains(&host->msi);
841 static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
843 struct rcar_pcie *pcie = &host->pcie;
844 struct device *dev = pcie->dev;
848 host->phy = devm_phy_optional_get(dev, "pcie");
849 if (IS_ERR(host->phy))
850 return PTR_ERR(host->phy);
852 err = of_address_to_resource(dev->of_node, 0, &res);
856 pcie->base = devm_ioremap_resource(dev, &res);
857 if (IS_ERR(pcie->base))
858 return PTR_ERR(pcie->base);
860 host->bus_clk = devm_clk_get(dev, "pcie_bus");
861 if (IS_ERR(host->bus_clk)) {
862 dev_err(dev, "cannot get pcie bus clock\n");
863 return PTR_ERR(host->bus_clk);
866 i = irq_of_parse_and_map(dev->of_node, 0);
868 dev_err(dev, "cannot get platform resources for msi interrupt\n");
874 i = irq_of_parse_and_map(dev->of_node, 1);
876 dev_err(dev, "cannot get platform resources for msi interrupt\n");
883 /* Cache static copy for L1 link state fixup hook on aarch32 */
884 pcie_base = pcie->base;
885 pcie_dev = pcie->dev;
891 irq_dispose_mapping(host->msi.irq1);
896 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
897 struct resource_entry *entry,
900 u64 restype = entry->res->flags;
901 u64 cpu_addr = entry->res->start;
902 u64 cpu_end = entry->res->end;
903 u64 pci_addr = entry->res->start - entry->offset;
904 u32 flags = LAM_64BIT | LAR_ENABLE;
906 u64 size = resource_size(entry->res);
909 if (restype & IORESOURCE_PREFETCH)
910 flags |= LAM_PREFETCH;
912 while (cpu_addr < cpu_end) {
913 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
914 dev_err(pcie->dev, "Failed to map inbound regions!\n");
918 * If the size of the range is larger than the alignment of
919 * the start address, we have to use multiple entries to
920 * perform the mapping.
923 unsigned long nr_zeros = __ffs64(cpu_addr);
924 u64 alignment = 1ULL << nr_zeros;
926 size = min(size, alignment);
928 /* Hardware supports max 4GiB inbound region */
929 size = min(size, 1ULL << 32);
931 mask = roundup_pow_of_two(size) - 1;
934 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
935 lower_32_bits(mask) | flags, idx, true);
946 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
948 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
949 struct resource_entry *entry;
950 int index = 0, err = 0;
952 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
953 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
961 static const struct of_device_id rcar_pcie_of_match[] = {
962 { .compatible = "renesas,pcie-r8a7779",
963 .data = rcar_pcie_phy_init_h1 },
964 { .compatible = "renesas,pcie-r8a7790",
965 .data = rcar_pcie_phy_init_gen2 },
966 { .compatible = "renesas,pcie-r8a7791",
967 .data = rcar_pcie_phy_init_gen2 },
968 { .compatible = "renesas,pcie-rcar-gen2",
969 .data = rcar_pcie_phy_init_gen2 },
970 { .compatible = "renesas,pcie-r8a7795",
971 .data = rcar_pcie_phy_init_gen3 },
972 { .compatible = "renesas,pcie-rcar-gen3",
973 .data = rcar_pcie_phy_init_gen3 },
977 static int rcar_pcie_probe(struct platform_device *pdev)
979 struct device *dev = &pdev->dev;
980 struct rcar_pcie_host *host;
981 struct rcar_pcie *pcie;
984 struct pci_host_bridge *bridge;
986 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
990 host = pci_host_bridge_priv(bridge);
993 platform_set_drvdata(pdev, host);
995 pm_runtime_enable(pcie->dev);
996 err = pm_runtime_get_sync(pcie->dev);
998 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
1002 err = rcar_pcie_get_resources(host);
1004 dev_err(dev, "failed to request resources: %d\n", err);
1008 err = clk_prepare_enable(host->bus_clk);
1010 dev_err(dev, "failed to enable bus clock: %d\n", err);
1011 goto err_unmap_msi_irqs;
1014 err = rcar_pcie_parse_map_dma_ranges(host);
1016 goto err_clk_disable;
1018 host->phy_init_fn = of_device_get_match_data(dev);
1019 err = host->phy_init_fn(host);
1021 dev_err(dev, "failed to init PCIe PHY\n");
1022 goto err_clk_disable;
1025 /* Failure to get a link might just be that no cards are inserted */
1026 if (rcar_pcie_hw_init(pcie)) {
1027 dev_info(dev, "PCIe link down\n");
1029 goto err_phy_shutdown;
1032 data = rcar_pci_read_reg(pcie, MACSR);
1033 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1035 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1036 err = rcar_pcie_enable_msi(host);
1039 "failed to enable MSI support: %d\n",
1041 goto err_phy_shutdown;
1045 err = rcar_pcie_enable(host);
1047 goto err_msi_teardown;
1052 if (IS_ENABLED(CONFIG_PCI_MSI))
1053 rcar_pcie_teardown_msi(host);
1057 phy_power_off(host->phy);
1058 phy_exit(host->phy);
1062 clk_disable_unprepare(host->bus_clk);
1065 irq_dispose_mapping(host->msi.irq2);
1066 irq_dispose_mapping(host->msi.irq1);
1069 pm_runtime_put(dev);
1070 pm_runtime_disable(dev);
1075 static int rcar_pcie_resume(struct device *dev)
1077 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1078 struct rcar_pcie *pcie = &host->pcie;
1082 err = rcar_pcie_parse_map_dma_ranges(host);
1086 /* Failure to get a link might just be that no cards are inserted */
1087 err = host->phy_init_fn(host);
1089 dev_info(dev, "PCIe link down\n");
1093 data = rcar_pci_read_reg(pcie, MACSR);
1094 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1097 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1098 struct resource res;
1101 of_address_to_resource(dev->of_node, 0, &res);
1102 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
1103 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
1105 bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR);
1106 rcar_pci_write_reg(pcie, val, PCIEMSIIER);
1109 rcar_pcie_hw_enable(host);
1114 static int rcar_pcie_resume_noirq(struct device *dev)
1116 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1117 struct rcar_pcie *pcie = &host->pcie;
1119 if (rcar_pci_read_reg(pcie, PMSR) &&
1120 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1123 /* Re-establish the PCIe link */
1124 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1125 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1126 return rcar_pcie_wait_for_dl(pcie);
1129 static const struct dev_pm_ops rcar_pcie_pm_ops = {
1130 SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1131 .resume_noirq = rcar_pcie_resume_noirq,
1134 static struct platform_driver rcar_pcie_driver = {
1136 .name = "rcar-pcie",
1137 .of_match_table = rcar_pcie_of_match,
1138 .pm = &rcar_pcie_pm_ops,
1139 .suppress_bind_attrs = true,
1141 .probe = rcar_pcie_probe,
1145 static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
1146 unsigned int fsr, struct pt_regs *regs)
1148 return !fixup_exception(regs);
1151 static const struct of_device_id rcar_pcie_abort_handler_of_match[] __initconst = {
1152 { .compatible = "renesas,pcie-r8a7779" },
1153 { .compatible = "renesas,pcie-r8a7790" },
1154 { .compatible = "renesas,pcie-r8a7791" },
1155 { .compatible = "renesas,pcie-rcar-gen2" },
1159 static int __init rcar_pcie_init(void)
1161 if (of_find_matching_node(NULL, rcar_pcie_abort_handler_of_match)) {
1162 #ifdef CONFIG_ARM_LPAE
1163 hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1164 "asynchronous external abort");
1166 hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1167 "imprecise external abort");
1171 return platform_driver_register(&rcar_pcie_driver);
1173 device_initcall(rcar_pcie_init);
1175 builtin_platform_driver(rcar_pcie_driver);