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>
28 /* Register definitions */
29 #define XILINX_PCIE_REG_BIR 0x00000130
30 #define XILINX_PCIE_REG_IDR 0x00000138
31 #define XILINX_PCIE_REG_IMR 0x0000013c
32 #define XILINX_PCIE_REG_PSCR 0x00000144
33 #define XILINX_PCIE_REG_RPSC 0x00000148
34 #define XILINX_PCIE_REG_MSIBASE1 0x0000014c
35 #define XILINX_PCIE_REG_MSIBASE2 0x00000150
36 #define XILINX_PCIE_REG_RPEFR 0x00000154
37 #define XILINX_PCIE_REG_RPIFR1 0x00000158
38 #define XILINX_PCIE_REG_RPIFR2 0x0000015c
40 /* Interrupt registers definitions */
41 #define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
42 #define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
43 #define XILINX_PCIE_INTR_STR_ERR BIT(2)
44 #define XILINX_PCIE_INTR_HOT_RESET BIT(3)
45 #define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
46 #define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
47 #define XILINX_PCIE_INTR_NONFATAL BIT(10)
48 #define XILINX_PCIE_INTR_FATAL BIT(11)
49 #define XILINX_PCIE_INTR_INTX BIT(16)
50 #define XILINX_PCIE_INTR_MSI BIT(17)
51 #define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
52 #define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
53 #define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
54 #define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
55 #define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
56 #define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
57 #define XILINX_PCIE_INTR_MST_DECERR BIT(26)
58 #define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
59 #define XILINX_PCIE_INTR_MST_ERRP BIT(28)
60 #define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
61 #define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
62 #define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
64 /* Root Port Error FIFO Read Register definitions */
65 #define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
66 #define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
67 #define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
69 /* Root Port Interrupt FIFO Read Register 1 definitions */
70 #define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
71 #define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
72 #define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
73 #define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
74 #define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
76 /* Bridge Info Register definitions */
77 #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
78 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
80 /* Root Port Interrupt FIFO Read Register 2 definitions */
81 #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
83 /* Root Port Status/control Register definitions */
84 #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
86 /* Phy Status/Control Register definitions */
87 #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
89 /* ECAM definitions */
90 #define ECAM_BUS_NUM_SHIFT 20
91 #define ECAM_DEV_NUM_SHIFT 12
93 /* Number of MSI IRQs */
94 #define XILINX_NUM_MSI_IRQS 128
97 * struct xilinx_pcie_port - PCIe port information
98 * @reg_base: IO Mapped Register Base
99 * @irq: Interrupt number
100 * @msi_pages: MSI pages
101 * @root_busno: Root Bus number
102 * @dev: Device pointer
103 * @msi_domain: MSI IRQ domain pointer
104 * @leg_domain: Legacy IRQ domain pointer
105 * @resources: Bus Resources
107 struct xilinx_pcie_port
{
108 void __iomem
*reg_base
;
110 unsigned long msi_pages
;
113 struct irq_domain
*msi_domain
;
114 struct irq_domain
*leg_domain
;
115 struct list_head resources
;
118 static DECLARE_BITMAP(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
120 static inline u32
pcie_read(struct xilinx_pcie_port
*port
, u32 reg
)
122 return readl(port
->reg_base
+ reg
);
125 static inline void pcie_write(struct xilinx_pcie_port
*port
, u32 val
, u32 reg
)
127 writel(val
, port
->reg_base
+ reg
);
130 static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port
*port
)
132 return (pcie_read(port
, XILINX_PCIE_REG_PSCR
) &
133 XILINX_PCIE_REG_PSCR_LNKUP
) ? 1 : 0;
137 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
138 * @port: PCIe port information
140 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port
*port
)
142 struct device
*dev
= port
->dev
;
143 unsigned long val
= pcie_read(port
, XILINX_PCIE_REG_RPEFR
);
145 if (val
& XILINX_PCIE_RPEFR_ERR_VALID
) {
146 dev_dbg(dev
, "Requester ID %lu\n",
147 val
& XILINX_PCIE_RPEFR_REQ_ID
);
148 pcie_write(port
, XILINX_PCIE_RPEFR_ALL_MASK
,
149 XILINX_PCIE_REG_RPEFR
);
154 * xilinx_pcie_valid_device - Check if a valid device is present on bus
155 * @bus: PCI Bus structure
156 * @devfn: device/function
158 * Return: 'true' on success and 'false' if invalid device is found
160 static bool xilinx_pcie_valid_device(struct pci_bus
*bus
, unsigned int devfn
)
162 struct xilinx_pcie_port
*port
= bus
->sysdata
;
164 /* Check if link is up when trying to access downstream ports */
165 if (bus
->number
!= port
->root_busno
)
166 if (!xilinx_pcie_link_up(port
))
169 /* Only one device down on each root port */
170 if (bus
->number
== port
->root_busno
&& devfn
> 0)
177 * xilinx_pcie_map_bus - Get configuration base
178 * @bus: PCI Bus structure
179 * @devfn: Device/function
180 * @where: Offset from base
182 * Return: Base address of the configuration space needed to be
185 static void __iomem
*xilinx_pcie_map_bus(struct pci_bus
*bus
,
186 unsigned int devfn
, int where
)
188 struct xilinx_pcie_port
*port
= bus
->sysdata
;
191 if (!xilinx_pcie_valid_device(bus
, devfn
))
194 relbus
= (bus
->number
<< ECAM_BUS_NUM_SHIFT
) |
195 (devfn
<< ECAM_DEV_NUM_SHIFT
);
197 return port
->reg_base
+ relbus
+ where
;
200 /* PCIe operations */
201 static struct pci_ops xilinx_pcie_ops
= {
202 .map_bus
= xilinx_pcie_map_bus
,
203 .read
= pci_generic_config_read
,
204 .write
= pci_generic_config_write
,
210 * xilinx_pcie_destroy_msi - Free MSI number
211 * @irq: IRQ to be freed
213 static void xilinx_pcie_destroy_msi(unsigned int irq
)
215 struct msi_desc
*msi
;
216 struct xilinx_pcie_port
*port
;
217 struct irq_data
*d
= irq_get_irq_data(irq
);
218 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
220 if (!test_bit(hwirq
, msi_irq_in_use
)) {
221 msi
= irq_get_msi_desc(irq
);
222 port
= msi_desc_to_pci_sysdata(msi
);
223 dev_err(port
->dev
, "Trying to free unused MSI#%d\n", irq
);
225 clear_bit(hwirq
, msi_irq_in_use
);
230 * xilinx_pcie_assign_msi - Allocate MSI number
232 * Return: A valid IRQ on success and error value on failure.
234 static int xilinx_pcie_assign_msi(void)
238 pos
= find_first_zero_bit(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
239 if (pos
< XILINX_NUM_MSI_IRQS
)
240 set_bit(pos
, msi_irq_in_use
);
248 * xilinx_msi_teardown_irq - Destroy the MSI
249 * @chip: MSI Chip descriptor
250 * @irq: MSI IRQ to destroy
252 static void xilinx_msi_teardown_irq(struct msi_controller
*chip
,
255 xilinx_pcie_destroy_msi(irq
);
256 irq_dispose_mapping(irq
);
260 * xilinx_pcie_msi_setup_irq - Setup MSI request
261 * @chip: MSI chip pointer
262 * @pdev: PCIe device pointer
263 * @desc: MSI descriptor pointer
265 * Return: '0' on success and error value on failure
267 static int xilinx_pcie_msi_setup_irq(struct msi_controller
*chip
,
268 struct pci_dev
*pdev
,
269 struct msi_desc
*desc
)
271 struct xilinx_pcie_port
*port
= pdev
->bus
->sysdata
;
275 phys_addr_t msg_addr
;
277 hwirq
= xilinx_pcie_assign_msi();
281 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
285 irq_set_msi_desc(irq
, desc
);
287 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
290 msg
.address_lo
= msg_addr
;
293 pci_write_msi_msg(irq
, &msg
);
298 /* MSI Chip Descriptor */
299 static struct msi_controller xilinx_pcie_msi_chip
= {
300 .setup_irq
= xilinx_pcie_msi_setup_irq
,
301 .teardown_irq
= xilinx_msi_teardown_irq
,
304 /* HW Interrupt Chip Descriptor */
305 static struct irq_chip xilinx_msi_irq_chip
= {
306 .name
= "Xilinx PCIe MSI",
307 .irq_enable
= pci_msi_unmask_irq
,
308 .irq_disable
= pci_msi_mask_irq
,
309 .irq_mask
= pci_msi_mask_irq
,
310 .irq_unmask
= pci_msi_unmask_irq
,
314 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
315 * @domain: IRQ domain
316 * @irq: Virtual IRQ number
317 * @hwirq: HW interrupt number
319 * Return: Always returns 0.
321 static int xilinx_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
322 irq_hw_number_t hwirq
)
324 irq_set_chip_and_handler(irq
, &xilinx_msi_irq_chip
, handle_simple_irq
);
325 irq_set_chip_data(irq
, domain
->host_data
);
330 /* IRQ Domain operations */
331 static const struct irq_domain_ops msi_domain_ops
= {
332 .map
= xilinx_pcie_msi_map
,
336 * xilinx_pcie_enable_msi - Enable MSI support
337 * @port: PCIe port information
339 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port
*port
)
341 phys_addr_t msg_addr
;
343 port
->msi_pages
= __get_free_pages(GFP_KERNEL
, 0);
344 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
345 pcie_write(port
, 0x0, XILINX_PCIE_REG_MSIBASE1
);
346 pcie_write(port
, msg_addr
, XILINX_PCIE_REG_MSIBASE2
);
352 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
353 * @domain: IRQ domain
354 * @irq: Virtual IRQ number
355 * @hwirq: HW interrupt number
357 * Return: Always returns 0.
359 static int xilinx_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
360 irq_hw_number_t hwirq
)
362 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
363 irq_set_chip_data(irq
, domain
->host_data
);
368 /* INTx IRQ Domain operations */
369 static const struct irq_domain_ops intx_domain_ops
= {
370 .map
= xilinx_pcie_intx_map
,
371 .xlate
= pci_irqd_intx_xlate
,
374 /* PCIe HW Functions */
377 * xilinx_pcie_intr_handler - Interrupt Service Handler
379 * @data: PCIe port information
381 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
383 static irqreturn_t
xilinx_pcie_intr_handler(int irq
, void *data
)
385 struct xilinx_pcie_port
*port
= (struct xilinx_pcie_port
*)data
;
386 struct device
*dev
= port
->dev
;
387 u32 val
, mask
, status
;
389 /* Read interrupt decode and mask registers */
390 val
= pcie_read(port
, XILINX_PCIE_REG_IDR
);
391 mask
= pcie_read(port
, XILINX_PCIE_REG_IMR
);
397 if (status
& XILINX_PCIE_INTR_LINK_DOWN
)
398 dev_warn(dev
, "Link Down\n");
400 if (status
& XILINX_PCIE_INTR_ECRC_ERR
)
401 dev_warn(dev
, "ECRC failed\n");
403 if (status
& XILINX_PCIE_INTR_STR_ERR
)
404 dev_warn(dev
, "Streaming error\n");
406 if (status
& XILINX_PCIE_INTR_HOT_RESET
)
407 dev_info(dev
, "Hot reset\n");
409 if (status
& XILINX_PCIE_INTR_CFG_TIMEOUT
)
410 dev_warn(dev
, "ECAM access timeout\n");
412 if (status
& XILINX_PCIE_INTR_CORRECTABLE
) {
413 dev_warn(dev
, "Correctable error message\n");
414 xilinx_pcie_clear_err_interrupts(port
);
417 if (status
& XILINX_PCIE_INTR_NONFATAL
) {
418 dev_warn(dev
, "Non fatal error message\n");
419 xilinx_pcie_clear_err_interrupts(port
);
422 if (status
& XILINX_PCIE_INTR_FATAL
) {
423 dev_warn(dev
, "Fatal error message\n");
424 xilinx_pcie_clear_err_interrupts(port
);
427 if (status
& (XILINX_PCIE_INTR_INTX
| XILINX_PCIE_INTR_MSI
)) {
428 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR1
);
430 /* Check whether interrupt valid */
431 if (!(val
& XILINX_PCIE_RPIFR1_INTR_VALID
)) {
432 dev_warn(dev
, "RP Intr FIFO1 read error\n");
436 /* Decode the IRQ number */
437 if (val
& XILINX_PCIE_RPIFR1_MSI_INTR
) {
438 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR2
) &
439 XILINX_PCIE_RPIFR2_MSG_DATA
;
441 val
= (val
& XILINX_PCIE_RPIFR1_INTR_MASK
) >>
442 XILINX_PCIE_RPIFR1_INTR_SHIFT
;
443 val
= irq_find_mapping(port
->leg_domain
, val
);
446 /* Clear interrupt FIFO register 1 */
447 pcie_write(port
, XILINX_PCIE_RPIFR1_ALL_MASK
,
448 XILINX_PCIE_REG_RPIFR1
);
450 /* Handle the interrupt */
451 if (IS_ENABLED(CONFIG_PCI_MSI
) ||
452 !(val
& XILINX_PCIE_RPIFR1_MSI_INTR
))
453 generic_handle_irq(val
);
456 if (status
& XILINX_PCIE_INTR_SLV_UNSUPP
)
457 dev_warn(dev
, "Slave unsupported request\n");
459 if (status
& XILINX_PCIE_INTR_SLV_UNEXP
)
460 dev_warn(dev
, "Slave unexpected completion\n");
462 if (status
& XILINX_PCIE_INTR_SLV_COMPL
)
463 dev_warn(dev
, "Slave completion timeout\n");
465 if (status
& XILINX_PCIE_INTR_SLV_ERRP
)
466 dev_warn(dev
, "Slave Error Poison\n");
468 if (status
& XILINX_PCIE_INTR_SLV_CMPABT
)
469 dev_warn(dev
, "Slave Completer Abort\n");
471 if (status
& XILINX_PCIE_INTR_SLV_ILLBUR
)
472 dev_warn(dev
, "Slave Illegal Burst\n");
474 if (status
& XILINX_PCIE_INTR_MST_DECERR
)
475 dev_warn(dev
, "Master decode error\n");
477 if (status
& XILINX_PCIE_INTR_MST_SLVERR
)
478 dev_warn(dev
, "Master slave error\n");
480 if (status
& XILINX_PCIE_INTR_MST_ERRP
)
481 dev_warn(dev
, "Master error poison\n");
484 /* Clear the Interrupt Decode register */
485 pcie_write(port
, status
, XILINX_PCIE_REG_IDR
);
491 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
492 * @port: PCIe port information
494 * Return: '0' on success and error value on failure
496 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port
*port
)
498 struct device
*dev
= port
->dev
;
499 struct device_node
*node
= dev
->of_node
;
500 struct device_node
*pcie_intc_node
;
503 pcie_intc_node
= of_get_next_child(node
, NULL
);
504 if (!pcie_intc_node
) {
505 dev_err(dev
, "No PCIe Intc node found\n");
509 port
->leg_domain
= irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
512 of_node_put(pcie_intc_node
);
513 if (!port
->leg_domain
) {
514 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
519 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
520 port
->msi_domain
= irq_domain_add_linear(node
,
523 &xilinx_pcie_msi_chip
);
524 if (!port
->msi_domain
) {
525 dev_err(dev
, "Failed to get a MSI IRQ domain\n");
529 xilinx_pcie_enable_msi(port
);
536 * xilinx_pcie_init_port - Initialize hardware
537 * @port: PCIe port information
539 static void xilinx_pcie_init_port(struct xilinx_pcie_port
*port
)
541 struct device
*dev
= port
->dev
;
543 if (xilinx_pcie_link_up(port
))
544 dev_info(dev
, "PCIe Link is UP\n");
546 dev_info(dev
, "PCIe Link is DOWN\n");
548 /* Disable all interrupts */
549 pcie_write(port
, ~XILINX_PCIE_IDR_ALL_MASK
,
550 XILINX_PCIE_REG_IMR
);
552 /* Clear pending interrupts */
553 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_IDR
) &
554 XILINX_PCIE_IMR_ALL_MASK
,
555 XILINX_PCIE_REG_IDR
);
557 /* Enable all interrupts we handle */
558 pcie_write(port
, XILINX_PCIE_IMR_ENABLE_MASK
, XILINX_PCIE_REG_IMR
);
560 /* Enable the Bridge enable bit */
561 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_RPSC
) |
562 XILINX_PCIE_REG_RPSC_BEN
,
563 XILINX_PCIE_REG_RPSC
);
567 * xilinx_pcie_parse_dt - Parse Device tree
568 * @port: PCIe port information
570 * Return: '0' on success and error value on failure
572 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port
*port
)
574 struct device
*dev
= port
->dev
;
575 struct device_node
*node
= dev
->of_node
;
576 struct resource regs
;
580 type
= of_get_property(node
, "device_type", NULL
);
581 if (!type
|| strcmp(type
, "pci")) {
582 dev_err(dev
, "invalid \"device_type\" %s\n", type
);
586 err
= of_address_to_resource(node
, 0, ®s
);
588 dev_err(dev
, "missing \"reg\" property\n");
592 port
->reg_base
= devm_pci_remap_cfg_resource(dev
, ®s
);
593 if (IS_ERR(port
->reg_base
))
594 return PTR_ERR(port
->reg_base
);
596 port
->irq
= irq_of_parse_and_map(node
, 0);
597 err
= devm_request_irq(dev
, port
->irq
, xilinx_pcie_intr_handler
,
598 IRQF_SHARED
| IRQF_NO_THREAD
,
599 "xilinx-pcie", port
);
601 dev_err(dev
, "unable to request irq %d\n", port
->irq
);
609 * xilinx_pcie_probe - Probe function
610 * @pdev: Platform device pointer
612 * Return: '0' on success and error value on failure
614 static int xilinx_pcie_probe(struct platform_device
*pdev
)
616 struct device
*dev
= &pdev
->dev
;
617 struct xilinx_pcie_port
*port
;
618 struct pci_bus
*bus
, *child
;
619 struct pci_host_bridge
*bridge
;
621 resource_size_t iobase
= 0;
627 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*port
));
631 port
= pci_host_bridge_priv(bridge
);
635 err
= xilinx_pcie_parse_dt(port
);
637 dev_err(dev
, "Parsing DT failed\n");
641 xilinx_pcie_init_port(port
);
643 err
= xilinx_pcie_init_irq_domain(port
);
645 dev_err(dev
, "Failed creating IRQ Domain\n");
649 err
= devm_of_pci_get_host_bridge_resources(dev
, 0, 0xff, &res
,
652 dev_err(dev
, "Getting bridge resources failed\n");
656 err
= devm_request_pci_bus_resources(dev
, &res
);
661 list_splice_init(&res
, &bridge
->windows
);
662 bridge
->dev
.parent
= dev
;
663 bridge
->sysdata
= port
;
665 bridge
->ops
= &xilinx_pcie_ops
;
666 bridge
->map_irq
= of_irq_parse_and_map_pci
;
667 bridge
->swizzle_irq
= pci_common_swizzle
;
669 #ifdef CONFIG_PCI_MSI
670 xilinx_pcie_msi_chip
.dev
= dev
;
671 bridge
->msi
= &xilinx_pcie_msi_chip
;
673 err
= pci_scan_root_bus_bridge(bridge
);
679 pci_assign_unassigned_bus_resources(bus
);
680 list_for_each_entry(child
, &bus
->children
, node
)
681 pcie_bus_configure_settings(child
);
682 pci_bus_add_devices(bus
);
686 pci_free_resource_list(&res
);
690 static const struct of_device_id xilinx_pcie_of_match
[] = {
691 { .compatible
= "xlnx,axi-pcie-host-1.00.a", },
695 static struct platform_driver xilinx_pcie_driver
= {
697 .name
= "xilinx-pcie",
698 .of_match_table
= xilinx_pcie_of_match
,
699 .suppress_bind_attrs
= true,
701 .probe
= xilinx_pcie_probe
,
703 builtin_platform_driver(xilinx_pcie_driver
);