Merge branch 'remotes/lorenzo/pci/cadence'
[linux-2.6-microblaze.git] / drivers / pci / controller / pcie-rcar-host.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe driver for Renesas R-Car SoCs
4  *  Copyright (C) 2014-2020 Renesas Electronics Europe Ltd
5  *
6  * Based on:
7  *  arch/sh/drivers/pci/pcie-sh7786.c
8  *  arch/sh/drivers/pci/ops-sh7786.c
9  *  Copyright (C) 2009 - 2011  Paul Mundt
10  *
11  * Author: Phil Edworthy <phil.edworthy@renesas.com>
12  */
13
14 #include <linux/bitops.h>
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/init.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/phy/phy.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/slab.h>
32
33 #include "pcie-rcar.h"
34
35 struct rcar_msi {
36         DECLARE_BITMAP(used, INT_PCI_MSI_NR);
37         struct irq_domain *domain;
38         struct mutex map_lock;
39         spinlock_t mask_lock;
40         int irq1;
41         int irq2;
42 };
43
44 /* Structure representing the PCIe interface */
45 struct rcar_pcie_host {
46         struct rcar_pcie        pcie;
47         struct phy              *phy;
48         struct clk              *bus_clk;
49         struct                  rcar_msi msi;
50         int                     (*phy_init_fn)(struct rcar_pcie_host *host);
51 };
52
53 static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi)
54 {
55         return container_of(msi, struct rcar_pcie_host, msi);
56 }
57
58 static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
59 {
60         unsigned int shift = BITS_PER_BYTE * (where & 3);
61         u32 val = rcar_pci_read_reg(pcie, where & ~3);
62
63         return val >> shift;
64 }
65
66 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
67 static int rcar_pcie_config_access(struct rcar_pcie_host *host,
68                 unsigned char access_type, struct pci_bus *bus,
69                 unsigned int devfn, int where, u32 *data)
70 {
71         struct rcar_pcie *pcie = &host->pcie;
72         unsigned int dev, func, reg, index;
73
74         dev = PCI_SLOT(devfn);
75         func = PCI_FUNC(devfn);
76         reg = where & ~3;
77         index = reg / 4;
78
79         /*
80          * While each channel has its own memory-mapped extended config
81          * space, it's generally only accessible when in endpoint mode.
82          * When in root complex mode, the controller is unable to target
83          * itself with either type 0 or type 1 accesses, and indeed, any
84          * controller initiated target transfer to its own config space
85          * result in a completer abort.
86          *
87          * Each channel effectively only supports a single device, but as
88          * the same channel <-> device access works for any PCI_SLOT()
89          * value, we cheat a bit here and bind the controller's config
90          * space to devfn 0 in order to enable self-enumeration. In this
91          * case the regular ECAR/ECDR path is sidelined and the mangled
92          * config access itself is initiated as an internal bus transaction.
93          */
94         if (pci_is_root_bus(bus)) {
95                 if (dev != 0)
96                         return PCIBIOS_DEVICE_NOT_FOUND;
97
98                 if (access_type == RCAR_PCI_ACCESS_READ)
99                         *data = rcar_pci_read_reg(pcie, PCICONF(index));
100                 else
101                         rcar_pci_write_reg(pcie, *data, PCICONF(index));
102
103                 return PCIBIOS_SUCCESSFUL;
104         }
105
106         /* Clear errors */
107         rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
108
109         /* Set the PIO address */
110         rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
111                 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
112
113         /* Enable the configuration access */
114         if (pci_is_root_bus(bus->parent))
115                 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
116         else
117                 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
118
119         /* Check for errors */
120         if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
121                 return PCIBIOS_DEVICE_NOT_FOUND;
122
123         /* Check for master and target aborts */
124         if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
125                 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
126                 return PCIBIOS_DEVICE_NOT_FOUND;
127
128         if (access_type == RCAR_PCI_ACCESS_READ)
129                 *data = rcar_pci_read_reg(pcie, PCIECDR);
130         else
131                 rcar_pci_write_reg(pcie, *data, PCIECDR);
132
133         /* Disable the configuration access */
134         rcar_pci_write_reg(pcie, 0, PCIECCTLR);
135
136         return PCIBIOS_SUCCESSFUL;
137 }
138
139 static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
140                                int where, int size, u32 *val)
141 {
142         struct rcar_pcie_host *host = bus->sysdata;
143         int ret;
144
145         ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
146                                       bus, devfn, where, val);
147         if (ret != PCIBIOS_SUCCESSFUL) {
148                 *val = 0xffffffff;
149                 return ret;
150         }
151
152         if (size == 1)
153                 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
154         else if (size == 2)
155                 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
156
157         dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
158                 bus->number, devfn, where, size, *val);
159
160         return ret;
161 }
162
163 /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
164 static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
165                                 int where, int size, u32 val)
166 {
167         struct rcar_pcie_host *host = bus->sysdata;
168         unsigned int shift;
169         u32 data;
170         int ret;
171
172         ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
173                                       bus, devfn, where, &data);
174         if (ret != PCIBIOS_SUCCESSFUL)
175                 return ret;
176
177         dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
178                 bus->number, devfn, where, size, val);
179
180         if (size == 1) {
181                 shift = BITS_PER_BYTE * (where & 3);
182                 data &= ~(0xff << shift);
183                 data |= ((val & 0xff) << shift);
184         } else if (size == 2) {
185                 shift = BITS_PER_BYTE * (where & 2);
186                 data &= ~(0xffff << shift);
187                 data |= ((val & 0xffff) << shift);
188         } else
189                 data = val;
190
191         ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
192                                       bus, devfn, where, &data);
193
194         return ret;
195 }
196
197 static struct pci_ops rcar_pcie_ops = {
198         .read   = rcar_pcie_read_conf,
199         .write  = rcar_pcie_write_conf,
200 };
201
202 static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
203 {
204         struct device *dev = pcie->dev;
205         unsigned int timeout = 1000;
206         u32 macsr;
207
208         if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
209                 return;
210
211         if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
212                 dev_err(dev, "Speed change already in progress\n");
213                 return;
214         }
215
216         macsr = rcar_pci_read_reg(pcie, MACSR);
217         if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
218                 goto done;
219
220         /* Set target link speed to 5.0 GT/s */
221         rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
222                    PCI_EXP_LNKSTA_CLS_5_0GB);
223
224         /* Set speed change reason as intentional factor */
225         rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
226
227         /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
228         if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
229                 rcar_pci_write_reg(pcie, macsr, MACSR);
230
231         /* Start link speed change */
232         rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
233
234         while (timeout--) {
235                 macsr = rcar_pci_read_reg(pcie, MACSR);
236                 if (macsr & SPCHGFIN) {
237                         /* Clear the interrupt bits */
238                         rcar_pci_write_reg(pcie, macsr, MACSR);
239
240                         if (macsr & SPCHGFAIL)
241                                 dev_err(dev, "Speed change failed\n");
242
243                         goto done;
244                 }
245
246                 msleep(1);
247         }
248
249         dev_err(dev, "Speed change timed out\n");
250
251 done:
252         dev_info(dev, "Current link speed is %s GT/s\n",
253                  (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
254 }
255
256 static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
257 {
258         struct rcar_pcie *pcie = &host->pcie;
259         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
260         struct resource_entry *win;
261         LIST_HEAD(res);
262         int i = 0;
263
264         /* Try setting 5 GT/s link speed */
265         rcar_pcie_force_speedup(pcie);
266
267         /* Setup PCI resources */
268         resource_list_for_each_entry(win, &bridge->windows) {
269                 struct resource *res = win->res;
270
271                 if (!res->flags)
272                         continue;
273
274                 switch (resource_type(res)) {
275                 case IORESOURCE_IO:
276                 case IORESOURCE_MEM:
277                         rcar_pcie_set_outbound(pcie, i, win);
278                         i++;
279                         break;
280                 }
281         }
282 }
283
284 static int rcar_pcie_enable(struct rcar_pcie_host *host)
285 {
286         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
287
288         rcar_pcie_hw_enable(host);
289
290         pci_add_flags(PCI_REASSIGN_ALL_BUS);
291
292         bridge->sysdata = host;
293         bridge->ops = &rcar_pcie_ops;
294
295         return pci_host_probe(bridge);
296 }
297
298 static int phy_wait_for_ack(struct rcar_pcie *pcie)
299 {
300         struct device *dev = pcie->dev;
301         unsigned int timeout = 100;
302
303         while (timeout--) {
304                 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
305                         return 0;
306
307                 udelay(100);
308         }
309
310         dev_err(dev, "Access to PCIe phy timed out\n");
311
312         return -ETIMEDOUT;
313 }
314
315 static void phy_write_reg(struct rcar_pcie *pcie,
316                           unsigned int rate, u32 addr,
317                           unsigned int lane, u32 data)
318 {
319         u32 phyaddr;
320
321         phyaddr = WRITE_CMD |
322                 ((rate & 1) << RATE_POS) |
323                 ((lane & 0xf) << LANE_POS) |
324                 ((addr & 0xff) << ADR_POS);
325
326         /* Set write data */
327         rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
328         rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
329
330         /* Ignore errors as they will be dealt with if the data link is down */
331         phy_wait_for_ack(pcie);
332
333         /* Clear command */
334         rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
335         rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
336
337         /* Ignore errors as they will be dealt with if the data link is down */
338         phy_wait_for_ack(pcie);
339 }
340
341 static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
342 {
343         int err;
344
345         /* Begin initialization */
346         rcar_pci_write_reg(pcie, 0, PCIETCTLR);
347
348         /* Set mode */
349         rcar_pci_write_reg(pcie, 1, PCIEMSR);
350
351         err = rcar_pcie_wait_for_phyrdy(pcie);
352         if (err)
353                 return err;
354
355         /*
356          * Initial header for port config space is type 1, set the device
357          * class to match. Hardware takes care of propagating the IDSETR
358          * settings, so there is no need to bother with a quirk.
359          */
360         rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
361
362         /*
363          * Setup Secondary Bus Number & Subordinate Bus Number, even though
364          * they aren't used, to avoid bridge being detected as broken.
365          */
366         rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
367         rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
368
369         /* Initialize default capabilities. */
370         rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
371         rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
372                 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
373         rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
374                 PCI_HEADER_TYPE_BRIDGE);
375
376         /* Enable data link layer active state reporting */
377         rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
378                 PCI_EXP_LNKCAP_DLLLARC);
379
380         /* Write out the physical slot number = 0 */
381         rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
382
383         /* Set the completion timer timeout to the maximum 50ms. */
384         rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
385
386         /* Terminate list of capabilities (Next Capability Offset=0) */
387         rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
388
389         /* Enable MSI */
390         if (IS_ENABLED(CONFIG_PCI_MSI))
391                 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
392
393         rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
394
395         /* Finish initialization - establish a PCI Express link */
396         rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
397
398         /* This will timeout if we don't have a link. */
399         err = rcar_pcie_wait_for_dl(pcie);
400         if (err)
401                 return err;
402
403         /* Enable INTx interrupts */
404         rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
405
406         wmb();
407
408         return 0;
409 }
410
411 static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
412 {
413         struct rcar_pcie *pcie = &host->pcie;
414
415         /* Initialize the phy */
416         phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
417         phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
418         phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
419         phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
420         phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
421         phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
422         phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
423         phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
424         phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
425         phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
426         phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
427         phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
428
429         phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
430         phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
431         phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
432
433         return 0;
434 }
435
436 static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
437 {
438         struct rcar_pcie *pcie = &host->pcie;
439
440         /*
441          * These settings come from the R-Car Series, 2nd Generation User's
442          * Manual, section 50.3.1 (2) Initialization of the physical layer.
443          */
444         rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
445         rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
446         rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
447         rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
448
449         rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
450         /* The following value is for DC connection, no termination resistor */
451         rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
452         rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
453         rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
454
455         return 0;
456 }
457
458 static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
459 {
460         int err;
461
462         err = phy_init(host->phy);
463         if (err)
464                 return err;
465
466         err = phy_power_on(host->phy);
467         if (err)
468                 phy_exit(host->phy);
469
470         return err;
471 }
472
473 static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
474 {
475         struct rcar_pcie_host *host = data;
476         struct rcar_pcie *pcie = &host->pcie;
477         struct rcar_msi *msi = &host->msi;
478         struct device *dev = pcie->dev;
479         unsigned long reg;
480
481         reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
482
483         /* MSI & INTx share an interrupt - we only handle MSI here */
484         if (!reg)
485                 return IRQ_NONE;
486
487         while (reg) {
488                 unsigned int index = find_first_bit(&reg, 32);
489                 int ret;
490
491                 ret = generic_handle_domain_irq(msi->domain->parent, index);
492                 if (ret) {
493                         /* Unknown MSI, just clear it */
494                         dev_dbg(dev, "unexpected MSI\n");
495                         rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR);
496                 }
497
498                 /* see if there's any more pending in this vector */
499                 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
500         }
501
502         return IRQ_HANDLED;
503 }
504
505 static void rcar_msi_top_irq_ack(struct irq_data *d)
506 {
507         irq_chip_ack_parent(d);
508 }
509
510 static void rcar_msi_top_irq_mask(struct irq_data *d)
511 {
512         pci_msi_mask_irq(d);
513         irq_chip_mask_parent(d);
514 }
515
516 static void rcar_msi_top_irq_unmask(struct irq_data *d)
517 {
518         pci_msi_unmask_irq(d);
519         irq_chip_unmask_parent(d);
520 }
521
522 static struct irq_chip rcar_msi_top_chip = {
523         .name           = "PCIe MSI",
524         .irq_ack        = rcar_msi_top_irq_ack,
525         .irq_mask       = rcar_msi_top_irq_mask,
526         .irq_unmask     = rcar_msi_top_irq_unmask,
527 };
528
529 static void rcar_msi_irq_ack(struct irq_data *d)
530 {
531         struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
532         struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
533
534         /* clear the interrupt */
535         rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR);
536 }
537
538 static void rcar_msi_irq_mask(struct irq_data *d)
539 {
540         struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
541         struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
542         unsigned long flags;
543         u32 value;
544
545         spin_lock_irqsave(&msi->mask_lock, flags);
546         value = rcar_pci_read_reg(pcie, PCIEMSIIER);
547         value &= ~BIT(d->hwirq);
548         rcar_pci_write_reg(pcie, value, PCIEMSIIER);
549         spin_unlock_irqrestore(&msi->mask_lock, flags);
550 }
551
552 static void rcar_msi_irq_unmask(struct irq_data *d)
553 {
554         struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
555         struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
556         unsigned long flags;
557         u32 value;
558
559         spin_lock_irqsave(&msi->mask_lock, flags);
560         value = rcar_pci_read_reg(pcie, PCIEMSIIER);
561         value |= BIT(d->hwirq);
562         rcar_pci_write_reg(pcie, value, PCIEMSIIER);
563         spin_unlock_irqrestore(&msi->mask_lock, flags);
564 }
565
566 static int rcar_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force)
567 {
568         return -EINVAL;
569 }
570
571 static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
572 {
573         struct rcar_msi *msi = irq_data_get_irq_chip_data(data);
574         struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
575
576         msg->address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
577         msg->address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
578         msg->data = data->hwirq;
579 }
580
581 static struct irq_chip rcar_msi_bottom_chip = {
582         .name                   = "Rcar MSI",
583         .irq_ack                = rcar_msi_irq_ack,
584         .irq_mask               = rcar_msi_irq_mask,
585         .irq_unmask             = rcar_msi_irq_unmask,
586         .irq_set_affinity       = rcar_msi_set_affinity,
587         .irq_compose_msi_msg    = rcar_compose_msi_msg,
588 };
589
590 static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
591                                   unsigned int nr_irqs, void *args)
592 {
593         struct rcar_msi *msi = domain->host_data;
594         unsigned int i;
595         int hwirq;
596
597         mutex_lock(&msi->map_lock);
598
599         hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
600
601         mutex_unlock(&msi->map_lock);
602
603         if (hwirq < 0)
604                 return -ENOSPC;
605
606         for (i = 0; i < nr_irqs; i++)
607                 irq_domain_set_info(domain, virq + i, hwirq + i,
608                                     &rcar_msi_bottom_chip, domain->host_data,
609                                     handle_edge_irq, NULL, NULL);
610
611         return 0;
612 }
613
614 static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq,
615                                   unsigned int nr_irqs)
616 {
617         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
618         struct rcar_msi *msi = domain->host_data;
619
620         mutex_lock(&msi->map_lock);
621
622         bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
623
624         mutex_unlock(&msi->map_lock);
625 }
626
627 static const struct irq_domain_ops rcar_msi_domain_ops = {
628         .alloc  = rcar_msi_domain_alloc,
629         .free   = rcar_msi_domain_free,
630 };
631
632 static struct msi_domain_info rcar_msi_info = {
633         .flags  = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
634                    MSI_FLAG_MULTI_PCI_MSI),
635         .chip   = &rcar_msi_top_chip,
636 };
637
638 static int rcar_allocate_domains(struct rcar_msi *msi)
639 {
640         struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
641         struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
642         struct irq_domain *parent;
643
644         parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR,
645                                           &rcar_msi_domain_ops, msi);
646         if (!parent) {
647                 dev_err(pcie->dev, "failed to create IRQ domain\n");
648                 return -ENOMEM;
649         }
650         irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
651
652         msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent);
653         if (!msi->domain) {
654                 dev_err(pcie->dev, "failed to create MSI domain\n");
655                 irq_domain_remove(parent);
656                 return -ENOMEM;
657         }
658
659         return 0;
660 }
661
662 static void rcar_free_domains(struct rcar_msi *msi)
663 {
664         struct irq_domain *parent = msi->domain->parent;
665
666         irq_domain_remove(msi->domain);
667         irq_domain_remove(parent);
668 }
669
670 static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
671 {
672         struct rcar_pcie *pcie = &host->pcie;
673         struct device *dev = pcie->dev;
674         struct rcar_msi *msi = &host->msi;
675         struct resource res;
676         int err;
677
678         mutex_init(&msi->map_lock);
679         spin_lock_init(&msi->mask_lock);
680
681         err = of_address_to_resource(dev->of_node, 0, &res);
682         if (err)
683                 return err;
684
685         err = rcar_allocate_domains(msi);
686         if (err)
687                 return err;
688
689         /* Two irqs are for MSI, but they are also used for non-MSI irqs */
690         err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
691                                IRQF_SHARED | IRQF_NO_THREAD,
692                                rcar_msi_bottom_chip.name, host);
693         if (err < 0) {
694                 dev_err(dev, "failed to request IRQ: %d\n", err);
695                 goto err;
696         }
697
698         err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
699                                IRQF_SHARED | IRQF_NO_THREAD,
700                                rcar_msi_bottom_chip.name, host);
701         if (err < 0) {
702                 dev_err(dev, "failed to request IRQ: %d\n", err);
703                 goto err;
704         }
705
706         /* disable all MSIs */
707         rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
708
709         /*
710          * Setup MSI data target using RC base address address, which
711          * is guaranteed to be in the low 32bit range on any RCar HW.
712          */
713         rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
714         rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
715
716         return 0;
717
718 err:
719         rcar_free_domains(msi);
720         return err;
721 }
722
723 static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
724 {
725         struct rcar_pcie *pcie = &host->pcie;
726
727         /* Disable all MSI interrupts */
728         rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
729
730         /* Disable address decoding of the MSI interrupt, MSIFE */
731         rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
732
733         rcar_free_domains(&host->msi);
734 }
735
736 static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
737 {
738         struct rcar_pcie *pcie = &host->pcie;
739         struct device *dev = pcie->dev;
740         struct resource res;
741         int err, i;
742
743         host->phy = devm_phy_optional_get(dev, "pcie");
744         if (IS_ERR(host->phy))
745                 return PTR_ERR(host->phy);
746
747         err = of_address_to_resource(dev->of_node, 0, &res);
748         if (err)
749                 return err;
750
751         pcie->base = devm_ioremap_resource(dev, &res);
752         if (IS_ERR(pcie->base))
753                 return PTR_ERR(pcie->base);
754
755         host->bus_clk = devm_clk_get(dev, "pcie_bus");
756         if (IS_ERR(host->bus_clk)) {
757                 dev_err(dev, "cannot get pcie bus clock\n");
758                 return PTR_ERR(host->bus_clk);
759         }
760
761         i = irq_of_parse_and_map(dev->of_node, 0);
762         if (!i) {
763                 dev_err(dev, "cannot get platform resources for msi interrupt\n");
764                 err = -ENOENT;
765                 goto err_irq1;
766         }
767         host->msi.irq1 = i;
768
769         i = irq_of_parse_and_map(dev->of_node, 1);
770         if (!i) {
771                 dev_err(dev, "cannot get platform resources for msi interrupt\n");
772                 err = -ENOENT;
773                 goto err_irq2;
774         }
775         host->msi.irq2 = i;
776
777         return 0;
778
779 err_irq2:
780         irq_dispose_mapping(host->msi.irq1);
781 err_irq1:
782         return err;
783 }
784
785 static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
786                                     struct resource_entry *entry,
787                                     int *index)
788 {
789         u64 restype = entry->res->flags;
790         u64 cpu_addr = entry->res->start;
791         u64 cpu_end = entry->res->end;
792         u64 pci_addr = entry->res->start - entry->offset;
793         u32 flags = LAM_64BIT | LAR_ENABLE;
794         u64 mask;
795         u64 size = resource_size(entry->res);
796         int idx = *index;
797
798         if (restype & IORESOURCE_PREFETCH)
799                 flags |= LAM_PREFETCH;
800
801         while (cpu_addr < cpu_end) {
802                 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
803                         dev_err(pcie->dev, "Failed to map inbound regions!\n");
804                         return -EINVAL;
805                 }
806                 /*
807                  * If the size of the range is larger than the alignment of
808                  * the start address, we have to use multiple entries to
809                  * perform the mapping.
810                  */
811                 if (cpu_addr > 0) {
812                         unsigned long nr_zeros = __ffs64(cpu_addr);
813                         u64 alignment = 1ULL << nr_zeros;
814
815                         size = min(size, alignment);
816                 }
817                 /* Hardware supports max 4GiB inbound region */
818                 size = min(size, 1ULL << 32);
819
820                 mask = roundup_pow_of_two(size) - 1;
821                 mask &= ~0xf;
822
823                 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
824                                       lower_32_bits(mask) | flags, idx, true);
825
826                 pci_addr += size;
827                 cpu_addr += size;
828                 idx += 2;
829         }
830         *index = idx;
831
832         return 0;
833 }
834
835 static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
836 {
837         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
838         struct resource_entry *entry;
839         int index = 0, err = 0;
840
841         resource_list_for_each_entry(entry, &bridge->dma_ranges) {
842                 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
843                 if (err)
844                         break;
845         }
846
847         return err;
848 }
849
850 static const struct of_device_id rcar_pcie_of_match[] = {
851         { .compatible = "renesas,pcie-r8a7779",
852           .data = rcar_pcie_phy_init_h1 },
853         { .compatible = "renesas,pcie-r8a7790",
854           .data = rcar_pcie_phy_init_gen2 },
855         { .compatible = "renesas,pcie-r8a7791",
856           .data = rcar_pcie_phy_init_gen2 },
857         { .compatible = "renesas,pcie-rcar-gen2",
858           .data = rcar_pcie_phy_init_gen2 },
859         { .compatible = "renesas,pcie-r8a7795",
860           .data = rcar_pcie_phy_init_gen3 },
861         { .compatible = "renesas,pcie-rcar-gen3",
862           .data = rcar_pcie_phy_init_gen3 },
863         {},
864 };
865
866 static int rcar_pcie_probe(struct platform_device *pdev)
867 {
868         struct device *dev = &pdev->dev;
869         struct rcar_pcie_host *host;
870         struct rcar_pcie *pcie;
871         u32 data;
872         int err;
873         struct pci_host_bridge *bridge;
874
875         bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
876         if (!bridge)
877                 return -ENOMEM;
878
879         host = pci_host_bridge_priv(bridge);
880         pcie = &host->pcie;
881         pcie->dev = dev;
882         platform_set_drvdata(pdev, host);
883
884         pm_runtime_enable(pcie->dev);
885         err = pm_runtime_get_sync(pcie->dev);
886         if (err < 0) {
887                 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
888                 goto err_pm_put;
889         }
890
891         err = rcar_pcie_get_resources(host);
892         if (err < 0) {
893                 dev_err(dev, "failed to request resources: %d\n", err);
894                 goto err_pm_put;
895         }
896
897         err = clk_prepare_enable(host->bus_clk);
898         if (err) {
899                 dev_err(dev, "failed to enable bus clock: %d\n", err);
900                 goto err_unmap_msi_irqs;
901         }
902
903         err = rcar_pcie_parse_map_dma_ranges(host);
904         if (err)
905                 goto err_clk_disable;
906
907         host->phy_init_fn = of_device_get_match_data(dev);
908         err = host->phy_init_fn(host);
909         if (err) {
910                 dev_err(dev, "failed to init PCIe PHY\n");
911                 goto err_clk_disable;
912         }
913
914         /* Failure to get a link might just be that no cards are inserted */
915         if (rcar_pcie_hw_init(pcie)) {
916                 dev_info(dev, "PCIe link down\n");
917                 err = -ENODEV;
918                 goto err_phy_shutdown;
919         }
920
921         data = rcar_pci_read_reg(pcie, MACSR);
922         dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
923
924         if (IS_ENABLED(CONFIG_PCI_MSI)) {
925                 err = rcar_pcie_enable_msi(host);
926                 if (err < 0) {
927                         dev_err(dev,
928                                 "failed to enable MSI support: %d\n",
929                                 err);
930                         goto err_phy_shutdown;
931                 }
932         }
933
934         err = rcar_pcie_enable(host);
935         if (err)
936                 goto err_msi_teardown;
937
938         return 0;
939
940 err_msi_teardown:
941         if (IS_ENABLED(CONFIG_PCI_MSI))
942                 rcar_pcie_teardown_msi(host);
943
944 err_phy_shutdown:
945         if (host->phy) {
946                 phy_power_off(host->phy);
947                 phy_exit(host->phy);
948         }
949
950 err_clk_disable:
951         clk_disable_unprepare(host->bus_clk);
952
953 err_unmap_msi_irqs:
954         irq_dispose_mapping(host->msi.irq2);
955         irq_dispose_mapping(host->msi.irq1);
956
957 err_pm_put:
958         pm_runtime_put(dev);
959         pm_runtime_disable(dev);
960
961         return err;
962 }
963
964 static int __maybe_unused rcar_pcie_resume(struct device *dev)
965 {
966         struct rcar_pcie_host *host = dev_get_drvdata(dev);
967         struct rcar_pcie *pcie = &host->pcie;
968         unsigned int data;
969         int err;
970
971         err = rcar_pcie_parse_map_dma_ranges(host);
972         if (err)
973                 return 0;
974
975         /* Failure to get a link might just be that no cards are inserted */
976         err = host->phy_init_fn(host);
977         if (err) {
978                 dev_info(dev, "PCIe link down\n");
979                 return 0;
980         }
981
982         data = rcar_pci_read_reg(pcie, MACSR);
983         dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
984
985         /* Enable MSI */
986         if (IS_ENABLED(CONFIG_PCI_MSI)) {
987                 struct resource res;
988                 u32 val;
989
990                 of_address_to_resource(dev->of_node, 0, &res);
991                 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
992                 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
993
994                 bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR);
995                 rcar_pci_write_reg(pcie, val, PCIEMSIIER);
996         }
997
998         rcar_pcie_hw_enable(host);
999
1000         return 0;
1001 }
1002
1003 static int rcar_pcie_resume_noirq(struct device *dev)
1004 {
1005         struct rcar_pcie_host *host = dev_get_drvdata(dev);
1006         struct rcar_pcie *pcie = &host->pcie;
1007
1008         if (rcar_pci_read_reg(pcie, PMSR) &&
1009             !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1010                 return 0;
1011
1012         /* Re-establish the PCIe link */
1013         rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1014         rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1015         return rcar_pcie_wait_for_dl(pcie);
1016 }
1017
1018 static const struct dev_pm_ops rcar_pcie_pm_ops = {
1019         SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1020         .resume_noirq = rcar_pcie_resume_noirq,
1021 };
1022
1023 static struct platform_driver rcar_pcie_driver = {
1024         .driver = {
1025                 .name = "rcar-pcie",
1026                 .of_match_table = rcar_pcie_of_match,
1027                 .pm = &rcar_pcie_pm_ops,
1028                 .suppress_bind_attrs = true,
1029         },
1030         .probe = rcar_pcie_probe,
1031 };
1032 builtin_platform_driver(rcar_pcie_driver);