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/module.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
97 /* Number of Memory Resources */
98 #define XILINX_MAX_NUM_RESOURCES 3
101 * struct xilinx_pcie_port - PCIe port information
102 * @reg_base: IO Mapped Register Base
103 * @irq: Interrupt number
104 * @msi_pages: MSI pages
105 * @root_busno: Root Bus number
106 * @dev: Device pointer
107 * @irq_domain: IRQ domain pointer
108 * @bus_range: Bus range
109 * @resources: Bus Resources
111 struct xilinx_pcie_port {
112 void __iomem *reg_base;
114 unsigned long msi_pages;
117 struct irq_domain *irq_domain;
118 struct resource bus_range;
119 struct list_head resources;
122 static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
124 static inline struct xilinx_pcie_port *sys_to_pcie(struct pci_sys_data *sys)
126 return sys->private_data;
129 static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
131 return readl(port->reg_base + reg);
134 static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
136 writel(val, port->reg_base + reg);
139 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
141 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
142 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
146 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
147 * @port: PCIe port information
149 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
151 u32 val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
153 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
154 dev_dbg(port->dev, "Requester ID %d\n",
155 val & XILINX_PCIE_RPEFR_REQ_ID);
156 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
157 XILINX_PCIE_REG_RPEFR);
162 * xilinx_pcie_valid_device - Check if a valid device is present on bus
163 * @bus: PCI Bus structure
164 * @devfn: device/function
166 * Return: 'true' on success and 'false' if invalid device is found
168 static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
170 struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
172 /* Check if link is up when trying to access downstream ports */
173 if (bus->number != port->root_busno)
174 if (!xilinx_pcie_link_is_up(port))
177 /* Only one device down on each root port */
178 if (bus->number == port->root_busno && devfn > 0)
182 * Do not read more than one device on the bus directly attached
185 if (bus->primary == port->root_busno && devfn > 0)
192 * xilinx_pcie_config_base - Get configuration base
193 * @bus: PCI Bus structure
194 * @devfn: Device/function
195 * @where: Offset from base
197 * Return: Base address of the configuration space needed to be
200 static void __iomem *xilinx_pcie_config_base(struct pci_bus *bus,
201 unsigned int devfn, int where)
203 struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
206 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
207 (devfn << ECAM_DEV_NUM_SHIFT);
209 return port->reg_base + relbus + where;
213 * xilinx_pcie_read_config - Read configuration space
214 * @bus: PCI Bus structure
215 * @devfn: Device/function
216 * @where: Offset from base
217 * @size: Byte/word/dword
218 * @val: Value to be read
220 * Return: PCIBIOS_SUCCESSFUL on success
221 * PCIBIOS_DEVICE_NOT_FOUND on failure
223 static int xilinx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
224 int where, int size, u32 *val)
228 if (!xilinx_pcie_valid_device(bus, devfn)) {
230 return PCIBIOS_DEVICE_NOT_FOUND;
233 addr = xilinx_pcie_config_base(bus, devfn, where);
247 return PCIBIOS_SUCCESSFUL;
251 * xilinx_pcie_write_config - Write configuration space
252 * @bus: PCI Bus structure
253 * @devfn: Device/function
254 * @where: Offset from base
255 * @size: Byte/word/dword
256 * @val: Value to be written to device
258 * Return: PCIBIOS_SUCCESSFUL on success
259 * PCIBIOS_DEVICE_NOT_FOUND on failure
261 static int xilinx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
262 int where, int size, u32 val)
266 if (!xilinx_pcie_valid_device(bus, devfn))
267 return PCIBIOS_DEVICE_NOT_FOUND;
269 addr = xilinx_pcie_config_base(bus, devfn, where);
283 return PCIBIOS_SUCCESSFUL;
286 /* PCIe operations */
287 static struct pci_ops xilinx_pcie_ops = {
288 .read = xilinx_pcie_read_config,
289 .write = xilinx_pcie_write_config,
295 * xilinx_pcie_destroy_msi - Free MSI number
296 * @irq: IRQ to be freed
298 static void xilinx_pcie_destroy_msi(unsigned int irq)
300 struct irq_desc *desc;
301 struct msi_desc *msi;
302 struct xilinx_pcie_port *port;
304 desc = irq_to_desc(irq);
305 msi = irq_desc_get_msi_desc(desc);
306 port = sys_to_pcie(msi->dev->bus->sysdata);
308 if (!test_bit(irq, msi_irq_in_use))
309 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
311 clear_bit(irq, msi_irq_in_use);
315 * xilinx_pcie_assign_msi - Allocate MSI number
316 * @port: PCIe port structure
318 * Return: A valid IRQ on success and error value on failure.
320 static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
324 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
325 if (pos < XILINX_NUM_MSI_IRQS)
326 set_bit(pos, msi_irq_in_use);
334 * xilinx_msi_teardown_irq - Destroy the MSI
335 * @chip: MSI Chip descriptor
336 * @irq: MSI IRQ to destroy
338 static void xilinx_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
340 xilinx_pcie_destroy_msi(irq);
344 * xilinx_pcie_msi_setup_irq - Setup MSI request
345 * @chip: MSI chip pointer
346 * @pdev: PCIe device pointer
347 * @desc: MSI descriptor pointer
349 * Return: '0' on success and error value on failure
351 static int xilinx_pcie_msi_setup_irq(struct msi_chip *chip,
352 struct pci_dev *pdev,
353 struct msi_desc *desc)
355 struct xilinx_pcie_port *port = sys_to_pcie(pdev->bus->sysdata);
359 phys_addr_t msg_addr;
361 hwirq = xilinx_pcie_assign_msi(port);
365 irq = irq_create_mapping(port->irq_domain, hwirq);
369 irq_set_msi_desc(irq, desc);
371 msg_addr = virt_to_phys((void *)port->msi_pages);
374 msg.address_lo = msg_addr;
377 write_msi_msg(irq, &msg);
382 /* MSI Chip Descriptor */
383 static struct msi_chip xilinx_pcie_msi_chip = {
384 .setup_irq = xilinx_pcie_msi_setup_irq,
385 .teardown_irq = xilinx_msi_teardown_irq,
388 /* HW Interrupt Chip Descriptor */
389 static struct irq_chip xilinx_msi_irq_chip = {
390 .name = "Xilinx PCIe MSI",
391 .irq_enable = unmask_msi_irq,
392 .irq_disable = mask_msi_irq,
393 .irq_mask = mask_msi_irq,
394 .irq_unmask = unmask_msi_irq,
398 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
399 * @domain: IRQ domain
400 * @irq: Virtual IRQ number
401 * @hwirq: HW interrupt number
403 * Return: Always returns 0.
405 static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
406 irq_hw_number_t hwirq)
408 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
409 irq_set_chip_data(irq, domain->host_data);
410 set_irq_flags(irq, IRQF_VALID);
415 /* IRQ Domain operations */
416 static const struct irq_domain_ops msi_domain_ops = {
417 .map = xilinx_pcie_msi_map,
421 * xilinx_pcie_enable_msi - Enable MSI support
422 * @port: PCIe port information
424 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
426 phys_addr_t msg_addr;
428 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
429 msg_addr = virt_to_phys((void *)port->msi_pages);
430 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
431 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
435 * xilinx_pcie_add_bus - Add MSI chip info to PCIe bus
438 static void xilinx_pcie_add_bus(struct pci_bus *bus)
440 if (IS_ENABLED(CONFIG_PCI_MSI)) {
441 struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
443 xilinx_pcie_msi_chip.dev = port->dev;
444 bus->msi = &xilinx_pcie_msi_chip;
451 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
452 * @domain: IRQ domain
453 * @irq: Virtual IRQ number
454 * @hwirq: HW interrupt number
456 * Return: Always returns 0.
458 static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
459 irq_hw_number_t hwirq)
461 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
462 irq_set_chip_data(irq, domain->host_data);
463 set_irq_flags(irq, IRQF_VALID);
468 /* INTx IRQ Domain operations */
469 static const struct irq_domain_ops intx_domain_ops = {
470 .map = xilinx_pcie_intx_map,
473 /* PCIe HW Functions */
476 * xilinx_pcie_intr_handler - Interrupt Service Handler
478 * @data: PCIe port information
480 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
482 static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
484 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
485 u32 val, mask, status, msi_data;
487 /* Read interrupt decode and mask registers */
488 val = pcie_read(port, XILINX_PCIE_REG_IDR);
489 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
495 if (status & XILINX_PCIE_INTR_LINK_DOWN)
496 dev_warn(port->dev, "Link Down\n");
498 if (status & XILINX_PCIE_INTR_ECRC_ERR)
499 dev_warn(port->dev, "ECRC failed\n");
501 if (status & XILINX_PCIE_INTR_STR_ERR)
502 dev_warn(port->dev, "Streaming error\n");
504 if (status & XILINX_PCIE_INTR_HOT_RESET)
505 dev_info(port->dev, "Hot reset\n");
507 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
508 dev_warn(port->dev, "ECAM access timeout\n");
510 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
511 dev_warn(port->dev, "Correctable error message\n");
512 xilinx_pcie_clear_err_interrupts(port);
515 if (status & XILINX_PCIE_INTR_NONFATAL) {
516 dev_warn(port->dev, "Non fatal error message\n");
517 xilinx_pcie_clear_err_interrupts(port);
520 if (status & XILINX_PCIE_INTR_FATAL) {
521 dev_warn(port->dev, "Fatal error message\n");
522 xilinx_pcie_clear_err_interrupts(port);
525 if (status & XILINX_PCIE_INTR_INTX) {
526 /* INTx interrupt received */
527 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
529 /* Check whether interrupt valid */
530 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
531 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
535 /* Clear interrupt FIFO register 1 */
536 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
537 XILINX_PCIE_REG_RPIFR1);
539 /* Handle INTx Interrupt */
540 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
541 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
542 generic_handle_irq(irq_find_mapping(port->irq_domain, val));
545 if (status & XILINX_PCIE_INTR_MSI) {
547 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
549 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
550 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
554 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
555 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
556 XILINX_PCIE_RPIFR2_MSG_DATA;
558 /* Clear interrupt FIFO register 1 */
559 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
560 XILINX_PCIE_REG_RPIFR1);
562 if (IS_ENABLED(CONFIG_PCI_MSI)) {
563 /* Handle MSI Interrupt */
564 generic_handle_irq(msi_data);
569 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
570 dev_warn(port->dev, "Slave unsupported request\n");
572 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
573 dev_warn(port->dev, "Slave unexpected completion\n");
575 if (status & XILINX_PCIE_INTR_SLV_COMPL)
576 dev_warn(port->dev, "Slave completion timeout\n");
578 if (status & XILINX_PCIE_INTR_SLV_ERRP)
579 dev_warn(port->dev, "Slave Error Poison\n");
581 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
582 dev_warn(port->dev, "Slave Completer Abort\n");
584 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
585 dev_warn(port->dev, "Slave Illegal Burst\n");
587 if (status & XILINX_PCIE_INTR_MST_DECERR)
588 dev_warn(port->dev, "Master decode error\n");
590 if (status & XILINX_PCIE_INTR_MST_SLVERR)
591 dev_warn(port->dev, "Master slave error\n");
593 if (status & XILINX_PCIE_INTR_MST_ERRP)
594 dev_warn(port->dev, "Master error poison\n");
596 /* Clear the Interrupt Decode register */
597 pcie_write(port, status, XILINX_PCIE_REG_IDR);
603 * xilinx_pcie_free_irq_domain - Free IRQ domain
604 * @port: PCIe port information
606 static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
611 /* Free IRQ Domain */
612 if (IS_ENABLED(CONFIG_PCI_MSI)) {
614 free_pages(port->msi_pages, 0);
616 num_irqs = XILINX_NUM_MSI_IRQS;
622 for (i = 0; i < num_irqs; i++) {
623 irq = irq_find_mapping(port->irq_domain, i);
625 irq_dispose_mapping(irq);
628 irq_domain_remove(port->irq_domain);
632 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
633 * @port: PCIe port information
635 * Return: '0' on success and error value on failure
637 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
639 struct device *dev = port->dev;
640 struct device_node *node = dev->of_node;
641 struct device_node *pcie_intc_node;
644 pcie_intc_node = of_get_next_child(node, NULL);
645 if (!pcie_intc_node) {
646 dev_err(dev, "No PCIe Intc node found\n");
647 return PTR_ERR(pcie_intc_node);
650 port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
653 if (!port->irq_domain) {
654 dev_err(dev, "Failed to get a INTx IRQ domain\n");
655 return PTR_ERR(port->irq_domain);
659 if (IS_ENABLED(CONFIG_PCI_MSI)) {
660 port->irq_domain = irq_domain_add_linear(node,
663 &xilinx_pcie_msi_chip);
664 if (!port->irq_domain) {
665 dev_err(dev, "Failed to get a MSI IRQ domain\n");
666 return PTR_ERR(port->irq_domain);
669 xilinx_pcie_enable_msi(port);
676 * xilinx_pcie_init_port - Initialize hardware
677 * @port: PCIe port information
679 static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
681 if (xilinx_pcie_link_is_up(port))
682 dev_info(port->dev, "PCIe Link is UP\n");
684 dev_info(port->dev, "PCIe Link is DOWN\n");
686 /* Disable all interrupts */
687 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
688 XILINX_PCIE_REG_IMR);
690 /* Clear pending interrupts */
691 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
692 XILINX_PCIE_IMR_ALL_MASK,
693 XILINX_PCIE_REG_IDR);
695 /* Enable all interrupts */
696 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
698 /* Enable the Bridge enable bit */
699 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
700 XILINX_PCIE_REG_RPSC_BEN,
701 XILINX_PCIE_REG_RPSC);
705 * xilinx_pcie_setup - Setup memory resources
707 * @sys: Per controller structure
709 * Return: '1' on success and error value on failure
711 static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
713 struct xilinx_pcie_port *port = sys_to_pcie(sys);
715 list_splice_init(&port->resources, &sys->resources);
721 * xilinx_pcie_scan_bus - Scan PCIe bus for devices
723 * @sys: Per controller structure
725 * Return: Valid Bus pointer on success and NULL on failure
727 static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
729 struct xilinx_pcie_port *port = sys_to_pcie(sys);
732 port->root_busno = sys->busnr;
733 bus = pci_scan_root_bus(port->dev, sys->busnr, &xilinx_pcie_ops,
734 sys, &sys->resources);
740 * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
741 * @port: PCIe port information
743 * Return: '0' on success and error value on failure
745 static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
747 struct device *dev = port->dev;
748 struct device_node *node = dev->of_node;
749 struct resource *mem;
750 resource_size_t offset;
751 struct of_pci_range_parser parser;
752 struct of_pci_range range;
753 struct pci_host_bridge_window *win;
754 int err = 0, mem_resno = 0;
757 if (of_pci_range_parser_init(&parser, node)) {
758 dev_err(dev, "missing \"ranges\" property\n");
762 /* Parse the ranges and add the resources found to the list */
763 for_each_of_pci_range(&parser, &range) {
765 if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
766 dev_err(dev, "Maximum memory resources exceeded\n");
770 mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
776 of_pci_range_to_resource(&range, node, mem);
778 switch (mem->flags & IORESOURCE_TYPE_BITS) {
780 offset = range.cpu_addr - range.pci_addr;
789 dev_warn(dev, "Invalid resource found %pR\n", mem);
793 err = request_resource(&iomem_resource, mem);
797 pci_add_resource_offset(&port->resources, mem, offset);
800 /* Get the bus range */
801 if (of_pci_parse_bus_range(node, &port->bus_range)) {
802 u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
805 last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
806 XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
808 port->bus_range = (struct resource) {
812 .flags = IORESOURCE_BUS,
816 /* Register bus resource */
817 pci_add_resource(&port->resources, &port->bus_range);
822 release_child_resources(&iomem_resource);
823 list_for_each_entry(win, &port->resources, list)
824 devm_kfree(dev, win->res);
825 pci_free_resource_list(&port->resources);
831 * xilinx_pcie_parse_dt - Parse Device tree
832 * @port: PCIe port information
834 * Return: '0' on success and error value on failure
836 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
838 struct device *dev = port->dev;
839 struct device_node *node = dev->of_node;
840 struct resource regs;
844 type = of_get_property(node, "device_type", NULL);
845 if (!type || strcmp(type, "pci")) {
846 dev_err(dev, "invalid \"device_type\" %s\n", type);
850 err = of_address_to_resource(node, 0, ®s);
852 dev_err(dev, "missing \"reg\" property\n");
856 port->reg_base = devm_ioremap_resource(dev, ®s);
857 if (IS_ERR(port->reg_base))
858 return PTR_ERR(port->reg_base);
860 port->irq = irq_of_parse_and_map(node, 0);
861 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
862 IRQF_SHARED, "xilinx-pcie", port);
864 dev_err(dev, "unable to request irq %d\n", port->irq);
872 * xilinx_pcie_probe - Probe function
873 * @pdev: Platform device pointer
875 * Return: '0' on success and error value on failure
877 static int xilinx_pcie_probe(struct platform_device *pdev)
879 struct xilinx_pcie_port *port;
881 struct device *dev = &pdev->dev;
887 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
893 err = xilinx_pcie_parse_dt(port);
895 dev_err(dev, "Parsing DT failed\n");
899 xilinx_pcie_init_port(port);
901 err = xilinx_pcie_init_irq_domain(port);
903 dev_err(dev, "Failed creating IRQ Domain\n");
908 * Parse PCI ranges, configuration bus range and
909 * request their resources
911 INIT_LIST_HEAD(&port->resources);
912 err = xilinx_pcie_parse_and_add_res(port);
914 dev_err(dev, "Failed adding resources\n");
918 platform_set_drvdata(pdev, port);
920 /* Register the device */
921 memset(&hw, 0, sizeof(hw));
922 hw = (struct hw_pci) {
924 .private_data = (void **)&port,
925 .setup = xilinx_pcie_setup,
926 .map_irq = of_irq_parse_and_map_pci,
927 .add_bus = xilinx_pcie_add_bus,
928 .scan = xilinx_pcie_scan_bus,
929 .ops = &xilinx_pcie_ops,
931 pci_common_init_dev(dev, &hw);
937 * xilinx_pcie_remove - Remove function
938 * @pdev: Platform device pointer
942 static int xilinx_pcie_remove(struct platform_device *pdev)
944 struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
946 xilinx_pcie_free_irq_domain(port);
951 static struct of_device_id xilinx_pcie_of_match[] = {
952 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
956 static struct platform_driver xilinx_pcie_driver = {
958 .name = "xilinx-pcie",
959 .owner = THIS_MODULE,
960 .of_match_table = xilinx_pcie_of_match,
961 .suppress_bind_attrs = true,
963 .probe = xilinx_pcie_probe,
964 .remove = xilinx_pcie_remove,
966 module_platform_driver(xilinx_pcie_driver);
968 MODULE_AUTHOR("Xilinx Inc");
969 MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
970 MODULE_LICENSE("GPL v2");