pcie-xilinx.c 17.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8
/*
 * PCIe host controller driver for Xilinx AXI PCIe Bridge
 *
 * Copyright (c) 2012 - 2014 Xilinx, Inc.
 *
 * Based on the Tegra PCIe driver
 *
9
 * Bits taken from Synopsys DesignWare Host controller driver and
10 11 12 13 14 15 16
 * ARM PCI Host generic driver.
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/kernel.h>
17
#include <linux/init.h>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <linux/msi.h>
#include <linux/of_address.h>
#include <linux/of_pci.h>
#include <linux/of_platform.h>
#include <linux/of_irq.h>
#include <linux/pci.h>
#include <linux/platform_device.h>

/* Register definitions */
#define XILINX_PCIE_REG_BIR		0x00000130
#define XILINX_PCIE_REG_IDR		0x00000138
#define XILINX_PCIE_REG_IMR		0x0000013c
#define XILINX_PCIE_REG_PSCR		0x00000144
#define XILINX_PCIE_REG_RPSC		0x00000148
#define XILINX_PCIE_REG_MSIBASE1	0x0000014c
#define XILINX_PCIE_REG_MSIBASE2	0x00000150
#define XILINX_PCIE_REG_RPEFR		0x00000154
#define XILINX_PCIE_REG_RPIFR1		0x00000158
#define XILINX_PCIE_REG_RPIFR2		0x0000015c

/* Interrupt registers definitions */
#define XILINX_PCIE_INTR_LINK_DOWN	BIT(0)
#define XILINX_PCIE_INTR_ECRC_ERR	BIT(1)
#define XILINX_PCIE_INTR_STR_ERR	BIT(2)
#define XILINX_PCIE_INTR_HOT_RESET	BIT(3)
#define XILINX_PCIE_INTR_CFG_TIMEOUT	BIT(8)
#define XILINX_PCIE_INTR_CORRECTABLE	BIT(9)
#define XILINX_PCIE_INTR_NONFATAL	BIT(10)
#define XILINX_PCIE_INTR_FATAL		BIT(11)
#define XILINX_PCIE_INTR_INTX		BIT(16)
#define XILINX_PCIE_INTR_MSI		BIT(17)
#define XILINX_PCIE_INTR_SLV_UNSUPP	BIT(20)
#define XILINX_PCIE_INTR_SLV_UNEXP	BIT(21)
#define XILINX_PCIE_INTR_SLV_COMPL	BIT(22)
#define XILINX_PCIE_INTR_SLV_ERRP	BIT(23)
#define XILINX_PCIE_INTR_SLV_CMPABT	BIT(24)
#define XILINX_PCIE_INTR_SLV_ILLBUR	BIT(25)
#define XILINX_PCIE_INTR_MST_DECERR	BIT(26)
#define XILINX_PCIE_INTR_MST_SLVERR	BIT(27)
#define XILINX_PCIE_INTR_MST_ERRP	BIT(28)
#define XILINX_PCIE_IMR_ALL_MASK	0x1FF30FED
59
#define XILINX_PCIE_IMR_ENABLE_MASK	0x1FF30F0D
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define XILINX_PCIE_IDR_ALL_MASK	0xFFFFFFFF

/* Root Port Error FIFO Read Register definitions */
#define XILINX_PCIE_RPEFR_ERR_VALID	BIT(18)
#define XILINX_PCIE_RPEFR_REQ_ID	GENMASK(15, 0)
#define XILINX_PCIE_RPEFR_ALL_MASK	0xFFFFFFFF

/* Root Port Interrupt FIFO Read Register 1 definitions */
#define XILINX_PCIE_RPIFR1_INTR_VALID	BIT(31)
#define XILINX_PCIE_RPIFR1_MSI_INTR	BIT(30)
#define XILINX_PCIE_RPIFR1_INTR_MASK	GENMASK(28, 27)
#define XILINX_PCIE_RPIFR1_ALL_MASK	0xFFFFFFFF
#define XILINX_PCIE_RPIFR1_INTR_SHIFT	27

