blob: 2ef47afbc3d8f703e37d9d17d4fbe0d1c066b043 [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>
Paul Gortmakerda4eafc2016-08-24 16:57:49 -040021#include <linux/init.h>
Srikanth Thokala8961def2014-08-20 21:56:02 +053022#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{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500143 struct device *dev = port->dev;
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100144 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530145
146 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500147 dev_dbg(dev, "Requester ID %lu\n",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530148 val & XILINX_PCIE_RPEFR_REQ_ID);
149 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
150 XILINX_PCIE_REG_RPEFR);
151 }
152}
153
154/**
155 * xilinx_pcie_valid_device - Check if a valid device is present on bus
156 * @bus: PCI Bus structure
157 * @devfn: device/function
158 *
159 * Return: 'true' on success and 'false' if invalid device is found
160 */
161static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
162{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530163 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530164
165 /* Check if link is up when trying to access downstream ports */
166 if (bus->number != port->root_busno)
167 if (!xilinx_pcie_link_is_up(port))
168 return false;
169
170 /* Only one device down on each root port */
171 if (bus->number == port->root_busno && devfn > 0)
172 return false;
173
Srikanth Thokala8961def2014-08-20 21:56:02 +0530174 return true;
175}
176
177/**
Rob Herring029e2152015-01-09 20:34:50 -0600178 * xilinx_pcie_map_bus - Get configuration base
Srikanth Thokala8961def2014-08-20 21:56:02 +0530179 * @bus: PCI Bus structure
180 * @devfn: Device/function
181 * @where: Offset from base
182 *
183 * Return: Base address of the configuration space needed to be
184 * accessed.
185 */
Rob Herring029e2152015-01-09 20:34:50 -0600186static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
187 unsigned int devfn, int where)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530188{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530189 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530190 int relbus;
191
Rob Herring029e2152015-01-09 20:34:50 -0600192 if (!xilinx_pcie_valid_device(bus, devfn))
193 return NULL;
194
Srikanth Thokala8961def2014-08-20 21:56:02 +0530195 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
196 (devfn << ECAM_DEV_NUM_SHIFT);
197
198 return port->reg_base + relbus + where;
199}
200
Srikanth Thokala8961def2014-08-20 21:56:02 +0530201/* PCIe operations */
202static struct pci_ops xilinx_pcie_ops = {
Rob Herring029e2152015-01-09 20:34:50 -0600203 .map_bus = xilinx_pcie_map_bus,
204 .read = pci_generic_config_read,
205 .write = pci_generic_config_write,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530206};
207
208/* MSI functions */
209
210/**
211 * xilinx_pcie_destroy_msi - Free MSI number
212 * @irq: IRQ to be freed
213 */
214static void xilinx_pcie_destroy_msi(unsigned int irq)
215{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530216 struct msi_desc *msi;
217 struct xilinx_pcie_port *port;
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530218 struct irq_data *d = irq_get_irq_data(irq);
219 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530220
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530221 if (!test_bit(hwirq, msi_irq_in_use)) {
Jiang Liue39758e2015-07-09 16:00:43 +0800222 msi = irq_get_msi_desc(irq);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530223 port = msi_desc_to_pci_sysdata(msi);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530224 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800225 } else {
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530226 clear_bit(hwirq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800227 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530228}
229
230/**
231 * xilinx_pcie_assign_msi - Allocate MSI number
232 * @port: PCIe port structure
233 *
234 * Return: A valid IRQ on success and error value on failure.
235 */
236static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
237{
238 int pos;
239
240 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
241 if (pos < XILINX_NUM_MSI_IRQS)
242 set_bit(pos, msi_irq_in_use);
243 else
244 return -ENOSPC;
245
246 return pos;
247}
248
249/**
250 * xilinx_msi_teardown_irq - Destroy the MSI
251 * @chip: MSI Chip descriptor
252 * @irq: MSI IRQ to destroy
253 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700254static void xilinx_msi_teardown_irq(struct msi_controller *chip,
255 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530256{
257 xilinx_pcie_destroy_msi(irq);
Bharat Kumar Gogadab328f3c2016-09-01 15:44:44 +0530258 irq_dispose_mapping(irq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530259}
260
261/**
262 * xilinx_pcie_msi_setup_irq - Setup MSI request
263 * @chip: MSI chip pointer
264 * @pdev: PCIe device pointer
265 * @desc: MSI descriptor pointer
266 *
267 * Return: '0' on success and error value on failure
268 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700269static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530270 struct pci_dev *pdev,
271 struct msi_desc *desc)
272{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530273 struct xilinx_pcie_port *port = pdev->bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530274 unsigned int irq;
275 int hwirq;
276 struct msi_msg msg;
277 phys_addr_t msg_addr;
278
279 hwirq = xilinx_pcie_assign_msi(port);
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300280 if (hwirq < 0)
281 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530282
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530283 irq = irq_create_mapping(port->msi_domain, hwirq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530284 if (!irq)
285 return -EINVAL;
286
287 irq_set_msi_desc(irq, desc);
288
289 msg_addr = virt_to_phys((void *)port->msi_pages);
290
291 msg.address_hi = 0;
292 msg.address_lo = msg_addr;
293 msg.data = irq;
294
Jiang Liu83a18912014-11-09 23:10:34 +0800295 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530296
297 return 0;
298}
299
300/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700301static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530302 .setup_irq = xilinx_pcie_msi_setup_irq,
303 .teardown_irq = xilinx_msi_teardown_irq,
304};
305
306/* HW Interrupt Chip Descriptor */
307static struct irq_chip xilinx_msi_irq_chip = {
308 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100309 .irq_enable = pci_msi_unmask_irq,
310 .irq_disable = pci_msi_mask_irq,
311 .irq_mask = pci_msi_mask_irq,
312 .irq_unmask = pci_msi_unmask_irq,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530313};
314
315/**
316 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
317 * @domain: IRQ domain
318 * @irq: Virtual IRQ number
319 * @hwirq: HW interrupt number
320 *
321 * Return: Always returns 0.
322 */
323static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
324 irq_hw_number_t hwirq)
325{
326 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
327 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530328
329 return 0;
330}
331
332/* IRQ Domain operations */
333static const struct irq_domain_ops msi_domain_ops = {
334 .map = xilinx_pcie_msi_map,
335};
336
337/**
338 * xilinx_pcie_enable_msi - Enable MSI support
339 * @port: PCIe port information
340 */
341static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
342{
343 phys_addr_t msg_addr;
344
345 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
346 msg_addr = virt_to_phys((void *)port->msi_pages);
347 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
348 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
349}
350
Srikanth Thokala8961def2014-08-20 21:56:02 +0530351/* INTx Functions */
352
353/**
354 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
355 * @domain: IRQ domain
356 * @irq: Virtual IRQ number
357 * @hwirq: HW interrupt number
358 *
359 * Return: Always returns 0.
360 */
361static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
362 irq_hw_number_t hwirq)
363{
364 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
365 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530366
367 return 0;
368}
369
370/* INTx IRQ Domain operations */
371static const struct irq_domain_ops intx_domain_ops = {
372 .map = xilinx_pcie_intx_map,
373};
374
375/* PCIe HW Functions */
376
377/**
378 * xilinx_pcie_intr_handler - Interrupt Service Handler
379 * @irq: IRQ number
380 * @data: PCIe port information
381 *
382 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
383 */
384static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
385{
386 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500387 struct device *dev = port->dev;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530388 u32 val, mask, status, msi_data;
389
390 /* Read interrupt decode and mask registers */
391 val = pcie_read(port, XILINX_PCIE_REG_IDR);
392 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
393
394 status = val & mask;
395 if (!status)
396 return IRQ_NONE;
397
398 if (status & XILINX_PCIE_INTR_LINK_DOWN)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500399 dev_warn(dev, "Link Down\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530400
401 if (status & XILINX_PCIE_INTR_ECRC_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500402 dev_warn(dev, "ECRC failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530403
404 if (status & XILINX_PCIE_INTR_STR_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500405 dev_warn(dev, "Streaming error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530406
407 if (status & XILINX_PCIE_INTR_HOT_RESET)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500408 dev_info(dev, "Hot reset\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530409
410 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500411 dev_warn(dev, "ECAM access timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530412
413 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500414 dev_warn(dev, "Correctable error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530415 xilinx_pcie_clear_err_interrupts(port);
416 }
417
418 if (status & XILINX_PCIE_INTR_NONFATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500419 dev_warn(dev, "Non fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530420 xilinx_pcie_clear_err_interrupts(port);
421 }
422
423 if (status & XILINX_PCIE_INTR_FATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500424 dev_warn(dev, "Fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530425 xilinx_pcie_clear_err_interrupts(port);
426 }
427
428 if (status & XILINX_PCIE_INTR_INTX) {
429 /* INTx interrupt received */
430 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
431
432 /* Check whether interrupt valid */
433 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500434 dev_warn(dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530435 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530436 }
437
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100438 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
439 /* Clear interrupt FIFO register 1 */
440 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
441 XILINX_PCIE_REG_RPIFR1);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530442
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100443 /* Handle INTx Interrupt */
444 val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
445 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530446 generic_handle_irq(irq_find_mapping(port->leg_domain,
Russell Joycee4a8f8e2015-07-07 17:54:19 +0100447 val));
448 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530449 }
450
451 if (status & XILINX_PCIE_INTR_MSI) {
452 /* MSI Interrupt */
453 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
454
455 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500456 dev_warn(dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530457 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530458 }
459
460 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
461 msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
462 XILINX_PCIE_RPIFR2_MSG_DATA;
463
464 /* Clear interrupt FIFO register 1 */
465 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
466 XILINX_PCIE_REG_RPIFR1);
467
468 if (IS_ENABLED(CONFIG_PCI_MSI)) {
469 /* Handle MSI Interrupt */
470 generic_handle_irq(msi_data);
471 }
472 }
473 }
474
475 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500476 dev_warn(dev, "Slave unsupported request\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530477
478 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500479 dev_warn(dev, "Slave unexpected completion\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530480
481 if (status & XILINX_PCIE_INTR_SLV_COMPL)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500482 dev_warn(dev, "Slave completion timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530483
484 if (status & XILINX_PCIE_INTR_SLV_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500485 dev_warn(dev, "Slave Error Poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530486
487 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500488 dev_warn(dev, "Slave Completer Abort\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530489
490 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500491 dev_warn(dev, "Slave Illegal Burst\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530492
493 if (status & XILINX_PCIE_INTR_MST_DECERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500494 dev_warn(dev, "Master decode error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530495
496 if (status & XILINX_PCIE_INTR_MST_SLVERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500497 dev_warn(dev, "Master slave error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530498
499 if (status & XILINX_PCIE_INTR_MST_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500500 dev_warn(dev, "Master error poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530501
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530502error:
Srikanth Thokala8961def2014-08-20 21:56:02 +0530503 /* Clear the Interrupt Decode register */
504 pcie_write(port, status, XILINX_PCIE_REG_IDR);
505
506 return IRQ_HANDLED;
507}
508
509/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530510 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
511 * @port: PCIe port information
512 *
513 * Return: '0' on success and error value on failure
514 */
515static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
516{
517 struct device *dev = port->dev;
518 struct device_node *node = dev->of_node;
519 struct device_node *pcie_intc_node;
520
521 /* Setup INTx */
522 pcie_intc_node = of_get_next_child(node, NULL);
523 if (!pcie_intc_node) {
524 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200525 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530526 }
527
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530528 port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530529 &intx_domain_ops,
530 port);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530531 if (!port->leg_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530532 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200533 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530534 }
535
536 /* Setup MSI */
537 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530538 port->msi_domain = irq_domain_add_linear(node,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530539 XILINX_NUM_MSI_IRQS,
540 &msi_domain_ops,
541 &xilinx_pcie_msi_chip);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530542 if (!port->msi_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530543 dev_err(dev, "Failed to get a MSI IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200544 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530545 }
546
547 xilinx_pcie_enable_msi(port);
548 }
549
550 return 0;
551}
552
553/**
554 * xilinx_pcie_init_port - Initialize hardware
555 * @port: PCIe port information
556 */
557static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
558{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500559 struct device *dev = port->dev;
560
Srikanth Thokala8961def2014-08-20 21:56:02 +0530561 if (xilinx_pcie_link_is_up(port))
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500562 dev_info(dev, "PCIe Link is UP\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530563 else
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500564 dev_info(dev, "PCIe Link is DOWN\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530565
566 /* Disable all interrupts */
567 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
568 XILINX_PCIE_REG_IMR);
569
570 /* Clear pending interrupts */
571 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
572 XILINX_PCIE_IMR_ALL_MASK,
573 XILINX_PCIE_REG_IDR);
574
575 /* Enable all interrupts */
576 pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
577
578 /* Enable the Bridge enable bit */
579 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
580 XILINX_PCIE_REG_RPSC_BEN,
581 XILINX_PCIE_REG_RPSC);
582}
583
584/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530585 * xilinx_pcie_parse_dt - Parse Device tree
586 * @port: PCIe port information
587 *
588 * Return: '0' on success and error value on failure
589 */
590static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
591{
592 struct device *dev = port->dev;
593 struct device_node *node = dev->of_node;
594 struct resource regs;
595 const char *type;
596 int err;
597
598 type = of_get_property(node, "device_type", NULL);
599 if (!type || strcmp(type, "pci")) {
600 dev_err(dev, "invalid \"device_type\" %s\n", type);
601 return -EINVAL;
602 }
603
604 err = of_address_to_resource(node, 0, &regs);
605 if (err) {
606 dev_err(dev, "missing \"reg\" property\n");
607 return err;
608 }
609
610 port->reg_base = devm_ioremap_resource(dev, &regs);
611 if (IS_ERR(port->reg_base))
612 return PTR_ERR(port->reg_base);
613
614 port->irq = irq_of_parse_and_map(node, 0);
615 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200616 IRQF_SHARED | IRQF_NO_THREAD,
617 "xilinx-pcie", port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530618 if (err) {
619 dev_err(dev, "unable to request irq %d\n", port->irq);
620 return err;
621 }
622
623 return 0;
624}
625
626/**
627 * xilinx_pcie_probe - Probe function
628 * @pdev: Platform device pointer
629 *
630 * Return: '0' on success and error value on failure
631 */
632static int xilinx_pcie_probe(struct platform_device *pdev)
633{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530634 struct device *dev = &pdev->dev;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500635 struct xilinx_pcie_port *port;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530636 struct pci_bus *bus;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530637 int err;
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530638 resource_size_t iobase = 0;
639 LIST_HEAD(res);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530640
641 if (!dev->of_node)
642 return -ENODEV;
643
644 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
645 if (!port)
646 return -ENOMEM;
647
648 port->dev = dev;
649
650 err = xilinx_pcie_parse_dt(port);
651 if (err) {
652 dev_err(dev, "Parsing DT failed\n");
653 return err;
654 }
655
656 xilinx_pcie_init_port(port);
657
658 err = xilinx_pcie_init_irq_domain(port);
659 if (err) {
660 dev_err(dev, "Failed creating IRQ Domain\n");
661 return err;
662 }
663
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530664 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
665 &iobase);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530666 if (err) {
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530667 dev_err(dev, "Getting bridge resources failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530668 return err;
669 }
Bjorn Helgaas93a5b5e2016-05-28 18:27:03 -0500670
671 err = devm_request_pci_bus_resources(dev, &res);
672 if (err)
673 goto error;
674
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500675 bus = pci_create_root_bus(dev, 0, &xilinx_pcie_ops, port, &res);
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500676 if (!bus) {
677 err = -ENOMEM;
678 goto error;
679 }
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700680
681#ifdef CONFIG_PCI_MSI
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500682 xilinx_pcie_msi_chip.dev = dev;
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530683 bus->msi = &xilinx_pcie_msi_chip;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700684#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530685 pci_scan_child_bus(bus);
686 pci_assign_unassigned_bus_resources(bus);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530687#ifndef CONFIG_MICROBLAZE
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530688 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
Bharat Kumar Gogada2c513912016-02-11 21:58:09 +0530689#endif
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530690 pci_bus_add_devices(bus);
691 platform_set_drvdata(pdev, port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530692
693 return 0;
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500694
695error:
696 pci_free_resource_list(&res);
697 return err;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530698}
699
Srikanth Thokala8961def2014-08-20 21:56:02 +0530700static struct of_device_id xilinx_pcie_of_match[] = {
701 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
702 {}
703};
704
705static struct platform_driver xilinx_pcie_driver = {
706 .driver = {
707 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530708 .of_match_table = xilinx_pcie_of_match,
709 .suppress_bind_attrs = true,
710 },
711 .probe = xilinx_pcie_probe,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530712};
Paul Gortmakerda4eafc2016-08-24 16:57:49 -0400713builtin_platform_driver(xilinx_pcie_driver);