blob: f41bc601b7b031e9bd7d0ea0a4e3fec1eec739e6 [file] [log] [blame]
Srikanth Thokala8961def2014-08-20 21:56:02 +05301/*
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 */
111struct 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
122static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
123
124static inline struct xilinx_pcie_port *sys_to_pcie(struct pci_sys_data *sys)
125{
126 return sys->private_data;
127}
128
129static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
130{
131 return readl(port->reg_base + reg);
132}
133
134static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
135{
136 writel(val, port->reg_base + reg);
137}
138
139static 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 */
149static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
150{
151 u32 val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
152
153 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
154 dev_dbg(port->dev, "Requester ID %d\n",
155 val & XILINX_PCIE_RPEFR_REQ_ID);
156 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
157 XILINX_PCIE_REG_RPEFR);
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 */
168static 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_config_base - 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 */
200static void __iomem *xilinx_pcie_config_base(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 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
207 (devfn << ECAM_DEV_NUM_SHIFT);
208
209 return port->reg_base + relbus + where;
210}
211
212/**
213 * xilinx_pcie_read_config - Read configuration space
214 * @bus: PCI Bus structure
215 * @devfn: Device/function
216 * @where: Offset from base
217 * @size: Byte/word/dword
218 * @val: Value to be read
219 *
220 * Return: PCIBIOS_SUCCESSFUL on success
221 * PCIBIOS_DEVICE_NOT_FOUND on failure
222 */
223static int xilinx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
224 int where, int size, u32 *val)
225{
226 void __iomem *addr;
227
228 if (!xilinx_pcie_valid_device(bus, devfn)) {
229 *val = 0xFFFFFFFF;
230 return PCIBIOS_DEVICE_NOT_FOUND;
231 }
232
233 addr = xilinx_pcie_config_base(bus, devfn, where);
234
235 switch (size) {
236 case 1:
237 *val = readb(addr);
238 break;
239 case 2:
240 *val = readw(addr);
241 break;
242 default:
243 *val = readl(addr);
244 break;
245 }
246
247 return PCIBIOS_SUCCESSFUL;
248}
249
250/**
251 * xilinx_pcie_write_config - Write configuration space
252 * @bus: PCI Bus structure
253 * @devfn: Device/function
254 * @where: Offset from base
255 * @size: Byte/word/dword
256 * @val: Value to be written to device
257 *
258 * Return: PCIBIOS_SUCCESSFUL on success
259 * PCIBIOS_DEVICE_NOT_FOUND on failure
260 */
261static int xilinx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
262 int where, int size, u32 val)
263{
264 void __iomem *addr;
265
266 if (!xilinx_pcie_valid_device(bus, devfn))
267 return PCIBIOS_DEVICE_NOT_FOUND;
268
269 addr = xilinx_pcie_config_base(bus, devfn, where);
270
271 switch (size) {
272 case 1:
273 writeb(val, addr);
274 break;
275 case 2:
276 writew(val, addr);
277 break;
278 default:
279 writel(val, addr);
280 break;
281 }
282
283 return PCIBIOS_SUCCESSFUL;
284}
285
286/* PCIe operations */
287static struct pci_ops xilinx_pcie_ops = {
288 .read = xilinx_pcie_read_config,
289 .write = xilinx_pcie_write_config,
290};
291
292/* MSI functions */
293
294/**
295 * xilinx_pcie_destroy_msi - Free MSI number
296 * @irq: IRQ to be freed
297 */
298static void xilinx_pcie_destroy_msi(unsigned int irq)
299{
300 struct irq_desc *desc;
301 struct msi_desc *msi;
302 struct xilinx_pcie_port *port;
303
304 desc = irq_to_desc(irq);
305 msi = irq_desc_get_msi_desc(desc);
306 port = sys_to_pcie(msi->dev->bus->sysdata);
307
308 if (!test_bit(irq, msi_irq_in_use))
309 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
310 else
311 clear_bit(irq, msi_irq_in_use);
312}
313
314/**
315 * xilinx_pcie_assign_msi - Allocate MSI number
316 * @port: PCIe port structure
317 *
318 * Return: A valid IRQ on success and error value on failure.
319 */
320static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
321{
322 int pos;
323
324 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
325 if (pos < XILINX_NUM_MSI_IRQS)
326 set_bit(pos, msi_irq_in_use);
327 else
328 return -ENOSPC;
329
330 return pos;
331}
332
333/**
334 * xilinx_msi_teardown_irq - Destroy the MSI
335 * @chip: MSI Chip descriptor
336 * @irq: MSI IRQ to destroy
337 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700338static void xilinx_msi_teardown_irq(struct msi_controller *chip,
339 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530340{
341 xilinx_pcie_destroy_msi(irq);
342}
343
344/**
345 * xilinx_pcie_msi_setup_irq - Setup MSI request
346 * @chip: MSI chip pointer
347 * @pdev: PCIe device pointer
348 * @desc: MSI descriptor pointer
349 *
350 * Return: '0' on success and error value on failure
351 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700352static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530353 struct pci_dev *pdev,
354 struct msi_desc *desc)
355{
356 struct xilinx_pcie_port *port = sys_to_pcie(pdev->bus->sysdata);
357 unsigned int irq;
358 int hwirq;
359 struct msi_msg msg;
360 phys_addr_t msg_addr;
361
362 hwirq = xilinx_pcie_assign_msi(port);
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300363 if (hwirq < 0)
364 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530365
366 irq = irq_create_mapping(port->irq_domain, hwirq);
367 if (!irq)
368 return -EINVAL;
369
370 irq_set_msi_desc(irq, desc);
371
372 msg_addr = virt_to_phys((void *)port->msi_pages);
373
374 msg.address_hi = 0;
375 msg.address_lo = msg_addr;
376 msg.data = irq;
377
378 write_msi_msg(irq, &msg);
379
380 return 0;
381}
382
383/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700384static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530385 .setup_irq = xilinx_pcie_msi_setup_irq,
386 .teardown_irq = xilinx_msi_teardown_irq,
387};
388
389/* HW Interrupt Chip Descriptor */
390static struct irq_chip xilinx_msi_irq_chip = {
391 .name = "Xilinx PCIe MSI",
392 .irq_enable = unmask_msi_irq,
393 .irq_disable = mask_msi_irq,
394 .irq_mask = mask_msi_irq,
395 .irq_unmask = unmask_msi_irq,
396};
397
398/**
399 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
400 * @domain: IRQ domain
401 * @irq: Virtual IRQ number
402 * @hwirq: HW interrupt number
403 *
404 * Return: Always returns 0.
405 */
406static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
407 irq_hw_number_t hwirq)
408{
409 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
410 irq_set_chip_data(irq, domain->host_data);
411 set_irq_flags(irq, IRQF_VALID);
412
413 return 0;
414}
415
416/* IRQ Domain operations */
417static const struct irq_domain_ops msi_domain_ops = {
418 .map = xilinx_pcie_msi_map,
419};
420
421/**
422 * xilinx_pcie_enable_msi - Enable MSI support
423 * @port: PCIe port information
424 */
425static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
426{
427 phys_addr_t msg_addr;
428
429 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
430 msg_addr = virt_to_phys((void *)port->msi_pages);
431 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
432 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
433}
434
435/**
436 * xilinx_pcie_add_bus - Add MSI chip info to PCIe bus
437 * @bus: PCIe bus
438 */
439static void xilinx_pcie_add_bus(struct pci_bus *bus)
440{
441 if (IS_ENABLED(CONFIG_PCI_MSI)) {
442 struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
443
444 xilinx_pcie_msi_chip.dev = port->dev;
445 bus->msi = &xilinx_pcie_msi_chip;
446 }
447}
448
449/* INTx Functions */
450
451/**
452 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
453 * @domain: IRQ domain
454 * @irq: Virtual IRQ number
455 * @hwirq: HW interrupt number
456 *
457 * Return: Always returns 0.
458 */
459static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
460 irq_hw_number_t hwirq)
461{
462 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
463 irq_set_chip_data(irq, domain->host_data);
464 set_irq_flags(irq, IRQF_VALID);
465
466 return 0;
467}
468
469/* INTx IRQ Domain operations */
470static const struct irq_domain_ops intx_domain_ops = {
471 .map = xilinx_pcie_intx_map,
472};
473
474/* PCIe HW Functions */
475
476/**
477 * xilinx_pcie_intr_handler - Interrupt Service Handler
478 * @irq: IRQ number
479 * @data: PCIe port information
480 *
481 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
482 */
483static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
484{
485 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
486 u32 val, mask, status, msi_data;
487
488 /* Read interrupt decode and mask registers */
489 val = pcie_read(port, XILINX_PCIE_REG_IDR);
490 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
491
492 status = val & mask;
493 if (!status)
494 return IRQ_NONE;
495
496 if (status & XILINX_PCIE_INTR_LINK_DOWN)
497 dev_warn(port->dev, "Link Down\n");
498
499 if (status & XILINX_PCIE_INTR_ECRC_ERR)
500 dev_warn(port->dev, "ECRC failed\n");
501
502 if (status & XILINX_PCIE_INTR_STR_ERR)
503 dev_warn(port->dev, "Streaming error\n");
504
505 if (status & XILINX_PCIE_INTR_HOT_RESET)
506 dev_info(port->dev, "Hot reset\n");
507
508 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
509 dev_warn(port->dev, "ECAM access timeout\n");
510
511 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
512 dev_warn(port->dev, "Correctable error message\n");
513 xilinx_pcie_clear_err_interrupts(port);
514 }
515
516 if (status & XILINX_PCIE_INTR_NONFATAL) {
517 dev_warn(port->dev, "Non fatal error message\n");
518 xilinx_pcie_clear_err_interrupts(port);
519 }
520
521 if (status & XILINX_PCIE_INTR_FATAL) {
522 dev_warn(port->dev, "Fatal error message\n");
523 xilinx_pcie_clear_err_interrupts(port);
524 }
525
526 if (status & XILINX_PCIE_INTR_INTX) {
527 /* INTx interrupt received */
528 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
529
530 /* Check whether interrupt valid */
531 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
532 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
533 return IRQ_HANDLED;
534 }
535
536 /* Clear interrupt FIFO register 1 */
537 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
538 XILINX_PCIE_REG_RPIFR1);
539
540 /* Handle INTx Interrupt */
541 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
542 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
543 generic_handle_irq(irq_find_mapping(port->irq_domain, val));
544 }
545
546 if (status & XILINX_PCIE_INTR_MSI) {
547 /* MSI Interrupt */
548 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
549
550 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
551 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
552 return IRQ_HANDLED;
553 }
554
555 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
556 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
557 XILINX_PCIE_RPIFR2_MSG_DATA;
558
559 /* Clear interrupt FIFO register 1 */
560 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
561 XILINX_PCIE_REG_RPIFR1);
562
563 if (IS_ENABLED(CONFIG_PCI_MSI)) {
564 /* Handle MSI Interrupt */
565 generic_handle_irq(msi_data);
566 }
567 }
568 }
569
570 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
571 dev_warn(port->dev, "Slave unsupported request\n");
572
573 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
574 dev_warn(port->dev, "Slave unexpected completion\n");
575
576 if (status & XILINX_PCIE_INTR_SLV_COMPL)
577 dev_warn(port->dev, "Slave completion timeout\n");
578
579 if (status & XILINX_PCIE_INTR_SLV_ERRP)
580 dev_warn(port->dev, "Slave Error Poison\n");
581
582 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
583 dev_warn(port->dev, "Slave Completer Abort\n");
584
585 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
586 dev_warn(port->dev, "Slave Illegal Burst\n");
587
588 if (status & XILINX_PCIE_INTR_MST_DECERR)
589 dev_warn(port->dev, "Master decode error\n");
590
591 if (status & XILINX_PCIE_INTR_MST_SLVERR)
592 dev_warn(port->dev, "Master slave error\n");
593
594 if (status & XILINX_PCIE_INTR_MST_ERRP)
595 dev_warn(port->dev, "Master error poison\n");
596
597 /* Clear the Interrupt Decode register */
598 pcie_write(port, status, XILINX_PCIE_REG_IDR);
599
600 return IRQ_HANDLED;
601}
602
603/**
604 * xilinx_pcie_free_irq_domain - Free IRQ domain
605 * @port: PCIe port information
606 */
607static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
608{
609 int i;
610 u32 irq, num_irqs;
611
612 /* Free IRQ Domain */
613 if (IS_ENABLED(CONFIG_PCI_MSI)) {
614
615 free_pages(port->msi_pages, 0);
616
617 num_irqs = XILINX_NUM_MSI_IRQS;
618 } else {
619 /* INTx */
620 num_irqs = 4;
621 }
622
623 for (i = 0; i < num_irqs; i++) {
624 irq = irq_find_mapping(port->irq_domain, i);
625 if (irq > 0)
626 irq_dispose_mapping(irq);
627 }
628
629 irq_domain_remove(port->irq_domain);
630}
631
632/**
633 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
634 * @port: PCIe port information
635 *
636 * Return: '0' on success and error value on failure
637 */
638static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
639{
640 struct device *dev = port->dev;
641 struct device_node *node = dev->of_node;
642 struct device_node *pcie_intc_node;
643
644 /* Setup INTx */
645 pcie_intc_node = of_get_next_child(node, NULL);
646 if (!pcie_intc_node) {
647 dev_err(dev, "No PCIe Intc node found\n");
648 return PTR_ERR(pcie_intc_node);
649 }
650
651 port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
652 &intx_domain_ops,
653 port);
654 if (!port->irq_domain) {
655 dev_err(dev, "Failed to get a INTx IRQ domain\n");
656 return PTR_ERR(port->irq_domain);
657 }
658
659 /* Setup MSI */
660 if (IS_ENABLED(CONFIG_PCI_MSI)) {
661 port->irq_domain = irq_domain_add_linear(node,
662 XILINX_NUM_MSI_IRQS,
663 &msi_domain_ops,
664 &xilinx_pcie_msi_chip);
665 if (!port->irq_domain) {
666 dev_err(dev, "Failed to get a MSI IRQ domain\n");
667 return PTR_ERR(port->irq_domain);
668 }
669
670 xilinx_pcie_enable_msi(port);
671 }
672
673 return 0;
674}
675
676/**
677 * xilinx_pcie_init_port - Initialize hardware
678 * @port: PCIe port information
679 */
680static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
681{
682 if (xilinx_pcie_link_is_up(port))
683 dev_info(port->dev, "PCIe Link is UP\n");
684 else
685 dev_info(port->dev, "PCIe Link is DOWN\n");
686
687 /* Disable all interrupts */
688 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
689 XILINX_PCIE_REG_IMR);
690
691 /* Clear pending interrupts */
692 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
693 XILINX_PCIE_IMR_ALL_MASK,
694 XILINX_PCIE_REG_IDR);
695
696 /* Enable all interrupts */
697 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
698
699 /* Enable the Bridge enable bit */
700 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
701 XILINX_PCIE_REG_RPSC_BEN,
702 XILINX_PCIE_REG_RPSC);
703}
704
705/**
706 * xilinx_pcie_setup - Setup memory resources
707 * @nr: Bus number
708 * @sys: Per controller structure
709 *
710 * Return: '1' on success and error value on failure
711 */
712static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
713{
714 struct xilinx_pcie_port *port = sys_to_pcie(sys);
715
716 list_splice_init(&port->resources, &sys->resources);
717
718 return 1;
719}
720
721/**
722 * xilinx_pcie_scan_bus - Scan PCIe bus for devices
723 * @nr: Bus number
724 * @sys: Per controller structure
725 *
726 * Return: Valid Bus pointer on success and NULL on failure
727 */
728static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
729{
730 struct xilinx_pcie_port *port = sys_to_pcie(sys);
731 struct pci_bus *bus;
732
733 port->root_busno = sys->busnr;
734 bus = pci_scan_root_bus(port->dev, sys->busnr, &xilinx_pcie_ops,
735 sys, &sys->resources);
736
737 return bus;
738}
739
740/**
741 * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
742 * @port: PCIe port information
743 *
744 * Return: '0' on success and error value on failure
745 */
746static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
747{
748 struct device *dev = port->dev;
749 struct device_node *node = dev->of_node;
750 struct resource *mem;
751 resource_size_t offset;
752 struct of_pci_range_parser parser;
753 struct of_pci_range range;
754 struct pci_host_bridge_window *win;
755 int err = 0, mem_resno = 0;
756
757 /* Get the ranges */
758 if (of_pci_range_parser_init(&parser, node)) {
759 dev_err(dev, "missing \"ranges\" property\n");
760 return -EINVAL;
761 }
762
763 /* Parse the ranges and add the resources found to the list */
764 for_each_of_pci_range(&parser, &range) {
765
766 if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
767 dev_err(dev, "Maximum memory resources exceeded\n");
768 return -EINVAL;
769 }
770
771 mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
772 if (!mem) {
773 err = -ENOMEM;
774 goto free_resources;
775 }
776
777 of_pci_range_to_resource(&range, node, mem);
778
779 switch (mem->flags & IORESOURCE_TYPE_BITS) {
780 case IORESOURCE_MEM:
781 offset = range.cpu_addr - range.pci_addr;
782 mem_resno++;
783 break;
784 default:
785 err = -EINVAL;
786 break;
787 }
788
789 if (err < 0) {
790 dev_warn(dev, "Invalid resource found %pR\n", mem);
791 continue;
792 }
793
794 err = request_resource(&iomem_resource, mem);
795 if (err)
796 goto free_resources;
797
798 pci_add_resource_offset(&port->resources, mem, offset);
799 }
800
801 /* Get the bus range */
802 if (of_pci_parse_bus_range(node, &port->bus_range)) {
803 u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
804 u8 last;
805
806 last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
807 XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
808
809 port->bus_range = (struct resource) {
810 .name = node->name,
811 .start = 0,
812 .end = last,
813 .flags = IORESOURCE_BUS,
814 };
815 }
816
817 /* Register bus resource */
818 pci_add_resource(&port->resources, &port->bus_range);
819
820 return 0;
821
822free_resources:
823 release_child_resources(&iomem_resource);
824 list_for_each_entry(win, &port->resources, list)
825 devm_kfree(dev, win->res);
826 pci_free_resource_list(&port->resources);
827
828 return err;
829}
830
831/**
832 * xilinx_pcie_parse_dt - Parse Device tree
833 * @port: PCIe port information
834 *
835 * Return: '0' on success and error value on failure
836 */
837static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
838{
839 struct device *dev = port->dev;
840 struct device_node *node = dev->of_node;
841 struct resource regs;
842 const char *type;
843 int err;
844
845 type = of_get_property(node, "device_type", NULL);
846 if (!type || strcmp(type, "pci")) {
847 dev_err(dev, "invalid \"device_type\" %s\n", type);
848 return -EINVAL;
849 }
850
851 err = of_address_to_resource(node, 0, &regs);
852 if (err) {
853 dev_err(dev, "missing \"reg\" property\n");
854 return err;
855 }
856
857 port->reg_base = devm_ioremap_resource(dev, &regs);
858 if (IS_ERR(port->reg_base))
859 return PTR_ERR(port->reg_base);
860
861 port->irq = irq_of_parse_and_map(node, 0);
862 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
863 IRQF_SHARED, "xilinx-pcie", port);
864 if (err) {
865 dev_err(dev, "unable to request irq %d\n", port->irq);
866 return err;
867 }
868
869 return 0;
870}
871
872/**
873 * xilinx_pcie_probe - Probe function
874 * @pdev: Platform device pointer
875 *
876 * Return: '0' on success and error value on failure
877 */
878static int xilinx_pcie_probe(struct platform_device *pdev)
879{
880 struct xilinx_pcie_port *port;
881 struct hw_pci hw;
882 struct device *dev = &pdev->dev;
883 int err;
884
885 if (!dev->of_node)
886 return -ENODEV;
887
888 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
889 if (!port)
890 return -ENOMEM;
891
892 port->dev = dev;
893
894 err = xilinx_pcie_parse_dt(port);
895 if (err) {
896 dev_err(dev, "Parsing DT failed\n");
897 return err;
898 }
899
900 xilinx_pcie_init_port(port);
901
902 err = xilinx_pcie_init_irq_domain(port);
903 if (err) {
904 dev_err(dev, "Failed creating IRQ Domain\n");
905 return err;
906 }
907
908 /*
909 * Parse PCI ranges, configuration bus range and
910 * request their resources
911 */
912 INIT_LIST_HEAD(&port->resources);
913 err = xilinx_pcie_parse_and_add_res(port);
914 if (err) {
915 dev_err(dev, "Failed adding resources\n");
916 return err;
917 }
918
919 platform_set_drvdata(pdev, port);
920
921 /* Register the device */
922 memset(&hw, 0, sizeof(hw));
923 hw = (struct hw_pci) {
924 .nr_controllers = 1,
925 .private_data = (void **)&port,
926 .setup = xilinx_pcie_setup,
927 .map_irq = of_irq_parse_and_map_pci,
928 .add_bus = xilinx_pcie_add_bus,
929 .scan = xilinx_pcie_scan_bus,
930 .ops = &xilinx_pcie_ops,
931 };
932 pci_common_init_dev(dev, &hw);
933
934 return 0;
935}
936
937/**
938 * xilinx_pcie_remove - Remove function
939 * @pdev: Platform device pointer
940 *
941 * Return: '0' always
942 */
943static int xilinx_pcie_remove(struct platform_device *pdev)
944{
945 struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
946
947 xilinx_pcie_free_irq_domain(port);
948
949 return 0;
950}
951
952static struct of_device_id xilinx_pcie_of_match[] = {
953 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
954 {}
955};
956
957static struct platform_driver xilinx_pcie_driver = {
958 .driver = {
959 .name = "xilinx-pcie",
960 .owner = THIS_MODULE,
961 .of_match_table = xilinx_pcie_of_match,
962 .suppress_bind_attrs = true,
963 },
964 .probe = xilinx_pcie_probe,
965 .remove = xilinx_pcie_remove,
966};
967module_platform_driver(xilinx_pcie_driver);
968
969MODULE_AUTHOR("Xilinx Inc");
970MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
971MODULE_LICENSE("GPL v2");