2 * PCIe host controller driver for NWL PCIe Bridge
3 * Based on pcie-xilinx.c, pci-tegra.c
5 * (C) Copyright 2014 - 2015, Xilinx, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/msi.h>
20 #include <linux/of_address.h>
21 #include <linux/of_pci.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_irq.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/irqchip/chained_irq.h>
28 /* Bridge core config registers */
29 #define BRCFG_PCIE_RX0 0x00000000
30 #define BRCFG_INTERRUPT 0x00000010
31 #define BRCFG_PCIE_RX_MSG_FILTER 0x00000020
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
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
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
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_SLT_MSG_FWD BIT(5)
66 #define CFG_ENABLE_VEN_MSG_FWD BIT(7)
67 #define CFG_ENABLE_OTH_MSG_FWD BIT(13)
68 #define CFG_ENABLE_VEN_MSG_EN BIT(14)
69 #define CFG_ENABLE_VEN_MSG_VEN_INV BIT(15)
70 #define CFG_ENABLE_VEN_MSG_VEN_ID GENMASK(31, 16)
71 #define CFG_ENABLE_MSG_FILTER_MASK (CFG_ENABLE_PM_MSG_FWD | \
72 CFG_ENABLE_INT_MSG_FWD | \
73 CFG_ENABLE_ERR_MSG_FWD | \
74 CFG_ENABLE_SLT_MSG_FWD | \
75 CFG_ENABLE_VEN_MSG_FWD | \
76 CFG_ENABLE_OTH_MSG_FWD | \
77 CFG_ENABLE_VEN_MSG_EN | \
78 CFG_ENABLE_VEN_MSG_VEN_INV | \
79 CFG_ENABLE_VEN_MSG_VEN_ID)
81 /* Misc interrupt status mask bits */
82 #define MSGF_MISC_SR_RXMSG_AVAIL BIT(0)
83 #define MSGF_MISC_SR_RXMSG_OVER BIT(1)
84 #define MSGF_MISC_SR_SLAVE_ERR BIT(4)
85 #define MSGF_MISC_SR_MASTER_ERR BIT(5)
86 #define MSGF_MISC_SR_I_ADDR_ERR BIT(6)
87 #define MSGF_MISC_SR_E_ADDR_ERR BIT(7)
88 #define MSGF_MISC_SR_FATAL_AER BIT(16)
89 #define MSGF_MISC_SR_NON_FATAL_AER BIT(17)
90 #define MSGF_MISC_SR_CORR_AER BIT(18)
91 #define MSGF_MISC_SR_UR_DETECT BIT(20)
92 #define MSGF_MISC_SR_NON_FATAL_DEV BIT(22)
93 #define MSGF_MISC_SR_FATAL_DEV BIT(23)
94 #define MSGF_MISC_SR_LINK_DOWN BIT(24)
95 #define MSGF_MSIC_SR_LINK_AUTO_BWIDTH BIT(25)
96 #define MSGF_MSIC_SR_LINK_BWIDTH BIT(26)
98 #define MSGF_MISC_SR_MASKALL (MSGF_MISC_SR_RXMSG_AVAIL | \
99 MSGF_MISC_SR_RXMSG_OVER | \
100 MSGF_MISC_SR_SLAVE_ERR | \
101 MSGF_MISC_SR_MASTER_ERR | \
102 MSGF_MISC_SR_I_ADDR_ERR | \
103 MSGF_MISC_SR_E_ADDR_ERR | \
104 MSGF_MISC_SR_FATAL_AER | \
105 MSGF_MISC_SR_NON_FATAL_AER | \
106 MSGF_MISC_SR_CORR_AER | \
107 MSGF_MISC_SR_UR_DETECT | \
108 MSGF_MISC_SR_NON_FATAL_DEV | \
109 MSGF_MISC_SR_FATAL_DEV | \
110 MSGF_MISC_SR_LINK_DOWN | \
111 MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
112 MSGF_MSIC_SR_LINK_BWIDTH)
114 /* Legacy interrupt status mask bits */
115 #define MSGF_LEG_SR_INTA BIT(0)
116 #define MSGF_LEG_SR_INTB BIT(1)
117 #define MSGF_LEG_SR_INTC BIT(2)
118 #define MSGF_LEG_SR_INTD BIT(3)
119 #define MSGF_LEG_SR_MASKALL (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
120 MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
122 /* MSI interrupt status mask bits */
123 #define MSGF_MSI_SR_LO_MASK GENMASK(31, 0)
124 #define MSGF_MSI_SR_HI_MASK GENMASK(31, 0)
126 #define MSII_PRESENT BIT(0)
127 #define MSII_ENABLE BIT(0)
128 #define MSII_STATUS_ENABLE BIT(15)
130 /* Bridge config interrupt mask */
131 #define BRCFG_INTERRUPT_MASK BIT(0)
132 #define BREG_PRESENT BIT(0)
133 #define BREG_ENABLE BIT(0)
134 #define BREG_ENABLE_FORCE BIT(1)
136 /* E_ECAM status mask bits */
137 #define E_ECAM_PRESENT BIT(0)
138 #define E_ECAM_CR_ENABLE BIT(0)
139 #define E_ECAM_SIZE_LOC GENMASK(20, 16)
140 #define E_ECAM_SIZE_SHIFT 16
141 #define ECAM_BUS_LOC_SHIFT 20
142 #define ECAM_DEV_LOC_SHIFT 12
143 #define NWL_ECAM_VALUE_DEFAULT 12
145 #define CFG_DMA_REG_BAR GENMASK(2, 0)
147 #define INT_PCI_MSI_NR (2 * 32)
150 /* Readin the PS_LINKUP */
151 #define PS_LINKUP_OFFSET 0x00000238
152 #define PCIE_PHY_LINKUP_BIT BIT(0)
153 #define PHY_RDY_LINKUP_BIT BIT(1)
155 /* Parameters for the waiting for link up routine */
156 #define LINK_WAIT_MAX_RETRIES 10
157 #define LINK_WAIT_USLEEP_MIN 90000
158 #define LINK_WAIT_USLEEP_MAX 100000
160 struct nwl_msi { /* MSI information */
161 struct irq_domain *msi_domain;
162 unsigned long *bitmap;
163 struct irq_domain *dev_domain;
164 struct mutex lock; /* protect bitmap variable */
171 void __iomem *breg_base;
172 void __iomem *pcireg_base;
173 void __iomem *ecam_base;
174 phys_addr_t phys_breg_base; /* Physical Bridge Register Base */
175 phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
176 phys_addr_t phys_ecam_base; /* Physical Configuration Base */
186 struct irq_domain *legacy_irq_domain;
189 static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
191 return readl(pcie->breg_base + off);
194 static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
196 writel(val, pcie->breg_base + off);
199 static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
201 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
206 static bool nwl_phy_link_up(struct nwl_pcie *pcie)
208 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
213 static int nwl_wait_for_link(struct nwl_pcie *pcie)
217 /* check if the link is up or not */
218 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
219 if (nwl_phy_link_up(pcie))
221 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
224 dev_err(pcie->dev, "PHY link never came up\n");
228 static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
230 struct nwl_pcie *pcie = bus->sysdata;
232 /* Check link before accessing downstream ports */
233 if (bus->number != pcie->root_busno) {
234 if (!nwl_pcie_link_up(pcie))
238 /* Only one device down on each root port */
239 if (bus->number == pcie->root_busno && devfn > 0)
246 * nwl_pcie_map_bus - Get configuration base
248 * @bus: Bus structure of current bus
249 * @devfn: Device/function
250 * @where: Offset from base
252 * Return: Base address of the configuration space needed to be
255 static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
258 struct nwl_pcie *pcie = bus->sysdata;
261 if (!nwl_pcie_valid_device(bus, devfn))
264 relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
265 (devfn << ECAM_DEV_LOC_SHIFT);
267 return pcie->ecam_base + relbus + where;
270 /* PCIe operations */
271 static struct pci_ops nwl_pcie_ops = {
272 .map_bus = nwl_pcie_map_bus,
273 .read = pci_generic_config_read,
274 .write = pci_generic_config_write,
277 static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
279 struct nwl_pcie *pcie = data;
282 /* Checking for misc interrupts */
283 misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
284 MSGF_MISC_SR_MASKALL;
288 if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
289 dev_err(pcie->dev, "Received Message FIFO Overflow\n");
291 if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
292 dev_err(pcie->dev, "Slave error\n");
294 if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
295 dev_err(pcie->dev, "Master error\n");
297 if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
299 "In Misc Ingress address translation error\n");
301 if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
303 "In Misc Egress address translation error\n");
305 if (misc_stat & MSGF_MISC_SR_FATAL_AER)
306 dev_err(pcie->dev, "Fatal Error in AER Capability\n");
308 if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
309 dev_err(pcie->dev, "Non-Fatal Error in AER Capability\n");
311 if (misc_stat & MSGF_MISC_SR_CORR_AER)
312 dev_err(pcie->dev, "Correctable Error in AER Capability\n");
314 if (misc_stat & MSGF_MISC_SR_UR_DETECT)
315 dev_err(pcie->dev, "Unsupported request Detected\n");
317 if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
318 dev_err(pcie->dev, "Non-Fatal Error Detected\n");
320 if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
321 dev_err(pcie->dev, "Fatal Error Detected\n");
323 if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
324 dev_info(pcie->dev, "Link Autonomous Bandwidth Management Status bit set\n");
326 if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
327 dev_info(pcie->dev, "Link Bandwidth Management Status bit set\n");
329 /* Clear misc interrupt status */
330 nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
335 static void nwl_pcie_leg_handler(struct irq_desc *desc)
337 struct irq_chip *chip = irq_desc_get_chip(desc);
338 struct nwl_pcie *pcie;
339 unsigned long status;
343 chained_irq_enter(chip, desc);
344 pcie = irq_desc_get_handler_data(desc);
346 while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
347 MSGF_LEG_SR_MASKALL) != 0) {
348 for_each_set_bit(bit, &status, INTX_NUM) {
349 virq = irq_find_mapping(pcie->legacy_irq_domain,
352 generic_handle_irq(virq);
356 chained_irq_exit(chip, desc);
359 static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
362 unsigned long status;
368 while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
369 for_each_set_bit(bit, &status, 32) {
370 nwl_bridge_writel(pcie, 1 << bit, status_reg);
371 virq = irq_find_mapping(msi->dev_domain, bit);
373 generic_handle_irq(virq);
378 static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
380 struct irq_chip *chip = irq_desc_get_chip(desc);
381 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
383 chained_irq_enter(chip, desc);
384 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
385 chained_irq_exit(chip, desc);
388 static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
390 struct irq_chip *chip = irq_desc_get_chip(desc);
391 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
393 chained_irq_enter(chip, desc);
394 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
395 chained_irq_exit(chip, desc);
398 static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
399 irq_hw_number_t hwirq)
401 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
402 irq_set_chip_data(irq, domain->host_data);
407 static const struct irq_domain_ops legacy_domain_ops = {
408 .map = nwl_legacy_map,
411 #ifdef CONFIG_PCI_MSI
412 static struct irq_chip nwl_msi_irq_chip = {
413 .name = "nwl_pcie:msi",
414 .irq_enable = unmask_msi_irq,
415 .irq_disable = mask_msi_irq,
416 .irq_mask = mask_msi_irq,
417 .irq_unmask = unmask_msi_irq,
421 static struct msi_domain_info nwl_msi_domain_info = {
422 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
423 MSI_FLAG_MULTI_PCI_MSI),
424 .chip = &nwl_msi_irq_chip,
428 static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
430 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
431 phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
433 msg->address_lo = lower_32_bits(msi_addr);
434 msg->address_hi = upper_32_bits(msi_addr);
435 msg->data = data->hwirq;
438 static int nwl_msi_set_affinity(struct irq_data *irq_data,
439 const struct cpumask *mask, bool force)
444 static struct irq_chip nwl_irq_chip = {
445 .name = "Xilinx MSI",
446 .irq_compose_msi_msg = nwl_compose_msi_msg,
447 .irq_set_affinity = nwl_msi_set_affinity,
450 static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
451 unsigned int nr_irqs, void *args)
453 struct nwl_pcie *pcie = domain->host_data;
454 struct nwl_msi *msi = &pcie->msi;
458 mutex_lock(&msi->lock);
459 bit = bitmap_find_next_zero_area(msi->bitmap, INT_PCI_MSI_NR, 0,
461 if (bit >= INT_PCI_MSI_NR) {
462 mutex_unlock(&msi->lock);
466 bitmap_set(msi->bitmap, bit, nr_irqs);
468 for (i = 0; i < nr_irqs; i++) {
469 irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
470 domain->host_data, handle_simple_irq,
473 mutex_unlock(&msi->lock);
477 static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
478 unsigned int nr_irqs)
480 struct irq_data *data = irq_domain_get_irq_data(domain, virq);
481 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
482 struct nwl_msi *msi = &pcie->msi;
484 mutex_lock(&msi->lock);
485 bitmap_clear(msi->bitmap, data->hwirq, nr_irqs);
486 mutex_unlock(&msi->lock);
489 static const struct irq_domain_ops dev_msi_domain_ops = {
490 .alloc = nwl_irq_domain_alloc,
491 .free = nwl_irq_domain_free,
494 static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
496 #ifdef CONFIG_PCI_MSI
497 struct fwnode_handle *fwnode = of_node_to_fwnode(pcie->dev->of_node);
498 struct nwl_msi *msi = &pcie->msi;
500 msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
501 &dev_msi_domain_ops, pcie);
502 if (!msi->dev_domain) {
503 dev_err(pcie->dev, "failed to create dev IRQ domain\n");
506 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
507 &nwl_msi_domain_info,
509 if (!msi->msi_domain) {
510 dev_err(pcie->dev, "failed to create msi IRQ domain\n");
511 irq_domain_remove(msi->dev_domain);
518 static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
520 struct device_node *node = pcie->dev->of_node;
521 struct device_node *legacy_intc_node;
523 legacy_intc_node = of_get_next_child(node, NULL);
524 if (!legacy_intc_node) {
525 dev_err(pcie->dev, "No legacy intc node found\n");
529 pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
534 if (!pcie->legacy_irq_domain) {
535 dev_err(pcie->dev, "failed to create IRQ domain\n");
539 nwl_pcie_init_msi_irq_domain(pcie);
543 static int nwl_pcie_enable_msi(struct nwl_pcie *pcie, struct pci_bus *bus)
545 struct platform_device *pdev = to_platform_device(pcie->dev);
546 struct nwl_msi *msi = &pcie->msi;
549 int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
551 mutex_init(&msi->lock);
553 msi->bitmap = kzalloc(size, GFP_KERNEL);
557 /* Get msi_1 IRQ number */
558 msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
559 if (msi->irq_msi1 < 0) {
560 dev_err(&pdev->dev, "failed to get IRQ#%d\n", msi->irq_msi1);
565 irq_set_chained_handler_and_data(msi->irq_msi1,
566 nwl_pcie_msi_handler_high, pcie);
568 /* Get msi_0 IRQ number */
569 msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
570 if (msi->irq_msi0 < 0) {
571 dev_err(&pdev->dev, "failed to get IRQ#%d\n", msi->irq_msi0);
576 irq_set_chained_handler_and_data(msi->irq_msi0,
577 nwl_pcie_msi_handler_low, pcie);
579 /* Check for msii_present bit */
580 ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
582 dev_err(pcie->dev, "MSI not present\n");
588 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
589 MSII_ENABLE, I_MSII_CONTROL);
591 /* Enable MSII status */
592 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
593 MSII_STATUS_ENABLE, I_MSII_CONTROL);
595 /* setup AFI/FPCI range */
596 base = pcie->phys_pcie_reg_base;
597 nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
598 nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
601 * For high range MSI interrupts: disable, clear any pending,
604 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
606 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
607 MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
609 nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
612 * For low range MSI interrupts: disable, clear any pending,
615 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
617 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
618 MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
620 nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
629 static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
631 struct platform_device *pdev = to_platform_device(pcie->dev);
632 u32 breg_val, ecam_val, first_busno = 0;
635 breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
637 dev_err(pcie->dev, "BREG is not present\n");
641 /* Write bridge_off to breg base */
642 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
644 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
648 nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
651 /* Disable DMA channel registers */
652 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
653 CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
655 /* Enable Ingress subtractive decode translation */
656 nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
658 /* Enable msg filtering details */
659 nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
660 BRCFG_PCIE_RX_MSG_FILTER);
662 err = nwl_wait_for_link(pcie);
666 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
668 dev_err(pcie->dev, "ECAM is not present\n");
673 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
674 E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
676 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
677 (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
680 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
682 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
686 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
687 pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
688 /* Write primary, secondary and subordinate bus numbers */
689 ecam_val = first_busno;
690 ecam_val |= (first_busno + 1) << 8;
691 ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
692 writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
694 if (nwl_pcie_link_up(pcie))
695 dev_info(pcie->dev, "Link is UP\n");
697 dev_info(pcie->dev, "Link is DOWN\n");
699 /* Get misc IRQ number */
700 pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
701 if (pcie->irq_misc < 0) {
702 dev_err(&pdev->dev, "failed to get misc IRQ %d\n",
707 err = devm_request_irq(pcie->dev, pcie->irq_misc,
708 nwl_pcie_misc_handler, IRQF_SHARED,
709 "nwl_pcie:misc", pcie);
711 dev_err(pcie->dev, "fail to register misc IRQ#%d\n",
716 /* Disable all misc interrupts */
717 nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
719 /* Clear pending misc interrupts */
720 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
721 MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
723 /* Enable all misc interrupts */
724 nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
727 /* Disable all legacy interrupts */
728 nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
730 /* Clear pending legacy interrupts */
731 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
732 MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
734 /* Enable all legacy interrupts */
735 nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
737 /* Enable the bridge config interrupt */
738 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
739 BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
744 static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
745 struct platform_device *pdev)
747 struct device_node *node = pcie->dev->of_node;
748 struct resource *res;
751 /* Check for device type */
752 type = of_get_property(node, "device_type", NULL);
753 if (!type || strcmp(type, "pci")) {
754 dev_err(pcie->dev, "invalid \"device_type\" %s\n", type);
758 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
759 pcie->breg_base = devm_ioremap_resource(pcie->dev, res);
760 if (IS_ERR(pcie->breg_base))
761 return PTR_ERR(pcie->breg_base);
762 pcie->phys_breg_base = res->start;
764 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
765 pcie->pcireg_base = devm_ioremap_resource(pcie->dev, res);
766 if (IS_ERR(pcie->pcireg_base))
767 return PTR_ERR(pcie->pcireg_base);
768 pcie->phys_pcie_reg_base = res->start;
770 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
771 pcie->ecam_base = devm_ioremap_resource(pcie->dev, res);
772 if (IS_ERR(pcie->ecam_base))
773 return PTR_ERR(pcie->ecam_base);
774 pcie->phys_ecam_base = res->start;
776 /* Get intx IRQ number */
777 pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
778 if (pcie->irq_intx < 0) {
779 dev_err(&pdev->dev, "failed to get intx IRQ %d\n",
784 irq_set_chained_handler_and_data(pcie->irq_intx,
785 nwl_pcie_leg_handler, pcie);
790 static const struct of_device_id nwl_pcie_of_match[] = {
791 { .compatible = "xlnx,nwl-pcie-2.11", },
795 static int nwl_pcie_probe(struct platform_device *pdev)
797 struct device_node *node = pdev->dev.of_node;
798 struct nwl_pcie *pcie;
800 struct pci_bus *child;
802 resource_size_t iobase = 0;
805 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
809 pcie->dev = &pdev->dev;
810 pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
812 err = nwl_pcie_parse_dt(pcie, pdev);
814 dev_err(pcie->dev, "Parsing DT failed\n");
818 err = nwl_pcie_bridge_init(pcie);
820 dev_err(pcie->dev, "HW Initialization failed\n");
824 err = of_pci_get_host_bridge_resources(node, 0, 0xff, &res, &iobase);
826 dev_err(pcie->dev, "Getting bridge resources failed\n");
830 err = devm_request_pci_bus_resources(pcie->dev, &res);
834 err = nwl_pcie_init_irq_domain(pcie);
836 dev_err(pcie->dev, "Failed creating IRQ Domain\n");
840 bus = pci_create_root_bus(&pdev->dev, pcie->root_busno,
841 &nwl_pcie_ops, pcie, &res);
847 if (IS_ENABLED(CONFIG_PCI_MSI)) {
848 err = nwl_pcie_enable_msi(pcie, bus);
851 "failed to enable MSI support: %d\n", err);
855 pci_scan_child_bus(bus);
856 pci_assign_unassigned_bus_resources(bus);
857 list_for_each_entry(child, &bus->children, node)
858 pcie_bus_configure_settings(child);
859 pci_bus_add_devices(bus);
860 platform_set_drvdata(pdev, pcie);
864 pci_free_resource_list(&res);
868 static struct platform_driver nwl_pcie_driver = {
871 .suppress_bind_attrs = true,
872 .of_match_table = nwl_pcie_of_match,
874 .probe = nwl_pcie_probe,
876 builtin_platform_driver(nwl_pcie_driver);