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