blob: ec2844aeecce2315d5a0103ed7af50370c744e89 [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;
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530224 struct irq_data *d = irq_get_irq_data(irq);
225 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530226
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530227 if (!test_bit(hwirq, msi_irq_in_use)) {
Jiang Liue39758e2015-07-09 16:00:43 +0800228 msi = irq_get_msi_desc(irq);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530229 port = msi_desc_to_pci_sysdata(msi);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530230 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800231 } else {
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530232 clear_bit(hwirq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800233 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530234}
235
236/**
237 * xilinx_pcie_assign_msi - Allocate MSI number
238 * @port: PCIe port structure
239 *
240 * Return: A valid IRQ on success and error value on failure.
241 */
242static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
243{
244 int pos;
245
246 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
247 if (pos < XILINX_NUM_MSI_IRQS)
248 set_bit(pos, msi_irq_in_use);
249 else
250 return -ENOSPC;
251
252 return pos;
253}
254
255/**
256 * xilinx_msi_teardown_irq - Destroy the MSI
257 * @chip: MSI Chip descriptor
258 * @irq: MSI IRQ to destroy
259 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700260static void xilinx_msi_teardown_irq(struct msi_controller *chip,
261 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530262{
263 xilinx_pcie_destroy_msi(irq);
264}
265
266/**
267 * xilinx_pcie_msi_setup_irq - Setup MSI request
268 * @chip: MSI chip pointer
269 * @pdev: PCIe device pointer
270 * @desc: MSI descriptor pointer
271 *
272 * Return: '0' on success and error value on failure
273 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700274static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530275 struct pci_dev *pdev,
276 struct msi_desc *desc)
277{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530278 struct xilinx_pcie_port *port = pdev->bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530279 unsigned int irq;
280 int hwirq;
281 struct msi_msg msg;
282 phys_addr_t msg_addr;
283
284 hwirq = xilinx_pcie_assign_msi(port);
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300285 if (hwirq < 0)
286 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530287
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530288 irq = irq_create_mapping(port->msi_domain, hwirq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530289 if (!irq)
290 return -EINVAL;
291
292 irq_set_msi_desc(irq, desc);
293
294 msg_addr = virt_to_phys((void *)port->msi_pages);
295
296 msg.address_hi = 0;
297 msg.address_lo = msg_addr;
298 msg.data = irq;
299
Jiang Liu83a18912014-11-09 23:10:34 +0800300 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530301
302 return 0;
303}
304
305/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700306static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530307 .setup_irq = xilinx_pcie_msi_setup_irq,
308 .teardown_irq = xilinx_msi_teardown_irq,
309};
310
311/* HW Interrupt Chip Descriptor */
312static struct irq_chip xilinx_msi_irq_chip = {
313 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100314 .irq_enable = pci_msi_unmask_irq,
315 .irq_disable = pci_msi_mask_irq,
316 .irq_mask = pci_msi_mask_irq,
317 .irq_unmask = pci_msi_unmask_irq,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530318};
319
320/**
321 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
322 * @domain: IRQ domain
323 * @irq: Virtual IRQ number
324 * @hwirq: HW interrupt number
325 *
326 * Return: Always returns 0.
327 */
328static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
329 irq_hw_number_t hwirq)
330{
331 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
332 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530333
334 return 0;
335}
336
337/* IRQ Domain operations */
338static const struct irq_domain_ops msi_domain_ops = {
339 .map = xilinx_pcie_msi_map,
340};
341
342/**
343 * xilinx_pcie_enable_msi - Enable MSI support
344 * @port: PCIe port information
345 */
346static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
347{
348 phys_addr_t msg_addr;
349
350 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
351 msg_addr = virt_to_phys((void *)port->msi_pages);
352 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
353 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
354}
355
Srikanth Thokala8961def2014-08-20 21:56:02 +0530356/* INTx Functions */
357
358/**
359 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
360 * @domain: IRQ domain
361 * @irq: Virtual IRQ number
362 * @hwirq: HW interrupt number
363 *
364 * Return: Always returns 0.
365 */
366static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
367 irq_hw_number_t hwirq)
368{
369 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
370 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530371
372 return 0;
373}
374
375/* INTx IRQ Domain operations */
376static const struct irq_domain_ops intx_domain_ops = {
377 .map = xilinx_pcie_intx_map,
378};
379
380/* PCIe HW Functions */
381
382/**
383 * xilinx_pcie_intr_handler - Interrupt Service Handler
384 * @irq: IRQ number
385 * @data: PCIe port information
386 *
387 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
388 */
389static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
390{
391 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
392 u32 val, mask, status, msi_data;
393
394 /* Read interrupt decode and mask registers */
395 val = pcie_read(port, XILINX_PCIE_REG_IDR);
396 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
397
398 status = val & mask;
399 if (!status)
400 return IRQ_NONE;
401
402 if (status & XILINX_PCIE_INTR_LINK_DOWN)
403 dev_warn(port->dev, "Link Down\n");
404
405 if (status & XILINX_PCIE_INTR_ECRC_ERR)
406 dev_warn(port->dev, "ECRC failed\n");
407
408 if (status & XILINX_PCIE_INTR_STR_ERR)
409 dev_warn(port->dev, "Streaming error\n");
410
411 if (status & XILINX_PCIE_INTR_HOT_RESET)
412 dev_info(port->dev, "Hot reset\n");
413
414 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
415 dev_warn(port->dev, "ECAM access timeout\n");
416
417 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
418 dev_warn(port->dev, "Correctable error message\n");
419 xilinx_pcie_clear_err_interrupts(port);
420 }
421
422 if (status & XILINX_PCIE_INTR_NONFATAL) {
423 dev_warn(port->dev, "Non fatal error message\n");
424 xilinx_pcie_clear_err_interrupts(port);
425 }
426
427 if (status & XILINX_PCIE_INTR_FATAL) {
428 dev_warn(port->dev, "Fatal error message\n");
429 xilinx_pcie_clear_err_interrupts(port);
430 }
431
432 if (status & XILINX_PCIE_INTR_INTX) {
433 /* INTx interrupt received */
434 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
435
436 /* Check whether interrupt valid */
437 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
438 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530439 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530440 }
441
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100442 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
443 /* Clear interrupt FIFO register 1 */
444 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
445 XILINX_PCIE_REG_RPIFR1);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530446
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100447 /* Handle INTx Interrupt */
448 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
449 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530450 generic_handle_irq(irq_find_mapping(port->leg_domain,
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100451 val));
452 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530453 }
454
455 if (status & XILINX_PCIE_INTR_MSI) {
456 /* MSI Interrupt */
457 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
458
459 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
460 dev_warn(port->dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530461 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530462 }
463
464 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
465 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
466 XILINX_PCIE_RPIFR2_MSG_DATA;
467
468 /* Clear interrupt FIFO register 1 */
469 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
470 XILINX_PCIE_REG_RPIFR1);
471
472 if (IS_ENABLED(CONFIG_PCI_MSI)) {
473 /* Handle MSI Interrupt */
474 generic_handle_irq(msi_data);
475 }
476 }
477 }
478
479 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
480 dev_warn(port->dev, "Slave unsupported request\n");
481
482 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
483 dev_warn(port->dev, "Slave unexpected completion\n");
484
485 if (status & XILINX_PCIE_INTR_SLV_COMPL)
486 dev_warn(port->dev, "Slave completion timeout\n");
487
488 if (status & XILINX_PCIE_INTR_SLV_ERRP)
489 dev_warn(port->dev, "Slave Error Poison\n");
490
491 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
492 dev_warn(port->dev, "Slave Completer Abort\n");
493
494 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
495 dev_warn(port->dev, "Slave Illegal Burst\n");
496
497 if (status & XILINX_PCIE_INTR_MST_DECERR)
498 dev_warn(port->dev, "Master decode error\n");
499
500 if (status & XILINX_PCIE_INTR_MST_SLVERR)
501 dev_warn(port->dev, "Master slave error\n");
502
503 if (status & XILINX_PCIE_INTR_MST_ERRP)
504 dev_warn(port->dev, "Master error poison\n");
505
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530506error:
Srikanth Thokala8961def2014-08-20 21:56:02 +0530507 /* Clear the Interrupt Decode register */
508 pcie_write(port, status, XILINX_PCIE_REG_IDR);
509
510 return IRQ_HANDLED;
511}
512
513/**
514 * xilinx_pcie_free_irq_domain - Free IRQ domain
515 * @port: PCIe port information
516 */
517static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
518{
519 int i;
520 u32 irq, num_irqs;
521
522 /* Free IRQ Domain */
523 if (IS_ENABLED(CONFIG_PCI_MSI)) {
524
525 free_pages(port->msi_pages, 0);
526
527 num_irqs = XILINX_NUM_MSI_IRQS;
528 } else {
529 /* INTx */
530 num_irqs = 4;
531 }
532
533 for (i = 0; i < num_irqs; i++) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530534 irq = irq_find_mapping(port->leg_domain, i);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530535 if (irq > 0)
536 irq_dispose_mapping(irq);
537 }
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530538 if (port->leg_domain)
539 irq_domain_remove(port->leg_domain);
540 if (port->msi_domain)
541 irq_domain_remove(port->msi_domain);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530542}
543
544/**
545 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
546 * @port: PCIe port information
547 *
548 * Return: '0' on success and error value on failure
549 */
550static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
551{
552 struct device *dev = port->dev;
553 struct device_node *node = dev->of_node;
554 struct device_node *pcie_intc_node;
555
556 /* Setup INTx */
557 pcie_intc_node = of_get_next_child(node, NULL);
558 if (!pcie_intc_node) {
559 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200560 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530561 }
562
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530563 port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530564 &intx_domain_ops,
565 port);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530566 if (!port->leg_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530567 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200568 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530569 }
570
571 /* Setup MSI */
572 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530573 port->msi_domain = irq_domain_add_linear(node,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530574 XILINX_NUM_MSI_IRQS,
575 &msi_domain_ops,
576 &xilinx_pcie_msi_chip);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530577 if (!port->msi_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530578 dev_err(dev, "Failed to get a MSI IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200579 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530580 }
581
582 xilinx_pcie_enable_msi(port);
583 }
584
585 return 0;
586}
587
588/**
589 * xilinx_pcie_init_port - Initialize hardware
590 * @port: PCIe port information
591 */
592static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
593{
594 if (xilinx_pcie_link_is_up(port))
595 dev_info(port->dev, "PCIe Link is UP\n");
596 else
597 dev_info(port->dev, "PCIe Link is DOWN\n");
598
599 /* Disable all interrupts */
600 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
601 XILINX_PCIE_REG_IMR);
602
603 /* Clear pending interrupts */
604 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
605 XILINX_PCIE_IMR_ALL_MASK,
606 XILINX_PCIE_REG_IDR);
607
608 /* Enable all interrupts */
609 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
610
611 /* Enable the Bridge enable bit */
612 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
613 XILINX_PCIE_REG_RPSC_BEN,
614 XILINX_PCIE_REG_RPSC);
615}
616
617/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530618 * xilinx_pcie_parse_dt - Parse Device tree
619 * @port: PCIe port information
620 *
621 * Return: '0' on success and error value on failure
622 */
623static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
624{
625 struct device *dev = port->dev;
626 struct device_node *node = dev->of_node;
627 struct resource regs;
628 const char *type;
629 int err;
630
631 type = of_get_property(node, "device_type", NULL);
632 if (!type || strcmp(type, "pci")) {
633 dev_err(dev, "invalid \"device_type\" %s\n", type);
634 return -EINVAL;
635 }
636
637 err = of_address_to_resource(node, 0, &regs);
638 if (err) {
639 dev_err(dev, "missing \"reg\" property\n");
640 return err;
641 }
642
643 port->reg_base = devm_ioremap_resource(dev, &regs);
644 if (IS_ERR(port->reg_base))
645 return PTR_ERR(port->reg_base);
646
647 port->irq = irq_of_parse_and_map(node, 0);
648 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200649 IRQF_SHARED | IRQF_NO_THREAD,
650 "xilinx-pcie", port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530651 if (err) {
652 dev_err(dev, "unable to request irq %d\n", port->irq);
653 return err;
654 }
655
656 return 0;
657}
658
659/**
660 * xilinx_pcie_probe - Probe function
661 * @pdev: Platform device pointer
662 *
663 * Return: '0' on success and error value on failure
664 */
665static int xilinx_pcie_probe(struct platform_device *pdev)
666{
667 struct xilinx_pcie_port *port;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530668 struct device *dev = &pdev->dev;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530669 struct pci_bus *bus;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530670 int err;
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530671 resource_size_t iobase = 0;
672 LIST_HEAD(res);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530673
674 if (!dev->of_node)
675 return -ENODEV;
676
677 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
678 if (!port)
679 return -ENOMEM;
680
681 port->dev = dev;
682
683 err = xilinx_pcie_parse_dt(port);
684 if (err) {
685 dev_err(dev, "Parsing DT failed\n");
686 return err;
687 }
688
689 xilinx_pcie_init_port(port);
690
691 err = xilinx_pcie_init_irq_domain(port);
692 if (err) {
693 dev_err(dev, "Failed creating IRQ Domain\n");
694 return err;
695 }
696
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530697 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
698 &iobase);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530699 if (err) {
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530700 dev_err(dev, "Getting bridge resources failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530701 return err;
702 }
Bjorn Helgaas93a5b5e2016-05-28 18:27:03 -0500703
704 err = devm_request_pci_bus_resources(dev, &res);
705 if (err)
706 goto error;
707
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530708 bus = pci_create_root_bus(&pdev->dev, 0,
709 &xilinx_pcie_ops, port, &res);
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500710 if (!bus) {
711 err = -ENOMEM;
712 goto error;
713 }
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700714
715#ifdef CONFIG_PCI_MSI
716 xilinx_pcie_msi_chip.dev = port->dev;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530717 bus->msi = &xilinx_pcie_msi_chip;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700718#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530719 pci_scan_child_bus(bus);
720 pci_assign_unassigned_bus_resources(bus);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530721#ifndef CONFIG_MICROBLAZE
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530722 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530723#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530724 pci_bus_add_devices(bus);
725 platform_set_drvdata(pdev, port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530726
727 return 0;
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500728
729error:
730 pci_free_resource_list(&res);
731 return err;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530732}
733
734/**
735 * xilinx_pcie_remove - Remove function
736 * @pdev: Platform device pointer
737 *
738 * Return: '0' always
739 */
740static int xilinx_pcie_remove(struct platform_device *pdev)
741{
742 struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
743
744 xilinx_pcie_free_irq_domain(port);
745
746 return 0;
747}
748
749static struct of_device_id xilinx_pcie_of_match[] = {
750 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
751 {}
752};
753
754static struct platform_driver xilinx_pcie_driver = {
755 .driver = {
756 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530757 .of_match_table = xilinx_pcie_of_match,
758 .suppress_bind_attrs = true,
759 },
760 .probe = xilinx_pcie_probe,
761 .remove = xilinx_pcie_remove,
762};
763module_platform_driver(xilinx_pcie_driver);
764
765MODULE_AUTHOR("Xilinx Inc");
766MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
767MODULE_LICENSE("GPL v2");