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