/* Bridge Info Register definitions */
#define XILINX_PCIE_BIR_ECAM_SZ_MASK	GENMASK(18, 16)
#define XILINX_PCIE_BIR_ECAM_SZ_SHIFT	16

/* Root Port Interrupt FIFO Read Register 2 definitions */
#define XILINX_PCIE_RPIFR2_MSG_DATA	GENMASK(15, 0)

/* Root Port Status/control Register definitions */
#define XILINX_PCIE_REG_RPSC_BEN	BIT(0)

/* Phy Status/Control Register definitions */
#define XILINX_PCIE_REG_PSCR_LNKUP	BIT(11)

/* ECAM definitions */
#define ECAM_BUS_NUM_SHIFT		20
#define ECAM_DEV_NUM_SHIFT		12

/* Number of MSI IRQs */
#define XILINX_NUM_MSI_IRQS		128

/**
 * struct xilinx_pcie_port - PCIe port information
 * @reg_base: IO Mapped Register Base
 * @irq: Interrupt number
 * @msi_pages: MSI pages
 * @root_busno: Root Bus number
 * @dev: Device pointer
101 102
 * @msi_domain: MSI IRQ domain pointer
 * @leg_domain: Legacy IRQ domain pointer
103 104 105 106 107 108 109 110
 * @resources: Bus Resources
 */
struct xilinx_pcie_port {
	void __iomem *reg_base;
	u32 irq;
	unsigned long msi_pages;
	u8 root_busno;
	struct device *dev;
111 112
	struct irq_domain *msi_domain;
	struct irq_domain *leg_domain;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	struct list_head resources;
};

static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);

static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
{
	return readl(port->reg_base + reg);
}

static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
{
	writel(val, port->reg_base + reg);
}

128
static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port *port)
129 130 131 132 133 134 135 136 137 138 139
{
	return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
		XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
}

/**
 * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
 * @port: PCIe port information
 */
static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
{
140
	struct device *dev = port->dev;
141
	unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
142 143

	if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
144
		dev_dbg(dev, "Requester ID %lu\n",
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
			val & XILINX_PCIE_RPEFR_REQ_ID);
		pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
			   XILINX_PCIE_REG_RPEFR);
	}
}

/**
 * xilinx_pcie_valid_device - Check if a valid device is present on bus
 * @bus: PCI Bus structure
 * @devfn: device/function
 *
 * Return: 'true' on success and 'false' if invalid device is found
 */
static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
{
160
	struct xilinx_pcie_port *port = bus->sysdata;
161 162 163

	/* Check if link is up when trying to access downstream ports */
	if (bus->number != port->root_busno)
164
		if (!xilinx_pcie_link_up(port))
165 166 167 168 169 170 171 172 173 174
			return false;

	/* Only one device down on each root port */
	if (bus->number == port->root_busno && devfn > 0)
		return false;

	return true;
}

/**
175
 * xilinx_pcie_map_bus - Get configuration base
176 177 178 179 180 181 182
 * @bus: PCI Bus structure
 * @devfn: Device/function
 * @where: Offset from base
 *
 * Return: Base address of the configuration space needed to be
 *	   accessed.
 */
183 184
static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
					 unsigned int devfn, int where)
185
{
186
	struct xilinx_pcie_port *port = bus->sysdata;
187 188
	int relbus;

189 190 191
	if (!xilinx_pcie_valid_device(bus, devfn))
		return NULL;

192 193 194 195 196 197 198 199
	relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
		 (devfn << ECAM_DEV_NUM_SHIFT);

	return port->reg_base + relbus + where;
}

/* PCIe operations */
static struct pci_ops xilinx_pcie_ops = {
200 201 202
	.map_bus = xilinx_pcie_map_bus,
	.read	= pci_generic_config_read,
	.write	= pci_generic_config_write,
203 204 205 206 207 208 209 210 211 212 213 214
};

