blob: de1c758d17d778ad2c98a67bcc968472fca6cc87 [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
Srikanth Thokala8961def2014-08-20 21:56:02 +053097/**
98 * struct xilinx_pcie_port - PCIe port information
99 * @reg_base: IO Mapped Register Base
100 * @irq: Interrupt number
101 * @msi_pages: MSI pages
102 * @root_busno: Root Bus number
103 * @dev: Device pointer
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530104 * @msi_domain: MSI IRQ domain pointer
105 * @leg_domain: Legacy IRQ domain pointer
Srikanth Thokala8961def2014-08-20 21:56:02 +0530106 * @resources: Bus Resources
107 */
108struct xilinx_pcie_port {
109 void __iomem *reg_base;
110 u32 irq;
111 unsigned long msi_pages;
112 u8 root_busno;
113 struct device *dev;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530114 struct irq_domain *msi_domain;
115 struct irq_domain *leg_domain;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530116 struct list_head resources;
117};
118
119static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
120
Srikanth Thokala8961def2014-08-20 21:56:02 +0530121static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
122{
123 return readl(port->reg_base + reg);
124}
125
126static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
127{
128 writel(val, port->reg_base + reg);
129}
130
131static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
132{
133 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
134 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
135}
136
137/**
138 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
139 * @port: PCIe port information
140 */
141static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
142{
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100143 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530144
145 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100146 dev_dbg(port->dev, "Requester ID %lu\n",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530147 val & XILINX_PCIE_RPEFR_REQ_ID);
148 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
149 XILINX_PCIE_REG_RPEFR);
150 }
151}
152
153/**
154 * xilinx_pcie_valid_device - Check if a valid device is present on bus
155 * @bus: PCI Bus structure
156 * @devfn: device/function
157 *
158 * Return: 'true' on success and 'false' if invalid device is found
159 */
160static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
161{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530162 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530163
164 /* Check if link is up when trying to access downstream ports */
165 if (bus->number != port->root_busno)
166 if (!xilinx_pcie_link_is_up(port))
167 return false;
168
169 /* Only one device down on each root port */
170 if (bus->number == port->root_busno && devfn > 0)
171 return false;
172
173 /*
174 * Do not read more than one device on the bus directly attached
175 * to RC.
176 */
177 if (bus->primary == port->root_busno && devfn > 0)
178 return false;
179
180 return true;
181}
182
183/**
Rob Herring029e2152015-01-09 20:34:50 -0600184 * xilinx_pcie_map_bus - Get configuration base
Srikanth Thokala8961def2014-08-20 21:56:02 +0530185 * @bus: PCI Bus structure
186 * @devfn: Device/function
187 * @where: Offset from base
188 *
189 * Return: Base address of the configuration space needed to be
190 * accessed.
191 */
Rob Herring029e2152015-01-09 20:34:50 -0600192static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
193 unsigned int devfn, int where)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530194{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530195 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530196 int relbus;
197
Rob Herring029e2152015-01-09 20:34:50 -0600198 if (!xilinx_pcie_valid_device(bus, devfn))
199 return NULL;
200
Srikanth Thokala8961def2014-08-20 21:56:02 +0530201 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
202 (devfn << ECAM_DEV_NUM_SHIFT);
203
204 return port->reg_base + relbus + where;
205}
206
Srikanth Thokala8961def2014-08-20 21:56:02 +0530207/* PCIe operations */
208static struct pci_ops xilinx_pcie_ops = {
Rob Herring029e2152015-01-09 20:34:50 -0600209 .map_bus = xilinx_pcie_map_bus,
210 .read = pci_generic_config_read,
211 .write = pci_generic_config_write,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530212};
213
214/* MSI functions */
215
216/**
217 * xilinx_pcie_destroy_msi - Free MSI number
218 * @irq: IRQ to be freed
219 */
220static void xilinx_pcie_destroy_msi(unsigned int irq)
221{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530222 struct msi_desc *msi;
223 struct xilinx_pcie_port *port;
224
Jiang Liue39758e2015-07-09 16:00:43 +0800225 if (!test_bit(irq, msi_irq_in_use)) {
226 msi = irq_get_msi_desc(irq);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530227 port = msi_desc_to_pci_sysdata(msi);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530228 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800229 } else {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530230 clear_bit(irq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800231 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530232}
233
234/**
235 * xilinx_pcie_assign_msi - Allocate MSI number
236 * @port: PCIe port structure
237 *
238 * Return: A valid IRQ on success and error value on failure.
239 */
240static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
241{
242 int pos;
243
244 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
245 if (pos < XILINX_NUM_MSI_IRQS)
246 set_bit(pos, msi_irq_in_use);
247 else
248 return -ENOSPC;
249
250 return pos;
251}
252
253/**
254 * xilinx_msi_teardown_irq - Destroy the MSI
255 * @chip: MSI Chip descriptor
256 * @irq: MSI IRQ to destroy
257 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700258static void xilinx_msi_teardown_irq(struct msi_controller *chip,
259 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530260{
261 xilinx_pcie_destroy_msi(irq);
262}
263
264/**
265 * xilinx_pcie_msi_setup_irq - Setup MSI request
266 * @chip: MSI chip pointer
267 * @pdev: PCIe device pointer
268 * @desc: MSI descriptor pointer
269 *
270 * Return: '0' on success and error value on failure
271 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700272static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530273 struct pci_dev *pdev,
274 struct msi_desc *desc)
275{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530276 struct xilinx_pcie_port *port = pdev->bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530277 unsigned int irq;
278 int hwirq;
279 struct msi_msg msg;
280 phys_addr_t msg_addr;
281
282 hwirq = xilinx_pcie_assign_msi(port);
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300283 if (hwirq < 0)
284 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530285
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530286 irq = irq_create_mapping(port->msi_domain, hwirq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530287 if (!irq)
288 return -EINVAL;
289
290 irq_set_msi_desc(irq, desc);
291
292 msg_addr = virt_to_phys((void *)port->msi_pages);
293
294 msg.address_hi = 0;
295 msg.address_lo = msg_addr;
296 msg.data = irq;
297
Jiang Liu83a18912014-11-09 23:10:34 +0800298 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530299
300 return 0;
301}
302
303/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700304static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530305 .setup_irq = xilinx_pcie_msi_setup_irq,
306 .teardown_irq = xilinx_msi_teardown_irq,
307};
308
309/* HW Interrupt Chip Descriptor */
310static struct irq_chip xilinx_msi_irq_chip = {
311 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100312 .irq_enable = pci_msi_unmask_irq,
313 .irq_disable = pci_msi_mask_irq,
314 .irq_mask = pci_msi_mask_irq,
315 .irq_unmask = pci_msi_unmask_irq,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530316};
317
318/**
319 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
320 * @domain: IRQ domain
321 * @irq: Virtual IRQ number
322 * @hwirq: HW interrupt number
323 *
324 * Return: Always returns 0.
325 */
326static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
327 irq_hw_number_t hwirq)
328{
329 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
330 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530331
332 return 0;
333}
334
335/* IRQ Domain operations */
336static const struct irq_domain_ops msi_domain_ops = {
337 .map = xilinx_pcie_msi_map,
338};
339
340/**
341 * xilinx_pcie_enable_msi - Enable MSI support
342 * @port: PCIe port information
343 */
344static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
345{
346 phys_addr_t msg_addr;
347
348 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
349 msg_addr = virt_to_phys((void *)port->msi_pages);
350 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
351 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
352}
353
Srikanth Thokala8961def2014-08-20 21:56:02 +0530354/* INTx Functions */
355
356/**
357 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
358 * @domain: IRQ domain
359 * @irq: Virtual IRQ number
360 * @hwirq: HW interrupt number
361 *
362 * Return: Always returns 0.
363 */
364static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
365 irq_hw_number_t hwirq)
366{
367 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
368 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530369
370 return 0;
371}
372
373/* INTx IRQ Domain operations */
374static const struct irq_domain_ops intx_domain_ops = {
375 .map = xilinx_pcie_intx_map,
376};
377
378/* PCIe HW Functions */
379
380/**
381 * xilinx_pcie_intr_handler - Interrupt Service Handler
382 * @irq: IRQ number
383 * @data: PCIe port information
384 *
385 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
386 */
387static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
388{
389 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
390 u32 val, mask, status, msi_data;
391
392 /* Read interrupt decode and mask registers */
393 val = pcie_read(port, XILINX_PCIE_REG_IDR);
394 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
395
396 status = val & mask;
397 if (!status)
398 return IRQ_NONE;
399
400 if (status & XILINX_PCIE_INTR_LINK_DOWN)
401 dev_warn(port->dev, "Link Down\n");
402
403 if (status & XILINX_PCIE_INTR_ECRC_ERR)
404 dev_warn(port->dev, "ECRC failed\n");
405
406 if (status & XILINX_PCIE_INTR_STR_ERR)
407 dev_warn(port->dev, "Streaming error\n");
408
409 if (status & XILINX_PCIE_INTR_HOT_RESET)
410 dev_info(port->dev, "Hot reset\n");
411
412 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
413 dev_warn(port->dev, "ECAM access timeout\n");
414
415 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
416 dev_warn(port->dev, "Correctable error message\n");
417 xilinx_pcie_clear_err_interrupts(port);
418 }
419
420 if (status & XILINX_PCIE_INTR_NONFATAL) {
421 dev_warn(port->dev, "Non fatal error message\n");
422 xilinx_pcie_clear_err_interrupts(port);
423 }
424
425 if (status & XILINX_PCIE_INTR_FATAL) {
426 dev_warn(port->dev, "Fatal error message\n");
427 xilinx_pcie_clear_err_interrupts(port);
428 }
429
430 if (status & XILINX_PCIE_INTR_INTX) {
431 /* INTx interrupt received */
432 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
433
434 /* Check whether interrupt valid */
435 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
436 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530437 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530438 }
439
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100440 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
441 /* Clear interrupt FIFO register 1 */
442 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
443 XILINX_PCIE_REG_RPIFR1);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530444
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100445 /* Handle INTx Interrupt */
446 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
447 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530448 generic_handle_irq(irq_find_mapping(port->leg_domain,
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100449 val));
450 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530451 }
452
453 if (status & XILINX_PCIE_INTR_MSI) {
454 /* MSI Interrupt */
455 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
456
457 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
458 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530459 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530460 }
461
462 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
463 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
464 XILINX_PCIE_RPIFR2_MSG_DATA;
465
466 /* Clear interrupt FIFO register 1 */
467 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
468 XILINX_PCIE_REG_RPIFR1);
469
470 if (IS_ENABLED(CONFIG_PCI_MSI)) {
471 /* Handle MSI Interrupt */
472 generic_handle_irq(msi_data);
473 }
474 }
475 }
476
477 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
478 dev_warn(port->dev, "Slave unsupported request\n");
479
480 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
481 dev_warn(port->dev, "Slave unexpected completion\n");
482
483 if (status & XILINX_PCIE_INTR_SLV_COMPL)
484 dev_warn(port->dev, "Slave completion timeout\n");
485
486 if (status & XILINX_PCIE_INTR_SLV_ERRP)
487 dev_warn(port->dev, "Slave Error Poison\n");
488
489 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
490 dev_warn(port->dev, "Slave Completer Abort\n");
491
492 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
493 dev_warn(port->dev, "Slave Illegal Burst\n");
494
495 if (status & XILINX_PCIE_INTR_MST_DECERR)
496 dev_warn(port->dev, "Master decode error\n");
497
498 if (status & XILINX_PCIE_INTR_MST_SLVERR)
499 dev_warn(port->dev, "Master slave error\n");
500
501 if (status & XILINX_PCIE_INTR_MST_ERRP)
502 dev_warn(port->dev, "Master error poison\n");
503
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530504error:
Srikanth Thokala8961def2014-08-20 21:56:02 +0530505 /* Clear the Interrupt Decode register */
506 pcie_write(port, status, XILINX_PCIE_REG_IDR);
507
508 return IRQ_HANDLED;
509}
510
511/**
512 * xilinx_pcie_free_irq_domain - Free IRQ domain
513 * @port: PCIe port information
514 */
515static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
516{
517 int i;
518 u32 irq, num_irqs;
519
520 /* Free IRQ Domain */
521 if (IS_ENABLED(CONFIG_PCI_MSI)) {
522
523 free_pages(port->msi_pages, 0);
524
525 num_irqs = XILINX_NUM_MSI_IRQS;
526 } else {
527 /* INTx */
528 num_irqs = 4;
529 }
530
531 for (i = 0; i < num_irqs; i++) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530532 irq = irq_find_mapping(port->leg_domain, i);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530533 if (irq > 0)
534 irq_dispose_mapping(irq);
535 }
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530536 if (port->leg_domain)
537 irq_domain_remove(port->leg_domain);
538 if (port->msi_domain)
539 irq_domain_remove(port->msi_domain);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530540}
541
542/**
543 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
544 * @port: PCIe port information
545 *
546 * Return: '0' on success and error value on failure
547 */
548static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
549{
550 struct device *dev = port->dev;
551 struct device_node *node = dev->of_node;
552 struct device_node *pcie_intc_node;
553
554 /* Setup INTx */
555 pcie_intc_node = of_get_next_child(node, NULL);
556 if (!pcie_intc_node) {
557 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200558 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530559 }
560
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530561 port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530562 &intx_domain_ops,
563 port);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530564 if (!port->leg_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530565 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200566 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530567 }
568
569 /* Setup MSI */
570 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530571 port->msi_domain = irq_domain_add_linear(node,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530572 XILINX_NUM_MSI_IRQS,
573 &msi_domain_ops,
574 &xilinx_pcie_msi_chip);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530575 if (!port->msi_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530576 dev_err(dev, "Failed to get a MSI IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200577 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530578 }
579
580 xilinx_pcie_enable_msi(port);
581 }
582
583 return 0;
584}
585
586/**
587 * xilinx_pcie_init_port - Initialize hardware
588 * @port: PCIe port information
589 */
590static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
591{
592 if (xilinx_pcie_link_is_up(port))
593 dev_info(port->dev, "PCIe Link is UP\n");
594 else
595 dev_info(port->dev, "PCIe Link is DOWN\n");
596
597 /* Disable all interrupts */
598 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
599 XILINX_PCIE_REG_IMR);
600
601 /* Clear pending interrupts */
602 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
603 XILINX_PCIE_IMR_ALL_MASK,
604 XILINX_PCIE_REG_IDR);
605
606 /* Enable all interrupts */
607 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
608
609 /* Enable the Bridge enable bit */
610 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
611 XILINX_PCIE_REG_RPSC_BEN,
612 XILINX_PCIE_REG_RPSC);
613}
614
615/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530616 * xilinx_pcie_parse_dt - Parse Device tree
617 * @port: PCIe port information
618 *
619 * Return: '0' on success and error value on failure
620 */
621static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
622{
623 struct device *dev = port->dev;
624 struct device_node *node = dev->of_node;
625 struct resource regs;
626 const char *type;
627 int err;
628
629 type = of_get_property(node, "device_type", NULL);
630 if (!type || strcmp(type, "pci")) {
631 dev_err(dev, "invalid \"device_type\" %s\n", type);
632 return -EINVAL;
633 }
634
635 err = of_address_to_resource(node, 0, &regs);
636 if (err) {
637 dev_err(dev, "missing \"reg\" property\n");
638 return err;
639 }
640
641 port->reg_base = devm_ioremap_resource(dev, &regs);
642 if (IS_ERR(port->reg_base))
643 return PTR_ERR(port->reg_base);
644
645 port->irq = irq_of_parse_and_map(node, 0);
646 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200647 IRQF_SHARED | IRQF_NO_THREAD,
648 "xilinx-pcie", port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530649 if (err) {
650 dev_err(dev, "unable to request irq %d\n", port->irq);
651 return err;
652 }
653
654 return 0;
655}
656
657/**
658 * xilinx_pcie_probe - Probe function
659 * @pdev: Platform device pointer
660 *
661 * Return: '0' on success and error value on failure
662 */
663static int xilinx_pcie_probe(struct platform_device *pdev)
664{
665 struct xilinx_pcie_port *port;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530666 struct device *dev = &pdev->dev;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530667 struct pci_bus *bus;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530668 int err;
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530669 resource_size_t iobase = 0;
670 LIST_HEAD(res);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530671
672 if (!dev->of_node)
673 return -ENODEV;
674
675 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
676 if (!port)
677 return -ENOMEM;
678
679 port->dev = dev;
680
681 err = xilinx_pcie_parse_dt(port);
682 if (err) {
683 dev_err(dev, "Parsing DT failed\n");
684 return err;
685 }
686
687 xilinx_pcie_init_port(port);
688
689 err = xilinx_pcie_init_irq_domain(port);
690 if (err) {
691 dev_err(dev, "Failed creating IRQ Domain\n");
692 return err;
693 }
694
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530695 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
696 &iobase);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530697 if (err) {
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530698 dev_err(dev, "Getting bridge resources failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530699 return err;
700 }
Bjorn Helgaas93a5b5e2016-05-28 18:27:03 -0500701
702 err = devm_request_pci_bus_resources(dev, &res);
703 if (err)
704 goto error;
705
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530706 bus = pci_create_root_bus(&pdev->dev, 0,
707 &xilinx_pcie_ops, port, &res);
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500708 if (!bus) {
709 err = -ENOMEM;
710 goto error;
711 }
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700712
713#ifdef CONFIG_PCI_MSI
714 xilinx_pcie_msi_chip.dev = port->dev;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530715 bus->msi = &xilinx_pcie_msi_chip;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700716#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530717 pci_scan_child_bus(bus);
718 pci_assign_unassigned_bus_resources(bus);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530719#ifndef CONFIG_MICROBLAZE
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530720 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530721#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530722 pci_bus_add_devices(bus);
723 platform_set_drvdata(pdev, port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530724
725 return 0;
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500726
727error:
728 pci_free_resource_list(&res);
729 return err;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530730}
731
732/**
733 * xilinx_pcie_remove - Remove function
734 * @pdev: Platform device pointer
735 *
736 * Return: '0' always
737 */
738static int xilinx_pcie_remove(struct platform_device *pdev)
739{
740 struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
741
742 xilinx_pcie_free_irq_domain(port);
743
744 return 0;
745}
746
747static struct of_device_id xilinx_pcie_of_match[] = {
748 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
749 {}
750};
751
752static struct platform_driver xilinx_pcie_driver = {
753 .driver = {
754 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530755 .of_match_table = xilinx_pcie_of_match,
756 .suppress_bind_attrs = true,
757 },
758 .probe = xilinx_pcie_probe,
759 .remove = xilinx_pcie_remove,
760};
761module_platform_driver(xilinx_pcie_driver);
762
763MODULE_AUTHOR("Xilinx Inc");
764MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
765MODULE_LICENSE("GPL v2");