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