/* MSI functions */

/**
 * xilinx_pcie_destroy_msi - Free MSI number
 * @irq: IRQ to be freed
 */
static void xilinx_pcie_destroy_msi(unsigned int irq)
{
	struct msi_desc *msi;
	struct xilinx_pcie_port *port;
215 216
	struct irq_data *d = irq_get_irq_data(irq);
	irq_hw_number_t hwirq = irqd_to_hwirq(d);
217

218
	if (!test_bit(hwirq, msi_irq_in_use)) {
219
		msi = irq_get_msi_desc(irq);
220
		port = msi_desc_to_pci_sysdata(msi);
221
		dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
222
	} else {
223
		clear_bit(hwirq, msi_irq_in_use);
224
	}
225 226 227 228 229 230 231
}

/**
 * xilinx_pcie_assign_msi - Allocate MSI number
 *
 * Return: A valid IRQ on success and error value on failure.
 */
232
static int xilinx_pcie_assign_msi(void)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
{
	int pos;

	pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
	if (pos < XILINX_NUM_MSI_IRQS)
		set_bit(pos, msi_irq_in_use);
	else
		return -ENOSPC;

	return pos;
}

/**
 * xilinx_msi_teardown_irq - Destroy the MSI
 * @chip: MSI Chip descriptor
 * @irq: MSI IRQ to destroy
 */
250 251
static void xilinx_msi_teardown_irq(struct msi_controller *chip,
				    unsigned int irq)
252 253
{
	xilinx_pcie_destroy_msi(irq);
254
	irq_dispose_mapping(irq);
255 256 257 258 259 260 261 262 263 264
}

/**
 * xilinx_pcie_msi_setup_irq - Setup MSI request
 * @chip: MSI chip pointer
 * @pdev: PCIe device pointer
 * @desc: MSI descriptor pointer
 *
 * Return: '0' on success and error value on failure
 */
265
static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
266 267 268
				     struct pci_dev *pdev,
				     struct msi_desc *desc)
{
269
	struct xilinx_pcie_port *port = pdev->bus->sysdata;
270 271 272 273 274
	unsigned int irq;
	int hwirq;
	struct msi_msg msg;
	phys_addr_t msg_addr;

275
	hwirq = xilinx_pcie_assign_msi();
276 277
	if (hwirq < 0)
		return hwirq;
278

279
	irq = irq_create_mapping(port->msi_domain, hwirq);
280 281 282 283 284 285 286 287 288 289 290
	if (!irq)
		return -EINVAL;

	irq_set_msi_desc(irq, desc);

	msg_addr = virt_to_phys((void *)port->msi_pages);

	msg.address_hi = 0;
	msg.address_lo = msg_addr;
	msg.data = irq;

291
	pci_write_msi_msg(irq, &msg);
292 293 294 295 296

	return 0;
}

/* MSI Chip Descriptor */
297
static struct msi_controller xilinx_pcie_msi_chip = {
298 299 300 301 302 303 304
	.setup_irq = xilinx_pcie_msi_setup_irq,
	.teardown_irq = xilinx_msi_teardown_irq,
};

/* HW Interrupt Chip Descriptor */
static struct irq_chip xilinx_msi_irq_chip = {
	.name = "Xilinx PCIe MSI",
305 306 307 308
	.irq_enable = pci_msi_unmask_irq,
	.irq_disable = pci_msi_mask_irq,
	.irq_mask = pci_msi_mask_irq,
	.irq_unmask = pci_msi_unmask_irq,
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
};

/**
 * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
 * @domain: IRQ domain
 * @irq: Virtual IRQ number
 * @hwirq: HW interrupt number
 *
 * Return: Always returns 0.
 */
static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
			       irq_hw_number_t hwirq)
{
	irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
	irq_set_chip_data(irq, domain->host_data);

	return 0;
}

