blob: dfdf58f20b199380bf3c35867b02a560fabcc2d0 [file] [log] [blame]
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +05301/*
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/module.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_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)
80
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)
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +053088#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)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053097
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 | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530104 MSGF_MISC_SR_FATAL_AER | \
105 MSGF_MISC_SR_NON_FATAL_AER | \
106 MSGF_MISC_SR_CORR_AER | \
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530107 MSGF_MISC_SR_UR_DETECT | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530108 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)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530113
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)
121
122/* MSI interrupt status mask bits */
123#define MSGF_MSI_SR_LO_MASK BIT(0)
124#define MSGF_MSI_SR_HI_MASK BIT(0)
125
126#define MSII_PRESENT BIT(0)
127#define MSII_ENABLE BIT(0)
128#define MSII_STATUS_ENABLE BIT(15)
129
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)
135
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
144
145#define CFG_DMA_REG_BAR GENMASK(2, 0)
146
147#define INT_PCI_MSI_NR (2 * 32)
148#define INTX_NUM 4
149
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)
154
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
159
160struct 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 */
165 int irq_msi0;
166 int irq_msi1;
167};
168
169struct nwl_pcie {
170 struct device *dev;
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 */
177 u32 breg_size;
178 u32 pcie_reg_size;
179 u32 ecam_size;
180 int irq_intx;
181 int irq_misc;
182 u32 ecam_value;
183 u8 last_busno;
184 u8 root_busno;
185 struct nwl_msi msi;
186 struct irq_domain *legacy_irq_domain;
187};
188
189static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
190{
191 return readl(pcie->breg_base + off);
192}
193
194static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
195{
196 writel(val, pcie->breg_base + off);
197}
198
199static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
200{
201 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
202 return true;
203 return false;
204}
205
206static bool nwl_phy_link_up(struct nwl_pcie *pcie)
207{
208 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
209 return true;
210 return false;
211}
212
213static int nwl_wait_for_link(struct nwl_pcie *pcie)
214{
215 int retries;
216
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))
220 return 0;
221 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
222 }
223
224 dev_err(pcie->dev, "PHY link never came up\n");
225 return -ETIMEDOUT;
226}
227
228static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
229{
230 struct nwl_pcie *pcie = bus->sysdata;
231
232 /* Check link before accessing downstream ports */
233 if (bus->number != pcie->root_busno) {
234 if (!nwl_pcie_link_up(pcie))
235 return false;
236 }
237
238 /* Only one device down on each root port */
239 if (bus->number == pcie->root_busno && devfn > 0)
240 return false;
241
242 return true;
243}
244
245/**
246 * nwl_pcie_map_bus - Get configuration base
247 *
248 * @bus: Bus structure of current bus
249 * @devfn: Device/function
250 * @where: Offset from base
251 *
252 * Return: Base address of the configuration space needed to be
253 * accessed.
254 */
255static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
256 int where)
257{
258 struct nwl_pcie *pcie = bus->sysdata;
259 int relbus;
260
261 if (!nwl_pcie_valid_device(bus, devfn))
262 return NULL;
263
264 relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
265 (devfn << ECAM_DEV_LOC_SHIFT);
266
267 return pcie->ecam_base + relbus + where;
268}
269
270/* PCIe operations */
271static 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,
275};
276
277static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
278{
279 struct nwl_pcie *pcie = data;
280 u32 misc_stat;
281
282 /* Checking for misc interrupts */
283 misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
284 MSGF_MISC_SR_MASKALL;
285 if (!misc_stat)
286 return IRQ_NONE;
287
288 if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
289 dev_err(pcie->dev, "Received Message FIFO Overflow\n");
290
291 if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
292 dev_err(pcie->dev, "Slave error\n");
293
294 if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
295 dev_err(pcie->dev, "Master error\n");
296
297 if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
298 dev_err(pcie->dev,
299 "In Misc Ingress address translation error\n");
300
301 if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
302 dev_err(pcie->dev,
303 "In Misc Egress address translation error\n");
304
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530305 if (misc_stat & MSGF_MISC_SR_FATAL_AER)
306 dev_err(pcie->dev, "Fatal Error in AER Capability\n");
307
308 if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
309 dev_err(pcie->dev, "Non-Fatal Error in AER Capability\n");
310
311 if (misc_stat & MSGF_MISC_SR_CORR_AER)
312 dev_err(pcie->dev, "Correctable Error in AER Capability\n");
313
314 if (misc_stat & MSGF_MISC_SR_UR_DETECT)
315 dev_err(pcie->dev, "Unsupported request Detected\n");
316
317 if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
318 dev_err(pcie->dev, "Non-Fatal Error Detected\n");
319
320 if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
321 dev_err(pcie->dev, "Fatal Error Detected\n");
322
323 if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
324 dev_info(pcie->dev, "Link Autonomous Bandwidth Management Status bit set\n");
325
326 if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
327 dev_info(pcie->dev, "Link Bandwidth Management Status bit set\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530328
329 /* Clear misc interrupt status */
330 nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
331
332 return IRQ_HANDLED;
333}
334
335static void nwl_pcie_leg_handler(struct irq_desc *desc)
336{
337 struct irq_chip *chip = irq_desc_get_chip(desc);
338 struct nwl_pcie *pcie;
339 unsigned long status;
340 u32 bit;
341 u32 virq;
342
343 chained_irq_enter(chip, desc);
344 pcie = irq_desc_get_handler_data(desc);
345
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,
350 bit + 1);
351 if (virq)
352 generic_handle_irq(virq);
353 }
354 }
355
356 chained_irq_exit(chip, desc);
357}
358
359static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
360{
361 struct nwl_msi *msi;
362 unsigned long status;
363 u32 bit;
364 u32 virq;
365
366 msi = &pcie->msi;
367
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);
372 if (virq)
373 generic_handle_irq(virq);
374 }
375 }
376}
377
378static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
379{
380 struct irq_chip *chip = irq_desc_get_chip(desc);
381 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
382
383 chained_irq_enter(chip, desc);
384 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
385 chained_irq_exit(chip, desc);
386}
387
388static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
389{
390 struct irq_chip *chip = irq_desc_get_chip(desc);
391 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
392
393 chained_irq_enter(chip, desc);
394 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
395 chained_irq_exit(chip, desc);
396}
397
398static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
399 irq_hw_number_t hwirq)
400{
401 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
402 irq_set_chip_data(irq, domain->host_data);
403
404 return 0;
405}
406
407static const struct irq_domain_ops legacy_domain_ops = {
408 .map = nwl_legacy_map,
409};
410
411#ifdef CONFIG_PCI_MSI
412static 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,
418
419};
420
421static 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,
425};
426#endif
427
428static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
429{
430 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
431 phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
432
433 msg->address_lo = lower_32_bits(msi_addr);
434 msg->address_hi = upper_32_bits(msi_addr);
435 msg->data = data->hwirq;
436}
437
438static int nwl_msi_set_affinity(struct irq_data *irq_data,
439 const struct cpumask *mask, bool force)
440{
441 return -EINVAL;
442}
443
444static 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,
448};
449
450static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
451 unsigned int nr_irqs, void *args)
452{
453 struct nwl_pcie *pcie = domain->host_data;
454 struct nwl_msi *msi = &pcie->msi;
455 int bit;
456 int i;
457
458 mutex_lock(&msi->lock);
459 bit = bitmap_find_next_zero_area(msi->bitmap, INT_PCI_MSI_NR, 0,
460 nr_irqs, 0);
461 if (bit >= INT_PCI_MSI_NR) {
462 mutex_unlock(&msi->lock);
463 return -ENOSPC;
464 }
465
466 bitmap_set(msi->bitmap, bit, nr_irqs);
467
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,
471 NULL, NULL);
472 }
473 mutex_unlock(&msi->lock);
474 return 0;
475}
476
477static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
478 unsigned int nr_irqs)
479{
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;
483
484 mutex_lock(&msi->lock);
485 bitmap_clear(msi->bitmap, data->hwirq, nr_irqs);
486 mutex_unlock(&msi->lock);
487}
488
489static const struct irq_domain_ops dev_msi_domain_ops = {
490 .alloc = nwl_irq_domain_alloc,
491 .free = nwl_irq_domain_free,
492};
493
494static void nwl_msi_free_irq_domain(struct nwl_pcie *pcie)
495{
496 struct nwl_msi *msi = &pcie->msi;
497
498 if (msi->irq_msi0)
499 irq_set_chained_handler_and_data(msi->irq_msi0, NULL, NULL);
500 if (msi->irq_msi1)
501 irq_set_chained_handler_and_data(msi->irq_msi1, NULL, NULL);
502
503 if (msi->msi_domain)
504 irq_domain_remove(msi->msi_domain);
505 if (msi->dev_domain)
506 irq_domain_remove(msi->dev_domain);
507
508 kfree(msi->bitmap);
509 msi->bitmap = NULL;
510}
511
512static void nwl_pcie_free_irq_domain(struct nwl_pcie *pcie)
513{
514 int i;
515 u32 irq;
516
517 for (i = 0; i < INTX_NUM; i++) {
518 irq = irq_find_mapping(pcie->legacy_irq_domain, i + 1);
519 if (irq > 0)
520 irq_dispose_mapping(irq);
521 }
522 if (pcie->legacy_irq_domain)
523 irq_domain_remove(pcie->legacy_irq_domain);
524
525 nwl_msi_free_irq_domain(pcie);
526}
527
528static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
529{
530#ifdef CONFIG_PCI_MSI
531 struct fwnode_handle *fwnode = of_node_to_fwnode(pcie->dev->of_node);
532 struct nwl_msi *msi = &pcie->msi;
533
534 msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
535 &dev_msi_domain_ops, pcie);
536 if (!msi->dev_domain) {
537 dev_err(pcie->dev, "failed to create dev IRQ domain\n");
538 return -ENOMEM;
539 }
540 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
541 &nwl_msi_domain_info,
542 msi->dev_domain);
543 if (!msi->msi_domain) {
544 dev_err(pcie->dev, "failed to create msi IRQ domain\n");
545 irq_domain_remove(msi->dev_domain);
546 return -ENOMEM;
547 }
548#endif
549 return 0;
550}
551
552static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
553{
554 struct device_node *node = pcie->dev->of_node;
555 struct device_node *legacy_intc_node;
556
557 legacy_intc_node = of_get_next_child(node, NULL);
558 if (!legacy_intc_node) {
559 dev_err(pcie->dev, "No legacy intc node found\n");
560 return -EINVAL;
561 }
562
563 pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
564 INTX_NUM,
565 &legacy_domain_ops,
566 pcie);
567
568 if (!pcie->legacy_irq_domain) {
569 dev_err(pcie->dev, "failed to create IRQ domain\n");
570 return -ENOMEM;
571 }
572
573 nwl_pcie_init_msi_irq_domain(pcie);
574 return 0;
575}
576
577static int nwl_pcie_enable_msi(struct nwl_pcie *pcie, struct pci_bus *bus)
578{
579 struct platform_device *pdev = to_platform_device(pcie->dev);
580 struct nwl_msi *msi = &pcie->msi;
581 unsigned long base;
582 int ret;
583 int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
584
585 mutex_init(&msi->lock);
586
587 msi->bitmap = kzalloc(size, GFP_KERNEL);
588 if (!msi->bitmap)
589 return -ENOMEM;
590
591 /* Get msi_1 IRQ number */
592 msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
593 if (msi->irq_msi1 < 0) {
594 dev_err(&pdev->dev, "failed to get IRQ#%d\n", msi->irq_msi1);
595 ret = -EINVAL;
596 goto err;
597 }
598
599 irq_set_chained_handler_and_data(msi->irq_msi1,
600 nwl_pcie_msi_handler_high, pcie);
601
602 /* Get msi_0 IRQ number */
603 msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
604 if (msi->irq_msi0 < 0) {
605 dev_err(&pdev->dev, "failed to get IRQ#%d\n", msi->irq_msi0);
606 ret = -EINVAL;
607 goto err;
608 }
609
610 irq_set_chained_handler_and_data(msi->irq_msi0,
611 nwl_pcie_msi_handler_low, pcie);
612
613 /* Check for msii_present bit */
614 ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
615 if (!ret) {
616 dev_err(pcie->dev, "MSI not present\n");
617 ret = -EIO;
618 goto err;
619 }
620
621 /* Enable MSII */
622 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
623 MSII_ENABLE, I_MSII_CONTROL);
624
625 /* Enable MSII status */
626 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
627 MSII_STATUS_ENABLE, I_MSII_CONTROL);
628
629 /* setup AFI/FPCI range */
630 base = pcie->phys_pcie_reg_base;
631 nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
632 nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
633
634 /*
635 * For high range MSI interrupts: disable, clear any pending,
636 * and enable
637 */
638 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
639
640 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
641 MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
642
643 nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
644
645 /*
646 * For low range MSI interrupts: disable, clear any pending,
647 * and enable
648 */
649 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
650
651 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
652 MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
653
654 nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
655
656 return 0;
657err:
658 kfree(msi->bitmap);
659 msi->bitmap = NULL;
660 return ret;
661}
662
663static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
664{
665 struct platform_device *pdev = to_platform_device(pcie->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(pcie->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(pcie->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(pcie->dev, "Link is UP\n");
730 else
731 dev_info(pcie->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(&pdev->dev, "failed to get misc IRQ %d\n",
737 pcie->irq_misc);
738 return -EINVAL;
739 }
740
741 err = devm_request_irq(pcie->dev, pcie->irq_misc,
742 nwl_pcie_misc_handler, IRQF_SHARED,
743 "nwl_pcie:misc", pcie);
744 if (err) {
745 dev_err(pcie->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
778static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
779 struct platform_device *pdev)
780{
781 struct device_node *node = pcie->dev->of_node;
782 struct resource *res;
783 const char *type;
784
785 /* Check for device type */
786 type = of_get_property(node, "device_type", NULL);
787 if (!type || strcmp(type, "pci")) {
788 dev_err(pcie->dev, "invalid \"device_type\" %s\n", type);
789 return -EINVAL;
790 }
791
792 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
793 pcie->breg_base = devm_ioremap_resource(pcie->dev, res);
794 if (IS_ERR(pcie->breg_base))
795 return PTR_ERR(pcie->breg_base);
796 pcie->phys_breg_base = res->start;
797
798 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
799 pcie->pcireg_base = devm_ioremap_resource(pcie->dev, res);
800 if (IS_ERR(pcie->pcireg_base))
801 return PTR_ERR(pcie->pcireg_base);
802 pcie->phys_pcie_reg_base = res->start;
803
804 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
805 pcie->ecam_base = devm_ioremap_resource(pcie->dev, res);
806 if (IS_ERR(pcie->ecam_base))
807 return PTR_ERR(pcie->ecam_base);
808 pcie->phys_ecam_base = res->start;
809
810 /* Get intx IRQ number */
811 pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
812 if (pcie->irq_intx < 0) {
813 dev_err(&pdev->dev, "failed to get intx IRQ %d\n",
814 pcie->irq_intx);
815 return -EINVAL;
816 }
817
818 irq_set_chained_handler_and_data(pcie->irq_intx,
819 nwl_pcie_leg_handler, pcie);
820
821 return 0;
822}
823
824static const struct of_device_id nwl_pcie_of_match[] = {
825 { .compatible = "xlnx,nwl-pcie-2.11", },
826 {}
827};
828
829static int nwl_pcie_probe(struct platform_device *pdev)
830{
831 struct device_node *node = pdev->dev.of_node;
832 struct nwl_pcie *pcie;
833 struct pci_bus *bus;
834 struct pci_bus *child;
835 int err;
836 resource_size_t iobase = 0;
837 LIST_HEAD(res);
838
839 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
840 if (!pcie)
841 return -ENOMEM;
842
843 pcie->dev = &pdev->dev;
844 pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
845
846 err = nwl_pcie_parse_dt(pcie, pdev);
847 if (err) {
848 dev_err(pcie->dev, "Parsing DT failed\n");
849 return err;
850 }
851
852 err = nwl_pcie_bridge_init(pcie);
853 if (err) {
Colin Ian Kingdcbb4082016-04-05 12:12:45 -0500854 dev_err(pcie->dev, "HW Initialization failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530855 return err;
856 }
857
858 err = of_pci_get_host_bridge_resources(node, 0, 0xff, &res, &iobase);
859 if (err) {
Bjorn Helgaas9061f9b2016-05-28 18:26:01 -0500860 dev_err(pcie->dev, "Getting bridge resources failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530861 return err;
862 }
863
Bjorn Helgaas21f7fc22016-05-28 18:24:36 -0500864 err = devm_request_pci_bus_resources(pcie->dev, &res);
865 if (err)
866 goto error;
867
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530868 err = nwl_pcie_init_irq_domain(pcie);
869 if (err) {
870 dev_err(pcie->dev, "Failed creating IRQ Domain\n");
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500871 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530872 }
873
874 bus = pci_create_root_bus(&pdev->dev, pcie->root_busno,
875 &nwl_pcie_ops, pcie, &res);
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500876 if (!bus) {
877 err = -ENOMEM;
878 goto error;
879 }
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530880
881 if (IS_ENABLED(CONFIG_PCI_MSI)) {
882 err = nwl_pcie_enable_msi(pcie, bus);
883 if (err < 0) {
884 dev_err(&pdev->dev,
885 "failed to enable MSI support: %d\n", err);
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500886 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530887 }
888 }
889 pci_scan_child_bus(bus);
890 pci_assign_unassigned_bus_resources(bus);
891 list_for_each_entry(child, &bus->children, node)
892 pcie_bus_configure_settings(child);
893 pci_bus_add_devices(bus);
894 platform_set_drvdata(pdev, pcie);
895 return 0;
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500896
897error:
898 pci_free_resource_list(&res);
899 return err;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530900}
901
902static int nwl_pcie_remove(struct platform_device *pdev)
903{
904 struct nwl_pcie *pcie = platform_get_drvdata(pdev);
905
906 nwl_pcie_free_irq_domain(pcie);
907 platform_set_drvdata(pdev, NULL);
908 return 0;
909}
910
911static struct platform_driver nwl_pcie_driver = {
912 .driver = {
913 .name = "nwl-pcie",
914 .of_match_table = nwl_pcie_of_match,
915 },
916 .probe = nwl_pcie_probe,
917 .remove = nwl_pcie_remove,
918};
919module_platform_driver(nwl_pcie_driver);
920
921MODULE_AUTHOR("Xilinx, Inc");
922MODULE_DESCRIPTION("NWL PCIe driver");
923MODULE_LICENSE("GPL");