PCI: Bulk conversion to generic_handle_domain_irq()
[linux-2.6-microblaze.git] / drivers / pci / controller / pcie-xilinx-nwl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCIe host controller driver for NWL PCIe Bridge
4  * Based on pcie-xilinx.c, pci-tegra.c
5  *
6  * (C) Copyright 2014 - 2015, Xilinx, Inc.
7  */
8
9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/msi.h>
16 #include <linux/of_address.h>
17 #include <linux/of_pci.h>
18 #include <linux/of_platform.h>
19 #include <linux/of_irq.h>
20 #include <linux/pci.h>
21 #include <linux/pci-ecam.h>
22 #include <linux/platform_device.h>
23 #include <linux/irqchip/chained_irq.h>
24
25 #include "../pci.h"
26
27 /* Bridge core config registers */
28 #define BRCFG_PCIE_RX0                  0x00000000
29 #define BRCFG_PCIE_RX1                  0x00000004
30 #define BRCFG_INTERRUPT                 0x00000010
31 #define BRCFG_PCIE_RX_MSG_FILTER        0x00000020
32
33 /* Egress - Bridge translation registers */
34 #define E_BREG_CAPABILITIES             0x00000200
35 #define E_BREG_CONTROL                  0x00000208
36 #define E_BREG_BASE_LO                  0x00000210
37 #define E_BREG_BASE_HI                  0x00000214
38 #define E_ECAM_CAPABILITIES             0x00000220
39 #define E_ECAM_CONTROL                  0x00000228
40 #define E_ECAM_BASE_LO                  0x00000230
41 #define E_ECAM_BASE_HI                  0x00000234
42
43 /* Ingress - address translations */
44 #define I_MSII_CAPABILITIES             0x00000300
45 #define I_MSII_CONTROL                  0x00000308
46 #define I_MSII_BASE_LO                  0x00000310
47 #define I_MSII_BASE_HI                  0x00000314
48
49 #define I_ISUB_CONTROL                  0x000003E8
50 #define SET_ISUB_CONTROL                BIT(0)
51 /* Rxed msg fifo  - Interrupt status registers */
52 #define MSGF_MISC_STATUS                0x00000400
53 #define MSGF_MISC_MASK                  0x00000404
54 #define MSGF_LEG_STATUS                 0x00000420
55 #define MSGF_LEG_MASK                   0x00000424
56 #define MSGF_MSI_STATUS_LO              0x00000440
57 #define MSGF_MSI_STATUS_HI              0x00000444
58 #define MSGF_MSI_MASK_LO                0x00000448
59 #define MSGF_MSI_MASK_HI                0x0000044C
60
61 /* Msg filter mask bits */
62 #define CFG_ENABLE_PM_MSG_FWD           BIT(1)
63 #define CFG_ENABLE_INT_MSG_FWD          BIT(2)
64 #define CFG_ENABLE_ERR_MSG_FWD          BIT(3)
65 #define CFG_ENABLE_MSG_FILTER_MASK      (CFG_ENABLE_PM_MSG_FWD | \
66                                         CFG_ENABLE_INT_MSG_FWD | \
67                                         CFG_ENABLE_ERR_MSG_FWD)
68
69 /* Misc interrupt status mask bits */
70 #define MSGF_MISC_SR_RXMSG_AVAIL        BIT(0)
71 #define MSGF_MISC_SR_RXMSG_OVER         BIT(1)
72 #define MSGF_MISC_SR_SLAVE_ERR          BIT(4)
73 #define MSGF_MISC_SR_MASTER_ERR         BIT(5)
74 #define MSGF_MISC_SR_I_ADDR_ERR         BIT(6)
75 #define MSGF_MISC_SR_E_ADDR_ERR         BIT(7)
76 #define MSGF_MISC_SR_FATAL_AER          BIT(16)
77 #define MSGF_MISC_SR_NON_FATAL_AER      BIT(17)
78 #define MSGF_MISC_SR_CORR_AER           BIT(18)
79 #define MSGF_MISC_SR_UR_DETECT          BIT(20)
80 #define MSGF_MISC_SR_NON_FATAL_DEV      BIT(22)
81 #define MSGF_MISC_SR_FATAL_DEV          BIT(23)
82 #define MSGF_MISC_SR_LINK_DOWN          BIT(24)
83 #define MSGF_MSIC_SR_LINK_AUTO_BWIDTH   BIT(25)
84 #define MSGF_MSIC_SR_LINK_BWIDTH        BIT(26)
85
86 #define MSGF_MISC_SR_MASKALL            (MSGF_MISC_SR_RXMSG_AVAIL | \
87                                         MSGF_MISC_SR_RXMSG_OVER | \
88                                         MSGF_MISC_SR_SLAVE_ERR | \
89                                         MSGF_MISC_SR_MASTER_ERR | \
90                                         MSGF_MISC_SR_I_ADDR_ERR | \
91                                         MSGF_MISC_SR_E_ADDR_ERR | \
92                                         MSGF_MISC_SR_FATAL_AER | \
93                                         MSGF_MISC_SR_NON_FATAL_AER | \
94                                         MSGF_MISC_SR_CORR_AER | \
95                                         MSGF_MISC_SR_UR_DETECT | \
96                                         MSGF_MISC_SR_NON_FATAL_DEV | \
97                                         MSGF_MISC_SR_FATAL_DEV | \
98                                         MSGF_MISC_SR_LINK_DOWN | \
99                                         MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
100                                         MSGF_MSIC_SR_LINK_BWIDTH)
101
102 /* Legacy interrupt status mask bits */
103 #define MSGF_LEG_SR_INTA                BIT(0)
104 #define MSGF_LEG_SR_INTB                BIT(1)
105 #define MSGF_LEG_SR_INTC                BIT(2)
106 #define MSGF_LEG_SR_INTD                BIT(3)
107 #define MSGF_LEG_SR_MASKALL             (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
108                                         MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
109
110 /* MSI interrupt status mask bits */
111 #define MSGF_MSI_SR_LO_MASK             GENMASK(31, 0)
112 #define MSGF_MSI_SR_HI_MASK             GENMASK(31, 0)
113
114 #define MSII_PRESENT                    BIT(0)
115 #define MSII_ENABLE                     BIT(0)
116 #define MSII_STATUS_ENABLE              BIT(15)
117
118 /* Bridge config interrupt mask */
119 #define BRCFG_INTERRUPT_MASK            BIT(0)
120 #define BREG_PRESENT                    BIT(0)
121 #define BREG_ENABLE                     BIT(0)
122 #define BREG_ENABLE_FORCE               BIT(1)
123
124 /* E_ECAM status mask bits */
125 #define E_ECAM_PRESENT                  BIT(0)
126 #define E_ECAM_CR_ENABLE                BIT(0)
127 #define E_ECAM_SIZE_LOC                 GENMASK(20, 16)
128 #define E_ECAM_SIZE_SHIFT               16
129 #define NWL_ECAM_VALUE_DEFAULT          12
130
131 #define CFG_DMA_REG_BAR                 GENMASK(2, 0)
132 #define CFG_PCIE_CACHE                  GENMASK(7, 0)
133
134 #define INT_PCI_MSI_NR                  (2 * 32)
135
136 /* Readin the PS_LINKUP */
137 #define PS_LINKUP_OFFSET                0x00000238
138 #define PCIE_PHY_LINKUP_BIT             BIT(0)
139 #define PHY_RDY_LINKUP_BIT              BIT(1)
140
141 /* Parameters for the waiting for link up routine */
142 #define LINK_WAIT_MAX_RETRIES          10
143 #define LINK_WAIT_USLEEP_MIN           90000
144 #define LINK_WAIT_USLEEP_MAX           100000
145
146 struct nwl_msi {                        /* MSI information */
147         struct irq_domain *msi_domain;
148         unsigned long *bitmap;
149         struct irq_domain *dev_domain;
150         struct mutex lock;              /* protect bitmap variable */
151         int irq_msi0;
152         int irq_msi1;
153 };
154
155 struct nwl_pcie {
156         struct device *dev;
157         void __iomem *breg_base;
158         void __iomem *pcireg_base;
159         void __iomem *ecam_base;
160         phys_addr_t phys_breg_base;     /* Physical Bridge Register Base */
161         phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
162         phys_addr_t phys_ecam_base;     /* Physical Configuration Base */
163         u32 breg_size;
164         u32 pcie_reg_size;
165         u32 ecam_size;
166         int irq_intx;
167         int irq_misc;
168         u32 ecam_value;
169         u8 last_busno;
170         struct nwl_msi msi;
171         struct irq_domain *legacy_irq_domain;
172         raw_spinlock_t leg_mask_lock;
173 };
174
175 static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
176 {
177         return readl(pcie->breg_base + off);
178 }
179
180 static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
181 {
182         writel(val, pcie->breg_base + off);
183 }
184
185 static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
186 {
187         if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
188                 return true;
189         return false;
190 }
191
192 static bool nwl_phy_link_up(struct nwl_pcie *pcie)
193 {
194         if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
195                 return true;
196         return false;
197 }
198
199 static int nwl_wait_for_link(struct nwl_pcie *pcie)
200 {
201         struct device *dev = pcie->dev;
202         int retries;
203
204         /* check if the link is up or not */
205         for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
206                 if (nwl_phy_link_up(pcie))
207                         return 0;
208                 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
209         }
210
211         dev_err(dev, "PHY link never came up\n");
212         return -ETIMEDOUT;
213 }
214
215 static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
216 {
217         struct nwl_pcie *pcie = bus->sysdata;
218
219         /* Check link before accessing downstream ports */
220         if (!pci_is_root_bus(bus)) {
221                 if (!nwl_pcie_link_up(pcie))
222                         return false;
223         } else if (devfn > 0)
224                 /* Only one device down on each root port */
225                 return false;
226
227         return true;
228 }
229
230 /**
231  * nwl_pcie_map_bus - Get configuration base
232  *
233  * @bus: Bus structure of current bus
234  * @devfn: Device/function
235  * @where: Offset from base
236  *
237  * Return: Base address of the configuration space needed to be
238  *         accessed.
239  */
240 static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
241                                       int where)
242 {
243         struct nwl_pcie *pcie = bus->sysdata;
244
245         if (!nwl_pcie_valid_device(bus, devfn))
246                 return NULL;
247
248         return pcie->ecam_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
249 }
250
251 /* PCIe operations */
252 static struct pci_ops nwl_pcie_ops = {
253         .map_bus = nwl_pcie_map_bus,
254         .read  = pci_generic_config_read,
255         .write = pci_generic_config_write,
256 };
257
258 static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
259 {
260         struct nwl_pcie *pcie = data;
261         struct device *dev = pcie->dev;
262         u32 misc_stat;
263
264         /* Checking for misc interrupts */
265         misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
266                                      MSGF_MISC_SR_MASKALL;
267         if (!misc_stat)
268                 return IRQ_NONE;
269
270         if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
271                 dev_err(dev, "Received Message FIFO Overflow\n");
272
273         if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
274                 dev_err(dev, "Slave error\n");
275
276         if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
277                 dev_err(dev, "Master error\n");
278
279         if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
280                 dev_err(dev, "In Misc Ingress address translation error\n");
281
282         if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
283                 dev_err(dev, "In Misc Egress address translation error\n");
284
285         if (misc_stat & MSGF_MISC_SR_FATAL_AER)
286                 dev_err(dev, "Fatal Error in AER Capability\n");
287
288         if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
289                 dev_err(dev, "Non-Fatal Error in AER Capability\n");
290
291         if (misc_stat & MSGF_MISC_SR_CORR_AER)
292                 dev_err(dev, "Correctable Error in AER Capability\n");
293
294         if (misc_stat & MSGF_MISC_SR_UR_DETECT)
295                 dev_err(dev, "Unsupported request Detected\n");
296
297         if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
298                 dev_err(dev, "Non-Fatal Error Detected\n");
299
300         if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
301                 dev_err(dev, "Fatal Error Detected\n");
302
303         if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
304                 dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
305
306         if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
307                 dev_info(dev, "Link Bandwidth Management Status bit set\n");
308
309         /* Clear misc interrupt status */
310         nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
311
312         return IRQ_HANDLED;
313 }
314
315 static void nwl_pcie_leg_handler(struct irq_desc *desc)
316 {
317         struct irq_chip *chip = irq_desc_get_chip(desc);
318         struct nwl_pcie *pcie;
319         unsigned long status;
320         u32 bit;
321
322         chained_irq_enter(chip, desc);
323         pcie = irq_desc_get_handler_data(desc);
324
325         while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
326                                 MSGF_LEG_SR_MASKALL) != 0) {
327                 for_each_set_bit(bit, &status, PCI_NUM_INTX)
328                         generic_handle_domain_irq(pcie->legacy_irq_domain, bit);
329         }
330
331         chained_irq_exit(chip, desc);
332 }
333
334 static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
335 {
336         struct nwl_msi *msi;
337         unsigned long status;
338         u32 bit;
339
340         msi = &pcie->msi;
341
342         while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
343                 for_each_set_bit(bit, &status, 32) {
344                         nwl_bridge_writel(pcie, 1 << bit, status_reg);
345                         generic_handle_domain_irq(msi->dev_domain, bit);
346                 }
347         }
348 }
349
350 static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
351 {
352         struct irq_chip *chip = irq_desc_get_chip(desc);
353         struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
354
355         chained_irq_enter(chip, desc);
356         nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
357         chained_irq_exit(chip, desc);
358 }
359
360 static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
361 {
362         struct irq_chip *chip = irq_desc_get_chip(desc);
363         struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
364
365         chained_irq_enter(chip, desc);
366         nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
367         chained_irq_exit(chip, desc);
368 }
369
370 static void nwl_mask_leg_irq(struct irq_data *data)
371 {
372         struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
373         unsigned long flags;
374         u32 mask;
375         u32 val;
376
377         mask = 1 << (data->hwirq - 1);
378         raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
379         val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
380         nwl_bridge_writel(pcie, (val & (~mask)), MSGF_LEG_MASK);
381         raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
382 }
383
384 static void nwl_unmask_leg_irq(struct irq_data *data)
385 {
386         struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
387         unsigned long flags;
388         u32 mask;
389         u32 val;
390
391         mask = 1 << (data->hwirq - 1);
392         raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
393         val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
394         nwl_bridge_writel(pcie, (val | mask), MSGF_LEG_MASK);
395         raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
396 }
397
398 static struct irq_chip nwl_leg_irq_chip = {
399         .name = "nwl_pcie:legacy",
400         .irq_enable = nwl_unmask_leg_irq,
401         .irq_disable = nwl_mask_leg_irq,
402         .irq_mask = nwl_mask_leg_irq,
403         .irq_unmask = nwl_unmask_leg_irq,
404 };
405
406 static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
407                           irq_hw_number_t hwirq)
408 {
409         irq_set_chip_and_handler(irq, &nwl_leg_irq_chip, handle_level_irq);
410         irq_set_chip_data(irq, domain->host_data);
411         irq_set_status_flags(irq, IRQ_LEVEL);
412
413         return 0;
414 }
415
416 static const struct irq_domain_ops legacy_domain_ops = {
417         .map = nwl_legacy_map,
418         .xlate = pci_irqd_intx_xlate,
419 };
420
421 #ifdef CONFIG_PCI_MSI
422 static struct irq_chip nwl_msi_irq_chip = {
423         .name = "nwl_pcie:msi",
424         .irq_enable = pci_msi_unmask_irq,
425         .irq_disable = pci_msi_mask_irq,
426         .irq_mask = pci_msi_mask_irq,
427         .irq_unmask = pci_msi_unmask_irq,
428 };
429
430 static struct msi_domain_info nwl_msi_domain_info = {
431         .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
432                   MSI_FLAG_MULTI_PCI_MSI),
433         .chip = &nwl_msi_irq_chip,
434 };
435 #endif
436
437 static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
438 {
439         struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
440         phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
441
442         msg->address_lo = lower_32_bits(msi_addr);
443         msg->address_hi = upper_32_bits(msi_addr);
444         msg->data = data->hwirq;
445 }
446
447 static int nwl_msi_set_affinity(struct irq_data *irq_data,
448                                 const struct cpumask *mask, bool force)
449 {
450         return -EINVAL;
451 }
452
453 static struct irq_chip nwl_irq_chip = {
454         .name = "Xilinx MSI",
455         .irq_compose_msi_msg = nwl_compose_msi_msg,
456         .irq_set_affinity = nwl_msi_set_affinity,
457 };
458
459 static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
460                                 unsigned int nr_irqs, void *args)
461 {
462         struct nwl_pcie *pcie = domain->host_data;
463         struct nwl_msi *msi = &pcie->msi;
464         int bit;
465         int i;
466
467         mutex_lock(&msi->lock);
468         bit = bitmap_find_free_region(msi->bitmap, INT_PCI_MSI_NR,
469                                       get_count_order(nr_irqs));
470         if (bit < 0) {
471                 mutex_unlock(&msi->lock);
472                 return -ENOSPC;
473         }
474
475         for (i = 0; i < nr_irqs; i++) {
476                 irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
477                                 domain->host_data, handle_simple_irq,
478                                 NULL, NULL);
479         }
480         mutex_unlock(&msi->lock);
481         return 0;
482 }
483
484 static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
485                                         unsigned int nr_irqs)
486 {
487         struct irq_data *data = irq_domain_get_irq_data(domain, virq);
488         struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
489         struct nwl_msi *msi = &pcie->msi;
490
491         mutex_lock(&msi->lock);
492         bitmap_release_region(msi->bitmap, data->hwirq,
493                               get_count_order(nr_irqs));
494         mutex_unlock(&msi->lock);
495 }
496
497 static const struct irq_domain_ops dev_msi_domain_ops = {
498         .alloc  = nwl_irq_domain_alloc,
499         .free   = nwl_irq_domain_free,
500 };
501
502 static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
503 {
504 #ifdef CONFIG_PCI_MSI
505         struct device *dev = pcie->dev;
506         struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
507         struct nwl_msi *msi = &pcie->msi;
508
509         msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
510                                                 &dev_msi_domain_ops, pcie);
511         if (!msi->dev_domain) {
512                 dev_err(dev, "failed to create dev IRQ domain\n");
513                 return -ENOMEM;
514         }
515         msi->msi_domain = pci_msi_create_irq_domain(fwnode,
516                                                     &nwl_msi_domain_info,
517                                                     msi->dev_domain);
518         if (!msi->msi_domain) {
519                 dev_err(dev, "failed to create msi IRQ domain\n");
520                 irq_domain_remove(msi->dev_domain);
521                 return -ENOMEM;
522         }
523 #endif
524         return 0;
525 }
526
527 static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
528 {
529         struct device *dev = pcie->dev;
530         struct device_node *node = dev->of_node;
531         struct device_node *legacy_intc_node;
532
533         legacy_intc_node = of_get_next_child(node, NULL);
534         if (!legacy_intc_node) {
535                 dev_err(dev, "No legacy intc node found\n");
536                 return -EINVAL;
537         }
538
539         pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
540                                                         PCI_NUM_INTX,
541                                                         &legacy_domain_ops,
542                                                         pcie);
543         of_node_put(legacy_intc_node);
544         if (!pcie->legacy_irq_domain) {
545                 dev_err(dev, "failed to create IRQ domain\n");
546                 return -ENOMEM;
547         }
548
549         raw_spin_lock_init(&pcie->leg_mask_lock);
550         nwl_pcie_init_msi_irq_domain(pcie);
551         return 0;
552 }
553
554 static int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
555 {
556         struct device *dev = pcie->dev;
557         struct platform_device *pdev = to_platform_device(dev);
558         struct nwl_msi *msi = &pcie->msi;
559         unsigned long base;
560         int ret;
561         int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
562
563         mutex_init(&msi->lock);
564
565         msi->bitmap = kzalloc(size, GFP_KERNEL);
566         if (!msi->bitmap)
567                 return -ENOMEM;
568
569         /* Get msi_1 IRQ number */
570         msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
571         if (msi->irq_msi1 < 0) {
572                 ret = -EINVAL;
573                 goto err;
574         }
575
576         irq_set_chained_handler_and_data(msi->irq_msi1,
577                                          nwl_pcie_msi_handler_high, pcie);
578
579         /* Get msi_0 IRQ number */
580         msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
581         if (msi->irq_msi0 < 0) {
582                 ret = -EINVAL;
583                 goto err;
584         }
585
586         irq_set_chained_handler_and_data(msi->irq_msi0,
587                                          nwl_pcie_msi_handler_low, pcie);
588
589         /* Check for msii_present bit */
590         ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
591         if (!ret) {
592                 dev_err(dev, "MSI not present\n");
593                 ret = -EIO;
594                 goto err;
595         }
596
597         /* Enable MSII */
598         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
599                           MSII_ENABLE, I_MSII_CONTROL);
600
601         /* Enable MSII status */
602         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
603                           MSII_STATUS_ENABLE, I_MSII_CONTROL);
604
605         /* setup AFI/FPCI range */
606         base = pcie->phys_pcie_reg_base;
607         nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
608         nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
609
610         /*
611          * For high range MSI interrupts: disable, clear any pending,
612          * and enable
613          */
614         nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_HI);
615
616         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie,  MSGF_MSI_STATUS_HI) &
617                           MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
618
619         nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
620
621         /*
622          * For low range MSI interrupts: disable, clear any pending,
623          * and enable
624          */
625         nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_LO);
626
627         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
628                           MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
629
630         nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
631
632         return 0;
633 err:
634         kfree(msi->bitmap);
635         msi->bitmap = NULL;
636         return ret;
637 }
638
639 static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
640 {
641         struct device *dev = pcie->dev;
642         struct platform_device *pdev = to_platform_device(dev);
643         u32 breg_val, ecam_val, first_busno = 0;
644         int err;
645
646         breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
647         if (!breg_val) {
648                 dev_err(dev, "BREG is not present\n");
649                 return breg_val;
650         }
651
652         /* Write bridge_off to breg base */
653         nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
654                           E_BREG_BASE_LO);
655         nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
656                           E_BREG_BASE_HI);
657
658         /* Enable BREG */
659         nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
660                           E_BREG_CONTROL);
661
662         /* Disable DMA channel registers */
663         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
664                           CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
665
666         /* Enable Ingress subtractive decode translation */
667         nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
668
669         /* Enable msg filtering details */
670         nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
671                           BRCFG_PCIE_RX_MSG_FILTER);
672
673         /* This routes the PCIe DMA traffic to go through CCI path */
674         if (of_dma_is_coherent(dev->of_node))
675                 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX1) |
676                                   CFG_PCIE_CACHE, BRCFG_PCIE_RX1);
677
678         err = nwl_wait_for_link(pcie);
679         if (err)
680                 return err;
681
682         ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
683         if (!ecam_val) {
684                 dev_err(dev, "ECAM is not present\n");
685                 return ecam_val;
686         }
687
688         /* Enable ECAM */
689         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
690                           E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
691
692         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
693                           (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
694                           E_ECAM_CONTROL);
695
696         nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
697                           E_ECAM_BASE_LO);
698         nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
699                           E_ECAM_BASE_HI);
700
701         /* Get bus range */
702         ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
703         pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
704         /* Write primary, secondary and subordinate bus numbers */
705         ecam_val = first_busno;
706         ecam_val |= (first_busno + 1) << 8;
707         ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
708         writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
709
710         if (nwl_pcie_link_up(pcie))
711                 dev_info(dev, "Link is UP\n");
712         else
713                 dev_info(dev, "Link is DOWN\n");
714
715         /* Get misc IRQ number */
716         pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
717         if (pcie->irq_misc < 0)
718                 return -EINVAL;
719
720         err = devm_request_irq(dev, pcie->irq_misc,
721                                nwl_pcie_misc_handler, IRQF_SHARED,
722                                "nwl_pcie:misc", pcie);
723         if (err) {
724                 dev_err(dev, "fail to register misc IRQ#%d\n",
725                         pcie->irq_misc);
726                 return err;
727         }
728
729         /* Disable all misc interrupts */
730         nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
731
732         /* Clear pending misc interrupts */
733         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
734                           MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
735
736         /* Enable all misc interrupts */
737         nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
738
739
740         /* Disable all legacy interrupts */
741         nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
742
743         /* Clear pending legacy interrupts */
744         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
745                           MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
746
747         /* Enable all legacy interrupts */
748         nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
749
750         /* Enable the bridge config interrupt */
751         nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
752                           BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
753
754         return 0;
755 }
756
757 static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
758                              struct platform_device *pdev)
759 {
760         struct device *dev = pcie->dev;
761         struct resource *res;
762
763         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
764         pcie->breg_base = devm_ioremap_resource(dev, res);
765         if (IS_ERR(pcie->breg_base))
766                 return PTR_ERR(pcie->breg_base);
767         pcie->phys_breg_base = res->start;
768
769         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
770         pcie->pcireg_base = devm_ioremap_resource(dev, res);
771         if (IS_ERR(pcie->pcireg_base))
772                 return PTR_ERR(pcie->pcireg_base);
773         pcie->phys_pcie_reg_base = res->start;
774
775         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
776         pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
777         if (IS_ERR(pcie->ecam_base))
778                 return PTR_ERR(pcie->ecam_base);
779         pcie->phys_ecam_base = res->start;
780
781         /* Get intx IRQ number */
782         pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
783         if (pcie->irq_intx < 0)
784                 return pcie->irq_intx;
785
786         irq_set_chained_handler_and_data(pcie->irq_intx,
787                                          nwl_pcie_leg_handler, pcie);
788
789         return 0;
790 }
791
792 static const struct of_device_id nwl_pcie_of_match[] = {
793         { .compatible = "xlnx,nwl-pcie-2.11", },
794         {}
795 };
796
797 static int nwl_pcie_probe(struct platform_device *pdev)
798 {
799         struct device *dev = &pdev->dev;
800         struct nwl_pcie *pcie;
801         struct pci_host_bridge *bridge;
802         int err;
803
804         bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
805         if (!bridge)
806                 return -ENODEV;
807
808         pcie = pci_host_bridge_priv(bridge);
809
810         pcie->dev = dev;
811         pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
812
813         err = nwl_pcie_parse_dt(pcie, pdev);
814         if (err) {
815                 dev_err(dev, "Parsing DT failed\n");
816                 return err;
817         }
818
819         err = nwl_pcie_bridge_init(pcie);
820         if (err) {
821                 dev_err(dev, "HW Initialization failed\n");
822                 return err;
823         }
824
825         err = nwl_pcie_init_irq_domain(pcie);
826         if (err) {
827                 dev_err(dev, "Failed creating IRQ Domain\n");
828                 return err;
829         }
830
831         bridge->sysdata = pcie;
832         bridge->ops = &nwl_pcie_ops;
833
834         if (IS_ENABLED(CONFIG_PCI_MSI)) {
835                 err = nwl_pcie_enable_msi(pcie);
836                 if (err < 0) {
837                         dev_err(dev, "failed to enable MSI support: %d\n", err);
838                         return err;
839                 }
840         }
841
842         return pci_host_probe(bridge);
843 }
844
845 static struct platform_driver nwl_pcie_driver = {
846         .driver = {
847                 .name = "nwl-pcie",
848                 .suppress_bind_attrs = true,
849                 .of_match_table = nwl_pcie_of_match,
850         },
851         .probe = nwl_pcie_probe,
852 };
853 builtin_platform_driver(nwl_pcie_driver);