1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Texas Instruments Keystone SoCs
5 * Copyright (C) 2013-2014 Texas Instruments., Ltd.
8 * Author: Murali Karicheri <m-karicheri2@ti.com>
9 * Implementation based on pci-exynos.c and pcie-designware.c
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/init.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/msi.h>
20 #include <linux/of_irq.h>
22 #include <linux/of_pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy/phy.h>
25 #include <linux/regmap.h>
26 #include <linux/resource.h>
27 #include <linux/signal.h>
29 #include "pcie-designware.h"
31 #define DRIVER_NAME "keystone-pcie"
33 #define PCIE_VENDORID_MASK 0xffff
34 #define PCIE_DEVICEID_SHIFT 16
37 #define PCIE_CAP_BASE 0x70
39 /* Application register defines */
40 #define LTSSM_EN_VAL BIT(0)
41 #define LTSSM_STATE_MASK 0x1f
42 #define LTSSM_STATE_L0 0x11
43 #define DBI_CS2_EN_VAL 0x20
44 #define OB_XLAT_EN_VAL 2
46 /* Application registers */
47 #define CMD_STATUS 0x004
49 #define CFG_SETUP 0x008
50 #define CFG_BUS(x) (((x) & 0xff) << 16)
51 #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
52 #define CFG_FUNC(x) ((x) & 0x7)
53 #define CFG_TYPE1 BIT(24)
56 #define CFG_PCIM_WIN_SZ_IDX 3
57 #define CFG_PCIM_WIN_CNT 32
58 #define SPACE0_REMOTE_CFG_OFFSET 0x1000
59 #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
60 #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
62 /* IRQ register defines */
64 #define IRQ_STATUS 0x184
65 #define IRQ_ENABLE_SET 0x188
66 #define IRQ_ENABLE_CLR 0x18c
69 #define MSI0_IRQ_STATUS 0x104
70 #define MSI0_IRQ_ENABLE_SET 0x108
71 #define MSI0_IRQ_ENABLE_CLR 0x10c
72 #define IRQ_STATUS 0x184
73 #define MSI_IRQ_OFFSET 4
76 #define ERR_AER BIT(5) /* ECRC error */
77 #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
78 #define ERR_CORR BIT(3) /* Correctable error */
79 #define ERR_NONFATAL BIT(2) /* Non-fatal error */
80 #define ERR_FATAL BIT(1) /* Fatal error */
81 #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
82 #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
83 ERR_NONFATAL | ERR_FATAL | ERR_SYS)
84 #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
85 #define ERR_IRQ_STATUS_RAW 0x1c0
86 #define ERR_IRQ_STATUS 0x1c4
87 #define ERR_IRQ_ENABLE_SET 0x1c8
88 #define ERR_IRQ_ENABLE_CLR 0x1cc
90 /* Config space registers */
93 #define MAX_MSI_HOST_IRQS 8
95 /* PCIE controller device IDs */
96 #define PCIE_RC_K2HK 0xb008
97 #define PCIE_RC_K2E 0xb009
98 #define PCIE_RC_K2L 0xb00a
99 #define PCIE_RC_K2G 0xb00b
101 #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
103 struct keystone_pcie {
107 int num_legacy_host_irqs;
108 int legacy_host_irqs[PCI_NUM_INTX];
109 struct device_node *legacy_intc_np;
111 int num_msi_host_irqs;
112 int msi_host_irqs[MAX_MSI_HOST_IRQS];
115 struct device_link **link;
116 struct device_node *msi_intc_np;
117 struct irq_domain *legacy_irq_domain;
118 struct device_node *np;
122 /* Application register space */
123 void __iomem *va_app_base; /* DT 1st resource */
127 static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
130 *reg_offset = offset % 8;
131 *bit_pos = offset >> 3;
134 static phys_addr_t ks_pcie_get_msi_addr(struct pcie_port *pp)
136 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
137 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
139 return ks_pcie->app.start + MSI_IRQ;
142 static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
144 return readl(ks_pcie->va_app_base + offset);
147 static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
150 writel(val, ks_pcie->va_app_base + offset);
153 static void ks_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
155 struct dw_pcie *pci = ks_pcie->pci;
156 struct pcie_port *pp = &pci->pp;
157 struct device *dev = pci->dev;
161 pending = ks_pcie_app_readl(ks_pcie, MSI0_IRQ_STATUS + (offset << 4));
164 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
165 * shows 1, 9, 17, 25 and so forth
167 for (src = 0; src < 4; src++) {
168 if (BIT(src) & pending) {
169 vector = offset + (src << 3);
170 virq = irq_linear_revmap(pp->irq_domain, vector);
171 dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
173 generic_handle_irq(virq);
178 static void ks_pcie_msi_irq_ack(int irq, struct pcie_port *pp)
180 u32 reg_offset, bit_pos;
181 struct keystone_pcie *ks_pcie;
184 pci = to_dw_pcie_from_pp(pp);
185 ks_pcie = to_keystone_pcie(pci);
186 update_reg_offset_bit_pos(irq, ®_offset, &bit_pos);
188 ks_pcie_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
190 ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
193 static void ks_pcie_msi_set_irq(struct pcie_port *pp, int irq)
195 u32 reg_offset, bit_pos;
196 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
197 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
199 update_reg_offset_bit_pos(irq, ®_offset, &bit_pos);
200 ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
204 static void ks_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
206 u32 reg_offset, bit_pos;
207 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
208 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
210 update_reg_offset_bit_pos(irq, ®_offset, &bit_pos);
211 ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
215 static int ks_pcie_msi_host_init(struct pcie_port *pp)
217 return dw_pcie_allocate_domains(pp);
220 static void ks_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
224 for (i = 0; i < PCI_NUM_INTX; i++)
225 ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
228 static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie,
231 struct dw_pcie *pci = ks_pcie->pci;
232 struct device *dev = pci->dev;
236 pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS + (offset << 4));
238 if (BIT(0) & pending) {
239 virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
240 dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
241 generic_handle_irq(virq);
244 /* EOI the INTx interrupt */
245 ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset);
248 static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
250 ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
253 static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
257 status = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
261 if (status & ERR_FATAL_IRQ)
262 dev_err(ks_pcie->pci->dev, "fatal error (status %#010x)\n",
265 /* Ack the IRQ; status bits are RW1C */
266 ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, status);
270 static void ks_pcie_ack_legacy_irq(struct irq_data *d)
274 static void ks_pcie_mask_legacy_irq(struct irq_data *d)
278 static void ks_pcie_unmask_legacy_irq(struct irq_data *d)
282 static struct irq_chip ks_pcie_legacy_irq_chip = {
283 .name = "Keystone-PCI-Legacy-IRQ",
284 .irq_ack = ks_pcie_ack_legacy_irq,
285 .irq_mask = ks_pcie_mask_legacy_irq,
286 .irq_unmask = ks_pcie_unmask_legacy_irq,
289 static int ks_pcie_init_legacy_irq_map(struct irq_domain *d,
291 irq_hw_number_t hw_irq)
293 irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip,
295 irq_set_chip_data(irq, d->host_data);
300 static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = {
301 .map = ks_pcie_init_legacy_irq_map,
302 .xlate = irq_domain_xlate_onetwocell,
306 * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
309 * Since modification of dbi_cs2 involves different clock domain, read the
310 * status back to ensure the transition is complete.
312 static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
316 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
317 ks_pcie_app_writel(ks_pcie, CMD_STATUS, DBI_CS2_EN_VAL | val);
320 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
321 } while (!(val & DBI_CS2_EN_VAL));
325 * ks_pcie_clear_dbi_mode() - Disable DBI mode
327 * Since modification of dbi_cs2 involves different clock domain, read the
328 * status back to ensure the transition is complete.
330 static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
334 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
335 ks_pcie_app_writel(ks_pcie, CMD_STATUS, ~DBI_CS2_EN_VAL & val);
338 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
339 } while (val & DBI_CS2_EN_VAL);
342 static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
344 struct dw_pcie *pci = ks_pcie->pci;
345 struct pcie_port *pp = &pci->pp;
346 u32 start = pp->mem->start, end = pp->mem->end;
350 /* Disable BARs for inbound access */
351 ks_pcie_set_dbi_mode(ks_pcie);
352 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
353 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
354 ks_pcie_clear_dbi_mode(ks_pcie);
356 /* Set outbound translation size per window division */
357 ks_pcie_app_writel(ks_pcie, OB_SIZE, CFG_PCIM_WIN_SZ_IDX & 0x7);
359 tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
361 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
362 for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
363 ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i), start | 1);
364 ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i), 0);
368 /* Enable OB translation */
369 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
370 ks_pcie_app_writel(ks_pcie, CMD_STATUS, OB_XLAT_EN_VAL | val);
373 static int ks_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
374 unsigned int devfn, int where, int size,
377 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
378 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
381 reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
382 CFG_FUNC(PCI_FUNC(devfn));
383 if (bus->parent->number != pp->root_bus_nr)
385 ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
387 return dw_pcie_read(pp->va_cfg0_base + where, size, val);
390 static int ks_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
391 unsigned int devfn, int where, int size,
394 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
395 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
398 reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
399 CFG_FUNC(PCI_FUNC(devfn));
400 if (bus->parent->number != pp->root_bus_nr)
402 ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
404 return dw_pcie_write(pp->va_cfg0_base + where, size, val);
408 * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
410 * This sets BAR0 to enable inbound access for MSI_IRQ register
412 static void ks_pcie_v3_65_scan_bus(struct pcie_port *pp)
414 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
415 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
417 /* Configure and set up BAR0 */
418 ks_pcie_set_dbi_mode(ks_pcie);
421 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
422 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
424 ks_pcie_clear_dbi_mode(ks_pcie);
427 * For BAR0, just setting bus address for inbound writes (MSI) should
428 * be sufficient. Use physical address to avoid any conflicts.
430 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
434 * ks_pcie_link_up() - Check if link up
436 static int ks_pcie_link_up(struct dw_pcie *pci)
440 val = dw_pcie_readl_dbi(pci, DEBUG0);
441 return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
444 static void ks_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
448 /* Disable Link training */
449 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
450 val &= ~LTSSM_EN_VAL;
451 ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
453 /* Initiate Link Training */
454 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
455 ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
459 * ks_pcie_dw_host_init() - initialize host for v3_65 dw hardware
461 * Ioremap the register resources, initialize legacy irq domain
462 * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
463 * PCI host controller.
465 static int __init ks_pcie_dw_host_init(struct keystone_pcie *ks_pcie)
467 struct dw_pcie *pci = ks_pcie->pci;
468 struct pcie_port *pp = &pci->pp;
469 struct device *dev = pci->dev;
470 struct platform_device *pdev = to_platform_device(dev);
471 struct resource *res;
473 /* Index 0 is the config reg. space address */
474 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
476 if (IS_ERR(pci->dbi_base))
477 return PTR_ERR(pci->dbi_base);
480 * We set these same and is used in pcie rd/wr_other_conf
483 pp->va_cfg0_base = pci->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
484 pp->va_cfg1_base = pp->va_cfg0_base;
486 /* Index 1 is the application reg. space address */
487 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
488 ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
489 if (IS_ERR(ks_pcie->va_app_base))
490 return PTR_ERR(ks_pcie->va_app_base);
494 /* Create legacy IRQ domain */
495 ks_pcie->legacy_irq_domain =
496 irq_domain_add_linear(ks_pcie->legacy_intc_np,
498 &ks_pcie_legacy_irq_domain_ops,
500 if (!ks_pcie->legacy_irq_domain) {
501 dev_err(dev, "Failed to add irq domain for legacy irqs\n");
505 return dw_pcie_host_init(pp);
508 static void ks_pcie_quirk(struct pci_dev *dev)
510 struct pci_bus *bus = dev->bus;
511 struct pci_dev *bridge;
512 static const struct pci_device_id rc_pci_devids[] = {
513 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
514 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
515 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
516 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
517 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
518 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
519 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
520 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
524 if (pci_is_root_bus(bus))
527 /* look for the host bridge */
528 while (!pci_is_root_bus(bus)) {
537 * Keystone PCI controller has a h/w limitation of
538 * 256 bytes maximum read request size. It can't handle
539 * anything higher than this. So force this limit on
540 * all downstream devices.
542 if (pci_match_id(rc_pci_devids, bridge)) {
543 if (pcie_get_readrq(dev) > 256) {
544 dev_info(&dev->dev, "limiting MRRS to 256\n");
545 pcie_set_readrq(dev, 256);
549 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk);
551 static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
553 struct dw_pcie *pci = ks_pcie->pci;
554 struct device *dev = pci->dev;
556 if (dw_pcie_link_up(pci)) {
557 dev_info(dev, "Link already up\n");
561 ks_pcie_initiate_link_train(ks_pcie);
563 /* check if the link is up or not */
564 if (!dw_pcie_wait_for_link(pci))
567 dev_err(dev, "phy link never came up\n");
571 static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
573 unsigned int irq = irq_desc_get_irq(desc);
574 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
575 u32 offset = irq - ks_pcie->msi_host_irqs[0];
576 struct dw_pcie *pci = ks_pcie->pci;
577 struct device *dev = pci->dev;
578 struct irq_chip *chip = irq_desc_get_chip(desc);
580 dev_dbg(dev, "%s, irq %d\n", __func__, irq);
583 * The chained irq handler installation would have replaced normal
584 * interrupt driver handler so we need to take care of mask/unmask and
587 chained_irq_enter(chip, desc);
588 ks_pcie_handle_msi_irq(ks_pcie, offset);
589 chained_irq_exit(chip, desc);
593 * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
594 * @irq: IRQ line for legacy interrupts
595 * @desc: Pointer to irq descriptor
597 * Traverse through pending legacy interrupts and invoke handler for each. Also
598 * takes care of interrupt controller level mask/ack operation.
600 static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
602 unsigned int irq = irq_desc_get_irq(desc);
603 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
604 struct dw_pcie *pci = ks_pcie->pci;
605 struct device *dev = pci->dev;
606 u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
607 struct irq_chip *chip = irq_desc_get_chip(desc);
609 dev_dbg(dev, ": Handling legacy irq %d\n", irq);
612 * The chained irq handler installation would have replaced normal
613 * interrupt driver handler so we need to take care of mask/unmask and
616 chained_irq_enter(chip, desc);
617 ks_pcie_handle_legacy_irq(ks_pcie, irq_offset);
618 chained_irq_exit(chip, desc);
621 static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
622 char *controller, int *num_irqs)
624 int temp, max_host_irqs, legacy = 1, *host_irqs;
625 struct device *dev = ks_pcie->pci->dev;
626 struct device_node *np_pcie = dev->of_node, **np_temp;
628 if (!strcmp(controller, "msi-interrupt-controller"))
632 np_temp = &ks_pcie->legacy_intc_np;
633 max_host_irqs = PCI_NUM_INTX;
634 host_irqs = &ks_pcie->legacy_host_irqs[0];
636 np_temp = &ks_pcie->msi_intc_np;
637 max_host_irqs = MAX_MSI_HOST_IRQS;
638 host_irqs = &ks_pcie->msi_host_irqs[0];
641 /* interrupt controller is in a child node */
642 *np_temp = of_get_child_by_name(np_pcie, controller);
644 dev_err(dev, "Node for %s is absent\n", controller);
648 temp = of_irq_count(*np_temp);
650 dev_err(dev, "No IRQ entries in %s\n", controller);
651 of_node_put(*np_temp);
655 if (temp > max_host_irqs)
656 dev_warn(dev, "Too many %s interrupts defined %u\n",
657 (legacy ? "legacy" : "MSI"), temp);
660 * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
663 for (temp = 0; temp < max_host_irqs; temp++) {
664 host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
665 if (!host_irqs[temp])
669 of_node_put(*np_temp);
679 static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
684 for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
685 irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
686 ks_pcie_legacy_irq_handler,
689 ks_pcie_enable_legacy_irqs(ks_pcie);
692 if (IS_ENABLED(CONFIG_PCI_MSI)) {
693 for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
694 irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
695 ks_pcie_msi_irq_handler,
700 if (ks_pcie->error_irq > 0)
701 ks_pcie_enable_error_irq(ks_pcie);
705 * When a PCI device does not exist during config cycles, keystone host gets a
706 * bus error instead of returning 0xffffffff. This handler always returns 0
707 * for this kind of faults.
709 static int ks_pcie_fault(unsigned long addr, unsigned int fsr,
710 struct pt_regs *regs)
712 unsigned long instr = *(unsigned long *) instruction_pointer(regs);
714 if ((instr & 0x0e100090) == 0x00100090) {
715 int reg = (instr >> 12) & 15;
717 regs->uregs[reg] = -1;
724 static int __init ks_pcie_init_id(struct keystone_pcie *ks_pcie)
728 struct regmap *devctrl_regs;
729 struct dw_pcie *pci = ks_pcie->pci;
730 struct device *dev = pci->dev;
731 struct device_node *np = dev->of_node;
733 devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id");
734 if (IS_ERR(devctrl_regs))
735 return PTR_ERR(devctrl_regs);
737 ret = regmap_read(devctrl_regs, 0, &id);
741 dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK);
742 dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT);
747 static int __init ks_pcie_host_init(struct pcie_port *pp)
749 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
750 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
753 dw_pcie_setup_rc(pp);
755 ks_pcie_establish_link(ks_pcie);
756 ks_pcie_setup_rc_app_regs(ks_pcie);
757 ks_pcie_setup_interrupts(ks_pcie);
758 writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
759 pci->dbi_base + PCI_IO_BASE);
761 ret = ks_pcie_init_id(ks_pcie);
766 * PCIe access errors that result into OCP errors are caught by ARM as
769 hook_fault_code(17, ks_pcie_fault, SIGBUS, 0,
770 "Asynchronous external abort");
775 static const struct dw_pcie_host_ops ks_pcie_host_ops = {
776 .rd_other_conf = ks_pcie_rd_other_conf,
777 .wr_other_conf = ks_pcie_wr_other_conf,
778 .host_init = ks_pcie_host_init,
779 .msi_set_irq = ks_pcie_msi_set_irq,
780 .msi_clear_irq = ks_pcie_msi_clear_irq,
781 .get_msi_addr = ks_pcie_get_msi_addr,
782 .msi_host_init = ks_pcie_msi_host_init,
783 .msi_irq_ack = ks_pcie_msi_irq_ack,
784 .scan_bus = ks_pcie_v3_65_scan_bus,
787 static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv)
789 struct keystone_pcie *ks_pcie = priv;
791 return ks_pcie_handle_error_irq(ks_pcie);
794 static int __init ks_pcie_add_pcie_port(struct keystone_pcie *ks_pcie,
795 struct platform_device *pdev)
797 struct dw_pcie *pci = ks_pcie->pci;
798 struct pcie_port *pp = &pci->pp;
799 struct device *dev = &pdev->dev;
802 ret = ks_pcie_get_irq_controller_info(ks_pcie,
803 "legacy-interrupt-controller",
804 &ks_pcie->num_legacy_host_irqs);
808 if (IS_ENABLED(CONFIG_PCI_MSI)) {
809 ret = ks_pcie_get_irq_controller_info(ks_pcie,
810 "msi-interrupt-controller",
811 &ks_pcie->num_msi_host_irqs);
817 * Index 0 is the platform interrupt for error interrupt
818 * from RC. This is optional.
820 ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
821 if (ks_pcie->error_irq <= 0)
822 dev_info(dev, "no error IRQ defined\n");
824 ret = request_irq(ks_pcie->error_irq, ks_pcie_err_irq_handler,
825 IRQF_SHARED, "pcie-error-irq", ks_pcie);
827 dev_err(dev, "failed to request error IRQ %d\n",
833 pp->ops = &ks_pcie_host_ops;
834 ret = ks_pcie_dw_host_init(ks_pcie);
836 dev_err(dev, "failed to initialize host\n");
843 static const struct of_device_id ks_pcie_of_match[] = {
846 .compatible = "ti,keystone-pcie",
851 static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = {
852 .link_up = ks_pcie_link_up,
855 static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie)
857 int num_lanes = ks_pcie->num_lanes;
859 while (num_lanes--) {
860 phy_power_off(ks_pcie->phy[num_lanes]);
861 phy_exit(ks_pcie->phy[num_lanes]);
865 static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie)
869 int num_lanes = ks_pcie->num_lanes;
871 for (i = 0; i < num_lanes; i++) {
872 ret = phy_init(ks_pcie->phy[i]);
876 ret = phy_power_on(ks_pcie->phy[i]);
878 phy_exit(ks_pcie->phy[i]);
887 phy_power_off(ks_pcie->phy[i]);
888 phy_exit(ks_pcie->phy[i]);
894 static int __init ks_pcie_probe(struct platform_device *pdev)
896 struct device *dev = &pdev->dev;
897 struct device_node *np = dev->of_node;
899 struct keystone_pcie *ks_pcie;
900 struct device_link **link;
907 ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
911 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
916 pci->ops = &ks_pcie_dw_pcie_ops;
918 ret = of_property_read_u32(np, "num-lanes", &num_lanes);
922 phy = devm_kzalloc(dev, sizeof(*phy) * num_lanes, GFP_KERNEL);
926 link = devm_kzalloc(dev, sizeof(*link) * num_lanes, GFP_KERNEL);
930 for (i = 0; i < num_lanes; i++) {
931 snprintf(name, sizeof(name), "pcie-phy%d", i);
932 phy[i] = devm_phy_optional_get(dev, name);
933 if (IS_ERR(phy[i])) {
934 ret = PTR_ERR(phy[i]);
941 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
950 ks_pcie->link = link;
951 ks_pcie->num_lanes = num_lanes;
954 ret = ks_pcie_enable_phy(ks_pcie);
956 dev_err(dev, "failed to enable phy\n");
960 platform_set_drvdata(pdev, ks_pcie);
961 pm_runtime_enable(dev);
962 ret = pm_runtime_get_sync(dev);
964 dev_err(dev, "pm_runtime_get_sync failed\n");
968 ret = ks_pcie_add_pcie_port(ks_pcie, pdev);
976 pm_runtime_disable(dev);
977 ks_pcie_disable_phy(ks_pcie);
980 while (--i >= 0 && link[i])
981 device_link_del(link[i]);
986 static int __exit ks_pcie_remove(struct platform_device *pdev)
988 struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
989 struct device_link **link = ks_pcie->link;
990 int num_lanes = ks_pcie->num_lanes;
991 struct device *dev = &pdev->dev;
994 pm_runtime_disable(dev);
995 ks_pcie_disable_phy(ks_pcie);
997 device_link_del(link[num_lanes]);
1002 static struct platform_driver ks_pcie_driver __refdata = {
1003 .probe = ks_pcie_probe,
1004 .remove = __exit_p(ks_pcie_remove),
1006 .name = "keystone-pcie",
1007 .of_match_table = of_match_ptr(ks_pcie_of_match),
1010 builtin_platform_driver(ks_pcie_driver);