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