Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 2 | /* |
| 3 | * PCIe driver for Renesas R-Car SoCs |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 4 | * Copyright (C) 2014-2020 Renesas Electronics Europe Ltd |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 5 | * |
| 6 | * Based on: |
| 7 | * arch/sh/drivers/pci/pcie-sh7786.c |
| 8 | * arch/sh/drivers/pci/ops-sh7786.c |
| 9 | * Copyright (C) 2009 - 2011 Paul Mundt |
| 10 | * |
Paul Gortmaker | 42d1071 | 2016-07-22 16:23:21 -0500 | [diff] [blame] | 11 | * Author: Phil Edworthy <phil.edworthy@renesas.com> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
Marek Vasut | 0ee4082 | 2018-04-08 20:04:31 +0200 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 15 | #include <linux/clk.h> |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 16 | #include <linux/clk-provider.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | #include <linux/interrupt.h> |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 19 | #include <linux/irq.h> |
| 20 | #include <linux/irqdomain.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Paul Gortmaker | 42d1071 | 2016-07-22 16:23:21 -0500 | [diff] [blame] | 22 | #include <linux/init.h> |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 23 | #include <linux/iopoll.h> |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 24 | #include <linux/msi.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_irq.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/pci.h> |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 29 | #include <linux/phy/phy.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 32 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 33 | #include "pcie-rcar.h" |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 34 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 35 | struct rcar_msi { |
| 36 | DECLARE_BITMAP(used, INT_PCI_MSI_NR); |
| 37 | struct irq_domain *domain; |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 38 | struct mutex map_lock; |
| 39 | spinlock_t mask_lock; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 40 | int irq1; |
| 41 | int irq2; |
| 42 | }; |
| 43 | |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 44 | #ifdef CONFIG_ARM |
| 45 | /* |
| 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. |
| 50 | */ |
| 51 | static void __iomem *pcie_base; |
| 52 | /* |
| 53 | * Static copy of bus clock pointer, so we can check whether the clock |
| 54 | * is enabled or not. |
| 55 | */ |
| 56 | static struct clk *pcie_bus_clk; |
| 57 | #endif |
| 58 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 59 | /* Structure representing the PCIe interface */ |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 60 | struct rcar_pcie_host { |
| 61 | struct rcar_pcie pcie; |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 62 | struct phy *phy; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 63 | struct clk *bus_clk; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 64 | struct rcar_msi msi; |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 65 | int (*phy_init_fn)(struct rcar_pcie_host *host); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 68 | static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi) |
| 69 | { |
| 70 | return container_of(msi, struct rcar_pcie_host, msi); |
| 71 | } |
| 72 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 73 | static u32 rcar_read_conf(struct rcar_pcie *pcie, int where) |
| 74 | { |
Marek Vasut | 29ffa6d | 2019-03-25 12:40:59 +0100 | [diff] [blame] | 75 | unsigned int shift = BITS_PER_BYTE * (where & 3); |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 76 | u32 val = rcar_pci_read_reg(pcie, where & ~3); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 77 | |
| 78 | return val >> shift; |
| 79 | } |
| 80 | |
| 81 | /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 82 | static int rcar_pcie_config_access(struct rcar_pcie_host *host, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 83 | unsigned char access_type, struct pci_bus *bus, |
| 84 | unsigned int devfn, int where, u32 *data) |
| 85 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 86 | struct rcar_pcie *pcie = &host->pcie; |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 87 | unsigned int dev, func, reg, index; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 88 | |
| 89 | dev = PCI_SLOT(devfn); |
| 90 | func = PCI_FUNC(devfn); |
| 91 | reg = where & ~3; |
| 92 | index = reg / 4; |
| 93 | |
| 94 | /* |
| 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. |
| 101 | * |
| 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. |
| 108 | */ |
| 109 | if (pci_is_root_bus(bus)) { |
| 110 | if (dev != 0) |
| 111 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 112 | |
Rob Herring | 6176a5f | 2020-07-21 20:25:05 -0600 | [diff] [blame] | 113 | if (access_type == RCAR_PCI_ACCESS_READ) |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 114 | *data = rcar_pci_read_reg(pcie, PCICONF(index)); |
Rob Herring | 6176a5f | 2020-07-21 20:25:05 -0600 | [diff] [blame] | 115 | else |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 116 | rcar_pci_write_reg(pcie, *data, PCICONF(index)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 117 | |
| 118 | return PCIBIOS_SUCCESSFUL; |
| 119 | } |
| 120 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 121 | /* Clear errors */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 122 | rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 123 | |
| 124 | /* Set the PIO address */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 125 | rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) | |
| 126 | PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 127 | |
| 128 | /* Enable the configuration access */ |
Rob Herring | 6176a5f | 2020-07-21 20:25:05 -0600 | [diff] [blame] | 129 | if (pci_is_root_bus(bus->parent)) |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 130 | rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 131 | else |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 132 | rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 133 | |
| 134 | /* Check for errors */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 135 | if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 136 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 137 | |
| 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; |
| 142 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 143 | if (access_type == RCAR_PCI_ACCESS_READ) |
| 144 | *data = rcar_pci_read_reg(pcie, PCIECDR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 145 | else |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 146 | rcar_pci_write_reg(pcie, *data, PCIECDR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 147 | |
| 148 | /* Disable the configuration access */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 149 | rcar_pci_write_reg(pcie, 0, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 150 | |
| 151 | return PCIBIOS_SUCCESSFUL; |
| 152 | } |
| 153 | |
| 154 | static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn, |
| 155 | int where, int size, u32 *val) |
| 156 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 157 | struct rcar_pcie_host *host = bus->sysdata; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 158 | int ret; |
| 159 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 160 | ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 161 | bus, devfn, where, val); |
| 162 | if (ret != PCIBIOS_SUCCESSFUL) { |
| 163 | *val = 0xffffffff; |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | if (size == 1) |
Marek Vasut | 29ffa6d | 2019-03-25 12:40:59 +0100 | [diff] [blame] | 168 | *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 169 | else if (size == 2) |
Marek Vasut | 29ffa6d | 2019-03-25 12:40:59 +0100 | [diff] [blame] | 170 | *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 171 | |
Marek Vasut | 42a58f7 | 2019-03-25 12:41:00 +0100 | [diff] [blame] | 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); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 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) |
| 181 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 182 | struct rcar_pcie_host *host = bus->sysdata; |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 183 | unsigned int shift; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 184 | u32 data; |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 185 | int ret; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 186 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 187 | ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 188 | bus, devfn, where, &data); |
| 189 | if (ret != PCIBIOS_SUCCESSFUL) |
| 190 | return ret; |
| 191 | |
Marek Vasut | 42a58f7 | 2019-03-25 12:41:00 +0100 | [diff] [blame] | 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); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 194 | |
| 195 | if (size == 1) { |
Marek Vasut | 29ffa6d | 2019-03-25 12:40:59 +0100 | [diff] [blame] | 196 | shift = BITS_PER_BYTE * (where & 3); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 197 | data &= ~(0xff << shift); |
| 198 | data |= ((val & 0xff) << shift); |
| 199 | } else if (size == 2) { |
Marek Vasut | 29ffa6d | 2019-03-25 12:40:59 +0100 | [diff] [blame] | 200 | shift = BITS_PER_BYTE * (where & 2); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 201 | data &= ~(0xffff << shift); |
| 202 | data |= ((val & 0xffff) << shift); |
| 203 | } else |
| 204 | data = val; |
| 205 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 206 | ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 207 | bus, devfn, where, &data); |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static struct pci_ops rcar_pcie_ops = { |
| 213 | .read = rcar_pcie_read_conf, |
| 214 | .write = rcar_pcie_write_conf, |
| 215 | }; |
| 216 | |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 217 | static void rcar_pcie_force_speedup(struct rcar_pcie *pcie) |
| 218 | { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 219 | struct device *dev = pcie->dev; |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 220 | unsigned int timeout = 1000; |
| 221 | u32 macsr; |
| 222 | |
| 223 | if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS) |
| 224 | return; |
| 225 | |
| 226 | if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 227 | dev_err(dev, "Speed change already in progress\n"); |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | macsr = rcar_pci_read_reg(pcie, MACSR); |
| 232 | if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS) |
| 233 | goto done; |
| 234 | |
| 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); |
| 238 | |
| 239 | /* Set speed change reason as intentional factor */ |
| 240 | rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0); |
| 241 | |
| 242 | /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */ |
| 243 | if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL)) |
| 244 | rcar_pci_write_reg(pcie, macsr, MACSR); |
| 245 | |
| 246 | /* Start link speed change */ |
| 247 | rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE); |
| 248 | |
| 249 | while (timeout--) { |
| 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); |
| 254 | |
| 255 | if (macsr & SPCHGFAIL) |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 256 | dev_err(dev, "Speed change failed\n"); |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 257 | |
| 258 | goto done; |
| 259 | } |
| 260 | |
| 261 | msleep(1); |
Fengguang Wu | d170867 | 2018-03-07 09:42:39 -0600 | [diff] [blame] | 262 | } |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 263 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 264 | dev_err(dev, "Speed change timed out\n"); |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 265 | |
| 266 | done: |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 267 | dev_info(dev, "Current link speed is %s GT/s\n", |
Sergei Shtylyov | b3327f7 | 2016-09-22 23:20:18 +0300 | [diff] [blame] | 268 | (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5"); |
| 269 | } |
| 270 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 271 | static void rcar_pcie_hw_enable(struct rcar_pcie_host *host) |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 272 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 273 | struct rcar_pcie *pcie = &host->pcie; |
Rob Herring | b411b2e | 2020-07-21 20:25:10 -0600 | [diff] [blame] | 274 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 275 | struct resource_entry *win; |
| 276 | LIST_HEAD(res); |
| 277 | int i = 0; |
| 278 | |
| 279 | /* Try setting 5 GT/s link speed */ |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 280 | rcar_pcie_force_speedup(pcie); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 281 | |
| 282 | /* Setup PCI resources */ |
Rob Herring | b411b2e | 2020-07-21 20:25:10 -0600 | [diff] [blame] | 283 | resource_list_for_each_entry(win, &bridge->windows) { |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 284 | struct resource *res = win->res; |
| 285 | |
| 286 | if (!res->flags) |
| 287 | continue; |
| 288 | |
| 289 | switch (resource_type(res)) { |
| 290 | case IORESOURCE_IO: |
| 291 | case IORESOURCE_MEM: |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 292 | rcar_pcie_set_outbound(pcie, i, win); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 293 | i++; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 299 | static int rcar_pcie_enable(struct rcar_pcie_host *host) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 300 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 301 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 302 | |
Rob Herring | b411b2e | 2020-07-21 20:25:10 -0600 | [diff] [blame] | 303 | rcar_pcie_hw_enable(host); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 304 | |
Bjorn Helgaas | 7153884 | 2017-11-30 11:21:57 -0600 | [diff] [blame] | 305 | pci_add_flags(PCI_REASSIGN_ALL_BUS); |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 306 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 307 | bridge->sysdata = host; |
Lorenzo Pieralisi | 90634e8 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 308 | bridge->ops = &rcar_pcie_ops; |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 309 | |
Rob Herring | 56d2923 | 2020-05-22 17:48:29 -0600 | [diff] [blame] | 310 | return pci_host_probe(bridge); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int phy_wait_for_ack(struct rcar_pcie *pcie) |
| 314 | { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 315 | struct device *dev = pcie->dev; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 316 | unsigned int timeout = 100; |
| 317 | |
| 318 | while (timeout--) { |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 319 | if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | udelay(100); |
| 323 | } |
| 324 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 325 | dev_err(dev, "Access to PCIe phy timed out\n"); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 326 | |
| 327 | return -ETIMEDOUT; |
| 328 | } |
| 329 | |
| 330 | static void phy_write_reg(struct rcar_pcie *pcie, |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 331 | unsigned int rate, u32 addr, |
| 332 | unsigned int lane, u32 data) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 333 | { |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 334 | u32 phyaddr; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 335 | |
| 336 | phyaddr = WRITE_CMD | |
| 337 | ((rate & 1) << RATE_POS) | |
| 338 | ((lane & 0xf) << LANE_POS) | |
| 339 | ((addr & 0xff) << ADR_POS); |
| 340 | |
| 341 | /* Set write data */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 342 | rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR); |
| 343 | rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 344 | |
| 345 | /* Ignore errors as they will be dealt with if the data link is down */ |
| 346 | phy_wait_for_ack(pcie); |
| 347 | |
| 348 | /* Clear command */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 349 | rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR); |
| 350 | rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 351 | |
| 352 | /* Ignore errors as they will be dealt with if the data link is down */ |
| 353 | phy_wait_for_ack(pcie); |
| 354 | } |
| 355 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 356 | static int rcar_pcie_hw_init(struct rcar_pcie *pcie) |
| 357 | { |
| 358 | int err; |
| 359 | |
| 360 | /* Begin initialization */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 361 | rcar_pci_write_reg(pcie, 0, PCIETCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 362 | |
| 363 | /* Set mode */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 364 | rcar_pci_write_reg(pcie, 1, PCIEMSR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 365 | |
Sergei Shtylyov | 3ad1d32 | 2018-05-03 22:36:37 +0300 | [diff] [blame] | 366 | err = rcar_pcie_wait_for_phyrdy(pcie); |
| 367 | if (err) |
| 368 | return err; |
| 369 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 370 | /* |
| 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. |
| 374 | */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 375 | rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * Setup Secondary Bus Number & Subordinate Bus Number, even though |
| 379 | * they aren't used, to avoid bridge being detected as broken. |
| 380 | */ |
| 381 | rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1); |
| 382 | rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1); |
| 383 | |
| 384 | /* Initialize default capabilities. */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 385 | rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 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); |
| 390 | |
| 391 | /* Enable data link layer active state reporting */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 392 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC, |
| 393 | PCI_EXP_LNKCAP_DLLLARC); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 394 | |
| 395 | /* Write out the physical slot number = 0 */ |
| 396 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0); |
| 397 | |
| 398 | /* Set the completion timer timeout to the maximum 50ms. */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 399 | rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 400 | |
| 401 | /* Terminate list of capabilities (Next Capability Offset=0) */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 402 | rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 403 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 404 | /* Enable MSI */ |
| 405 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
Nobuhiro Iwamatsu | 1fc6aa9 | 2015-02-02 14:09:39 +0900 | [diff] [blame] | 406 | rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 407 | |
Yoshihiro Shimoda | 7c7e53e | 2019-11-05 19:51:29 +0900 | [diff] [blame] | 408 | rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR); |
| 409 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 410 | /* Finish initialization - establish a PCI Express link */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 411 | rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 412 | |
| 413 | /* This will timeout if we don't have a link. */ |
| 414 | err = rcar_pcie_wait_for_dl(pcie); |
| 415 | if (err) |
| 416 | return err; |
| 417 | |
| 418 | /* Enable INTx interrupts */ |
| 419 | rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8); |
| 420 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 421 | wmb(); |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 426 | static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 427 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 428 | struct rcar_pcie *pcie = &host->pcie; |
| 429 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 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); |
| 443 | |
| 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); |
| 447 | |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 448 | return 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 451 | static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host) |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame] | 452 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 453 | struct rcar_pcie *pcie = &host->pcie; |
| 454 | |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame] | 455 | /* |
| 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. |
| 458 | */ |
| 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); |
| 463 | |
| 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); |
| 469 | |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 470 | return 0; |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 473 | static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host) |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 474 | { |
| 475 | int err; |
| 476 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 477 | err = phy_init(host->phy); |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 478 | if (err) |
| 479 | return err; |
| 480 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 481 | err = phy_power_on(host->phy); |
Marek Vasut | 3c5777c | 2018-06-29 13:48:15 -0500 | [diff] [blame] | 482 | if (err) |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 483 | phy_exit(host->phy); |
Marek Vasut | 3c5777c | 2018-06-29 13:48:15 -0500 | [diff] [blame] | 484 | |
| 485 | return err; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 488 | static irqreturn_t rcar_pcie_msi_irq(int irq, void *data) |
| 489 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 490 | struct rcar_pcie_host *host = data; |
| 491 | struct rcar_pcie *pcie = &host->pcie; |
| 492 | struct rcar_msi *msi = &host->msi; |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 493 | struct device *dev = pcie->dev; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 494 | unsigned long reg; |
| 495 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 496 | reg = rcar_pci_read_reg(pcie, PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 497 | |
| 498 | /* MSI & INTx share an interrupt - we only handle MSI here */ |
| 499 | if (!reg) |
| 500 | return IRQ_NONE; |
| 501 | |
| 502 | while (reg) { |
| 503 | unsigned int index = find_first_bit(®, 32); |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 504 | int ret; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 505 | |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 506 | ret = generic_handle_domain_irq(msi->domain->parent, index); |
| 507 | if (ret) { |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 508 | /* Unknown MSI, just clear it */ |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 509 | dev_dbg(dev, "unexpected MSI\n"); |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 510 | rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* see if there's any more pending in this vector */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 514 | reg = rcar_pci_read_reg(pcie, PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return IRQ_HANDLED; |
| 518 | } |
| 519 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 520 | static void rcar_msi_top_irq_ack(struct irq_data *d) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 521 | { |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 522 | irq_chip_ack_parent(d); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 525 | static void rcar_msi_top_irq_mask(struct irq_data *d) |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 526 | { |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 527 | pci_msi_mask_irq(d); |
| 528 | irq_chip_mask_parent(d); |
| 529 | } |
| 530 | |
| 531 | static void rcar_msi_top_irq_unmask(struct irq_data *d) |
| 532 | { |
| 533 | pci_msi_unmask_irq(d); |
| 534 | irq_chip_unmask_parent(d); |
| 535 | } |
| 536 | |
| 537 | static struct irq_chip rcar_msi_top_chip = { |
| 538 | .name = "PCIe MSI", |
| 539 | .irq_ack = rcar_msi_top_irq_ack, |
| 540 | .irq_mask = rcar_msi_top_irq_mask, |
| 541 | .irq_unmask = rcar_msi_top_irq_unmask, |
| 542 | }; |
| 543 | |
| 544 | static void rcar_msi_irq_ack(struct irq_data *d) |
| 545 | { |
| 546 | struct rcar_msi *msi = irq_data_get_irq_chip_data(d); |
| 547 | struct rcar_pcie *pcie = &msi_to_host(msi)->pcie; |
| 548 | |
| 549 | /* clear the interrupt */ |
| 550 | rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR); |
| 551 | } |
| 552 | |
| 553 | static void rcar_msi_irq_mask(struct irq_data *d) |
| 554 | { |
| 555 | struct rcar_msi *msi = irq_data_get_irq_chip_data(d); |
| 556 | struct rcar_pcie *pcie = &msi_to_host(msi)->pcie; |
| 557 | unsigned long flags; |
| 558 | u32 value; |
| 559 | |
| 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); |
| 565 | } |
| 566 | |
| 567 | static void rcar_msi_irq_unmask(struct irq_data *d) |
| 568 | { |
| 569 | struct rcar_msi *msi = irq_data_get_irq_chip_data(d); |
| 570 | struct rcar_pcie *pcie = &msi_to_host(msi)->pcie; |
| 571 | unsigned long flags; |
| 572 | u32 value; |
| 573 | |
| 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); |
| 579 | } |
| 580 | |
| 581 | static int rcar_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force) |
| 582 | { |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 587 | { |
| 588 | struct rcar_msi *msi = irq_data_get_irq_chip_data(data); |
| 589 | struct rcar_pcie *pcie = &msi_to_host(msi)->pcie; |
| 590 | |
| 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; |
| 594 | } |
| 595 | |
| 596 | static struct irq_chip rcar_msi_bottom_chip = { |
| 597 | .name = "Rcar MSI", |
| 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, |
| 603 | }; |
| 604 | |
| 605 | static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 606 | unsigned int nr_irqs, void *args) |
| 607 | { |
| 608 | struct rcar_msi *msi = domain->host_data; |
| 609 | unsigned int i; |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 610 | int hwirq; |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 611 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 612 | mutex_lock(&msi->map_lock); |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 613 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 614 | hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs)); |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 615 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 616 | mutex_unlock(&msi->map_lock); |
| 617 | |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 618 | if (hwirq < 0) |
| 619 | return -ENOSPC; |
| 620 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 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); |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 629 | static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq, |
| 630 | unsigned int nr_irqs) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 631 | { |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 632 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 633 | struct rcar_msi *msi = domain->host_data; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 634 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 635 | mutex_lock(&msi->map_lock); |
| 636 | |
| 637 | bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs)); |
| 638 | |
| 639 | mutex_unlock(&msi->map_lock); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 642 | static const struct irq_domain_ops rcar_msi_domain_ops = { |
| 643 | .alloc = rcar_msi_domain_alloc, |
| 644 | .free = rcar_msi_domain_free, |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 645 | }; |
| 646 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 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, |
| 651 | }; |
| 652 | |
| 653 | static int rcar_allocate_domains(struct rcar_msi *msi) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 654 | { |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 655 | struct rcar_pcie *pcie = &msi_to_host(msi)->pcie; |
| 656 | struct fwnode_handle *fwnode = dev_fwnode(pcie->dev); |
| 657 | struct irq_domain *parent; |
| 658 | |
| 659 | parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR, |
| 660 | &rcar_msi_domain_ops, msi); |
| 661 | if (!parent) { |
| 662 | dev_err(pcie->dev, "failed to create IRQ domain\n"); |
| 663 | return -ENOMEM; |
| 664 | } |
| 665 | irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS); |
| 666 | |
| 667 | msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent); |
| 668 | if (!msi->domain) { |
| 669 | dev_err(pcie->dev, "failed to create MSI domain\n"); |
| 670 | irq_domain_remove(parent); |
| 671 | return -ENOMEM; |
| 672 | } |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 677 | static void rcar_free_domains(struct rcar_msi *msi) |
Marek Vasut | 0bbf6b9 | 2018-05-24 16:36:23 +0200 | [diff] [blame] | 678 | { |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 679 | struct irq_domain *parent = msi->domain->parent; |
Marek Vasut | 0bbf6b9 | 2018-05-24 16:36:23 +0200 | [diff] [blame] | 680 | |
| 681 | irq_domain_remove(msi->domain); |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 682 | irq_domain_remove(parent); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 685 | static int rcar_pcie_enable_msi(struct rcar_pcie_host *host) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 686 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 687 | struct rcar_pcie *pcie = &host->pcie; |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 688 | struct device *dev = pcie->dev; |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 689 | struct rcar_msi *msi = &host->msi; |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 690 | struct resource res; |
| 691 | int err; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 692 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 693 | mutex_init(&msi->map_lock); |
| 694 | spin_lock_init(&msi->mask_lock); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 695 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 696 | err = of_address_to_resource(dev->of_node, 0, &res); |
| 697 | if (err) |
| 698 | return err; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 699 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 700 | err = rcar_allocate_domains(msi); |
| 701 | if (err) |
| 702 | return err; |
Grigory Kletsko | e3123c2 | 2016-09-08 22:32:59 +0300 | [diff] [blame] | 703 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 704 | /* Two irqs are for MSI, but they are also used for non-MSI irqs */ |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 705 | err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq, |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 706 | IRQF_SHARED | IRQF_NO_THREAD, |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 707 | rcar_msi_bottom_chip.name, host); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 708 | if (err < 0) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 709 | dev_err(dev, "failed to request IRQ: %d\n", err); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 710 | goto err; |
| 711 | } |
| 712 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 713 | err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq, |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 714 | IRQF_SHARED | IRQF_NO_THREAD, |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 715 | rcar_msi_bottom_chip.name, host); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 716 | if (err < 0) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 717 | dev_err(dev, "failed to request IRQ: %d\n", err); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 718 | goto err; |
| 719 | } |
| 720 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 721 | /* disable all MSIs */ |
| 722 | rcar_pci_write_reg(pcie, 0, PCIEMSIIER); |
| 723 | |
| 724 | /* |
| 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. |
| 727 | */ |
| 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); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 730 | |
| 731 | return 0; |
| 732 | |
| 733 | err: |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 734 | rcar_free_domains(msi); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 735 | return err; |
| 736 | } |
| 737 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 738 | static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host) |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 739 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 740 | struct rcar_pcie *pcie = &host->pcie; |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 741 | |
| 742 | /* Disable all MSI interrupts */ |
| 743 | rcar_pci_write_reg(pcie, 0, PCIEMSIIER); |
| 744 | |
| 745 | /* Disable address decoding of the MSI interrupt, MSIFE */ |
| 746 | rcar_pci_write_reg(pcie, 0, PCIEMSIALR); |
| 747 | |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 748 | rcar_free_domains(&host->msi); |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 751 | static int rcar_pcie_get_resources(struct rcar_pcie_host *host) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 752 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 753 | struct rcar_pcie *pcie = &host->pcie; |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 754 | struct device *dev = pcie->dev; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 755 | struct resource res; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 756 | int err, i; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 757 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 758 | host->phy = devm_phy_optional_get(dev, "pcie"); |
| 759 | if (IS_ERR(host->phy)) |
| 760 | return PTR_ERR(host->phy); |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 761 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 762 | err = of_address_to_resource(dev->of_node, 0, &res); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 763 | if (err) |
| 764 | return err; |
| 765 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 766 | pcie->base = devm_ioremap_resource(dev, &res); |
Bjorn Helgaas | 51afa3c | 2016-08-22 14:16:38 -0500 | [diff] [blame] | 767 | if (IS_ERR(pcie->base)) |
| 768 | return PTR_ERR(pcie->base); |
| 769 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 770 | host->bus_clk = devm_clk_get(dev, "pcie_bus"); |
| 771 | if (IS_ERR(host->bus_clk)) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 772 | dev_err(dev, "cannot get pcie bus clock\n"); |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 773 | return PTR_ERR(host->bus_clk); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 774 | } |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 775 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 776 | i = irq_of_parse_and_map(dev->of_node, 0); |
Dmitry Torokhov | c51d411 | 2014-11-14 14:21:53 -0800 | [diff] [blame] | 777 | if (!i) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 778 | dev_err(dev, "cannot get platform resources for msi interrupt\n"); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 779 | err = -ENOENT; |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 780 | goto err_irq1; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 781 | } |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 782 | host->msi.irq1 = i; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 783 | |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 784 | i = irq_of_parse_and_map(dev->of_node, 1); |
Dmitry Torokhov | c51d411 | 2014-11-14 14:21:53 -0800 | [diff] [blame] | 785 | if (!i) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 786 | dev_err(dev, "cannot get platform resources for msi interrupt\n"); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 787 | err = -ENOENT; |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 788 | goto err_irq2; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 789 | } |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 790 | host->msi.irq2 = i; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 791 | |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 792 | #ifdef CONFIG_ARM |
| 793 | /* Cache static copy for L1 link state fixup hook on aarch32 */ |
| 794 | pcie_base = pcie->base; |
| 795 | pcie_bus_clk = host->bus_clk; |
| 796 | #endif |
| 797 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 798 | return 0; |
| 799 | |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 800 | err_irq2: |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 801 | irq_dispose_mapping(host->msi.irq1); |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 802 | err_irq1: |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 803 | return err; |
| 804 | } |
| 805 | |
| 806 | static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 807 | struct resource_entry *entry, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 808 | int *index) |
| 809 | { |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 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; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 814 | u32 flags = LAM_64BIT | LAR_ENABLE; |
| 815 | u64 mask; |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 816 | u64 size = resource_size(entry->res); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 817 | int idx = *index; |
| 818 | |
| 819 | if (restype & IORESOURCE_PREFETCH) |
| 820 | flags |= LAM_PREFETCH; |
| 821 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 822 | while (cpu_addr < cpu_end) { |
Marek Vasut | 85bff4c | 2019-10-26 20:26:58 +0200 | [diff] [blame] | 823 | if (idx >= MAX_NR_INBOUND_MAPS - 1) { |
| 824 | dev_err(pcie->dev, "Failed to map inbound regions!\n"); |
| 825 | return -EINVAL; |
| 826 | } |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 827 | /* |
Marek Vasut | 767c784 | 2019-10-26 20:26:59 +0200 | [diff] [blame] | 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. |
| 831 | */ |
| 832 | if (cpu_addr > 0) { |
| 833 | unsigned long nr_zeros = __ffs64(cpu_addr); |
| 834 | u64 alignment = 1ULL << nr_zeros; |
| 835 | |
Bjorn Helgaas | 7bd4c4a | 2019-11-28 08:54:53 -0600 | [diff] [blame] | 836 | size = min(size, alignment); |
Marek Vasut | 767c784 | 2019-10-26 20:26:59 +0200 | [diff] [blame] | 837 | } |
| 838 | /* Hardware supports max 4GiB inbound region */ |
| 839 | size = min(size, 1ULL << 32); |
| 840 | |
| 841 | mask = roundup_pow_of_two(size) - 1; |
| 842 | mask &= ~0xf; |
| 843 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 844 | rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr, |
| 845 | lower_32_bits(mask) | flags, idx, true); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 846 | |
| 847 | pci_addr += size; |
| 848 | cpu_addr += size; |
| 849 | idx += 2; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 850 | } |
| 851 | *index = idx; |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 856 | static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 857 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 858 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 859 | struct resource_entry *entry; |
| 860 | int index = 0, err = 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 861 | |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 862 | resource_list_for_each_entry(entry, &bridge->dma_ranges) { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 863 | err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 864 | if (err) |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 865 | break; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Rob Herring | 085f793 | 2019-10-28 11:32:55 -0500 | [diff] [blame] | 868 | return err; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static const struct of_device_id rcar_pcie_of_match[] = { |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 872 | { .compatible = "renesas,pcie-r8a7779", |
| 873 | .data = rcar_pcie_phy_init_h1 }, |
Sergei Shtylyov | f7bc638 | 2016-09-09 01:26:18 +0300 | [diff] [blame] | 874 | { .compatible = "renesas,pcie-r8a7790", |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 875 | .data = rcar_pcie_phy_init_gen2 }, |
Sergei Shtylyov | f7bc638 | 2016-09-09 01:26:18 +0300 | [diff] [blame] | 876 | { .compatible = "renesas,pcie-r8a7791", |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 877 | .data = rcar_pcie_phy_init_gen2 }, |
Simon Horman | d83a328 | 2016-12-06 16:51:30 +0100 | [diff] [blame] | 878 | { .compatible = "renesas,pcie-rcar-gen2", |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 879 | .data = rcar_pcie_phy_init_gen2 }, |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 880 | { .compatible = "renesas,pcie-r8a7795", |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 881 | .data = rcar_pcie_phy_init_gen3 }, |
Sergei Shtylyov | 517ca93 | 2018-05-03 22:40:54 +0300 | [diff] [blame] | 882 | { .compatible = "renesas,pcie-rcar-gen3", |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 883 | .data = rcar_pcie_phy_init_gen3 }, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 884 | {}, |
| 885 | }; |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 886 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 887 | static int rcar_pcie_probe(struct platform_device *pdev) |
| 888 | { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 889 | struct device *dev = &pdev->dev; |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 890 | struct rcar_pcie_host *host; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 891 | struct rcar_pcie *pcie; |
Marek Vasut | d8fa266 | 2019-03-25 12:40:58 +0100 | [diff] [blame] | 892 | u32 data; |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 893 | int err; |
Lorenzo Pieralisi | 90634e8 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 894 | struct pci_host_bridge *bridge; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 895 | |
Rob Herring | 61f11f8 | 2020-07-21 20:25:09 -0600 | [diff] [blame] | 896 | bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host)); |
Lorenzo Pieralisi | 90634e8 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 897 | if (!bridge) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 898 | return -ENOMEM; |
| 899 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 900 | host = pci_host_bridge_priv(bridge); |
| 901 | pcie = &host->pcie; |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 902 | pcie->dev = dev; |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 903 | platform_set_drvdata(pdev, host); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 904 | |
Dien Pham | 0df6150 | 2018-04-08 15:09:25 +0200 | [diff] [blame] | 905 | pm_runtime_enable(pcie->dev); |
| 906 | err = pm_runtime_get_sync(pcie->dev); |
| 907 | if (err < 0) { |
| 908 | dev_err(pcie->dev, "pm_runtime_get_sync failed\n"); |
Dinghao Liu | a68e06e | 2020-07-09 14:43:56 +0800 | [diff] [blame] | 909 | goto err_pm_put; |
Dien Pham | 0df6150 | 2018-04-08 15:09:25 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 912 | err = rcar_pcie_get_resources(host); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 913 | if (err < 0) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 914 | dev_err(dev, "failed to request resources: %d\n", err); |
Dien Pham | 0df6150 | 2018-04-08 15:09:25 +0200 | [diff] [blame] | 915 | goto err_pm_put; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 918 | err = clk_prepare_enable(host->bus_clk); |
Marek Vasut | 80b8471 | 2018-05-24 16:36:19 +0200 | [diff] [blame] | 919 | if (err) { |
| 920 | dev_err(dev, "failed to enable bus clock: %d\n", err); |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 921 | goto err_unmap_msi_irqs; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 924 | err = rcar_pcie_parse_map_dma_ranges(host); |
Sergei Shtylyov | f7bc638 | 2016-09-09 01:26:18 +0300 | [diff] [blame] | 925 | if (err) |
Marek Vasut | 80b8471 | 2018-05-24 16:36:19 +0200 | [diff] [blame] | 926 | goto err_clk_disable; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 927 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 928 | host->phy_init_fn = of_device_get_match_data(dev); |
| 929 | err = host->phy_init_fn(host); |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 930 | if (err) { |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 931 | dev_err(dev, "failed to init PCIe PHY\n"); |
Marek Vasut | 80b8471 | 2018-05-24 16:36:19 +0200 | [diff] [blame] | 932 | goto err_clk_disable; |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /* Failure to get a link might just be that no cards are inserted */ |
Sergei Shtylyov | 9d5014e | 2018-05-03 22:43:13 +0300 | [diff] [blame] | 936 | if (rcar_pcie_hw_init(pcie)) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 937 | dev_info(dev, "PCIe link down\n"); |
Harunobu Kurokawa | e94888d | 2016-12-16 12:50:04 +0100 | [diff] [blame] | 938 | err = -ENODEV; |
Marek Vasut | 4050360 | 2018-06-29 13:47:38 -0500 | [diff] [blame] | 939 | goto err_phy_shutdown; |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | data = rcar_pci_read_reg(pcie, MACSR); |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 943 | dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f); |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 944 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 945 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 946 | err = rcar_pcie_enable_msi(host); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 947 | if (err < 0) { |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 948 | dev_err(dev, |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 949 | "failed to enable MSI support: %d\n", |
| 950 | err); |
Marek Vasut | 4050360 | 2018-06-29 13:47:38 -0500 | [diff] [blame] | 951 | goto err_phy_shutdown; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 955 | err = rcar_pcie_enable(host); |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 956 | if (err) |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 957 | goto err_msi_teardown; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 958 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 959 | return 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 960 | |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 961 | err_msi_teardown: |
| 962 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 963 | rcar_pcie_teardown_msi(host); |
Marek Vasut | 1aea58b | 2018-05-24 16:36:21 +0200 | [diff] [blame] | 964 | |
Marek Vasut | 4050360 | 2018-06-29 13:47:38 -0500 | [diff] [blame] | 965 | err_phy_shutdown: |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 966 | if (host->phy) { |
| 967 | phy_power_off(host->phy); |
| 968 | phy_exit(host->phy); |
Marek Vasut | 4050360 | 2018-06-29 13:47:38 -0500 | [diff] [blame] | 969 | } |
| 970 | |
Marek Vasut | 80b8471 | 2018-05-24 16:36:19 +0200 | [diff] [blame] | 971 | err_clk_disable: |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 972 | clk_disable_unprepare(host->bus_clk); |
Marek Vasut | 80b8471 | 2018-05-24 16:36:19 +0200 | [diff] [blame] | 973 | |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 974 | err_unmap_msi_irqs: |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 975 | irq_dispose_mapping(host->msi.irq2); |
| 976 | irq_dispose_mapping(host->msi.irq1); |
Marek Vasut | 53f1aeb | 2018-05-24 16:36:20 +0200 | [diff] [blame] | 977 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 978 | err_pm_put: |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 979 | pm_runtime_put(dev); |
Bjorn Helgaas | 4ef80d7 | 2016-10-10 14:31:28 -0500 | [diff] [blame] | 980 | pm_runtime_disable(dev); |
Lorenzo Pieralisi | 3c60e01 | 2017-08-04 12:32:54 +0900 | [diff] [blame] | 981 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 982 | return err; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 985 | static int __maybe_unused rcar_pcie_resume(struct device *dev) |
| 986 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 987 | struct rcar_pcie_host *host = dev_get_drvdata(dev); |
| 988 | struct rcar_pcie *pcie = &host->pcie; |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 989 | unsigned int data; |
| 990 | int err; |
| 991 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 992 | err = rcar_pcie_parse_map_dma_ranges(host); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 993 | if (err) |
| 994 | return 0; |
| 995 | |
| 996 | /* Failure to get a link might just be that no cards are inserted */ |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 997 | err = host->phy_init_fn(host); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 998 | if (err) { |
| 999 | dev_info(dev, "PCIe link down\n"); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | data = rcar_pci_read_reg(pcie, MACSR); |
| 1004 | dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f); |
| 1005 | |
| 1006 | /* Enable MSI */ |
Marc Zyngier | 83ed8d4 | 2021-03-30 16:11:34 +0100 | [diff] [blame] | 1007 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 1008 | struct resource res; |
| 1009 | u32 val; |
| 1010 | |
| 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); |
| 1014 | |
| 1015 | bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR); |
| 1016 | rcar_pci_write_reg(pcie, val, PCIEMSIIER); |
| 1017 | } |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 1018 | |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 1019 | rcar_pcie_hw_enable(host); |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Kazufumi Ikeda | be20bbc | 2019-03-25 20:43:19 +0100 | [diff] [blame] | 1024 | static int rcar_pcie_resume_noirq(struct device *dev) |
| 1025 | { |
Lad Prabhakar | 78a0d7f | 2020-05-07 13:33:13 +0100 | [diff] [blame] | 1026 | struct rcar_pcie_host *host = dev_get_drvdata(dev); |
| 1027 | struct rcar_pcie *pcie = &host->pcie; |
Kazufumi Ikeda | be20bbc | 2019-03-25 20:43:19 +0100 | [diff] [blame] | 1028 | |
| 1029 | if (rcar_pci_read_reg(pcie, PMSR) && |
| 1030 | !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN)) |
| 1031 | return 0; |
| 1032 | |
| 1033 | /* Re-establish the PCIe link */ |
Yoshihiro Shimoda | 7c7e53e | 2019-11-05 19:51:29 +0900 | [diff] [blame] | 1034 | rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR); |
Kazufumi Ikeda | be20bbc | 2019-03-25 20:43:19 +0100 | [diff] [blame] | 1035 | rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR); |
| 1036 | return rcar_pcie_wait_for_dl(pcie); |
| 1037 | } |
| 1038 | |
| 1039 | static const struct dev_pm_ops rcar_pcie_pm_ops = { |
Kazufumi Ikeda | ce35163 | 2020-03-14 20:12:32 +0100 | [diff] [blame] | 1040 | SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume) |
Kazufumi Ikeda | be20bbc | 2019-03-25 20:43:19 +0100 | [diff] [blame] | 1041 | .resume_noirq = rcar_pcie_resume_noirq, |
| 1042 | }; |
| 1043 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1044 | static struct platform_driver rcar_pcie_driver = { |
| 1045 | .driver = { |
Bjorn Helgaas | 3ff8e4a | 2016-10-06 13:40:28 -0500 | [diff] [blame] | 1046 | .name = "rcar-pcie", |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1047 | .of_match_table = rcar_pcie_of_match, |
Kazufumi Ikeda | be20bbc | 2019-03-25 20:43:19 +0100 | [diff] [blame] | 1048 | .pm = &rcar_pcie_pm_ops, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1049 | .suppress_bind_attrs = true, |
| 1050 | }, |
| 1051 | .probe = rcar_pcie_probe, |
| 1052 | }; |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 1053 | |
| 1054 | #ifdef CONFIG_ARM |
| 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) |
| 1058 | { |
| 1059 | unsigned long flags; |
| 1060 | u32 pmsr, val; |
| 1061 | int ret = 0; |
| 1062 | |
| 1063 | spin_lock_irqsave(&pmsr_lock, flags); |
| 1064 | |
| 1065 | if (!pcie_base || !__clk_is_enabled(pcie_bus_clk)) { |
| 1066 | ret = 1; |
| 1067 | goto unlock_exit; |
| 1068 | } |
| 1069 | |
| 1070 | pmsr = readl(pcie_base + PMSR); |
| 1071 | |
| 1072 | /* |
| 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. |
| 1077 | */ |
| 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); |
| 1084 | } |
| 1085 | |
| 1086 | unlock_exit: |
| 1087 | spin_unlock_irqrestore(&pmsr_lock, flags); |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 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" }, |
| 1096 | {}, |
| 1097 | }; |
| 1098 | |
| 1099 | static int __init rcar_pcie_init(void) |
| 1100 | { |
| 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"); |
| 1105 | #else |
| 1106 | hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0, |
| 1107 | "imprecise external abort"); |
| 1108 | #endif |
| 1109 | } |
| 1110 | |
| 1111 | return platform_driver_register(&rcar_pcie_driver); |
| 1112 | } |
| 1113 | device_initcall(rcar_pcie_init); |
| 1114 | #else |
Paul Gortmaker | 42d1071 | 2016-07-22 16:23:21 -0500 | [diff] [blame] | 1115 | builtin_platform_driver(rcar_pcie_driver); |
Marek Vasut | a115b1b | 2021-08-15 20:16:50 +0200 | [diff] [blame] | 1116 | #endif |