GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / pci / host / pcie-xilinx.c
1 /*
2  * PCIe host controller driver for Xilinx AXI PCIe Bridge
3  *
4  * Copyright (c) 2012 - 2014 Xilinx, Inc.
5  *
6  * Based on the Tegra PCIe driver
7  *
8  * Bits taken from Synopsys Designware Host controller driver and
9  * ARM PCI Host generic driver.
10  *
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.
15  */
16
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>
29
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
41
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
64
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
69
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
76
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
80
81 /* Root Port Interrupt FIFO Read Register 2 definitions */
82 #define XILINX_PCIE_RPIFR2_MSG_DATA     GENMASK(15, 0)
83
84 /* Root Port Status/control Register definitions */
85 #define XILINX_PCIE_REG_RPSC_BEN        BIT(0)
86
87 /* Phy Status/Control Register definitions */
88 #define XILINX_PCIE_REG_PSCR_LNKUP      BIT(11)
89
90 /* ECAM definitions */
91 #define ECAM_BUS_NUM_SHIFT              20
92 #define ECAM_DEV_NUM_SHIFT              12
93
94 /* Number of MSI IRQs */
95 #define XILINX_NUM_MSI_IRQS             128
96
97 /* Number of Memory Resources */
98 #define XILINX_MAX_NUM_RESOURCES        3
99
100 /**
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
110  */
111 struct xilinx_pcie_port {
112         void __iomem *reg_base;
113         u32 irq;
114         unsigned long msi_pages;
115         u8 root_busno;
116         struct device *dev;
117         struct irq_domain *irq_domain;
118         struct resource bus_range;
119         struct list_head resources;
120 };
121
122 static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
123
124 static inline struct xilinx_pcie_port *sys_to_pcie(struct pci_sys_data *sys)
125 {
126         return sys->private_data;
127 }
128
129 static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
130 {
131         return readl(port->reg_base + reg);
132 }
133
134 static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
135 {
136         writel(val, port->reg_base + reg);
137 }
138
139 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
140 {
141         return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
142                 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
143 }
144
145 /**
146  * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
147  * @port: PCIe port information
148  */
149 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
150 {
151         unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
152
153         if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
154                 dev_dbg(port->dev, "Requester ID %lu\n",
155                         val & XILINX_PCIE_RPEFR_REQ_ID);
156                 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
157                            XILINX_PCIE_REG_RPEFR);
158         }
159 }
160
161 /**
162  * xilinx_pcie_valid_device - Check if a valid device is present on bus
163  * @bus: PCI Bus structure
164  * @devfn: device/function
165  *
166  * Return: 'true' on success and 'false' if invalid device is found
167  */
168 static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
169 {
170         struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
171
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))
175                         return false;
176
177         /* Only one device down on each root port */
178         if (bus->number == port->root_busno && devfn > 0)
179                 return false;
180
181         /*
182          * Do not read more than one device on the bus directly attached
183          * to RC.
184          */
185         if (bus->primary == port->root_busno && devfn > 0)
186                 return false;
187
188         return true;
189 }
190
191 /**
192  * xilinx_pcie_map_bus - Get configuration base
193  * @bus: PCI Bus structure
194  * @devfn: Device/function
195  * @where: Offset from base
196  *
197  * Return: Base address of the configuration space needed to be
198  *         accessed.
199  */
200 static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
201                                          unsigned int devfn, int where)
202 {
203         struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
204         int relbus;
205
206         if (!xilinx_pcie_valid_device(bus, devfn))
207                 return NULL;
208
209         relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
210                  (devfn << ECAM_DEV_NUM_SHIFT);
211
212         return port->reg_base + relbus + where;
213 }
214
215 /* PCIe operations */
216 static struct pci_ops xilinx_pcie_ops = {
217         .map_bus = xilinx_pcie_map_bus,
218         .read   = pci_generic_config_read,
219         .write  = pci_generic_config_write,
220 };
221
222 /* MSI functions */
223
224 /**
225  * xilinx_pcie_destroy_msi - Free MSI number
226  * @irq: IRQ to be freed
227  */
228 static void xilinx_pcie_destroy_msi(unsigned int irq)
229 {
230         struct msi_desc *msi;
231         struct xilinx_pcie_port *port;
232
233         if (!test_bit(irq, msi_irq_in_use)) {
234                 msi = irq_get_msi_desc(irq);
235                 port = sys_to_pcie(msi_desc_to_pci_sysdata(msi));
236                 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
237         } else {
238                 clear_bit(irq, msi_irq_in_use);
239         }
240 }
241
242 /**
243  * xilinx_pcie_assign_msi - Allocate MSI number
244  * @port: PCIe port structure
245  *
246  * Return: A valid IRQ on success and error value on failure.
247  */
248 static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
249 {
250         int pos;
251
252         pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
253         if (pos < XILINX_NUM_MSI_IRQS)
254                 set_bit(pos, msi_irq_in_use);
255         else
256                 return -ENOSPC;
257
258         return pos;
259 }
260
261 /**
262  * xilinx_msi_teardown_irq - Destroy the MSI
263  * @chip: MSI Chip descriptor
264  * @irq: MSI IRQ to destroy
265  */
266 static void xilinx_msi_teardown_irq(struct msi_controller *chip,
267                                     unsigned int irq)
268 {
269         xilinx_pcie_destroy_msi(irq);
270 }
271
272 /**
273  * xilinx_pcie_msi_setup_irq - Setup MSI request
274  * @chip: MSI chip pointer
275  * @pdev: PCIe device pointer
276  * @desc: MSI descriptor pointer
277  *
278  * Return: '0' on success and error value on failure
279  */
280 static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
281                                      struct pci_dev *pdev,
282                                      struct msi_desc *desc)
283 {
284         struct xilinx_pcie_port *port = sys_to_pcie(pdev->bus->sysdata);
285         unsigned int irq;
286         int hwirq;
287         struct msi_msg msg;
288         phys_addr_t msg_addr;
289
290         hwirq = xilinx_pcie_assign_msi(port);
291         if (hwirq < 0)
292                 return hwirq;
293
294         irq = irq_create_mapping(port->irq_domain, hwirq);
295         if (!irq)
296                 return -EINVAL;
297
298         irq_set_msi_desc(irq, desc);
299
300         msg_addr = virt_to_phys((void *)port->msi_pages);
301
302         msg.address_hi = 0;
303         msg.address_lo = msg_addr;
304         msg.data = irq;
305
306         pci_write_msi_msg(irq, &msg);
307
308         return 0;
309 }
310
311 /* MSI Chip Descriptor */
312 static struct msi_controller xilinx_pcie_msi_chip = {
313         .setup_irq = xilinx_pcie_msi_setup_irq,
314         .teardown_irq = xilinx_msi_teardown_irq,
315 };
316
317 /* HW Interrupt Chip Descriptor */
318 static struct irq_chip xilinx_msi_irq_chip = {
319         .name = "Xilinx PCIe MSI",
320         .irq_enable = pci_msi_unmask_irq,
321         .irq_disable = pci_msi_mask_irq,
322         .irq_mask = pci_msi_mask_irq,
323         .irq_unmask = pci_msi_unmask_irq,
324 };
325
326 /**
327  * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
328  * @domain: IRQ domain
329  * @irq: Virtual IRQ number
330  * @hwirq: HW interrupt number
331  *
332  * Return: Always returns 0.
333  */
334 static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
335                                irq_hw_number_t hwirq)
336 {
337         irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
338         irq_set_chip_data(irq, domain->host_data);
339
340         return 0;
341 }
342
343 /* IRQ Domain operations */
344 static const struct irq_domain_ops msi_domain_ops = {
345         .map = xilinx_pcie_msi_map,
346 };
347
348 /**
349  * xilinx_pcie_enable_msi - Enable MSI support
350  * @port: PCIe port information
351  */
352 static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
353 {
354         phys_addr_t msg_addr;
355
356         port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
357         if (!port->msi_pages)
358                 return -ENOMEM;
359
360         msg_addr = virt_to_phys((void *)port->msi_pages);
361         pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
362         pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
363
364         return 0;
365 }
366
367 /* INTx Functions */
368
369 /**
370  * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
371  * @domain: IRQ domain
372  * @irq: Virtual IRQ number
373  * @hwirq: HW interrupt number
374  *
375  * Return: Always returns 0.
376  */
377 static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
378                                 irq_hw_number_t hwirq)
379 {
380         irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
381         irq_set_chip_data(irq, domain->host_data);
382
383         return 0;
384 }
385
386 /* INTx IRQ Domain operations */
387 static const struct irq_domain_ops intx_domain_ops = {
388         .map = xilinx_pcie_intx_map,
389 };
390
391 /* PCIe HW Functions */
392
393 /**
394  * xilinx_pcie_intr_handler - Interrupt Service Handler
395  * @irq: IRQ number
396  * @data: PCIe port information
397  *
398  * Return: IRQ_HANDLED on success and IRQ_NONE on failure
399  */
400 static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
401 {
402         struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
403         u32 val, mask, status, msi_data;
404
405         /* Read interrupt decode and mask registers */
406         val = pcie_read(port, XILINX_PCIE_REG_IDR);
407         mask = pcie_read(port, XILINX_PCIE_REG_IMR);
408
409         status = val & mask;
410         if (!status)
411                 return IRQ_NONE;
412
413         if (status & XILINX_PCIE_INTR_LINK_DOWN)
414                 dev_warn(port->dev, "Link Down\n");
415
416         if (status & XILINX_PCIE_INTR_ECRC_ERR)
417                 dev_warn(port->dev, "ECRC failed\n");
418
419         if (status & XILINX_PCIE_INTR_STR_ERR)
420                 dev_warn(port->dev, "Streaming error\n");
421
422         if (status & XILINX_PCIE_INTR_HOT_RESET)
423                 dev_info(port->dev, "Hot reset\n");
424
425         if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
426                 dev_warn(port->dev, "ECAM access timeout\n");
427
428         if (status & XILINX_PCIE_INTR_CORRECTABLE) {
429                 dev_warn(port->dev, "Correctable error message\n");
430                 xilinx_pcie_clear_err_interrupts(port);
431         }
432
433         if (status & XILINX_PCIE_INTR_NONFATAL) {
434                 dev_warn(port->dev, "Non fatal error message\n");
435                 xilinx_pcie_clear_err_interrupts(port);
436         }
437
438         if (status & XILINX_PCIE_INTR_FATAL) {
439                 dev_warn(port->dev, "Fatal error message\n");
440                 xilinx_pcie_clear_err_interrupts(port);
441         }
442
443         if (status & XILINX_PCIE_INTR_INTX) {
444                 /* INTx interrupt received */
445                 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
446
447                 /* Check whether interrupt valid */
448                 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
449                         dev_warn(port->dev, "RP Intr FIFO1 read error\n");
450                         return IRQ_HANDLED;
451                 }
452
453                 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
454                         /* Clear interrupt FIFO register 1 */
455                         pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
456                                    XILINX_PCIE_REG_RPIFR1);
457
458                         /* Handle INTx Interrupt */
459                         val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
460                                 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
461                         generic_handle_irq(irq_find_mapping(port->irq_domain,
462                                                             val));
463                 }
464         }
465
466         if (status & XILINX_PCIE_INTR_MSI) {
467                 /* MSI Interrupt */
468                 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
469
470                 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
471                         dev_warn(port->dev, "RP Intr FIFO1 read error\n");
472                         return IRQ_HANDLED;
473                 }
474
475                 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
476                         msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
477                                    XILINX_PCIE_RPIFR2_MSG_DATA;
478
479                         /* Clear interrupt FIFO register 1 */
480                         pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
481                                    XILINX_PCIE_REG_RPIFR1);
482
483                         if (IS_ENABLED(CONFIG_PCI_MSI)) {
484                                 /* Handle MSI Interrupt */
485                                 generic_handle_irq(msi_data);
486                         }
487                 }
488         }
489
490         if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
491                 dev_warn(port->dev, "Slave unsupported request\n");
492
493         if (status & XILINX_PCIE_INTR_SLV_UNEXP)
494                 dev_warn(port->dev, "Slave unexpected completion\n");
495
496         if (status & XILINX_PCIE_INTR_SLV_COMPL)
497                 dev_warn(port->dev, "Slave completion timeout\n");
498
499         if (status & XILINX_PCIE_INTR_SLV_ERRP)
500                 dev_warn(port->dev, "Slave Error Poison\n");
501
502         if (status & XILINX_PCIE_INTR_SLV_CMPABT)
503                 dev_warn(port->dev, "Slave Completer Abort\n");
504
505         if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
506                 dev_warn(port->dev, "Slave Illegal Burst\n");
507
508         if (status & XILINX_PCIE_INTR_MST_DECERR)
509                 dev_warn(port->dev, "Master decode error\n");
510
511         if (status & XILINX_PCIE_INTR_MST_SLVERR)
512                 dev_warn(port->dev, "Master slave error\n");
513
514         if (status & XILINX_PCIE_INTR_MST_ERRP)
515                 dev_warn(port->dev, "Master error poison\n");
516
517         /* Clear the Interrupt Decode register */
518         pcie_write(port, status, XILINX_PCIE_REG_IDR);
519
520         return IRQ_HANDLED;
521 }
522
523 /**
524  * xilinx_pcie_free_irq_domain - Free IRQ domain
525  * @port: PCIe port information
526  */
527 static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
528 {
529         int i;
530         u32 irq, num_irqs;
531
532         /* Free IRQ Domain */
533         if (IS_ENABLED(CONFIG_PCI_MSI)) {
534
535                 free_pages(port->msi_pages, 0);
536
537                 num_irqs = XILINX_NUM_MSI_IRQS;
538         } else {
539                 /* INTx */
540                 num_irqs = 4;
541         }
542
543         for (i = 0; i < num_irqs; i++) {
544                 irq = irq_find_mapping(port->irq_domain, i);
545                 if (irq > 0)
546                         irq_dispose_mapping(irq);
547         }
548
549         irq_domain_remove(port->irq_domain);
550 }
551
552 /**
553  * xilinx_pcie_init_irq_domain - Initialize IRQ domain
554  * @port: PCIe port information
555  *
556  * Return: '0' on success and error value on failure
557  */
558 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
559 {
560         struct device *dev = port->dev;
561         struct device_node *node = dev->of_node;
562         struct device_node *pcie_intc_node;
563         int ret;
564
565         /* Setup INTx */
566         pcie_intc_node = of_get_next_child(node, NULL);
567         if (!pcie_intc_node) {
568                 dev_err(dev, "No PCIe Intc node found\n");
569                 return PTR_ERR(pcie_intc_node);
570         }
571
572         port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
573                                                  &intx_domain_ops,
574                                                  port);
575         if (!port->irq_domain) {
576                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
577                 return PTR_ERR(port->irq_domain);
578         }
579
580         /* Setup MSI */
581         if (IS_ENABLED(CONFIG_PCI_MSI)) {
582                 port->irq_domain = irq_domain_add_linear(node,
583                                                          XILINX_NUM_MSI_IRQS,
584                                                          &msi_domain_ops,
585                                                          &xilinx_pcie_msi_chip);
586                 if (!port->irq_domain) {
587                         dev_err(dev, "Failed to get a MSI IRQ domain\n");
588                         return PTR_ERR(port->irq_domain);
589                 }
590
591                 ret = xilinx_pcie_enable_msi(port);
592                 if (ret)
593                         return ret;
594         }
595
596         return 0;
597 }
598
599 /**
600  * xilinx_pcie_init_port - Initialize hardware
601  * @port: PCIe port information
602  */
603 static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
604 {
605         if (xilinx_pcie_link_is_up(port))
606                 dev_info(port->dev, "PCIe Link is UP\n");
607         else
608                 dev_info(port->dev, "PCIe Link is DOWN\n");
609
610         /* Disable all interrupts */
611         pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
612                    XILINX_PCIE_REG_IMR);
613
614         /* Clear pending interrupts */
615         pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
616                          XILINX_PCIE_IMR_ALL_MASK,
617                    XILINX_PCIE_REG_IDR);
618
619         /* Enable all interrupts */
620         pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
621
622         /* Enable the Bridge enable bit */
623         pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
624                          XILINX_PCIE_REG_RPSC_BEN,
625                    XILINX_PCIE_REG_RPSC);
626 }
627
628 /**
629  * xilinx_pcie_setup - Setup memory resources
630  * @nr: Bus number
631  * @sys: Per controller structure
632  *
633  * Return: '1' on success and error value on failure
634  */
635 static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
636 {
637         struct xilinx_pcie_port *port = sys_to_pcie(sys);
638
639         list_splice_init(&port->resources, &sys->resources);
640
641         return 1;
642 }
643
644 /**
645  * xilinx_pcie_scan_bus - Scan PCIe bus for devices
646  * @nr: Bus number
647  * @sys: Per controller structure
648  *
649  * Return: Valid Bus pointer on success and NULL on failure
650  */
651 static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
652 {
653         struct xilinx_pcie_port *port = sys_to_pcie(sys);
654         struct pci_bus *bus;
655
656         port->root_busno = sys->busnr;
657
658         if (IS_ENABLED(CONFIG_PCI_MSI))
659                 bus = pci_scan_root_bus_msi(port->dev, sys->busnr,
660                                             &xilinx_pcie_ops, sys,
661                                             &sys->resources,
662                                             &xilinx_pcie_msi_chip);
663         else
664                 bus = pci_scan_root_bus(port->dev, sys->busnr,
665                                         &xilinx_pcie_ops, sys, &sys->resources);
666         return bus;
667 }
668
669 /**
670  * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
671  * @port: PCIe port information
672  *
673  * Return: '0' on success and error value on failure
674  */
675 static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
676 {
677         struct device *dev = port->dev;
678         struct device_node *node = dev->of_node;
679         struct resource *mem;
680         resource_size_t offset;
681         struct of_pci_range_parser parser;
682         struct of_pci_range range;
683         struct resource_entry *win;
684         int err = 0, mem_resno = 0;
685
686         /* Get the ranges */
687         if (of_pci_range_parser_init(&parser, node)) {
688                 dev_err(dev, "missing \"ranges\" property\n");
689                 return -EINVAL;
690         }
691
692         /* Parse the ranges and add the resources found to the list */
693         for_each_of_pci_range(&parser, &range) {
694
695                 if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
696                         dev_err(dev, "Maximum memory resources exceeded\n");
697                         return -EINVAL;
698                 }
699
700                 mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
701                 if (!mem) {
702                         err = -ENOMEM;
703                         goto free_resources;
704                 }
705
706                 of_pci_range_to_resource(&range, node, mem);
707
708                 switch (mem->flags & IORESOURCE_TYPE_BITS) {
709                 case IORESOURCE_MEM:
710                         offset = range.cpu_addr - range.pci_addr;
711                         mem_resno++;
712                         break;
713                 default:
714                         err = -EINVAL;
715                         break;
716                 }
717
718                 if (err < 0) {
719                         dev_warn(dev, "Invalid resource found %pR\n", mem);
720                         continue;
721                 }
722
723                 err = request_resource(&iomem_resource, mem);
724                 if (err)
725                         goto free_resources;
726
727                 pci_add_resource_offset(&port->resources, mem, offset);
728         }
729
730         /* Get the bus range */
731         if (of_pci_parse_bus_range(node, &port->bus_range)) {
732                 u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
733                 u8 last;
734
735                 last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
736                         XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
737
738                 port->bus_range = (struct resource) {
739                         .name   = node->name,
740                         .start  = 0,
741                         .end    = last,
742                         .flags  = IORESOURCE_BUS,
743                 };
744         }
745
746         /* Register bus resource */
747         pci_add_resource(&port->resources, &port->bus_range);
748
749         return 0;
750
751 free_resources:
752         release_child_resources(&iomem_resource);
753         resource_list_for_each_entry(win, &port->resources)
754                 devm_kfree(dev, win->res);
755         pci_free_resource_list(&port->resources);
756
757         return err;
758 }
759
760 /**
761  * xilinx_pcie_parse_dt - Parse Device tree
762  * @port: PCIe port information
763  *
764  * Return: '0' on success and error value on failure
765  */
766 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
767 {
768         struct device *dev = port->dev;
769         struct device_node *node = dev->of_node;
770         struct resource regs;
771         const char *type;
772         int err;
773
774         type = of_get_property(node, "device_type", NULL);
775         if (!type || strcmp(type, "pci")) {
776                 dev_err(dev, "invalid \"device_type\" %s\n", type);
777                 return -EINVAL;
778         }
779
780         err = of_address_to_resource(node, 0, &regs);
781         if (err) {
782                 dev_err(dev, "missing \"reg\" property\n");
783                 return err;
784         }
785
786         port->reg_base = devm_ioremap_resource(dev, &regs);
787         if (IS_ERR(port->reg_base))
788                 return PTR_ERR(port->reg_base);
789
790         port->irq = irq_of_parse_and_map(node, 0);
791         err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
792                                IRQF_SHARED | IRQF_NO_THREAD,
793                                "xilinx-pcie", port);
794         if (err) {
795                 dev_err(dev, "unable to request irq %d\n", port->irq);
796                 return err;
797         }
798
799         return 0;
800 }
801
802 /**
803  * xilinx_pcie_probe - Probe function
804  * @pdev: Platform device pointer
805  *
806  * Return: '0' on success and error value on failure
807  */
808 static int xilinx_pcie_probe(struct platform_device *pdev)
809 {
810         struct xilinx_pcie_port *port;
811         struct hw_pci hw;
812         struct device *dev = &pdev->dev;
813         int err;
814
815         if (!dev->of_node)
816                 return -ENODEV;
817
818         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
819         if (!port)
820                 return -ENOMEM;
821
822         port->dev = dev;
823
824         err = xilinx_pcie_parse_dt(port);
825         if (err) {
826                 dev_err(dev, "Parsing DT failed\n");
827                 return err;
828         }
829
830         xilinx_pcie_init_port(port);
831
832         err = xilinx_pcie_init_irq_domain(port);
833         if (err) {
834                 dev_err(dev, "Failed creating IRQ Domain\n");
835                 return err;
836         }
837
838         /*
839          * Parse PCI ranges, configuration bus range and
840          * request their resources
841          */
842         INIT_LIST_HEAD(&port->resources);
843         err = xilinx_pcie_parse_and_add_res(port);
844         if (err) {
845                 dev_err(dev, "Failed adding resources\n");
846                 return err;
847         }
848
849         platform_set_drvdata(pdev, port);
850
851         /* Register the device */
852         memset(&hw, 0, sizeof(hw));
853         hw = (struct hw_pci) {
854                 .nr_controllers = 1,
855                 .private_data   = (void **)&port,
856                 .setup          = xilinx_pcie_setup,
857                 .map_irq        = of_irq_parse_and_map_pci,
858                 .scan           = xilinx_pcie_scan_bus,
859                 .ops            = &xilinx_pcie_ops,
860         };
861
862 #ifdef CONFIG_PCI_MSI
863         xilinx_pcie_msi_chip.dev = port->dev;
864 #endif
865         pci_common_init_dev(dev, &hw);
866
867         return 0;
868 }
869
870 /**
871  * xilinx_pcie_remove - Remove function
872  * @pdev: Platform device pointer
873  *
874  * Return: '0' always
875  */
876 static int xilinx_pcie_remove(struct platform_device *pdev)
877 {
878         struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
879
880         xilinx_pcie_free_irq_domain(port);
881
882         return 0;
883 }
884
885 static struct of_device_id xilinx_pcie_of_match[] = {
886         { .compatible = "xlnx,axi-pcie-host-1.00.a", },
887         {}
888 };
889
890 static struct platform_driver xilinx_pcie_driver = {
891         .driver = {
892                 .name = "xilinx-pcie",
893                 .of_match_table = xilinx_pcie_of_match,
894                 .suppress_bind_attrs = true,
895         },
896         .probe = xilinx_pcie_probe,
897         .remove = xilinx_pcie_remove,
898 };
899 module_platform_driver(xilinx_pcie_driver);
900
901 MODULE_AUTHOR("Xilinx Inc");
902 MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
903 MODULE_LICENSE("GPL v2");