/* IRQ Domain operations */
static const struct irq_domain_ops msi_domain_ops = {
	.map = xilinx_pcie_msi_map,
};

/**
 * xilinx_pcie_enable_msi - Enable MSI support
 * @port: PCIe port information
 */
static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
{
	phys_addr_t msg_addr;

	port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
	msg_addr = virt_to_phys((void *)port->msi_pages);
	pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
	pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
}

/* INTx Functions */

/**
 * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
 * @domain: IRQ domain
 * @irq: Virtual IRQ number
 * @hwirq: HW interrupt number
 *
 * Return: Always returns 0.
 */
static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
				irq_hw_number_t hwirq)
{
	irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
	irq_set_chip_data(irq, domain->host_data);

	return 0;
}

/* INTx IRQ Domain operations */
static const struct irq_domain_ops intx_domain_ops = {
	.map = xilinx_pcie_intx_map,
369
	.xlate = pci_irqd_intx_xlate,
370 371 372 373 374 375 376 377 378 379 380 381 382 383
};

/* PCIe HW Functions */

/**
 * xilinx_pcie_intr_handler - Interrupt Service Handler
 * @irq: IRQ number
 * @data: PCIe port information
 *
 * Return: IRQ_HANDLED on success and IRQ_NONE on failure
 */
static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
{
	struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
384
	struct device *dev = port->dev;
385
	u32 val, mask, status;
386 387 388 389 390 391 392 393 394 395

	/* Read interrupt decode and mask registers */
	val = pcie_read(port, XILINX_PCIE_REG_IDR);
	mask = pcie_read(port, XILINX_PCIE_REG_IMR);

	status = val & mask;
	if (!status)
		return IRQ_NONE;

	if (status & XILINX_PCIE_INTR_LINK_DOWN)
396
		dev_warn(dev, "Link Down\n");
397 398

	if (status & XILINX_PCIE_INTR_ECRC_ERR)
399
		dev_warn(dev, "ECRC failed\n");
400 401

	if (status & XILINX_PCIE_INTR_STR_ERR)
402
		dev_warn(dev, "Streaming error\n");
403 404

	if (status & XILINX_PCIE_INTR_HOT_RESET)
405
		dev_info(dev, "Hot reset\n");
406 407

	if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
408
		dev_warn(dev, "ECAM access timeout\n");
409 410

	if (status & XILINX_PCIE_INTR_CORRECTABLE) {
411
		dev_warn(dev, "Correctable error message\n");
412 413 414 415
		xilinx_pcie_clear_err_interrupts(port);
	}

	if (status & XILINX_PCIE_INTR_NONFATAL) {
416
		dev_warn(dev, "Non fatal error message\n");
417 418 419 420
		xilinx_pcie_clear_err_interrupts(port);
	}

	if (status & XILINX_PCIE_INTR_FATAL) {
421
		dev_warn(dev, "Fatal error message\n");
422 423 424
		xilinx_pcie_clear_err_interrupts(port);
	}

425
	if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
426 427 428 429
		val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);

		/* Check whether interrupt valid */
		if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
430
			dev_warn(dev, "RP Intr FIFO1 read error\n");
431
			goto error;
432 433
		}

434 435 436 437 438
		/* Decode the IRQ number */
		if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
			val = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
				XILINX_PCIE_RPIFR2_MSG_DATA;
		} else {
439 440
			val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
				XILINX_PCIE_RPIFR1_INTR_SHIFT;
441
			val = irq_find_mapping(port->leg_domain, val);
442
		}
443

444 445 446
		/* Clear interrupt FIFO register 1 */
		pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
			   XILINX_PCIE_REG_RPIFR1);
447

448 449 450 451
		/* Handle the interrupt */
		if (IS_ENABLED(CONFIG_PCI_MSI) ||
		    !(val & XILINX_PCIE_RPIFR1_MSI_INTR))
			generic_handle_irq(val);
