blob: ee20676de2c7294c6b807e4a19c02a54815144a4 [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 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010018#include <linux/irq.h>
19#include <linux/irqdomain.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010020#include <linux/kernel.h>
21#include <linux/module.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010022#include <linux/msi.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010023#include <linux/of_address.h>
24#include <linux/of_irq.h>
25#include <linux/of_pci.h>
26#include <linux/of_platform.h>
27#include <linux/pci.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
31#define DRV_NAME "rcar-pcie"
32
33#define PCIECAR 0x000010
34#define PCIECCTLR 0x000018
35#define CONFIG_SEND_ENABLE (1 << 31)
36#define TYPE0 (0 << 8)
37#define TYPE1 (1 << 8)
38#define PCIECDR 0x000020
39#define PCIEMSR 0x000028
40#define PCIEINTXR 0x000400
Phil Edworthy290c1fb2014-05-12 11:57:49 +010041#define PCIEMSITXR 0x000840
Phil Edworthyc25da472014-05-12 11:57:48 +010042
43/* Transfer control */
44#define PCIETCTLR 0x02000
45#define CFINIT 1
46#define PCIETSTR 0x02004
47#define DATA_LINK_ACTIVE 1
48#define PCIEERRFR 0x02020
49#define UNSUPPORTED_REQUEST (1 << 4)
Phil Edworthy290c1fb2014-05-12 11:57:49 +010050#define PCIEMSIFR 0x02044
51#define PCIEMSIALR 0x02048
52#define MSIFE 1
53#define PCIEMSIAUR 0x0204c
54#define PCIEMSIIER 0x02050
Phil Edworthyc25da472014-05-12 11:57:48 +010055
56/* root port address */
57#define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
58
59/* local address reg & mask */
60#define PCIELAR(x) (0x02200 + ((x) * 0x20))
61#define PCIELAMR(x) (0x02208 + ((x) * 0x20))
62#define LAM_PREFETCH (1 << 3)
63#define LAM_64BIT (1 << 2)
64#define LAR_ENABLE (1 << 1)
65
66/* PCIe address reg & mask */
67#define PCIEPARL(x) (0x03400 + ((x) * 0x20))
68#define PCIEPARH(x) (0x03404 + ((x) * 0x20))
69#define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
70#define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
71#define PAR_ENABLE (1 << 31)
72#define IO_SPACE (1 << 8)
73
74/* Configuration */
75#define PCICONF(x) (0x010000 + ((x) * 0x4))
76#define PMCAP(x) (0x010040 + ((x) * 0x4))
77#define EXPCAP(x) (0x010070 + ((x) * 0x4))
78#define VCCAP(x) (0x010100 + ((x) * 0x4))
79
80/* link layer */
81#define IDSETR1 0x011004
82#define TLCTLR 0x011048
83#define MACSR 0x011054
84#define MACCTLR 0x011058
85#define SCRAMBLE_DISABLE (1 << 27)
86
87/* R-Car H1 PHY */
88#define H1_PCIEPHYADRR 0x04000c
89#define WRITE_CMD (1 << 16)
90#define PHY_ACK (1 << 24)
91#define RATE_POS 12
92#define LANE_POS 8
93#define ADR_POS 0
94#define H1_PCIEPHYDOUTR 0x040014
95#define H1_PCIEPHYSR 0x040018
96
Phil Edworthy290c1fb2014-05-12 11:57:49 +010097#define INT_PCI_MSI_NR 32
98
Phil Edworthyc25da472014-05-12 11:57:48 +010099#define RCONF(x) (PCICONF(0)+(x))
100#define RPMCAP(x) (PMCAP(0)+(x))
101#define REXPCAP(x) (EXPCAP(0)+(x))
102#define RVCCAP(x) (VCCAP(0)+(x))
103
104#define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
105#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
106#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
107
Phil Edworthyb77188492014-06-30 08:54:23 +0100108#define RCAR_PCI_MAX_RESOURCES 4
Phil Edworthyc25da472014-05-12 11:57:48 +0100109#define MAX_NR_INBOUND_MAPS 6
110
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100111struct rcar_msi {
112 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
113 struct irq_domain *domain;
114 struct msi_chip chip;
115 unsigned long pages;
116 struct mutex lock;
117 int irq1;
118 int irq2;
119};
120
121static inline struct rcar_msi *to_rcar_msi(struct msi_chip *chip)
122{
123 return container_of(chip, struct rcar_msi, chip);
124}
125
Phil Edworthyc25da472014-05-12 11:57:48 +0100126/* Structure representing the PCIe interface */
127struct rcar_pcie {
128 struct device *dev;
129 void __iomem *base;
Phil Edworthyb77188492014-06-30 08:54:23 +0100130 struct resource res[RCAR_PCI_MAX_RESOURCES];
Phil Edworthyc25da472014-05-12 11:57:48 +0100131 struct resource busn;
132 int root_bus_nr;
133 struct clk *clk;
134 struct clk *bus_clk;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100135 struct rcar_msi msi;
Phil Edworthyc25da472014-05-12 11:57:48 +0100136};
137
138static inline struct rcar_pcie *sys_to_pcie(struct pci_sys_data *sys)
139{
140 return sys->private_data;
141}
142
Phil Edworthyb77188492014-06-30 08:54:23 +0100143static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
144 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100145{
146 writel(val, pcie->base + reg);
147}
148
Phil Edworthyb77188492014-06-30 08:54:23 +0100149static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
150 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100151{
152 return readl(pcie->base + reg);
153}
154
155enum {
Phil Edworthyb77188492014-06-30 08:54:23 +0100156 RCAR_PCI_ACCESS_READ,
157 RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100158};
159
160static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
161{
162 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100163 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100164
165 val &= ~(mask << shift);
166 val |= data << shift;
Phil Edworthyb77188492014-06-30 08:54:23 +0100167 rcar_pci_write_reg(pcie, val, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100168}
169
170static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
171{
172 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100173 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100174
175 return val >> shift;
176}
177
178/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
179static int rcar_pcie_config_access(struct rcar_pcie *pcie,
180 unsigned char access_type, struct pci_bus *bus,
181 unsigned int devfn, int where, u32 *data)
182{
183 int dev, func, reg, index;
184
185 dev = PCI_SLOT(devfn);
186 func = PCI_FUNC(devfn);
187 reg = where & ~3;
188 index = reg / 4;
189
190 /*
191 * While each channel has its own memory-mapped extended config
192 * space, it's generally only accessible when in endpoint mode.
193 * When in root complex mode, the controller is unable to target
194 * itself with either type 0 or type 1 accesses, and indeed, any
195 * controller initiated target transfer to its own config space
196 * result in a completer abort.
197 *
198 * Each channel effectively only supports a single device, but as
199 * the same channel <-> device access works for any PCI_SLOT()
200 * value, we cheat a bit here and bind the controller's config
201 * space to devfn 0 in order to enable self-enumeration. In this
202 * case the regular ECAR/ECDR path is sidelined and the mangled
203 * config access itself is initiated as an internal bus transaction.
204 */
205 if (pci_is_root_bus(bus)) {
206 if (dev != 0)
207 return PCIBIOS_DEVICE_NOT_FOUND;
208
Phil Edworthyb77188492014-06-30 08:54:23 +0100209 if (access_type == RCAR_PCI_ACCESS_READ) {
210 *data = rcar_pci_read_reg(pcie, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100211 } else {
212 /* Keep an eye out for changes to the root bus number */
213 if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
214 pcie->root_bus_nr = *data & 0xff;
215
Phil Edworthyb77188492014-06-30 08:54:23 +0100216 rcar_pci_write_reg(pcie, *data, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100217 }
218
219 return PCIBIOS_SUCCESSFUL;
220 }
221
222 if (pcie->root_bus_nr < 0)
223 return PCIBIOS_DEVICE_NOT_FOUND;
224
225 /* Clear errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100226 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100227
228 /* Set the PIO address */
Phil Edworthyb77188492014-06-30 08:54:23 +0100229 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
230 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100231
232 /* Enable the configuration access */
233 if (bus->parent->number == pcie->root_bus_nr)
Phil Edworthyb77188492014-06-30 08:54:23 +0100234 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100235 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100236 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100237
238 /* Check for errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100239 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
Phil Edworthyc25da472014-05-12 11:57:48 +0100240 return PCIBIOS_DEVICE_NOT_FOUND;
241
242 /* Check for master and target aborts */
243 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
244 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
245 return PCIBIOS_DEVICE_NOT_FOUND;
246
Phil Edworthyb77188492014-06-30 08:54:23 +0100247 if (access_type == RCAR_PCI_ACCESS_READ)
248 *data = rcar_pci_read_reg(pcie, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100249 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100250 rcar_pci_write_reg(pcie, *data, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100251
252 /* Disable the configuration access */
Phil Edworthyb77188492014-06-30 08:54:23 +0100253 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100254
255 return PCIBIOS_SUCCESSFUL;
256}
257
258static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
259 int where, int size, u32 *val)
260{
261 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
262 int ret;
263
Phil Edworthyb77188492014-06-30 08:54:23 +0100264 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100265 bus, devfn, where, val);
266 if (ret != PCIBIOS_SUCCESSFUL) {
267 *val = 0xffffffff;
268 return ret;
269 }
270
271 if (size == 1)
272 *val = (*val >> (8 * (where & 3))) & 0xff;
273 else if (size == 2)
274 *val = (*val >> (8 * (where & 2))) & 0xffff;
275
Ryan Desfosses227f0642014-04-18 20:13:50 -0400276 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
277 bus->number, devfn, where, size, (unsigned long)*val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100278
279 return ret;
280}
281
282/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
283static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
284 int where, int size, u32 val)
285{
286 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
287 int shift, ret;
288 u32 data;
289
Phil Edworthyb77188492014-06-30 08:54:23 +0100290 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100291 bus, devfn, where, &data);
292 if (ret != PCIBIOS_SUCCESSFUL)
293 return ret;
294
Ryan Desfosses227f0642014-04-18 20:13:50 -0400295 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
296 bus->number, devfn, where, size, (unsigned long)val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100297
298 if (size == 1) {
299 shift = 8 * (where & 3);
300 data &= ~(0xff << shift);
301 data |= ((val & 0xff) << shift);
302 } else if (size == 2) {
303 shift = 8 * (where & 2);
304 data &= ~(0xffff << shift);
305 data |= ((val & 0xffff) << shift);
306 } else
307 data = val;
308
Phil Edworthyb77188492014-06-30 08:54:23 +0100309 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100310 bus, devfn, where, &data);
311
312 return ret;
313}
314
315static struct pci_ops rcar_pcie_ops = {
316 .read = rcar_pcie_read_conf,
317 .write = rcar_pcie_write_conf,
318};
319
320static void rcar_pcie_setup_window(int win, struct resource *res,
321 struct rcar_pcie *pcie)
322{
323 /* Setup PCIe address space mappings for each resource */
324 resource_size_t size;
325 u32 mask;
326
Phil Edworthyb77188492014-06-30 08:54:23 +0100327 rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100328
329 /*
330 * The PAMR mask is calculated in units of 128Bytes, which
331 * keeps things pretty simple.
332 */
333 size = resource_size(res);
334 mask = (roundup_pow_of_two(size) / SZ_128) - 1;
Phil Edworthyb77188492014-06-30 08:54:23 +0100335 rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100336
Phil Edworthyb77188492014-06-30 08:54:23 +0100337 rcar_pci_write_reg(pcie, upper_32_bits(res->start), PCIEPARH(win));
338 rcar_pci_write_reg(pcie, lower_32_bits(res->start), PCIEPARL(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100339
340 /* First resource is for IO */
341 mask = PAR_ENABLE;
342 if (res->flags & IORESOURCE_IO)
343 mask |= IO_SPACE;
344
Phil Edworthyb77188492014-06-30 08:54:23 +0100345 rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100346}
347
348static int rcar_pcie_setup(int nr, struct pci_sys_data *sys)
349{
350 struct rcar_pcie *pcie = sys_to_pcie(sys);
351 struct resource *res;
352 int i;
353
354 pcie->root_bus_nr = -1;
355
356 /* Setup PCI resources */
Phil Edworthyb77188492014-06-30 08:54:23 +0100357 for (i = 0; i < RCAR_PCI_MAX_RESOURCES; i++) {
Phil Edworthyc25da472014-05-12 11:57:48 +0100358
359 res = &pcie->res[i];
360 if (!res->flags)
361 continue;
362
363 rcar_pcie_setup_window(i, res, pcie);
364
365 if (res->flags & IORESOURCE_IO)
366 pci_ioremap_io(nr * SZ_64K, res->start);
367 else
368 pci_add_resource(&sys->resources, res);
369 }
370 pci_add_resource(&sys->resources, &pcie->busn);
371
372 return 1;
373}
374
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100375static void rcar_pcie_add_bus(struct pci_bus *bus)
376{
377 if (IS_ENABLED(CONFIG_PCI_MSI)) {
378 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
379
380 bus->msi = &pcie->msi.chip;
381 }
382}
383
Phil Edworthyc25da472014-05-12 11:57:48 +0100384struct hw_pci rcar_pci = {
385 .setup = rcar_pcie_setup,
386 .map_irq = of_irq_parse_and_map_pci,
387 .ops = &rcar_pcie_ops,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100388 .add_bus = rcar_pcie_add_bus,
Phil Edworthyc25da472014-05-12 11:57:48 +0100389};
390
391static void rcar_pcie_enable(struct rcar_pcie *pcie)
392{
393 struct platform_device *pdev = to_platform_device(pcie->dev);
394
395 rcar_pci.nr_controllers = 1;
396 rcar_pci.private_data = (void **)&pcie;
397
398 pci_common_init_dev(&pdev->dev, &rcar_pci);
399#ifdef CONFIG_PCI_DOMAINS
400 rcar_pci.domain++;
401#endif
402}
403
404static int phy_wait_for_ack(struct rcar_pcie *pcie)
405{
406 unsigned int timeout = 100;
407
408 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100409 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
Phil Edworthyc25da472014-05-12 11:57:48 +0100410 return 0;
411
412 udelay(100);
413 }
414
415 dev_err(pcie->dev, "Access to PCIe phy timed out\n");
416
417 return -ETIMEDOUT;
418}
419
420static void phy_write_reg(struct rcar_pcie *pcie,
421 unsigned int rate, unsigned int addr,
422 unsigned int lane, unsigned int data)
423{
424 unsigned long phyaddr;
425
426 phyaddr = WRITE_CMD |
427 ((rate & 1) << RATE_POS) |
428 ((lane & 0xf) << LANE_POS) |
429 ((addr & 0xff) << ADR_POS);
430
431 /* Set write data */
Phil Edworthyb77188492014-06-30 08:54:23 +0100432 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
433 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100434
435 /* Ignore errors as they will be dealt with if the data link is down */
436 phy_wait_for_ack(pcie);
437
438 /* Clear command */
Phil Edworthyb77188492014-06-30 08:54:23 +0100439 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
440 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100441
442 /* Ignore errors as they will be dealt with if the data link is down */
443 phy_wait_for_ack(pcie);
444}
445
446static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
447{
448 unsigned int timeout = 10;
449
450 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100451 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
Phil Edworthyc25da472014-05-12 11:57:48 +0100452 return 0;
453
454 msleep(5);
455 }
456
457 return -ETIMEDOUT;
458}
459
460static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
461{
462 int err;
463
464 /* Begin initialization */
Phil Edworthyb77188492014-06-30 08:54:23 +0100465 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100466
467 /* Set mode */
Phil Edworthyb77188492014-06-30 08:54:23 +0100468 rcar_pci_write_reg(pcie, 1, PCIEMSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100469
470 /*
471 * Initial header for port config space is type 1, set the device
472 * class to match. Hardware takes care of propagating the IDSETR
473 * settings, so there is no need to bother with a quirk.
474 */
Phil Edworthyb77188492014-06-30 08:54:23 +0100475 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
Phil Edworthyc25da472014-05-12 11:57:48 +0100476
477 /*
478 * Setup Secondary Bus Number & Subordinate Bus Number, even though
479 * they aren't used, to avoid bridge being detected as broken.
480 */
481 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
482 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
483
484 /* Initialize default capabilities. */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100485 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
Phil Edworthyc25da472014-05-12 11:57:48 +0100486 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
487 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
488 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
489 PCI_HEADER_TYPE_BRIDGE);
490
491 /* Enable data link layer active state reporting */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100492 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
493 PCI_EXP_LNKCAP_DLLLARC);
Phil Edworthyc25da472014-05-12 11:57:48 +0100494
495 /* Write out the physical slot number = 0 */
496 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
497
498 /* Set the completion timer timeout to the maximum 50ms. */
Phil Edworthyb77188492014-06-30 08:54:23 +0100499 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
Phil Edworthyc25da472014-05-12 11:57:48 +0100500
501 /* Terminate list of capabilities (Next Capability Offset=0) */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100502 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
Phil Edworthyc25da472014-05-12 11:57:48 +0100503
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100504 /* Enable MSI */
505 if (IS_ENABLED(CONFIG_PCI_MSI))
Phil Edworthyb77188492014-06-30 08:54:23 +0100506 rcar_pci_write_reg(pcie, 0x101f0000, PCIEMSITXR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100507
Phil Edworthyc25da472014-05-12 11:57:48 +0100508 /* Finish initialization - establish a PCI Express link */
Phil Edworthyb77188492014-06-30 08:54:23 +0100509 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100510
511 /* This will timeout if we don't have a link. */
512 err = rcar_pcie_wait_for_dl(pcie);
513 if (err)
514 return err;
515
516 /* Enable INTx interrupts */
517 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
518
Phil Edworthyc25da472014-05-12 11:57:48 +0100519 wmb();
520
521 return 0;
522}
523
524static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
525{
526 unsigned int timeout = 10;
527
528 /* Initialize the phy */
529 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
530 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
531 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
532 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
533 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
534 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
535 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
536 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
537 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
538 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
539 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
540 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
541
542 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
543 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
544 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
545
546 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100547 if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
Phil Edworthyc25da472014-05-12 11:57:48 +0100548 return rcar_pcie_hw_init(pcie);
549
550 msleep(5);
551 }
552
553 return -ETIMEDOUT;
554}
555
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100556static int rcar_msi_alloc(struct rcar_msi *chip)
557{
558 int msi;
559
560 mutex_lock(&chip->lock);
561
562 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
563 if (msi < INT_PCI_MSI_NR)
564 set_bit(msi, chip->used);
565 else
566 msi = -ENOSPC;
567
568 mutex_unlock(&chip->lock);
569
570 return msi;
571}
572
573static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
574{
575 mutex_lock(&chip->lock);
576 clear_bit(irq, chip->used);
577 mutex_unlock(&chip->lock);
578}
579
580static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
581{
582 struct rcar_pcie *pcie = data;
583 struct rcar_msi *msi = &pcie->msi;
584 unsigned long reg;
585
Phil Edworthyb77188492014-06-30 08:54:23 +0100586 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100587
588 /* MSI & INTx share an interrupt - we only handle MSI here */
589 if (!reg)
590 return IRQ_NONE;
591
592 while (reg) {
593 unsigned int index = find_first_bit(&reg, 32);
594 unsigned int irq;
595
596 /* clear the interrupt */
Phil Edworthyb77188492014-06-30 08:54:23 +0100597 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100598
599 irq = irq_find_mapping(msi->domain, index);
600 if (irq) {
601 if (test_bit(index, msi->used))
602 generic_handle_irq(irq);
603 else
604 dev_info(pcie->dev, "unhandled MSI\n");
605 } else {
606 /* Unknown MSI, just clear it */
607 dev_dbg(pcie->dev, "unexpected MSI\n");
608 }
609
610 /* see if there's any more pending in this vector */
Phil Edworthyb77188492014-06-30 08:54:23 +0100611 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100612 }
613
614 return IRQ_HANDLED;
615}
616
617static int rcar_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
618 struct msi_desc *desc)
619{
620 struct rcar_msi *msi = to_rcar_msi(chip);
621 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
622 struct msi_msg msg;
623 unsigned int irq;
624 int hwirq;
625
626 hwirq = rcar_msi_alloc(msi);
627 if (hwirq < 0)
628 return hwirq;
629
630 irq = irq_create_mapping(msi->domain, hwirq);
631 if (!irq) {
632 rcar_msi_free(msi, hwirq);
633 return -EINVAL;
634 }
635
636 irq_set_msi_desc(irq, desc);
637
Phil Edworthyb77188492014-06-30 08:54:23 +0100638 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
639 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100640 msg.data = hwirq;
641
642 write_msi_msg(irq, &msg);
643
644 return 0;
645}
646
647static void rcar_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
648{
649 struct rcar_msi *msi = to_rcar_msi(chip);
650 struct irq_data *d = irq_get_irq_data(irq);
651
652 rcar_msi_free(msi, d->hwirq);
653}
654
655static struct irq_chip rcar_msi_irq_chip = {
656 .name = "R-Car PCIe MSI",
657 .irq_enable = unmask_msi_irq,
658 .irq_disable = mask_msi_irq,
659 .irq_mask = mask_msi_irq,
660 .irq_unmask = unmask_msi_irq,
661};
662
663static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
664 irq_hw_number_t hwirq)
665{
666 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
667 irq_set_chip_data(irq, domain->host_data);
668 set_irq_flags(irq, IRQF_VALID);
669
670 return 0;
671}
672
673static const struct irq_domain_ops msi_domain_ops = {
674 .map = rcar_msi_map,
675};
676
677static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
678{
679 struct platform_device *pdev = to_platform_device(pcie->dev);
680 struct rcar_msi *msi = &pcie->msi;
681 unsigned long base;
682 int err;
683
684 mutex_init(&msi->lock);
685
686 msi->chip.dev = pcie->dev;
687 msi->chip.setup_irq = rcar_msi_setup_irq;
688 msi->chip.teardown_irq = rcar_msi_teardown_irq;
689
690 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
691 &msi_domain_ops, &msi->chip);
692 if (!msi->domain) {
693 dev_err(&pdev->dev, "failed to create IRQ domain\n");
694 return -ENOMEM;
695 }
696
697 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
698 err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
699 IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
700 if (err < 0) {
701 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
702 goto err;
703 }
704
705 err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
706 IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
707 if (err < 0) {
708 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
709 goto err;
710 }
711
712 /* setup MSI data target */
713 msi->pages = __get_free_pages(GFP_KERNEL, 0);
714 base = virt_to_phys((void *)msi->pages);
715
Phil Edworthyb77188492014-06-30 08:54:23 +0100716 rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
717 rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100718
719 /* enable all MSI interrupts */
Phil Edworthyb77188492014-06-30 08:54:23 +0100720 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100721
722 return 0;
723
724err:
725 irq_domain_remove(msi->domain);
726 return err;
727}
728
Phil Edworthyc25da472014-05-12 11:57:48 +0100729static int rcar_pcie_get_resources(struct platform_device *pdev,
730 struct rcar_pcie *pcie)
731{
732 struct resource res;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100733 int err, i;
Phil Edworthyc25da472014-05-12 11:57:48 +0100734
735 err = of_address_to_resource(pdev->dev.of_node, 0, &res);
736 if (err)
737 return err;
738
739 pcie->clk = devm_clk_get(&pdev->dev, "pcie");
740 if (IS_ERR(pcie->clk)) {
741 dev_err(pcie->dev, "cannot get platform clock\n");
742 return PTR_ERR(pcie->clk);
743 }
744 err = clk_prepare_enable(pcie->clk);
745 if (err)
746 goto fail_clk;
747
748 pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
749 if (IS_ERR(pcie->bus_clk)) {
750 dev_err(pcie->dev, "cannot get pcie bus clock\n");
751 err = PTR_ERR(pcie->bus_clk);
752 goto fail_clk;
753 }
754 err = clk_prepare_enable(pcie->bus_clk);
755 if (err)
756 goto err_map_reg;
757
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100758 i = irq_of_parse_and_map(pdev->dev.of_node, 0);
759 if (i < 0) {
760 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
761 err = -ENOENT;
762 goto err_map_reg;
763 }
764 pcie->msi.irq1 = i;
765
766 i = irq_of_parse_and_map(pdev->dev.of_node, 1);
767 if (i < 0) {
768 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
769 err = -ENOENT;
770 goto err_map_reg;
771 }
772 pcie->msi.irq2 = i;
773
Phil Edworthyc25da472014-05-12 11:57:48 +0100774 pcie->base = devm_ioremap_resource(&pdev->dev, &res);
775 if (IS_ERR(pcie->base)) {
776 err = PTR_ERR(pcie->base);
777 goto err_map_reg;
778 }
779
780 return 0;
781
782err_map_reg:
783 clk_disable_unprepare(pcie->bus_clk);
784fail_clk:
785 clk_disable_unprepare(pcie->clk);
786
787 return err;
788}
789
790static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
791 struct of_pci_range *range,
792 int *index)
793{
794 u64 restype = range->flags;
795 u64 cpu_addr = range->cpu_addr;
796 u64 cpu_end = range->cpu_addr + range->size;
797 u64 pci_addr = range->pci_addr;
798 u32 flags = LAM_64BIT | LAR_ENABLE;
799 u64 mask;
800 u64 size;
801 int idx = *index;
802
803 if (restype & IORESOURCE_PREFETCH)
804 flags |= LAM_PREFETCH;
805
806 /*
807 * If the size of the range is larger than the alignment of the start
808 * address, we have to use multiple entries to perform the mapping.
809 */
810 if (cpu_addr > 0) {
811 unsigned long nr_zeros = __ffs64(cpu_addr);
812 u64 alignment = 1ULL << nr_zeros;
Phil Edworthyb77188492014-06-30 08:54:23 +0100813
Phil Edworthyc25da472014-05-12 11:57:48 +0100814 size = min(range->size, alignment);
815 } else {
816 size = range->size;
817 }
818 /* Hardware supports max 4GiB inbound region */
819 size = min(size, 1ULL << 32);
820
821 mask = roundup_pow_of_two(size) - 1;
822 mask &= ~0xf;
823
824 while (cpu_addr < cpu_end) {
825 /*
826 * Set up 64-bit inbound regions as the range parser doesn't
827 * distinguish between 32 and 64-bit types.
828 */
Phil Edworthyb77188492014-06-30 08:54:23 +0100829 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
830 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
831 rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
Phil Edworthyc25da472014-05-12 11:57:48 +0100832
Phil Edworthyb77188492014-06-30 08:54:23 +0100833 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
834 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
835 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
Phil Edworthyc25da472014-05-12 11:57:48 +0100836
837 pci_addr += size;
838 cpu_addr += size;
839 idx += 2;
840
841 if (idx > MAX_NR_INBOUND_MAPS) {
842 dev_err(pcie->dev, "Failed to map inbound regions!\n");
843 return -EINVAL;
844 }
845 }
846 *index = idx;
847
848 return 0;
849}
850
851static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
852 struct device_node *node)
853{
854 const int na = 3, ns = 2;
855 int rlen;
856
857 parser->node = node;
858 parser->pna = of_n_addr_cells(node);
859 parser->np = parser->pna + na + ns;
860
861 parser->range = of_get_property(node, "dma-ranges", &rlen);
862 if (!parser->range)
863 return -ENOENT;
864
865 parser->end = parser->range + rlen / sizeof(__be32);
866 return 0;
867}
868
869static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
870 struct device_node *np)
871{
872 struct of_pci_range range;
873 struct of_pci_range_parser parser;
874 int index = 0;
875 int err;
876
877 if (pci_dma_range_parser_init(&parser, np))
878 return -EINVAL;
879
880 /* Get the dma-ranges from DT */
881 for_each_of_pci_range(&parser, &range) {
882 u64 end = range.cpu_addr + range.size - 1;
883 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
884 range.flags, range.cpu_addr, end, range.pci_addr);
885
886 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
887 if (err)
888 return err;
889 }
890
891 return 0;
892}
893
894static const struct of_device_id rcar_pcie_of_match[] = {
895 { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
896 { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init },
897 { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init },
898 {},
899};
900MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
901
902static int rcar_pcie_probe(struct platform_device *pdev)
903{
904 struct rcar_pcie *pcie;
905 unsigned int data;
906 struct of_pci_range range;
907 struct of_pci_range_parser parser;
908 const struct of_device_id *of_id;
909 int err, win = 0;
910 int (*hw_init_fn)(struct rcar_pcie *);
911
912 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
913 if (!pcie)
914 return -ENOMEM;
915
916 pcie->dev = &pdev->dev;
917 platform_set_drvdata(pdev, pcie);
918
919 /* Get the bus range */
920 if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) {
921 dev_err(&pdev->dev, "failed to parse bus-range property\n");
922 return -EINVAL;
923 }
924
925 if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) {
926 dev_err(&pdev->dev, "missing ranges property\n");
927 return -EINVAL;
928 }
929
930 err = rcar_pcie_get_resources(pdev, pcie);
931 if (err < 0) {
932 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
933 return err;
934 }
935
936 for_each_of_pci_range(&parser, &range) {
937 of_pci_range_to_resource(&range, pdev->dev.of_node,
938 &pcie->res[win++]);
939
Phil Edworthyb77188492014-06-30 08:54:23 +0100940 if (win > RCAR_PCI_MAX_RESOURCES)
Phil Edworthyc25da472014-05-12 11:57:48 +0100941 break;
942 }
943
944 err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
945 if (err)
946 return err;
947
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100948 if (IS_ENABLED(CONFIG_PCI_MSI)) {
949 err = rcar_pcie_enable_msi(pcie);
950 if (err < 0) {
951 dev_err(&pdev->dev,
952 "failed to enable MSI support: %d\n",
953 err);
954 return err;
955 }
956 }
957
Phil Edworthyc25da472014-05-12 11:57:48 +0100958 of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
959 if (!of_id || !of_id->data)
960 return -EINVAL;
961 hw_init_fn = of_id->data;
962
963 /* Failure to get a link might just be that no cards are inserted */
964 err = hw_init_fn(pcie);
965 if (err) {
966 dev_info(&pdev->dev, "PCIe link down\n");
967 return 0;
968 }
969
Phil Edworthyb77188492014-06-30 08:54:23 +0100970 data = rcar_pci_read_reg(pcie, MACSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100971 dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
972
973 rcar_pcie_enable(pcie);
974
975 return 0;
976}
977
978static struct platform_driver rcar_pcie_driver = {
979 .driver = {
980 .name = DRV_NAME,
981 .owner = THIS_MODULE,
982 .of_match_table = rcar_pcie_of_match,
983 .suppress_bind_attrs = true,
984 },
985 .probe = rcar_pcie_probe,
986};
987module_platform_driver(rcar_pcie_driver);
988
989MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
990MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
991MODULE_LICENSE("GPLv2");