1 // SPDX-License-Identifier: GPL-2.0+
3 * PCIe host controller driver for Xilinx Versal CPM DMA Bridge
5 * (C) Copyright 2019 - 2020, Xilinx, Inc.
8 #include <linux/bitfield.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/irqchip.h>
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_pci.h>
18 #include <linux/of_platform.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/pci-ecam.h>
25 /* Register definitions */
26 #define XILINX_CPM_PCIE_REG_IDR 0x00000E10
27 #define XILINX_CPM_PCIE_REG_IMR 0x00000E14
28 #define XILINX_CPM_PCIE_REG_PSCR 0x00000E1C
29 #define XILINX_CPM_PCIE_REG_RPSC 0x00000E20
30 #define XILINX_CPM_PCIE_REG_RPEFR 0x00000E2C
31 #define XILINX_CPM_PCIE_REG_IDRN 0x00000E38
32 #define XILINX_CPM_PCIE_REG_IDRN_MASK 0x00000E3C
33 #define XILINX_CPM_PCIE_MISC_IR_STATUS 0x00000340
34 #define XILINX_CPM_PCIE_MISC_IR_ENABLE 0x00000348
35 #define XILINX_CPM_PCIE_MISC_IR_LOCAL BIT(1)
37 #define XILINX_CPM_PCIE_IR_STATUS 0x000002A0
38 #define XILINX_CPM_PCIE_IR_ENABLE 0x000002A8
39 #define XILINX_CPM_PCIE_IR_LOCAL BIT(0)
41 /* Interrupt registers definitions */
42 #define XILINX_CPM_PCIE_INTR_LINK_DOWN 0
43 #define XILINX_CPM_PCIE_INTR_HOT_RESET 3
44 #define XILINX_CPM_PCIE_INTR_CFG_PCIE_TIMEOUT 4
45 #define XILINX_CPM_PCIE_INTR_CFG_TIMEOUT 8
46 #define XILINX_CPM_PCIE_INTR_CORRECTABLE 9
47 #define XILINX_CPM_PCIE_INTR_NONFATAL 10
48 #define XILINX_CPM_PCIE_INTR_FATAL 11
49 #define XILINX_CPM_PCIE_INTR_CFG_ERR_POISON 12
50 #define XILINX_CPM_PCIE_INTR_PME_TO_ACK_RCVD 15
51 #define XILINX_CPM_PCIE_INTR_INTX 16
52 #define XILINX_CPM_PCIE_INTR_PM_PME_RCVD 17
53 #define XILINX_CPM_PCIE_INTR_SLV_UNSUPP 20
54 #define XILINX_CPM_PCIE_INTR_SLV_UNEXP 21
55 #define XILINX_CPM_PCIE_INTR_SLV_COMPL 22
56 #define XILINX_CPM_PCIE_INTR_SLV_ERRP 23
57 #define XILINX_CPM_PCIE_INTR_SLV_CMPABT 24
58 #define XILINX_CPM_PCIE_INTR_SLV_ILLBUR 25
59 #define XILINX_CPM_PCIE_INTR_MST_DECERR 26
60 #define XILINX_CPM_PCIE_INTR_MST_SLVERR 27
61 #define XILINX_CPM_PCIE_INTR_SLV_PCIE_TIMEOUT 28
63 #define IMR(x) BIT(XILINX_CPM_PCIE_INTR_ ##x)
65 #define XILINX_CPM_PCIE_IMR_ALL_MASK \
69 IMR(CFG_PCIE_TIMEOUT) | \
74 IMR(CFG_ERR_POISON) | \
75 IMR(PME_TO_ACK_RCVD) | \
86 IMR(SLV_PCIE_TIMEOUT) \
89 #define XILINX_CPM_PCIE_IDR_ALL_MASK 0xFFFFFFFF
90 #define XILINX_CPM_PCIE_IDRN_MASK GENMASK(19, 16)
91 #define XILINX_CPM_PCIE_IDRN_SHIFT 16
93 /* Root Port Error FIFO Read Register definitions */
94 #define XILINX_CPM_PCIE_RPEFR_ERR_VALID BIT(18)
95 #define XILINX_CPM_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
96 #define XILINX_CPM_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
98 /* Root Port Status/control Register definitions */
99 #define XILINX_CPM_PCIE_REG_RPSC_BEN BIT(0)
101 /* Phy Status/Control Register definitions */
102 #define XILINX_CPM_PCIE_REG_PSCR_LNKUP BIT(11)
104 enum xilinx_cpm_version {
110 * struct xilinx_cpm_variant - CPM variant information
111 * @version: CPM version
113 struct xilinx_cpm_variant {
114 enum xilinx_cpm_version version;
118 * struct xilinx_cpm_pcie - PCIe port information
119 * @dev: Device pointer
120 * @reg_base: Bridge Register Base
121 * @cpm_base: CPM System Level Control and Status Register(SLCR) Base
122 * @intx_domain: Legacy IRQ domain pointer
123 * @cpm_domain: CPM IRQ domain pointer
124 * @cfg: Holds mappings of config space window
125 * @intx_irq: legacy interrupt number
126 * @irq: Error interrupt number
127 * @lock: lock protecting shared register access
128 * @variant: CPM version check pointer
130 struct xilinx_cpm_pcie {
132 void __iomem *reg_base;
133 void __iomem *cpm_base;
134 struct irq_domain *intx_domain;
135 struct irq_domain *cpm_domain;
136 struct pci_config_window *cfg;
140 const struct xilinx_cpm_variant *variant;
143 static u32 pcie_read(struct xilinx_cpm_pcie *port, u32 reg)
145 return readl_relaxed(port->reg_base + reg);
148 static void pcie_write(struct xilinx_cpm_pcie *port,
151 writel_relaxed(val, port->reg_base + reg);
154 static bool cpm_pcie_link_up(struct xilinx_cpm_pcie *port)
156 return (pcie_read(port, XILINX_CPM_PCIE_REG_PSCR) &
157 XILINX_CPM_PCIE_REG_PSCR_LNKUP);
160 static void cpm_pcie_clear_err_interrupts(struct xilinx_cpm_pcie *port)
162 unsigned long val = pcie_read(port, XILINX_CPM_PCIE_REG_RPEFR);
164 if (val & XILINX_CPM_PCIE_RPEFR_ERR_VALID) {
165 dev_dbg(port->dev, "Requester ID %lu\n",
166 val & XILINX_CPM_PCIE_RPEFR_REQ_ID);
167 pcie_write(port, XILINX_CPM_PCIE_RPEFR_ALL_MASK,
168 XILINX_CPM_PCIE_REG_RPEFR);
172 static void xilinx_cpm_mask_leg_irq(struct irq_data *data)
174 struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
179 mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
180 raw_spin_lock_irqsave(&port->lock, flags);
181 val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
182 pcie_write(port, (val & (~mask)), XILINX_CPM_PCIE_REG_IDRN_MASK);
183 raw_spin_unlock_irqrestore(&port->lock, flags);
186 static void xilinx_cpm_unmask_leg_irq(struct irq_data *data)
188 struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
193 mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
194 raw_spin_lock_irqsave(&port->lock, flags);
195 val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
196 pcie_write(port, (val | mask), XILINX_CPM_PCIE_REG_IDRN_MASK);
197 raw_spin_unlock_irqrestore(&port->lock, flags);
200 static struct irq_chip xilinx_cpm_leg_irq_chip = {
202 .irq_mask = xilinx_cpm_mask_leg_irq,
203 .irq_unmask = xilinx_cpm_unmask_leg_irq,
207 * xilinx_cpm_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
208 * @domain: IRQ domain
209 * @irq: Virtual IRQ number
210 * @hwirq: HW interrupt number
212 * Return: Always returns 0.
214 static int xilinx_cpm_pcie_intx_map(struct irq_domain *domain,
215 unsigned int irq, irq_hw_number_t hwirq)
217 irq_set_chip_and_handler(irq, &xilinx_cpm_leg_irq_chip,
219 irq_set_chip_data(irq, domain->host_data);
220 irq_set_status_flags(irq, IRQ_LEVEL);
225 /* INTx IRQ Domain operations */
226 static const struct irq_domain_ops intx_domain_ops = {
227 .map = xilinx_cpm_pcie_intx_map,
230 static void xilinx_cpm_pcie_intx_flow(struct irq_desc *desc)
232 struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
233 struct irq_chip *chip = irq_desc_get_chip(desc);
237 chained_irq_enter(chip, desc);
239 val = FIELD_GET(XILINX_CPM_PCIE_IDRN_MASK,
240 pcie_read(port, XILINX_CPM_PCIE_REG_IDRN));
242 for_each_set_bit(i, &val, PCI_NUM_INTX)
243 generic_handle_domain_irq(port->intx_domain, i);
245 chained_irq_exit(chip, desc);
248 static void xilinx_cpm_mask_event_irq(struct irq_data *d)
250 struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
253 raw_spin_lock(&port->lock);
254 val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
255 val &= ~BIT(d->hwirq);
256 pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
257 raw_spin_unlock(&port->lock);
260 static void xilinx_cpm_unmask_event_irq(struct irq_data *d)
262 struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
265 raw_spin_lock(&port->lock);
266 val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
267 val |= BIT(d->hwirq);
268 pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
269 raw_spin_unlock(&port->lock);
272 static struct irq_chip xilinx_cpm_event_irq_chip = {
274 .irq_mask = xilinx_cpm_mask_event_irq,
275 .irq_unmask = xilinx_cpm_unmask_event_irq,
278 static int xilinx_cpm_pcie_event_map(struct irq_domain *domain,
279 unsigned int irq, irq_hw_number_t hwirq)
281 irq_set_chip_and_handler(irq, &xilinx_cpm_event_irq_chip,
283 irq_set_chip_data(irq, domain->host_data);
284 irq_set_status_flags(irq, IRQ_LEVEL);
288 static const struct irq_domain_ops event_domain_ops = {
289 .map = xilinx_cpm_pcie_event_map,
292 static void xilinx_cpm_pcie_event_flow(struct irq_desc *desc)
294 struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
295 struct irq_chip *chip = irq_desc_get_chip(desc);
299 chained_irq_enter(chip, desc);
300 val = pcie_read(port, XILINX_CPM_PCIE_REG_IDR);
301 val &= pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
302 for_each_set_bit(i, &val, 32)
303 generic_handle_domain_irq(port->cpm_domain, i);
304 pcie_write(port, val, XILINX_CPM_PCIE_REG_IDR);
306 if (port->variant->version == CPM5) {
307 val = readl_relaxed(port->cpm_base + XILINX_CPM_PCIE_IR_STATUS);
309 writel_relaxed(val, port->cpm_base +
310 XILINX_CPM_PCIE_IR_STATUS);
314 * XILINX_CPM_PCIE_MISC_IR_STATUS register is mapped to
317 val = readl_relaxed(port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
320 port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
322 chained_irq_exit(chip, desc);
326 [XILINX_CPM_PCIE_INTR_ ## x] = { __stringify(x), s }
328 static const struct {
332 _IC(LINK_DOWN, "Link Down"),
333 _IC(HOT_RESET, "Hot reset"),
334 _IC(CFG_TIMEOUT, "ECAM access timeout"),
335 _IC(CORRECTABLE, "Correctable error message"),
336 _IC(NONFATAL, "Non fatal error message"),
337 _IC(FATAL, "Fatal error message"),
338 _IC(SLV_UNSUPP, "Slave unsupported request"),
339 _IC(SLV_UNEXP, "Slave unexpected completion"),
340 _IC(SLV_COMPL, "Slave completion timeout"),
341 _IC(SLV_ERRP, "Slave Error Poison"),
342 _IC(SLV_CMPABT, "Slave Completer Abort"),
343 _IC(SLV_ILLBUR, "Slave Illegal Burst"),
344 _IC(MST_DECERR, "Master decode error"),
345 _IC(MST_SLVERR, "Master slave error"),
346 _IC(CFG_PCIE_TIMEOUT, "PCIe ECAM access timeout"),
347 _IC(CFG_ERR_POISON, "ECAM poisoned completion received"),
348 _IC(PME_TO_ACK_RCVD, "PME_TO_ACK message received"),
349 _IC(PM_PME_RCVD, "PM_PME message received"),
350 _IC(SLV_PCIE_TIMEOUT, "PCIe completion timeout received"),
353 static irqreturn_t xilinx_cpm_pcie_intr_handler(int irq, void *dev_id)
355 struct xilinx_cpm_pcie *port = dev_id;
356 struct device *dev = port->dev;
359 d = irq_domain_get_irq_data(port->cpm_domain, irq);
362 case XILINX_CPM_PCIE_INTR_CORRECTABLE:
363 case XILINX_CPM_PCIE_INTR_NONFATAL:
364 case XILINX_CPM_PCIE_INTR_FATAL:
365 cpm_pcie_clear_err_interrupts(port);
369 if (intr_cause[d->hwirq].str)
370 dev_warn(dev, "%s\n", intr_cause[d->hwirq].str);
372 dev_warn(dev, "Unknown IRQ %ld\n", d->hwirq);
378 static void xilinx_cpm_free_irq_domains(struct xilinx_cpm_pcie *port)
380 if (port->intx_domain) {
381 irq_domain_remove(port->intx_domain);
382 port->intx_domain = NULL;
385 if (port->cpm_domain) {
386 irq_domain_remove(port->cpm_domain);
387 port->cpm_domain = NULL;
392 * xilinx_cpm_pcie_init_irq_domain - Initialize IRQ domain
393 * @port: PCIe port information
395 * Return: '0' on success and error value on failure
397 static int xilinx_cpm_pcie_init_irq_domain(struct xilinx_cpm_pcie *port)
399 struct device *dev = port->dev;
400 struct device_node *node = dev->of_node;
401 struct device_node *pcie_intc_node;
404 pcie_intc_node = of_get_next_child(node, NULL);
405 if (!pcie_intc_node) {
406 dev_err(dev, "No PCIe Intc node found\n");
410 port->cpm_domain = irq_domain_add_linear(pcie_intc_node, 32,
413 if (!port->cpm_domain)
416 irq_domain_update_bus_token(port->cpm_domain, DOMAIN_BUS_NEXUS);
418 port->intx_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
421 if (!port->intx_domain)
424 irq_domain_update_bus_token(port->intx_domain, DOMAIN_BUS_WIRED);
426 of_node_put(pcie_intc_node);
427 raw_spin_lock_init(&port->lock);
431 xilinx_cpm_free_irq_domains(port);
432 of_node_put(pcie_intc_node);
433 dev_err(dev, "Failed to allocate IRQ domains\n");
438 static int xilinx_cpm_setup_irq(struct xilinx_cpm_pcie *port)
440 struct device *dev = port->dev;
441 struct platform_device *pdev = to_platform_device(dev);
444 port->irq = platform_get_irq(pdev, 0);
448 for (i = 0; i < ARRAY_SIZE(intr_cause); i++) {
451 if (!intr_cause[i].str)
454 irq = irq_create_mapping(port->cpm_domain, i);
456 dev_err(dev, "Failed to map interrupt\n");
460 err = devm_request_irq(dev, irq, xilinx_cpm_pcie_intr_handler,
461 0, intr_cause[i].sym, port);
463 dev_err(dev, "Failed to request IRQ %d\n", irq);
468 port->intx_irq = irq_create_mapping(port->cpm_domain,
469 XILINX_CPM_PCIE_INTR_INTX);
470 if (!port->intx_irq) {
471 dev_err(dev, "Failed to map INTx interrupt\n");
475 /* Plug the INTx chained handler */
476 irq_set_chained_handler_and_data(port->intx_irq,
477 xilinx_cpm_pcie_intx_flow, port);
479 /* Plug the main event chained handler */
480 irq_set_chained_handler_and_data(port->irq,
481 xilinx_cpm_pcie_event_flow, port);
487 * xilinx_cpm_pcie_init_port - Initialize hardware
488 * @port: PCIe port information
490 static void xilinx_cpm_pcie_init_port(struct xilinx_cpm_pcie *port)
492 if (cpm_pcie_link_up(port))
493 dev_info(port->dev, "PCIe Link is UP\n");
495 dev_info(port->dev, "PCIe Link is DOWN\n");
497 /* Disable all interrupts */
498 pcie_write(port, ~XILINX_CPM_PCIE_IDR_ALL_MASK,
499 XILINX_CPM_PCIE_REG_IMR);
501 /* Clear pending interrupts */
502 pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_IDR) &
503 XILINX_CPM_PCIE_IMR_ALL_MASK,
504 XILINX_CPM_PCIE_REG_IDR);
507 * XILINX_CPM_PCIE_MISC_IR_ENABLE register is mapped to
510 writel(XILINX_CPM_PCIE_MISC_IR_LOCAL,
511 port->cpm_base + XILINX_CPM_PCIE_MISC_IR_ENABLE);
513 if (port->variant->version == CPM5) {
514 writel(XILINX_CPM_PCIE_IR_LOCAL,
515 port->cpm_base + XILINX_CPM_PCIE_IR_ENABLE);
518 /* Enable the Bridge enable bit */
519 pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_RPSC) |
520 XILINX_CPM_PCIE_REG_RPSC_BEN,
521 XILINX_CPM_PCIE_REG_RPSC);
525 * xilinx_cpm_pcie_parse_dt - Parse Device tree
526 * @port: PCIe port information
527 * @bus_range: Bus resource
529 * Return: '0' on success and error value on failure
531 static int xilinx_cpm_pcie_parse_dt(struct xilinx_cpm_pcie *port,
532 struct resource *bus_range)
534 struct device *dev = port->dev;
535 struct platform_device *pdev = to_platform_device(dev);
536 struct resource *res;
538 port->cpm_base = devm_platform_ioremap_resource_byname(pdev,
540 if (IS_ERR(port->cpm_base))
541 return PTR_ERR(port->cpm_base);
543 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
547 port->cfg = pci_ecam_create(dev, res, bus_range,
548 &pci_generic_ecam_ops);
549 if (IS_ERR(port->cfg))
550 return PTR_ERR(port->cfg);
552 if (port->variant->version == CPM5) {
553 port->reg_base = devm_platform_ioremap_resource_byname(pdev,
555 if (IS_ERR(port->reg_base))
556 return PTR_ERR(port->reg_base);
558 port->reg_base = port->cfg->win;
564 static void xilinx_cpm_free_interrupts(struct xilinx_cpm_pcie *port)
566 irq_set_chained_handler_and_data(port->intx_irq, NULL, NULL);
567 irq_set_chained_handler_and_data(port->irq, NULL, NULL);
571 * xilinx_cpm_pcie_probe - Probe function
572 * @pdev: Platform device pointer
574 * Return: '0' on success and error value on failure
576 static int xilinx_cpm_pcie_probe(struct platform_device *pdev)
578 struct xilinx_cpm_pcie *port;
579 struct device *dev = &pdev->dev;
580 struct pci_host_bridge *bridge;
581 struct resource_entry *bus;
584 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
588 port = pci_host_bridge_priv(bridge);
592 err = xilinx_cpm_pcie_init_irq_domain(port);
596 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
600 port->variant = of_device_get_match_data(dev);
602 err = xilinx_cpm_pcie_parse_dt(port, bus->res);
604 dev_err(dev, "Parsing DT failed\n");
608 xilinx_cpm_pcie_init_port(port);
610 err = xilinx_cpm_setup_irq(port);
612 dev_err(dev, "Failed to set up interrupts\n");
616 bridge->sysdata = port->cfg;
617 bridge->ops = (struct pci_ops *)&pci_generic_ecam_ops.pci_ops;
619 err = pci_host_probe(bridge);
621 goto err_host_bridge;
626 xilinx_cpm_free_interrupts(port);
628 pci_ecam_free(port->cfg);
630 xilinx_cpm_free_irq_domains(port);
634 static const struct xilinx_cpm_variant cpm_host = {
638 static const struct xilinx_cpm_variant cpm5_host = {
642 static const struct of_device_id xilinx_cpm_pcie_of_match[] = {
644 .compatible = "xlnx,versal-cpm-host-1.00",
648 .compatible = "xlnx,versal-cpm5-host",
654 static struct platform_driver xilinx_cpm_pcie_driver = {
656 .name = "xilinx-cpm-pcie",
657 .of_match_table = xilinx_cpm_pcie_of_match,
658 .suppress_bind_attrs = true,
660 .probe = xilinx_cpm_pcie_probe,
663 builtin_platform_driver(xilinx_cpm_pcie_driver);