1 // SPDX-License-Identifier: GPL-2.0+
3 * PCIe host controller driver for Xilinx AXI PCIe Bridge
5 * Copyright (c) 2012 - 2014 Xilinx, Inc.
7 * Based on the Tegra PCIe driver
9 * Bits taken from Synopsys DesignWare Host controller driver and
10 * ARM PCI Host generic driver.
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/msi.h>
19 #include <linux/of_address.h>
20 #include <linux/of_pci.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_irq.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
26 /* Register definitions */
27 #define XILINX_PCIE_REG_BIR 0x00000130
28 #define XILINX_PCIE_REG_IDR 0x00000138
29 #define XILINX_PCIE_REG_IMR 0x0000013c
30 #define XILINX_PCIE_REG_PSCR 0x00000144
31 #define XILINX_PCIE_REG_RPSC 0x00000148
32 #define XILINX_PCIE_REG_MSIBASE1 0x0000014c
33 #define XILINX_PCIE_REG_MSIBASE2 0x00000150
34 #define XILINX_PCIE_REG_RPEFR 0x00000154
35 #define XILINX_PCIE_REG_RPIFR1 0x00000158
36 #define XILINX_PCIE_REG_RPIFR2 0x0000015c
38 /* Interrupt registers definitions */
39 #define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
40 #define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
41 #define XILINX_PCIE_INTR_STR_ERR BIT(2)
42 #define XILINX_PCIE_INTR_HOT_RESET BIT(3)
43 #define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
44 #define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
45 #define XILINX_PCIE_INTR_NONFATAL BIT(10)
46 #define XILINX_PCIE_INTR_FATAL BIT(11)
47 #define XILINX_PCIE_INTR_INTX BIT(16)
48 #define XILINX_PCIE_INTR_MSI BIT(17)
49 #define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
50 #define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
51 #define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
52 #define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
53 #define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
54 #define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
55 #define XILINX_PCIE_INTR_MST_DECERR BIT(26)
56 #define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
57 #define XILINX_PCIE_INTR_MST_ERRP BIT(28)
58 #define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
59 #define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
60 #define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
62 /* Root Port Error FIFO Read Register definitions */
63 #define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
64 #define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
65 #define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
67 /* Root Port Interrupt FIFO Read Register 1 definitions */
68 #define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
69 #define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
70 #define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
71 #define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
72 #define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
74 /* Bridge Info Register definitions */
75 #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
76 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
78 /* Root Port Interrupt FIFO Read Register 2 definitions */
79 #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
81 /* Root Port Status/control Register definitions */
82 #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
84 /* Phy Status/Control Register definitions */
85 #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
87 /* ECAM definitions */
88 #define ECAM_BUS_NUM_SHIFT 20
89 #define ECAM_DEV_NUM_SHIFT 12
91 /* Number of MSI IRQs */
92 #define XILINX_NUM_MSI_IRQS 128
95 * struct xilinx_pcie_port - PCIe port information
96 * @reg_base: IO Mapped Register Base
97 * @irq: Interrupt number
98 * @msi_pages: MSI pages
99 * @root_busno: Root Bus number
100 * @dev: Device pointer
101 * @msi_domain: MSI IRQ domain pointer
102 * @leg_domain: Legacy IRQ domain pointer
103 * @resources: Bus Resources
105 struct xilinx_pcie_port
{
106 void __iomem
*reg_base
;
108 unsigned long msi_pages
;
111 struct irq_domain
*msi_domain
;
112 struct irq_domain
*leg_domain
;
113 struct list_head resources
;
116 static DECLARE_BITMAP(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
118 static inline u32
pcie_read(struct xilinx_pcie_port
*port
, u32 reg
)
120 return readl(port
->reg_base
+ reg
);
123 static inline void pcie_write(struct xilinx_pcie_port
*port
, u32 val
, u32 reg
)
125 writel(val
, port
->reg_base
+ reg
);
128 static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port
*port
)
130 return (pcie_read(port
, XILINX_PCIE_REG_PSCR
) &
131 XILINX_PCIE_REG_PSCR_LNKUP
) ? 1 : 0;
135 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
136 * @port: PCIe port information
138 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port
*port
)
140 struct device
*dev
= port
->dev
;
141 unsigned long val
= pcie_read(port
, XILINX_PCIE_REG_RPEFR
);
143 if (val
& XILINX_PCIE_RPEFR_ERR_VALID
) {
144 dev_dbg(dev
, "Requester ID %lu\n",
145 val
& XILINX_PCIE_RPEFR_REQ_ID
);
146 pcie_write(port
, XILINX_PCIE_RPEFR_ALL_MASK
,
147 XILINX_PCIE_REG_RPEFR
);
152 * xilinx_pcie_valid_device - Check if a valid device is present on bus
153 * @bus: PCI Bus structure
154 * @devfn: device/function
156 * Return: 'true' on success and 'false' if invalid device is found
158 static bool xilinx_pcie_valid_device(struct pci_bus
*bus
, unsigned int devfn
)
160 struct xilinx_pcie_port
*port
= bus
->sysdata
;
162 /* Check if link is up when trying to access downstream ports */
163 if (bus
->number
!= port
->root_busno
)
164 if (!xilinx_pcie_link_up(port
))
167 /* Only one device down on each root port */
168 if (bus
->number
== port
->root_busno
&& devfn
> 0)
175 * xilinx_pcie_map_bus - Get configuration base
176 * @bus: PCI Bus structure
177 * @devfn: Device/function
178 * @where: Offset from base
180 * Return: Base address of the configuration space needed to be
183 static void __iomem
*xilinx_pcie_map_bus(struct pci_bus
*bus
,
184 unsigned int devfn
, int where
)
186 struct xilinx_pcie_port
*port
= bus
->sysdata
;
189 if (!xilinx_pcie_valid_device(bus
, devfn
))
192 relbus
= (bus
->number
<< ECAM_BUS_NUM_SHIFT
) |
193 (devfn
<< ECAM_DEV_NUM_SHIFT
);
195 return port
->reg_base
+ relbus
+ where
;
198 /* PCIe operations */
199 static struct pci_ops xilinx_pcie_ops
= {
200 .map_bus
= xilinx_pcie_map_bus
,
201 .read
= pci_generic_config_read
,
202 .write
= pci_generic_config_write
,
208 * xilinx_pcie_destroy_msi - Free MSI number
209 * @irq: IRQ to be freed
211 static void xilinx_pcie_destroy_msi(unsigned int irq
)
213 struct msi_desc
*msi
;
214 struct xilinx_pcie_port
*port
;
215 struct irq_data
*d
= irq_get_irq_data(irq
);
216 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
218 if (!test_bit(hwirq
, msi_irq_in_use
)) {
219 msi
= irq_get_msi_desc(irq
);
220 port
= msi_desc_to_pci_sysdata(msi
);
221 dev_err(port
->dev
, "Trying to free unused MSI#%d\n", irq
);
223 clear_bit(hwirq
, msi_irq_in_use
);
228 * xilinx_pcie_assign_msi - Allocate MSI number
230 * Return: A valid IRQ on success and error value on failure.
232 static int xilinx_pcie_assign_msi(void)
236 pos
= find_first_zero_bit(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
237 if (pos
< XILINX_NUM_MSI_IRQS
)
238 set_bit(pos
, msi_irq_in_use
);
246 * xilinx_msi_teardown_irq - Destroy the MSI
247 * @chip: MSI Chip descriptor
248 * @irq: MSI IRQ to destroy
250 static void xilinx_msi_teardown_irq(struct msi_controller
*chip
,
253 xilinx_pcie_destroy_msi(irq
);
254 irq_dispose_mapping(irq
);
258 * xilinx_pcie_msi_setup_irq - Setup MSI request
259 * @chip: MSI chip pointer
260 * @pdev: PCIe device pointer
261 * @desc: MSI descriptor pointer
263 * Return: '0' on success and error value on failure
265 static int xilinx_pcie_msi_setup_irq(struct msi_controller
*chip
,
266 struct pci_dev
*pdev
,
267 struct msi_desc
*desc
)
269 struct xilinx_pcie_port
*port
= pdev
->bus
->sysdata
;
273 phys_addr_t msg_addr
;
275 hwirq
= xilinx_pcie_assign_msi();
279 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
283 irq_set_msi_desc(irq
, desc
);
285 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
288 msg
.address_lo
= msg_addr
;
291 pci_write_msi_msg(irq
, &msg
);
296 /* MSI Chip Descriptor */
297 static struct msi_controller xilinx_pcie_msi_chip
= {
298 .setup_irq
= xilinx_pcie_msi_setup_irq
,
299 .teardown_irq
= xilinx_msi_teardown_irq
,
302 /* HW Interrupt Chip Descriptor */
303 static struct irq_chip xilinx_msi_irq_chip
= {
304 .name
= "Xilinx PCIe MSI",
305 .irq_enable
= pci_msi_unmask_irq
,
306 .irq_disable
= pci_msi_mask_irq
,
307 .irq_mask
= pci_msi_mask_irq
,
308 .irq_unmask
= pci_msi_unmask_irq
,
312 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
313 * @domain: IRQ domain
314 * @irq: Virtual IRQ number
315 * @hwirq: HW interrupt number
317 * Return: Always returns 0.
319 static int xilinx_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
320 irq_hw_number_t hwirq
)
322 irq_set_chip_and_handler(irq
, &xilinx_msi_irq_chip
, handle_simple_irq
);
323 irq_set_chip_data(irq
, domain
->host_data
);
328 /* IRQ Domain operations */
329 static const struct irq_domain_ops msi_domain_ops
= {
330 .map
= xilinx_pcie_msi_map
,
334 * xilinx_pcie_enable_msi - Enable MSI support
335 * @port: PCIe port information
337 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port
*port
)
339 phys_addr_t msg_addr
;
341 port
->msi_pages
= __get_free_pages(GFP_KERNEL
, 0);
342 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
343 pcie_write(port
, 0x0, XILINX_PCIE_REG_MSIBASE1
);
344 pcie_write(port
, msg_addr
, XILINX_PCIE_REG_MSIBASE2
);
350 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
351 * @domain: IRQ domain
352 * @irq: Virtual IRQ number
353 * @hwirq: HW interrupt number
355 * Return: Always returns 0.
357 static int xilinx_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
358 irq_hw_number_t hwirq
)
360 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
361 irq_set_chip_data(irq
, domain
->host_data
);
366 /* INTx IRQ Domain operations */
367 static const struct irq_domain_ops intx_domain_ops
= {
368 .map
= xilinx_pcie_intx_map
,
369 .xlate
= pci_irqd_intx_xlate
,
372 /* PCIe HW Functions */
375 * xilinx_pcie_intr_handler - Interrupt Service Handler
377 * @data: PCIe port information
379 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
381 static irqreturn_t
xilinx_pcie_intr_handler(int irq
, void *data
)
383 struct xilinx_pcie_port
*port
= (struct xilinx_pcie_port
*)data
;
384 struct device
*dev
= port
->dev
;
385 u32 val
, mask
, status
;
387 /* Read interrupt decode and mask registers */
388 val
= pcie_read(port
, XILINX_PCIE_REG_IDR
);
389 mask
= pcie_read(port
, XILINX_PCIE_REG_IMR
);
395 if (status
& XILINX_PCIE_INTR_LINK_DOWN
)
396 dev_warn(dev
, "Link Down\n");
398 if (status
& XILINX_PCIE_INTR_ECRC_ERR
)
399 dev_warn(dev
, "ECRC failed\n");
401 if (status
& XILINX_PCIE_INTR_STR_ERR
)
402 dev_warn(dev
, "Streaming error\n");
404 if (status
& XILINX_PCIE_INTR_HOT_RESET
)
405 dev_info(dev
, "Hot reset\n");
407 if (status
& XILINX_PCIE_INTR_CFG_TIMEOUT
)
408 dev_warn(dev
, "ECAM access timeout\n");
410 if (status
& XILINX_PCIE_INTR_CORRECTABLE
) {
411 dev_warn(dev
, "Correctable error message\n");
412 xilinx_pcie_clear_err_interrupts(port
);
415 if (status
& XILINX_PCIE_INTR_NONFATAL
) {
416 dev_warn(dev
, "Non fatal error message\n");
417 xilinx_pcie_clear_err_interrupts(port
);
420 if (status
& XILINX_PCIE_INTR_FATAL
) {
421 dev_warn(dev
, "Fatal error message\n");
422 xilinx_pcie_clear_err_interrupts(port
);
425 if (status
& (XILINX_PCIE_INTR_INTX
| XILINX_PCIE_INTR_MSI
)) {
426 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR1
);
428 /* Check whether interrupt valid */
429 if (!(val
& XILINX_PCIE_RPIFR1_INTR_VALID
)) {
430 dev_warn(dev
, "RP Intr FIFO1 read error\n");
434 /* Decode the IRQ number */
435 if (val
& XILINX_PCIE_RPIFR1_MSI_INTR
) {
436 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR2
) &
437 XILINX_PCIE_RPIFR2_MSG_DATA
;
439 val
= (val
& XILINX_PCIE_RPIFR1_INTR_MASK
) >>
440 XILINX_PCIE_RPIFR1_INTR_SHIFT
;
441 val
= irq_find_mapping(port
->leg_domain
, val
);
444 /* Clear interrupt FIFO register 1 */
445 pcie_write(port
, XILINX_PCIE_RPIFR1_ALL_MASK
,
446 XILINX_PCIE_REG_RPIFR1
);
448 /* Handle the interrupt */
449 if (IS_ENABLED(CONFIG_PCI_MSI
) ||
450 !(val
& XILINX_PCIE_RPIFR1_MSI_INTR
))
451 generic_handle_irq(val
);
454 if (status
& XILINX_PCIE_INTR_SLV_UNSUPP
)
455 dev_warn(dev
, "Slave unsupported request\n");
457 if (status
& XILINX_PCIE_INTR_SLV_UNEXP
)
458 dev_warn(dev
, "Slave unexpected completion\n");
460 if (status
& XILINX_PCIE_INTR_SLV_COMPL
)
461 dev_warn(dev
, "Slave completion timeout\n");
463 if (status
& XILINX_PCIE_INTR_SLV_ERRP
)
464 dev_warn(dev
, "Slave Error Poison\n");
466 if (status
& XILINX_PCIE_INTR_SLV_CMPABT
)
467 dev_warn(dev
, "Slave Completer Abort\n");
469 if (status
& XILINX_PCIE_INTR_SLV_ILLBUR
)
470 dev_warn(dev
, "Slave Illegal Burst\n");
472 if (status
& XILINX_PCIE_INTR_MST_DECERR
)
473 dev_warn(dev
, "Master decode error\n");
475 if (status
& XILINX_PCIE_INTR_MST_SLVERR
)
476 dev_warn(dev
, "Master slave error\n");
478 if (status
& XILINX_PCIE_INTR_MST_ERRP
)
479 dev_warn(dev
, "Master error poison\n");
482 /* Clear the Interrupt Decode register */
483 pcie_write(port
, status
, XILINX_PCIE_REG_IDR
);
489 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
490 * @port: PCIe port information
492 * Return: '0' on success and error value on failure
494 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port
*port
)
496 struct device
*dev
= port
->dev
;
497 struct device_node
*node
= dev
->of_node
;
498 struct device_node
*pcie_intc_node
;
501 pcie_intc_node
= of_get_next_child(node
, NULL
);
502 if (!pcie_intc_node
) {
503 dev_err(dev
, "No PCIe Intc node found\n");
507 port
->leg_domain
= irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
510 if (!port
->leg_domain
) {
511 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
516 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
517 port
->msi_domain
= irq_domain_add_linear(node
,
520 &xilinx_pcie_msi_chip
);
521 if (!port
->msi_domain
) {
522 dev_err(dev
, "Failed to get a MSI IRQ domain\n");
526 xilinx_pcie_enable_msi(port
);
533 * xilinx_pcie_init_port - Initialize hardware
534 * @port: PCIe port information
536 static void xilinx_pcie_init_port(struct xilinx_pcie_port
*port
)
538 struct device
*dev
= port
->dev
;
540 if (xilinx_pcie_link_up(port
))
541 dev_info(dev
, "PCIe Link is UP\n");
543 dev_info(dev
, "PCIe Link is DOWN\n");
545 /* Disable all interrupts */
546 pcie_write(port
, ~XILINX_PCIE_IDR_ALL_MASK
,
547 XILINX_PCIE_REG_IMR
);
549 /* Clear pending interrupts */
550 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_IDR
) &
551 XILINX_PCIE_IMR_ALL_MASK
,
552 XILINX_PCIE_REG_IDR
);
554 /* Enable all interrupts we handle */
555 pcie_write(port
, XILINX_PCIE_IMR_ENABLE_MASK
, XILINX_PCIE_REG_IMR
);
557 /* Enable the Bridge enable bit */
558 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_RPSC
) |
559 XILINX_PCIE_REG_RPSC_BEN
,
560 XILINX_PCIE_REG_RPSC
);
564 * xilinx_pcie_parse_dt - Parse Device tree
565 * @port: PCIe port information
567 * Return: '0' on success and error value on failure
569 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port
*port
)
571 struct device
*dev
= port
->dev
;
572 struct device_node
*node
= dev
->of_node
;
573 struct resource regs
;
577 type
= of_get_property(node
, "device_type", NULL
);
578 if (!type
|| strcmp(type
, "pci")) {
579 dev_err(dev
, "invalid \"device_type\" %s\n", type
);
583 err
= of_address_to_resource(node
, 0, ®s
);
585 dev_err(dev
, "missing \"reg\" property\n");
589 port
->reg_base
= devm_pci_remap_cfg_resource(dev
, ®s
);
590 if (IS_ERR(port
->reg_base
))
591 return PTR_ERR(port
->reg_base
);
593 port
->irq
= irq_of_parse_and_map(node
, 0);
594 err
= devm_request_irq(dev
, port
->irq
, xilinx_pcie_intr_handler
,
595 IRQF_SHARED
| IRQF_NO_THREAD
,
596 "xilinx-pcie", port
);
598 dev_err(dev
, "unable to request irq %d\n", port
->irq
);
606 * xilinx_pcie_probe - Probe function
607 * @pdev: Platform device pointer
609 * Return: '0' on success and error value on failure
611 static int xilinx_pcie_probe(struct platform_device
*pdev
)
613 struct device
*dev
= &pdev
->dev
;
614 struct xilinx_pcie_port
*port
;
615 struct pci_bus
*bus
, *child
;
616 struct pci_host_bridge
*bridge
;
618 resource_size_t iobase
= 0;
624 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*port
));
628 port
= pci_host_bridge_priv(bridge
);
632 err
= xilinx_pcie_parse_dt(port
);
634 dev_err(dev
, "Parsing DT failed\n");
638 xilinx_pcie_init_port(port
);
640 err
= xilinx_pcie_init_irq_domain(port
);
642 dev_err(dev
, "Failed creating IRQ Domain\n");
646 err
= of_pci_get_host_bridge_resources(dev
->of_node
, 0, 0xff, &res
,
649 dev_err(dev
, "Getting bridge resources failed\n");
653 err
= devm_request_pci_bus_resources(dev
, &res
);
658 list_splice_init(&res
, &bridge
->windows
);
659 bridge
->dev
.parent
= dev
;
660 bridge
->sysdata
= port
;
662 bridge
->ops
= &xilinx_pcie_ops
;
663 bridge
->map_irq
= of_irq_parse_and_map_pci
;
664 bridge
->swizzle_irq
= pci_common_swizzle
;
666 #ifdef CONFIG_PCI_MSI
667 xilinx_pcie_msi_chip
.dev
= dev
;
668 bridge
->msi
= &xilinx_pcie_msi_chip
;
670 err
= pci_scan_root_bus_bridge(bridge
);
676 pci_assign_unassigned_bus_resources(bus
);
677 list_for_each_entry(child
, &bus
->children
, node
)
678 pcie_bus_configure_settings(child
);
679 pci_bus_add_devices(bus
);
683 pci_free_resource_list(&res
);
687 static const struct of_device_id xilinx_pcie_of_match
[] = {
688 { .compatible
= "xlnx,axi-pcie-host-1.00.a", },
692 static struct platform_driver xilinx_pcie_driver
= {
694 .name
= "xilinx-pcie",
695 .of_match_table
= xilinx_pcie_of_match
,
696 .suppress_bind_attrs
= true,
698 .probe
= xilinx_pcie_probe
,
700 builtin_platform_driver(xilinx_pcie_driver
);