2 * PCIe host controller driver for Xilinx AXI PCIe Bridge
4 * Copyright (c) 2012 - 2014 Xilinx, Inc.
6 * Based on the Tegra PCIe driver
8 * Bits taken from Synopsys DesignWare Host controller driver and
9 * ARM PCI Host generic driver.
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
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>
30 /* Register definitions */
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
42 /* Interrupt registers definitions */
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_IMR_ENABLE_MASK 0x1FF30F0D
64 #define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
66 /* Root Port Error FIFO Read Register definitions */
67 #define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
68 #define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
69 #define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
71 /* Root Port Interrupt FIFO Read Register 1 definitions */
72 #define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
73 #define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
74 #define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
75 #define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
76 #define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
78 /* Bridge Info Register definitions */
79 #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
80 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
82 /* Root Port Interrupt FIFO Read Register 2 definitions */
83 #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
85 /* Root Port Status/control Register definitions */
86 #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
88 /* Phy Status/Control Register definitions */
89 #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
91 /* ECAM definitions */
92 #define ECAM_BUS_NUM_SHIFT 20
93 #define ECAM_DEV_NUM_SHIFT 12
95 /* Number of MSI IRQs */
96 #define XILINX_NUM_MSI_IRQS 128
99 * struct xilinx_pcie_port - PCIe port information
100 * @reg_base: IO Mapped Register Base
101 * @irq: Interrupt number
102 * @msi_pages: MSI pages
103 * @root_busno: Root Bus number
104 * @dev: Device pointer
105 * @msi_domain: MSI IRQ domain pointer
106 * @leg_domain: Legacy IRQ domain pointer
107 * @resources: Bus Resources
109 struct xilinx_pcie_port
{
110 void __iomem
*reg_base
;
112 unsigned long msi_pages
;
115 struct irq_domain
*msi_domain
;
116 struct irq_domain
*leg_domain
;
117 struct list_head resources
;
120 static DECLARE_BITMAP(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
122 static inline u32
pcie_read(struct xilinx_pcie_port
*port
, u32 reg
)
124 return readl(port
->reg_base
+ reg
);
127 static inline void pcie_write(struct xilinx_pcie_port
*port
, u32 val
, u32 reg
)
129 writel(val
, port
->reg_base
+ reg
);
132 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port
*port
)
134 return (pcie_read(port
, XILINX_PCIE_REG_PSCR
) &
135 XILINX_PCIE_REG_PSCR_LNKUP
) ? 1 : 0;
139 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
140 * @port: PCIe port information
142 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port
*port
)
144 struct device
*dev
= port
->dev
;
145 unsigned long val
= pcie_read(port
, XILINX_PCIE_REG_RPEFR
);
147 if (val
& XILINX_PCIE_RPEFR_ERR_VALID
) {
148 dev_dbg(dev
, "Requester ID %lu\n",
149 val
& XILINX_PCIE_RPEFR_REQ_ID
);
150 pcie_write(port
, XILINX_PCIE_RPEFR_ALL_MASK
,
151 XILINX_PCIE_REG_RPEFR
);
156 * xilinx_pcie_valid_device - Check if a valid device is present on bus
157 * @bus: PCI Bus structure
158 * @devfn: device/function
160 * Return: 'true' on success and 'false' if invalid device is found
162 static bool xilinx_pcie_valid_device(struct pci_bus
*bus
, unsigned int devfn
)
164 struct xilinx_pcie_port
*port
= bus
->sysdata
;
166 /* Check if link is up when trying to access downstream ports */
167 if (bus
->number
!= port
->root_busno
)
168 if (!xilinx_pcie_link_is_up(port
))
171 /* Only one device down on each root port */
172 if (bus
->number
== port
->root_busno
&& devfn
> 0)
179 * xilinx_pcie_map_bus - Get configuration base
180 * @bus: PCI Bus structure
181 * @devfn: Device/function
182 * @where: Offset from base
184 * Return: Base address of the configuration space needed to be
187 static void __iomem
*xilinx_pcie_map_bus(struct pci_bus
*bus
,
188 unsigned int devfn
, int where
)
190 struct xilinx_pcie_port
*port
= bus
->sysdata
;
193 if (!xilinx_pcie_valid_device(bus
, devfn
))
196 relbus
= (bus
->number
<< ECAM_BUS_NUM_SHIFT
) |
197 (devfn
<< ECAM_DEV_NUM_SHIFT
);
199 return port
->reg_base
+ relbus
+ where
;
202 /* PCIe operations */
203 static struct pci_ops xilinx_pcie_ops
= {
204 .map_bus
= xilinx_pcie_map_bus
,
205 .read
= pci_generic_config_read
,
206 .write
= pci_generic_config_write
,
212 * xilinx_pcie_destroy_msi - Free MSI number
213 * @irq: IRQ to be freed
215 static void xilinx_pcie_destroy_msi(unsigned int irq
)
217 struct msi_desc
*msi
;
218 struct xilinx_pcie_port
*port
;
219 struct irq_data
*d
= irq_get_irq_data(irq
);
220 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
222 if (!test_bit(hwirq
, msi_irq_in_use
)) {
223 msi
= irq_get_msi_desc(irq
);
224 port
= msi_desc_to_pci_sysdata(msi
);
225 dev_err(port
->dev
, "Trying to free unused MSI#%d\n", irq
);
227 clear_bit(hwirq
, msi_irq_in_use
);
232 * xilinx_pcie_assign_msi - Allocate MSI number
234 * Return: A valid IRQ on success and error value on failure.
236 static int xilinx_pcie_assign_msi(void)
240 pos
= find_first_zero_bit(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
241 if (pos
< XILINX_NUM_MSI_IRQS
)
242 set_bit(pos
, msi_irq_in_use
);
250 * xilinx_msi_teardown_irq - Destroy the MSI
251 * @chip: MSI Chip descriptor
252 * @irq: MSI IRQ to destroy
254 static void xilinx_msi_teardown_irq(struct msi_controller
*chip
,
257 xilinx_pcie_destroy_msi(irq
);
258 irq_dispose_mapping(irq
);
262 * xilinx_pcie_msi_setup_irq - Setup MSI request
263 * @chip: MSI chip pointer
264 * @pdev: PCIe device pointer
265 * @desc: MSI descriptor pointer
267 * Return: '0' on success and error value on failure
269 static int xilinx_pcie_msi_setup_irq(struct msi_controller
*chip
,
270 struct pci_dev
*pdev
,
271 struct msi_desc
*desc
)
273 struct xilinx_pcie_port
*port
= pdev
->bus
->sysdata
;
277 phys_addr_t msg_addr
;
279 hwirq
= xilinx_pcie_assign_msi();
283 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
287 irq_set_msi_desc(irq
, desc
);
289 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
292 msg
.address_lo
= msg_addr
;
295 pci_write_msi_msg(irq
, &msg
);
300 /* MSI Chip Descriptor */
301 static struct msi_controller xilinx_pcie_msi_chip
= {
302 .setup_irq
= xilinx_pcie_msi_setup_irq
,
303 .teardown_irq
= xilinx_msi_teardown_irq
,
306 /* HW Interrupt Chip Descriptor */
307 static struct irq_chip xilinx_msi_irq_chip
= {
308 .name
= "Xilinx PCIe MSI",
309 .irq_enable
= pci_msi_unmask_irq
,
310 .irq_disable
= pci_msi_mask_irq
,
311 .irq_mask
= pci_msi_mask_irq
,
312 .irq_unmask
= pci_msi_unmask_irq
,
316 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
317 * @domain: IRQ domain
318 * @irq: Virtual IRQ number
319 * @hwirq: HW interrupt number
321 * Return: Always returns 0.
323 static int xilinx_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
324 irq_hw_number_t hwirq
)
326 irq_set_chip_and_handler(irq
, &xilinx_msi_irq_chip
, handle_simple_irq
);
327 irq_set_chip_data(irq
, domain
->host_data
);
332 /* IRQ Domain operations */
333 static const struct irq_domain_ops msi_domain_ops
= {
334 .map
= xilinx_pcie_msi_map
,
338 * xilinx_pcie_enable_msi - Enable MSI support
339 * @port: PCIe port information
341 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port
*port
)
343 phys_addr_t msg_addr
;
345 port
->msi_pages
= __get_free_pages(GFP_KERNEL
, 0);
346 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
347 pcie_write(port
, 0x0, XILINX_PCIE_REG_MSIBASE1
);
348 pcie_write(port
, msg_addr
, XILINX_PCIE_REG_MSIBASE2
);
354 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
355 * @domain: IRQ domain
356 * @irq: Virtual IRQ number
357 * @hwirq: HW interrupt number
359 * Return: Always returns 0.
361 static int xilinx_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
362 irq_hw_number_t hwirq
)
364 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
365 irq_set_chip_data(irq
, domain
->host_data
);
370 /* INTx IRQ Domain operations */
371 static const struct irq_domain_ops intx_domain_ops
= {
372 .map
= xilinx_pcie_intx_map
,
373 .xlate
= pci_irqd_intx_xlate
,
376 /* PCIe HW Functions */
379 * xilinx_pcie_intr_handler - Interrupt Service Handler
381 * @data: PCIe port information
383 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
385 static irqreturn_t
xilinx_pcie_intr_handler(int irq
, void *data
)
387 struct xilinx_pcie_port
*port
= (struct xilinx_pcie_port
*)data
;
388 struct device
*dev
= port
->dev
;
389 u32 val
, mask
, status
;
391 /* Read interrupt decode and mask registers */
392 val
= pcie_read(port
, XILINX_PCIE_REG_IDR
);
393 mask
= pcie_read(port
, XILINX_PCIE_REG_IMR
);
399 if (status
& XILINX_PCIE_INTR_LINK_DOWN
)
400 dev_warn(dev
, "Link Down\n");
402 if (status
& XILINX_PCIE_INTR_ECRC_ERR
)
403 dev_warn(dev
, "ECRC failed\n");
405 if (status
& XILINX_PCIE_INTR_STR_ERR
)
406 dev_warn(dev
, "Streaming error\n");
408 if (status
& XILINX_PCIE_INTR_HOT_RESET
)
409 dev_info(dev
, "Hot reset\n");
411 if (status
& XILINX_PCIE_INTR_CFG_TIMEOUT
)
412 dev_warn(dev
, "ECAM access timeout\n");
414 if (status
& XILINX_PCIE_INTR_CORRECTABLE
) {
415 dev_warn(dev
, "Correctable error message\n");
416 xilinx_pcie_clear_err_interrupts(port
);
419 if (status
& XILINX_PCIE_INTR_NONFATAL
) {
420 dev_warn(dev
, "Non fatal error message\n");
421 xilinx_pcie_clear_err_interrupts(port
);
424 if (status
& XILINX_PCIE_INTR_FATAL
) {
425 dev_warn(dev
, "Fatal error message\n");
426 xilinx_pcie_clear_err_interrupts(port
);
429 if (status
& (XILINX_PCIE_INTR_INTX
| XILINX_PCIE_INTR_MSI
)) {
430 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR1
);
432 /* Check whether interrupt valid */
433 if (!(val
& XILINX_PCIE_RPIFR1_INTR_VALID
)) {
434 dev_warn(dev
, "RP Intr FIFO1 read error\n");
438 /* Decode the IRQ number */
439 if (val
& XILINX_PCIE_RPIFR1_MSI_INTR
) {
440 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR2
) &
441 XILINX_PCIE_RPIFR2_MSG_DATA
;
443 val
= (val
& XILINX_PCIE_RPIFR1_INTR_MASK
) >>
444 XILINX_PCIE_RPIFR1_INTR_SHIFT
;
445 val
= irq_find_mapping(port
->leg_domain
, val
);
448 /* Clear interrupt FIFO register 1 */
449 pcie_write(port
, XILINX_PCIE_RPIFR1_ALL_MASK
,
450 XILINX_PCIE_REG_RPIFR1
);
452 /* Handle the interrupt */
453 if (IS_ENABLED(CONFIG_PCI_MSI
) ||
454 !(val
& XILINX_PCIE_RPIFR1_MSI_INTR
))
455 generic_handle_irq(val
);
458 if (status
& XILINX_PCIE_INTR_SLV_UNSUPP
)
459 dev_warn(dev
, "Slave unsupported request\n");
461 if (status
& XILINX_PCIE_INTR_SLV_UNEXP
)
462 dev_warn(dev
, "Slave unexpected completion\n");
464 if (status
& XILINX_PCIE_INTR_SLV_COMPL
)
465 dev_warn(dev
, "Slave completion timeout\n");
467 if (status
& XILINX_PCIE_INTR_SLV_ERRP
)
468 dev_warn(dev
, "Slave Error Poison\n");
470 if (status
& XILINX_PCIE_INTR_SLV_CMPABT
)
471 dev_warn(dev
, "Slave Completer Abort\n");
473 if (status
& XILINX_PCIE_INTR_SLV_ILLBUR
)
474 dev_warn(dev
, "Slave Illegal Burst\n");
476 if (status
& XILINX_PCIE_INTR_MST_DECERR
)
477 dev_warn(dev
, "Master decode error\n");
479 if (status
& XILINX_PCIE_INTR_MST_SLVERR
)
480 dev_warn(dev
, "Master slave error\n");
482 if (status
& XILINX_PCIE_INTR_MST_ERRP
)
483 dev_warn(dev
, "Master error poison\n");
486 /* Clear the Interrupt Decode register */
487 pcie_write(port
, status
, XILINX_PCIE_REG_IDR
);
493 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
494 * @port: PCIe port information
496 * Return: '0' on success and error value on failure
498 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port
*port
)
500 struct device
*dev
= port
->dev
;
501 struct device_node
*node
= dev
->of_node
;
502 struct device_node
*pcie_intc_node
;
505 pcie_intc_node
= of_get_next_child(node
, NULL
);
506 if (!pcie_intc_node
) {
507 dev_err(dev
, "No PCIe Intc node found\n");
511 port
->leg_domain
= irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
514 of_node_put(pcie_intc_node
);
515 if (!port
->leg_domain
) {
516 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
521 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
522 port
->msi_domain
= irq_domain_add_linear(node
,
525 &xilinx_pcie_msi_chip
);
526 if (!port
->msi_domain
) {
527 dev_err(dev
, "Failed to get a MSI IRQ domain\n");
531 xilinx_pcie_enable_msi(port
);
538 * xilinx_pcie_init_port - Initialize hardware
539 * @port: PCIe port information
541 static void xilinx_pcie_init_port(struct xilinx_pcie_port
*port
)
543 struct device
*dev
= port
->dev
;
545 if (xilinx_pcie_link_is_up(port
))
546 dev_info(dev
, "PCIe Link is UP\n");
548 dev_info(dev
, "PCIe Link is DOWN\n");
550 /* Disable all interrupts */
551 pcie_write(port
, ~XILINX_PCIE_IDR_ALL_MASK
,
552 XILINX_PCIE_REG_IMR
);
554 /* Clear pending interrupts */
555 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_IDR
) &
556 XILINX_PCIE_IMR_ALL_MASK
,
557 XILINX_PCIE_REG_IDR
);
559 /* Enable all interrupts we handle */
560 pcie_write(port
, XILINX_PCIE_IMR_ENABLE_MASK
, XILINX_PCIE_REG_IMR
);
562 /* Enable the Bridge enable bit */
563 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_RPSC
) |
564 XILINX_PCIE_REG_RPSC_BEN
,
565 XILINX_PCIE_REG_RPSC
);
569 * xilinx_pcie_parse_dt - Parse Device tree
570 * @port: PCIe port information
572 * Return: '0' on success and error value on failure
574 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port
*port
)
576 struct device
*dev
= port
->dev
;
577 struct device_node
*node
= dev
->of_node
;
578 struct resource regs
;
582 type
= of_get_property(node
, "device_type", NULL
);
583 if (!type
|| strcmp(type
, "pci")) {
584 dev_err(dev
, "invalid \"device_type\" %s\n", type
);
588 err
= of_address_to_resource(node
, 0, ®s
);
590 dev_err(dev
, "missing \"reg\" property\n");
594 port
->reg_base
= devm_pci_remap_cfg_resource(dev
, ®s
);
595 if (IS_ERR(port
->reg_base
))
596 return PTR_ERR(port
->reg_base
);
598 port
->irq
= irq_of_parse_and_map(node
, 0);
599 err
= devm_request_irq(dev
, port
->irq
, xilinx_pcie_intr_handler
,
600 IRQF_SHARED
| IRQF_NO_THREAD
,
601 "xilinx-pcie", port
);
603 dev_err(dev
, "unable to request irq %d\n", port
->irq
);
611 * xilinx_pcie_probe - Probe function
612 * @pdev: Platform device pointer
614 * Return: '0' on success and error value on failure
616 static int xilinx_pcie_probe(struct platform_device
*pdev
)
618 struct device
*dev
= &pdev
->dev
;
619 struct xilinx_pcie_port
*port
;
620 struct pci_bus
*bus
, *child
;
621 struct pci_host_bridge
*bridge
;
623 resource_size_t iobase
= 0;
629 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*port
));
633 port
= pci_host_bridge_priv(bridge
);
637 err
= xilinx_pcie_parse_dt(port
);
639 dev_err(dev
, "Parsing DT failed\n");
643 xilinx_pcie_init_port(port
);
645 err
= xilinx_pcie_init_irq_domain(port
);
647 dev_err(dev
, "Failed creating IRQ Domain\n");
651 err
= of_pci_get_host_bridge_resources(dev
->of_node
, 0, 0xff, &res
,
654 dev_err(dev
, "Getting bridge resources failed\n");
658 err
= devm_request_pci_bus_resources(dev
, &res
);
663 list_splice_init(&res
, &bridge
->windows
);
664 bridge
->dev
.parent
= dev
;
665 bridge
->sysdata
= port
;
667 bridge
->ops
= &xilinx_pcie_ops
;
668 bridge
->map_irq
= of_irq_parse_and_map_pci
;
669 bridge
->swizzle_irq
= pci_common_swizzle
;
671 #ifdef CONFIG_PCI_MSI
672 xilinx_pcie_msi_chip
.dev
= dev
;
673 bridge
->msi
= &xilinx_pcie_msi_chip
;
675 err
= pci_scan_root_bus_bridge(bridge
);
681 pci_assign_unassigned_bus_resources(bus
);
682 list_for_each_entry(child
, &bus
->children
, node
)
683 pcie_bus_configure_settings(child
);
684 pci_bus_add_devices(bus
);
688 pci_free_resource_list(&res
);
692 static const struct of_device_id xilinx_pcie_of_match
[] = {
693 { .compatible
= "xlnx,axi-pcie-host-1.00.a", },
697 static struct platform_driver xilinx_pcie_driver
= {
699 .name
= "xilinx-pcie",
700 .of_match_table
= xilinx_pcie_of_match
,
701 .suppress_bind_attrs
= true,
703 .probe
= xilinx_pcie_probe
,
705 builtin_platform_driver(xilinx_pcie_driver
);