Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / pci / host / pcie-rcar.c
1 /*
2  * PCIe driver for Renesas R-Car SoCs
3  *  Copyright (C) 2014 Renesas Electronics Europe Ltd
4  *
5  * Based on:
6  *  arch/sh/drivers/pci/pcie-sh7786.c
7  *  arch/sh/drivers/pci/ops-sh7786.c
8  *  Copyright (C) 2009 - 2011  Paul Mundt
9  *
10  * This file is licensed under the terms of the GNU General Public
11  * License version 2.  This program is licensed "as is" without any
12  * warranty of any kind, whether express or implied.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/msi.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_pci.h>
26 #include <linux/of_platform.h>
27 #include <linux/pci.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30
31 #define DRV_NAME "rcar-pcie"
32
33 #define PCIECAR                 0x000010
34 #define PCIECCTLR               0x000018
35 #define  CONFIG_SEND_ENABLE     (1 << 31)
36 #define  TYPE0                  (0 << 8)
37 #define  TYPE1                  (1 << 8)
38 #define PCIECDR                 0x000020
39 #define PCIEMSR                 0x000028
40 #define PCIEINTXR               0x000400
41 #define PCIEMSITXR              0x000840
42
43 /* Transfer control */
44 #define PCIETCTLR               0x02000
45 #define  CFINIT                 1
46 #define PCIETSTR                0x02004
47 #define  DATA_LINK_ACTIVE       1
48 #define PCIEERRFR               0x02020
49 #define  UNSUPPORTED_REQUEST    (1 << 4)
50 #define PCIEMSIFR               0x02044
51 #define PCIEMSIALR              0x02048
52 #define  MSIFE                  1
53 #define PCIEMSIAUR              0x0204c
54 #define PCIEMSIIER              0x02050
55
56 /* root port address */
57 #define PCIEPRAR(x)             (0x02080 + ((x) * 0x4))
58
59 /* local address reg & mask */
60 #define PCIELAR(x)              (0x02200 + ((x) * 0x20))
61 #define PCIELAMR(x)             (0x02208 + ((x) * 0x20))
62 #define  LAM_PREFETCH           (1 << 3)
63 #define  LAM_64BIT              (1 << 2)
64 #define  LAR_ENABLE             (1 << 1)
65
66 /* PCIe address reg & mask */
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
97 #define INT_PCI_MSI_NR  32
98
99 #define RCONF(x)        (PCICONF(0)+(x))
100 #define RPMCAP(x)       (PMCAP(0)+(x))
101 #define REXPCAP(x)      (EXPCAP(0)+(x))
102 #define RVCCAP(x)       (VCCAP(0)+(x))
103
104 #define  PCIE_CONF_BUS(b)       (((b) & 0xff) << 24)
105 #define  PCIE_CONF_DEV(d)       (((d) & 0x1f) << 19)
106 #define  PCIE_CONF_FUNC(f)      (((f) & 0x7) << 16)
107
108 #define RCAR_PCI_MAX_RESOURCES 4
109 #define MAX_NR_INBOUND_MAPS 6
110
111 struct 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
121 static inline struct rcar_msi *to_rcar_msi(struct msi_chip *chip)
122 {
123         return container_of(chip, struct rcar_msi, chip);
124 }
125
126 /* Structure representing the PCIe interface */
127 struct rcar_pcie {
128         struct device           *dev;
129         void __iomem            *base;
130         struct resource         res[RCAR_PCI_MAX_RESOURCES];
131         struct resource         busn;
132         int                     root_bus_nr;
133         struct clk              *clk;
134         struct clk              *bus_clk;
135         struct                  rcar_msi msi;
136 };
137
138 static inline struct rcar_pcie *sys_to_pcie(struct pci_sys_data *sys)
139 {
140         return sys->private_data;
141 }
142
143 static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
144                                unsigned long reg)
145 {
146         writel(val, pcie->base + reg);
147 }
148
149 static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
150                                        unsigned long reg)
151 {
152         return readl(pcie->base + reg);
153 }
154
155 enum {
156         RCAR_PCI_ACCESS_READ,
157         RCAR_PCI_ACCESS_WRITE,
158 };
159
160 static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
161 {
162         int shift = 8 * (where & 3);
163         u32 val = rcar_pci_read_reg(pcie, where & ~3);
164
165         val &= ~(mask << shift);
166         val |= data << shift;
167         rcar_pci_write_reg(pcie, val, where & ~3);
168 }
169
170 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
171 {
172         int shift = 8 * (where & 3);
173         u32 val = rcar_pci_read_reg(pcie, where & ~3);
174
175         return val >> shift;
176 }
177
178 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
179 static 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
209                 if (access_type == RCAR_PCI_ACCESS_READ) {
210                         *data = rcar_pci_read_reg(pcie, PCICONF(index));
211                 } 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
216                         rcar_pci_write_reg(pcie, *data, PCICONF(index));
217                 }
218
219                 return PCIBIOS_SUCCESSFUL;
220         }
221
222         if (pcie->root_bus_nr < 0)
223                 return PCIBIOS_DEVICE_NOT_FOUND;
224
225         /* Clear errors */
226         rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
227
228         /* Set the PIO address */
229         rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
230                 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
231
232         /* Enable the configuration access */
233         if (bus->parent->number == pcie->root_bus_nr)
234                 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
235         else
236                 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
237
238         /* Check for errors */
239         if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
240                 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
247         if (access_type == RCAR_PCI_ACCESS_READ)
248                 *data = rcar_pci_read_reg(pcie, PCIECDR);
249         else
250                 rcar_pci_write_reg(pcie, *data, PCIECDR);
251
252         /* Disable the configuration access */
253         rcar_pci_write_reg(pcie, 0, PCIECCTLR);
254
255         return PCIBIOS_SUCCESSFUL;
256 }
257
258 static 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
264         ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
265                                       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
276         dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
277                 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
278                 devfn, where, size, (unsigned long)*val);
279
280         return ret;
281 }
282
283 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
284 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
285                                 int where, int size, u32 val)
286 {
287         struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
288         int shift, ret;
289         u32 data;
290
291         ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
292                                       bus, devfn, where, &data);
293         if (ret != PCIBIOS_SUCCESSFUL)
294                 return ret;
295
296         dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
297                 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
298                 devfn, where, size, (unsigned long)val);
299
300         if (size == 1) {
301                 shift = 8 * (where & 3);
302                 data &= ~(0xff << shift);
303                 data |= ((val & 0xff) << shift);
304         } else if (size == 2) {
305                 shift = 8 * (where & 2);
306                 data &= ~(0xffff << shift);
307                 data |= ((val & 0xffff) << shift);
308         } else
309                 data = val;
310
311         ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
312                                       bus, devfn, where, &data);
313
314         return ret;
315 }
316
317 static struct pci_ops rcar_pcie_ops = {
318         .read   = rcar_pcie_read_conf,
319         .write  = rcar_pcie_write_conf,
320 };
321
322 static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie)
323 {
324         struct resource *res = &pcie->res[win];
325
326         /* Setup PCIe address space mappings for each resource */
327         resource_size_t size;
328         u32 mask;
329
330         rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
331
332         /*
333          * The PAMR mask is calculated in units of 128Bytes, which
334          * keeps things pretty simple.
335          */
336         size = resource_size(res);
337         mask = (roundup_pow_of_two(size) / SZ_128) - 1;
338         rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
339
340         rcar_pci_write_reg(pcie, upper_32_bits(res->start), PCIEPARH(win));
341         rcar_pci_write_reg(pcie, lower_32_bits(res->start), PCIEPARL(win));
342
343         /* First resource is for IO */
344         mask = PAR_ENABLE;
345         if (res->flags & IORESOURCE_IO)
346                 mask |= IO_SPACE;
347
348         rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
349 }
350
351 static int rcar_pcie_setup(int nr, struct pci_sys_data *sys)
352 {
353         struct rcar_pcie *pcie = sys_to_pcie(sys);
354         struct resource *res;
355         int i;
356
357         pcie->root_bus_nr = -1;
358
359         /* Setup PCI resources */
360         for (i = 0; i < RCAR_PCI_MAX_RESOURCES; i++) {
361
362                 res = &pcie->res[i];
363                 if (!res->flags)
364                         continue;
365
366                 rcar_pcie_setup_window(i, pcie);
367
368                 if (res->flags & IORESOURCE_IO)
369                         pci_ioremap_io(nr * SZ_64K, res->start);
370                 else
371                         pci_add_resource(&sys->resources, res);
372         }
373         pci_add_resource(&sys->resources, &pcie->busn);
374
375         return 1;
376 }
377
378 static void rcar_pcie_add_bus(struct pci_bus *bus)
379 {
380         if (IS_ENABLED(CONFIG_PCI_MSI)) {
381                 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
382
383                 bus->msi = &pcie->msi.chip;
384         }
385 }
386
387 struct hw_pci rcar_pci = {
388         .setup          = rcar_pcie_setup,
389         .map_irq        = of_irq_parse_and_map_pci,
390         .ops            = &rcar_pcie_ops,
391         .add_bus        = rcar_pcie_add_bus,
392 };
393
394 static void rcar_pcie_enable(struct rcar_pcie *pcie)
395 {
396         struct platform_device *pdev = to_platform_device(pcie->dev);
397
398         rcar_pci.nr_controllers = 1;
399         rcar_pci.private_data = (void **)&pcie;
400
401         pci_common_init_dev(&pdev->dev, &rcar_pci);
402 #ifdef CONFIG_PCI_DOMAINS
403         rcar_pci.domain++;
404 #endif
405 }
406
407 static int phy_wait_for_ack(struct rcar_pcie *pcie)
408 {
409         unsigned int timeout = 100;
410
411         while (timeout--) {
412                 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
413                         return 0;
414
415                 udelay(100);
416         }
417
418         dev_err(pcie->dev, "Access to PCIe phy timed out\n");
419
420         return -ETIMEDOUT;
421 }
422
423 static void phy_write_reg(struct rcar_pcie *pcie,
424                                  unsigned int rate, unsigned int addr,
425                                  unsigned int lane, unsigned int data)
426 {
427         unsigned long phyaddr;
428
429         phyaddr = WRITE_CMD |
430                 ((rate & 1) << RATE_POS) |
431                 ((lane & 0xf) << LANE_POS) |
432                 ((addr & 0xff) << ADR_POS);
433
434         /* Set write data */
435         rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
436         rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
437
438         /* Ignore errors as they will be dealt with if the data link is down */
439         phy_wait_for_ack(pcie);
440
441         /* Clear command */
442         rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
443         rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
444
445         /* Ignore errors as they will be dealt with if the data link is down */
446         phy_wait_for_ack(pcie);
447 }
448
449 static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
450 {
451         unsigned int timeout = 10;
452
453         while (timeout--) {
454                 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
455                         return 0;
456
457                 msleep(5);
458         }
459
460         return -ETIMEDOUT;
461 }
462
463 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
464 {
465         int err;
466
467         /* Begin initialization */
468         rcar_pci_write_reg(pcie, 0, PCIETCTLR);
469
470         /* Set mode */
471         rcar_pci_write_reg(pcie, 1, PCIEMSR);
472
473         /*
474          * Initial header for port config space is type 1, set the device
475          * class to match. Hardware takes care of propagating the IDSETR
476          * settings, so there is no need to bother with a quirk.
477          */
478         rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
479
480         /*
481          * Setup Secondary Bus Number & Subordinate Bus Number, even though
482          * they aren't used, to avoid bridge being detected as broken.
483          */
484         rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
485         rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
486
487         /* Initialize default capabilities. */
488         rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
489         rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
490                 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
491         rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
492                 PCI_HEADER_TYPE_BRIDGE);
493
494         /* Enable data link layer active state reporting */
495         rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
496                 PCI_EXP_LNKCAP_DLLLARC);
497
498         /* Write out the physical slot number = 0 */
499         rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
500
501         /* Set the completion timer timeout to the maximum 50ms. */
502         rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
503
504         /* Terminate list of capabilities (Next Capability Offset=0) */
505         rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
506
507         /* Enable MSI */
508         if (IS_ENABLED(CONFIG_PCI_MSI))
509                 rcar_pci_write_reg(pcie, 0x101f0000, PCIEMSITXR);
510
511         /* Finish initialization - establish a PCI Express link */
512         rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
513
514         /* This will timeout if we don't have a link. */
515         err = rcar_pcie_wait_for_dl(pcie);
516         if (err)
517                 return err;
518
519         /* Enable INTx interrupts */
520         rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
521
522         wmb();
523
524         return 0;
525 }
526
527 static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
528 {
529         unsigned int timeout = 10;
530
531         /* Initialize the phy */
532         phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
533         phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
534         phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
535         phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
536         phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
537         phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
538         phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
539         phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
540         phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
541         phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
542         phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
543         phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
544
545         phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
546         phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
547         phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
548
549         while (timeout--) {
550                 if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
551                         return rcar_pcie_hw_init(pcie);
552
553                 msleep(5);
554         }
555
556         return -ETIMEDOUT;
557 }
558
559 static int rcar_msi_alloc(struct rcar_msi *chip)
560 {
561         int msi;
562
563         mutex_lock(&chip->lock);
564
565         msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
566         if (msi < INT_PCI_MSI_NR)
567                 set_bit(msi, chip->used);
568         else
569                 msi = -ENOSPC;
570
571         mutex_unlock(&chip->lock);
572
573         return msi;
574 }
575
576 static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
577 {
578         mutex_lock(&chip->lock);
579         clear_bit(irq, chip->used);
580         mutex_unlock(&chip->lock);
581 }
582
583 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
584 {
585         struct rcar_pcie *pcie = data;
586         struct rcar_msi *msi = &pcie->msi;
587         unsigned long reg;
588
589         reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
590
591         /* MSI & INTx share an interrupt - we only handle MSI here */
592         if (!reg)
593                 return IRQ_NONE;
594
595         while (reg) {
596                 unsigned int index = find_first_bit(&reg, 32);
597                 unsigned int irq;
598
599                 /* clear the interrupt */
600                 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
601
602                 irq = irq_find_mapping(msi->domain, index);
603                 if (irq) {
604                         if (test_bit(index, msi->used))
605                                 generic_handle_irq(irq);
606                         else
607                                 dev_info(pcie->dev, "unhandled MSI\n");
608                 } else {
609                         /* Unknown MSI, just clear it */
610                         dev_dbg(pcie->dev, "unexpected MSI\n");
611                 }
612
613                 /* see if there's any more pending in this vector */
614                 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
615         }
616
617         return IRQ_HANDLED;
618 }
619
620 static int rcar_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
621                               struct msi_desc *desc)
622 {
623         struct rcar_msi *msi = to_rcar_msi(chip);
624         struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
625         struct msi_msg msg;
626         unsigned int irq;
627         int hwirq;
628
629         hwirq = rcar_msi_alloc(msi);
630         if (hwirq < 0)
631                 return hwirq;
632
633         irq = irq_create_mapping(msi->domain, hwirq);
634         if (!irq) {
635                 rcar_msi_free(msi, hwirq);
636                 return -EINVAL;
637         }
638
639         irq_set_msi_desc(irq, desc);
640
641         msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
642         msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
643         msg.data = hwirq;
644
645         write_msi_msg(irq, &msg);
646
647         return 0;
648 }
649
650 static void rcar_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
651 {
652         struct rcar_msi *msi = to_rcar_msi(chip);
653         struct irq_data *d = irq_get_irq_data(irq);
654
655         rcar_msi_free(msi, d->hwirq);
656 }
657
658 static struct irq_chip rcar_msi_irq_chip = {
659         .name = "R-Car PCIe MSI",
660         .irq_enable = unmask_msi_irq,
661         .irq_disable = mask_msi_irq,
662         .irq_mask = mask_msi_irq,
663         .irq_unmask = unmask_msi_irq,
664 };
665
666 static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
667                         irq_hw_number_t hwirq)
668 {
669         irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
670         irq_set_chip_data(irq, domain->host_data);
671         set_irq_flags(irq, IRQF_VALID);
672
673         return 0;
674 }
675
676 static const struct irq_domain_ops msi_domain_ops = {
677         .map = rcar_msi_map,
678 };
679
680 static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
681 {
682         struct platform_device *pdev = to_platform_device(pcie->dev);
683         struct rcar_msi *msi = &pcie->msi;
684         unsigned long base;
685         int err;
686
687         mutex_init(&msi->lock);
688
689         msi->chip.dev = pcie->dev;
690         msi->chip.setup_irq = rcar_msi_setup_irq;
691         msi->chip.teardown_irq = rcar_msi_teardown_irq;
692
693         msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
694                                             &msi_domain_ops, &msi->chip);
695         if (!msi->domain) {
696                 dev_err(&pdev->dev, "failed to create IRQ domain\n");
697                 return -ENOMEM;
698         }
699
700         /* Two irqs are for MSI, but they are also used for non-MSI irqs */
701         err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
702                                IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
703         if (err < 0) {
704                 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
705                 goto err;
706         }
707
708         err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
709                                IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
710         if (err < 0) {
711                 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
712                 goto err;
713         }
714
715         /* setup MSI data target */
716         msi->pages = __get_free_pages(GFP_KERNEL, 0);
717         base = virt_to_phys((void *)msi->pages);
718
719         rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
720         rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
721
722         /* enable all MSI interrupts */
723         rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
724
725         return 0;
726
727 err:
728         irq_domain_remove(msi->domain);
729         return err;
730 }
731
732 static int rcar_pcie_get_resources(struct platform_device *pdev,
733                                    struct rcar_pcie *pcie)
734 {
735         struct resource res;
736         int err, i;
737
738         err = of_address_to_resource(pdev->dev.of_node, 0, &res);
739         if (err)
740                 return err;
741
742         pcie->clk = devm_clk_get(&pdev->dev, "pcie");
743         if (IS_ERR(pcie->clk)) {
744                 dev_err(pcie->dev, "cannot get platform clock\n");
745                 return PTR_ERR(pcie->clk);
746         }
747         err = clk_prepare_enable(pcie->clk);
748         if (err)
749                 goto fail_clk;
750
751         pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
752         if (IS_ERR(pcie->bus_clk)) {
753                 dev_err(pcie->dev, "cannot get pcie bus clock\n");
754                 err = PTR_ERR(pcie->bus_clk);
755                 goto fail_clk;
756         }
757         err = clk_prepare_enable(pcie->bus_clk);
758         if (err)
759                 goto err_map_reg;
760
761         i = irq_of_parse_and_map(pdev->dev.of_node, 0);
762         if (i < 0) {
763                 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
764                 err = -ENOENT;
765                 goto err_map_reg;
766         }
767         pcie->msi.irq1 = i;
768
769         i = irq_of_parse_and_map(pdev->dev.of_node, 1);
770         if (i < 0) {
771                 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
772                 err = -ENOENT;
773                 goto err_map_reg;
774         }
775         pcie->msi.irq2 = i;
776
777         pcie->base = devm_ioremap_resource(&pdev->dev, &res);
778         if (IS_ERR(pcie->base)) {
779                 err = PTR_ERR(pcie->base);
780                 goto err_map_reg;
781         }
782
783         return 0;
784
785 err_map_reg:
786         clk_disable_unprepare(pcie->bus_clk);
787 fail_clk:
788         clk_disable_unprepare(pcie->clk);
789
790         return err;
791 }
792
793 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
794                                     struct of_pci_range *range,
795                                     int *index)
796 {
797         u64 restype = range->flags;
798         u64 cpu_addr = range->cpu_addr;
799         u64 cpu_end = range->cpu_addr + range->size;
800         u64 pci_addr = range->pci_addr;
801         u32 flags = LAM_64BIT | LAR_ENABLE;
802         u64 mask;
803         u64 size;
804         int idx = *index;
805
806         if (restype & IORESOURCE_PREFETCH)
807                 flags |= LAM_PREFETCH;
808
809         /*
810          * If the size of the range is larger than the alignment of the start
811          * address, we have to use multiple entries to perform the mapping.
812          */
813         if (cpu_addr > 0) {
814                 unsigned long nr_zeros = __ffs64(cpu_addr);
815                 u64 alignment = 1ULL << nr_zeros;
816
817                 size = min(range->size, alignment);
818         } else {
819                 size = range->size;
820         }
821         /* Hardware supports max 4GiB inbound region */
822         size = min(size, 1ULL << 32);
823
824         mask = roundup_pow_of_two(size) - 1;
825         mask &= ~0xf;
826
827         while (cpu_addr < cpu_end) {
828                 /*
829                  * Set up 64-bit inbound regions as the range parser doesn't
830                  * distinguish between 32 and 64-bit types.
831                  */
832                 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
833                 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
834                 rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
835
836                 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
837                 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
838                 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
839
840                 pci_addr += size;
841                 cpu_addr += size;
842                 idx += 2;
843
844                 if (idx > MAX_NR_INBOUND_MAPS) {
845                         dev_err(pcie->dev, "Failed to map inbound regions!\n");
846                         return -EINVAL;
847                 }
848         }
849         *index = idx;
850
851         return 0;
852 }
853
854 static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
855                                      struct device_node *node)
856 {
857         const int na = 3, ns = 2;
858         int rlen;
859
860         parser->node = node;
861         parser->pna = of_n_addr_cells(node);
862         parser->np = parser->pna + na + ns;
863
864         parser->range = of_get_property(node, "dma-ranges", &rlen);
865         if (!parser->range)
866                 return -ENOENT;
867
868         parser->end = parser->range + rlen / sizeof(__be32);
869         return 0;
870 }
871
872 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
873                                           struct device_node *np)
874 {
875         struct of_pci_range range;
876         struct of_pci_range_parser parser;
877         int index = 0;
878         int err;
879
880         if (pci_dma_range_parser_init(&parser, np))
881                 return -EINVAL;
882
883         /* Get the dma-ranges from DT */
884         for_each_of_pci_range(&parser, &range) {
885                 u64 end = range.cpu_addr + range.size - 1;
886                 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
887                         range.flags, range.cpu_addr, end, range.pci_addr);
888
889                 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
890                 if (err)
891                         return err;
892         }
893
894         return 0;
895 }
896
897 static const struct of_device_id rcar_pcie_of_match[] = {
898         { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
899         { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init },
900         { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init },
901         {},
902 };
903 MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
904
905 static int rcar_pcie_probe(struct platform_device *pdev)
906 {
907         struct rcar_pcie *pcie;
908         unsigned int data;
909         struct of_pci_range range;
910         struct of_pci_range_parser parser;
911         const struct of_device_id *of_id;
912         int err, win = 0;
913         int (*hw_init_fn)(struct rcar_pcie *);
914
915         pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
916         if (!pcie)
917                 return -ENOMEM;
918
919         pcie->dev = &pdev->dev;
920         platform_set_drvdata(pdev, pcie);
921
922         /* Get the bus range */
923         if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) {
924                 dev_err(&pdev->dev, "failed to parse bus-range property\n");
925                 return -EINVAL;
926         }
927
928         if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) {
929                 dev_err(&pdev->dev, "missing ranges property\n");
930                 return -EINVAL;
931         }
932
933         err = rcar_pcie_get_resources(pdev, pcie);
934         if (err < 0) {
935                 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
936                 return err;
937         }
938
939         for_each_of_pci_range(&parser, &range) {
940                 of_pci_range_to_resource(&range, pdev->dev.of_node,
941                                                 &pcie->res[win++]);
942
943                 if (win > RCAR_PCI_MAX_RESOURCES)
944                         break;
945         }
946
947          err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
948          if (err)
949                 return err;
950
951         if (IS_ENABLED(CONFIG_PCI_MSI)) {
952                 err = rcar_pcie_enable_msi(pcie);
953                 if (err < 0) {
954                         dev_err(&pdev->dev,
955                                 "failed to enable MSI support: %d\n",
956                                 err);
957                         return err;
958                 }
959         }
960
961         of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
962         if (!of_id || !of_id->data)
963                 return -EINVAL;
964         hw_init_fn = of_id->data;
965
966         /* Failure to get a link might just be that no cards are inserted */
967         err = hw_init_fn(pcie);
968         if (err) {
969                 dev_info(&pdev->dev, "PCIe link down\n");
970                 return 0;
971         }
972
973         data = rcar_pci_read_reg(pcie, MACSR);
974         dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
975
976         rcar_pcie_enable(pcie);
977
978         return 0;
979 }
980
981 static struct platform_driver rcar_pcie_driver = {
982         .driver = {
983                 .name = DRV_NAME,
984                 .owner = THIS_MODULE,
985                 .of_match_table = rcar_pcie_of_match,
986                 .suppress_bind_attrs = true,
987         },
988         .probe = rcar_pcie_probe,
989 };
990 module_platform_driver(rcar_pcie_driver);
991
992 MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
993 MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
994 MODULE_LICENSE("GPL v2");