452 453 454
	}

	if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
455
		dev_warn(dev, "Slave unsupported request\n");
456 457

	if (status & XILINX_PCIE_INTR_SLV_UNEXP)
458
		dev_warn(dev, "Slave unexpected completion\n");
459 460

	if (status & XILINX_PCIE_INTR_SLV_COMPL)
461
		dev_warn(dev, "Slave completion timeout\n");
462 463

	if (status & XILINX_PCIE_INTR_SLV_ERRP)
464
		dev_warn(dev, "Slave Error Poison\n");
465 466

	if (status & XILINX_PCIE_INTR_SLV_CMPABT)
467
		dev_warn(dev, "Slave Completer Abort\n");
468 469

	if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
470
		dev_warn(dev, "Slave Illegal Burst\n");
471 472

	if (status & XILINX_PCIE_INTR_MST_DECERR)
473
		dev_warn(dev, "Master decode error\n");
474 475

	if (status & XILINX_PCIE_INTR_MST_SLVERR)
476
		dev_warn(dev, "Master slave error\n");
477 478

	if (status & XILINX_PCIE_INTR_MST_ERRP)
479
		dev_warn(dev, "Master error poison\n");
480

481
error:
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	/* Clear the Interrupt Decode register */
	pcie_write(port, status, XILINX_PCIE_REG_IDR);

	return IRQ_HANDLED;
}

/**
 * xilinx_pcie_init_irq_domain - Initialize IRQ domain
 * @port: PCIe port information
 *
 * Return: '0' on success and error value on failure
 */
static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
{
	struct device *dev = port->dev;
	struct device_node *node = dev->of_node;
	struct device_node *pcie_intc_node;

	/* Setup INTx */
	pcie_intc_node = of_get_next_child(node, NULL);
	if (!pcie_intc_node) {
		dev_err(dev, "No PCIe Intc node found\n");
504
		return -ENODEV;
505 506
	}

507
	port->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
508 509
						 &intx_domain_ops,
						 port);
510
	if (!port->leg_domain) {
511
		dev_err(dev, "Failed to get a INTx IRQ domain\n");
512
		return -ENODEV;
513 514 515 516
	}

	/* Setup MSI */
	if (IS_ENABLED(CONFIG_PCI_MSI)) {
517
		port->msi_domain = irq_domain_add_linear(node,
518 519 520
							 XILINX_NUM_MSI_IRQS,
							 &msi_domain_ops,
							 &xilinx_pcie_msi_chip);
521
		if (!port->msi_domain) {
522
			dev_err(dev, "Failed to get a MSI IRQ domain\n");
523
			return -ENODEV;
524 525 526 527 528 529 530 531 532 533 534 535 536 537
		}

		xilinx_pcie_enable_msi(port);
	}

	return 0;
}

/**
 * xilinx_pcie_init_port - Initialize hardware
 * @port: PCIe port information
 */
static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
{
538 539
	struct device *dev = port->dev;

540
	if (xilinx_pcie_link_up(port))
541
		dev_info(dev, "PCIe Link is UP\n");
542
	else
543
		dev_info(dev, "PCIe Link is DOWN\n");
544 545 546 547 548 549 550 551 552 553

	/* Disable all interrupts */
	pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
		   XILINX_PCIE_REG_IMR);

	/* Clear pending interrupts */
	pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
			 XILINX_PCIE_IMR_ALL_MASK,
		   XILINX_PCIE_REG_IDR);

554 555
	/* Enable all interrupts we handle */
	pcie_write(port, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

	/* Enable the Bridge enable bit */
	pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
			 XILINX_PCIE_REG_RPSC_BEN,
		   XILINX_PCIE_REG_RPSC);
}

/**
 * xilinx_pcie_parse_dt - Parse Device tree
 * @port: PCIe port information
 *
 * Return: '0' on success and error value on failure
 */
