xref: /linux/drivers/pci/controller/pcie-xilinx-cpm.c (revision b428bc9a1532e6f2616242ce8425fa271331726b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCIe host controller driver for Xilinx Versal CPM DMA Bridge
4  *
5  * (C) Copyright 2019 - 2020, Xilinx, Inc.
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/irqchip.h>
14 #include <linux/irqchip/chained_irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/of_pci.h>
20 #include <linux/of_platform.h>
21 #include <linux/reset.h>
22 
23 #include "../pci.h"
24 #include "pcie-xilinx-common.h"
25 
26 /* Register definitions */
27 #define XILINX_CPM_PCIE_REG_IDR		0x00000E10
28 #define XILINX_CPM_PCIE_REG_IMR		0x00000E14
29 #define XILINX_CPM_PCIE_REG_PSCR	0x00000E1C
30 #define XILINX_CPM_PCIE_REG_RPSC	0x00000E20
31 #define XILINX_CPM_PCIE_REG_RPEFR	0x00000E2C
32 #define XILINX_CPM_PCIE_REG_IDRN	0x00000E38
33 #define XILINX_CPM_PCIE_REG_IDRN_MASK	0x00000E3C
34 #define XILINX_CPM_PCIE_MISC_IR_STATUS	0x00000340
35 #define XILINX_CPM_PCIE_MISC_IR_ENABLE	0x00000348
36 #define XILINX_CPM_PCIE0_MISC_IR_LOCAL	BIT(1)
37 #define XILINX_CPM_PCIE1_MISC_IR_LOCAL	BIT(2)
38 
39 #define XILINX_CPM_PCIE0_IR_STATUS	0x000002A0
40 #define XILINX_CPM_PCIE1_IR_STATUS	0x000002B4
41 #define XILINX_CPM_PCIE0_IR_ENABLE	0x000002A8
42 #define XILINX_CPM_PCIE1_IR_ENABLE	0x000002BC
43 #define XILINX_CPM_PCIE_IR_LOCAL	BIT(0)
44 
45 #define IMR(x) BIT(XILINX_PCIE_INTR_ ##x)
46 
47 #define XILINX_CPM_PCIE_IMR_ALL_MASK			\
48 	(						\
49 		IMR(LINK_DOWN)		|		\
50 		IMR(HOT_RESET)		|		\
51 		IMR(CFG_PCIE_TIMEOUT)	|		\
52 		IMR(CFG_TIMEOUT)	|		\
53 		IMR(CORRECTABLE)	|		\
54 		IMR(NONFATAL)		|		\
55 		IMR(FATAL)		|		\
56 		IMR(CFG_ERR_POISON)	|		\
57 		IMR(PME_TO_ACK_RCVD)	|		\
58 		IMR(INTX)		|		\
59 		IMR(PM_PME_RCVD)	|		\
60 		IMR(SLV_UNSUPP)		|		\
61 		IMR(SLV_UNEXP)		|		\
62 		IMR(SLV_COMPL)		|		\
63 		IMR(SLV_ERRP)		|		\
64 		IMR(SLV_CMPABT)		|		\
65 		IMR(SLV_ILLBUR)		|		\
66 		IMR(MST_DECERR)		|		\
67 		IMR(MST_SLVERR)		|		\
68 		IMR(SLV_PCIE_TIMEOUT)			\
69 	)
70 
71 #define XILINX_CPM_PCIE_IDR_ALL_MASK		0xFFFFFFFF
72 #define XILINX_CPM_PCIE_IDRN_MASK		GENMASK(19, 16)
73 #define XILINX_CPM_PCIE_IDRN_SHIFT		16
74 
75 /* Root Port Error FIFO Read Register definitions */
76 #define XILINX_CPM_PCIE_RPEFR_ERR_VALID		BIT(18)
77 #define XILINX_CPM_PCIE_RPEFR_REQ_ID		GENMASK(15, 0)
78 #define XILINX_CPM_PCIE_RPEFR_ALL_MASK		0xFFFFFFFF
79 
80 /* Root Port Status/control Register definitions */
81 #define XILINX_CPM_PCIE_REG_RPSC_BEN		BIT(0)
82 
83 /* Phy Status/Control Register definitions */
84 #define XILINX_CPM_PCIE_REG_PSCR_LNKUP		BIT(11)
85 
86 enum xilinx_cpm_version {
87 	CPM,
88 	CPM5,
89 	CPM5_HOST1,
90 	CPM5NC_HOST,
91 };
92 
93 /**
94  * struct xilinx_cpm_variant - CPM variant information
95  * @version: CPM version
96  * @ir_status: Offset for the error interrupt status register
97  * @ir_enable: Offset for the CPM5 local error interrupt enable register
98  * @ir_misc_value: A bitmask for the miscellaneous interrupt status
99  */
100 struct xilinx_cpm_variant {
101 	enum xilinx_cpm_version version;
102 	u32 ir_status;
103 	u32 ir_enable;
104 	u32 ir_misc_value;
105 };
106 
107 /**
108  * struct xilinx_cpm_pcie - PCIe port information
109  * @dev: Device pointer
110  * @reg_base: Bridge Register Base
111  * @cpm_base: CPM System Level Control and Status Register(SLCR) Base
112  * @intx_domain: Legacy IRQ domain pointer
113  * @cpm_domain: CPM IRQ domain pointer
114  * @cfg: Holds mappings of config space window
115  * @intx_irq: legacy interrupt number
116  * @irq: Error interrupt number
117  * @lock: lock protecting shared register access
118  * @variant: CPM version check pointer
119  * @perst_gpio: GPIO descriptor for PERST# signal handling
120  * @rstc: Pointer to the PCIe controller reset
121  */
122 struct xilinx_cpm_pcie {
123 	struct device			*dev;
124 	void __iomem			*reg_base;
125 	void __iomem			*cpm_base;
126 	struct irq_domain		*intx_domain;
127 	struct irq_domain		*cpm_domain;
128 	struct pci_config_window	*cfg;
129 	int				intx_irq;
130 	int				irq;
131 	raw_spinlock_t			lock;
132 	const struct xilinx_cpm_variant   *variant;
133 	struct gpio_desc		*perst_gpio;
134 	struct reset_control		*rstc;
135 };
136 
137 static u32 pcie_read(struct xilinx_cpm_pcie *port, u32 reg)
138 {
139 	return readl_relaxed(port->reg_base + reg);
140 }
141 
142 static void pcie_write(struct xilinx_cpm_pcie *port,
143 		       u32 val, u32 reg)
144 {
145 	writel_relaxed(val, port->reg_base + reg);
146 }
147 
148 static bool cpm_pcie_link_up(struct xilinx_cpm_pcie *port)
149 {
150 	return (pcie_read(port, XILINX_CPM_PCIE_REG_PSCR) &
151 		XILINX_CPM_PCIE_REG_PSCR_LNKUP);
152 }
153 
154 static void cpm_pcie_clear_err_interrupts(struct xilinx_cpm_pcie *port)
155 {
156 	unsigned long val = pcie_read(port, XILINX_CPM_PCIE_REG_RPEFR);
157 
158 	if (val & XILINX_CPM_PCIE_RPEFR_ERR_VALID) {
159 		dev_dbg(port->dev, "Requester ID %lu\n",
160 			val & XILINX_CPM_PCIE_RPEFR_REQ_ID);
161 		pcie_write(port, XILINX_CPM_PCIE_RPEFR_ALL_MASK,
162 			   XILINX_CPM_PCIE_REG_RPEFR);
163 	}
164 }
165 
166 static void xilinx_cpm_mask_leg_irq(struct irq_data *data)
167 {
168 	struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
169 	unsigned long flags;
170 	u32 mask;
171 	u32 val;
172 
173 	mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
174 	raw_spin_lock_irqsave(&port->lock, flags);
175 	val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
176 	pcie_write(port, (val & (~mask)), XILINX_CPM_PCIE_REG_IDRN_MASK);
177 	raw_spin_unlock_irqrestore(&port->lock, flags);
178 }
179 
180 static void xilinx_cpm_unmask_leg_irq(struct irq_data *data)
181 {
182 	struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
183 	unsigned long flags;
184 	u32 mask;
185 	u32 val;
186 
187 	mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
188 	raw_spin_lock_irqsave(&port->lock, flags);
189 	val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
190 	pcie_write(port, (val | mask), XILINX_CPM_PCIE_REG_IDRN_MASK);
191 	raw_spin_unlock_irqrestore(&port->lock, flags);
192 }
193 
194 static struct irq_chip xilinx_cpm_leg_irq_chip = {
195 	.name		= "INTx",
196 	.irq_mask	= xilinx_cpm_mask_leg_irq,
197 	.irq_unmask	= xilinx_cpm_unmask_leg_irq,
198 };
199 
200 /**
201  * xilinx_cpm_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
202  * @domain: IRQ domain
203  * @irq: Virtual IRQ number
204  * @hwirq: HW interrupt number
205  *
206  * Return: Always returns 0.
207  */
208 static int xilinx_cpm_pcie_intx_map(struct irq_domain *domain,
209 				    unsigned int irq, irq_hw_number_t hwirq)
210 {
211 	irq_set_chip_and_handler(irq, &xilinx_cpm_leg_irq_chip,
212 				 handle_level_irq);
213 	irq_set_chip_data(irq, domain->host_data);
214 	irq_set_status_flags(irq, IRQ_LEVEL);
215 
216 	return 0;
217 }
218 
219 /* INTx IRQ Domain operations */
220 static const struct irq_domain_ops intx_domain_ops = {
221 	.map = xilinx_cpm_pcie_intx_map,
222 };
223 
224 static void xilinx_cpm_pcie_intx_flow(struct irq_desc *desc)
225 {
226 	struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
227 	struct irq_chip *chip = irq_desc_get_chip(desc);
228 	unsigned long val;
229 	int i;
230 
231 	chained_irq_enter(chip, desc);
232 
233 	val = FIELD_GET(XILINX_CPM_PCIE_IDRN_MASK,
234 			pcie_read(port, XILINX_CPM_PCIE_REG_IDRN));
235 
236 	for_each_set_bit(i, &val, PCI_NUM_INTX)
237 		generic_handle_domain_irq(port->intx_domain, i);
238 
239 	chained_irq_exit(chip, desc);
240 }
241 
242 static void xilinx_cpm_mask_event_irq(struct irq_data *d)
243 {
244 	struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
245 	u32 val;
246 
247 	raw_spin_lock(&port->lock);
248 	val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
249 	val &= ~BIT(d->hwirq);
250 	pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
251 	raw_spin_unlock(&port->lock);
252 }
253 
254 static void xilinx_cpm_unmask_event_irq(struct irq_data *d)
255 {
256 	struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
257 	u32 val;
258 
259 	raw_spin_lock(&port->lock);
260 	val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
261 	val |= BIT(d->hwirq);
262 	pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
263 	raw_spin_unlock(&port->lock);
264 }
265 
266 static struct irq_chip xilinx_cpm_event_irq_chip = {
267 	.name		= "RC-Event",
268 	.irq_mask	= xilinx_cpm_mask_event_irq,
269 	.irq_unmask	= xilinx_cpm_unmask_event_irq,
270 };
271 
272 static int xilinx_cpm_pcie_event_map(struct irq_domain *domain,
273 				     unsigned int irq, irq_hw_number_t hwirq)
274 {
275 	irq_set_chip_and_handler(irq, &xilinx_cpm_event_irq_chip,
276 				 handle_level_irq);
277 	irq_set_chip_data(irq, domain->host_data);
278 	irq_set_status_flags(irq, IRQ_LEVEL);
279 	return 0;
280 }
281 
282 static const struct irq_domain_ops event_domain_ops = {
283 	.map = xilinx_cpm_pcie_event_map,
284 };
285 
286 static void xilinx_cpm_pcie_event_flow(struct irq_desc *desc)
287 {
288 	struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
289 	struct irq_chip *chip = irq_desc_get_chip(desc);
290 	const struct xilinx_cpm_variant *variant = port->variant;
291 	unsigned long val;
292 	int i;
293 
294 	chained_irq_enter(chip, desc);
295 	val =  pcie_read(port, XILINX_CPM_PCIE_REG_IDR);
296 	val &= pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
297 	for_each_set_bit(i, &val, 32)
298 		generic_handle_domain_irq(port->cpm_domain, i);
299 	pcie_write(port, val, XILINX_CPM_PCIE_REG_IDR);
300 
301 	if (variant->ir_status) {
302 		val = readl_relaxed(port->cpm_base + variant->ir_status);
303 		if (val)
304 			writel_relaxed(val, port->cpm_base +
305 				       variant->ir_status);
306 	}
307 
308 	/*
309 	 * XILINX_CPM_PCIE_MISC_IR_STATUS register is mapped to
310 	 * CPM SLCR block.
311 	 */
312 	val = readl_relaxed(port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
313 	if (val)
314 		writel_relaxed(val,
315 			       port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
316 
317 	chained_irq_exit(chip, desc);
318 }
319 
320 #define _IC(x, s)                              \
321 	[XILINX_PCIE_INTR_ ## x] = { __stringify(x), s }
322 
323 static const struct {
324 	const char      *sym;
325 	const char      *str;
326 } intr_cause[32] = {
327 	_IC(LINK_DOWN,		"Link Down"),
328 	_IC(HOT_RESET,		"Hot reset"),
329 	_IC(CFG_TIMEOUT,	"ECAM access timeout"),
330 	_IC(CORRECTABLE,	"Correctable error message"),
331 	_IC(NONFATAL,		"Non fatal error message"),
332 	_IC(FATAL,		"Fatal error message"),
333 	_IC(SLV_UNSUPP,		"Slave unsupported request"),
334 	_IC(SLV_UNEXP,		"Slave unexpected completion"),
335 	_IC(SLV_COMPL,		"Slave completion timeout"),
336 	_IC(SLV_ERRP,		"Slave Error Poison"),
337 	_IC(SLV_CMPABT,		"Slave Completer Abort"),
338 	_IC(SLV_ILLBUR,		"Slave Illegal Burst"),
339 	_IC(MST_DECERR,		"Master decode error"),
340 	_IC(MST_SLVERR,		"Master slave error"),
341 	_IC(CFG_PCIE_TIMEOUT,	"PCIe ECAM access timeout"),
342 	_IC(CFG_ERR_POISON,	"ECAM poisoned completion received"),
343 	_IC(PME_TO_ACK_RCVD,	"PME_TO_ACK message received"),
344 	_IC(PM_PME_RCVD,	"PM_PME message received"),
345 	_IC(SLV_PCIE_TIMEOUT,	"PCIe completion timeout received"),
346 };
347 
348 static irqreturn_t xilinx_cpm_pcie_intr_handler(int irq, void *dev_id)
349 {
350 	struct xilinx_cpm_pcie *port = dev_id;
351 	struct device *dev = port->dev;
352 	struct irq_data *d;
353 
354 	d = irq_domain_get_irq_data(port->cpm_domain, irq);
355 
356 	switch (d->hwirq) {
357 	case XILINX_PCIE_INTR_CORRECTABLE:
358 	case XILINX_PCIE_INTR_NONFATAL:
359 	case XILINX_PCIE_INTR_FATAL:
360 		cpm_pcie_clear_err_interrupts(port);
361 		fallthrough;
362 
363 	default:
364 		if (intr_cause[d->hwirq].str)
365 			dev_warn(dev, "%s\n", intr_cause[d->hwirq].str);
366 		else
367 			dev_warn(dev, "Unknown IRQ %ld\n", d->hwirq);
368 	}
369 
370 	return IRQ_HANDLED;
371 }
372 
373 static void xilinx_cpm_free_irq_domains(struct xilinx_cpm_pcie *port)
374 {
375 	if (port->intx_domain) {
376 		irq_domain_remove(port->intx_domain);
377 		port->intx_domain = NULL;
378 	}
379 
380 	if (port->cpm_domain) {
381 		irq_domain_remove(port->cpm_domain);
382 		port->cpm_domain = NULL;
383 	}
384 }
385 
386 /**
387  * xilinx_cpm_pcie_init_irq_domain - Initialize IRQ domain
388  * @port: PCIe port information
389  *
390  * Return: '0' on success and error value on failure
391  */
392 static int xilinx_cpm_pcie_init_irq_domain(struct xilinx_cpm_pcie *port)
393 {
394 	struct device *dev = port->dev;
395 	struct device_node *node = dev->of_node;
396 	struct device_node *pcie_intc_node;
397 
398 	/* Setup INTx */
399 	pcie_intc_node = of_get_child_by_name(node, "interrupt-controller");
400 	if (!pcie_intc_node) {
401 		dev_err(dev, "No PCIe Intc node found\n");
402 		return -EINVAL;
403 	}
404 
405 	port->cpm_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), 32,
406 						    &event_domain_ops, port);
407 	if (!port->cpm_domain)
408 		goto out;
409 
410 	irq_domain_update_bus_token(port->cpm_domain, DOMAIN_BUS_NEXUS);
411 
412 	port->intx_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), PCI_NUM_INTX,
413 						     &intx_domain_ops, port);
414 	if (!port->intx_domain)
415 		goto out;
416 
417 	irq_domain_update_bus_token(port->intx_domain, DOMAIN_BUS_WIRED);
418 
419 	of_node_put(pcie_intc_node);
420 	raw_spin_lock_init(&port->lock);
421 
422 	return 0;
423 out:
424 	xilinx_cpm_free_irq_domains(port);
425 	of_node_put(pcie_intc_node);
426 	dev_err(dev, "Failed to allocate IRQ domains\n");
427 
428 	return -ENOMEM;
429 }
430 
431 static int xilinx_cpm_setup_irq(struct xilinx_cpm_pcie *port)
432 {
433 	struct device *dev = port->dev;
434 	struct platform_device *pdev = to_platform_device(dev);
435 	int i, irq;
436 
437 	port->irq = platform_get_irq(pdev, 0);
438 	if (port->irq < 0)
439 		return port->irq;
440 
441 	for (i = 0; i < ARRAY_SIZE(intr_cause); i++) {
442 		int err;
443 
444 		if (!intr_cause[i].str)
445 			continue;
446 
447 		irq = irq_create_mapping(port->cpm_domain, i);
448 		if (!irq) {
449 			dev_err(dev, "Failed to map interrupt\n");
450 			return -ENXIO;
451 		}
452 
453 		err = devm_request_irq(dev, irq, xilinx_cpm_pcie_intr_handler,
454 				       0, intr_cause[i].sym, port);
455 		if (err) {
456 			dev_err(dev, "Failed to request IRQ %d\n", irq);
457 			return err;
458 		}
459 	}
460 
461 	port->intx_irq = irq_create_mapping(port->cpm_domain,
462 					    XILINX_PCIE_INTR_INTX);
463 	if (!port->intx_irq) {
464 		dev_err(dev, "Failed to map INTx interrupt\n");
465 		return -ENXIO;
466 	}
467 
468 	/* Plug the INTx chained handler */
469 	irq_set_chained_handler_and_data(port->intx_irq,
470 					 xilinx_cpm_pcie_intx_flow, port);
471 
472 	/* Plug the main event chained handler */
473 	irq_set_chained_handler_and_data(port->irq,
474 					 xilinx_cpm_pcie_event_flow, port);
475 
476 	return 0;
477 }
478 
479 /**
480  * xilinx_cpm_pcie_reset - Reset the PCIe controller and deassert PERST#
481  * @port: PCIe port information
482  *
483  * Reset the PCIe controller and then release the PERST# signal so that the
484  * link can train once the bridge is enabled.
485  */
486 static void xilinx_cpm_pcie_reset(struct xilinx_cpm_pcie *port)
487 {
488 	reset_control_assert(port->rstc);
489 	udelay(PCIE_T_PERST_US);
490 	reset_control_deassert(port->rstc);
491 	gpiod_set_value_cansleep(port->perst_gpio, 0);
492 	msleep(PCIE_RESET_CONFIG_WAIT_MS);
493 }
494 
495 /**
496  * xilinx_cpm_pcie_init_port - Initialize hardware
497  * @port: PCIe port information
498  */
499 static void xilinx_cpm_pcie_init_port(struct xilinx_cpm_pcie *port)
500 {
501 	const struct xilinx_cpm_variant *variant = port->variant;
502 
503 	if (variant->version == CPM5NC_HOST)
504 		return;
505 
506 	if (port->perst_gpio || port->rstc)
507 		xilinx_cpm_pcie_reset(port);
508 
509 	if (cpm_pcie_link_up(port))
510 		dev_info(port->dev, "PCIe Link is UP\n");
511 	else
512 		dev_info(port->dev, "PCIe Link is DOWN\n");
513 
514 	/* Disable all interrupts */
515 	pcie_write(port, ~XILINX_CPM_PCIE_IDR_ALL_MASK,
516 		   XILINX_CPM_PCIE_REG_IMR);
517 
518 	/* Clear pending interrupts */
519 	pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_IDR) &
520 		   XILINX_CPM_PCIE_IMR_ALL_MASK,
521 		   XILINX_CPM_PCIE_REG_IDR);
522 
523 	/*
524 	 * XILINX_CPM_PCIE_MISC_IR_ENABLE register is mapped to
525 	 * CPM SLCR block.
526 	 */
527 	writel(variant->ir_misc_value,
528 	       port->cpm_base + XILINX_CPM_PCIE_MISC_IR_ENABLE);
529 
530 	if (variant->ir_enable) {
531 		writel(XILINX_CPM_PCIE_IR_LOCAL,
532 		       port->cpm_base + variant->ir_enable);
533 	}
534 
535 	/* Set Bridge enable bit */
536 	pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_RPSC) |
537 		   XILINX_CPM_PCIE_REG_RPSC_BEN,
538 		   XILINX_CPM_PCIE_REG_RPSC);
539 }
540 
541 /**
542  * xilinx_cpm_pcie_parse_port - Parse the PCIe Root Port child node
543  * @port: PCIe port information
544  *
545  * Read the PERST# GPIO from the Root Port child node.
546  *
547  * Return: '0' on success and error value on failure
548  */
549 static int xilinx_cpm_pcie_parse_port(struct xilinx_cpm_pcie *port)
550 {
551 	struct device *dev = port->dev;
552 
553 	/*
554 	 * This platform currently supports only one Root Port, so the loop
555 	 * will execute only once.
556 	 * TODO: Enhance the driver to handle multiple Root Ports in the future.
557 	 */
558 	for_each_child_of_node_with_prefix(dev->of_node, pcie_port_node, "pcie") {
559 		port->perst_gpio = devm_fwnode_gpiod_get(dev,
560 							 of_fwnode_handle(pcie_port_node),
561 							 "reset", GPIOD_OUT_HIGH,
562 							 NULL);
563 		if (IS_ERR(port->perst_gpio)) {
564 			if (PTR_ERR(port->perst_gpio) == -ENOENT) {
565 				port->perst_gpio = NULL;
566 				return 0;
567 			}
568 			return dev_err_probe(dev, PTR_ERR(port->perst_gpio),
569 					     "Failed to request reset GPIO\n");
570 		}
571 		return 0;
572 	}
573 
574 	return 0;
575 }
576 
577 /**
578  * xilinx_cpm_pcie_parse_dt - Parse Device tree
579  * @port: PCIe port information
580  * @bus_range: Bus resource
581  *
582  * Return: '0' on success and error value on failure
583  */
584 static int xilinx_cpm_pcie_parse_dt(struct xilinx_cpm_pcie *port,
585 				    struct resource *bus_range)
586 {
587 	struct device *dev = port->dev;
588 	struct platform_device *pdev = to_platform_device(dev);
589 	struct resource *res;
590 	int ret;
591 
592 	/* CPM5NC does not support PERST# handling yet */
593 	if (port->variant->version != CPM5NC_HOST) {
594 		port->rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
595 		if (IS_ERR(port->rstc))
596 			return dev_err_probe(dev, PTR_ERR(port->rstc),
597 					     "Failed to request reset\n");
598 
599 		ret = xilinx_cpm_pcie_parse_port(port);
600 		if (ret)
601 			return ret;
602 	}
603 
604 	port->cpm_base = devm_platform_ioremap_resource_byname(pdev,
605 							       "cpm_slcr");
606 	if (IS_ERR(port->cpm_base))
607 		return PTR_ERR(port->cpm_base);
608 
609 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
610 	if (!res)
611 		return -ENXIO;
612 
613 	port->cfg = pci_ecam_create(dev, res, bus_range,
614 				    &pci_generic_ecam_ops);
615 	if (IS_ERR(port->cfg))
616 		return PTR_ERR(port->cfg);
617 
618 	if (port->variant->version == CPM5 ||
619 	    port->variant->version == CPM5_HOST1) {
620 		port->reg_base = devm_platform_ioremap_resource_byname(pdev,
621 								    "cpm_csr");
622 		if (IS_ERR(port->reg_base))
623 			return PTR_ERR(port->reg_base);
624 	} else {
625 		port->reg_base = port->cfg->win;
626 	}
627 
628 	return 0;
629 }
630 
631 static void xilinx_cpm_free_interrupts(struct xilinx_cpm_pcie *port)
632 {
633 	irq_set_chained_handler_and_data(port->intx_irq, NULL, NULL);
634 	irq_set_chained_handler_and_data(port->irq, NULL, NULL);
635 }
636 
637 /**
638  * xilinx_cpm_pcie_probe - Probe function
639  * @pdev: Platform device pointer
640  *
641  * Return: '0' on success and error value on failure
642  */
643 static int xilinx_cpm_pcie_probe(struct platform_device *pdev)
644 {
645 	struct xilinx_cpm_pcie *port;
646 	struct device *dev = &pdev->dev;
647 	struct pci_host_bridge *bridge;
648 	struct resource_entry *bus;
649 	int err;
650 
651 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
652 	if (!bridge)
653 		return -ENODEV;
654 
655 	port = pci_host_bridge_priv(bridge);
656 
657 	port->dev = dev;
658 
659 	port->variant = of_device_get_match_data(dev);
660 
661 	if (port->variant->version != CPM5NC_HOST) {
662 		err = xilinx_cpm_pcie_init_irq_domain(port);
663 		if (err)
664 			return err;
665 	}
666 
667 	bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
668 	if (!bus) {
669 		err = -ENODEV;
670 		goto err_free_irq_domains;
671 	}
672 
673 	err = xilinx_cpm_pcie_parse_dt(port, bus->res);
674 	if (err) {
675 		dev_err(dev, "Parsing DT failed\n");
676 		goto err_free_irq_domains;
677 	}
678 
679 	xilinx_cpm_pcie_init_port(port);
680 
681 	if (port->variant->version != CPM5NC_HOST) {
682 		err = xilinx_cpm_setup_irq(port);
683 		if (err) {
684 			dev_err(dev, "Failed to set up interrupts\n");
685 			goto err_setup_irq;
686 		}
687 	}
688 
689 	bridge->sysdata = port->cfg;
690 	bridge->ops = (struct pci_ops *)&pci_generic_ecam_ops.pci_ops;
691 
692 	err = pci_host_probe(bridge);
693 	if (err < 0)
694 		goto err_host_bridge;
695 
696 	return 0;
697 
698 err_host_bridge:
699 	if (port->variant->version != CPM5NC_HOST)
700 		xilinx_cpm_free_interrupts(port);
701 err_setup_irq:
702 	pci_ecam_free(port->cfg);
703 err_free_irq_domains:
704 	if (port->variant->version != CPM5NC_HOST)
705 		xilinx_cpm_free_irq_domains(port);
706 	return err;
707 }
708 
709 static const struct xilinx_cpm_variant cpm_host = {
710 	.version = CPM,
711 	.ir_misc_value = XILINX_CPM_PCIE0_MISC_IR_LOCAL,
712 };
713 
714 static const struct xilinx_cpm_variant cpm5_host = {
715 	.version = CPM5,
716 	.ir_misc_value = XILINX_CPM_PCIE0_MISC_IR_LOCAL,
717 	.ir_status = XILINX_CPM_PCIE0_IR_STATUS,
718 	.ir_enable = XILINX_CPM_PCIE0_IR_ENABLE,
719 };
720 
721 static const struct xilinx_cpm_variant cpm5_host1 = {
722 	.version = CPM5_HOST1,
723 	.ir_misc_value = XILINX_CPM_PCIE1_MISC_IR_LOCAL,
724 	.ir_status = XILINX_CPM_PCIE1_IR_STATUS,
725 	.ir_enable = XILINX_CPM_PCIE1_IR_ENABLE,
726 };
727 
728 static const struct xilinx_cpm_variant cpm5n_host = {
729 	.version = CPM5NC_HOST,
730 };
731 
732 static const struct of_device_id xilinx_cpm_pcie_of_match[] = {
733 	{
734 		.compatible = "xlnx,versal-cpm-host-1.00",
735 		.data = &cpm_host,
736 	},
737 	{
738 		.compatible = "xlnx,versal-cpm5-host",
739 		.data = &cpm5_host,
740 	},
741 	{
742 		.compatible = "xlnx,versal-cpm5-host1",
743 		.data = &cpm5_host1,
744 	},
745 	{
746 		.compatible = "xlnx,versal-cpm5nc-host",
747 		.data = &cpm5n_host,
748 	},
749 	{}
750 };
751 
752 static struct platform_driver xilinx_cpm_pcie_driver = {
753 	.driver = {
754 		.name = "xilinx-cpm-pcie",
755 		.of_match_table = xilinx_cpm_pcie_of_match,
756 		.suppress_bind_attrs = true,
757 	},
758 	.probe = xilinx_cpm_pcie_probe,
759 };
760 
761 builtin_platform_driver(xilinx_cpm_pcie_driver);
762