blob: b110a3a814e35e3bbd7839e68994cef56f19ed43 [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
Rob Herring9e2aee82018-05-11 12:15:30 -050026#include "../pci.h"
27
Srikanth Thokala8961def2014-08-20 21:56:02 +053028/* Register definitions */
29#define XILINX_PCIE_REG_BIR 0x00000130
30#define XILINX_PCIE_REG_IDR 0x00000138
31#define XILINX_PCIE_REG_IMR 0x0000013c
32#define XILINX_PCIE_REG_PSCR 0x00000144
33#define XILINX_PCIE_REG_RPSC 0x00000148
34#define XILINX_PCIE_REG_MSIBASE1 0x0000014c
35#define XILINX_PCIE_REG_MSIBASE2 0x00000150
36#define XILINX_PCIE_REG_RPEFR 0x00000154
37#define XILINX_PCIE_REG_RPIFR1 0x00000158
38#define XILINX_PCIE_REG_RPIFR2 0x0000015c
39
40/* Interrupt registers definitions */
41#define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
42#define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
43#define XILINX_PCIE_INTR_STR_ERR BIT(2)
44#define XILINX_PCIE_INTR_HOT_RESET BIT(3)
45#define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
46#define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
47#define XILINX_PCIE_INTR_NONFATAL BIT(10)
48#define XILINX_PCIE_INTR_FATAL BIT(11)
49#define XILINX_PCIE_INTR_INTX BIT(16)
50#define XILINX_PCIE_INTR_MSI BIT(17)
51#define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
52#define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
53#define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
54#define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
55#define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
56#define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
57#define XILINX_PCIE_INTR_MST_DECERR BIT(26)
58#define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
59#define XILINX_PCIE_INTR_MST_ERRP BIT(28)
60#define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
Paul Burtonaac2e962017-08-15 16:25:25 -050061#define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
Srikanth Thokala8961def2014-08-20 21:56:02 +053062#define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
63
64/* Root Port Error FIFO Read Register definitions */
65#define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
66#define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
67#define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
68
69/* Root Port Interrupt FIFO Read Register 1 definitions */
70#define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
71#define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
72#define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
73#define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
74#define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
75
76/* Bridge Info Register definitions */
77#define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
78#define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
79
80/* Root Port Interrupt FIFO Read Register 2 definitions */
81#define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
82
83/* Root Port Status/control Register definitions */
84#define XILINX_PCIE_REG_RPSC_BEN BIT(0)
85
86/* Phy Status/Control Register definitions */
87#define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
88
89/* ECAM definitions */
90#define ECAM_BUS_NUM_SHIFT 20
91#define ECAM_DEV_NUM_SHIFT 12
92
93/* Number of MSI IRQs */
94#define XILINX_NUM_MSI_IRQS 128
95
Srikanth Thokala8961def2014-08-20 21:56:02 +053096/**
97 * struct xilinx_pcie_port - PCIe port information
98 * @reg_base: IO Mapped Register Base
99 * @irq: Interrupt number
100 * @msi_pages: MSI pages
101 * @root_busno: Root Bus number
102 * @dev: Device pointer
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530103 * @msi_domain: MSI IRQ domain pointer
104 * @leg_domain: Legacy IRQ domain pointer
Srikanth Thokala8961def2014-08-20 21:56:02 +0530105 * @resources: Bus Resources
106 */
107struct xilinx_pcie_port {
108 void __iomem *reg_base;
109 u32 irq;
110 unsigned long msi_pages;
111 u8 root_busno;
112 struct device *dev;
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530113 struct irq_domain *msi_domain;
114 struct irq_domain *leg_domain;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530115 struct list_head resources;
116};
117
118static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
119
Srikanth Thokala8961def2014-08-20 21:56:02 +0530120static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
121{
122 return readl(port->reg_base + reg);
123}
124
125static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
126{
127 writel(val, port->reg_base + reg);
128}
129
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600130static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port *port)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530131{
132 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
133 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
134}
135
136/**
137 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
138 * @port: PCIe port information
139 */
140static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
141{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500142 struct device *dev = port->dev;
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) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500146 dev_dbg(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)
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600166 if (!xilinx_pcie_link_up(port))
Srikanth Thokala8961def2014-08-20 21:56:02 +0530167 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
Srikanth Thokala8961def2014-08-20 21:56:02 +0530173 return true;
174}
175
176/**
Rob Herring029e2152015-01-09 20:34:50 -0600177 * xilinx_pcie_map_bus - Get configuration base
Srikanth Thokala8961def2014-08-20 21:56:02 +0530178 * @bus: PCI Bus structure
179 * @devfn: Device/function
180 * @where: Offset from base
181 *
182 * Return: Base address of the configuration space needed to be
183 * accessed.
184 */
Rob Herring029e2152015-01-09 20:34:50 -0600185static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
186 unsigned int devfn, int where)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530187{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530188 struct xilinx_pcie_port *port = bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530189 int relbus;
190
Rob Herring029e2152015-01-09 20:34:50 -0600191 if (!xilinx_pcie_valid_device(bus, devfn))
192 return NULL;
193
Srikanth Thokala8961def2014-08-20 21:56:02 +0530194 relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
195 (devfn << ECAM_DEV_NUM_SHIFT);
196
197 return port->reg_base + relbus + where;
198}
199
Srikanth Thokala8961def2014-08-20 21:56:02 +0530200/* PCIe operations */
201static struct pci_ops xilinx_pcie_ops = {
Rob Herring029e2152015-01-09 20:34:50 -0600202 .map_bus = xilinx_pcie_map_bus,
203 .read = pci_generic_config_read,
204 .write = pci_generic_config_write,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530205};
206
207/* MSI functions */
208
209/**
210 * xilinx_pcie_destroy_msi - Free MSI number
211 * @irq: IRQ to be freed
212 */
213static void xilinx_pcie_destroy_msi(unsigned int irq)
214{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530215 struct msi_desc *msi;
216 struct xilinx_pcie_port *port;
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530217 struct irq_data *d = irq_get_irq_data(irq);
218 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530219
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530220 if (!test_bit(hwirq, msi_irq_in_use)) {
Jiang Liue39758e2015-07-09 16:00:43 +0800221 msi = irq_get_msi_desc(irq);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530222 port = msi_desc_to_pci_sysdata(msi);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530223 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
Jiang Liue39758e2015-07-09 16:00:43 +0800224 } else {
Bharat Kumar Gogada8a4036e2016-09-01 15:44:43 +0530225 clear_bit(hwirq, msi_irq_in_use);
Jiang Liue39758e2015-07-09 16:00:43 +0800226 }
Srikanth Thokala8961def2014-08-20 21:56:02 +0530227}
228
229/**
230 * xilinx_pcie_assign_msi - Allocate MSI number
Srikanth Thokala8961def2014-08-20 21:56:02 +0530231 *
232 * Return: A valid IRQ on success and error value on failure.
233 */
Bjorn Helgaase59e5ff2016-10-11 11:36:49 -0500234static int xilinx_pcie_assign_msi(void)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530235{
236 int pos;
237
238 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
239 if (pos < XILINX_NUM_MSI_IRQS)
240 set_bit(pos, msi_irq_in_use);
241 else
242 return -ENOSPC;
243
244 return pos;
245}
246
247/**
248 * xilinx_msi_teardown_irq - Destroy the MSI
249 * @chip: MSI Chip descriptor
250 * @irq: MSI IRQ to destroy
251 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700252static void xilinx_msi_teardown_irq(struct msi_controller *chip,
253 unsigned int irq)
Srikanth Thokala8961def2014-08-20 21:56:02 +0530254{
255 xilinx_pcie_destroy_msi(irq);
Bharat Kumar Gogadab328f3c2016-09-01 15:44:44 +0530256 irq_dispose_mapping(irq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530257}
258
259/**
260 * xilinx_pcie_msi_setup_irq - Setup MSI request
261 * @chip: MSI chip pointer
262 * @pdev: PCIe device pointer
263 * @desc: MSI descriptor pointer
264 *
265 * Return: '0' on success and error value on failure
266 */
Yijing Wangc2791b82014-11-11 17:45:45 -0700267static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530268 struct pci_dev *pdev,
269 struct msi_desc *desc)
270{
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530271 struct xilinx_pcie_port *port = pdev->bus->sysdata;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530272 unsigned int irq;
273 int hwirq;
274 struct msi_msg msg;
275 phys_addr_t msg_addr;
276
Bjorn Helgaase59e5ff2016-10-11 11:36:49 -0500277 hwirq = xilinx_pcie_assign_msi();
Dan Carpenterf9dd0ce2014-09-09 15:11:50 +0300278 if (hwirq < 0)
279 return hwirq;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530280
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530281 irq = irq_create_mapping(port->msi_domain, hwirq);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530282 if (!irq)
283 return -EINVAL;
284
285 irq_set_msi_desc(irq, desc);
286
287 msg_addr = virt_to_phys((void *)port->msi_pages);
288
289 msg.address_hi = 0;
290 msg.address_lo = msg_addr;
291 msg.data = irq;
292
Jiang Liu83a18912014-11-09 23:10:34 +0800293 pci_write_msi_msg(irq, &msg);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530294
295 return 0;
296}
297
298/* MSI Chip Descriptor */
Yijing Wangc2791b82014-11-11 17:45:45 -0700299static struct msi_controller xilinx_pcie_msi_chip = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530300 .setup_irq = xilinx_pcie_msi_setup_irq,
301 .teardown_irq = xilinx_msi_teardown_irq,
302};
303
304/* HW Interrupt Chip Descriptor */
305static struct irq_chip xilinx_msi_irq_chip = {
306 .name = "Xilinx PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100307 .irq_enable = pci_msi_unmask_irq,
308 .irq_disable = pci_msi_mask_irq,
309 .irq_mask = pci_msi_mask_irq,
310 .irq_unmask = pci_msi_unmask_irq,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530311};
312
313/**
314 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
315 * @domain: IRQ domain
316 * @irq: Virtual IRQ number
317 * @hwirq: HW interrupt number
318 *
319 * Return: Always returns 0.
320 */
321static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
322 irq_hw_number_t hwirq)
323{
324 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
325 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530326
327 return 0;
328}
329
330/* IRQ Domain operations */
331static const struct irq_domain_ops msi_domain_ops = {
332 .map = xilinx_pcie_msi_map,
333};
334
335/**
336 * xilinx_pcie_enable_msi - Enable MSI support
337 * @port: PCIe port information
338 */
339static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
340{
341 phys_addr_t msg_addr;
342
343 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
344 msg_addr = virt_to_phys((void *)port->msi_pages);
345 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
346 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
347}
348
Srikanth Thokala8961def2014-08-20 21:56:02 +0530349/* INTx Functions */
350
351/**
352 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
353 * @domain: IRQ domain
354 * @irq: Virtual IRQ number
355 * @hwirq: HW interrupt number
356 *
357 * Return: Always returns 0.
358 */
359static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
360 irq_hw_number_t hwirq)
361{
362 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
363 irq_set_chip_data(irq, domain->host_data);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530364
365 return 0;
366}
367
368/* INTx IRQ Domain operations */
369static const struct irq_domain_ops intx_domain_ops = {
370 .map = xilinx_pcie_intx_map,
Paul Burton5c125682017-08-15 16:25:08 -0500371 .xlate = pci_irqd_intx_xlate,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530372};
373
374/* PCIe HW Functions */
375
376/**
377 * xilinx_pcie_intr_handler - Interrupt Service Handler
378 * @irq: IRQ number
379 * @data: PCIe port information
380 *
381 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
382 */
383static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
384{
385 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500386 struct device *dev = port->dev;
Paul Burtond0b5dda2017-08-15 16:25:21 -0500387 u32 val, mask, status;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530388
389 /* Read interrupt decode and mask registers */
390 val = pcie_read(port, XILINX_PCIE_REG_IDR);
391 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
392
393 status = val & mask;
394 if (!status)
395 return IRQ_NONE;
396
397 if (status & XILINX_PCIE_INTR_LINK_DOWN)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500398 dev_warn(dev, "Link Down\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530399
400 if (status & XILINX_PCIE_INTR_ECRC_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500401 dev_warn(dev, "ECRC failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530402
403 if (status & XILINX_PCIE_INTR_STR_ERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500404 dev_warn(dev, "Streaming error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530405
406 if (status & XILINX_PCIE_INTR_HOT_RESET)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500407 dev_info(dev, "Hot reset\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530408
409 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500410 dev_warn(dev, "ECAM access timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530411
412 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500413 dev_warn(dev, "Correctable error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530414 xilinx_pcie_clear_err_interrupts(port);
415 }
416
417 if (status & XILINX_PCIE_INTR_NONFATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500418 dev_warn(dev, "Non fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530419 xilinx_pcie_clear_err_interrupts(port);
420 }
421
422 if (status & XILINX_PCIE_INTR_FATAL) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500423 dev_warn(dev, "Fatal error message\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530424 xilinx_pcie_clear_err_interrupts(port);
425 }
426
Paul Burtond0b5dda2017-08-15 16:25:21 -0500427 if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530428 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
429
430 /* Check whether interrupt valid */
431 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500432 dev_warn(dev, "RP Intr FIFO1 read error\n");
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530433 goto error;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530434 }
435
Paul Burtond0b5dda2017-08-15 16:25:21 -0500436 /* Decode the IRQ number */
437 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
438 val = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
439 XILINX_PCIE_RPIFR2_MSG_DATA;
440 } else {
Paul Burton5c125682017-08-15 16:25:08 -0500441 val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
442 XILINX_PCIE_RPIFR1_INTR_SHIFT;
Paul Burtond0b5dda2017-08-15 16:25:21 -0500443 val = irq_find_mapping(port->leg_domain, val);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530444 }
445
Paul Burtond0b5dda2017-08-15 16:25:21 -0500446 /* Clear interrupt FIFO register 1 */
447 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
448 XILINX_PCIE_REG_RPIFR1);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530449
Paul Burtond0b5dda2017-08-15 16:25:21 -0500450 /* Handle the interrupt */
451 if (IS_ENABLED(CONFIG_PCI_MSI) ||
452 !(val & XILINX_PCIE_RPIFR1_MSI_INTR))
453 generic_handle_irq(val);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530454 }
455
456 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500457 dev_warn(dev, "Slave unsupported request\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530458
459 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500460 dev_warn(dev, "Slave unexpected completion\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530461
462 if (status & XILINX_PCIE_INTR_SLV_COMPL)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500463 dev_warn(dev, "Slave completion timeout\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530464
465 if (status & XILINX_PCIE_INTR_SLV_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500466 dev_warn(dev, "Slave Error Poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530467
468 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500469 dev_warn(dev, "Slave Completer Abort\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530470
471 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500472 dev_warn(dev, "Slave Illegal Burst\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530473
474 if (status & XILINX_PCIE_INTR_MST_DECERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500475 dev_warn(dev, "Master decode error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530476
477 if (status & XILINX_PCIE_INTR_MST_SLVERR)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500478 dev_warn(dev, "Master slave error\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530479
480 if (status & XILINX_PCIE_INTR_MST_ERRP)
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500481 dev_warn(dev, "Master error poison\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530482
Bharat Kumar Gogada3cd049a2016-09-01 15:44:42 +0530483error:
Srikanth Thokala8961def2014-08-20 21:56:02 +0530484 /* Clear the Interrupt Decode register */
485 pcie_write(port, status, XILINX_PCIE_REG_IDR);
486
487 return IRQ_HANDLED;
488}
489
490/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530491 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
492 * @port: PCIe port information
493 *
494 * Return: '0' on success and error value on failure
495 */
496static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
497{
498 struct device *dev = port->dev;
499 struct device_node *node = dev->of_node;
500 struct device_node *pcie_intc_node;
501
502 /* Setup INTx */
503 pcie_intc_node = of_get_next_child(node, NULL);
504 if (!pcie_intc_node) {
505 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200506 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530507 }
508
Paul Burton5c125682017-08-15 16:25:08 -0500509 port->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530510 &intx_domain_ops,
511 port);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530512 if (!port->leg_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530513 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200514 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530515 }
516
517 /* Setup MSI */
518 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530519 port->msi_domain = irq_domain_add_linear(node,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530520 XILINX_NUM_MSI_IRQS,
521 &msi_domain_ops,
522 &xilinx_pcie_msi_chip);
Bharat Kumar Gogadab584fa12016-09-01 15:44:41 +0530523 if (!port->msi_domain) {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530524 dev_err(dev, "Failed to get a MSI IRQ domain\n");
Christophe JAILLETcec6dba2016-07-14 12:10:46 +0200525 return -ENODEV;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530526 }
527
528 xilinx_pcie_enable_msi(port);
529 }
530
531 return 0;
532}
533
534/**
535 * xilinx_pcie_init_port - Initialize hardware
536 * @port: PCIe port information
537 */
538static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
539{
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500540 struct device *dev = port->dev;
541
Bjorn Helgaas1eaa8702017-11-09 16:19:47 -0600542 if (xilinx_pcie_link_up(port))
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500543 dev_info(dev, "PCIe Link is UP\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530544 else
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500545 dev_info(dev, "PCIe Link is DOWN\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530546
547 /* Disable all interrupts */
548 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
549 XILINX_PCIE_REG_IMR);
550
551 /* Clear pending interrupts */
552 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
553 XILINX_PCIE_IMR_ALL_MASK,
554 XILINX_PCIE_REG_IDR);
555
Paul Burtonaac2e962017-08-15 16:25:25 -0500556 /* Enable all interrupts we handle */
557 pcie_write(port, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530558
559 /* Enable the Bridge enable bit */
560 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
561 XILINX_PCIE_REG_RPSC_BEN,
562 XILINX_PCIE_REG_RPSC);
563}
564
565/**
Srikanth Thokala8961def2014-08-20 21:56:02 +0530566 * xilinx_pcie_parse_dt - Parse Device tree
567 * @port: PCIe port information
568 *
569 * Return: '0' on success and error value on failure
570 */
571static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
572{
573 struct device *dev = port->dev;
574 struct device_node *node = dev->of_node;
575 struct resource regs;
576 const char *type;
577 int err;
578
579 type = of_get_property(node, "device_type", NULL);
580 if (!type || strcmp(type, "pci")) {
581 dev_err(dev, "invalid \"device_type\" %s\n", type);
582 return -EINVAL;
583 }
584
585 err = of_address_to_resource(node, 0, &regs);
586 if (err) {
587 dev_err(dev, "missing \"reg\" property\n");
588 return err;
589 }
590
Lorenzo Pieralisieb21f9e2017-04-19 17:48:57 +0100591 port->reg_base = devm_pci_remap_cfg_resource(dev, &regs);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530592 if (IS_ERR(port->reg_base))
593 return PTR_ERR(port->reg_base);
594
595 port->irq = irq_of_parse_and_map(node, 0);
596 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200597 IRQF_SHARED | IRQF_NO_THREAD,
598 "xilinx-pcie", port);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530599 if (err) {
600 dev_err(dev, "unable to request irq %d\n", port->irq);
601 return err;
602 }
603
604 return 0;
605}
606
607/**
608 * xilinx_pcie_probe - Probe function
609 * @pdev: Platform device pointer
610 *
611 * Return: '0' on success and error value on failure
612 */
613static int xilinx_pcie_probe(struct platform_device *pdev)
614{
Srikanth Thokala8961def2014-08-20 21:56:02 +0530615 struct device *dev = &pdev->dev;
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500616 struct xilinx_pcie_port *port;
Bjorn Helgaasec6bd782017-02-08 15:37:47 -0600617 struct pci_bus *bus, *child;
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500618 struct pci_host_bridge *bridge;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530619 int err;
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530620 resource_size_t iobase = 0;
621 LIST_HEAD(res);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530622
623 if (!dev->of_node)
624 return -ENODEV;
625
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500626 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
627 if (!bridge)
628 return -ENODEV;
629
630 port = pci_host_bridge_priv(bridge);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530631
632 port->dev = dev;
633
634 err = xilinx_pcie_parse_dt(port);
635 if (err) {
636 dev_err(dev, "Parsing DT failed\n");
637 return err;
638 }
639
640 xilinx_pcie_init_port(port);
641
642 err = xilinx_pcie_init_irq_domain(port);
643 if (err) {
644 dev_err(dev, "Failed creating IRQ Domain\n");
645 return err;
646 }
647
Jan Kiszka5bd51b32018-05-15 11:07:05 +0200648 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &res,
Jan Kiszka055f87a2018-05-15 11:07:03 +0200649 &iobase);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530650 if (err) {
Bharat Kumar Gogada02598822016-02-11 21:58:07 +0530651 dev_err(dev, "Getting bridge resources failed\n");
Srikanth Thokala8961def2014-08-20 21:56:02 +0530652 return err;
653 }
Bjorn Helgaas93a5b5e2016-05-28 18:27:03 -0500654
655 err = devm_request_pci_bus_resources(dev, &res);
656 if (err)
657 goto error;
658
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500659
660 list_splice_init(&res, &bridge->windows);
661 bridge->dev.parent = dev;
662 bridge->sysdata = port;
663 bridge->busnr = 0;
664 bridge->ops = &xilinx_pcie_ops;
Lorenzo Pieralisicc2eaae2017-06-28 15:14:06 -0500665 bridge->map_irq = of_irq_parse_and_map_pci;
666 bridge->swizzle_irq = pci_common_swizzle;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700667
668#ifdef CONFIG_PCI_MSI
Bjorn Helgaas5d071882016-10-06 13:44:42 -0500669 xilinx_pcie_msi_chip.dev = dev;
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500670 bridge->msi = &xilinx_pcie_msi_chip;
Yijing Wang8dd26dc2014-11-11 15:45:31 -0700671#endif
Lorenzo Pieralisi8c790a82017-06-28 15:13:59 -0500672 err = pci_scan_root_bus_bridge(bridge);
673 if (err < 0)
674 goto error;
675
676 bus = bridge->bus;
677
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530678 pci_assign_unassigned_bus_resources(bus);
Bjorn Helgaasec6bd782017-02-08 15:37:47 -0600679 list_for_each_entry(child, &bus->children, node)
680 pcie_bus_configure_settings(child);
Bharat Kumar Gogada4c01f3b2016-02-11 21:58:08 +0530681 pci_bus_add_devices(bus);
Srikanth Thokala8961def2014-08-20 21:56:02 +0530682 return 0;
Bjorn Helgaasc41be7a2016-05-31 11:49:14 -0500683
684error:
685 pci_free_resource_list(&res);
686 return err;
Srikanth Thokala8961def2014-08-20 21:56:02 +0530687}
688
Arvind Yadavfdc71ce2017-06-20 11:17:48 +0530689static const struct of_device_id xilinx_pcie_of_match[] = {
Srikanth Thokala8961def2014-08-20 21:56:02 +0530690 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
691 {}
692};
693
694static struct platform_driver xilinx_pcie_driver = {
695 .driver = {
696 .name = "xilinx-pcie",
Srikanth Thokala8961def2014-08-20 21:56:02 +0530697 .of_match_table = xilinx_pcie_of_match,
698 .suppress_bind_attrs = true,
699 },
700 .probe = xilinx_pcie_probe,
Srikanth Thokala8961def2014-08-20 21:56:02 +0530701};
Paul Gortmakerda4eafc2016-08-24 16:57:49 -0400702builtin_platform_driver(xilinx_pcie_driver);