blob: 88c96e5669e0786709457fa64fa7dd61910f7f86 [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Srikanth Thokala8961def2014-08-20 21:56:02 +05302/*
3 * PCIe host controller driver for Xilinx AXI PCIe Bridge
4 *
5 * Copyright (c) 2012 - 2014 Xilinx, Inc.
6 *
7 * Based on the Tegra PCIe driver
8 *
Bjorn Helgaas96291d52017-09-01 16:35:50 -05009 * Bits taken from Synopsys DesignWare Host controller driver and
Srikanth Thokala8961def2014-08-20 21:56:02 +053010 * ARM PCI Host generic driver.
Srikanth Thokala8961def2014-08-20 21:56:02 +053011 */
12
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/kernel.h>
Paul Gortmakerda4eafc2016-08-24 16:57:49 -040017#include <linux/init.h>
Srikanth Thokala8961def2014-08-20 21:56:02 +053018#include <linux/msi.h>
19#include <linux/of_address.h>
20#include <linux/of_pci.h>
21#include <linux/of_platform.h>
22#include <linux/of_irq.h>
23#include <linux/pci.h>
24#include <linux/platform_device.h>
25
26/* Register definitions */
27#define XILINX_PCIE_REG_BIR 0x00000130
28#define XILINX_PCIE_REG_IDR 0x00000138
29#define XILINX_PCIE_REG_IMR 0x0000013c
30#define XILINX_PCIE_REG_PSCR 0x00000144
31#define XILINX_PCIE_REG_RPSC 0x00000148
32#define XILINX_PCIE_REG_MSIBASE1 0x0000014c
33#define XILINX_PCIE_REG_MSIBASE2 0x00000150
34#define XILINX_PCIE_REG_RPEFR 0x00000154
35#define XILINX_PCIE_REG_RPIFR1 0x00000158
36#define XILINX_PCIE_REG_RPIFR2 0x0000015c
37
38/* Interrupt registers definitions */
39#define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
40#define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
41#define XILINX_PCIE_INTR_STR_ERR BIT(2)
42#define XILINX_PCIE_INTR_HOT_RESET BIT(3)
43#define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
44#define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
45#define XILINX_PCIE_INTR_NONFATAL BIT(10)
46#define XILINX_PCIE_INTR_FATAL BIT(11)
47#define XILINX_PCIE_INTR_INTX BIT(16)
48#define XILINX_PCIE_INTR_MSI BIT(17)
49#define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
50#define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
51#define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
52#define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
53#define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
54#define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
55#define XILINX_PCIE_INTR_MST_DECERR BIT(26)
56#define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
57#define XILINX_PCIE_INTR_MST_ERRP BIT(28)
58#define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
Paul Burtonaac2e962017-08-15 16:25:25 -050059#define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
Srikanth Thokala8961def2014-08-20 21:56:02 +053060#define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
61
62/* Root Port Error FIFO Read Register definitions */
63#define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
64#define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
65#define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
66
67/* Root Port Interrupt FIFO Read Register 1 definitions */
68#define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
69#define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
70#define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
71#define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
72#define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
73
74/* Bridge Info Register definitions */
75#define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
76#define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
77
78/* Root Port Interrupt FIFO Read Register 2 definitions */
79#define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
80
81/* Root Port Status/control Register definitions */
82#define XILINX_PCIE_REG_RPSC_BEN BIT(0)
83
84/* Phy Status/Control Register definitions */
85#define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
86
87/* ECAM definitions */
88#define ECAM_BUS_NUM_SHIFT 20
89#define ECAM_DEV_NUM_SHIFT 12
90
91/* Number of MSI IRQs */
92#define XILINX_NUM_MSI_IRQS 128
93
Srikanth Thokala8961def2014-08-20 21:56:02 +053094/**
95 * struct xilinx_pcie_port - PCIe port information
96 * @reg_base: IO Mapped Register Base
97 * @irq: Interrupt number
98 * @msi_pages: MSI pages
99 * @root_busno: Root Bus number
100 * @dev: Device pointer
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530101 * @msi_domain: MSI IRQ domain pointer
102 * @leg_domain: Legacy IRQ domain pointer
Srikanth Thokala8961def2014-08-20 21:56:02 +0530103 * @resources: Bus Resources
104 */
105struct xilinx_pcie_port {
106 void __iomem *reg_base;
107 u32 irq;
108 unsigned long msi_pages;
109 u8 root_busno;
110 struct device *dev;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530111 struct irq_domain *msi_domain;
112 struct irq_domain *leg_domain;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530113 struct list_head resources;
114};
115
116static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
117
Srikanth Thokala8961def2014-08-20 21:56:02 +0530118static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
119{
120 return readl(port->reg_base + reg);
121}
122
123static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
124{
125 writel(val, port->reg_base + reg);
126}
127
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600128static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port *port)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530129{
130 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
131 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
132}
133
134/**
135 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
136 * @port: PCIe port information
137 */
138static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
139{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500140 struct device *dev = port->dev;
Arnd Bergmannabc596b2015-01-13 15:20:05 +0100141 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530142
143 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500144 dev_dbg(dev, "Requester ID %lu\n",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530145 val & XILINX_PCIE_RPEFR_REQ_ID);
146 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
147 XILINX_PCIE_REG_RPEFR);
148 }
149}
150
151/**
152 * xilinx_pcie_valid_device - Check if a valid device is present on bus
153 * @bus: PCI Bus structure
154 * @devfn: device/function
155 *
156 * Return: 'true' on success and 'false' if invalid device is found
157 */
158static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
159{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530160 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530161
162 /* Check if link is up when trying to access downstream ports */
163 if (bus->number != port->root_busno)
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600164 if (!xilinx_pcie_link_up(port))
Srikanth Thokala8961def2014-08-20 21:56:02 +0530165 return false;
166
167 /* Only one device down on each root port */
168 if (bus->number == port->root_busno && devfn > 0)
169 return false;
170
Srikanth Thokala8961def2014-08-20 21:56:02 +0530171 return true;
172}
173
174/**
Rob Herring029e2152015-01-09 20:34:50 -0600175 * xilinx_pcie_map_bus - Get configuration base
Srikanth Thokala8961def2014-08-20 21:56:02 +0530176 * @bus: PCI Bus structure
177 * @devfn: Device/function
178 * @where: Offset from base
179 *
180 * Return: Base address of the configuration space needed to be
181 * accessed.
182 */
Rob Herring029e2152015-01-09 20:34:50 -0600183static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
184 unsigned int devfn, int where)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530185{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530186 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530187 int relbus;
188
Rob Herring029e2152015-01-09 20:34:50 -0600189 if (!xilinx_pcie_valid_device(bus, devfn))
190 return NULL;
191
Srikanth Thokala8961def2014-08-20 21:56:02 +0530192 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
193 (devfn << ECAM_DEV_NUM_SHIFT);
194
195 return port->reg_base + relbus + where;
196}
197
Srikanth Thokala8961def2014-08-20 21:56:02 +0530198/* PCIe operations */
199static struct pci_ops xilinx_pcie_ops = {
Rob Herring029e2152015-01-09 20:34:50 -0600200 .map_bus = xilinx_pcie_map_bus,
201 .read = pci_generic_config_read,
202 .write = pci_generic_config_write,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530203};
204
205/* MSI functions */
206
207/**
208 * xilinx_pcie_destroy_msi - Free MSI number
209 * @irq: IRQ to be freed
210 */
211static void xilinx_pcie_destroy_msi(unsigned int irq)
212{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530213 struct msi_desc *msi;
214 struct xilinx_pcie_port *port;
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530215 struct irq_data *d = irq_get_irq_data(irq);
216 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530217
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530218 if (!test_bit(hwirq, msi_irq_in_use)) {
Jiang Liue39758e2015-07-09 16:00:43 +0800219 msi = irq_get_msi_desc(irq);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530220 port = msi_desc_to_pci_sysdata(msi);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530221 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800222 } else {
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530223 clear_bit(hwirq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800224 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530225}
226
227/**
228 * xilinx_pcie_assign_msi - Allocate MSI number
Srikanth Thokala8961def2014-08-20 21:56:02 +0530229 *
230 * Return: A valid IRQ on success and error value on failure.
231 */
Bjorn Helgaase59e5ff2016-10-11 11:36:49 -0500232static int xilinx_pcie_assign_msi(void)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530233{
234 int pos;
235
236 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
237 if (pos < XILINX_NUM_MSI_IRQS)
238 set_bit(pos, msi_irq_in_use);
239 else
240 return -ENOSPC;
241
242 return pos;
243}
244
245/**
246 * xilinx_msi_teardown_irq - Destroy the MSI
247 * @chip: MSI Chip descriptor
248 * @irq: MSI IRQ to destroy
249 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700250static void xilinx_msi_teardown_irq(struct msi_controller *chip,
251 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530252{
253 xilinx_pcie_destroy_msi(irq);
Bharat Kumar Gogadab328f3c2016-09-01 15:44:44 +0530254 irq_dispose_mapping(irq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530255}
256
257/**
258 * xilinx_pcie_msi_setup_irq - Setup MSI request
259 * @chip: MSI chip pointer
260 * @pdev: PCIe device pointer
261 * @desc: MSI descriptor pointer
262 *
263 * Return: '0' on success and error value on failure
264 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700265static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530266 struct pci_dev *pdev,
267 struct msi_desc *desc)
268{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530269 struct xilinx_pcie_port *port = pdev->bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530270 unsigned int irq;
271 int hwirq;
272 struct msi_msg msg;
273 phys_addr_t msg_addr;
274
Bjorn Helgaase59e5ff2016-10-11 11:36:49 -0500275 hwirq = xilinx_pcie_assign_msi();
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300276 if (hwirq < 0)
277 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530278
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530279 irq = irq_create_mapping(port->msi_domain, hwirq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530280 if (!irq)
281 return -EINVAL;
282
283 irq_set_msi_desc(irq, desc);
284
285 msg_addr = virt_to_phys((void *)port->msi_pages);
286
287 msg.address_hi = 0;
288 msg.address_lo = msg_addr;
289 msg.data = irq;
290
Jiang Liu83a18912014-11-09 23:10:34 +0800291 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530292
293 return 0;
294}
295
296/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700297static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530298 .setup_irq = xilinx_pcie_msi_setup_irq,
299 .teardown_irq = xilinx_msi_teardown_irq,
300};
301
302/* HW Interrupt Chip Descriptor */
303static struct irq_chip xilinx_msi_irq_chip = {
304 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100305 .irq_enable = pci_msi_unmask_irq,
306 .irq_disable = pci_msi_mask_irq,
307 .irq_mask = pci_msi_mask_irq,
308 .irq_unmask = pci_msi_unmask_irq,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530309};
310
311/**
312 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
313 * @domain: IRQ domain
314 * @irq: Virtual IRQ number
315 * @hwirq: HW interrupt number
316 *
317 * Return: Always returns 0.
318 */
319static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
320 irq_hw_number_t hwirq)
321{
322 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
323 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530324
325 return 0;
326}
327
328/* IRQ Domain operations */
329static const struct irq_domain_ops msi_domain_ops = {
330 .map = xilinx_pcie_msi_map,
331};
332
333/**
334 * xilinx_pcie_enable_msi - Enable MSI support
335 * @port: PCIe port information
336 */
337static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
338{
339 phys_addr_t msg_addr;
340
341 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
342 msg_addr = virt_to_phys((void *)port->msi_pages);
343 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
344 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
345}
346
Srikanth Thokala8961def2014-08-20 21:56:02 +0530347/* INTx Functions */
348
349/**
350 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
351 * @domain: IRQ domain
352 * @irq: Virtual IRQ number
353 * @hwirq: HW interrupt number
354 *
355 * Return: Always returns 0.
356 */
357static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
358 irq_hw_number_t hwirq)
359{
360 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
361 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530362
363 return 0;
364}
365
366/* INTx IRQ Domain operations */
367static const struct irq_domain_ops intx_domain_ops = {
368 .map = xilinx_pcie_intx_map,
Paul Burton5c125682017-08-15 16:25:08 -0500369 .xlate = pci_irqd_intx_xlate,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530370};
371
372/* PCIe HW Functions */
373
374/**
375 * xilinx_pcie_intr_handler - Interrupt Service Handler
376 * @irq: IRQ number
377 * @data: PCIe port information
378 *
379 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
380 */
381static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
382{
383 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500384 struct device *dev = port->dev;
Paul Burtond0b5dda2017-08-15 16:25:21 -0500385 u32 val, mask, status;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530386
387 /* Read interrupt decode and mask registers */
388 val = pcie_read(port, XILINX_PCIE_REG_IDR);
389 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
390
391 status = val & mask;
392 if (!status)
393 return IRQ_NONE;
394
395 if (status & XILINX_PCIE_INTR_LINK_DOWN)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500396 dev_warn(dev, "Link Down\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530397
398 if (status & XILINX_PCIE_INTR_ECRC_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500399 dev_warn(dev, "ECRC failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530400
401 if (status & XILINX_PCIE_INTR_STR_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500402 dev_warn(dev, "Streaming error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530403
404 if (status & XILINX_PCIE_INTR_HOT_RESET)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500405 dev_info(dev, "Hot reset\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530406
407 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500408 dev_warn(dev, "ECAM access timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530409
410 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500411 dev_warn(dev, "Correctable error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530412 xilinx_pcie_clear_err_interrupts(port);
413 }
414
415 if (status & XILINX_PCIE_INTR_NONFATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500416 dev_warn(dev, "Non fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530417 xilinx_pcie_clear_err_interrupts(port);
418 }
419
420 if (status & XILINX_PCIE_INTR_FATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500421 dev_warn(dev, "Fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530422 xilinx_pcie_clear_err_interrupts(port);
423 }
424
Paul Burtond0b5dda2017-08-15 16:25:21 -0500425 if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530426 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
427
428 /* Check whether interrupt valid */
429 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500430 dev_warn(dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530431 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530432 }
433
Paul Burtond0b5dda2017-08-15 16:25:21 -0500434 /* Decode the IRQ number */
435 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
436 val = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
437 XILINX_PCIE_RPIFR2_MSG_DATA;
438 } else {
Paul Burton5c125682017-08-15 16:25:08 -0500439 val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
440 XILINX_PCIE_RPIFR1_INTR_SHIFT;
Paul Burtond0b5dda2017-08-15 16:25:21 -0500441 val = irq_find_mapping(port->leg_domain, val);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530442 }
443
Paul Burtond0b5dda2017-08-15 16:25:21 -0500444 /* Clear interrupt FIFO register 1 */
445 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
446 XILINX_PCIE_REG_RPIFR1);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530447
Paul Burtond0b5dda2017-08-15 16:25:21 -0500448 /* Handle the interrupt */
449 if (IS_ENABLED(CONFIG_PCI_MSI) ||
450 !(val & XILINX_PCIE_RPIFR1_MSI_INTR))
451 generic_handle_irq(val);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530452 }
453
454 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500455 dev_warn(dev, "Slave unsupported request\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530456
457 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500458 dev_warn(dev, "Slave unexpected completion\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530459
460 if (status & XILINX_PCIE_INTR_SLV_COMPL)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500461 dev_warn(dev, "Slave completion timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530462
463 if (status & XILINX_PCIE_INTR_SLV_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500464 dev_warn(dev, "Slave Error Poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530465
466 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500467 dev_warn(dev, "Slave Completer Abort\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530468
469 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500470 dev_warn(dev, "Slave Illegal Burst\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530471
472 if (status & XILINX_PCIE_INTR_MST_DECERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500473 dev_warn(dev, "Master decode error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530474
475 if (status & XILINX_PCIE_INTR_MST_SLVERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500476 dev_warn(dev, "Master slave error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530477
478 if (status & XILINX_PCIE_INTR_MST_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500479 dev_warn(dev, "Master error poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530480
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530481error:
Srikanth Thokala8961def2014-08-20 21:56:02 +0530482 /* Clear the Interrupt Decode register */
483 pcie_write(port, status, XILINX_PCIE_REG_IDR);
484
485 return IRQ_HANDLED;
486}
487
488/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530489 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
490 * @port: PCIe port information
491 *
492 * Return: '0' on success and error value on failure
493 */
494static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
495{
496 struct device *dev = port->dev;
497 struct device_node *node = dev->of_node;
498 struct device_node *pcie_intc_node;
499
500 /* Setup INTx */
501 pcie_intc_node = of_get_next_child(node, NULL);
502 if (!pcie_intc_node) {
503 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200504 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530505 }
506
Paul Burton5c125682017-08-15 16:25:08 -0500507 port->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530508 &intx_domain_ops,
509 port);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530510 if (!port->leg_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530511 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200512 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530513 }
514
515 /* Setup MSI */
516 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530517 port->msi_domain = irq_domain_add_linear(node,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530518 XILINX_NUM_MSI_IRQS,
519 &msi_domain_ops,
520 &xilinx_pcie_msi_chip);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530521 if (!port->msi_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530522 dev_err(dev, "Failed to get a MSI IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200523 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530524 }
525
526 xilinx_pcie_enable_msi(port);
527 }
528
529 return 0;
530}
531
532/**
533 * xilinx_pcie_init_port - Initialize hardware
534 * @port: PCIe port information
535 */
536static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
537{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500538 struct device *dev = port->dev;
539
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600540 if (xilinx_pcie_link_up(port))
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500541 dev_info(dev, "PCIe Link is UP\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530542 else
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500543 dev_info(dev, "PCIe Link is DOWN\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530544
545 /* Disable all interrupts */
546 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
547 XILINX_PCIE_REG_IMR);
548
549 /* Clear pending interrupts */
550 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
551 XILINX_PCIE_IMR_ALL_MASK,
552 XILINX_PCIE_REG_IDR);
553
Paul Burtonaac2e962017-08-15 16:25:25 -0500554 /* Enable all interrupts we handle */
555 pcie_write(port, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530556
557 /* Enable the Bridge enable bit */
558 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
559 XILINX_PCIE_REG_RPSC_BEN,
560 XILINX_PCIE_REG_RPSC);
561}
562
563/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530564 * xilinx_pcie_parse_dt - Parse Device tree
565 * @port: PCIe port information
566 *
567 * Return: '0' on success and error value on failure
568 */
569static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
570{
571 struct device *dev = port->dev;
572 struct device_node *node = dev->of_node;
573 struct resource regs;
574 const char *type;
575 int err;
576
577 type = of_get_property(node, "device_type", NULL);
578 if (!type || strcmp(type, "pci")) {
579 dev_err(dev, "invalid \"device_type\" %s\n", type);
580 return -EINVAL;
581 }
582
583 err = of_address_to_resource(node, 0, &regs);
584 if (err) {
585 dev_err(dev, "missing \"reg\" property\n");
586 return err;
587 }
588
Lorenzo Pieralisieb21f9e2017-04-19 17:48:57 +0100589 port->reg_base = devm_pci_remap_cfg_resource(dev, &regs);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530590 if (IS_ERR(port->reg_base))
591 return PTR_ERR(port->reg_base);
592
593 port->irq = irq_of_parse_and_map(node, 0);
594 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200595 IRQF_SHARED | IRQF_NO_THREAD,
596 "xilinx-pcie", port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530597 if (err) {
598 dev_err(dev, "unable to request irq %d\n", port->irq);
599 return err;
600 }
601
602 return 0;
603}
604
605/**
606 * xilinx_pcie_probe - Probe function
607 * @pdev: Platform device pointer
608 *
609 * Return: '0' on success and error value on failure
610 */
611static int xilinx_pcie_probe(struct platform_device *pdev)
612{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530613 struct device *dev = &pdev->dev;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500614 struct xilinx_pcie_port *port;
Bjorn Helgaasec6bd782017-02-08 15:37:47 -0600615 struct pci_bus *bus, *child;
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500616 struct pci_host_bridge *bridge;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530617 int err;
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530618 resource_size_t iobase = 0;
619 LIST_HEAD(res);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530620
621 if (!dev->of_node)
622 return -ENODEV;
623
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500624 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
625 if (!bridge)
626 return -ENODEV;
627
628 port = pci_host_bridge_priv(bridge);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530629
630 port->dev = dev;
631
632 err = xilinx_pcie_parse_dt(port);
633 if (err) {
634 dev_err(dev, "Parsing DT failed\n");
635 return err;
636 }
637
638 xilinx_pcie_init_port(port);
639
640 err = xilinx_pcie_init_irq_domain(port);
641 if (err) {
642 dev_err(dev, "Failed creating IRQ Domain\n");
643 return err;
644 }
645
Jan Kiszka5bd51b32018-05-15 11:07:05 +0200646 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &res,
Jan Kiszka055f87a2018-05-15 11:07:03 +0200647 &iobase);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530648 if (err) {
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530649 dev_err(dev, "Getting bridge resources failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530650 return err;
651 }
Bjorn Helgaas93a5b5e2016-05-28 18:27:03 -0500652
653 err = devm_request_pci_bus_resources(dev, &res);
654 if (err)
655 goto error;
656
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500657
658 list_splice_init(&res, &bridge->windows);
659 bridge->dev.parent = dev;
660 bridge->sysdata = port;
661 bridge->busnr = 0;
662 bridge->ops = &xilinx_pcie_ops;
Lorenzo Pieralisicc2eaae2017-06-28 15:14:06 -0500663 bridge->map_irq = of_irq_parse_and_map_pci;
664 bridge->swizzle_irq = pci_common_swizzle;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700665
666#ifdef CONFIG_PCI_MSI
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500667 xilinx_pcie_msi_chip.dev = dev;
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500668 bridge->msi = &xilinx_pcie_msi_chip;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700669#endif
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500670 err = pci_scan_root_bus_bridge(bridge);
671 if (err < 0)
672 goto error;
673
674 bus = bridge->bus;
675
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530676 pci_assign_unassigned_bus_resources(bus);
Bjorn Helgaasec6bd782017-02-08 15:37:47 -0600677 list_for_each_entry(child, &bus->children, node)
678 pcie_bus_configure_settings(child);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530679 pci_bus_add_devices(bus);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530680 return 0;
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500681
682error:
683 pci_free_resource_list(&res);
684 return err;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530685}
686
Arvind Yadavfdc71ce2017-06-20 11:17:48 +0530687static const struct of_device_id xilinx_pcie_of_match[] = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530688 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
689 {}
690};
691
692static struct platform_driver xilinx_pcie_driver = {
693 .driver = {
694 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530695 .of_match_table = xilinx_pcie_of_match,
696 .suppress_bind_attrs = true,
697 },
698 .probe = xilinx_pcie_probe,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530699};
Paul Gortmakerda4eafc2016-08-24 16:57:49 -0400700builtin_platform_driver(xilinx_pcie_driver);