• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * PCIe host controller driver for Xilinx AXI PCIe Bridge
3  *
4  * Copyright (c) 2012 - 2014 Xilinx, Inc.
5  *
6  * Based on the Tegra PCIe driver
7  *
8  * Bits taken from Synopsys DesignWare Host controller driver and
9  * ARM PCI Host generic driver.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or
14  * (at your option) any later version.
15  */
16 
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/msi.h>
23 #include <linux/of_address.h>
24 #include <linux/of_pci.h>
25 #include <linux/of_platform.h>
26 #include <linux/of_irq.h>
27 #include <linux/pci.h>
28 #include <linux/platform_device.h>
29 
30 /* Register definitions */
31 #define XILINX_PCIE_REG_BIR		0x00000130
32 #define XILINX_PCIE_REG_IDR		0x00000138
33 #define XILINX_PCIE_REG_IMR		0x0000013c
34 #define XILINX_PCIE_REG_PSCR		0x00000144
35 #define XILINX_PCIE_REG_RPSC		0x00000148
36 #define XILINX_PCIE_REG_MSIBASE1	0x0000014c
37 #define XILINX_PCIE_REG_MSIBASE2	0x00000150
38 #define XILINX_PCIE_REG_RPEFR		0x00000154
39 #define XILINX_PCIE_REG_RPIFR1		0x00000158
40 #define XILINX_PCIE_REG_RPIFR2		0x0000015c
41 
42 /* Interrupt registers definitions */
43 #define XILINX_PCIE_INTR_LINK_DOWN	BIT(0)
44 #define XILINX_PCIE_INTR_ECRC_ERR	BIT(1)
45 #define XILINX_PCIE_INTR_STR_ERR	BIT(2)
46 #define XILINX_PCIE_INTR_HOT_RESET	BIT(3)
47 #define XILINX_PCIE_INTR_CFG_TIMEOUT	BIT(8)
48 #define XILINX_PCIE_INTR_CORRECTABLE	BIT(9)
49 #define XILINX_PCIE_INTR_NONFATAL	BIT(10)
50 #define XILINX_PCIE_INTR_FATAL		BIT(11)
51 #define XILINX_PCIE_INTR_INTX		BIT(16)
52 #define XILINX_PCIE_INTR_MSI		BIT(17)
53 #define XILINX_PCIE_INTR_SLV_UNSUPP	BIT(20)
54 #define XILINX_PCIE_INTR_SLV_UNEXP	BIT(21)
55 #define XILINX_PCIE_INTR_SLV_COMPL	BIT(22)
56 #define XILINX_PCIE_INTR_SLV_ERRP	BIT(23)
57 #define XILINX_PCIE_INTR_SLV_CMPABT	BIT(24)
58 #define XILINX_PCIE_INTR_SLV_ILLBUR	BIT(25)
59 #define XILINX_PCIE_INTR_MST_DECERR	BIT(26)
60 #define XILINX_PCIE_INTR_MST_SLVERR	BIT(27)
61 #define XILINX_PCIE_INTR_MST_ERRP	BIT(28)
62 #define XILINX_PCIE_IMR_ALL_MASK	0x1FF30FED
63 #define XILINX_PCIE_IMR_ENABLE_MASK	0x1FF30F0D
64 #define XILINX_PCIE_IDR_ALL_MASK	0xFFFFFFFF
65 
66 /* Root Port Error FIFO Read Register definitions */
67 #define XILINX_PCIE_RPEFR_ERR_VALID	BIT(18)
68 #define XILINX_PCIE_RPEFR_REQ_ID	GENMASK(15, 0)
69 #define XILINX_PCIE_RPEFR_ALL_MASK	0xFFFFFFFF
70 
71 /* Root Port Interrupt FIFO Read Register 1 definitions */
72 #define XILINX_PCIE_RPIFR1_INTR_VALID	BIT(31)
73 #define XILINX_PCIE_RPIFR1_MSI_INTR	BIT(30)
74 #define XILINX_PCIE_RPIFR1_INTR_MASK	GENMASK(28, 27)
75 #define XILINX_PCIE_RPIFR1_ALL_MASK	0xFFFFFFFF
76 #define XILINX_PCIE_RPIFR1_INTR_SHIFT	27
77 
78 /* Bridge Info Register definitions */
79 #define XILINX_PCIE_BIR_ECAM_SZ_MASK	GENMASK(18, 16)
80 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT	16
81 
82 /* Root Port Interrupt FIFO Read Register 2 definitions */
83 #define XILINX_PCIE_RPIFR2_MSG_DATA	GENMASK(15, 0)
84 
85 /* Root Port Status/control Register definitions */
86 #define XILINX_PCIE_REG_RPSC_BEN	BIT(0)
87 
88 /* Phy Status/Control Register definitions */
89 #define XILINX_PCIE_REG_PSCR_LNKUP	BIT(11)
90 
91 /* ECAM definitions */
92 #define ECAM_BUS_NUM_SHIFT		20
93 #define ECAM_DEV_NUM_SHIFT		12
94 
95 /* Number of MSI IRQs */
96 #define XILINX_NUM_MSI_IRQS		128
97 
98 /**
99  * struct xilinx_pcie_port - PCIe port information
100  * @reg_base: IO Mapped Register Base
101  * @irq: Interrupt number
102  * @msi_pages: MSI pages
103  * @root_busno: Root Bus number
104  * @dev: Device pointer
105  * @msi_domain: MSI IRQ domain pointer
106  * @leg_domain: Legacy IRQ domain pointer
107  * @resources: Bus Resources
108  */
109 struct xilinx_pcie_port {
110 	void __iomem *reg_base;
111 	u32 irq;
112 	unsigned long msi_pages;
113 	u8 root_busno;
114 	struct device *dev;
115 	struct irq_domain *msi_domain;
116 	struct irq_domain *leg_domain;
117 	struct list_head resources;
118 };
119 
120 static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
121 
pcie_read(struct xilinx_pcie_port * port,u32 reg)122 static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
123 {
124 	return readl(port->reg_base + reg);
125 }
126 
pcie_write(struct xilinx_pcie_port * port,u32 val,u32 reg)127 static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
128 {
129 	writel(val, port->reg_base + reg);
130 }
131 
xilinx_pcie_link_is_up(struct xilinx_pcie_port * port)132 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
133 {
134 	return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
135 		XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
136 }
137 
138 /**
139  * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
140  * @port: PCIe port information
141  */
xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port * port)142 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
143 {
144 	struct device *dev = port->dev;
145 	unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
146 
147 	if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
148 		dev_dbg(dev, "Requester ID %lu\n",
149 			val & XILINX_PCIE_RPEFR_REQ_ID);
150 		pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
151 			   XILINX_PCIE_REG_RPEFR);
152 	}
153 }
154 
155 /**
156  * xilinx_pcie_valid_device - Check if a valid device is present on bus
157  * @bus: PCI Bus structure
158  * @devfn: device/function
159  *
160  * Return: 'true' on success and 'false' if invalid device is found
161  */
xilinx_pcie_valid_device(struct pci_bus * bus,unsigned int devfn)162 static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
163 {
164 	struct xilinx_pcie_port *port = bus->sysdata;
165 
166 	/* Check if link is up when trying to access downstream ports */
167 	if (bus->number != port->root_busno)
168 		if (!xilinx_pcie_link_is_up(port))
169 			return false;
170 
171 	/* Only one device down on each root port */
172 	if (bus->number == port->root_busno && devfn > 0)
173 		return false;
174 
175 	return true;
176 }
177 
178 /**
179  * xilinx_pcie_map_bus - Get configuration base
180  * @bus: PCI Bus structure
181  * @devfn: Device/function
182  * @where: Offset from base
183  *
184  * Return: Base address of the configuration space needed to be
185  *	   accessed.
186  */
xilinx_pcie_map_bus(struct pci_bus * bus,unsigned int devfn,int where)187 static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
188 					 unsigned int devfn, int where)
189 {
190 	struct xilinx_pcie_port *port = bus->sysdata;
191 	int relbus;
192 
193 	if (!xilinx_pcie_valid_device(bus, devfn))
194 		return NULL;
195 
196 	relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
197 		 (devfn << ECAM_DEV_NUM_SHIFT);
198 
199 	return port->reg_base + relbus + where;
200 }
201 
202 /* PCIe operations */
203 static struct pci_ops xilinx_pcie_ops = {
204 	.map_bus = xilinx_pcie_map_bus,
205 	.read	= pci_generic_config_read,
206 	.write	= pci_generic_config_write,
207 };
208 
209 /* MSI functions */
210 
211 /**
212  * xilinx_pcie_destroy_msi - Free MSI number
213  * @irq: IRQ to be freed
214  */
xilinx_pcie_destroy_msi(unsigned int irq)215 static void xilinx_pcie_destroy_msi(unsigned int irq)
216 {
217 	struct msi_desc *msi;
218 	struct xilinx_pcie_port *port;
219 	struct irq_data *d = irq_get_irq_data(irq);
220 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
221 
222 	if (!test_bit(hwirq, msi_irq_in_use)) {
223 		msi = irq_get_msi_desc(irq);
224 		port = msi_desc_to_pci_sysdata(msi);
225 		dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
226 	} else {
227 		clear_bit(hwirq, msi_irq_in_use);
228 	}
229 }
230 
231 /**
232  * xilinx_pcie_assign_msi - Allocate MSI number
233  *
234  * Return: A valid IRQ on success and error value on failure.
235  */
xilinx_pcie_assign_msi(void)236 static int xilinx_pcie_assign_msi(void)
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  */
xilinx_msi_teardown_irq(struct msi_controller * chip,unsigned int irq)254 static void xilinx_msi_teardown_irq(struct msi_controller *chip,
255 				    unsigned int irq)
256 {
257 	xilinx_pcie_destroy_msi(irq);
258 	irq_dispose_mapping(irq);
259 }
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  */
xilinx_pcie_msi_setup_irq(struct msi_controller * chip,struct pci_dev * pdev,struct msi_desc * desc)269 static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
270 				     struct pci_dev *pdev,
271 				     struct msi_desc *desc)
272 {
273 	struct xilinx_pcie_port *port = pdev->bus->sysdata;
274 	unsigned int irq;
275 	int hwirq;
276 	struct msi_msg msg;
277 	phys_addr_t msg_addr;
278 
279 	hwirq = xilinx_pcie_assign_msi();
280 	if (hwirq < 0)
281 		return hwirq;
282 
283 	irq = irq_create_mapping(port->msi_domain, hwirq);
284 	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 
295 	pci_write_msi_msg(irq, &msg);
296 
297 	return 0;
298 }
299 
300 /* MSI Chip Descriptor */
301 static struct msi_controller xilinx_pcie_msi_chip = {
302 	.setup_irq = xilinx_pcie_msi_setup_irq,
303 	.teardown_irq = xilinx_msi_teardown_irq,
304 };
305 
306 /* HW Interrupt Chip Descriptor */
307 static struct irq_chip xilinx_msi_irq_chip = {
308 	.name = "Xilinx PCIe MSI",
309 	.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,
313 };
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  */
xilinx_pcie_msi_map(struct irq_domain * domain,unsigned int irq,irq_hw_number_t hwirq)323 static 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);
328 
329 	return 0;
330 }
331 
332 /* IRQ Domain operations */
333 static 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  */
xilinx_pcie_enable_msi(struct xilinx_pcie_port * port)341 static int 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 	if (!port->msi_pages)
347 		return -ENOMEM;
348 
349 	msg_addr = virt_to_phys((void *)port->msi_pages);
350 	pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
351 	pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
352 
353 	return 0;
354 }
355 
356 /* INTx Functions */
357 
358 /**
359  * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
360  * @domain: IRQ domain
361  * @irq: Virtual IRQ number
362  * @hwirq: HW interrupt number
363  *
364  * Return: Always returns 0.
365  */
xilinx_pcie_intx_map(struct irq_domain * domain,unsigned int irq,irq_hw_number_t hwirq)366 static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
367 				irq_hw_number_t hwirq)
368 {
369 	irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
370 	irq_set_chip_data(irq, domain->host_data);
371 
372 	return 0;
373 }
374 
375 /* INTx IRQ Domain operations */
376 static const struct irq_domain_ops intx_domain_ops = {
377 	.map = xilinx_pcie_intx_map,
378 	.xlate = pci_irqd_intx_xlate,
379 };
380 
381 /* PCIe HW Functions */
382 
383 /**
384  * xilinx_pcie_intr_handler - Interrupt Service Handler
385  * @irq: IRQ number
386  * @data: PCIe port information
387  *
388  * Return: IRQ_HANDLED on success and IRQ_NONE on failure
389  */
xilinx_pcie_intr_handler(int irq,void * data)390 static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
391 {
392 	struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
393 	struct device *dev = port->dev;
394 	u32 val, mask, status;
395 
396 	/* Read interrupt decode and mask registers */
397 	val = pcie_read(port, XILINX_PCIE_REG_IDR);
398 	mask = pcie_read(port, XILINX_PCIE_REG_IMR);
399 
400 	status = val & mask;
401 	if (!status)
402 		return IRQ_NONE;
403 
404 	if (status & XILINX_PCIE_INTR_LINK_DOWN)
405 		dev_warn(dev, "Link Down\n");
406 
407 	if (status & XILINX_PCIE_INTR_ECRC_ERR)
408 		dev_warn(dev, "ECRC failed\n");
409 
410 	if (status & XILINX_PCIE_INTR_STR_ERR)
411 		dev_warn(dev, "Streaming error\n");
412 
413 	if (status & XILINX_PCIE_INTR_HOT_RESET)
414 		dev_info(dev, "Hot reset\n");
415 
416 	if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
417 		dev_warn(dev, "ECAM access timeout\n");
418 
419 	if (status & XILINX_PCIE_INTR_CORRECTABLE) {
420 		dev_warn(dev, "Correctable error message\n");
421 		xilinx_pcie_clear_err_interrupts(port);
422 	}
423 
424 	if (status & XILINX_PCIE_INTR_NONFATAL) {
425 		dev_warn(dev, "Non fatal error message\n");
426 		xilinx_pcie_clear_err_interrupts(port);
427 	}
428 
429 	if (status & XILINX_PCIE_INTR_FATAL) {
430 		dev_warn(dev, "Fatal error message\n");
431 		xilinx_pcie_clear_err_interrupts(port);
432 	}
433 
434 	if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
435 		val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
436 
437 		/* Check whether interrupt valid */
438 		if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
439 			dev_warn(dev, "RP Intr FIFO1 read error\n");
440 			goto error;
441 		}
442 
443 		/* Decode the IRQ number */
444 		if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
445 			val = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
446 				XILINX_PCIE_RPIFR2_MSG_DATA;
447 		} else {
448 			val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
449 				XILINX_PCIE_RPIFR1_INTR_SHIFT;
450 			val = irq_find_mapping(port->leg_domain, val);
451 		}
452 
453 		/* Clear interrupt FIFO register 1 */
454 		pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
455 			   XILINX_PCIE_REG_RPIFR1);
456 
457 		/* Handle the interrupt */
458 		if (IS_ENABLED(CONFIG_PCI_MSI) ||
459 		    !(val & XILINX_PCIE_RPIFR1_MSI_INTR))
460 			generic_handle_irq(val);
461 	}
462 
463 	if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
464 		dev_warn(dev, "Slave unsupported request\n");
465 
466 	if (status & XILINX_PCIE_INTR_SLV_UNEXP)
467 		dev_warn(dev, "Slave unexpected completion\n");
468 
469 	if (status & XILINX_PCIE_INTR_SLV_COMPL)
470 		dev_warn(dev, "Slave completion timeout\n");
471 
472 	if (status & XILINX_PCIE_INTR_SLV_ERRP)
473 		dev_warn(dev, "Slave Error Poison\n");
474 
475 	if (status & XILINX_PCIE_INTR_SLV_CMPABT)
476 		dev_warn(dev, "Slave Completer Abort\n");
477 
478 	if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
479 		dev_warn(dev, "Slave Illegal Burst\n");
480 
481 	if (status & XILINX_PCIE_INTR_MST_DECERR)
482 		dev_warn(dev, "Master decode error\n");
483 
484 	if (status & XILINX_PCIE_INTR_MST_SLVERR)
485 		dev_warn(dev, "Master slave error\n");
486 
487 	if (status & XILINX_PCIE_INTR_MST_ERRP)
488 		dev_warn(dev, "Master error poison\n");
489 
490 error:
491 	/* Clear the Interrupt Decode register */
492 	pcie_write(port, status, XILINX_PCIE_REG_IDR);
493 
494 	return IRQ_HANDLED;
495 }
496 
497 /**
498  * xilinx_pcie_init_irq_domain - Initialize IRQ domain
499  * @port: PCIe port information
500  *
501  * Return: '0' on success and error value on failure
502  */
xilinx_pcie_init_irq_domain(struct xilinx_pcie_port * port)503 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
504 {
505 	struct device *dev = port->dev;
506 	struct device_node *node = dev->of_node;
507 	struct device_node *pcie_intc_node;
508 	int ret;
509 
510 	/* Setup INTx */
511 	pcie_intc_node = of_get_next_child(node, NULL);
512 	if (!pcie_intc_node) {
513 		dev_err(dev, "No PCIe Intc node found\n");
514 		return -ENODEV;
515 	}
516 
517 	port->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
518 						 &intx_domain_ops,
519 						 port);
520 	of_node_put(pcie_intc_node);
521 	if (!port->leg_domain) {
522 		dev_err(dev, "Failed to get a INTx IRQ domain\n");
523 		return -ENODEV;
524 	}
525 
526 	/* Setup MSI */
527 	if (IS_ENABLED(CONFIG_PCI_MSI)) {
528 		port->msi_domain = irq_domain_add_linear(node,
529 							 XILINX_NUM_MSI_IRQS,
530 							 &msi_domain_ops,
531 							 &xilinx_pcie_msi_chip);
532 		if (!port->msi_domain) {
533 			dev_err(dev, "Failed to get a MSI IRQ domain\n");
534 			return -ENODEV;
535 		}
536 
537 		ret = xilinx_pcie_enable_msi(port);
538 		if (ret)
539 			return ret;
540 	}
541 
542 	return 0;
543 }
544 
545 /**
546  * xilinx_pcie_init_port - Initialize hardware
547  * @port: PCIe port information
548  */
xilinx_pcie_init_port(struct xilinx_pcie_port * port)549 static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
550 {
551 	struct device *dev = port->dev;
552 
553 	if (xilinx_pcie_link_is_up(port))
554 		dev_info(dev, "PCIe Link is UP\n");
555 	else
556 		dev_info(dev, "PCIe Link is DOWN\n");
557 
558 	/* Disable all interrupts */
559 	pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
560 		   XILINX_PCIE_REG_IMR);
561 
562 	/* Clear pending interrupts */
563 	pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
564 			 XILINX_PCIE_IMR_ALL_MASK,
565 		   XILINX_PCIE_REG_IDR);
566 
567 	/* Enable all interrupts we handle */
568 	pcie_write(port, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
569 
570 	/* Enable the Bridge enable bit */
571 	pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
572 			 XILINX_PCIE_REG_RPSC_BEN,
573 		   XILINX_PCIE_REG_RPSC);
574 }
575 
576 /**
577  * xilinx_pcie_parse_dt - Parse Device tree
578  * @port: PCIe port information
579  *
580  * Return: '0' on success and error value on failure
581  */
xilinx_pcie_parse_dt(struct xilinx_pcie_port * port)582 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
583 {
584 	struct device *dev = port->dev;
585 	struct device_node *node = dev->of_node;
586 	struct resource regs;
587 	const char *type;
588 	int err;
589 
590 	type = of_get_property(node, "device_type", NULL);
591 	if (!type || strcmp(type, "pci")) {
592 		dev_err(dev, "invalid \"device_type\" %s\n", type);
593 		return -EINVAL;
594 	}
595 
596 	err = of_address_to_resource(node, 0, &regs);
597 	if (err) {
598 		dev_err(dev, "missing \"reg\" property\n");
599 		return err;
600 	}
601 
602 	port->reg_base = devm_pci_remap_cfg_resource(dev, &regs);
603 	if (IS_ERR(port->reg_base))
604 		return PTR_ERR(port->reg_base);
605 
606 	port->irq = irq_of_parse_and_map(node, 0);
607 	err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
608 			       IRQF_SHARED | IRQF_NO_THREAD,
609 			       "xilinx-pcie", port);
610 	if (err) {
611 		dev_err(dev, "unable to request irq %d\n", port->irq);
612 		return err;
613 	}
614 
615 	return 0;
616 }
617 
618 /**
619  * xilinx_pcie_probe - Probe function
620  * @pdev: Platform device pointer
621  *
622  * Return: '0' on success and error value on failure
623  */
xilinx_pcie_probe(struct platform_device * pdev)624 static int xilinx_pcie_probe(struct platform_device *pdev)
625 {
626 	struct device *dev = &pdev->dev;
627 	struct xilinx_pcie_port *port;
628 	struct pci_bus *bus, *child;
629 	struct pci_host_bridge *bridge;
630 	int err;
631 	resource_size_t iobase = 0;
632 	LIST_HEAD(res);
633 
634 	if (!dev->of_node)
635 		return -ENODEV;
636 
637 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
638 	if (!bridge)
639 		return -ENODEV;
640 
641 	port = pci_host_bridge_priv(bridge);
642 
643 	port->dev = dev;
644 
645 	err = xilinx_pcie_parse_dt(port);
646 	if (err) {
647 		dev_err(dev, "Parsing DT failed\n");
648 		return err;
649 	}
650 
651 	xilinx_pcie_init_port(port);
652 
653 	err = xilinx_pcie_init_irq_domain(port);
654 	if (err) {
655 		dev_err(dev, "Failed creating IRQ Domain\n");
656 		return err;
657 	}
658 
659 	err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
660 					       &iobase);
661 	if (err) {
662 		dev_err(dev, "Getting bridge resources failed\n");
663 		return err;
664 	}
665 
666 	err = devm_request_pci_bus_resources(dev, &res);
667 	if (err)
668 		goto error;
669 
670 
671 	list_splice_init(&res, &bridge->windows);
672 	bridge->dev.parent = dev;
673 	bridge->sysdata = port;
674 	bridge->busnr = 0;
675 	bridge->ops = &xilinx_pcie_ops;
676 	bridge->map_irq = of_irq_parse_and_map_pci;
677 	bridge->swizzle_irq = pci_common_swizzle;
678 
679 #ifdef CONFIG_PCI_MSI
680 	xilinx_pcie_msi_chip.dev = dev;
681 	bridge->msi = &xilinx_pcie_msi_chip;
682 #endif
683 	err = pci_scan_root_bus_bridge(bridge);
684 	if (err < 0)
685 		goto error;
686 
687 	bus = bridge->bus;
688 
689 	pci_assign_unassigned_bus_resources(bus);
690 	list_for_each_entry(child, &bus->children, node)
691 		pcie_bus_configure_settings(child);
692 	pci_bus_add_devices(bus);
693 	return 0;
694 
695 error:
696 	pci_free_resource_list(&res);
697 	return err;
698 }
699 
700 static const struct of_device_id xilinx_pcie_of_match[] = {
701 	{ .compatible = "xlnx,axi-pcie-host-1.00.a", },
702 	{}
703 };
704 
705 static struct platform_driver xilinx_pcie_driver = {
706 	.driver = {
707 		.name = "xilinx-pcie",
708 		.of_match_table = xilinx_pcie_of_match,
709 		.suppress_bind_attrs = true,
710 	},
711 	.probe = xilinx_pcie_probe,
712 };
713 builtin_platform_driver(xilinx_pcie_driver);
714