2 * Xilinx AXI PCIe IP hardware initialation, setup and
3 * configuration spaces access file.
5 * Copyright (c) 2012 Xilinx, Inc.
7 * This program has adopted some work from PCI/PCIE support for AMCC
8 * PowerPC boards written by Benjamin Herrenschmidt.
9 * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/kernel.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/export.h>
32 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/init.h>
36 #include <linux/bootmem.h>
37 #include <linux/delay.h>
39 #include <linux/interrupt.h>
40 #include <linux/slab.h>
41 #include <asm/sizes.h>
43 #include <linux/pci.h>
44 #include <asm/mach/pci.h>
46 /* Register definitions */
47 #define PCIE_CFG_CMD 0x00000004
48 #define PCIE_CFG_CLS 0x00000008
49 #define PCIE_CFG_HDR 0x0000000C
50 #define PCIE_CFG_AD1 0x00000010
51 #define PCIE_CFG_AD2 0x00000014
52 #define PCIE_CFG_BUS 0x00000018
53 #define PCIE_CFG_IO 0x0000001C
54 #define PCIE_CFG_MEM 0x00000020
55 #define PCIE_CFG_PREF_MEM 0x00000024
56 #define PCIE_CFG_PREF_BASE_UPPER 0x00000028
57 #define PCIE_CFG_PREF_LIMIT_UPPER 0x0000002c
58 #define PCIE_CFG_IO_UPPER 0x00000030
60 #define XAXIPCIE_REG_VSECC 0x00000128
61 #define XAXIPCIE_REG_VSECH 0x0000012c
62 #define XAXIPCIE_REG_BIR 0x00000130
63 #define XAXIPCIE_REG_BSCR 0x00000134
64 #define XAXIPCIE_REG_IDR 0x00000138
65 #define XAXIPCIE_REG_IMR 0x0000013c
66 #define XAXIPCIE_REG_BLR 0x00000140
67 #define XAXIPCIE_REG_PSCR 0x00000144
68 #define XAXIPCIE_REG_RPSC 0x00000148
69 #define XAXIPCIE_REG_MSIBASE1 0x0000014c
70 #define XAXIPCIE_REG_MSIBASE2 0x00000150
71 #define XAXIPCIE_REG_RPEFR 0x00000154
72 #define XAXIPCIE_REG_RPIFR1 0x00000158
73 #define XAXIPCIE_REG_RPIFR2 0x0000015c
74 #define XAXIPCIE_REG_VSECC2 0x00000200
75 #define XAXIPCIE_REG_VSECH2 0x00000204
77 /* Interrupt register defines */
78 #define XAXIPCIE_INTR_LINK_DOWN (1 << 0)
79 #define XAXIPCIE_INTR_ECRC_ERR (1 << 1)
80 #define XAXIPCIE_INTR_STR_ERR (1 << 2)
81 #define XAXIPCIE_INTR_HOT_RESET (1 << 3)
82 #define XAXIPCIE_INTR_CFG_COMPL (7 << 5)
83 #define XAXIPCIE_INTR_CFG_TIMEOUT (1 << 8)
84 #define XAXIPCIE_INTR_CORRECTABLE (1 << 9)
85 #define XAXIPCIE_INTR_NONFATAL (1 << 10)
86 #define XAXIPCIE_INTR_FATAL (1 << 11)
87 #define XAXIPCIE_INTR_INTX (1 << 16)
88 #define XAXIPCIE_INTR_MSI (1 << 17)
89 #define XAXIPCIE_INTR_SLV_UNSUPP (1 << 20)
90 #define XAXIPCIE_INTR_SLV_UNEXP (1 << 21)
91 #define XAXIPCIE_INTR_SLV_COMPL (1 << 22)
92 #define XAXIPCIE_INTR_SLV_ERRP (1 << 23)
93 #define XAXIPCIE_INTR_SLV_CMPABT (1 << 24)
94 #define XAXIPCIE_INTR_SLV_ILLBUR (1 << 25)
95 #define XAXIPCIE_INTR_MST_DECERR (1 << 26)
96 #define XAXIPCIE_INTR_MST_SLVERR (1 << 27)
97 #define XAXIPCIE_INTR_MST_ERRP (1 << 28)
99 #define BUS_LOC_SHIFT 20
100 #define DEV_LOC_SHIFT 12
101 #define PRIMARY_BUS 1
102 #define PORT_REG_SIZE 0x1000
103 #define PORT_HEADER_SIZE 0x128
105 #define XAXIPCIE_LOCAL_CNFG_BASE 0x00000000
106 #define XAXIPCIE_REG_BASE 0x00000128
107 #define XAXIPCIE_REG_PSCR_LNKUP 0x00000800
108 #define XAXIPCIE_REG_IMR_MASKALL 0x1FF30FED
109 #define XAXIPCIE_REG_IDR_MASKALL 0xFFFFFFFF
110 #define XAXIPCIE_REG_RPSC_BEN 0x00000001
111 #define BUS_MASTER_ENABLE 0x00000004
113 #define XAXIPCIE_ACCESS8 1
114 #define XAXIPCIE_ACCESS16 2
116 #define XAXIPCIE_MEM_SPACE 2
117 #define XAXIPCIE_MEM_SPACE64 3
119 /* Config structure for PCIe */
120 struct xaxi_pcie_of_config {
139 /* PCIe Root Port Structure */
140 struct xaxi_pcie_port {
141 struct device_node *node;
157 unsigned int __iomem *base_addr_remap;
158 unsigned int __iomem *header_remap;
159 unsigned int __iomem *ecam_remap;
165 void __iomem *io_base_virt;
166 resource_size_t io_base_phys;
167 resource_size_t isa_mem_phys;
168 resource_size_t isa_mem_size;
169 resource_size_t pci_mem_offset;
170 resource_size_t pci_io_size;
171 struct resource io_resource;
172 struct resource mem_resources[3];
173 char io_space_name[16];
174 char mem_space_name[16];
177 static struct xaxi_pcie_port *xaxi_pcie_ports;
178 static int xaxi_pcie_port_cnt;
179 static int last_bus_on_record;
181 /* ISA Memory physical address */
182 resource_size_t isa_mem_base;
183 unsigned long isa_io_base;
185 #ifdef CONFIG_PCI_MSI
186 unsigned long xaxipcie_msg_addr;
190 * xaxi_pcie_verify_config
191 * @port: A pointer to a pcie port that needs to be handled
192 * @bus: Bus structure of current bus
193 * @devfun: device/function
195 * @return: Error / no error
197 * @note: Make sure we can handle this configuration call on our
200 static int xaxi_pcie_verify_config(struct xaxi_pcie_port *port,
206 /* Endpoint can not generate upstream(remote) config cycles */
207 if ((!port->type) && bus->number != port->first_busno)
208 return PCIBIOS_DEVICE_NOT_FOUND;
210 /* Check we are within the mapped range */
211 if (bus->number > port->last_busno) {
213 pr_warn("Warning! Probing bus %u out of range !\n",
217 return PCIBIOS_DEVICE_NOT_FOUND;
220 /* The other side of the RC has only one device as well */
221 if (bus->number == (port->first_busno + 1) &&
222 PCI_SLOT(devfn) != 0)
223 return PCIBIOS_DEVICE_NOT_FOUND;
225 /* Check if we have a link */
226 if ((bus->number != port->first_busno) && !port->link_up)
227 return PCIBIOS_DEVICE_NOT_FOUND;
233 * xaxi_pcie_get_config_base
234 * @port: A pointer to a pcie port that needs to be handled
235 * @bus: Bus structure of current bus
236 * @devfun: Device/function
238 * @return: Base address of the configuration space needed to be
241 * @note: Get the base address of the configuration space for this
244 static void __iomem *xaxi_pcie_get_config_base(
245 struct xaxi_pcie_port *port,
251 relbus = ((bus->number << BUS_LOC_SHIFT) | (devfn << DEV_LOC_SHIFT));
254 return (void __iomem *)port->header_remap;
256 return (void __iomem *)port->header_remap + relbus;
260 * xaxi_pcie_read_config - Read config reg.
261 * @port: A pointer to a pcie port that needs to be handled
262 * @bus: Bus structure of current bus
263 * @devfun: Device/function
264 * @offset: Offset from base
265 * @len: Byte/word/dword
266 * @val: A pointer to value read
268 * @return: Error / no error
271 * @note: Read byte/word/dword from pcie device config reg.
273 static int xaxi_pcie_read_config(struct pci_bus *bus,
279 struct pci_sys_data *sys = bus->sysdata;
280 struct xaxi_pcie_port *port = sys->private_data;
283 if (xaxi_pcie_verify_config(port, bus, devfn) != 0)
284 return PCIBIOS_DEVICE_NOT_FOUND;
286 addr = xaxi_pcie_get_config_base(port, bus, devfn);
288 if ((bus->number == 0) && devfn > 0) {
290 return PCIBIOS_SUCCESSFUL;
294 case XAXIPCIE_ACCESS8:
295 *val = readb((u8 *)(addr + offset));
297 case XAXIPCIE_ACCESS16:
298 *val = readw((u16 *)(addr + offset));
301 *val = readl((u32 *)(addr + offset));
305 return PCIBIOS_SUCCESSFUL;
309 * xaxi_pcie_write_config - Write config reg.
310 * @port: A pointer to a pcie port that needs to be handled
311 * @bus: Bus structure of current bus
312 * @devfun: Device/function
313 * @offset: Offset from base
314 * @len: Byte/word/dword
315 * @val: Value to be written to device
317 * @return: Error / no error
320 * @note: Write byte/word/dword to pcie device config reg.
322 static int xaxi_pcie_write_config(struct pci_bus *bus,
328 struct pci_sys_data *sys = bus->sysdata;
329 struct xaxi_pcie_port *port = sys->private_data;
332 if (xaxi_pcie_verify_config(port, bus, devfn) != 0)
333 return PCIBIOS_DEVICE_NOT_FOUND;
335 addr = xaxi_pcie_get_config_base(port, bus, devfn);
337 if ((bus->number == 0) && devfn > 0)
338 return PCIBIOS_SUCCESSFUL;
341 case XAXIPCIE_ACCESS8:
342 writeb(val, (u8 *)(addr + offset));
344 case XAXIPCIE_ACCESS16:
345 writew(val, (u16 *)(addr + offset));
348 writel(val, (u32 *)(addr + offset));
354 return PCIBIOS_SUCCESSFUL;
358 * xaxi_pcie_set_bridge_resource - Setup base & limit registers of config space.
359 * @port: Pointer to a root port
365 void __devinit xaxi_pcie_set_bridge_resource(struct xaxi_pcie_port *port)
367 const u32 *ranges = port->ranges;
368 int rlen = port->range_len;
369 int np = port->pna + 5;
371 unsigned long long pci_addr, size;
374 while ((rlen -= np * 4) >= 0) {
375 pci_space = be32_to_cpup(ranges);
376 pci_addr = of_read_number(ranges + 1, 2);
377 size = of_read_number(ranges + port->pna + 3, 2);
379 pr_info("%s:pci_space: 0x%08x pci_addr:0x%016llx size: 0x%016llx\n",
380 __func__, pci_space, pci_addr, size);
384 switch ((pci_space >> 24) & 0x3) {
385 case XAXIPCIE_MEM_SPACE: /* PCI Memory space */
386 pr_info("%s:Setting resource in Memory Space\n",
388 writel(port->pcie2axibar_0,
389 (u8 *)port->header_remap +
391 writel(port->pcie2axibar_1,
392 (u8 *)port->header_remap +
395 case XAXIPCIE_MEM_SPACE64: /* PCI 64 bits Memory space */
396 pr_info("%s:Setting resource in Prefetchable Memory Space\n",
399 val = ((pci_addr >> 16) & 0xfff0) |
400 ((pci_addr + size - 1) & 0xfff00000);
402 writel(val, (u8 *)port->header_remap +
405 val = ((pci_addr >> 32) & 0xffffffff);
406 writel(val, (u8 *)port->header_remap +
407 PCIE_CFG_PREF_BASE_UPPER);
409 val = (((pci_addr + size - 1) >> 32) & 0xffffffff);
410 writel(val, (u8 *)port->header_remap +
411 PCIE_CFG_PREF_LIMIT_UPPER);
417 static int xaxi_pcie_hookup_resources(struct xaxi_pcie_port *port,
418 struct pci_sys_data *sys)
420 struct resource *res;
421 resource_size_t io_offset;
424 /* Fixup IO space offset */
425 res = &port->io_resource;
426 io_offset = (unsigned long)port->io_base_virt - isa_io_base;
427 res->start = (res->start + io_offset) & 0xffffffffu;
428 res->end = (res->end + io_offset) & 0xffffffffu;
429 snprintf(port->io_space_name, sizeof(port->io_space_name),
430 "PCIe %d MEM", port->index);
431 port->io_space_name[sizeof(port->io_space_name) - 1] = 0;
432 res->name = port->io_space_name;
435 /* Workaround for lack of IO resource only on 32-bit */
436 res->start = (unsigned long)port->io_base_virt - isa_io_base;
437 res->end = res->start + IO_SPACE_LIMIT;
438 res->flags = IORESOURCE_IO;
441 if (request_resource(&ioport_resource, res))
442 panic("Request PCIe%d IO resource failed\n", port->index);
444 pci_add_resource_offset(&sys->resources, res,
445 (resource_size_t)(port->io_base_virt - 0x0));
447 pr_info("PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
448 (unsigned long long)res->start,
449 (unsigned long long)res->end,
450 (unsigned long)res->flags);
452 /* Hookup Memory resources */
453 for (i = 0; i < 3; ++i) {
454 res = &port->mem_resources[i];
455 snprintf(port->mem_space_name, sizeof(port->mem_space_name),
456 "PCIe %d MEM", port->index);
457 port->mem_space_name[sizeof(port->mem_space_name) - 1] = 0;
458 res->name = port->mem_space_name;
463 /* Workaround for lack of MEM resource only on 32-bit */
464 res->start = port->pci_mem_offset;
465 res->end = (resource_size_t)-1LL;
466 res->flags = IORESOURCE_MEM;
468 if (request_resource(&iomem_resource, res))
469 panic("Request PCIe%d Memory resource failed\n",
471 pci_add_resource_offset(&sys->resources,
472 res, port->pci_mem_offset);
474 pr_info("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n",
475 i, (unsigned long long)res->start,
476 (unsigned long long)res->end,
477 (unsigned long)res->flags);
483 void __devinit xaxi_pcie_process_bridge_OF_ranges(
484 struct xaxi_pcie_port *port,
487 /* The address cells of PCIe node */
490 int memno = 0, isa_hole = -1;
492 unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
493 unsigned long long isa_mb = 0;
494 struct resource *res;
495 const u32 *ranges = port->ranges;
496 int rlen = port->range_len;
497 struct device_node *node = port->node;
499 pr_info("PCI host bridge %s %s ranges:\n",
500 node->full_name, primary ? "(primary)" : "");
503 pr_debug("Parsing ranges property...\n");
504 while ((rlen -= np * 4) >= 0) {
505 /* Read next ranges element */
506 pci_space = be32_to_cpup(ranges);
507 pci_addr = of_read_number(ranges + 1, 2);
508 cpu_addr = of_translate_address(node, ranges + 3);
509 size = of_read_number(ranges + pna + 3, 2);
511 pr_debug("pci_space: 0x%08x pci_addr:0x%016llx\n",
512 pci_space, pci_addr);
513 pr_debug("cpu_addr:0x%016llx size:0x%016llx\n", cpu_addr, size);
517 /* If we failed translation or got a zero-sized region
518 * (some FW try to feed us with non sensical zero sized regions
519 * such as power3 which look like some kind of attempt
520 * at exposing the VGA memory hole)
522 if (cpu_addr == OF_BAD_ADDR || size == 0)
525 /* Now consume following elements while they are contiguous */
526 for (; rlen >= np * sizeof(u32);
527 ranges += np, rlen -= np * 4) {
528 if (be32_to_cpup(ranges) != pci_space)
530 pci_next = of_read_number(ranges + 1, 2);
531 cpu_next = of_translate_address(node, ranges + 3);
532 if (pci_next != pci_addr + size ||
533 cpu_next != cpu_addr + size)
535 size += of_read_number(ranges + pna + 3, 2);
538 /* Act based on address space type */
540 switch ((pci_space >> 24) & 0x3) {
541 case XAXIPCIE_MEM_SPACE: /* PCI Memory space */
542 case XAXIPCIE_MEM_SPACE64: /* PCI 64 bits Memory space */
543 pr_info("MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
544 cpu_addr, cpu_addr + size - 1, pci_addr,
545 (pci_space & 0x40000000) ? "Prefetch" : "");
547 /* We support only 3 memory ranges */
549 pr_info("\\--> Skipped (too many) !\n");
552 /* Handles ISA memory hole space here */
556 if (primary || isa_mem_base == 0)
557 isa_mem_base = cpu_addr;
558 port->isa_mem_phys = cpu_addr;
559 port->isa_mem_size = size;
562 /* We get the PCI/Mem offset from the first range or
563 * the, current one if the offset came from an ISA
564 * hole. If they don't match, bugger.
567 (isa_hole >= 0 && pci_addr != 0 &&
568 port->pci_mem_offset == isa_mb))
569 port->pci_mem_offset = cpu_addr - pci_addr;
570 else if (pci_addr != 0 &&
571 port->pci_mem_offset != cpu_addr - pci_addr) {
572 pr_info("\\--> Skipped (offset mismatch) !\n");
577 res = &port->mem_resources[memno++];
578 res->flags = IORESOURCE_MEM;
579 if (pci_space & 0x40000000)
580 res->flags |= IORESOURCE_PREFETCH;
581 res->start = cpu_addr;
585 res->name = node->full_name;
586 res->end = res->start + size - 1;
593 /* If there's an ISA hole and the pci_mem_offset is -not- matching
594 * the ISA hole offset, then we need to remove the ISA hole from
595 * the resource list for that brige
597 if (isa_hole >= 0 && port->pci_mem_offset != isa_mb) {
598 unsigned int next = isa_hole + 1;
599 pr_info("Removing ISA hole at 0x%016llx\n", isa_mb);
601 memmove(&port->mem_resources[isa_hole],
602 &port->mem_resources[next],
603 sizeof(struct resource) * (memno - next));
604 port->mem_resources[--memno].flags = 0;
608 static struct pci_ops xaxi_pcie_ops = {
609 .read = xaxi_pcie_read_config,
610 .write = xaxi_pcie_write_config,
613 static int __devinit xaxi_pcie_setup(int nr, struct pci_sys_data *sys)
616 struct xaxi_pcie_port *port = &xaxi_pcie_ports[nr];
618 sys->private_data = port;
621 port->first_busno = last_bus_on_record;
623 val = readl((u8 *)port->base_addr_remap + XAXIPCIE_REG_PSCR);
624 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_BIR);
625 val = (val >> 16) & 0x7;
626 port->last_busno = (((port->reg_base - port->reg_len - 1) >> 20)
629 /* Write primary, secondary and subordinate bus numbers */
630 val = port->first_busno;
631 val |= ((port->first_busno + 1) << 8);
632 val |= (port->last_busno << 16);
634 writel(val, ((u8 *)port->header_remap + PCIE_CFG_BUS));
635 last_bus_on_record = port->last_busno + 1;
637 xaxi_pcie_set_bridge_resource(port);
639 /* Parse outbound mapping resources */
640 xaxi_pcie_process_bridge_OF_ranges(port, PRIMARY_BUS);
641 xaxi_pcie_hookup_resources(port, sys);
646 static struct pci_bus __init *xaxi_pcie_scan_bus(int nr,
647 struct pci_sys_data *sys)
649 struct xaxi_pcie_port *port;
651 if (nr >= xaxi_pcie_port_cnt)
654 port = &xaxi_pcie_ports[nr];
655 port->root_bus_nr = sys->busnr;
657 return pci_scan_root_bus(NULL, sys->busnr, &xaxi_pcie_ops, sys,
661 static int xaxi_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
663 struct pci_sys_data *sys = dev->sysdata;
664 struct xaxi_pcie_port *port = sys->private_data;
666 return port->irq_num;
669 /* Interrupt handler */
670 static irqreturn_t xaxi_pcie_intr_handler(int irq, void *data)
672 struct xaxi_pcie_port *port = (struct xaxi_pcie_port *)data;
673 u32 val = 0, mask = 0;
678 /* Read interrupt decode and mask registers */
679 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_IDR);
680 mask = readl((u8 *)port->header_remap + XAXIPCIE_REG_IMR);
686 if (status & XAXIPCIE_INTR_LINK_DOWN)
687 pr_err("Link Down\n");
689 if (status & XAXIPCIE_INTR_ECRC_ERR)
690 pr_warn("ECRC failed\n");
692 if (status & XAXIPCIE_INTR_STR_ERR)
693 pr_warn("Streaming error\n");
695 if (status & XAXIPCIE_INTR_HOT_RESET)
696 pr_info("Hot reset\n");
698 if (status & XAXIPCIE_INTR_CFG_TIMEOUT)
699 pr_warn("ECAM access timeout\n");
701 if (status & XAXIPCIE_INTR_CORRECTABLE) {
702 pr_warn("Correctable error message\n");
703 val = readl((u8 *)port->header_remap +
705 if (val & (1 << 18)) {
707 (u8 *)port->base_addr_remap +
709 pr_debug("Requester ID %d\n", (val & 0xffff));
713 if (status & XAXIPCIE_INTR_NONFATAL) {
714 pr_warn("Non fatal error message\n");
715 val = readl(((u8 *)port->header_remap) +
717 if (val & (1 << 18)) {
719 (u8 *)port->base_addr_remap +
721 pr_debug("Requester ID %d\n", (val & 0xffff));
725 if (status & XAXIPCIE_INTR_FATAL) {
726 pr_warn("Fatal error message\n");
727 val = readl((u8 *)port->header_remap +
729 if (val & (1 << 18)) {
731 (u8 *)port->base_addr_remap +
733 pr_debug("Requester ID %d\n", (val & 0xffff));
737 if (status & XAXIPCIE_INTR_INTX) {
738 /* INTx interrupt received */
739 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_RPIFR1);
741 /* Check whether interrupt valid */
742 if (!(val & (1 << 31))) {
743 pr_warn("RP Intr FIFO1 read error\n");
747 /* Check MSI or INTX */
748 if (!(val & (1 << 30))) {
750 pr_debug("INTx assert\n");
752 pr_debug("INTx deassert\n");
755 /* Clear interrupt FIFO register 1 */
757 (u8 *)port->base_addr_remap + XAXIPCIE_REG_RPIFR1);
760 if (status & XAXIPCIE_INTR_MSI) {
762 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_RPIFR1);
764 if (!(val & (1 << 31))) {
765 pr_warn("RP Intr FIFO1 read error\n");
769 if (val & (1 << 30)) {
770 msi_addr = (val >> 16) & 0x7FF;
771 msi_data = readl((u8 *)port->header_remap +
772 XAXIPCIE_REG_RPIFR2) & 0xFFFF;
773 pr_debug("%s: msi_addr %08x msi_data %08x\n",
774 __func__, msi_addr, msi_data);
777 /* Clear interrupt FIFO register 1 */
779 (u8 *)port->base_addr_remap + XAXIPCIE_REG_RPIFR1);
780 #ifdef CONFIG_PCI_MSI
781 /* Handle MSI Interrupt */
782 if (msi_data >= IRQ_XILINX_MSI_0)
783 generic_handle_irq(msi_data);
787 if (status & XAXIPCIE_INTR_SLV_UNSUPP)
788 pr_warn("Slave unsupported request\n");
790 if (status & XAXIPCIE_INTR_SLV_UNEXP)
791 pr_warn("Slave unexpected completion\n");
793 if (status & XAXIPCIE_INTR_SLV_COMPL)
794 pr_warn("Slave completion timeout\n");
796 if (status & XAXIPCIE_INTR_SLV_ERRP)
797 pr_warn("Slave Error Poison\n");
799 if (status & XAXIPCIE_INTR_SLV_CMPABT)
800 pr_warn("Slave Completer Abort\n");
802 if (status & XAXIPCIE_INTR_SLV_ILLBUR)
803 pr_warn("Slave Illegal Burst\n");
805 if (status & XAXIPCIE_INTR_MST_DECERR)
806 pr_warn("Master decode error\n");
808 if (status & XAXIPCIE_INTR_MST_SLVERR)
809 pr_warn("Master slave error\n");
811 if (status & XAXIPCIE_INTR_MST_ERRP)
812 pr_warn("Master error poison\n");
814 /* Clear the Interrupt Decode register */
815 writel(status, (u8 *)port->base_addr_remap + XAXIPCIE_REG_IDR);
821 * xaxi_pcie_init_port - Initialize hardware
822 * @port: A pointer to a pcie port that needs to be initialized
824 * @return: Error / no error
828 static int xaxi_pcie_init_port(struct xaxi_pcie_port *port)
831 void __iomem *base_addr_remap = NULL;
834 base_addr_remap = ioremap(port->reg_base, port->reg_len);
835 if (!base_addr_remap)
838 port->base_addr_remap = base_addr_remap;
840 /* make sure it is root port before touching header */
842 port->header_remap = base_addr_remap;
843 writel(BUS_MASTER_ENABLE,
844 (u8 *)port->base_addr_remap + PCIE_CFG_CMD);
847 #ifdef CONFIG_PCI_MSI
848 xaxipcie_msg_addr = port->reg_base & ~0xFFF; /* 4KB aligned */
849 writel(0x0, (u8 *)port->base_addr_remap +
850 XAXIPCIE_REG_MSIBASE1);
852 writel(xaxipcie_msg_addr, (u8 *)port->base_addr_remap +
853 XAXIPCIE_REG_MSIBASE2);
856 /* make sure link is up */
857 val = readl((u8 *)port->base_addr_remap + XAXIPCIE_REG_PSCR);
859 if (!(val & XAXIPCIE_REG_PSCR_LNKUP)) {
860 pr_err("%s: Link is Down\n", __func__);
861 iounmap(base_addr_remap);
867 /* Disable all interrupts*/
868 writel(~XAXIPCIE_REG_IDR_MASKALL,
869 (u8 *)port->base_addr_remap + XAXIPCIE_REG_IMR);
871 /* Clear pending interrupts*/
872 writel(readl((u8 *)port->base_addr_remap + XAXIPCIE_REG_IDR) &
873 XAXIPCIE_REG_IMR_MASKALL,
874 (u8 *)port->base_addr_remap + XAXIPCIE_REG_IDR);
876 /* Enable all interrupts*/
877 writel(XAXIPCIE_REG_IMR_MASKALL,
878 (u8 *)port->base_addr_remap + XAXIPCIE_REG_IMR);
881 * Bridge enable must be done after enumeration,
882 * but there is no callback defined
884 val = readl((u8 *)port->base_addr_remap + XAXIPCIE_REG_RPSC);
885 val |= XAXIPCIE_REG_RPSC_BEN;
886 writel(val, (u8 *)port->base_addr_remap + XAXIPCIE_REG_RPSC);
888 /* Register Interrupt Handler */
889 err = request_irq(port->irq_num, xaxi_pcie_intr_handler,
890 IRQF_SHARED, "zynqpcie", port);
892 pr_err("%s: Could not allocate interrupt\n", __func__);
899 struct xaxi_pcie_port *
900 xaxi_pcie_instantiate_port_info(struct xaxi_pcie_of_config *config,
901 struct device_node *node)
903 struct xaxi_pcie_port *port;
906 port_num = config->device_id;
907 port = &xaxi_pcie_ports[port_num];
908 port->node = of_node_get(node);
909 port->index = port_num;
910 port->type = config->device_type;
911 port->reg_base = config->reg_base;
912 port->reg_len = config->reg_len;
913 port->bars_num = config->bars_num;
914 port->irq_num = config->irq_num;
915 port->header_addr = port->reg_base + XAXIPCIE_LOCAL_CNFG_BASE;
916 port->pcie2axibar_0 = config->pcie2axibar_0;
917 port->pcie2axibar_1 = config->pcie2axibar_1;
918 port->ranges = config->ranges;
919 port->range_len = config->range_len;
920 port->pna = config->address_cells;
926 * xaxi_get_pcie_of_config - Read info from device tree
927 * @node: A pointer to device node to read from
928 * @info: A pointer to xilinx_pcie_node struct to write device tree
931 * @return: Error / no error
933 * @note: Read related info from device tree
935 int __devinit xaxi_pcie_get_of_config(struct device_node *node,
936 struct xaxi_pcie_of_config *info)
941 info->num_instances = 1;
943 value = (u32 *) of_get_property(node, "xlnx,device-num", &rlen);
947 value = (u32 *) of_get_property(node, "xlnx,include-rc", &rlen);
949 info->device_type = be32_to_cpup(value);
953 value = (u32 *) of_get_property(node, "reg", &rlen);
958 be32_to_cpup(value + 1);
962 value = (u32 *) of_get_property(node, "xlnx,pciebar-num", &rlen);
964 info->bars_num = be32_to_cpup(value);
968 info->irq_num = irq_of_parse_and_map(node, 0);
970 /* Get address translation parameters */
971 value = (u32 *) of_get_property(node, "xlnx,pciebar2axibar-0", &rlen);
973 info->pcie2axibar_0 =
978 value = (u32 *) of_get_property(node, "xlnx,pciebar2axibar-1", &rlen);
980 info->pcie2axibar_1 =
985 /* The address cells of PCIe node */
986 info->address_cells = of_n_addr_cells(node);
988 /* Get ranges property */
989 value = (u32 *) of_get_property(node, "ranges", &rlen);
991 info->ranges = value;
992 info->range_len = rlen;
999 static int __init xaxi_pcie_of_probe(struct device_node *node)
1002 struct xaxi_pcie_of_config config;
1003 struct xaxi_pcie_port *port;
1005 err = xaxi_pcie_get_of_config(node, &config);
1007 pr_err("%s: Invalid Configuration\n", __func__);
1011 if (!xaxi_pcie_port_cnt) {
1012 xaxi_pcie_port_cnt = config.num_instances;
1014 if (xaxi_pcie_port_cnt) {
1015 xaxi_pcie_ports = (struct xaxi_pcie_port *)
1016 kzalloc(xaxi_pcie_port_cnt *
1017 sizeof(struct xaxi_pcie_port), GFP_KERNEL);
1019 if (!xaxi_pcie_ports) {
1020 pr_err("%s: Memory allocation failed\n",
1024 } else /* not suppose to be here
1025 * when we don't have pcie ports */
1029 port = xaxi_pcie_instantiate_port_info(&config, node);
1030 err = xaxi_pcie_init_port(port);
1032 pr_err("%s: Port Initalization failed\n", __func__);
1039 static struct of_device_id xaxi_pcie_match[] __devinitdata = {
1040 { .compatible = "xlnx,axi-pcie-1.05.a" ,},
1044 static struct hw_pci xaxi_pcie_hw __initdata = {
1045 .nr_controllers = 1,
1046 .setup = xaxi_pcie_setup,
1047 .scan = xaxi_pcie_scan_bus,
1048 .map_irq = xaxi_pcie_map_irq,
1051 static int __init xaxi_pcie_init(void)
1055 struct device_node *node;
1057 for_each_matching_node(node, xaxi_pcie_match) {
1058 err = xaxi_pcie_of_probe(node);
1060 pr_err("%s: Root Port Probe failed\n", __func__);
1064 pr_info("AXI PCIe Root Port Probe Successful\n");
1069 pci_common_init(&xaxi_pcie_hw);
1074 subsys_initcall(xaxi_pcie_init);