Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PCIe driver for Renesas R-Car SoCs |
| 3 | * Copyright (C) 2014 Renesas Electronics Europe Ltd |
| 4 | * |
| 5 | * Based on: |
| 6 | * arch/sh/drivers/pci/pcie-sh7786.c |
| 7 | * arch/sh/drivers/pci/ops-sh7786.c |
| 8 | * Copyright (C) 2009 - 2011 Paul Mundt |
| 9 | * |
| 10 | * This file is licensed under the terms of the GNU General Public |
| 11 | * License version 2. This program is licensed "as is" without any |
| 12 | * warranty of any kind, whether express or implied. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/interrupt.h> |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 18 | #include <linux/irq.h> |
| 19 | #include <linux/irqdomain.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 22 | #include <linux/msi.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 23 | #include <linux/of_address.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/of_pci.h> |
| 26 | #include <linux/of_platform.h> |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/platform_device.h> |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | |
| 32 | #define DRV_NAME "rcar-pcie" |
| 33 | |
| 34 | #define PCIECAR 0x000010 |
| 35 | #define PCIECCTLR 0x000018 |
| 36 | #define CONFIG_SEND_ENABLE (1 << 31) |
| 37 | #define TYPE0 (0 << 8) |
| 38 | #define TYPE1 (1 << 8) |
| 39 | #define PCIECDR 0x000020 |
| 40 | #define PCIEMSR 0x000028 |
| 41 | #define PCIEINTXR 0x000400 |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 42 | #define PCIEMSITXR 0x000840 |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 43 | |
| 44 | /* Transfer control */ |
| 45 | #define PCIETCTLR 0x02000 |
| 46 | #define CFINIT 1 |
| 47 | #define PCIETSTR 0x02004 |
| 48 | #define DATA_LINK_ACTIVE 1 |
| 49 | #define PCIEERRFR 0x02020 |
| 50 | #define UNSUPPORTED_REQUEST (1 << 4) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 51 | #define PCIEMSIFR 0x02044 |
| 52 | #define PCIEMSIALR 0x02048 |
| 53 | #define MSIFE 1 |
| 54 | #define PCIEMSIAUR 0x0204c |
| 55 | #define PCIEMSIIER 0x02050 |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 56 | |
| 57 | /* root port address */ |
| 58 | #define PCIEPRAR(x) (0x02080 + ((x) * 0x4)) |
| 59 | |
| 60 | /* local address reg & mask */ |
| 61 | #define PCIELAR(x) (0x02200 + ((x) * 0x20)) |
| 62 | #define PCIELAMR(x) (0x02208 + ((x) * 0x20)) |
| 63 | #define LAM_PREFETCH (1 << 3) |
| 64 | #define LAM_64BIT (1 << 2) |
| 65 | #define LAR_ENABLE (1 << 1) |
| 66 | |
| 67 | /* PCIe address reg & mask */ |
Nobuhiro Iwamatsu | ecd0630 | 2015-02-04 18:02:55 +0900 | [diff] [blame] | 68 | #define PCIEPALR(x) (0x03400 + ((x) * 0x20)) |
| 69 | #define PCIEPAUR(x) (0x03404 + ((x) * 0x20)) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 70 | #define PCIEPAMR(x) (0x03408 + ((x) * 0x20)) |
| 71 | #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20)) |
| 72 | #define PAR_ENABLE (1 << 31) |
| 73 | #define IO_SPACE (1 << 8) |
| 74 | |
| 75 | /* Configuration */ |
| 76 | #define PCICONF(x) (0x010000 + ((x) * 0x4)) |
| 77 | #define PMCAP(x) (0x010040 + ((x) * 0x4)) |
| 78 | #define EXPCAP(x) (0x010070 + ((x) * 0x4)) |
| 79 | #define VCCAP(x) (0x010100 + ((x) * 0x4)) |
| 80 | |
| 81 | /* link layer */ |
| 82 | #define IDSETR1 0x011004 |
| 83 | #define TLCTLR 0x011048 |
| 84 | #define MACSR 0x011054 |
| 85 | #define MACCTLR 0x011058 |
| 86 | #define SCRAMBLE_DISABLE (1 << 27) |
| 87 | |
| 88 | /* R-Car H1 PHY */ |
| 89 | #define H1_PCIEPHYADRR 0x04000c |
| 90 | #define WRITE_CMD (1 << 16) |
| 91 | #define PHY_ACK (1 << 24) |
| 92 | #define RATE_POS 12 |
| 93 | #define LANE_POS 8 |
| 94 | #define ADR_POS 0 |
| 95 | #define H1_PCIEPHYDOUTR 0x040014 |
| 96 | #define H1_PCIEPHYSR 0x040018 |
| 97 | |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame^] | 98 | /* R-Car Gen2 PHY */ |
| 99 | #define GEN2_PCIEPHYADDR 0x780 |
| 100 | #define GEN2_PCIEPHYDATA 0x784 |
| 101 | #define GEN2_PCIEPHYCTRL 0x78c |
| 102 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 103 | #define INT_PCI_MSI_NR 32 |
| 104 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 105 | #define RCONF(x) (PCICONF(0)+(x)) |
| 106 | #define RPMCAP(x) (PMCAP(0)+(x)) |
| 107 | #define REXPCAP(x) (EXPCAP(0)+(x)) |
| 108 | #define RVCCAP(x) (VCCAP(0)+(x)) |
| 109 | |
| 110 | #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24) |
| 111 | #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19) |
| 112 | #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16) |
| 113 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 114 | #define RCAR_PCI_MAX_RESOURCES 4 |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 115 | #define MAX_NR_INBOUND_MAPS 6 |
| 116 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 117 | struct rcar_msi { |
| 118 | DECLARE_BITMAP(used, INT_PCI_MSI_NR); |
| 119 | struct irq_domain *domain; |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 120 | struct msi_controller chip; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 121 | unsigned long pages; |
| 122 | struct mutex lock; |
| 123 | int irq1; |
| 124 | int irq2; |
| 125 | }; |
| 126 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 127 | static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 128 | { |
| 129 | return container_of(chip, struct rcar_msi, chip); |
| 130 | } |
| 131 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 132 | /* Structure representing the PCIe interface */ |
| 133 | struct rcar_pcie { |
| 134 | struct device *dev; |
| 135 | void __iomem *base; |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 136 | struct list_head resources; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 137 | int root_bus_nr; |
| 138 | struct clk *clk; |
| 139 | struct clk *bus_clk; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 140 | struct rcar_msi msi; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 143 | static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val, |
| 144 | unsigned long reg) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 145 | { |
| 146 | writel(val, pcie->base + reg); |
| 147 | } |
| 148 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 149 | static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie, |
| 150 | unsigned long reg) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 151 | { |
| 152 | return readl(pcie->base + reg); |
| 153 | } |
| 154 | |
| 155 | enum { |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 156 | RCAR_PCI_ACCESS_READ, |
| 157 | RCAR_PCI_ACCESS_WRITE, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data) |
| 161 | { |
| 162 | int shift = 8 * (where & 3); |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 163 | u32 val = rcar_pci_read_reg(pcie, where & ~3); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 164 | |
| 165 | val &= ~(mask << shift); |
| 166 | val |= data << shift; |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 167 | rcar_pci_write_reg(pcie, val, where & ~3); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static u32 rcar_read_conf(struct rcar_pcie *pcie, int where) |
| 171 | { |
| 172 | int shift = 8 * (where & 3); |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 173 | u32 val = rcar_pci_read_reg(pcie, where & ~3); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 174 | |
| 175 | return val >> shift; |
| 176 | } |
| 177 | |
| 178 | /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ |
| 179 | static int rcar_pcie_config_access(struct rcar_pcie *pcie, |
| 180 | unsigned char access_type, struct pci_bus *bus, |
| 181 | unsigned int devfn, int where, u32 *data) |
| 182 | { |
| 183 | int dev, func, reg, index; |
| 184 | |
| 185 | dev = PCI_SLOT(devfn); |
| 186 | func = PCI_FUNC(devfn); |
| 187 | reg = where & ~3; |
| 188 | index = reg / 4; |
| 189 | |
| 190 | /* |
| 191 | * While each channel has its own memory-mapped extended config |
| 192 | * space, it's generally only accessible when in endpoint mode. |
| 193 | * When in root complex mode, the controller is unable to target |
| 194 | * itself with either type 0 or type 1 accesses, and indeed, any |
| 195 | * controller initiated target transfer to its own config space |
| 196 | * result in a completer abort. |
| 197 | * |
| 198 | * Each channel effectively only supports a single device, but as |
| 199 | * the same channel <-> device access works for any PCI_SLOT() |
| 200 | * value, we cheat a bit here and bind the controller's config |
| 201 | * space to devfn 0 in order to enable self-enumeration. In this |
| 202 | * case the regular ECAR/ECDR path is sidelined and the mangled |
| 203 | * config access itself is initiated as an internal bus transaction. |
| 204 | */ |
| 205 | if (pci_is_root_bus(bus)) { |
| 206 | if (dev != 0) |
| 207 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 208 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 209 | if (access_type == RCAR_PCI_ACCESS_READ) { |
| 210 | *data = rcar_pci_read_reg(pcie, PCICONF(index)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 211 | } else { |
| 212 | /* Keep an eye out for changes to the root bus number */ |
| 213 | if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS)) |
| 214 | pcie->root_bus_nr = *data & 0xff; |
| 215 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 216 | rcar_pci_write_reg(pcie, *data, PCICONF(index)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return PCIBIOS_SUCCESSFUL; |
| 220 | } |
| 221 | |
| 222 | if (pcie->root_bus_nr < 0) |
| 223 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 224 | |
| 225 | /* Clear errors */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 226 | rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 227 | |
| 228 | /* Set the PIO address */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 229 | rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) | |
| 230 | PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 231 | |
| 232 | /* Enable the configuration access */ |
| 233 | if (bus->parent->number == pcie->root_bus_nr) |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 234 | rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 235 | else |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 236 | rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 237 | |
| 238 | /* Check for errors */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 239 | if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 240 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 241 | |
| 242 | /* Check for master and target aborts */ |
| 243 | if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) & |
| 244 | (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT)) |
| 245 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 246 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 247 | if (access_type == RCAR_PCI_ACCESS_READ) |
| 248 | *data = rcar_pci_read_reg(pcie, PCIECDR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 249 | else |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 250 | rcar_pci_write_reg(pcie, *data, PCIECDR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 251 | |
| 252 | /* Disable the configuration access */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 253 | rcar_pci_write_reg(pcie, 0, PCIECCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 254 | |
| 255 | return PCIBIOS_SUCCESSFUL; |
| 256 | } |
| 257 | |
| 258 | static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn, |
| 259 | int where, int size, u32 *val) |
| 260 | { |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 261 | struct rcar_pcie *pcie = bus->sysdata; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 262 | int ret; |
| 263 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 264 | ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 265 | bus, devfn, where, val); |
| 266 | if (ret != PCIBIOS_SUCCESSFUL) { |
| 267 | *val = 0xffffffff; |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | if (size == 1) |
| 272 | *val = (*val >> (8 * (where & 3))) & 0xff; |
| 273 | else if (size == 2) |
| 274 | *val = (*val >> (8 * (where & 2))) & 0xffff; |
| 275 | |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 276 | dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n", |
| 277 | bus->number, devfn, where, size, (unsigned long)*val); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ |
| 283 | static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn, |
| 284 | int where, int size, u32 val) |
| 285 | { |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 286 | struct rcar_pcie *pcie = bus->sysdata; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 287 | int shift, ret; |
| 288 | u32 data; |
| 289 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 290 | ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 291 | bus, devfn, where, &data); |
| 292 | if (ret != PCIBIOS_SUCCESSFUL) |
| 293 | return ret; |
| 294 | |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 295 | dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n", |
| 296 | bus->number, devfn, where, size, (unsigned long)val); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 297 | |
| 298 | if (size == 1) { |
| 299 | shift = 8 * (where & 3); |
| 300 | data &= ~(0xff << shift); |
| 301 | data |= ((val & 0xff) << shift); |
| 302 | } else if (size == 2) { |
| 303 | shift = 8 * (where & 2); |
| 304 | data &= ~(0xffff << shift); |
| 305 | data |= ((val & 0xffff) << shift); |
| 306 | } else |
| 307 | data = val; |
| 308 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 309 | ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 310 | bus, devfn, where, &data); |
| 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static struct pci_ops rcar_pcie_ops = { |
| 316 | .read = rcar_pcie_read_conf, |
| 317 | .write = rcar_pcie_write_conf, |
| 318 | }; |
| 319 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 320 | static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie, |
| 321 | struct resource *res) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 322 | { |
| 323 | /* Setup PCIe address space mappings for each resource */ |
| 324 | resource_size_t size; |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 325 | resource_size_t res_start; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 326 | u32 mask; |
| 327 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 328 | rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * The PAMR mask is calculated in units of 128Bytes, which |
| 332 | * keeps things pretty simple. |
| 333 | */ |
| 334 | size = resource_size(res); |
| 335 | mask = (roundup_pow_of_two(size) / SZ_128) - 1; |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 336 | rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 337 | |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 338 | if (res->flags & IORESOURCE_IO) |
| 339 | res_start = pci_pio_to_address(res->start); |
| 340 | else |
| 341 | res_start = res->start; |
| 342 | |
Nobuhiro Iwamatsu | ecd0630 | 2015-02-04 18:02:55 +0900 | [diff] [blame] | 343 | rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win)); |
Nobuhiro Iwamatsu | 2ea2a27 | 2015-02-02 14:09:58 +0900 | [diff] [blame] | 344 | rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F, |
Nobuhiro Iwamatsu | ecd0630 | 2015-02-04 18:02:55 +0900 | [diff] [blame] | 345 | PCIEPALR(win)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 346 | |
| 347 | /* First resource is for IO */ |
| 348 | mask = PAR_ENABLE; |
| 349 | if (res->flags & IORESOURCE_IO) |
| 350 | mask |= IO_SPACE; |
| 351 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 352 | rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 355 | static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 356 | { |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 357 | struct resource_entry *win; |
| 358 | int i = 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 359 | |
| 360 | /* Setup PCI resources */ |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 361 | resource_list_for_each_entry(win, &pci->resources) { |
| 362 | struct resource *res = win->res; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 363 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 364 | if (!res->flags) |
| 365 | continue; |
| 366 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 367 | switch (resource_type(res)) { |
| 368 | case IORESOURCE_IO: |
| 369 | case IORESOURCE_MEM: |
| 370 | rcar_pcie_setup_window(i, pci, res); |
| 371 | i++; |
| 372 | break; |
| 373 | case IORESOURCE_BUS: |
| 374 | pci->root_bus_nr = res->start; |
| 375 | break; |
| 376 | default: |
| 377 | continue; |
Phil Edworthy | d0c3f4d | 2015-10-02 11:25:04 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 380 | pci_add_resource(resource, res); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 381 | } |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 382 | |
| 383 | return 1; |
| 384 | } |
| 385 | |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 386 | static int rcar_pcie_enable(struct rcar_pcie *pcie) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 387 | { |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 388 | struct pci_bus *bus, *child; |
| 389 | LIST_HEAD(res); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 390 | |
Phil Edworthy | 8c53e8e | 2015-10-02 11:25:07 +0100 | [diff] [blame] | 391 | rcar_pcie_setup(&res, pcie); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 392 | |
Phil Edworthy | 79953dd | 2015-10-02 11:25:05 +0100 | [diff] [blame] | 393 | /* Do not reassign resources if probe only */ |
| 394 | if (!pci_has_flag(PCI_PROBE_ONLY)) |
| 395 | pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS); |
| 396 | |
| 397 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 398 | bus = pci_scan_root_bus_msi(pcie->dev, pcie->root_bus_nr, |
| 399 | &rcar_pcie_ops, pcie, &res, &pcie->msi.chip); |
| 400 | else |
| 401 | bus = pci_scan_root_bus(pcie->dev, pcie->root_bus_nr, |
| 402 | &rcar_pcie_ops, pcie, &res); |
| 403 | |
| 404 | if (!bus) { |
| 405 | dev_err(pcie->dev, "Scanning rootbus failed"); |
| 406 | return -ENODEV; |
| 407 | } |
| 408 | |
| 409 | pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci); |
| 410 | |
| 411 | if (!pci_has_flag(PCI_PROBE_ONLY)) { |
| 412 | pci_bus_size_bridges(bus); |
| 413 | pci_bus_assign_resources(bus); |
| 414 | |
| 415 | list_for_each_entry(child, &bus->children, node) |
| 416 | pcie_bus_configure_settings(child); |
| 417 | } |
| 418 | |
| 419 | pci_bus_add_devices(bus); |
| 420 | |
| 421 | return 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int phy_wait_for_ack(struct rcar_pcie *pcie) |
| 425 | { |
| 426 | unsigned int timeout = 100; |
| 427 | |
| 428 | while (timeout--) { |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 429 | if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 430 | return 0; |
| 431 | |
| 432 | udelay(100); |
| 433 | } |
| 434 | |
| 435 | dev_err(pcie->dev, "Access to PCIe phy timed out\n"); |
| 436 | |
| 437 | return -ETIMEDOUT; |
| 438 | } |
| 439 | |
| 440 | static void phy_write_reg(struct rcar_pcie *pcie, |
| 441 | unsigned int rate, unsigned int addr, |
| 442 | unsigned int lane, unsigned int data) |
| 443 | { |
| 444 | unsigned long phyaddr; |
| 445 | |
| 446 | phyaddr = WRITE_CMD | |
| 447 | ((rate & 1) << RATE_POS) | |
| 448 | ((lane & 0xf) << LANE_POS) | |
| 449 | ((addr & 0xff) << ADR_POS); |
| 450 | |
| 451 | /* Set write data */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 452 | rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR); |
| 453 | rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 454 | |
| 455 | /* Ignore errors as they will be dealt with if the data link is down */ |
| 456 | phy_wait_for_ack(pcie); |
| 457 | |
| 458 | /* Clear command */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 459 | rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR); |
| 460 | rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 461 | |
| 462 | /* Ignore errors as they will be dealt with if the data link is down */ |
| 463 | phy_wait_for_ack(pcie); |
| 464 | } |
| 465 | |
| 466 | static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie) |
| 467 | { |
| 468 | unsigned int timeout = 10; |
| 469 | |
| 470 | while (timeout--) { |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 471 | if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE)) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 472 | return 0; |
| 473 | |
| 474 | msleep(5); |
| 475 | } |
| 476 | |
| 477 | return -ETIMEDOUT; |
| 478 | } |
| 479 | |
| 480 | static int rcar_pcie_hw_init(struct rcar_pcie *pcie) |
| 481 | { |
| 482 | int err; |
| 483 | |
| 484 | /* Begin initialization */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 485 | rcar_pci_write_reg(pcie, 0, PCIETCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 486 | |
| 487 | /* Set mode */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 488 | rcar_pci_write_reg(pcie, 1, PCIEMSR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Initial header for port config space is type 1, set the device |
| 492 | * class to match. Hardware takes care of propagating the IDSETR |
| 493 | * settings, so there is no need to bother with a quirk. |
| 494 | */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 495 | rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * Setup Secondary Bus Number & Subordinate Bus Number, even though |
| 499 | * they aren't used, to avoid bridge being detected as broken. |
| 500 | */ |
| 501 | rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1); |
| 502 | rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1); |
| 503 | |
| 504 | /* Initialize default capabilities. */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 505 | rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 506 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS), |
| 507 | PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4); |
| 508 | rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f, |
| 509 | PCI_HEADER_TYPE_BRIDGE); |
| 510 | |
| 511 | /* Enable data link layer active state reporting */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 512 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC, |
| 513 | PCI_EXP_LNKCAP_DLLLARC); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 514 | |
| 515 | /* Write out the physical slot number = 0 */ |
| 516 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0); |
| 517 | |
| 518 | /* Set the completion timer timeout to the maximum 50ms. */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 519 | rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 520 | |
| 521 | /* Terminate list of capabilities (Next Capability Offset=0) */ |
Phil Edworthy | 2c3fd4c | 2014-06-30 08:54:22 +0100 | [diff] [blame] | 522 | rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 523 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 524 | /* Enable MSI */ |
| 525 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
Nobuhiro Iwamatsu | 1fc6aa9 | 2015-02-02 14:09:39 +0900 | [diff] [blame] | 526 | rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 527 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 528 | /* Finish initialization - establish a PCI Express link */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 529 | rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 530 | |
| 531 | /* This will timeout if we don't have a link. */ |
| 532 | err = rcar_pcie_wait_for_dl(pcie); |
| 533 | if (err) |
| 534 | return err; |
| 535 | |
| 536 | /* Enable INTx interrupts */ |
| 537 | rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8); |
| 538 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 539 | wmb(); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie) |
| 545 | { |
| 546 | unsigned int timeout = 10; |
| 547 | |
| 548 | /* Initialize the phy */ |
| 549 | phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191); |
| 550 | phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180); |
| 551 | phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188); |
| 552 | phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188); |
| 553 | phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014); |
| 554 | phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014); |
| 555 | phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0); |
| 556 | phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB); |
| 557 | phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062); |
| 558 | phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000); |
| 559 | phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000); |
| 560 | phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806); |
| 561 | |
| 562 | phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5); |
| 563 | phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F); |
| 564 | phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000); |
| 565 | |
| 566 | while (timeout--) { |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 567 | if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR)) |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 568 | return rcar_pcie_hw_init(pcie); |
| 569 | |
| 570 | msleep(5); |
| 571 | } |
| 572 | |
| 573 | return -ETIMEDOUT; |
| 574 | } |
| 575 | |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame^] | 576 | static int rcar_pcie_hw_init_gen2(struct rcar_pcie *pcie) |
| 577 | { |
| 578 | /* |
| 579 | * These settings come from the R-Car Series, 2nd Generation User's |
| 580 | * Manual, section 50.3.1 (2) Initialization of the physical layer. |
| 581 | */ |
| 582 | rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR); |
| 583 | rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA); |
| 584 | rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL); |
| 585 | rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL); |
| 586 | |
| 587 | rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR); |
| 588 | /* The following value is for DC connection, no termination resistor */ |
| 589 | rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA); |
| 590 | rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL); |
| 591 | rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL); |
| 592 | |
| 593 | return rcar_pcie_hw_init(pcie); |
| 594 | } |
| 595 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 596 | static int rcar_msi_alloc(struct rcar_msi *chip) |
| 597 | { |
| 598 | int msi; |
| 599 | |
| 600 | mutex_lock(&chip->lock); |
| 601 | |
| 602 | msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR); |
| 603 | if (msi < INT_PCI_MSI_NR) |
| 604 | set_bit(msi, chip->used); |
| 605 | else |
| 606 | msi = -ENOSPC; |
| 607 | |
| 608 | mutex_unlock(&chip->lock); |
| 609 | |
| 610 | return msi; |
| 611 | } |
| 612 | |
| 613 | static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq) |
| 614 | { |
| 615 | mutex_lock(&chip->lock); |
| 616 | clear_bit(irq, chip->used); |
| 617 | mutex_unlock(&chip->lock); |
| 618 | } |
| 619 | |
| 620 | static irqreturn_t rcar_pcie_msi_irq(int irq, void *data) |
| 621 | { |
| 622 | struct rcar_pcie *pcie = data; |
| 623 | struct rcar_msi *msi = &pcie->msi; |
| 624 | unsigned long reg; |
| 625 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 626 | reg = rcar_pci_read_reg(pcie, PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 627 | |
| 628 | /* MSI & INTx share an interrupt - we only handle MSI here */ |
| 629 | if (!reg) |
| 630 | return IRQ_NONE; |
| 631 | |
| 632 | while (reg) { |
| 633 | unsigned int index = find_first_bit(®, 32); |
| 634 | unsigned int irq; |
| 635 | |
| 636 | /* clear the interrupt */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 637 | rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 638 | |
| 639 | irq = irq_find_mapping(msi->domain, index); |
| 640 | if (irq) { |
| 641 | if (test_bit(index, msi->used)) |
| 642 | generic_handle_irq(irq); |
| 643 | else |
| 644 | dev_info(pcie->dev, "unhandled MSI\n"); |
| 645 | } else { |
| 646 | /* Unknown MSI, just clear it */ |
| 647 | dev_dbg(pcie->dev, "unexpected MSI\n"); |
| 648 | } |
| 649 | |
| 650 | /* see if there's any more pending in this vector */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 651 | reg = rcar_pci_read_reg(pcie, PCIEMSIFR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return IRQ_HANDLED; |
| 655 | } |
| 656 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 657 | static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev, |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 658 | struct msi_desc *desc) |
| 659 | { |
| 660 | struct rcar_msi *msi = to_rcar_msi(chip); |
| 661 | struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip); |
| 662 | struct msi_msg msg; |
| 663 | unsigned int irq; |
| 664 | int hwirq; |
| 665 | |
| 666 | hwirq = rcar_msi_alloc(msi); |
| 667 | if (hwirq < 0) |
| 668 | return hwirq; |
| 669 | |
| 670 | irq = irq_create_mapping(msi->domain, hwirq); |
| 671 | if (!irq) { |
| 672 | rcar_msi_free(msi, hwirq); |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | |
| 676 | irq_set_msi_desc(irq, desc); |
| 677 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 678 | msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE; |
| 679 | msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 680 | msg.data = hwirq; |
| 681 | |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 682 | pci_write_msi_msg(irq, &msg); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 687 | static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq) |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 688 | { |
| 689 | struct rcar_msi *msi = to_rcar_msi(chip); |
| 690 | struct irq_data *d = irq_get_irq_data(irq); |
| 691 | |
| 692 | rcar_msi_free(msi, d->hwirq); |
| 693 | } |
| 694 | |
| 695 | static struct irq_chip rcar_msi_irq_chip = { |
| 696 | .name = "R-Car PCIe MSI", |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 697 | .irq_enable = pci_msi_unmask_irq, |
| 698 | .irq_disable = pci_msi_mask_irq, |
| 699 | .irq_mask = pci_msi_mask_irq, |
| 700 | .irq_unmask = pci_msi_unmask_irq, |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | static int rcar_msi_map(struct irq_domain *domain, unsigned int irq, |
| 704 | irq_hw_number_t hwirq) |
| 705 | { |
| 706 | irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq); |
| 707 | irq_set_chip_data(irq, domain->host_data); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static const struct irq_domain_ops msi_domain_ops = { |
| 713 | .map = rcar_msi_map, |
| 714 | }; |
| 715 | |
| 716 | static int rcar_pcie_enable_msi(struct rcar_pcie *pcie) |
| 717 | { |
| 718 | struct platform_device *pdev = to_platform_device(pcie->dev); |
| 719 | struct rcar_msi *msi = &pcie->msi; |
| 720 | unsigned long base; |
| 721 | int err; |
| 722 | |
| 723 | mutex_init(&msi->lock); |
| 724 | |
| 725 | msi->chip.dev = pcie->dev; |
| 726 | msi->chip.setup_irq = rcar_msi_setup_irq; |
| 727 | msi->chip.teardown_irq = rcar_msi_teardown_irq; |
| 728 | |
| 729 | msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR, |
| 730 | &msi_domain_ops, &msi->chip); |
| 731 | if (!msi->domain) { |
| 732 | dev_err(&pdev->dev, "failed to create IRQ domain\n"); |
| 733 | return -ENOMEM; |
| 734 | } |
| 735 | |
| 736 | /* Two irqs are for MSI, but they are also used for non-MSI irqs */ |
| 737 | err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq, |
| 738 | IRQF_SHARED, rcar_msi_irq_chip.name, pcie); |
| 739 | if (err < 0) { |
| 740 | dev_err(&pdev->dev, "failed to request IRQ: %d\n", err); |
| 741 | goto err; |
| 742 | } |
| 743 | |
| 744 | err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq, |
| 745 | IRQF_SHARED, rcar_msi_irq_chip.name, pcie); |
| 746 | if (err < 0) { |
| 747 | dev_err(&pdev->dev, "failed to request IRQ: %d\n", err); |
| 748 | goto err; |
| 749 | } |
| 750 | |
| 751 | /* setup MSI data target */ |
| 752 | msi->pages = __get_free_pages(GFP_KERNEL, 0); |
| 753 | base = virt_to_phys((void *)msi->pages); |
| 754 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 755 | rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR); |
| 756 | rcar_pci_write_reg(pcie, 0, PCIEMSIAUR); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 757 | |
| 758 | /* enable all MSI interrupts */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 759 | rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER); |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 760 | |
| 761 | return 0; |
| 762 | |
| 763 | err: |
| 764 | irq_domain_remove(msi->domain); |
| 765 | return err; |
| 766 | } |
| 767 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 768 | static int rcar_pcie_get_resources(struct platform_device *pdev, |
| 769 | struct rcar_pcie *pcie) |
| 770 | { |
| 771 | struct resource res; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 772 | int err, i; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 773 | |
| 774 | err = of_address_to_resource(pdev->dev.of_node, 0, &res); |
| 775 | if (err) |
| 776 | return err; |
| 777 | |
| 778 | pcie->clk = devm_clk_get(&pdev->dev, "pcie"); |
| 779 | if (IS_ERR(pcie->clk)) { |
| 780 | dev_err(pcie->dev, "cannot get platform clock\n"); |
| 781 | return PTR_ERR(pcie->clk); |
| 782 | } |
| 783 | err = clk_prepare_enable(pcie->clk); |
| 784 | if (err) |
| 785 | goto fail_clk; |
| 786 | |
| 787 | pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus"); |
| 788 | if (IS_ERR(pcie->bus_clk)) { |
| 789 | dev_err(pcie->dev, "cannot get pcie bus clock\n"); |
| 790 | err = PTR_ERR(pcie->bus_clk); |
| 791 | goto fail_clk; |
| 792 | } |
| 793 | err = clk_prepare_enable(pcie->bus_clk); |
| 794 | if (err) |
| 795 | goto err_map_reg; |
| 796 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 797 | i = irq_of_parse_and_map(pdev->dev.of_node, 0); |
Dmitry Torokhov | c51d411 | 2014-11-14 14:21:53 -0800 | [diff] [blame] | 798 | if (!i) { |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 799 | dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n"); |
| 800 | err = -ENOENT; |
| 801 | goto err_map_reg; |
| 802 | } |
| 803 | pcie->msi.irq1 = i; |
| 804 | |
| 805 | i = irq_of_parse_and_map(pdev->dev.of_node, 1); |
Dmitry Torokhov | c51d411 | 2014-11-14 14:21:53 -0800 | [diff] [blame] | 806 | if (!i) { |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 807 | dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n"); |
| 808 | err = -ENOENT; |
| 809 | goto err_map_reg; |
| 810 | } |
| 811 | pcie->msi.irq2 = i; |
| 812 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 813 | pcie->base = devm_ioremap_resource(&pdev->dev, &res); |
| 814 | if (IS_ERR(pcie->base)) { |
| 815 | err = PTR_ERR(pcie->base); |
| 816 | goto err_map_reg; |
| 817 | } |
| 818 | |
| 819 | return 0; |
| 820 | |
| 821 | err_map_reg: |
| 822 | clk_disable_unprepare(pcie->bus_clk); |
| 823 | fail_clk: |
| 824 | clk_disable_unprepare(pcie->clk); |
| 825 | |
| 826 | return err; |
| 827 | } |
| 828 | |
| 829 | static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, |
| 830 | struct of_pci_range *range, |
| 831 | int *index) |
| 832 | { |
| 833 | u64 restype = range->flags; |
| 834 | u64 cpu_addr = range->cpu_addr; |
| 835 | u64 cpu_end = range->cpu_addr + range->size; |
| 836 | u64 pci_addr = range->pci_addr; |
| 837 | u32 flags = LAM_64BIT | LAR_ENABLE; |
| 838 | u64 mask; |
| 839 | u64 size; |
| 840 | int idx = *index; |
| 841 | |
| 842 | if (restype & IORESOURCE_PREFETCH) |
| 843 | flags |= LAM_PREFETCH; |
| 844 | |
| 845 | /* |
| 846 | * If the size of the range is larger than the alignment of the start |
| 847 | * address, we have to use multiple entries to perform the mapping. |
| 848 | */ |
| 849 | if (cpu_addr > 0) { |
| 850 | unsigned long nr_zeros = __ffs64(cpu_addr); |
| 851 | u64 alignment = 1ULL << nr_zeros; |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 852 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 853 | size = min(range->size, alignment); |
| 854 | } else { |
| 855 | size = range->size; |
| 856 | } |
| 857 | /* Hardware supports max 4GiB inbound region */ |
| 858 | size = min(size, 1ULL << 32); |
| 859 | |
| 860 | mask = roundup_pow_of_two(size) - 1; |
| 861 | mask &= ~0xf; |
| 862 | |
| 863 | while (cpu_addr < cpu_end) { |
| 864 | /* |
| 865 | * Set up 64-bit inbound regions as the range parser doesn't |
| 866 | * distinguish between 32 and 64-bit types. |
| 867 | */ |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 868 | rcar_pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx)); |
| 869 | rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx)); |
| 870 | rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 871 | |
Phil Edworthy | b7718849 | 2014-06-30 08:54:23 +0100 | [diff] [blame] | 872 | rcar_pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1)); |
| 873 | rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1)); |
| 874 | rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1)); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 875 | |
| 876 | pci_addr += size; |
| 877 | cpu_addr += size; |
| 878 | idx += 2; |
| 879 | |
| 880 | if (idx > MAX_NR_INBOUND_MAPS) { |
| 881 | dev_err(pcie->dev, "Failed to map inbound regions!\n"); |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | } |
| 885 | *index = idx; |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int pci_dma_range_parser_init(struct of_pci_range_parser *parser, |
| 891 | struct device_node *node) |
| 892 | { |
| 893 | const int na = 3, ns = 2; |
| 894 | int rlen; |
| 895 | |
| 896 | parser->node = node; |
| 897 | parser->pna = of_n_addr_cells(node); |
| 898 | parser->np = parser->pna + na + ns; |
| 899 | |
| 900 | parser->range = of_get_property(node, "dma-ranges", &rlen); |
| 901 | if (!parser->range) |
| 902 | return -ENOENT; |
| 903 | |
| 904 | parser->end = parser->range + rlen / sizeof(__be32); |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie, |
| 909 | struct device_node *np) |
| 910 | { |
| 911 | struct of_pci_range range; |
| 912 | struct of_pci_range_parser parser; |
| 913 | int index = 0; |
| 914 | int err; |
| 915 | |
| 916 | if (pci_dma_range_parser_init(&parser, np)) |
| 917 | return -EINVAL; |
| 918 | |
| 919 | /* Get the dma-ranges from DT */ |
| 920 | for_each_of_pci_range(&parser, &range) { |
| 921 | u64 end = range.cpu_addr + range.size - 1; |
| 922 | dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n", |
| 923 | range.flags, range.cpu_addr, end, range.pci_addr); |
| 924 | |
| 925 | err = rcar_pcie_inbound_ranges(pcie, &range, &index); |
| 926 | if (err) |
| 927 | return err; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static const struct of_device_id rcar_pcie_of_match[] = { |
| 934 | { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 }, |
Phil Edworthy | 581d943 | 2016-01-05 13:00:31 +0000 | [diff] [blame^] | 935 | { .compatible = "renesas,pcie-rcar-gen2", .data = rcar_pcie_hw_init_gen2 }, |
| 936 | { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init_gen2 }, |
| 937 | { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init_gen2 }, |
Harunobu Kurokawa | e015f88 | 2015-11-25 15:30:39 +0000 | [diff] [blame] | 938 | { .compatible = "renesas,pcie-r8a7795", .data = rcar_pcie_hw_init }, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 939 | {}, |
| 940 | }; |
| 941 | MODULE_DEVICE_TABLE(of, rcar_pcie_of_match); |
| 942 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 943 | static void rcar_pcie_release_of_pci_ranges(struct rcar_pcie *pci) |
| 944 | { |
| 945 | pci_free_resource_list(&pci->resources); |
| 946 | } |
| 947 | |
| 948 | static int rcar_pcie_parse_request_of_pci_ranges(struct rcar_pcie *pci) |
| 949 | { |
| 950 | int err; |
| 951 | struct device *dev = pci->dev; |
| 952 | struct device_node *np = dev->of_node; |
| 953 | resource_size_t iobase; |
| 954 | struct resource_entry *win; |
| 955 | |
| 956 | err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources, &iobase); |
| 957 | if (err) |
| 958 | return err; |
| 959 | |
| 960 | resource_list_for_each_entry(win, &pci->resources) { |
| 961 | struct resource *parent, *res = win->res; |
| 962 | |
| 963 | switch (resource_type(res)) { |
| 964 | case IORESOURCE_IO: |
| 965 | parent = &ioport_resource; |
| 966 | err = pci_remap_iospace(res, iobase); |
| 967 | if (err) { |
| 968 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 969 | err, res); |
| 970 | continue; |
| 971 | } |
| 972 | break; |
| 973 | case IORESOURCE_MEM: |
| 974 | parent = &iomem_resource; |
| 975 | break; |
| 976 | |
| 977 | case IORESOURCE_BUS: |
| 978 | default: |
| 979 | continue; |
| 980 | } |
| 981 | |
| 982 | err = devm_request_resource(dev, parent, res); |
| 983 | if (err) |
| 984 | goto out_release_res; |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | |
| 989 | out_release_res: |
| 990 | rcar_pcie_release_of_pci_ranges(pci); |
| 991 | return err; |
| 992 | } |
| 993 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 994 | static int rcar_pcie_probe(struct platform_device *pdev) |
| 995 | { |
| 996 | struct rcar_pcie *pcie; |
| 997 | unsigned int data; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 998 | const struct of_device_id *of_id; |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 999 | int err; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1000 | int (*hw_init_fn)(struct rcar_pcie *); |
| 1001 | |
| 1002 | pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL); |
| 1003 | if (!pcie) |
| 1004 | return -ENOMEM; |
| 1005 | |
| 1006 | pcie->dev = &pdev->dev; |
| 1007 | platform_set_drvdata(pdev, pcie); |
| 1008 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 1009 | INIT_LIST_HEAD(&pcie->resources); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1010 | |
Phil Edworthy | 5d2917d | 2015-11-25 15:30:37 +0000 | [diff] [blame] | 1011 | rcar_pcie_parse_request_of_pci_ranges(pcie); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1012 | |
| 1013 | err = rcar_pcie_get_resources(pdev, pcie); |
| 1014 | if (err < 0) { |
| 1015 | dev_err(&pdev->dev, "failed to request resources: %d\n", err); |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1019 | err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node); |
| 1020 | if (err) |
| 1021 | return err; |
| 1022 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1023 | of_id = of_match_device(rcar_pcie_of_match, pcie->dev); |
| 1024 | if (!of_id || !of_id->data) |
| 1025 | return -EINVAL; |
| 1026 | hw_init_fn = of_id->data; |
| 1027 | |
| 1028 | pm_runtime_enable(pcie->dev); |
| 1029 | err = pm_runtime_get_sync(pcie->dev); |
| 1030 | if (err < 0) { |
| 1031 | dev_err(pcie->dev, "pm_runtime_get_sync failed\n"); |
| 1032 | goto err_pm_disable; |
| 1033 | } |
| 1034 | |
| 1035 | /* Failure to get a link might just be that no cards are inserted */ |
| 1036 | err = hw_init_fn(pcie); |
| 1037 | if (err) { |
| 1038 | dev_info(&pdev->dev, "PCIe link down\n"); |
| 1039 | err = 0; |
| 1040 | goto err_pm_put; |
| 1041 | } |
| 1042 | |
| 1043 | data = rcar_pci_read_reg(pcie, MACSR); |
| 1044 | dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f); |
| 1045 | |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 1046 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 1047 | err = rcar_pcie_enable_msi(pcie); |
| 1048 | if (err < 0) { |
| 1049 | dev_err(&pdev->dev, |
| 1050 | "failed to enable MSI support: %d\n", |
| 1051 | err); |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1052 | goto err_pm_put; |
Phil Edworthy | 290c1fb | 2014-05-12 11:57:49 +0100 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1056 | err = rcar_pcie_enable(pcie); |
| 1057 | if (err) |
| 1058 | goto err_pm_put; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1059 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1060 | return 0; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1061 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1062 | err_pm_put: |
| 1063 | pm_runtime_put(pcie->dev); |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1064 | |
Phil Edworthy | de1be9a | 2016-01-05 13:00:30 +0000 | [diff] [blame] | 1065 | err_pm_disable: |
| 1066 | pm_runtime_disable(pcie->dev); |
| 1067 | return err; |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static struct platform_driver rcar_pcie_driver = { |
| 1071 | .driver = { |
| 1072 | .name = DRV_NAME, |
Phil Edworthy | c25da47 | 2014-05-12 11:57:48 +0100 | [diff] [blame] | 1073 | .of_match_table = rcar_pcie_of_match, |
| 1074 | .suppress_bind_attrs = true, |
| 1075 | }, |
| 1076 | .probe = rcar_pcie_probe, |
| 1077 | }; |
| 1078 | module_platform_driver(rcar_pcie_driver); |
| 1079 | |
| 1080 | MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>"); |
| 1081 | MODULE_DESCRIPTION("Renesas R-Car PCIe driver"); |
Bjorn Helgaas | 68947eb | 2014-07-15 15:06:12 -0600 | [diff] [blame] | 1082 | MODULE_LICENSE("GPL v2"); |