blob: 64454f4166396104126608f3c6cc001444695255 [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{
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100151 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530152
153 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100154 dev_dbg(port->dev, "Requester ID %lu\n",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530155 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/**
Rob Herring029e2152015-01-09 20:34:50 -0600192 * xilinx_pcie_map_bus - Get configuration base
Srikanth Thokala8961def2014-08-20 21:56:02 +0530193 * @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 */
Rob Herring029e2152015-01-09 20:34:50 -0600200static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
201 unsigned int devfn, int where)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530202{
203 struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
204 int relbus;
205
Rob Herring029e2152015-01-09 20:34:50 -0600206 if (!xilinx_pcie_valid_device(bus, devfn))
207 return NULL;
208
Srikanth Thokala8961def2014-08-20 21:56:02 +0530209 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
210 (devfn << ECAM_DEV_NUM_SHIFT);
211
212 return port->reg_base + relbus + where;
213}
214
Srikanth Thokala8961def2014-08-20 21:56:02 +0530215/* PCIe operations */
216static struct pci_ops xilinx_pcie_ops = {
Rob Herring029e2152015-01-09 20:34:50 -0600217 .map_bus = xilinx_pcie_map_bus,
218 .read = pci_generic_config_read,
219 .write = pci_generic_config_write,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530220};
221
222/* MSI functions */
223
224/**
225 * xilinx_pcie_destroy_msi - Free MSI number
226 * @irq: IRQ to be freed
227 */
228static void xilinx_pcie_destroy_msi(unsigned int irq)
229{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530230 struct msi_desc *msi;
231 struct xilinx_pcie_port *port;
232
Jiang Liue39758e2015-07-09 16:00:43 +0800233 if (!test_bit(irq, msi_irq_in_use)) {
234 msi = irq_get_msi_desc(irq);
235 port = sys_to_pcie(msi_desc_to_pci_sys_data(msi));
Srikanth Thokala8961def2014-08-20 21:56:02 +0530236 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800237 } else {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530238 clear_bit(irq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800239 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530240}
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 */
248static 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 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700266static void xilinx_msi_teardown_irq(struct msi_controller *chip,
267 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530268{
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 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700280static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530281 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);
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300291 if (hwirq < 0)
292 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530293
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
Jiang Liu83a18912014-11-09 23:10:34 +0800306 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530307
308 return 0;
309}
310
311/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700312static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530313 .setup_irq = xilinx_pcie_msi_setup_irq,
314 .teardown_irq = xilinx_msi_teardown_irq,
315};
316
317/* HW Interrupt Chip Descriptor */
318static struct irq_chip xilinx_msi_irq_chip = {
319 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100320 .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,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530324};
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 */
334static 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 set_irq_flags(irq, IRQF_VALID);
340
341 return 0;
342}
343
344/* IRQ Domain operations */
345static const struct irq_domain_ops msi_domain_ops = {
346 .map = xilinx_pcie_msi_map,
347};
348
349/**
350 * xilinx_pcie_enable_msi - Enable MSI support
351 * @port: PCIe port information
352 */
353static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
354{
355 phys_addr_t msg_addr;
356
357 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
358 msg_addr = virt_to_phys((void *)port->msi_pages);
359 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
360 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
361}
362
Srikanth Thokala8961def2014-08-20 21:56:02 +0530363/* INTx Functions */
364
365/**
366 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
367 * @domain: IRQ domain
368 * @irq: Virtual IRQ number
369 * @hwirq: HW interrupt number
370 *
371 * Return: Always returns 0.
372 */
373static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
374 irq_hw_number_t hwirq)
375{
376 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
377 irq_set_chip_data(irq, domain->host_data);
378 set_irq_flags(irq, IRQF_VALID);
379
380 return 0;
381}
382
383/* INTx IRQ Domain operations */
384static const struct irq_domain_ops intx_domain_ops = {
385 .map = xilinx_pcie_intx_map,
386};
387
388/* PCIe HW Functions */
389
390/**
391 * xilinx_pcie_intr_handler - Interrupt Service Handler
392 * @irq: IRQ number
393 * @data: PCIe port information
394 *
395 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
396 */
397static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
398{
399 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
400 u32 val, mask, status, msi_data;
401
402 /* Read interrupt decode and mask registers */
403 val = pcie_read(port, XILINX_PCIE_REG_IDR);
404 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
405
406 status = val & mask;
407 if (!status)
408 return IRQ_NONE;
409
410 if (status & XILINX_PCIE_INTR_LINK_DOWN)
411 dev_warn(port->dev, "Link Down\n");
412
413 if (status & XILINX_PCIE_INTR_ECRC_ERR)
414 dev_warn(port->dev, "ECRC failed\n");
415
416 if (status & XILINX_PCIE_INTR_STR_ERR)
417 dev_warn(port->dev, "Streaming error\n");
418
419 if (status & XILINX_PCIE_INTR_HOT_RESET)
420 dev_info(port->dev, "Hot reset\n");
421
422 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
423 dev_warn(port->dev, "ECAM access timeout\n");
424
425 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
426 dev_warn(port->dev, "Correctable error message\n");
427 xilinx_pcie_clear_err_interrupts(port);
428 }
429
430 if (status & XILINX_PCIE_INTR_NONFATAL) {
431 dev_warn(port->dev, "Non fatal error message\n");
432 xilinx_pcie_clear_err_interrupts(port);
433 }
434
435 if (status & XILINX_PCIE_INTR_FATAL) {
436 dev_warn(port->dev, "Fatal error message\n");
437 xilinx_pcie_clear_err_interrupts(port);
438 }
439
440 if (status & XILINX_PCIE_INTR_INTX) {
441 /* INTx interrupt received */
442 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
443
444 /* Check whether interrupt valid */
445 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
446 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
447 return IRQ_HANDLED;
448 }
449
450 /* Clear interrupt FIFO register 1 */
451 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
452 XILINX_PCIE_REG_RPIFR1);
453
454 /* Handle INTx Interrupt */
455 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
456 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
457 generic_handle_irq(irq_find_mapping(port->irq_domain, val));
458 }
459
460 if (status & XILINX_PCIE_INTR_MSI) {
461 /* MSI Interrupt */
462 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
463
464 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
465 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
466 return IRQ_HANDLED;
467 }
468
469 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
470 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
471 XILINX_PCIE_RPIFR2_MSG_DATA;
472
473 /* Clear interrupt FIFO register 1 */
474 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
475 XILINX_PCIE_REG_RPIFR1);
476
477 if (IS_ENABLED(CONFIG_PCI_MSI)) {
478 /* Handle MSI Interrupt */
479 generic_handle_irq(msi_data);
480 }
481 }
482 }
483
484 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
485 dev_warn(port->dev, "Slave unsupported request\n");
486
487 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
488 dev_warn(port->dev, "Slave unexpected completion\n");
489
490 if (status & XILINX_PCIE_INTR_SLV_COMPL)
491 dev_warn(port->dev, "Slave completion timeout\n");
492
493 if (status & XILINX_PCIE_INTR_SLV_ERRP)
494 dev_warn(port->dev, "Slave Error Poison\n");
495
496 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
497 dev_warn(port->dev, "Slave Completer Abort\n");
498
499 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
500 dev_warn(port->dev, "Slave Illegal Burst\n");
501
502 if (status & XILINX_PCIE_INTR_MST_DECERR)
503 dev_warn(port->dev, "Master decode error\n");
504
505 if (status & XILINX_PCIE_INTR_MST_SLVERR)
506 dev_warn(port->dev, "Master slave error\n");
507
508 if (status & XILINX_PCIE_INTR_MST_ERRP)
509 dev_warn(port->dev, "Master error poison\n");
510
511 /* Clear the Interrupt Decode register */
512 pcie_write(port, status, XILINX_PCIE_REG_IDR);
513
514 return IRQ_HANDLED;
515}
516
517/**
518 * xilinx_pcie_free_irq_domain - Free IRQ domain
519 * @port: PCIe port information
520 */
521static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
522{
523 int i;
524 u32 irq, num_irqs;
525
526 /* Free IRQ Domain */
527 if (IS_ENABLED(CONFIG_PCI_MSI)) {
528
529 free_pages(port->msi_pages, 0);
530
531 num_irqs = XILINX_NUM_MSI_IRQS;
532 } else {
533 /* INTx */
534 num_irqs = 4;
535 }
536
537 for (i = 0; i < num_irqs; i++) {
538 irq = irq_find_mapping(port->irq_domain, i);
539 if (irq > 0)
540 irq_dispose_mapping(irq);
541 }
542
543 irq_domain_remove(port->irq_domain);
544}
545
546/**
547 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
548 * @port: PCIe port information
549 *
550 * Return: '0' on success and error value on failure
551 */
552static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
553{
554 struct device *dev = port->dev;
555 struct device_node *node = dev->of_node;
556 struct device_node *pcie_intc_node;
557
558 /* Setup INTx */
559 pcie_intc_node = of_get_next_child(node, NULL);
560 if (!pcie_intc_node) {
561 dev_err(dev, "No PCIe Intc node found\n");
562 return PTR_ERR(pcie_intc_node);
563 }
564
565 port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
566 &intx_domain_ops,
567 port);
568 if (!port->irq_domain) {
569 dev_err(dev, "Failed to get a INTx IRQ domain\n");
570 return PTR_ERR(port->irq_domain);
571 }
572
573 /* Setup MSI */
574 if (IS_ENABLED(CONFIG_PCI_MSI)) {
575 port->irq_domain = irq_domain_add_linear(node,
576 XILINX_NUM_MSI_IRQS,
577 &msi_domain_ops,
578 &xilinx_pcie_msi_chip);
579 if (!port->irq_domain) {
580 dev_err(dev, "Failed to get a MSI IRQ domain\n");
581 return PTR_ERR(port->irq_domain);
582 }
583
584 xilinx_pcie_enable_msi(port);
585 }
586
587 return 0;
588}
589
590/**
591 * xilinx_pcie_init_port - Initialize hardware
592 * @port: PCIe port information
593 */
594static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
595{
596 if (xilinx_pcie_link_is_up(port))
597 dev_info(port->dev, "PCIe Link is UP\n");
598 else
599 dev_info(port->dev, "PCIe Link is DOWN\n");
600
601 /* Disable all interrupts */
602 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
603 XILINX_PCIE_REG_IMR);
604
605 /* Clear pending interrupts */
606 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
607 XILINX_PCIE_IMR_ALL_MASK,
608 XILINX_PCIE_REG_IDR);
609
610 /* Enable all interrupts */
611 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
612
613 /* Enable the Bridge enable bit */
614 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
615 XILINX_PCIE_REG_RPSC_BEN,
616 XILINX_PCIE_REG_RPSC);
617}
618
619/**
620 * xilinx_pcie_setup - Setup memory resources
621 * @nr: Bus number
622 * @sys: Per controller structure
623 *
624 * Return: '1' on success and error value on failure
625 */
626static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
627{
628 struct xilinx_pcie_port *port = sys_to_pcie(sys);
629
630 list_splice_init(&port->resources, &sys->resources);
631
632 return 1;
633}
634
635/**
636 * xilinx_pcie_scan_bus - Scan PCIe bus for devices
637 * @nr: Bus number
638 * @sys: Per controller structure
639 *
640 * Return: Valid Bus pointer on success and NULL on failure
641 */
642static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
643{
644 struct xilinx_pcie_port *port = sys_to_pcie(sys);
645 struct pci_bus *bus;
646
647 port->root_busno = sys->busnr;
648 bus = pci_scan_root_bus(port->dev, sys->busnr, &xilinx_pcie_ops,
649 sys, &sys->resources);
650
651 return bus;
652}
653
654/**
655 * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
656 * @port: PCIe port information
657 *
658 * Return: '0' on success and error value on failure
659 */
660static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
661{
662 struct device *dev = port->dev;
663 struct device_node *node = dev->of_node;
664 struct resource *mem;
665 resource_size_t offset;
666 struct of_pci_range_parser parser;
667 struct of_pci_range range;
Jiang Liu14d76b62015-02-05 13:44:44 +0800668 struct resource_entry *win;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530669 int err = 0, mem_resno = 0;
670
671 /* Get the ranges */
672 if (of_pci_range_parser_init(&parser, node)) {
673 dev_err(dev, "missing \"ranges\" property\n");
674 return -EINVAL;
675 }
676
677 /* Parse the ranges and add the resources found to the list */
678 for_each_of_pci_range(&parser, &range) {
679
680 if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
681 dev_err(dev, "Maximum memory resources exceeded\n");
682 return -EINVAL;
683 }
684
685 mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
686 if (!mem) {
687 err = -ENOMEM;
688 goto free_resources;
689 }
690
691 of_pci_range_to_resource(&range, node, mem);
692
693 switch (mem->flags & IORESOURCE_TYPE_BITS) {
694 case IORESOURCE_MEM:
695 offset = range.cpu_addr - range.pci_addr;
696 mem_resno++;
697 break;
698 default:
699 err = -EINVAL;
700 break;
701 }
702
703 if (err < 0) {
704 dev_warn(dev, "Invalid resource found %pR\n", mem);
705 continue;
706 }
707
708 err = request_resource(&iomem_resource, mem);
709 if (err)
710 goto free_resources;
711
712 pci_add_resource_offset(&port->resources, mem, offset);
713 }
714
715 /* Get the bus range */
716 if (of_pci_parse_bus_range(node, &port->bus_range)) {
717 u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
718 u8 last;
719
720 last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
721 XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
722
723 port->bus_range = (struct resource) {
724 .name = node->name,
725 .start = 0,
726 .end = last,
727 .flags = IORESOURCE_BUS,
728 };
729 }
730
731 /* Register bus resource */
732 pci_add_resource(&port->resources, &port->bus_range);
733
734 return 0;
735
736free_resources:
737 release_child_resources(&iomem_resource);
Jiang Liu14d76b62015-02-05 13:44:44 +0800738 resource_list_for_each_entry(win, &port->resources)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530739 devm_kfree(dev, win->res);
740 pci_free_resource_list(&port->resources);
741
742 return err;
743}
744
745/**
746 * xilinx_pcie_parse_dt - Parse Device tree
747 * @port: PCIe port information
748 *
749 * Return: '0' on success and error value on failure
750 */
751static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
752{
753 struct device *dev = port->dev;
754 struct device_node *node = dev->of_node;
755 struct resource regs;
756 const char *type;
757 int err;
758
759 type = of_get_property(node, "device_type", NULL);
760 if (!type || strcmp(type, "pci")) {
761 dev_err(dev, "invalid \"device_type\" %s\n", type);
762 return -EINVAL;
763 }
764
765 err = of_address_to_resource(node, 0, &regs);
766 if (err) {
767 dev_err(dev, "missing \"reg\" property\n");
768 return err;
769 }
770
771 port->reg_base = devm_ioremap_resource(dev, &regs);
772 if (IS_ERR(port->reg_base))
773 return PTR_ERR(port->reg_base);
774
775 port->irq = irq_of_parse_and_map(node, 0);
776 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
777 IRQF_SHARED, "xilinx-pcie", port);
778 if (err) {
779 dev_err(dev, "unable to request irq %d\n", port->irq);
780 return err;
781 }
782
783 return 0;
784}
785
786/**
787 * xilinx_pcie_probe - Probe function
788 * @pdev: Platform device pointer
789 *
790 * Return: '0' on success and error value on failure
791 */
792static int xilinx_pcie_probe(struct platform_device *pdev)
793{
794 struct xilinx_pcie_port *port;
795 struct hw_pci hw;
796 struct device *dev = &pdev->dev;
797 int err;
798
799 if (!dev->of_node)
800 return -ENODEV;
801
802 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
803 if (!port)
804 return -ENOMEM;
805
806 port->dev = dev;
807
808 err = xilinx_pcie_parse_dt(port);
809 if (err) {
810 dev_err(dev, "Parsing DT failed\n");
811 return err;
812 }
813
814 xilinx_pcie_init_port(port);
815
816 err = xilinx_pcie_init_irq_domain(port);
817 if (err) {
818 dev_err(dev, "Failed creating IRQ Domain\n");
819 return err;
820 }
821
822 /*
823 * Parse PCI ranges, configuration bus range and
824 * request their resources
825 */
826 INIT_LIST_HEAD(&port->resources);
827 err = xilinx_pcie_parse_and_add_res(port);
828 if (err) {
829 dev_err(dev, "Failed adding resources\n");
830 return err;
831 }
832
833 platform_set_drvdata(pdev, port);
834
835 /* Register the device */
836 memset(&hw, 0, sizeof(hw));
837 hw = (struct hw_pci) {
838 .nr_controllers = 1,
839 .private_data = (void **)&port,
840 .setup = xilinx_pcie_setup,
841 .map_irq = of_irq_parse_and_map_pci,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530842 .scan = xilinx_pcie_scan_bus,
843 .ops = &xilinx_pcie_ops,
844 };
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700845
846#ifdef CONFIG_PCI_MSI
847 xilinx_pcie_msi_chip.dev = port->dev;
848 hw.msi_ctrl = &xilinx_pcie_msi_chip;
849#endif
Srikanth Thokala8961def2014-08-20 21:56:02 +0530850 pci_common_init_dev(dev, &hw);
851
852 return 0;
853}
854
855/**
856 * xilinx_pcie_remove - Remove function
857 * @pdev: Platform device pointer
858 *
859 * Return: '0' always
860 */
861static int xilinx_pcie_remove(struct platform_device *pdev)
862{
863 struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
864
865 xilinx_pcie_free_irq_domain(port);
866
867 return 0;
868}
869
870static struct of_device_id xilinx_pcie_of_match[] = {
871 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
872 {}
873};
874
875static struct platform_driver xilinx_pcie_driver = {
876 .driver = {
877 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530878 .of_match_table = xilinx_pcie_of_match,
879 .suppress_bind_attrs = true,
880 },
881 .probe = xilinx_pcie_probe,
882 .remove = xilinx_pcie_remove,
883};
884module_platform_driver(xilinx_pcie_driver);
885
886MODULE_AUTHOR("Xilinx Inc");
887MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
888MODULE_LICENSE("GPL v2");