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