blob: 821d38a0e1ff83536fe8445657cde708d25cd69b [file] [log] [blame]
Phil Edworthyc25da472014-05-12 11:57:48 +01001/*
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 Gortmaker42d10712016-07-22 16:23:21 -050010 * Author: Phil Edworthy <phil.edworthy@renesas.com>
11 *
Phil Edworthyc25da472014-05-12 11:57:48 +010012 * 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 Edworthy290c1fb2014-05-12 11:57:49 +010020#include <linux/irq.h>
21#include <linux/irqdomain.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010022#include <linux/kernel.h>
Paul Gortmaker42d10712016-07-22 16:23:21 -050023#include <linux/init.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010024#include <linux/msi.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010025#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 Edworthyde1be9a2016-01-05 13:00:30 +000031#include <linux/pm_runtime.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010032#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 Edworthy290c1fb2014-05-12 11:57:49 +010044#define PCIEMSITXR 0x000840
Phil Edworthyc25da472014-05-12 11:57:48 +010045
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 Edworthy290c1fb2014-05-12 11:57:49 +010053#define PCIEMSIFR 0x02044
54#define PCIEMSIALR 0x02048
55#define MSIFE 1
56#define PCIEMSIAUR 0x0204c
57#define PCIEMSIIER 0x02050
Phil Edworthyc25da472014-05-12 11:57:48 +010058
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 Iwamatsuecd06302015-02-04 18:02:55 +090070#define PCIEPALR(x) (0x03400 + ((x) * 0x20))
71#define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
Phil Edworthyc25da472014-05-12 11:57:48 +010072#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 Shtylyovb3327f72016-09-22 23:20:18 +030087#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 Edworthyc25da472014-05-12 11:57:48 +010093#define MACCTLR 0x011058
Sergei Shtylyovb3327f72016-09-22 23:20:18 +030094#define SPEED_CHANGE (1 << 24)
Phil Edworthyc25da472014-05-12 11:57:48 +010095#define SCRAMBLE_DISABLE (1 << 27)
Sergei Shtylyovb3327f72016-09-22 23:20:18 +030096#define MACS2R 0x011078
97#define MACCGSPSETR 0x011084
98#define SPCNGRSN (1 << 31)
Phil Edworthyc25da472014-05-12 11:57:48 +010099
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 Edworthy581d9432016-01-05 13:00:31 +0000110/* R-Car Gen2 PHY */
111#define GEN2_PCIEPHYADDR 0x780
112#define GEN2_PCIEPHYDATA 0x784
113#define GEN2_PCIEPHYCTRL 0x78c
114
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100115#define INT_PCI_MSI_NR 32
116
Phil Edworthyc25da472014-05-12 11:57:48 +0100117#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 Edworthyb77188492014-06-30 08:54:23 +0100126#define RCAR_PCI_MAX_RESOURCES 4
Phil Edworthyc25da472014-05-12 11:57:48 +0100127#define MAX_NR_INBOUND_MAPS 6
128
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100129struct rcar_msi {
130 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
131 struct irq_domain *domain;
Yijing Wangc2791b82014-11-11 17:45:45 -0700132 struct msi_controller chip;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100133 unsigned long pages;
134 struct mutex lock;
135 int irq1;
136 int irq2;
137};
138
Yijing Wangc2791b82014-11-11 17:45:45 -0700139static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100140{
141 return container_of(chip, struct rcar_msi, chip);
142}
143
Phil Edworthyc25da472014-05-12 11:57:48 +0100144/* Structure representing the PCIe interface */
145struct rcar_pcie {
146 struct device *dev;
147 void __iomem *base;
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000148 struct list_head resources;
Phil Edworthyc25da472014-05-12 11:57:48 +0100149 int root_bus_nr;
150 struct clk *clk;
151 struct clk *bus_clk;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100152 struct rcar_msi msi;
Phil Edworthyc25da472014-05-12 11:57:48 +0100153};
154
Phil Edworthyb77188492014-06-30 08:54:23 +0100155static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
156 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100157{
158 writel(val, pcie->base + reg);
159}
160
Phil Edworthyb77188492014-06-30 08:54:23 +0100161static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
162 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100163{
164 return readl(pcie->base + reg);
165}
166
167enum {
Phil Edworthyb77188492014-06-30 08:54:23 +0100168 RCAR_PCI_ACCESS_READ,
169 RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100170};
171
172static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
173{
174 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100175 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100176
177 val &= ~(mask << shift);
178 val |= data << shift;
Phil Edworthyb77188492014-06-30 08:54:23 +0100179 rcar_pci_write_reg(pcie, val, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100180}
181
182static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
183{
184 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100185 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100186
187 return val >> shift;
188}
189
190/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
191static 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 Edworthyb77188492014-06-30 08:54:23 +0100221 if (access_type == RCAR_PCI_ACCESS_READ) {
222 *data = rcar_pci_read_reg(pcie, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100223 } 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 Edworthyb77188492014-06-30 08:54:23 +0100228 rcar_pci_write_reg(pcie, *data, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100229 }
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 Edworthyb77188492014-06-30 08:54:23 +0100238 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100239
240 /* Set the PIO address */
Phil Edworthyb77188492014-06-30 08:54:23 +0100241 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
242 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100243
244 /* Enable the configuration access */
245 if (bus->parent->number == pcie->root_bus_nr)
Phil Edworthyb77188492014-06-30 08:54:23 +0100246 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100247 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100248 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100249
250 /* Check for errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100251 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
Phil Edworthyc25da472014-05-12 11:57:48 +0100252 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 Edworthyb77188492014-06-30 08:54:23 +0100259 if (access_type == RCAR_PCI_ACCESS_READ)
260 *data = rcar_pci_read_reg(pcie, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100261 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100262 rcar_pci_write_reg(pcie, *data, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100263
264 /* Disable the configuration access */
Phil Edworthyb77188492014-06-30 08:54:23 +0100265 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100266
267 return PCIBIOS_SUCCESSFUL;
268}
269
270static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
271 int where, int size, u32 *val)
272{
Phil Edworthy79953dd2015-10-02 11:25:05 +0100273 struct rcar_pcie *pcie = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100274 int ret;
275
Phil Edworthyb77188492014-06-30 08:54:23 +0100276 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100277 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 Desfosses227f0642014-04-18 20:13:50 -0400288 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 Edworthyc25da472014-05-12 11:57:48 +0100290
291 return ret;
292}
293
294/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
295static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
296 int where, int size, u32 val)
297{
Phil Edworthy79953dd2015-10-02 11:25:05 +0100298 struct rcar_pcie *pcie = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100299 int shift, ret;
300 u32 data;
301
Phil Edworthyb77188492014-06-30 08:54:23 +0100302 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100303 bus, devfn, where, &data);
304 if (ret != PCIBIOS_SUCCESSFUL)
305 return ret;
306
Ryan Desfosses227f0642014-04-18 20:13:50 -0400307 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 Edworthyc25da472014-05-12 11:57:48 +0100309
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 Edworthyb77188492014-06-30 08:54:23 +0100321 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100322 bus, devfn, where, &data);
323
324 return ret;
325}
326
327static struct pci_ops rcar_pcie_ops = {
328 .read = rcar_pcie_read_conf,
329 .write = rcar_pcie_write_conf,
330};
331
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000332static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie,
333 struct resource *res)
Phil Edworthyc25da472014-05-12 11:57:48 +0100334{
335 /* Setup PCIe address space mappings for each resource */
336 resource_size_t size;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100337 resource_size_t res_start;
Phil Edworthyc25da472014-05-12 11:57:48 +0100338 u32 mask;
339
Phil Edworthyb77188492014-06-30 08:54:23 +0100340 rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100341
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 Edworthyb77188492014-06-30 08:54:23 +0100348 rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100349
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100350 if (res->flags & IORESOURCE_IO)
351 res_start = pci_pio_to_address(res->start);
352 else
353 res_start = res->start;
354
Nobuhiro Iwamatsuecd06302015-02-04 18:02:55 +0900355 rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
Nobuhiro Iwamatsu2ea2a272015-02-02 14:09:58 +0900356 rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
Nobuhiro Iwamatsuecd06302015-02-04 18:02:55 +0900357 PCIEPALR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100358
359 /* First resource is for IO */
360 mask = PAR_ENABLE;
361 if (res->flags & IORESOURCE_IO)
362 mask |= IO_SPACE;
363
Phil Edworthyb77188492014-06-30 08:54:23 +0100364 rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100365}
366
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000367static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci)
Phil Edworthyc25da472014-05-12 11:57:48 +0100368{
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000369 struct resource_entry *win;
370 int i = 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100371
372 /* Setup PCI resources */
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000373 resource_list_for_each_entry(win, &pci->resources) {
374 struct resource *res = win->res;
Phil Edworthyc25da472014-05-12 11:57:48 +0100375
Phil Edworthyc25da472014-05-12 11:57:48 +0100376 if (!res->flags)
377 continue;
378
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000379 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 Edworthyd0c3f4d2015-10-02 11:25:04 +0100390 }
391
Phil Edworthy79953dd2015-10-02 11:25:05 +0100392 pci_add_resource(resource, res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100393 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100394
395 return 1;
396}
397
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300398static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
399{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500400 struct device *dev = pcie->dev;
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300401 unsigned int timeout = 1000;
402 u32 macsr;
403
404 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
405 return;
406
407 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500408 dev_err(dev, "Speed change already in progress\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300409 return;
410 }
411
412 macsr = rcar_pci_read_reg(pcie, MACSR);
413 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
414 goto done;
415
416 /* Set target link speed to 5.0 GT/s */
417 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
418 PCI_EXP_LNKSTA_CLS_5_0GB);
419
420 /* Set speed change reason as intentional factor */
421 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
422
423 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
424 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
425 rcar_pci_write_reg(pcie, macsr, MACSR);
426
427 /* Start link speed change */
428 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
429
430 while (timeout--) {
431 macsr = rcar_pci_read_reg(pcie, MACSR);
432 if (macsr & SPCHGFIN) {
433 /* Clear the interrupt bits */
434 rcar_pci_write_reg(pcie, macsr, MACSR);
435
436 if (macsr & SPCHGFAIL)
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500437 dev_err(dev, "Speed change failed\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300438
439 goto done;
440 }
441
442 msleep(1);
443 };
444
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500445 dev_err(dev, "Speed change timed out\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300446
447done:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500448 dev_info(dev, "Current link speed is %s GT/s\n",
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300449 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
450}
451
Phil Edworthy79953dd2015-10-02 11:25:05 +0100452static int rcar_pcie_enable(struct rcar_pcie *pcie)
Phil Edworthyc25da472014-05-12 11:57:48 +0100453{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500454 struct device *dev = pcie->dev;
Phil Edworthy79953dd2015-10-02 11:25:05 +0100455 struct pci_bus *bus, *child;
456 LIST_HEAD(res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100457
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300458 /* Try setting 5 GT/s link speed */
459 rcar_pcie_force_speedup(pcie);
460
Phil Edworthy8c53e8e2015-10-02 11:25:07 +0100461 rcar_pcie_setup(&res, pcie);
Phil Edworthyc25da472014-05-12 11:57:48 +0100462
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000463 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100464
465 if (IS_ENABLED(CONFIG_PCI_MSI))
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500466 bus = pci_scan_root_bus_msi(dev, pcie->root_bus_nr,
Phil Edworthy79953dd2015-10-02 11:25:05 +0100467 &rcar_pcie_ops, pcie, &res, &pcie->msi.chip);
468 else
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500469 bus = pci_scan_root_bus(dev, pcie->root_bus_nr,
Phil Edworthy79953dd2015-10-02 11:25:05 +0100470 &rcar_pcie_ops, pcie, &res);
471
472 if (!bus) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500473 dev_err(dev, "Scanning rootbus failed");
Phil Edworthy79953dd2015-10-02 11:25:05 +0100474 return -ENODEV;
475 }
476
477 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
478
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000479 pci_bus_size_bridges(bus);
480 pci_bus_assign_resources(bus);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100481
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000482 list_for_each_entry(child, &bus->children, node)
483 pcie_bus_configure_settings(child);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100484
485 pci_bus_add_devices(bus);
486
487 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100488}
489
490static int phy_wait_for_ack(struct rcar_pcie *pcie)
491{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500492 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100493 unsigned int timeout = 100;
494
495 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100496 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
Phil Edworthyc25da472014-05-12 11:57:48 +0100497 return 0;
498
499 udelay(100);
500 }
501
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500502 dev_err(dev, "Access to PCIe phy timed out\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100503
504 return -ETIMEDOUT;
505}
506
507static 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 Edworthyb77188492014-06-30 08:54:23 +0100519 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
520 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100521
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 Edworthyb77188492014-06-30 08:54:23 +0100526 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
527 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100528
529 /* Ignore errors as they will be dealt with if the data link is down */
530 phy_wait_for_ack(pcie);
531}
532
533static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
534{
535 unsigned int timeout = 10;
536
537 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100538 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
Phil Edworthyc25da472014-05-12 11:57:48 +0100539 return 0;
540
541 msleep(5);
542 }
543
544 return -ETIMEDOUT;
545}
546
547static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
548{
549 int err;
550
551 /* Begin initialization */
Phil Edworthyb77188492014-06-30 08:54:23 +0100552 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100553
554 /* Set mode */
Phil Edworthyb77188492014-06-30 08:54:23 +0100555 rcar_pci_write_reg(pcie, 1, PCIEMSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100556
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 Edworthyb77188492014-06-30 08:54:23 +0100562 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
Phil Edworthyc25da472014-05-12 11:57:48 +0100563
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 Edworthy2c3fd4c2014-06-30 08:54:22 +0100572 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
Phil Edworthyc25da472014-05-12 11:57:48 +0100573 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 Edworthy2c3fd4c2014-06-30 08:54:22 +0100579 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
580 PCI_EXP_LNKCAP_DLLLARC);
Phil Edworthyc25da472014-05-12 11:57:48 +0100581
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 Edworthyb77188492014-06-30 08:54:23 +0100586 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
Phil Edworthyc25da472014-05-12 11:57:48 +0100587
588 /* Terminate list of capabilities (Next Capability Offset=0) */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100589 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
Phil Edworthyc25da472014-05-12 11:57:48 +0100590
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100591 /* Enable MSI */
592 if (IS_ENABLED(CONFIG_PCI_MSI))
Nobuhiro Iwamatsu1fc6aa92015-02-02 14:09:39 +0900593 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100594
Phil Edworthyc25da472014-05-12 11:57:48 +0100595 /* Finish initialization - establish a PCI Express link */
Phil Edworthyb77188492014-06-30 08:54:23 +0100596 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100597
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 Edworthyc25da472014-05-12 11:57:48 +0100606 wmb();
607
608 return 0;
609}
610
611static 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 Edworthyb77188492014-06-30 08:54:23 +0100634 if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
Phil Edworthyc25da472014-05-12 11:57:48 +0100635 return rcar_pcie_hw_init(pcie);
636
637 msleep(5);
638 }
639
640 return -ETIMEDOUT;
641}
642
Phil Edworthy581d9432016-01-05 13:00:31 +0000643static 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 Edworthy290c1fb2014-05-12 11:57:49 +0100663static 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 Kletskoe3123c22016-09-08 22:32:59 +0300680static 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 Edworthy290c1fb2014-05-12 11:57:49 +0100692static 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
699static 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 Helgaas4ef80d72016-10-10 14:31:28 -0500703 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100704 unsigned long reg;
705
Phil Edworthyb77188492014-06-30 08:54:23 +0100706 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100707
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(&reg, 32);
714 unsigned int irq;
715
716 /* clear the interrupt */
Phil Edworthyb77188492014-06-30 08:54:23 +0100717 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100718
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 Helgaas4ef80d72016-10-10 14:31:28 -0500724 dev_info(dev, "unhandled MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100725 } else {
726 /* Unknown MSI, just clear it */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500727 dev_dbg(dev, "unexpected MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100728 }
729
730 /* see if there's any more pending in this vector */
Phil Edworthyb77188492014-06-30 08:54:23 +0100731 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100732 }
733
734 return IRQ_HANDLED;
735}
736
Yijing Wangc2791b82014-11-11 17:45:45 -0700737static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100738 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 Kletskoe3123c22016-09-08 22:32:59 +0300750 irq = irq_find_mapping(msi->domain, hwirq);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100751 if (!irq) {
752 rcar_msi_free(msi, hwirq);
753 return -EINVAL;
754 }
755
756 irq_set_msi_desc(irq, desc);
757
Phil Edworthyb77188492014-06-30 08:54:23 +0100758 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
759 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100760 msg.data = hwirq;
761
Jiang Liu83a18912014-11-09 23:10:34 +0800762 pci_write_msi_msg(irq, &msg);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100763
764 return 0;
765}
766
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300767static 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 Wangc2791b82014-11-11 17:45:45 -0700819static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100820{
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
827static struct irq_chip rcar_msi_irq_chip = {
828 .name = "R-Car PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100829 .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 Edworthy290c1fb2014-05-12 11:57:49 +0100833};
834
835static 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 Edworthy290c1fb2014-05-12 11:57:49 +0100840
841 return 0;
842}
843
844static const struct irq_domain_ops msi_domain_ops = {
845 .map = rcar_msi_map,
846};
847
848static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
849{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500850 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100851 struct rcar_msi *msi = &pcie->msi;
852 unsigned long base;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300853 int err, i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100854
855 mutex_init(&msi->lock);
856
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500857 msi->chip.dev = dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100858 msi->chip.setup_irq = rcar_msi_setup_irq;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300859 msi->chip.setup_irqs = rcar_msi_setup_irqs;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100860 msi->chip.teardown_irq = rcar_msi_teardown_irq;
861
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500862 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100863 &msi_domain_ops, &msi->chip);
864 if (!msi->domain) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500865 dev_err(dev, "failed to create IRQ domain\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100866 return -ENOMEM;
867 }
868
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300869 for (i = 0; i < INT_PCI_MSI_NR; i++)
870 irq_create_mapping(msi->domain, i);
871
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100872 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500873 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200874 IRQF_SHARED | IRQF_NO_THREAD,
875 rcar_msi_irq_chip.name, pcie);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100876 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500877 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100878 goto err;
879 }
880
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500881 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200882 IRQF_SHARED | IRQF_NO_THREAD,
883 rcar_msi_irq_chip.name, pcie);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100884 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500885 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100886 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 Edworthyb77188492014-06-30 08:54:23 +0100893 rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
894 rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100895
896 /* enable all MSI interrupts */
Phil Edworthyb77188492014-06-30 08:54:23 +0100897 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100898
899 return 0;
900
901err:
902 irq_domain_remove(msi->domain);
903 return err;
904}
905
Phil Edworthyc25da472014-05-12 11:57:48 +0100906static int rcar_pcie_get_resources(struct platform_device *pdev,
907 struct rcar_pcie *pcie)
908{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500909 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100910 struct resource res;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100911 int err, i;
Phil Edworthyc25da472014-05-12 11:57:48 +0100912
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500913 err = of_address_to_resource(dev->of_node, 0, &res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100914 if (err)
915 return err;
916
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500917 pcie->base = devm_ioremap_resource(dev, &res);
Bjorn Helgaas51afa3c2016-08-22 14:16:38 -0500918 if (IS_ERR(pcie->base))
919 return PTR_ERR(pcie->base);
920
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500921 pcie->clk = devm_clk_get(dev, "pcie");
Phil Edworthyc25da472014-05-12 11:57:48 +0100922 if (IS_ERR(pcie->clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500923 dev_err(dev, "cannot get platform clock\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100924 return PTR_ERR(pcie->clk);
925 }
926 err = clk_prepare_enable(pcie->clk);
927 if (err)
Geert Uytterhoeven3d664b02016-08-31 11:28:22 +0200928 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100929
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500930 pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
Phil Edworthyc25da472014-05-12 11:57:48 +0100931 if (IS_ERR(pcie->bus_clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500932 dev_err(dev, "cannot get pcie bus clock\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100933 err = PTR_ERR(pcie->bus_clk);
934 goto fail_clk;
935 }
936 err = clk_prepare_enable(pcie->bus_clk);
937 if (err)
Geert Uytterhoeven3d664b02016-08-31 11:28:22 +0200938 goto fail_clk;
Phil Edworthyc25da472014-05-12 11:57:48 +0100939
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500940 i = irq_of_parse_and_map(dev->of_node, 0);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800941 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500942 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100943 err = -ENOENT;
944 goto err_map_reg;
945 }
946 pcie->msi.irq1 = i;
947
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500948 i = irq_of_parse_and_map(dev->of_node, 1);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800949 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500950 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100951 err = -ENOENT;
952 goto err_map_reg;
953 }
954 pcie->msi.irq2 = i;
955
Phil Edworthyc25da472014-05-12 11:57:48 +0100956 return 0;
957
958err_map_reg:
959 clk_disable_unprepare(pcie->bus_clk);
960fail_clk:
961 clk_disable_unprepare(pcie->clk);
962
963 return err;
964}
965
966static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
967 struct of_pci_range *range,
968 int *index)
969{
970 u64 restype = range->flags;
971 u64 cpu_addr = range->cpu_addr;
972 u64 cpu_end = range->cpu_addr + range->size;
973 u64 pci_addr = range->pci_addr;
974 u32 flags = LAM_64BIT | LAR_ENABLE;
975 u64 mask;
976 u64 size;
977 int idx = *index;
978
979 if (restype & IORESOURCE_PREFETCH)
980 flags |= LAM_PREFETCH;
981
982 /*
983 * If the size of the range is larger than the alignment of the start
984 * address, we have to use multiple entries to perform the mapping.
985 */
986 if (cpu_addr > 0) {
987 unsigned long nr_zeros = __ffs64(cpu_addr);
988 u64 alignment = 1ULL << nr_zeros;
Phil Edworthyb77188492014-06-30 08:54:23 +0100989
Phil Edworthyc25da472014-05-12 11:57:48 +0100990 size = min(range->size, alignment);
991 } else {
992 size = range->size;
993 }
994 /* Hardware supports max 4GiB inbound region */
995 size = min(size, 1ULL << 32);
996
997 mask = roundup_pow_of_two(size) - 1;
998 mask &= ~0xf;
999
1000 while (cpu_addr < cpu_end) {
1001 /*
1002 * Set up 64-bit inbound regions as the range parser doesn't
1003 * distinguish between 32 and 64-bit types.
1004 */
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001005 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr),
1006 PCIEPRAR(idx));
Phil Edworthyb77188492014-06-30 08:54:23 +01001007 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001008 rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags,
1009 PCIELAMR(idx));
Phil Edworthyc25da472014-05-12 11:57:48 +01001010
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001011 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr),
1012 PCIEPRAR(idx + 1));
1013 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr),
1014 PCIELAR(idx + 1));
Phil Edworthyb77188492014-06-30 08:54:23 +01001015 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
Phil Edworthyc25da472014-05-12 11:57:48 +01001016
1017 pci_addr += size;
1018 cpu_addr += size;
1019 idx += 2;
1020
1021 if (idx > MAX_NR_INBOUND_MAPS) {
1022 dev_err(pcie->dev, "Failed to map inbound regions!\n");
1023 return -EINVAL;
1024 }
1025 }
1026 *index = idx;
1027
1028 return 0;
1029}
1030
1031static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
1032 struct device_node *node)
1033{
1034 const int na = 3, ns = 2;
1035 int rlen;
1036
1037 parser->node = node;
1038 parser->pna = of_n_addr_cells(node);
1039 parser->np = parser->pna + na + ns;
1040
1041 parser->range = of_get_property(node, "dma-ranges", &rlen);
1042 if (!parser->range)
1043 return -ENOENT;
1044
1045 parser->end = parser->range + rlen / sizeof(__be32);
1046 return 0;
1047}
1048
1049static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
1050 struct device_node *np)
1051{
1052 struct of_pci_range range;
1053 struct of_pci_range_parser parser;
1054 int index = 0;
1055 int err;
1056
1057 if (pci_dma_range_parser_init(&parser, np))
1058 return -EINVAL;
1059
1060 /* Get the dma-ranges from DT */
1061 for_each_of_pci_range(&parser, &range) {
1062 u64 end = range.cpu_addr + range.size - 1;
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001063
Phil Edworthyc25da472014-05-12 11:57:48 +01001064 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
1065 range.flags, range.cpu_addr, end, range.pci_addr);
1066
1067 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
1068 if (err)
1069 return err;
1070 }
1071
1072 return 0;
1073}
1074
1075static const struct of_device_id rcar_pcie_of_match[] = {
1076 { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001077 { .compatible = "renesas,pcie-rcar-gen2",
1078 .data = rcar_pcie_hw_init_gen2 },
1079 { .compatible = "renesas,pcie-r8a7790",
1080 .data = rcar_pcie_hw_init_gen2 },
1081 { .compatible = "renesas,pcie-r8a7791",
1082 .data = rcar_pcie_hw_init_gen2 },
Harunobu Kurokawae015f882015-11-25 15:30:39 +00001083 { .compatible = "renesas,pcie-r8a7795", .data = rcar_pcie_hw_init },
Phil Edworthyc25da472014-05-12 11:57:48 +01001084 {},
1085};
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001086
1087static 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 Pieralisi5e8c8732016-08-15 17:50:44 +01001093 struct resource_entry *win, *tmp;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001094
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001095 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
1096 &iobase);
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001097 if (err)
1098 return err;
1099
Bjorn Helgaas6fd7f552016-05-31 12:20:57 -05001100 err = devm_request_pci_bus_resources(dev, &pci->resources);
1101 if (err)
1102 goto out_release_res;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001103
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001104 resource_list_for_each_entry_safe(win, tmp, &pci->resources) {
Bjorn Helgaas6fd7f552016-05-31 12:20:57 -05001105 struct resource *res = win->res;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001106
Bjorn Helgaas4c540a32016-05-28 18:37:46 -05001107 if (resource_type(res) == IORESOURCE_IO) {
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001108 err = pci_remap_iospace(res, iobase);
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001109 if (err) {
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001110 dev_warn(dev, "error %d: failed to map resource %pR\n",
1111 err, res);
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001112
1113 resource_list_destroy_entry(win);
1114 }
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001115 }
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001116 }
1117
1118 return 0;
1119
1120out_release_res:
Bjorn Helgaas4c540a32016-05-28 18:37:46 -05001121 pci_free_resource_list(&pci->resources);
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001122 return err;
1123}
1124
Phil Edworthyc25da472014-05-12 11:57:48 +01001125static int rcar_pcie_probe(struct platform_device *pdev)
1126{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001127 struct device *dev = &pdev->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +01001128 struct rcar_pcie *pcie;
1129 unsigned int data;
Phil Edworthyc25da472014-05-12 11:57:48 +01001130 const struct of_device_id *of_id;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001131 int err;
Phil Edworthyc25da472014-05-12 11:57:48 +01001132 int (*hw_init_fn)(struct rcar_pcie *);
1133
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001134 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Phil Edworthyc25da472014-05-12 11:57:48 +01001135 if (!pcie)
1136 return -ENOMEM;
1137
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001138 pcie->dev = dev;
Phil Edworthyc25da472014-05-12 11:57:48 +01001139 platform_set_drvdata(pdev, pcie);
1140
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001141 INIT_LIST_HEAD(&pcie->resources);
Phil Edworthyc25da472014-05-12 11:57:48 +01001142
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001143 rcar_pcie_parse_request_of_pci_ranges(pcie);
Phil Edworthyc25da472014-05-12 11:57:48 +01001144
1145 err = rcar_pcie_get_resources(pdev, pcie);
1146 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001147 dev_err(dev, "failed to request resources: %d\n", err);
Phil Edworthyc25da472014-05-12 11:57:48 +01001148 return err;
1149 }
1150
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001151 err = rcar_pcie_parse_map_dma_ranges(pcie, dev->of_node);
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001152 if (err)
Phil Edworthyc25da472014-05-12 11:57:48 +01001153 return err;
1154
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001155 of_id = of_match_device(rcar_pcie_of_match, dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001156 if (!of_id || !of_id->data)
1157 return -EINVAL;
1158 hw_init_fn = of_id->data;
1159
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001160 pm_runtime_enable(dev);
1161 err = pm_runtime_get_sync(dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001162 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001163 dev_err(dev, "pm_runtime_get_sync failed\n");
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001164 goto err_pm_disable;
1165 }
1166
1167 /* Failure to get a link might just be that no cards are inserted */
1168 err = hw_init_fn(pcie);
1169 if (err) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001170 dev_info(dev, "PCIe link down\n");
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001171 err = 0;
1172 goto err_pm_put;
1173 }
1174
1175 data = rcar_pci_read_reg(pcie, MACSR);
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001176 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001177
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001178 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1179 err = rcar_pcie_enable_msi(pcie);
1180 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001181 dev_err(dev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001182 "failed to enable MSI support: %d\n",
1183 err);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001184 goto err_pm_put;
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001185 }
1186 }
1187
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001188 err = rcar_pcie_enable(pcie);
1189 if (err)
1190 goto err_pm_put;
Phil Edworthyc25da472014-05-12 11:57:48 +01001191
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001192 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +01001193
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001194err_pm_put:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001195 pm_runtime_put(dev);
Phil Edworthyc25da472014-05-12 11:57:48 +01001196
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001197err_pm_disable:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001198 pm_runtime_disable(dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001199 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +01001200}
1201
1202static struct platform_driver rcar_pcie_driver = {
1203 .driver = {
1204 .name = DRV_NAME,
Phil Edworthyc25da472014-05-12 11:57:48 +01001205 .of_match_table = rcar_pcie_of_match,
1206 .suppress_bind_attrs = true,
1207 },
1208 .probe = rcar_pcie_probe,
1209};
Paul Gortmaker42d10712016-07-22 16:23:21 -05001210builtin_platform_driver(rcar_pcie_driver);