]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - arch/arm/mach-zynq/xaxipcie.c
zynq/microblaze: pcie: Modified to support PCIe node ranges property
[zynq/linux.git] / arch / arm / mach-zynq / xaxipcie.c
1 /*
2  * Xilinx AXI PCIe IP hardware initialation, setup and
3  * configuration spaces access file.
4  *
5  * Copyright (c) 2012 Xilinx, Inc.
6  *
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.
10  *
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.
15  *
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
19  * for more details.
20  *
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.
24  */
25
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>
33 #include <linux/of.h>
34 #include <linux/of_irq.h>
35 #include <linux/init.h>
36 #include <linux/bootmem.h>
37 #include <linux/delay.h>
38 #include <linux/io.h>
39 #include <linux/interrupt.h>
40 #include <linux/slab.h>
41 #include <asm/sizes.h>
42
43 #include <linux/pci.h>
44 #include <asm/mach/pci.h>
45
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
59
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
76
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)
98
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
104
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
112
113 #define XAXIPCIE_ACCESS8        1
114 #define XAXIPCIE_ACCESS16       2
115
116 #define XAXIPCIE_MEM_SPACE      2
117 #define XAXIPCIE_MEM_SPACE64    3
118
119 /* Config structure for PCIe */
120 struct xaxi_pcie_of_config {
121         u32 num_instances;
122         u32 device_id;
123         u32 device_type;
124         u32 ecam_base;
125         u32 ecam_high;
126         u32 baseaddr;
127         u32 highaddr;
128         u32 bars_num;
129         u32 irq_num;
130         u32 reg_base;
131         u32 reg_len;
132         u32 pcie2axibar_0;
133         u32 pcie2axibar_1;
134         u32 *ranges;
135         u32 range_len;
136         u32 address_cells;
137 };
138
139 /* PCIe Root Port Structure */
140 struct xaxi_pcie_port {
141         struct device_node *node;
142         u32 reg_base;
143         u32 reg_len;
144         u32 ecam_base;
145         u32 ecam_high;
146         u32 baseaddr;
147         u32 highaddr;
148         u32 header_addr;
149         u8 index;
150         u8 type;
151         u8 link_up;
152         u8 bars_num;
153         u32 irq_num;
154         u32 *ranges;
155         u32 range_len;
156         u32 pna;
157         unsigned int __iomem *base_addr_remap;
158         unsigned int __iomem *header_remap;
159         unsigned int __iomem *ecam_remap;
160         u32 pcie2axibar_0;
161         u32 pcie2axibar_1;
162         u32 root_bus_nr;
163         u32 first_busno;
164         u32 last_busno;
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];
175 };
176
177 static struct xaxi_pcie_port *xaxi_pcie_ports;
178 static int xaxi_pcie_port_cnt;
179 static int last_bus_on_record;
180
181 /* ISA Memory physical address */
182 resource_size_t isa_mem_base;
183 unsigned long isa_io_base;
184
185 #ifdef CONFIG_PCI_MSI
186 unsigned long xaxipcie_msg_addr;
187 #endif
188
189 /**
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
194  *
195  * @return: Error / no error
196  *
197  * @note: Make sure we can handle this configuration call on our
198  *        device.
199  */
200 static int xaxi_pcie_verify_config(struct xaxi_pcie_port *port,
201                                 struct pci_bus *bus,
202                                 unsigned int devfn)
203 {
204         static int message;
205
206         /* Endpoint can not generate upstream(remote) config cycles */
207         if ((!port->type) && bus->number != port->first_busno)
208                 return PCIBIOS_DEVICE_NOT_FOUND;
209
210         /* Check we are within the mapped range */
211         if (bus->number > port->last_busno) {
212                 if (!message) {
213                         pr_warn("Warning! Probing bus %u out of range !\n",
214                                 bus->number);
215                         message++;
216                 }
217                 return PCIBIOS_DEVICE_NOT_FOUND;
218         }
219
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;
224
225         /* Check if we have a link */
226         if ((bus->number != port->first_busno) && !port->link_up)
227                 return PCIBIOS_DEVICE_NOT_FOUND;
228
229         return 0;
230 }
231
232 /**
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
237  *
238  * @return: Base address of the configuration space needed to be
239  *          accessed.
240  *
241  * @note: Get the base address of the configuration space for this
242  *        pcie device.
243  */
244 static void __iomem *xaxi_pcie_get_config_base(
245                                 struct xaxi_pcie_port *port,
246                                 struct pci_bus *bus,
247                                 unsigned int devfn)
248 {
249         int relbus;
250
251         relbus = ((bus->number << BUS_LOC_SHIFT) | (devfn << DEV_LOC_SHIFT));
252
253         if (relbus == 0)
254                 return (void __iomem *)port->header_remap;
255
256         return (void __iomem *)port->header_remap + relbus;
257 }
258
259 /**
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
267  *
268  * @return: Error / no error
269  *
270  *
271  * @note: Read byte/word/dword from pcie device config reg.
272  */
273 static int xaxi_pcie_read_config(struct pci_bus *bus,
274                                 unsigned int devfn,
275                                 int offset,
276                                 int len,
277                                 u32 *val)
278 {
279         struct pci_sys_data *sys = bus->sysdata;
280         struct xaxi_pcie_port *port = sys->private_data;
281         void __iomem *addr;
282
283         if (xaxi_pcie_verify_config(port, bus, devfn) != 0)
284                 return PCIBIOS_DEVICE_NOT_FOUND;
285
286         addr = xaxi_pcie_get_config_base(port, bus, devfn);
287
288         if ((bus->number == 0) && devfn > 0) {
289                 *val = 0xFFFFFFFF;
290                 return PCIBIOS_SUCCESSFUL;
291         }
292
293         switch (len) {
294         case XAXIPCIE_ACCESS8:
295                 *val = readb((u8 *)(addr + offset));
296                 break;
297         case XAXIPCIE_ACCESS16:
298                 *val = readw((u16 *)(addr + offset));
299                 break;
300         default:
301                 *val = readl((u32 *)(addr + offset));
302                 break;
303         }
304
305         return PCIBIOS_SUCCESSFUL;
306 }
307
308 /**
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
316  *
317  * @return: Error / no error
318  *
319  *
320  * @note: Write byte/word/dword to pcie device config reg.
321  */
322 static int xaxi_pcie_write_config(struct pci_bus *bus,
323                                 unsigned int devfn,
324                                 int offset,
325                                 int len,
326                                 u32 val)
327 {
328         struct pci_sys_data *sys = bus->sysdata;
329         struct xaxi_pcie_port *port = sys->private_data;
330         void __iomem  *addr;
331
332         if (xaxi_pcie_verify_config(port, bus, devfn) != 0)
333                 return PCIBIOS_DEVICE_NOT_FOUND;
334
335         addr = xaxi_pcie_get_config_base(port, bus, devfn);
336
337         if ((bus->number == 0) && devfn > 0)
338                 return PCIBIOS_SUCCESSFUL;
339
340         switch (len) {
341         case XAXIPCIE_ACCESS8:
342                 writeb(val, (u8 *)(addr + offset));
343                 break;
344         case XAXIPCIE_ACCESS16:
345                 writew(val, (u16 *)(addr + offset));
346                 break;
347         default:
348                 writel(val, (u32 *)(addr + offset));
349                 break;
350         }
351
352         wmb();
353
354         return PCIBIOS_SUCCESSFUL;
355 }
356
357 /**
358  * xaxi_pcie_set_bridge_resource - Setup base & limit registers of config space.
359  * @port: Pointer to a root port
360  *
361  * @return: None
362  *
363  * @note: None
364  */
365 void __devinit xaxi_pcie_set_bridge_resource(struct xaxi_pcie_port *port)
366 {
367         const u32 *ranges = port->ranges;
368         int rlen = port->range_len;
369         int np = port->pna + 5;
370         u32 pci_space;
371         unsigned long long pci_addr, size;
372         u32 val = 0;
373
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);
378
379                 pr_info("%s:pci_space: 0x%08x pci_addr:0x%016llx size: 0x%016llx\n",
380                         __func__, pci_space, pci_addr, size);
381
382                 ranges += np;
383
384                 switch ((pci_space >> 24) & 0x3) {
385                 case XAXIPCIE_MEM_SPACE:        /* PCI Memory space */
386                         pr_info("%s:Setting resource in Memory Space\n",
387                                                                 __func__);
388                         writel(port->pcie2axibar_0,
389                                         (u8 *)port->header_remap +
390                                                 PCIE_CFG_AD1);
391                         writel(port->pcie2axibar_1,
392                                         (u8 *)port->header_remap +
393                                                 PCIE_CFG_AD2);
394                         break;
395                 case XAXIPCIE_MEM_SPACE64:      /* PCI 64 bits Memory space */
396                         pr_info("%s:Setting resource in Prefetchable Memory Space\n",
397                                 __func__);
398
399                         val = ((pci_addr >> 16) & 0xfff0) |
400                                         ((pci_addr + size - 1) & 0xfff00000);
401
402                         writel(val, (u8 *)port->header_remap +
403                                                 PCIE_CFG_PREF_MEM);
404
405                         val = ((pci_addr >> 32) & 0xffffffff);
406                         writel(val, (u8 *)port->header_remap +
407                                                 PCIE_CFG_PREF_BASE_UPPER);
408
409                         val = (((pci_addr + size - 1) >> 32) & 0xffffffff);
410                         writel(val, (u8 *)port->header_remap +
411                                                 PCIE_CFG_PREF_LIMIT_UPPER);
412                         break;
413                 }
414         }
415 }
416
417 static int xaxi_pcie_hookup_resources(struct xaxi_pcie_port *port,
418                                         struct pci_sys_data *sys)
419 {
420         struct resource *res;
421         resource_size_t io_offset;
422         int i;
423
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;
433
434         if (!res->flags) {
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;
439         }
440
441         if (request_resource(&ioport_resource, res))
442                 panic("Request PCIe%d IO resource failed\n", port->index);
443
444         pci_add_resource_offset(&sys->resources, res,
445                         (resource_size_t)(port->io_base_virt - 0x0));
446
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);
451
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;
459
460                 if (!res->flags) {
461                         if (i > 0)
462                                 continue;
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;
467                 }
468                 if (request_resource(&iomem_resource, res))
469                         panic("Request PCIe%d Memory resource failed\n",
470                                         port->index);
471                 pci_add_resource_offset(&sys->resources,
472                                 res, port->pci_mem_offset);
473
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);
478         }
479
480         return 0;
481 }
482
483 void __devinit xaxi_pcie_process_bridge_OF_ranges(
484                                         struct xaxi_pcie_port *port,
485                                         int primary)
486 {
487         /* The address cells of PCIe node */
488         int pna = port->pna;
489         int np = pna + 5;
490         int memno = 0, isa_hole = -1;
491         u32 pci_space;
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;
498
499         pr_info("PCI host bridge %s %s ranges:\n",
500                 node->full_name, primary ? "(primary)" : "");
501
502         /* Parse it */
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);
510
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);
514
515                 ranges += np;
516
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)
521                  */
522                 if (cpu_addr == OF_BAD_ADDR || size == 0)
523                         continue;
524
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)
529                                 break;
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)
534                                 break;
535                         size += of_read_number(ranges + pna + 3, 2);
536                 }
537
538                 /* Act based on address space type */
539                 res = NULL;
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" : "");
546
547                         /* We support only 3 memory ranges */
548                         if (memno >= 3) {
549                                 pr_info("\\--> Skipped (too many) !\n");
550                                 continue;
551                         }
552                         /* Handles ISA memory hole space here */
553                         if (pci_addr == 0) {
554                                 isa_mb = cpu_addr;
555                                 isa_hole = memno;
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;
560                         }
561
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.
565                          */
566                         if (memno == 0 ||
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");
573                                 continue;
574                         }
575
576                         /* Build resource */
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;
582                         break;
583                 }
584                 if (res != NULL) {
585                         res->name = node->full_name;
586                         res->end = res->start + size - 1;
587                         res->parent = NULL;
588                         res->sibling = NULL;
589                         res->child = NULL;
590                 }
591         }
592
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
596          */
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);
600                 if (next < memno)
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;
605         }
606 }
607
608 static struct pci_ops xaxi_pcie_ops = {
609         .read  = xaxi_pcie_read_config,
610         .write = xaxi_pcie_write_config,
611 };
612
613 static int __devinit xaxi_pcie_setup(int nr, struct pci_sys_data *sys)
614 {
615         u32 val;
616         struct xaxi_pcie_port *port = &xaxi_pcie_ports[nr];
617
618         sys->private_data = port;
619
620         /* Get bus range */
621         port->first_busno = last_bus_on_record;
622
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)
627                                                 & 0xFF) & val;
628
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);
633
634         writel(val, ((u8 *)port->header_remap + PCIE_CFG_BUS));
635         last_bus_on_record = port->last_busno + 1;
636
637         xaxi_pcie_set_bridge_resource(port);
638
639         /* Parse outbound mapping resources */
640         xaxi_pcie_process_bridge_OF_ranges(port, PRIMARY_BUS);
641         xaxi_pcie_hookup_resources(port, sys);
642
643         return 1;
644 }
645
646 static struct pci_bus __init *xaxi_pcie_scan_bus(int nr,
647                                 struct pci_sys_data *sys)
648 {
649         struct xaxi_pcie_port *port;
650
651         if (nr >= xaxi_pcie_port_cnt)
652                 return NULL;
653
654         port = &xaxi_pcie_ports[nr];
655         port->root_bus_nr = sys->busnr;
656
657         return pci_scan_root_bus(NULL, sys->busnr, &xaxi_pcie_ops, sys,
658                         &sys->resources);
659 }
660
661 static int xaxi_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
662 {
663         struct pci_sys_data *sys = dev->sysdata;
664         struct xaxi_pcie_port *port = sys->private_data;
665
666         return port->irq_num;
667 }
668
669 /* Interrupt handler */
670 static irqreturn_t xaxi_pcie_intr_handler(int irq, void *data)
671 {
672         struct xaxi_pcie_port *port = (struct xaxi_pcie_port *)data;
673         u32 val = 0, mask = 0;
674         u32 status;
675         u32 msi_addr = 0;
676         u32 msi_data = 0;
677
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);
681
682         status = val & mask;
683         if (!status)
684                 return IRQ_NONE;
685
686         if (status & XAXIPCIE_INTR_LINK_DOWN)
687                 pr_err("Link Down\n");
688
689         if (status & XAXIPCIE_INTR_ECRC_ERR)
690                 pr_warn("ECRC failed\n");
691
692         if (status & XAXIPCIE_INTR_STR_ERR)
693                 pr_warn("Streaming error\n");
694
695         if (status & XAXIPCIE_INTR_HOT_RESET)
696                 pr_info("Hot reset\n");
697
698         if (status & XAXIPCIE_INTR_CFG_TIMEOUT)
699                 pr_warn("ECAM access timeout\n");
700
701         if (status & XAXIPCIE_INTR_CORRECTABLE) {
702                 pr_warn("Correctable error message\n");
703                 val = readl((u8 *)port->header_remap +
704                                 XAXIPCIE_REG_RPEFR);
705                 if (val & (1 << 18)) {
706                         writel(0xFFFFFFFF,
707                                 (u8 *)port->base_addr_remap +
708                                 XAXIPCIE_REG_RPEFR);
709                         pr_debug("Requester ID %d\n", (val & 0xffff));
710                 }
711         }
712
713         if (status & XAXIPCIE_INTR_NONFATAL) {
714                 pr_warn("Non fatal error message\n");
715                 val = readl(((u8 *)port->header_remap) +
716                                 XAXIPCIE_REG_RPEFR);
717                 if (val & (1 << 18)) {
718                         writel(0xFFFFFFFF,
719                                 (u8 *)port->base_addr_remap +
720                                 XAXIPCIE_REG_RPEFR);
721                         pr_debug("Requester ID %d\n", (val & 0xffff));
722                 }
723         }
724
725         if (status & XAXIPCIE_INTR_FATAL) {
726                 pr_warn("Fatal error message\n");
727                 val = readl((u8 *)port->header_remap +
728                                 XAXIPCIE_REG_RPEFR);
729                 if (val & (1 << 18)) {
730                         writel(0xFFFFFFFF,
731                                 (u8 *)port->base_addr_remap +
732                                 XAXIPCIE_REG_RPEFR);
733                         pr_debug("Requester ID %d\n", (val & 0xffff));
734                 }
735         }
736
737         if (status & XAXIPCIE_INTR_INTX) {
738                 /* INTx interrupt received */
739                 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_RPIFR1);
740
741                 /* Check whether interrupt valid */
742                 if (!(val & (1 << 31))) {
743                         pr_warn("RP Intr FIFO1 read error\n");
744                         return IRQ_HANDLED;
745                 }
746
747                 /* Check MSI or INTX */
748                 if (!(val & (1 << 30))) {
749                         if (val & (1 << 29))
750                                 pr_debug("INTx assert\n");
751                         else
752                                 pr_debug("INTx deassert\n");
753                 }
754
755                 /* Clear interrupt FIFO register 1 */
756                 writel(0xFFFFFFFF,
757                         (u8 *)port->base_addr_remap + XAXIPCIE_REG_RPIFR1);
758         }
759
760         if (status & XAXIPCIE_INTR_MSI) {
761                 /* MSI Interrupt */
762                 val = readl((u8 *)port->header_remap + XAXIPCIE_REG_RPIFR1);
763
764                 if (!(val & (1 << 31))) {
765                         pr_warn("RP Intr FIFO1 read error\n");
766                         return IRQ_HANDLED;
767                 }
768
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);
775                 }
776
777                 /* Clear interrupt FIFO register 1 */
778                 writel(0xFFFFFFFF,
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);
784 #endif
785         }
786
787         if (status & XAXIPCIE_INTR_SLV_UNSUPP)
788                 pr_warn("Slave unsupported request\n");
789
790         if (status & XAXIPCIE_INTR_SLV_UNEXP)
791                 pr_warn("Slave unexpected completion\n");
792
793         if (status & XAXIPCIE_INTR_SLV_COMPL)
794                 pr_warn("Slave completion timeout\n");
795
796         if (status & XAXIPCIE_INTR_SLV_ERRP)
797                 pr_warn("Slave Error Poison\n");
798
799         if (status & XAXIPCIE_INTR_SLV_CMPABT)
800                 pr_warn("Slave Completer Abort\n");
801
802         if (status & XAXIPCIE_INTR_SLV_ILLBUR)
803                 pr_warn("Slave Illegal Burst\n");
804
805         if (status & XAXIPCIE_INTR_MST_DECERR)
806                 pr_warn("Master decode error\n");
807
808         if (status & XAXIPCIE_INTR_MST_SLVERR)
809                 pr_warn("Master slave error\n");
810
811         if (status & XAXIPCIE_INTR_MST_ERRP)
812                 pr_warn("Master error poison\n");
813
814         /* Clear the Interrupt Decode register */
815         writel(status, (u8 *)port->base_addr_remap + XAXIPCIE_REG_IDR);
816
817         return IRQ_HANDLED;
818 }
819
820 /**
821  * xaxi_pcie_init_port - Initialize hardware
822  * @port: A pointer to a pcie port that needs to be initialized
823  *
824  * @return: Error / no error
825  *
826  * @note: None
827  */
828 static int xaxi_pcie_init_port(struct xaxi_pcie_port *port)
829 {
830         u32 val = 0;
831         void __iomem *base_addr_remap = NULL;
832         int err = 0;
833
834         base_addr_remap = ioremap(port->reg_base, port->reg_len);
835         if (!base_addr_remap)
836                 return -ENOMEM;
837
838         port->base_addr_remap = base_addr_remap;
839
840         /* make sure it is root port before touching header */
841         if (port->type) {
842                 port->header_remap = base_addr_remap;
843                 writel(BUS_MASTER_ENABLE,
844                         (u8 *)port->base_addr_remap + PCIE_CFG_CMD);
845         }
846
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);
851
852         writel(xaxipcie_msg_addr, (u8 *)port->base_addr_remap +
853                                 XAXIPCIE_REG_MSIBASE2);
854 #endif
855
856         /* make sure link is up */
857         val = readl((u8 *)port->base_addr_remap + XAXIPCIE_REG_PSCR);
858
859         if (!(val & XAXIPCIE_REG_PSCR_LNKUP)) {
860                 pr_err("%s: Link is Down\n", __func__);
861                 iounmap(base_addr_remap);
862                 return -ENODEV;
863         }
864
865         port->link_up = 1;
866
867         /* Disable all interrupts*/
868         writel(~XAXIPCIE_REG_IDR_MASKALL,
869                 (u8 *)port->base_addr_remap + XAXIPCIE_REG_IMR);
870
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);
875
876         /* Enable all interrupts*/
877         writel(XAXIPCIE_REG_IMR_MASKALL,
878                         (u8 *)port->base_addr_remap + XAXIPCIE_REG_IMR);
879
880         /*
881          * Bridge enable must be done after enumeration,
882          * but there is no callback defined
883          */
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);
887
888         /* Register Interrupt Handler */
889         err = request_irq(port->irq_num, xaxi_pcie_intr_handler,
890                                         IRQF_SHARED, "zynqpcie", port);
891         if (err) {
892                 pr_err("%s: Could not allocate interrupt\n", __func__);
893                 return err;
894         }
895
896         return 0;
897 }
898
899 struct xaxi_pcie_port *
900 xaxi_pcie_instantiate_port_info(struct xaxi_pcie_of_config *config,
901                                         struct device_node *node)
902 {
903         struct xaxi_pcie_port *port;
904         int port_num;
905
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;
921
922         return port;
923 }
924
925 /**
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
929  *      info into to.
930  *
931  * @return: Error / no error
932  *
933  * @note: Read related info from device tree
934  */
935 int __devinit xaxi_pcie_get_of_config(struct device_node *node,
936                 struct xaxi_pcie_of_config *info)
937 {
938         u32 *value;
939         u32 rlen;
940
941         info->num_instances = 1;
942
943         value = (u32 *) of_get_property(node, "xlnx,device-num", &rlen);
944
945         info->device_id = 0;
946
947         value = (u32 *) of_get_property(node, "xlnx,include-rc", &rlen);
948         if (value)
949                 info->device_type = be32_to_cpup(value);
950         else
951                 return -ENODEV;
952
953         value = (u32 *) of_get_property(node, "reg", &rlen);
954         if (value) {
955                 info->reg_base =
956                         be32_to_cpup(value);
957                 info->reg_len =
958                         be32_to_cpup(value + 1);
959         } else
960                 return -ENODEV;
961
962         value = (u32 *) of_get_property(node, "xlnx,pciebar-num", &rlen);
963         if (value)
964                 info->bars_num = be32_to_cpup(value);
965         else
966                 return -ENODEV;
967
968         info->irq_num = irq_of_parse_and_map(node, 0);
969
970         /* Get address translation parameters */
971         value = (u32 *) of_get_property(node, "xlnx,pciebar2axibar-0", &rlen);
972         if (value) {
973                 info->pcie2axibar_0 =
974                         be32_to_cpup(value);
975         } else
976                 return -ENODEV;
977
978         value = (u32 *) of_get_property(node, "xlnx,pciebar2axibar-1", &rlen);
979         if (value) {
980                 info->pcie2axibar_1 =
981                         be32_to_cpup(value);
982         } else
983                 return -ENODEV;
984
985         /* The address cells of PCIe node */
986         info->address_cells = of_n_addr_cells(node);
987
988         /* Get ranges property */
989         value = (u32 *) of_get_property(node, "ranges", &rlen);
990         if (value) {
991                 info->ranges = value;
992                 info->range_len = rlen;
993         } else
994                 return -ENODEV;
995
996         return 0;
997 }
998
999 static int __init xaxi_pcie_of_probe(struct device_node *node)
1000 {
1001         int err = 0;
1002         struct xaxi_pcie_of_config config;
1003         struct xaxi_pcie_port *port;
1004
1005         err = xaxi_pcie_get_of_config(node, &config);
1006         if (err) {
1007                 pr_err("%s: Invalid Configuration\n", __func__);
1008                 return err;
1009         }
1010
1011         if (!xaxi_pcie_port_cnt) {
1012                 xaxi_pcie_port_cnt = config.num_instances;
1013
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);
1018
1019                         if (!xaxi_pcie_ports) {
1020                                 pr_err("%s: Memory allocation failed\n",
1021                                         __func__);
1022                                 return -ENOMEM;
1023                         }
1024                 } else /* not suppose to be here
1025                         * when we don't have pcie ports */
1026                         return -ENODEV;
1027         }
1028
1029         port = xaxi_pcie_instantiate_port_info(&config, node);
1030         err = xaxi_pcie_init_port(port);
1031         if (err) {
1032                 pr_err("%s: Port Initalization failed\n", __func__);
1033                 return err;
1034         }
1035
1036         return err;
1037 }
1038
1039 static struct of_device_id xaxi_pcie_match[] __devinitdata = {
1040         { .compatible = "xlnx,axi-pcie-1.05.a" ,},
1041         {}
1042 };
1043
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,
1049 };
1050
1051 static int __init xaxi_pcie_init(void)
1052 {
1053         int err;
1054         int init = 0;
1055         struct device_node *node;
1056
1057         for_each_matching_node(node, xaxi_pcie_match) {
1058                 err = xaxi_pcie_of_probe(node);
1059                 if (err) {
1060                         pr_err("%s: Root Port Probe failed\n", __func__);
1061
1062                         return err;
1063                 }
1064                 pr_info("AXI PCIe Root Port Probe Successful\n");
1065                 init++;
1066         }
1067
1068         if (init)
1069                 pci_common_init(&xaxi_pcie_hw);
1070
1071         return 0;
1072 }
1073
1074 subsys_initcall(xaxi_pcie_init);