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_IDR_ALL_MASK 0xFFFFFFFF
65 /* Root Port Error FIFO Read Register definitions */
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
70 /* Root Port Interrupt FIFO Read Register 1 definitions */
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
77 /* Bridge Info Register definitions */
78 #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
79 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
81 /* Root Port Interrupt FIFO Read Register 2 definitions */
82 #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
84 /* Root Port Status/control Register definitions */
85 #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
87 /* Phy Status/Control Register definitions */
88 #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
90 /* ECAM definitions */
91 #define ECAM_BUS_NUM_SHIFT 20
92 #define ECAM_DEV_NUM_SHIFT 12
94 /* Number of MSI IRQs */
95 #define XILINX_NUM_MSI_IRQS 128
98 * struct xilinx_pcie_port - PCIe port information
99 * @reg_base: IO Mapped Register Base
100 * @irq: Interrupt number
101 * @msi_pages: MSI pages
102 * @root_busno: Root Bus number
103 * @dev: Device pointer
104 * @msi_domain: MSI IRQ domain pointer
105 * @leg_domain: Legacy IRQ domain pointer
106 * @resources: Bus Resources
108 struct xilinx_pcie_port
{
109 void __iomem
*reg_base
;
111 unsigned long msi_pages
;
114 struct irq_domain
*msi_domain
;
115 struct irq_domain
*leg_domain
;
116 struct list_head resources
;
119 static DECLARE_BITMAP(msi_irq_in_use
, XILINX_NUM_MSI_IRQS
);
121 static inline u32
pcie_read(struct xilinx_pcie_port
*port
, u32 reg
)
123 return readl(port
->reg_base
+ reg
);
126 static inline void pcie_write(struct xilinx_pcie_port
*port
, u32 val
, u32 reg
)
128 writel(val
, port
->reg_base
+ reg
);
131 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port
*port
)
133 return (pcie_read(port
, XILINX_PCIE_REG_PSCR
) &
134 XILINX_PCIE_REG_PSCR_LNKUP
) ? 1 : 0;
138 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
139 * @port: PCIe port information
141 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port
*port
)
143 struct device
*dev
= port
->dev
;
144 unsigned long val
= pcie_read(port
, XILINX_PCIE_REG_RPEFR
);
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
);
155 * xilinx_pcie_valid_device - Check if a valid device is present on bus
156 * @bus: PCI Bus structure
157 * @devfn: device/function
159 * Return: 'true' on success and 'false' if invalid device is found
161 static bool xilinx_pcie_valid_device(struct pci_bus
*bus
, unsigned int devfn
)
163 struct xilinx_pcie_port
*port
= bus
->sysdata
;
165 /* Check if link is up when trying to access downstream ports */
166 if (bus
->number
!= port
->root_busno
)
167 if (!xilinx_pcie_link_is_up(port
))
170 /* Only one device down on each root port */
171 if (bus
->number
== port
->root_busno
&& devfn
> 0)
178 * xilinx_pcie_map_bus - Get configuration base
179 * @bus: PCI Bus structure
180 * @devfn: Device/function
181 * @where: Offset from base
183 * Return: Base address of the configuration space needed to be
186 static void __iomem
*xilinx_pcie_map_bus(struct pci_bus
*bus
,
187 unsigned int devfn
, int where
)
189 struct xilinx_pcie_port
*port
= bus
->sysdata
;
192 if (!xilinx_pcie_valid_device(bus
, devfn
))
195 relbus
= (bus
->number
<< ECAM_BUS_NUM_SHIFT
) |
196 (devfn
<< ECAM_DEV_NUM_SHIFT
);
198 return port
->reg_base
+ relbus
+ where
;
201 /* PCIe operations */
202 static 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
,
211 * xilinx_pcie_destroy_msi - Free MSI number
212 * @irq: IRQ to be freed
214 static void xilinx_pcie_destroy_msi(unsigned int irq
)
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
);
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
);
226 clear_bit(hwirq
, msi_irq_in_use
);
231 * xilinx_pcie_assign_msi - Allocate MSI number
233 * Return: A valid IRQ on success and error value on failure.
235 static int xilinx_pcie_assign_msi(void)
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
);
249 * xilinx_msi_teardown_irq - Destroy the MSI
250 * @chip: MSI Chip descriptor
251 * @irq: MSI IRQ to destroy
253 static void xilinx_msi_teardown_irq(struct msi_controller
*chip
,
256 xilinx_pcie_destroy_msi(irq
);
257 irq_dispose_mapping(irq
);
261 * xilinx_pcie_msi_setup_irq - Setup MSI request
262 * @chip: MSI chip pointer
263 * @pdev: PCIe device pointer
264 * @desc: MSI descriptor pointer
266 * Return: '0' on success and error value on failure
268 static int xilinx_pcie_msi_setup_irq(struct msi_controller
*chip
,
269 struct pci_dev
*pdev
,
270 struct msi_desc
*desc
)
272 struct xilinx_pcie_port
*port
= pdev
->bus
->sysdata
;
276 phys_addr_t msg_addr
;
278 hwirq
= xilinx_pcie_assign_msi();
282 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
286 irq_set_msi_desc(irq
, desc
);
288 msg_addr
= virt_to_phys((void *)port
->msi_pages
);
291 msg
.address_lo
= msg_addr
;
294 pci_write_msi_msg(irq
, &msg
);
299 /* MSI Chip Descriptor */
300 static struct msi_controller xilinx_pcie_msi_chip
= {
301 .setup_irq
= xilinx_pcie_msi_setup_irq
,
302 .teardown_irq
= xilinx_msi_teardown_irq
,
305 /* HW Interrupt Chip Descriptor */
306 static 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
,
315 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
316 * @domain: IRQ domain
317 * @irq: Virtual IRQ number
318 * @hwirq: HW interrupt number
320 * Return: Always returns 0.
322 static int xilinx_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
323 irq_hw_number_t hwirq
)
325 irq_set_chip_and_handler(irq
, &xilinx_msi_irq_chip
, handle_simple_irq
);
326 irq_set_chip_data(irq
, domain
->host_data
);
331 /* IRQ Domain operations */
332 static const struct irq_domain_ops msi_domain_ops
= {
333 .map
= xilinx_pcie_msi_map
,
337 * xilinx_pcie_enable_msi - Enable MSI support
338 * @port: PCIe port information
340 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port
*port
)
342 phys_addr_t msg_addr
;
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
);
353 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
354 * @domain: IRQ domain
355 * @irq: Virtual IRQ number
356 * @hwirq: HW interrupt number
358 * Return: Always returns 0.
360 static int xilinx_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
361 irq_hw_number_t hwirq
)
363 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
364 irq_set_chip_data(irq
, domain
->host_data
);
369 /* INTx IRQ Domain operations */
370 static const struct irq_domain_ops intx_domain_ops
= {
371 .map
= xilinx_pcie_intx_map
,
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
, msi_data
;
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
) {
428 /* INTx interrupt received */
429 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR1
);
431 /* Check whether interrupt valid */
432 if (!(val
& XILINX_PCIE_RPIFR1_INTR_VALID
)) {
433 dev_warn(dev
, "RP Intr FIFO1 read error\n");
437 if (!(val
& XILINX_PCIE_RPIFR1_MSI_INTR
)) {
438 /* Clear interrupt FIFO register 1 */
439 pcie_write(port
, XILINX_PCIE_RPIFR1_ALL_MASK
,
440 XILINX_PCIE_REG_RPIFR1
);
442 /* Handle INTx Interrupt */
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
,
450 if (status
& XILINX_PCIE_INTR_MSI
) {
452 val
= pcie_read(port
, XILINX_PCIE_REG_RPIFR1
);
454 if (!(val
& XILINX_PCIE_RPIFR1_INTR_VALID
)) {
455 dev_warn(dev
, "RP Intr FIFO1 read error\n");
459 if (val
& XILINX_PCIE_RPIFR1_MSI_INTR
) {
460 msi_data
= pcie_read(port
, XILINX_PCIE_REG_RPIFR2
) &
461 XILINX_PCIE_RPIFR2_MSG_DATA
;
463 /* Clear interrupt FIFO register 1 */
464 pcie_write(port
, XILINX_PCIE_RPIFR1_ALL_MASK
,
465 XILINX_PCIE_REG_RPIFR1
);
467 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
468 /* Handle MSI Interrupt */
469 generic_handle_irq(msi_data
);
474 if (status
& XILINX_PCIE_INTR_SLV_UNSUPP
)
475 dev_warn(dev
, "Slave unsupported request\n");
477 if (status
& XILINX_PCIE_INTR_SLV_UNEXP
)
478 dev_warn(dev
, "Slave unexpected completion\n");
480 if (status
& XILINX_PCIE_INTR_SLV_COMPL
)
481 dev_warn(dev
, "Slave completion timeout\n");
483 if (status
& XILINX_PCIE_INTR_SLV_ERRP
)
484 dev_warn(dev
, "Slave Error Poison\n");
486 if (status
& XILINX_PCIE_INTR_SLV_CMPABT
)
487 dev_warn(dev
, "Slave Completer Abort\n");
489 if (status
& XILINX_PCIE_INTR_SLV_ILLBUR
)
490 dev_warn(dev
, "Slave Illegal Burst\n");
492 if (status
& XILINX_PCIE_INTR_MST_DECERR
)
493 dev_warn(dev
, "Master decode error\n");
495 if (status
& XILINX_PCIE_INTR_MST_SLVERR
)
496 dev_warn(dev
, "Master slave error\n");
498 if (status
& XILINX_PCIE_INTR_MST_ERRP
)
499 dev_warn(dev
, "Master error poison\n");
502 /* Clear the Interrupt Decode register */
503 pcie_write(port
, status
, XILINX_PCIE_REG_IDR
);
509 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
510 * @port: PCIe port information
512 * Return: '0' on success and error value on failure
514 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port
*port
)
516 struct device
*dev
= port
->dev
;
517 struct device_node
*node
= dev
->of_node
;
518 struct device_node
*pcie_intc_node
;
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");
527 port
->leg_domain
= irq_domain_add_linear(pcie_intc_node
, 4,
530 if (!port
->leg_domain
) {
531 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
536 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
537 port
->msi_domain
= irq_domain_add_linear(node
,
540 &xilinx_pcie_msi_chip
);
541 if (!port
->msi_domain
) {
542 dev_err(dev
, "Failed to get a MSI IRQ domain\n");
546 xilinx_pcie_enable_msi(port
);
553 * xilinx_pcie_init_port - Initialize hardware
554 * @port: PCIe port information
556 static void xilinx_pcie_init_port(struct xilinx_pcie_port
*port
)
558 struct device
*dev
= port
->dev
;
560 if (xilinx_pcie_link_is_up(port
))
561 dev_info(dev
, "PCIe Link is UP\n");
563 dev_info(dev
, "PCIe Link is DOWN\n");
565 /* Disable all interrupts */
566 pcie_write(port
, ~XILINX_PCIE_IDR_ALL_MASK
,
567 XILINX_PCIE_REG_IMR
);
569 /* Clear pending interrupts */
570 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_IDR
) &
571 XILINX_PCIE_IMR_ALL_MASK
,
572 XILINX_PCIE_REG_IDR
);
574 /* Enable all interrupts */
575 pcie_write(port
, XILINX_PCIE_IMR_ALL_MASK
, XILINX_PCIE_REG_IMR
);
577 /* Enable the Bridge enable bit */
578 pcie_write(port
, pcie_read(port
, XILINX_PCIE_REG_RPSC
) |
579 XILINX_PCIE_REG_RPSC_BEN
,
580 XILINX_PCIE_REG_RPSC
);
584 * xilinx_pcie_parse_dt - Parse Device tree
585 * @port: PCIe port information
587 * Return: '0' on success and error value on failure
589 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port
*port
)
591 struct device
*dev
= port
->dev
;
592 struct device_node
*node
= dev
->of_node
;
593 struct resource regs
;
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
);
603 err
= of_address_to_resource(node
, 0, ®s
);
605 dev_err(dev
, "missing \"reg\" property\n");
609 port
->reg_base
= devm_ioremap_resource(dev
, ®s
);
610 if (IS_ERR(port
->reg_base
))
611 return PTR_ERR(port
->reg_base
);
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
);
618 dev_err(dev
, "unable to request irq %d\n", port
->irq
);
626 * xilinx_pcie_probe - Probe function
627 * @pdev: Platform device pointer
629 * Return: '0' on success and error value on failure
631 static int xilinx_pcie_probe(struct platform_device
*pdev
)
633 struct device
*dev
= &pdev
->dev
;
634 struct xilinx_pcie_port
*port
;
637 resource_size_t iobase
= 0;
643 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
649 err
= xilinx_pcie_parse_dt(port
);
651 dev_err(dev
, "Parsing DT failed\n");
655 xilinx_pcie_init_port(port
);
657 err
= xilinx_pcie_init_irq_domain(port
);
659 dev_err(dev
, "Failed creating IRQ Domain\n");
663 err
= of_pci_get_host_bridge_resources(dev
->of_node
, 0, 0xff, &res
,
666 dev_err(dev
, "Getting bridge resources failed\n");
670 err
= devm_request_pci_bus_resources(dev
, &res
);
674 bus
= pci_create_root_bus(dev
, 0, &xilinx_pcie_ops
, port
, &res
);
680 #ifdef CONFIG_PCI_MSI
681 xilinx_pcie_msi_chip
.dev
= dev
;
682 bus
->msi
= &xilinx_pcie_msi_chip
;
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
);
689 pci_bus_add_devices(bus
);
693 pci_free_resource_list(&res
);
697 static struct of_device_id xilinx_pcie_of_match
[] = {
698 { .compatible
= "xlnx,axi-pcie-host-1.00.a", },
702 static struct platform_driver xilinx_pcie_driver
= {
704 .name
= "xilinx-pcie",
705 .of_match_table
= xilinx_pcie_of_match
,
706 .suppress_bind_attrs
= true,
708 .probe
= xilinx_pcie_probe
,
710 builtin_platform_driver(xilinx_pcie_driver
);