static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
{
	struct device *dev = port->dev;
	struct device_node *node = dev->of_node;
	struct resource regs;
	const char *type;
	int err;

	type = of_get_property(node, "device_type", NULL);
	if (!type || strcmp(type, "pci")) {
		dev_err(dev, "invalid \"device_type\" %s\n", type);
		return -EINVAL;
	}

	err = of_address_to_resource(node, 0, &regs);
	if (err) {
		dev_err(dev, "missing \"reg\" property\n");
		return err;
	}

589
	port->reg_base = devm_pci_remap_cfg_resource(dev, &regs);
590 591 592 593 594
	if (IS_ERR(port->reg_base))
		return PTR_ERR(port->reg_base);

	port->irq = irq_of_parse_and_map(node, 0);
	err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
595 596
			       IRQF_SHARED | IRQF_NO_THREAD,
			       "xilinx-pcie", port);
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	if (err) {
		dev_err(dev, "unable to request irq %d\n", port->irq);
		return err;
	}

	return 0;
}

/**
 * xilinx_pcie_probe - Probe function
 * @pdev: Platform device pointer
 *
 * Return: '0' on success and error value on failure
 */
static int xilinx_pcie_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
614
	struct xilinx_pcie_port *port;
615
	struct pci_bus *bus, *child;
616
	struct pci_host_bridge *bridge;
617
	int err;
618 619
	resource_size_t iobase = 0;
	LIST_HEAD(res);
620 621 622 623

	if (!dev->of_node)
		return -ENODEV;

624 625 626 627 628
	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
	if (!bridge)
		return -ENODEV;

	port = pci_host_bridge_priv(bridge);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

	port->dev = dev;

	err = xilinx_pcie_parse_dt(port);
	if (err) {
		dev_err(dev, "Parsing DT failed\n");
		return err;
	}

	xilinx_pcie_init_port(port);

	err = xilinx_pcie_init_irq_domain(port);
	if (err) {
		dev_err(dev, "Failed creating IRQ Domain\n");
		return err;
	}

646
	err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &res,
647
						    &iobase);
648
	if (err) {
649
		dev_err(dev, "Getting bridge resources failed\n");
650 651
		return err;
	}
652 653 654 655 656

	err = devm_request_pci_bus_resources(dev, &res);
	if (err)
		goto error;

657 658 659 660 661 662

	list_splice_init(&res, &bridge->windows);
	bridge->dev.parent = dev;
	bridge->sysdata = port;
	bridge->busnr = 0;
	bridge->ops = &xilinx_pcie_ops;
663 664
	bridge->map_irq = of_irq_parse_and_map_pci;
	bridge->swizzle_irq = pci_common_swizzle;
665 666

#ifdef CONFIG_PCI_MSI
667
	xilinx_pcie_msi_chip.dev = dev;
668
	bridge->msi = &xilinx_pcie_msi_chip;
669
#endif
670 671 672 673 674 675
	err = pci_scan_root_bus_bridge(bridge);
	if (err < 0)
		goto error;

	bus = bridge->bus;

676
	pci_assign_unassigned_bus_resources(bus);
677 678
	list_for_each_entry(child, &bus->children, node)
		pcie_bus_configure_settings(child);
679
	pci_bus_add_devices(bus);
680
	return 0;
681 682 683 684

error:
	pci_free_resource_list(&res);
	return err;
685 686
}

687
static const struct of_device_id xilinx_pcie_of_match[] = {
688 689 690 691 692 693 694 695 696 697 698 699
	{ .compatible = "xlnx,axi-pcie-host-1.00.a", },
	{}
};

static struct platform_driver xilinx_pcie_driver = {
	.driver = {
		.name = "xilinx-pcie",
		.of_match_table = xilinx_pcie_of_match,
		.suppress_bind_attrs = true,
	},
	.probe = xilinx_pcie_probe,
};
700
builtin_platform_driver(xilinx_pcie_driver);