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/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/irqchip/chained_irq.h>
18 #include <linux/irqdomain.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/msi.h>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_pci.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
28 #include <linux/resource.h>
29 #include <linux/signal.h>
31 #include "../../pci.h"
32 #include "pcie-designware.h"
34 #define PCIE_VENDORID_MASK 0xffff
35 #define PCIE_DEVICEID_SHIFT 16
37 /* Application registers */
38 #define CMD_STATUS 0x004
39 #define LTSSM_EN_VAL BIT(0)
40 #define OB_XLAT_EN_VAL BIT(1)
41 #define DBI_CS2 BIT(5)
43 #define CFG_SETUP 0x008
44 #define CFG_BUS(x) (((x) & 0xff) << 16)
45 #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
46 #define CFG_FUNC(x) ((x) & 0x7)
47 #define CFG_TYPE1 BIT(24)
50 #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
51 #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
52 #define OB_ENABLEN BIT(0)
53 #define OB_WIN_SIZE 8 /* 8MB */
55 #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1)))
56 #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1)))
57 #define PCIE_EP_IRQ_SET 0x64
58 #define PCIE_EP_IRQ_CLR 0x68
59 #define INT_ENABLE BIT(0)
61 /* IRQ register defines */
65 #define MSI_IRQ_STATUS(n) (0x104 + ((n) << 4))
66 #define MSI_IRQ_ENABLE_SET(n) (0x108 + ((n) << 4))
67 #define MSI_IRQ_ENABLE_CLR(n) (0x10c + ((n) << 4))
68 #define MSI_IRQ_OFFSET 4
70 #define IRQ_STATUS(n) (0x184 + ((n) << 4))
71 #define IRQ_ENABLE_SET(n) (0x188 + ((n) << 4))
72 #define INTx_EN BIT(0)
74 #define ERR_IRQ_STATUS 0x1c4
75 #define ERR_IRQ_ENABLE_SET 0x1c8
76 #define ERR_AER BIT(5) /* ECRC error */
77 #define AM6_ERR_AER BIT(4) /* AM6 ECRC error */
78 #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
79 #define ERR_CORR BIT(3) /* Correctable error */
80 #define ERR_NONFATAL BIT(2) /* Non-fatal error */
81 #define ERR_FATAL BIT(1) /* Fatal error */
82 #define ERR_SYS BIT(0) /* System error */
83 #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
84 ERR_NONFATAL | ERR_FATAL | ERR_SYS)
86 /* PCIE controller device IDs */
87 #define PCIE_RC_K2HK 0xb008
88 #define PCIE_RC_K2E 0xb009
89 #define PCIE_RC_K2L 0xb00a
90 #define PCIE_RC_K2G 0xb00b
92 #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1)
93 #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1)
99 #define EXP_CAP_ID_OFFSET 0x70
101 #define KS_PCIE_SYSCLOCKOUTEN BIT(0)
103 #define AM654_PCIE_DEV_TYPE_MASK 0x3
104 #define AM654_WIN_SIZE SZ_64K
106 #define APP_ADDR_SPACE_0 (16 * SZ_1K)
108 #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
110 struct ks_pcie_of_data
{
111 enum dw_pcie_device_mode mode
;
112 const struct dw_pcie_host_ops
*host_ops
;
113 const struct dw_pcie_ep_ops
*ep_ops
;
114 unsigned int version
;
117 struct keystone_pcie
{
121 int legacy_host_irqs
[PCI_NUM_INTX
];
122 struct device_node
*legacy_intc_np
;
128 struct device_link
**link
;
129 struct device_node
*msi_intc_np
;
130 struct irq_domain
*legacy_irq_domain
;
131 struct device_node
*np
;
133 /* Application register space */
134 void __iomem
*va_app_base
; /* DT 1st resource */
139 static u32
ks_pcie_app_readl(struct keystone_pcie
*ks_pcie
, u32 offset
)
141 return readl(ks_pcie
->va_app_base
+ offset
);
144 static void ks_pcie_app_writel(struct keystone_pcie
*ks_pcie
, u32 offset
,
147 writel(val
, ks_pcie
->va_app_base
+ offset
);
150 static void ks_pcie_msi_irq_ack(struct irq_data
*data
)
152 struct pcie_port
*pp
= irq_data_get_irq_chip_data(data
);
153 struct keystone_pcie
*ks_pcie
;
154 u32 irq
= data
->hwirq
;
159 pci
= to_dw_pcie_from_pp(pp
);
160 ks_pcie
= to_keystone_pcie(pci
);
162 reg_offset
= irq
% 8;
165 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_STATUS(reg_offset
),
167 ks_pcie_app_writel(ks_pcie
, IRQ_EOI
, reg_offset
+ MSI_IRQ_OFFSET
);
170 static void ks_pcie_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
172 struct pcie_port
*pp
= irq_data_get_irq_chip_data(data
);
173 struct keystone_pcie
*ks_pcie
;
177 pci
= to_dw_pcie_from_pp(pp
);
178 ks_pcie
= to_keystone_pcie(pci
);
180 msi_target
= ks_pcie
->app
.start
+ MSI_IRQ
;
181 msg
->address_lo
= lower_32_bits(msi_target
);
182 msg
->address_hi
= upper_32_bits(msi_target
);
183 msg
->data
= data
->hwirq
;
185 dev_dbg(pci
->dev
, "msi#%d address_hi %#x address_lo %#x\n",
186 (int)data
->hwirq
, msg
->address_hi
, msg
->address_lo
);
189 static int ks_pcie_msi_set_affinity(struct irq_data
*irq_data
,
190 const struct cpumask
*mask
, bool force
)
195 static void ks_pcie_msi_mask(struct irq_data
*data
)
197 struct pcie_port
*pp
= irq_data_get_irq_chip_data(data
);
198 struct keystone_pcie
*ks_pcie
;
199 u32 irq
= data
->hwirq
;
205 raw_spin_lock_irqsave(&pp
->lock
, flags
);
207 pci
= to_dw_pcie_from_pp(pp
);
208 ks_pcie
= to_keystone_pcie(pci
);
210 reg_offset
= irq
% 8;
213 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_ENABLE_CLR(reg_offset
),
216 raw_spin_unlock_irqrestore(&pp
->lock
, flags
);
219 static void ks_pcie_msi_unmask(struct irq_data
*data
)
221 struct pcie_port
*pp
= irq_data_get_irq_chip_data(data
);
222 struct keystone_pcie
*ks_pcie
;
223 u32 irq
= data
->hwirq
;
229 raw_spin_lock_irqsave(&pp
->lock
, flags
);
231 pci
= to_dw_pcie_from_pp(pp
);
232 ks_pcie
= to_keystone_pcie(pci
);
234 reg_offset
= irq
% 8;
237 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_ENABLE_SET(reg_offset
),
240 raw_spin_unlock_irqrestore(&pp
->lock
, flags
);
243 static struct irq_chip ks_pcie_msi_irq_chip
= {
244 .name
= "KEYSTONE-PCI-MSI",
245 .irq_ack
= ks_pcie_msi_irq_ack
,
246 .irq_compose_msi_msg
= ks_pcie_compose_msi_msg
,
247 .irq_set_affinity
= ks_pcie_msi_set_affinity
,
248 .irq_mask
= ks_pcie_msi_mask
,
249 .irq_unmask
= ks_pcie_msi_unmask
,
252 static int ks_pcie_msi_host_init(struct pcie_port
*pp
)
254 pp
->msi_irq_chip
= &ks_pcie_msi_irq_chip
;
255 return dw_pcie_allocate_domains(pp
);
258 static void ks_pcie_handle_legacy_irq(struct keystone_pcie
*ks_pcie
,
261 struct dw_pcie
*pci
= ks_pcie
->pci
;
262 struct device
*dev
= pci
->dev
;
266 pending
= ks_pcie_app_readl(ks_pcie
, IRQ_STATUS(offset
));
268 if (BIT(0) & pending
) {
269 virq
= irq_linear_revmap(ks_pcie
->legacy_irq_domain
, offset
);
270 dev_dbg(dev
, ": irq: irq_offset %d, virq %d\n", offset
, virq
);
271 generic_handle_irq(virq
);
274 /* EOI the INTx interrupt */
275 ks_pcie_app_writel(ks_pcie
, IRQ_EOI
, offset
);
279 * Dummy function so that DW core doesn't configure MSI
281 static int ks_pcie_am654_msi_host_init(struct pcie_port
*pp
)
286 static void ks_pcie_enable_error_irq(struct keystone_pcie
*ks_pcie
)
288 ks_pcie_app_writel(ks_pcie
, ERR_IRQ_ENABLE_SET
, ERR_IRQ_ALL
);
291 static irqreturn_t
ks_pcie_handle_error_irq(struct keystone_pcie
*ks_pcie
)
294 struct device
*dev
= ks_pcie
->pci
->dev
;
296 reg
= ks_pcie_app_readl(ks_pcie
, ERR_IRQ_STATUS
);
301 dev_err(dev
, "System Error\n");
304 dev_err(dev
, "Fatal Error\n");
306 if (reg
& ERR_NONFATAL
)
307 dev_dbg(dev
, "Non Fatal Error\n");
310 dev_dbg(dev
, "Correctable Error\n");
312 if (!ks_pcie
->is_am6
&& (reg
& ERR_AXI
))
313 dev_err(dev
, "AXI tag lookup fatal Error\n");
315 if (reg
& ERR_AER
|| (ks_pcie
->is_am6
&& (reg
& AM6_ERR_AER
)))
316 dev_err(dev
, "ECRC Error\n");
318 ks_pcie_app_writel(ks_pcie
, ERR_IRQ_STATUS
, reg
);
323 static void ks_pcie_ack_legacy_irq(struct irq_data
*d
)
327 static void ks_pcie_mask_legacy_irq(struct irq_data
*d
)
331 static void ks_pcie_unmask_legacy_irq(struct irq_data
*d
)
335 static struct irq_chip ks_pcie_legacy_irq_chip
= {
336 .name
= "Keystone-PCI-Legacy-IRQ",
337 .irq_ack
= ks_pcie_ack_legacy_irq
,
338 .irq_mask
= ks_pcie_mask_legacy_irq
,
339 .irq_unmask
= ks_pcie_unmask_legacy_irq
,
342 static int ks_pcie_init_legacy_irq_map(struct irq_domain
*d
,
344 irq_hw_number_t hw_irq
)
346 irq_set_chip_and_handler(irq
, &ks_pcie_legacy_irq_chip
,
348 irq_set_chip_data(irq
, d
->host_data
);
353 static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops
= {
354 .map
= ks_pcie_init_legacy_irq_map
,
355 .xlate
= irq_domain_xlate_onetwocell
,
359 * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
362 * Since modification of dbi_cs2 involves different clock domain, read the
363 * status back to ensure the transition is complete.
365 static void ks_pcie_set_dbi_mode(struct keystone_pcie
*ks_pcie
)
369 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
371 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
374 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
375 } while (!(val
& DBI_CS2
));
379 * ks_pcie_clear_dbi_mode() - Disable DBI mode
381 * Since modification of dbi_cs2 involves different clock domain, read the
382 * status back to ensure the transition is complete.
384 static void ks_pcie_clear_dbi_mode(struct keystone_pcie
*ks_pcie
)
388 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
390 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
393 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
394 } while (val
& DBI_CS2
);
397 static void ks_pcie_setup_rc_app_regs(struct keystone_pcie
*ks_pcie
)
400 u32 num_viewport
= ks_pcie
->num_viewport
;
401 struct dw_pcie
*pci
= ks_pcie
->pci
;
402 struct pcie_port
*pp
= &pci
->pp
;
403 u64 start
= pp
->mem
->start
;
404 u64 end
= pp
->mem
->end
;
407 /* Disable BARs for inbound access */
408 ks_pcie_set_dbi_mode(ks_pcie
);
409 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 0);
410 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_1
, 0);
411 ks_pcie_clear_dbi_mode(ks_pcie
);
416 val
= ilog2(OB_WIN_SIZE
);
417 ks_pcie_app_writel(ks_pcie
, OB_SIZE
, val
);
419 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
420 for (i
= 0; i
< num_viewport
&& (start
< end
); i
++) {
421 ks_pcie_app_writel(ks_pcie
, OB_OFFSET_INDEX(i
),
422 lower_32_bits(start
) | OB_ENABLEN
);
423 ks_pcie_app_writel(ks_pcie
, OB_OFFSET_HI(i
),
424 upper_32_bits(start
));
425 start
+= OB_WIN_SIZE
* SZ_1M
;
428 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
429 val
|= OB_XLAT_EN_VAL
;
430 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
433 static int ks_pcie_rd_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
434 unsigned int devfn
, int where
, int size
,
437 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
438 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
441 reg
= CFG_BUS(bus
->number
) | CFG_DEVICE(PCI_SLOT(devfn
)) |
442 CFG_FUNC(PCI_FUNC(devfn
));
443 if (bus
->parent
->number
!= pp
->root_bus_nr
)
445 ks_pcie_app_writel(ks_pcie
, CFG_SETUP
, reg
);
447 return dw_pcie_read(pp
->va_cfg0_base
+ where
, size
, val
);
450 static int ks_pcie_wr_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
451 unsigned int devfn
, int where
, int size
,
454 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
455 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
458 reg
= CFG_BUS(bus
->number
) | CFG_DEVICE(PCI_SLOT(devfn
)) |
459 CFG_FUNC(PCI_FUNC(devfn
));
460 if (bus
->parent
->number
!= pp
->root_bus_nr
)
462 ks_pcie_app_writel(ks_pcie
, CFG_SETUP
, reg
);
464 return dw_pcie_write(pp
->va_cfg0_base
+ where
, size
, val
);
468 * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
470 * This sets BAR0 to enable inbound access for MSI_IRQ register
472 static void ks_pcie_v3_65_scan_bus(struct pcie_port
*pp
)
474 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
475 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
477 /* Configure and set up BAR0 */
478 ks_pcie_set_dbi_mode(ks_pcie
);
481 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 1);
482 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, SZ_4K
- 1);
484 ks_pcie_clear_dbi_mode(ks_pcie
);
487 * For BAR0, just setting bus address for inbound writes (MSI) should
488 * be sufficient. Use physical address to avoid any conflicts.
490 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, ks_pcie
->app
.start
);
494 * ks_pcie_link_up() - Check if link up
496 static int ks_pcie_link_up(struct dw_pcie
*pci
)
500 val
= dw_pcie_readl_dbi(pci
, PCIE_PORT_DEBUG0
);
501 val
&= PORT_LOGIC_LTSSM_STATE_MASK
;
502 return (val
== PORT_LOGIC_LTSSM_STATE_L0
);
505 static void ks_pcie_stop_link(struct dw_pcie
*pci
)
507 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
510 /* Disable Link training */
511 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
512 val
&= ~LTSSM_EN_VAL
;
513 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
516 static int ks_pcie_start_link(struct dw_pcie
*pci
)
518 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
519 struct device
*dev
= pci
->dev
;
522 if (dw_pcie_link_up(pci
)) {
523 dev_dbg(dev
, "link is already up\n");
527 /* Initiate Link Training */
528 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
529 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, LTSSM_EN_VAL
| val
);
534 static void ks_pcie_quirk(struct pci_dev
*dev
)
536 struct pci_bus
*bus
= dev
->bus
;
537 struct pci_dev
*bridge
;
538 static const struct pci_device_id rc_pci_devids
[] = {
539 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2HK
),
540 .class = PCI_CLASS_BRIDGE_PCI
<< 8, .class_mask
= ~0, },
541 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2E
),
542 .class = PCI_CLASS_BRIDGE_PCI
<< 8, .class_mask
= ~0, },
543 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2L
),
544 .class = PCI_CLASS_BRIDGE_PCI
<< 8, .class_mask
= ~0, },
545 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2G
),
546 .class = PCI_CLASS_BRIDGE_PCI
<< 8, .class_mask
= ~0, },
550 if (pci_is_root_bus(bus
))
553 /* look for the host bridge */
554 while (!pci_is_root_bus(bus
)) {
563 * Keystone PCI controller has a h/w limitation of
564 * 256 bytes maximum read request size. It can't handle
565 * anything higher than this. So force this limit on
566 * all downstream devices.
568 if (pci_match_id(rc_pci_devids
, bridge
)) {
569 if (pcie_get_readrq(dev
) > 256) {
570 dev_info(&dev
->dev
, "limiting MRRS to 256\n");
571 pcie_set_readrq(dev
, 256);
575 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID
, PCI_ANY_ID
, ks_pcie_quirk
);
577 static void ks_pcie_msi_irq_handler(struct irq_desc
*desc
)
579 unsigned int irq
= desc
->irq_data
.hwirq
;
580 struct keystone_pcie
*ks_pcie
= irq_desc_get_handler_data(desc
);
581 u32 offset
= irq
- ks_pcie
->msi_host_irq
;
582 struct dw_pcie
*pci
= ks_pcie
->pci
;
583 struct pcie_port
*pp
= &pci
->pp
;
584 struct device
*dev
= pci
->dev
;
585 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
586 u32 vector
, virq
, reg
, pos
;
588 dev_dbg(dev
, "%s, irq %d\n", __func__
, irq
);
591 * The chained irq handler installation would have replaced normal
592 * interrupt driver handler so we need to take care of mask/unmask and
595 chained_irq_enter(chip
, desc
);
597 reg
= ks_pcie_app_readl(ks_pcie
, MSI_IRQ_STATUS(offset
));
599 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
600 * shows 1, 9, 17, 25 and so forth
602 for (pos
= 0; pos
< 4; pos
++) {
603 if (!(reg
& BIT(pos
)))
606 vector
= offset
+ (pos
<< 3);
607 virq
= irq_linear_revmap(pp
->irq_domain
, vector
);
608 dev_dbg(dev
, "irq: bit %d, vector %d, virq %d\n", pos
, vector
,
610 generic_handle_irq(virq
);
613 chained_irq_exit(chip
, desc
);
617 * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
618 * @irq: IRQ line for legacy interrupts
619 * @desc: Pointer to irq descriptor
621 * Traverse through pending legacy interrupts and invoke handler for each. Also
622 * takes care of interrupt controller level mask/ack operation.
624 static void ks_pcie_legacy_irq_handler(struct irq_desc
*desc
)
626 unsigned int irq
= irq_desc_get_irq(desc
);
627 struct keystone_pcie
*ks_pcie
= irq_desc_get_handler_data(desc
);
628 struct dw_pcie
*pci
= ks_pcie
->pci
;
629 struct device
*dev
= pci
->dev
;
630 u32 irq_offset
= irq
- ks_pcie
->legacy_host_irqs
[0];
631 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
633 dev_dbg(dev
, ": Handling legacy irq %d\n", irq
);
636 * The chained irq handler installation would have replaced normal
637 * interrupt driver handler so we need to take care of mask/unmask and
640 chained_irq_enter(chip
, desc
);
641 ks_pcie_handle_legacy_irq(ks_pcie
, irq_offset
);
642 chained_irq_exit(chip
, desc
);
645 static int ks_pcie_config_msi_irq(struct keystone_pcie
*ks_pcie
)
647 struct device
*dev
= ks_pcie
->pci
->dev
;
648 struct device_node
*np
= ks_pcie
->np
;
649 struct device_node
*intc_np
;
650 struct irq_data
*irq_data
;
651 int irq_count
, irq
, ret
, i
;
653 if (!IS_ENABLED(CONFIG_PCI_MSI
))
656 intc_np
= of_get_child_by_name(np
, "msi-interrupt-controller");
660 dev_warn(dev
, "msi-interrupt-controller node is absent\n");
664 irq_count
= of_irq_count(intc_np
);
666 dev_err(dev
, "No IRQ entries in msi-interrupt-controller\n");
671 for (i
= 0; i
< irq_count
; i
++) {
672 irq
= irq_of_parse_and_map(intc_np
, i
);
678 if (!ks_pcie
->msi_host_irq
) {
679 irq_data
= irq_get_irq_data(irq
);
684 ks_pcie
->msi_host_irq
= irq_data
->hwirq
;
687 irq_set_chained_handler_and_data(irq
, ks_pcie_msi_irq_handler
,
691 of_node_put(intc_np
);
695 of_node_put(intc_np
);
699 static int ks_pcie_config_legacy_irq(struct keystone_pcie
*ks_pcie
)
701 struct device
*dev
= ks_pcie
->pci
->dev
;
702 struct irq_domain
*legacy_irq_domain
;
703 struct device_node
*np
= ks_pcie
->np
;
704 struct device_node
*intc_np
;
705 int irq_count
, irq
, ret
= 0, i
;
707 intc_np
= of_get_child_by_name(np
, "legacy-interrupt-controller");
710 * Since legacy interrupts are modeled as edge-interrupts in
711 * AM6, keep it disabled for now.
715 dev_warn(dev
, "legacy-interrupt-controller node is absent\n");
719 irq_count
= of_irq_count(intc_np
);
721 dev_err(dev
, "No IRQ entries in legacy-interrupt-controller\n");
726 for (i
= 0; i
< irq_count
; i
++) {
727 irq
= irq_of_parse_and_map(intc_np
, i
);
732 ks_pcie
->legacy_host_irqs
[i
] = irq
;
734 irq_set_chained_handler_and_data(irq
,
735 ks_pcie_legacy_irq_handler
,
740 irq_domain_add_linear(intc_np
, PCI_NUM_INTX
,
741 &ks_pcie_legacy_irq_domain_ops
, NULL
);
742 if (!legacy_irq_domain
) {
743 dev_err(dev
, "Failed to add irq domain for legacy irqs\n");
747 ks_pcie
->legacy_irq_domain
= legacy_irq_domain
;
749 for (i
= 0; i
< PCI_NUM_INTX
; i
++)
750 ks_pcie_app_writel(ks_pcie
, IRQ_ENABLE_SET(i
), INTx_EN
);
753 of_node_put(intc_np
);
759 * When a PCI device does not exist during config cycles, keystone host gets a
760 * bus error instead of returning 0xffffffff. This handler always returns 0
761 * for this kind of faults.
763 static int ks_pcie_fault(unsigned long addr
, unsigned int fsr
,
764 struct pt_regs
*regs
)
766 unsigned long instr
= *(unsigned long *) instruction_pointer(regs
);
768 if ((instr
& 0x0e100090) == 0x00100090) {
769 int reg
= (instr
>> 12) & 15;
771 regs
->uregs
[reg
] = -1;
779 static int __init
ks_pcie_init_id(struct keystone_pcie
*ks_pcie
)
783 struct regmap
*devctrl_regs
;
784 struct dw_pcie
*pci
= ks_pcie
->pci
;
785 struct device
*dev
= pci
->dev
;
786 struct device_node
*np
= dev
->of_node
;
788 devctrl_regs
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-id");
789 if (IS_ERR(devctrl_regs
))
790 return PTR_ERR(devctrl_regs
);
792 ret
= regmap_read(devctrl_regs
, 0, &id
);
796 dw_pcie_dbi_ro_wr_en(pci
);
797 dw_pcie_writew_dbi(pci
, PCI_VENDOR_ID
, id
& PCIE_VENDORID_MASK
);
798 dw_pcie_writew_dbi(pci
, PCI_DEVICE_ID
, id
>> PCIE_DEVICEID_SHIFT
);
799 dw_pcie_dbi_ro_wr_dis(pci
);
804 static int __init
ks_pcie_host_init(struct pcie_port
*pp
)
806 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
807 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
810 ret
= ks_pcie_config_legacy_irq(ks_pcie
);
814 ret
= ks_pcie_config_msi_irq(ks_pcie
);
818 dw_pcie_setup_rc(pp
);
820 ks_pcie_stop_link(pci
);
821 ks_pcie_setup_rc_app_regs(ks_pcie
);
822 writew(PCI_IO_RANGE_TYPE_32
| (PCI_IO_RANGE_TYPE_32
<< 8),
823 pci
->dbi_base
+ PCI_IO_BASE
);
825 ret
= ks_pcie_init_id(ks_pcie
);
831 * PCIe access errors that result into OCP errors are caught by ARM as
834 hook_fault_code(17, ks_pcie_fault
, SIGBUS
, 0,
835 "Asynchronous external abort");
838 ks_pcie_start_link(pci
);
839 dw_pcie_wait_for_link(pci
);
844 static const struct dw_pcie_host_ops ks_pcie_host_ops
= {
845 .rd_other_conf
= ks_pcie_rd_other_conf
,
846 .wr_other_conf
= ks_pcie_wr_other_conf
,
847 .host_init
= ks_pcie_host_init
,
848 .msi_host_init
= ks_pcie_msi_host_init
,
849 .scan_bus
= ks_pcie_v3_65_scan_bus
,
852 static const struct dw_pcie_host_ops ks_pcie_am654_host_ops
= {
853 .host_init
= ks_pcie_host_init
,
854 .msi_host_init
= ks_pcie_am654_msi_host_init
,
857 static irqreturn_t
ks_pcie_err_irq_handler(int irq
, void *priv
)
859 struct keystone_pcie
*ks_pcie
= priv
;
861 return ks_pcie_handle_error_irq(ks_pcie
);
864 static int __init
ks_pcie_add_pcie_port(struct keystone_pcie
*ks_pcie
,
865 struct platform_device
*pdev
)
867 struct dw_pcie
*pci
= ks_pcie
->pci
;
868 struct pcie_port
*pp
= &pci
->pp
;
869 struct device
*dev
= &pdev
->dev
;
870 struct resource
*res
;
873 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "config");
874 pp
->va_cfg0_base
= devm_pci_remap_cfg_resource(dev
, res
);
875 if (IS_ERR(pp
->va_cfg0_base
))
876 return PTR_ERR(pp
->va_cfg0_base
);
878 pp
->va_cfg1_base
= pp
->va_cfg0_base
;
880 ret
= dw_pcie_host_init(pp
);
882 dev_err(dev
, "failed to initialize host\n");
889 static u32
ks_pcie_am654_read_dbi2(struct dw_pcie
*pci
, void __iomem
*base
,
890 u32 reg
, size_t size
)
892 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
895 ks_pcie_set_dbi_mode(ks_pcie
);
896 dw_pcie_read(base
+ reg
, size
, &val
);
897 ks_pcie_clear_dbi_mode(ks_pcie
);
901 static void ks_pcie_am654_write_dbi2(struct dw_pcie
*pci
, void __iomem
*base
,
902 u32 reg
, size_t size
, u32 val
)
904 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
906 ks_pcie_set_dbi_mode(ks_pcie
);
907 dw_pcie_write(base
+ reg
, size
, val
);
908 ks_pcie_clear_dbi_mode(ks_pcie
);
911 static const struct dw_pcie_ops ks_pcie_dw_pcie_ops
= {
912 .start_link
= ks_pcie_start_link
,
913 .stop_link
= ks_pcie_stop_link
,
914 .link_up
= ks_pcie_link_up
,
915 .read_dbi2
= ks_pcie_am654_read_dbi2
,
916 .write_dbi2
= ks_pcie_am654_write_dbi2
,
919 static void ks_pcie_am654_ep_init(struct dw_pcie_ep
*ep
)
921 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
924 ep
->page_size
= AM654_WIN_SIZE
;
925 flags
= PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_32
;
926 dw_pcie_writel_dbi2(pci
, PCI_BASE_ADDRESS_0
, APP_ADDR_SPACE_0
- 1);
927 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, flags
);
930 static void ks_pcie_am654_raise_legacy_irq(struct keystone_pcie
*ks_pcie
)
932 struct dw_pcie
*pci
= ks_pcie
->pci
;
935 int_pin
= dw_pcie_readb_dbi(pci
, PCI_INTERRUPT_PIN
);
936 if (int_pin
== 0 || int_pin
> 4)
939 ks_pcie_app_writel(ks_pcie
, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin
),
941 ks_pcie_app_writel(ks_pcie
, PCIE_EP_IRQ_SET
, INT_ENABLE
);
943 ks_pcie_app_writel(ks_pcie
, PCIE_EP_IRQ_CLR
, INT_ENABLE
);
944 ks_pcie_app_writel(ks_pcie
, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin
),
948 static int ks_pcie_am654_raise_irq(struct dw_pcie_ep
*ep
, u8 func_no
,
949 enum pci_epc_irq_type type
,
952 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
953 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
956 case PCI_EPC_IRQ_LEGACY
:
957 ks_pcie_am654_raise_legacy_irq(ks_pcie
);
959 case PCI_EPC_IRQ_MSI
:
960 dw_pcie_ep_raise_msi_irq(ep
, func_no
, interrupt_num
);
963 dev_err(pci
->dev
, "UNKNOWN IRQ type\n");
970 static const struct pci_epc_features ks_pcie_am654_epc_features
= {
971 .linkup_notifier
= false,
973 .msix_capable
= false,
974 .reserved_bar
= 1 << BAR_0
| 1 << BAR_1
,
975 .bar_fixed_64bit
= 1 << BAR_0
,
976 .bar_fixed_size
[2] = SZ_1M
,
977 .bar_fixed_size
[3] = SZ_64K
,
978 .bar_fixed_size
[4] = 256,
979 .bar_fixed_size
[5] = SZ_1M
,
983 static const struct pci_epc_features
*
984 ks_pcie_am654_get_features(struct dw_pcie_ep
*ep
)
986 return &ks_pcie_am654_epc_features
;
989 static const struct dw_pcie_ep_ops ks_pcie_am654_ep_ops
= {
990 .ep_init
= ks_pcie_am654_ep_init
,
991 .raise_irq
= ks_pcie_am654_raise_irq
,
992 .get_features
= &ks_pcie_am654_get_features
,
995 static int __init
ks_pcie_add_pcie_ep(struct keystone_pcie
*ks_pcie
,
996 struct platform_device
*pdev
)
999 struct dw_pcie_ep
*ep
;
1000 struct resource
*res
;
1001 struct device
*dev
= &pdev
->dev
;
1002 struct dw_pcie
*pci
= ks_pcie
->pci
;
1006 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "addr_space");
1010 ep
->phys_base
= res
->start
;
1011 ep
->addr_size
= resource_size(res
);
1013 ret
= dw_pcie_ep_init(ep
);
1015 dev_err(dev
, "failed to initialize endpoint\n");
1022 static void ks_pcie_disable_phy(struct keystone_pcie
*ks_pcie
)
1024 int num_lanes
= ks_pcie
->num_lanes
;
1026 while (num_lanes
--) {
1027 phy_power_off(ks_pcie
->phy
[num_lanes
]);
1028 phy_exit(ks_pcie
->phy
[num_lanes
]);
1032 static int ks_pcie_enable_phy(struct keystone_pcie
*ks_pcie
)
1036 int num_lanes
= ks_pcie
->num_lanes
;
1038 for (i
= 0; i
< num_lanes
; i
++) {
1039 ret
= phy_reset(ks_pcie
->phy
[i
]);
1043 ret
= phy_init(ks_pcie
->phy
[i
]);
1047 ret
= phy_power_on(ks_pcie
->phy
[i
]);
1049 phy_exit(ks_pcie
->phy
[i
]);
1058 phy_power_off(ks_pcie
->phy
[i
]);
1059 phy_exit(ks_pcie
->phy
[i
]);
1065 static int ks_pcie_set_mode(struct device
*dev
)
1067 struct device_node
*np
= dev
->of_node
;
1068 struct regmap
*syscon
;
1073 syscon
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-mode");
1077 mask
= KS_PCIE_DEV_TYPE_MASK
| KS_PCIE_SYSCLOCKOUTEN
;
1078 val
= KS_PCIE_DEV_TYPE(RC
) | KS_PCIE_SYSCLOCKOUTEN
;
1080 ret
= regmap_update_bits(syscon
, 0, mask
, val
);
1082 dev_err(dev
, "failed to set pcie mode\n");
1089 static int ks_pcie_am654_set_mode(struct device
*dev
,
1090 enum dw_pcie_device_mode mode
)
1092 struct device_node
*np
= dev
->of_node
;
1093 struct regmap
*syscon
;
1098 syscon
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-mode");
1102 mask
= AM654_PCIE_DEV_TYPE_MASK
;
1105 case DW_PCIE_RC_TYPE
:
1108 case DW_PCIE_EP_TYPE
:
1112 dev_err(dev
, "INVALID device type %d\n", mode
);
1116 ret
= regmap_update_bits(syscon
, 0, mask
, val
);
1118 dev_err(dev
, "failed to set pcie mode\n");
1125 static void ks_pcie_set_link_speed(struct dw_pcie
*pci
, int link_speed
)
1129 dw_pcie_dbi_ro_wr_en(pci
);
1131 val
= dw_pcie_readl_dbi(pci
, EXP_CAP_ID_OFFSET
+ PCI_EXP_LNKCAP
);
1132 if ((val
& PCI_EXP_LNKCAP_SLS
) != link_speed
) {
1133 val
&= ~((u32
)PCI_EXP_LNKCAP_SLS
);
1135 dw_pcie_writel_dbi(pci
, EXP_CAP_ID_OFFSET
+ PCI_EXP_LNKCAP
,
1139 val
= dw_pcie_readl_dbi(pci
, EXP_CAP_ID_OFFSET
+ PCI_EXP_LNKCTL2
);
1140 if ((val
& PCI_EXP_LNKCAP_SLS
) != link_speed
) {
1141 val
&= ~((u32
)PCI_EXP_LNKCAP_SLS
);
1143 dw_pcie_writel_dbi(pci
, EXP_CAP_ID_OFFSET
+ PCI_EXP_LNKCTL2
,
1147 dw_pcie_dbi_ro_wr_dis(pci
);
1150 static const struct ks_pcie_of_data ks_pcie_rc_of_data
= {
1151 .host_ops
= &ks_pcie_host_ops
,
1155 static const struct ks_pcie_of_data ks_pcie_am654_rc_of_data
= {
1156 .host_ops
= &ks_pcie_am654_host_ops
,
1157 .mode
= DW_PCIE_RC_TYPE
,
1161 static const struct ks_pcie_of_data ks_pcie_am654_ep_of_data
= {
1162 .ep_ops
= &ks_pcie_am654_ep_ops
,
1163 .mode
= DW_PCIE_EP_TYPE
,
1167 static const struct of_device_id ks_pcie_of_match
[] = {
1170 .data
= &ks_pcie_rc_of_data
,
1171 .compatible
= "ti,keystone-pcie",
1174 .data
= &ks_pcie_am654_rc_of_data
,
1175 .compatible
= "ti,am654-pcie-rc",
1178 .data
= &ks_pcie_am654_ep_of_data
,
1179 .compatible
= "ti,am654-pcie-ep",
1184 static int __init
ks_pcie_probe(struct platform_device
*pdev
)
1186 const struct dw_pcie_host_ops
*host_ops
;
1187 const struct dw_pcie_ep_ops
*ep_ops
;
1188 struct device
*dev
= &pdev
->dev
;
1189 struct device_node
*np
= dev
->of_node
;
1190 const struct ks_pcie_of_data
*data
;
1191 const struct of_device_id
*match
;
1192 enum dw_pcie_device_mode mode
;
1193 struct dw_pcie
*pci
;
1194 struct keystone_pcie
*ks_pcie
;
1195 struct device_link
**link
;
1196 struct gpio_desc
*gpiod
;
1197 void __iomem
*atu_base
;
1198 struct resource
*res
;
1199 unsigned int version
;
1210 match
= of_match_device(of_match_ptr(ks_pcie_of_match
), dev
);
1211 data
= (struct ks_pcie_of_data
*)match
->data
;
1215 version
= data
->version
;
1216 host_ops
= data
->host_ops
;
1217 ep_ops
= data
->ep_ops
;
1220 ks_pcie
= devm_kzalloc(dev
, sizeof(*ks_pcie
), GFP_KERNEL
);
1224 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
1228 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "app");
1229 ks_pcie
->va_app_base
= devm_ioremap_resource(dev
, res
);
1230 if (IS_ERR(ks_pcie
->va_app_base
))
1231 return PTR_ERR(ks_pcie
->va_app_base
);
1233 ks_pcie
->app
= *res
;
1235 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbics");
1236 base
= devm_pci_remap_cfg_resource(dev
, res
);
1238 return PTR_ERR(base
);
1240 if (of_device_is_compatible(np
, "ti,am654-pcie-rc"))
1241 ks_pcie
->is_am6
= true;
1243 pci
->dbi_base
= base
;
1244 pci
->dbi_base2
= base
;
1246 pci
->ops
= &ks_pcie_dw_pcie_ops
;
1247 pci
->version
= version
;
1249 irq
= platform_get_irq(pdev
, 0);
1251 dev_err(dev
, "missing IRQ resource: %d\n", irq
);
1255 ret
= request_irq(irq
, ks_pcie_err_irq_handler
, IRQF_SHARED
,
1256 "ks-pcie-error-irq", ks_pcie
);
1258 dev_err(dev
, "failed to request error IRQ %d\n",
1263 ret
= of_property_read_u32(np
, "num-lanes", &num_lanes
);
1267 phy
= devm_kzalloc(dev
, sizeof(*phy
) * num_lanes
, GFP_KERNEL
);
1271 link
= devm_kzalloc(dev
, sizeof(*link
) * num_lanes
, GFP_KERNEL
);
1275 for (i
= 0; i
< num_lanes
; i
++) {
1276 snprintf(name
, sizeof(name
), "pcie-phy%d", i
);
1277 phy
[i
] = devm_phy_optional_get(dev
, name
);
1278 if (IS_ERR(phy
[i
])) {
1279 ret
= PTR_ERR(phy
[i
]);
1286 link
[i
] = device_link_add(dev
, &phy
[i
]->dev
, DL_FLAG_STATELESS
);
1295 ks_pcie
->link
= link
;
1296 ks_pcie
->num_lanes
= num_lanes
;
1299 gpiod
= devm_gpiod_get_optional(dev
, "reset",
1301 if (IS_ERR(gpiod
)) {
1302 ret
= PTR_ERR(gpiod
);
1303 if (ret
!= -EPROBE_DEFER
)
1304 dev_err(dev
, "Failed to get reset GPIO\n");
1308 ret
= ks_pcie_enable_phy(ks_pcie
);
1310 dev_err(dev
, "failed to enable phy\n");
1314 platform_set_drvdata(pdev
, ks_pcie
);
1315 pm_runtime_enable(dev
);
1316 ret
= pm_runtime_get_sync(dev
);
1318 dev_err(dev
, "pm_runtime_get_sync failed\n");
1322 if (pci
->version
>= 0x480A) {
1323 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "atu");
1324 atu_base
= devm_ioremap_resource(dev
, res
);
1325 if (IS_ERR(atu_base
)) {
1326 ret
= PTR_ERR(atu_base
);
1330 pci
->atu_base
= atu_base
;
1332 ret
= ks_pcie_am654_set_mode(dev
, mode
);
1336 ret
= ks_pcie_set_mode(dev
);
1341 link_speed
= of_pci_get_max_link_speed(np
);
1345 ks_pcie_set_link_speed(pci
, link_speed
);
1348 case DW_PCIE_RC_TYPE
:
1349 if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_HOST
)) {
1354 ret
= of_property_read_u32(np
, "num-viewport", &num_viewport
);
1356 dev_err(dev
, "unable to read *num-viewport* property\n");
1361 * "Power Sequencing and Reset Signal Timings" table in
1362 * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 2.0
1363 * indicates PERST# should be deasserted after minimum of 100us
1364 * once REFCLK is stable. The REFCLK to the connector in RC
1365 * mode is selected while enabling the PHY. So deassert PERST#
1369 usleep_range(100, 200);
1370 gpiod_set_value_cansleep(gpiod
, 1);
1373 ks_pcie
->num_viewport
= num_viewport
;
1374 pci
->pp
.ops
= host_ops
;
1375 ret
= ks_pcie_add_pcie_port(ks_pcie
, pdev
);
1379 case DW_PCIE_EP_TYPE
:
1380 if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_EP
)) {
1385 pci
->ep
.ops
= ep_ops
;
1386 ret
= ks_pcie_add_pcie_ep(ks_pcie
, pdev
);
1391 dev_err(dev
, "INVALID device type %d\n", mode
);
1394 ks_pcie_enable_error_irq(ks_pcie
);
1399 pm_runtime_put(dev
);
1400 pm_runtime_disable(dev
);
1401 ks_pcie_disable_phy(ks_pcie
);
1404 while (--i
>= 0 && link
[i
])
1405 device_link_del(link
[i
]);
1410 static int __exit
ks_pcie_remove(struct platform_device
*pdev
)
1412 struct keystone_pcie
*ks_pcie
= platform_get_drvdata(pdev
);
1413 struct device_link
**link
= ks_pcie
->link
;
1414 int num_lanes
= ks_pcie
->num_lanes
;
1415 struct device
*dev
= &pdev
->dev
;
1417 pm_runtime_put(dev
);
1418 pm_runtime_disable(dev
);
1419 ks_pcie_disable_phy(ks_pcie
);
1421 device_link_del(link
[num_lanes
]);
1426 static struct platform_driver ks_pcie_driver __refdata
= {
1427 .probe
= ks_pcie_probe
,
1428 .remove
= __exit_p(ks_pcie_remove
),
1430 .name
= "keystone-pcie",
1431 .of_match_table
= of_match_ptr(ks_pcie_of_match
),
1434 builtin_platform_driver(ks_pcie_driver
);