1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/irqdomain.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/msi.h>
23#include <linux/of_address.h>
24#include <linux/of_pci.h>
25#include <linux/of_platform.h>
26#include <linux/of_irq.h>
27#include <linux/pci.h>
28#include <linux/platform_device.h>
29
30
31#define XILINX_PCIE_REG_BIR 0x00000130
32#define XILINX_PCIE_REG_IDR 0x00000138
33#define XILINX_PCIE_REG_IMR 0x0000013c
34#define XILINX_PCIE_REG_PSCR 0x00000144
35#define XILINX_PCIE_REG_RPSC 0x00000148
36#define XILINX_PCIE_REG_MSIBASE1 0x0000014c
37#define XILINX_PCIE_REG_MSIBASE2 0x00000150
38#define XILINX_PCIE_REG_RPEFR 0x00000154
39#define XILINX_PCIE_REG_RPIFR1 0x00000158
40#define XILINX_PCIE_REG_RPIFR2 0x0000015c
41
42
43#define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
44#define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
45#define XILINX_PCIE_INTR_STR_ERR BIT(2)
46#define XILINX_PCIE_INTR_HOT_RESET BIT(3)
47#define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
48#define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
49#define XILINX_PCIE_INTR_NONFATAL BIT(10)
50#define XILINX_PCIE_INTR_FATAL BIT(11)
51#define XILINX_PCIE_INTR_INTX BIT(16)
52#define XILINX_PCIE_INTR_MSI BIT(17)
53#define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
54#define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
55#define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
56#define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
57#define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
58#define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
59#define XILINX_PCIE_INTR_MST_DECERR BIT(26)
60#define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
61#define XILINX_PCIE_INTR_MST_ERRP BIT(28)
62#define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
63#define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
64
65
66#define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
67#define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
68#define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
69
70
71#define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
72#define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
73#define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
74#define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
75#define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
76
77
78#define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
79#define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
80
81
82#define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
83
84
85#define XILINX_PCIE_REG_RPSC_BEN BIT(0)
86
87
88#define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
89
90
91#define ECAM_BUS_NUM_SHIFT 20
92#define ECAM_DEV_NUM_SHIFT 12
93
94
95#define XILINX_NUM_MSI_IRQS 128
96
97
98
99
100
101
102
103
104
105
106
107
108struct xilinx_pcie_port {
109 void __iomem *reg_base;
110 u32 irq;
111 unsigned long msi_pages;
112 u8 root_busno;
113 struct device *dev;
114 struct irq_domain *msi_domain;
115 struct irq_domain *leg_domain;
116 struct list_head resources;
117};
118
119static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
120
121static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
122{
123 return readl(port->reg_base + reg);
124}
125
126static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
127{
128 writel(val, port->reg_base + reg);
129}
130
131static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
132{
133 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
134 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
135}
136
137
138
139
140
141static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
142{
143 struct device *dev = port->dev;
144 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
145
146 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
147 dev_dbg(dev, "Requester ID %lu\n",
148 val & XILINX_PCIE_RPEFR_REQ_ID);
149 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
150 XILINX_PCIE_REG_RPEFR);
151 }
152}
153
154
155
156
157
158
159
160
161static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
162{
163 struct xilinx_pcie_port *port = bus->sysdata;
164
165
166 if (bus->number != port->root_busno)
167 if (!xilinx_pcie_link_is_up(port))
168 return false;
169
170
171 if (bus->number == port->root_busno && devfn > 0)
172 return false;
173
174 return true;
175}
176
177
178
179
180
181
182
183
184
185
186static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
187 unsigned int devfn, int where)
188{
189 struct xilinx_pcie_port *port = bus->sysdata;
190 int relbus;
191
192 if (!xilinx_pcie_valid_device(bus, devfn))
193 return NULL;
194
195 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
196 (devfn << ECAM_DEV_NUM_SHIFT);
197
198 return port->reg_base + relbus + where;
199}
200
201
202static struct pci_ops xilinx_pcie_ops = {
203 .map_bus = xilinx_pcie_map_bus,
204 .read = pci_generic_config_read,
205 .write = pci_generic_config_write,
206};
207
208
209
210
211
212
213
214static void xilinx_pcie_destroy_msi(unsigned int irq)
215{
216 struct msi_desc *msi;
217 struct xilinx_pcie_port *port;
218 struct irq_data *d = irq_get_irq_data(irq);
219 irq_hw_number_t hwirq = irqd_to_hwirq(d);
220
221 if (!test_bit(hwirq, msi_irq_in_use)) {
222 msi = irq_get_msi_desc(irq);
223 port = msi_desc_to_pci_sysdata(msi);
224 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
225 } else {
226 clear_bit(hwirq, msi_irq_in_use);
227 }
228}
229
230
231
232
233
234
235static int xilinx_pcie_assign_msi(void)
236{
237 int pos;
238
239 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
240 if (pos < XILINX_NUM_MSI_IRQS)
241 set_bit(pos, msi_irq_in_use);
242 else
243 return -ENOSPC;
244
245 return pos;
246}
247
248
249
250
251
252
253static void xilinx_msi_teardown_irq(struct msi_controller *chip,
254 unsigned int irq)
255{
256 xilinx_pcie_destroy_msi(irq);
257 irq_dispose_mapping(irq);
258}
259
260
261
262
263
264
265
266
267
268static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
269 struct pci_dev *pdev,
270 struct msi_desc *desc)
271{
272 struct xilinx_pcie_port *port = pdev->bus->sysdata;
273 unsigned int irq;
274 int hwirq;
275 struct msi_msg msg;
276 phys_addr_t msg_addr;
277
278 hwirq = xilinx_pcie_assign_msi();
279 if (hwirq < 0)
280 return hwirq;
281
282 irq = irq_create_mapping(port->msi_domain, hwirq);
283 if (!irq)
284 return -EINVAL;
285
286 irq_set_msi_desc(irq, desc);
287
288 msg_addr = virt_to_phys((void *)port->msi_pages);
289
290 msg.address_hi = 0;
291 msg.address_lo = msg_addr;
292 msg.data = irq;
293
294 pci_write_msi_msg(irq, &msg);
295
296 return 0;
297}
298
299
300static struct msi_controller xilinx_pcie_msi_chip = {
301 .setup_irq = xilinx_pcie_msi_setup_irq,
302 .teardown_irq = xilinx_msi_teardown_irq,
303};
304
305
306static struct irq_chip xilinx_msi_irq_chip = {
307 .name = "Xilinx PCIe MSI",
308 .irq_enable = pci_msi_unmask_irq,
309 .irq_disable = pci_msi_mask_irq,
310 .irq_mask = pci_msi_mask_irq,
311 .irq_unmask = pci_msi_unmask_irq,
312};
313
314
315
316
317
318
319
320
321
322static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
323 irq_hw_number_t hwirq)
324{
325 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
326 irq_set_chip_data(irq, domain->host_data);
327
328 return 0;
329}
330
331
332static const struct irq_domain_ops msi_domain_ops = {
333 .map = xilinx_pcie_msi_map,
334};
335
336
337
338
339
340static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
341{
342 phys_addr_t msg_addr;
343
344 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
345 msg_addr = virt_to_phys((void *)port->msi_pages);
346 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
347 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
348}
349
350
351
352
353
354
355
356
357
358
359
360static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
361 irq_hw_number_t hwirq)
362{
363 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
364 irq_set_chip_data(irq, domain->host_data);
365
366 return 0;
367}
368
369
370static const struct irq_domain_ops intx_domain_ops = {
371 .map = xilinx_pcie_intx_map,
372};
373
374
375
376
377
378
379
380
381
382
383static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
384{
385 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
386 struct device *dev = port->dev;
387 u32 val, mask, status, msi_data;
388
389
390 val = pcie_read(port, XILINX_PCIE_REG_IDR);
391 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
392
393 status = val & mask;
394 if (!status)
395 return IRQ_NONE;
396
397 if (status & XILINX_PCIE_INTR_LINK_DOWN)
398 dev_warn(dev, "Link Down\n");
399
400 if (status & XILINX_PCIE_INTR_ECRC_ERR)
401 dev_warn(dev, "ECRC failed\n");
402
403 if (status & XILINX_PCIE_INTR_STR_ERR)
404 dev_warn(dev, "Streaming error\n");
405
406 if (status & XILINX_PCIE_INTR_HOT_RESET)
407 dev_info(dev, "Hot reset\n");
408
409 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
410 dev_warn(dev, "ECAM access timeout\n");
411
412 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
413 dev_warn(dev, "Correctable error message\n");
414 xilinx_pcie_clear_err_interrupts(port);
415 }
416
417 if (status & XILINX_PCIE_INTR_NONFATAL) {
418 dev_warn(dev, "Non fatal error message\n");
419 xilinx_pcie_clear_err_interrupts(port);
420 }
421
422 if (status & XILINX_PCIE_INTR_FATAL) {
423 dev_warn(dev, "Fatal error message\n");
424 xilinx_pcie_clear_err_interrupts(port);
425 }
426
427 if (status & XILINX_PCIE_INTR_INTX) {
428
429 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
430
431
432 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
433 dev_warn(dev, "RP Intr FIFO1 read error\n");
434 goto error;
435 }
436
437 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
438
439 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
440 XILINX_PCIE_REG_RPIFR1);
441
442
443 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
444 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
445 generic_handle_irq(irq_find_mapping(port->leg_domain,
446 val));
447 }
448 }
449
450 if (status & XILINX_PCIE_INTR_MSI) {
451
452 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
453
454 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
455 dev_warn(dev, "RP Intr FIFO1 read error\n");
456 goto error;
457 }
458
459 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
460 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
461 XILINX_PCIE_RPIFR2_MSG_DATA;
462
463
464 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
465 XILINX_PCIE_REG_RPIFR1);
466
467 if (IS_ENABLED(CONFIG_PCI_MSI)) {
468
469 generic_handle_irq(msi_data);
470 }
471 }
472 }
473
474 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
475 dev_warn(dev, "Slave unsupported request\n");
476
477 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
478 dev_warn(dev, "Slave unexpected completion\n");
479
480 if (status & XILINX_PCIE_INTR_SLV_COMPL)
481 dev_warn(dev, "Slave completion timeout\n");
482
483 if (status & XILINX_PCIE_INTR_SLV_ERRP)
484 dev_warn(dev, "Slave Error Poison\n");
485
486 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
487 dev_warn(dev, "Slave Completer Abort\n");
488
489 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
490 dev_warn(dev, "Slave Illegal Burst\n");
491
492 if (status & XILINX_PCIE_INTR_MST_DECERR)
493 dev_warn(dev, "Master decode error\n");
494
495 if (status & XILINX_PCIE_INTR_MST_SLVERR)
496 dev_warn(dev, "Master slave error\n");
497
498 if (status & XILINX_PCIE_INTR_MST_ERRP)
499 dev_warn(dev, "Master error poison\n");
500
501error:
502
503 pcie_write(port, status, XILINX_PCIE_REG_IDR);
504
505 return IRQ_HANDLED;
506}
507
508
509
510
511
512
513
514static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
515{
516 struct device *dev = port->dev;
517 struct device_node *node = dev->of_node;
518 struct device_node *pcie_intc_node;
519
520
521 pcie_intc_node = of_get_next_child(node, NULL);
522 if (!pcie_intc_node) {
523 dev_err(dev, "No PCIe Intc node found\n");
524 return -ENODEV;
525 }
526
527 port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
528 &intx_domain_ops,
529 port);
530 if (!port->leg_domain) {
531 dev_err(dev, "Failed to get a INTx IRQ domain\n");
532 return -ENODEV;
533 }
534
535
536 if (IS_ENABLED(CONFIG_PCI_MSI)) {
537 port->msi_domain = irq_domain_add_linear(node,
538 XILINX_NUM_MSI_IRQS,
539 &msi_domain_ops,
540 &xilinx_pcie_msi_chip);
541 if (!port->msi_domain) {
542 dev_err(dev, "Failed to get a MSI IRQ domain\n");
543 return -ENODEV;
544 }
545
546 xilinx_pcie_enable_msi(port);
547 }
548
549 return 0;
550}
551
552
553
554
555
556static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
557{
558 struct device *dev = port->dev;
559
560 if (xilinx_pcie_link_is_up(port))
561 dev_info(dev, "PCIe Link is UP\n");
562 else
563 dev_info(dev, "PCIe Link is DOWN\n");
564
565
566 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
567 XILINX_PCIE_REG_IMR);
568
569
570 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
571 XILINX_PCIE_IMR_ALL_MASK,
572 XILINX_PCIE_REG_IDR);
573
574
575 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
576
577
578 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
579 XILINX_PCIE_REG_RPSC_BEN,
580 XILINX_PCIE_REG_RPSC);
581}
582
583
584
585
586
587
588
589static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
590{
591 struct device *dev = port->dev;
592 struct device_node *node = dev->of_node;
593 struct resource regs;
594 const char *type;
595 int err;
596
597 type = of_get_property(node, "device_type", NULL);
598 if (!type || strcmp(type, "pci")) {
599 dev_err(dev, "invalid \"device_type\" %s\n", type);
600 return -EINVAL;
601 }
602
603 err = of_address_to_resource(node, 0, ®s);
604 if (err) {
605 dev_err(dev, "missing \"reg\" property\n");
606 return err;
607 }
608
609 port->reg_base = devm_ioremap_resource(dev, ®s);
610 if (IS_ERR(port->reg_base))
611 return PTR_ERR(port->reg_base);
612
613 port->irq = irq_of_parse_and_map(node, 0);
614 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
615 IRQF_SHARED | IRQF_NO_THREAD,
616 "xilinx-pcie", port);
617 if (err) {
618 dev_err(dev, "unable to request irq %d\n", port->irq);
619 return err;
620 }
621
622 return 0;
623}
624
625
626
627
628
629
630
631static int xilinx_pcie_probe(struct platform_device *pdev)
632{
633 struct device *dev = &pdev->dev;
634 struct xilinx_pcie_port *port;
635 struct pci_bus *bus, *child;
636 int err;
637 resource_size_t iobase = 0;
638 LIST_HEAD(res);
639
640 if (!dev->of_node)
641 return -ENODEV;
642
643 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
644 if (!port)
645 return -ENOMEM;
646
647 port->dev = dev;
648
649 err = xilinx_pcie_parse_dt(port);
650 if (err) {
651 dev_err(dev, "Parsing DT failed\n");
652 return err;
653 }
654
655 xilinx_pcie_init_port(port);
656
657 err = xilinx_pcie_init_irq_domain(port);
658 if (err) {
659 dev_err(dev, "Failed creating IRQ Domain\n");
660 return err;
661 }
662
663 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
664 &iobase);
665 if (err) {
666 dev_err(dev, "Getting bridge resources failed\n");
667 return err;
668 }
669
670 err = devm_request_pci_bus_resources(dev, &res);
671 if (err)
672 goto error;
673
674 bus = pci_create_root_bus(dev, 0, &xilinx_pcie_ops, port, &res);
675 if (!bus) {
676 err = -ENOMEM;
677 goto error;
678 }
679
680#ifdef CONFIG_PCI_MSI
681 xilinx_pcie_msi_chip.dev = dev;
682 bus->msi = &xilinx_pcie_msi_chip;
683#endif
684 pci_scan_child_bus(bus);
685 pci_assign_unassigned_bus_resources(bus);
686#ifndef CONFIG_MICROBLAZE
687 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
688#endif
689 list_for_each_entry(child, &bus->children, node)
690 pcie_bus_configure_settings(child);
691 pci_bus_add_devices(bus);
692 return 0;
693
694error:
695 pci_free_resource_list(&res);
696 return err;
697}
698
699static struct of_device_id xilinx_pcie_of_match[] = {
700 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
701 {}
702};
703
704static struct platform_driver xilinx_pcie_driver = {
705 .driver = {
706 .name = "xilinx-pcie",
707 .of_match_table = xilinx_pcie_of_match,
708 .suppress_bind_attrs = true,
709 },
710 .probe = xilinx_pcie_probe,
711};
712builtin_platform_driver(xilinx_pcie_driver);
713