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_irq.h>
23 #include <linux/of_pci.h>
24 #include <linux/phy/phy.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/resource.h>
28 #include <linux/signal.h>
30 #include "../../pci.h"
31 #include "pcie-designware.h"
33 #define PCIE_VENDORID_MASK 0xffff
34 #define PCIE_DEVICEID_SHIFT 16
36 /* Application registers */
38 #define RTL GENMASK(15, 11)
40 #define AM6_PCI_PG1_RTL_VER 0x15
42 #define CMD_STATUS 0x004
43 #define LTSSM_EN_VAL BIT(0)
44 #define OB_XLAT_EN_VAL BIT(1)
45 #define DBI_CS2 BIT(5)
47 #define CFG_SETUP 0x008
48 #define CFG_BUS(x) (((x) & 0xff) << 16)
49 #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
50 #define CFG_FUNC(x) ((x) & 0x7)
51 #define CFG_TYPE1 BIT(24)
54 #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
55 #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
56 #define OB_ENABLEN BIT(0)
57 #define OB_WIN_SIZE 8 /* 8MB */
59 #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1)))
60 #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1)))
61 #define PCIE_EP_IRQ_SET 0x64
62 #define PCIE_EP_IRQ_CLR 0x68
63 #define INT_ENABLE BIT(0)
65 /* IRQ register defines */
69 #define MSI_IRQ_STATUS(n) (0x104 + ((n) << 4))
70 #define MSI_IRQ_ENABLE_SET(n) (0x108 + ((n) << 4))
71 #define MSI_IRQ_ENABLE_CLR(n) (0x10c + ((n) << 4))
72 #define MSI_IRQ_OFFSET 4
74 #define IRQ_STATUS(n) (0x184 + ((n) << 4))
75 #define IRQ_ENABLE_SET(n) (0x188 + ((n) << 4))
76 #define INTx_EN BIT(0)
78 #define ERR_IRQ_STATUS 0x1c4
79 #define ERR_IRQ_ENABLE_SET 0x1c8
80 #define ERR_AER BIT(5) /* ECRC error */
81 #define AM6_ERR_AER BIT(4) /* AM6 ECRC error */
82 #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
83 #define ERR_CORR BIT(3) /* Correctable error */
84 #define ERR_NONFATAL BIT(2) /* Non-fatal error */
85 #define ERR_FATAL BIT(1) /* Fatal error */
86 #define ERR_SYS BIT(0) /* System error */
87 #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
88 ERR_NONFATAL | ERR_FATAL | ERR_SYS)
90 /* PCIE controller device IDs */
91 #define PCIE_RC_K2HK 0xb008
92 #define PCIE_RC_K2E 0xb009
93 #define PCIE_RC_K2L 0xb00a
94 #define PCIE_RC_K2G 0xb00b
96 #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1)
97 #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1)
103 #define KS_PCIE_SYSCLOCKOUTEN BIT(0)
105 #define AM654_PCIE_DEV_TYPE_MASK 0x3
106 #define AM654_WIN_SIZE SZ_64K
108 #define APP_ADDR_SPACE_0 (16 * SZ_1K)
110 #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
112 #define PCI_DEVICE_ID_TI_AM654X 0xb00c
114 struct ks_pcie_of_data
{
115 enum dw_pcie_device_mode mode
;
116 const struct dw_pcie_host_ops
*host_ops
;
117 const struct dw_pcie_ep_ops
*ep_ops
;
121 struct keystone_pcie
{
125 int intx_host_irqs
[PCI_NUM_INTX
];
131 struct device_link
**link
;
132 struct device_node
*msi_intc_np
;
133 struct irq_domain
*intx_irq_domain
;
134 struct device_node
*np
;
136 /* Application register space */
137 void __iomem
*va_app_base
; /* DT 1st resource */
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_msi_irq_ack(struct irq_data
*data
)
155 struct dw_pcie_rp
*pp
= irq_data_get_irq_chip_data(data
);
156 struct keystone_pcie
*ks_pcie
;
157 u32 irq
= data
->hwirq
;
162 pci
= to_dw_pcie_from_pp(pp
);
163 ks_pcie
= to_keystone_pcie(pci
);
165 reg_offset
= irq
% 8;
168 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_STATUS(reg_offset
),
170 ks_pcie_app_writel(ks_pcie
, IRQ_EOI
, reg_offset
+ MSI_IRQ_OFFSET
);
173 static void ks_pcie_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
175 struct dw_pcie_rp
*pp
= irq_data_get_irq_chip_data(data
);
176 struct keystone_pcie
*ks_pcie
;
180 pci
= to_dw_pcie_from_pp(pp
);
181 ks_pcie
= to_keystone_pcie(pci
);
183 msi_target
= ks_pcie
->app
.start
+ MSI_IRQ
;
184 msg
->address_lo
= lower_32_bits(msi_target
);
185 msg
->address_hi
= upper_32_bits(msi_target
);
186 msg
->data
= data
->hwirq
;
188 dev_dbg(pci
->dev
, "msi#%d address_hi %#x address_lo %#x\n",
189 (int)data
->hwirq
, msg
->address_hi
, msg
->address_lo
);
192 static void ks_pcie_msi_mask(struct irq_data
*data
)
194 struct dw_pcie_rp
*pp
= irq_data_get_irq_chip_data(data
);
195 struct keystone_pcie
*ks_pcie
;
196 u32 irq
= data
->hwirq
;
202 raw_spin_lock_irqsave(&pp
->lock
, flags
);
204 pci
= to_dw_pcie_from_pp(pp
);
205 ks_pcie
= to_keystone_pcie(pci
);
207 reg_offset
= irq
% 8;
210 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_ENABLE_CLR(reg_offset
),
213 raw_spin_unlock_irqrestore(&pp
->lock
, flags
);
216 static void ks_pcie_msi_unmask(struct irq_data
*data
)
218 struct dw_pcie_rp
*pp
= irq_data_get_irq_chip_data(data
);
219 struct keystone_pcie
*ks_pcie
;
220 u32 irq
= data
->hwirq
;
226 raw_spin_lock_irqsave(&pp
->lock
, flags
);
228 pci
= to_dw_pcie_from_pp(pp
);
229 ks_pcie
= to_keystone_pcie(pci
);
231 reg_offset
= irq
% 8;
234 ks_pcie_app_writel(ks_pcie
, MSI_IRQ_ENABLE_SET(reg_offset
),
237 raw_spin_unlock_irqrestore(&pp
->lock
, flags
);
240 static struct irq_chip ks_pcie_msi_irq_chip
= {
241 .name
= "KEYSTONE-PCI-MSI",
242 .irq_ack
= ks_pcie_msi_irq_ack
,
243 .irq_compose_msi_msg
= ks_pcie_compose_msi_msg
,
244 .irq_mask
= ks_pcie_msi_mask
,
245 .irq_unmask
= ks_pcie_msi_unmask
,
249 * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask registers
250 * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone
251 * PCIe host controller driver information.
253 * Since modification of dbi_cs2 involves different clock domain, read the
254 * status back to ensure the transition is complete.
256 static void ks_pcie_set_dbi_mode(struct keystone_pcie
*ks_pcie
)
260 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
262 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
265 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
266 } while (!(val
& DBI_CS2
));
270 * ks_pcie_clear_dbi_mode() - Disable DBI mode
271 * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone
272 * PCIe host controller driver information.
274 * Since modification of dbi_cs2 involves different clock domain, read the
275 * status back to ensure the transition is complete.
277 static void ks_pcie_clear_dbi_mode(struct keystone_pcie
*ks_pcie
)
281 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
283 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
286 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
287 } while (val
& DBI_CS2
);
290 static int ks_pcie_msi_host_init(struct dw_pcie_rp
*pp
)
292 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
293 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
295 /* Configure and set up BAR0 */
296 ks_pcie_set_dbi_mode(ks_pcie
);
299 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 1);
300 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, SZ_4K
- 1);
302 ks_pcie_clear_dbi_mode(ks_pcie
);
305 * For BAR0, just setting bus address for inbound writes (MSI) should
306 * be sufficient. Use physical address to avoid any conflicts.
308 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, ks_pcie
->app
.start
);
310 pp
->msi_irq_chip
= &ks_pcie_msi_irq_chip
;
311 return dw_pcie_allocate_domains(pp
);
314 static void ks_pcie_handle_intx_irq(struct keystone_pcie
*ks_pcie
,
317 struct dw_pcie
*pci
= ks_pcie
->pci
;
318 struct device
*dev
= pci
->dev
;
321 pending
= ks_pcie_app_readl(ks_pcie
, IRQ_STATUS(offset
));
323 if (BIT(0) & pending
) {
324 dev_dbg(dev
, ": irq: irq_offset %d", offset
);
325 generic_handle_domain_irq(ks_pcie
->intx_irq_domain
, offset
);
328 /* EOI the INTx interrupt */
329 ks_pcie_app_writel(ks_pcie
, IRQ_EOI
, offset
);
332 static void ks_pcie_enable_error_irq(struct keystone_pcie
*ks_pcie
)
334 ks_pcie_app_writel(ks_pcie
, ERR_IRQ_ENABLE_SET
, ERR_IRQ_ALL
);
337 static irqreturn_t
ks_pcie_handle_error_irq(struct keystone_pcie
*ks_pcie
)
340 struct device
*dev
= ks_pcie
->pci
->dev
;
342 reg
= ks_pcie_app_readl(ks_pcie
, ERR_IRQ_STATUS
);
347 dev_err(dev
, "System Error\n");
350 dev_err(dev
, "Fatal Error\n");
352 if (reg
& ERR_NONFATAL
)
353 dev_dbg(dev
, "Non Fatal Error\n");
356 dev_dbg(dev
, "Correctable Error\n");
358 if (!ks_pcie
->is_am6
&& (reg
& ERR_AXI
))
359 dev_err(dev
, "AXI tag lookup fatal Error\n");
361 if (reg
& ERR_AER
|| (ks_pcie
->is_am6
&& (reg
& AM6_ERR_AER
)))
362 dev_err(dev
, "ECRC Error\n");
364 ks_pcie_app_writel(ks_pcie
, ERR_IRQ_STATUS
, reg
);
369 static void ks_pcie_ack_intx_irq(struct irq_data
*d
)
373 static void ks_pcie_mask_intx_irq(struct irq_data
*d
)
377 static void ks_pcie_unmask_intx_irq(struct irq_data
*d
)
381 static struct irq_chip ks_pcie_intx_irq_chip
= {
382 .name
= "Keystone-PCI-INTX-IRQ",
383 .irq_ack
= ks_pcie_ack_intx_irq
,
384 .irq_mask
= ks_pcie_mask_intx_irq
,
385 .irq_unmask
= ks_pcie_unmask_intx_irq
,
388 static int ks_pcie_init_intx_irq_map(struct irq_domain
*d
,
389 unsigned int irq
, irq_hw_number_t hw_irq
)
391 irq_set_chip_and_handler(irq
, &ks_pcie_intx_irq_chip
,
393 irq_set_chip_data(irq
, d
->host_data
);
398 static const struct irq_domain_ops ks_pcie_intx_irq_domain_ops
= {
399 .map
= ks_pcie_init_intx_irq_map
,
400 .xlate
= irq_domain_xlate_onetwocell
,
403 static int ks_pcie_setup_rc_app_regs(struct keystone_pcie
*ks_pcie
)
406 u32 num_viewport
= ks_pcie
->num_viewport
;
407 struct dw_pcie
*pci
= ks_pcie
->pci
;
408 struct dw_pcie_rp
*pp
= &pci
->pp
;
409 struct resource_entry
*entry
;
410 struct resource
*mem
;
414 entry
= resource_list_first_type(&pp
->bridge
->windows
, IORESOURCE_MEM
);
422 /* Disable BARs for inbound access */
423 ks_pcie_set_dbi_mode(ks_pcie
);
424 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 0);
425 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_1
, 0);
426 ks_pcie_clear_dbi_mode(ks_pcie
);
431 val
= ilog2(OB_WIN_SIZE
);
432 ks_pcie_app_writel(ks_pcie
, OB_SIZE
, val
);
434 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
435 for (i
= 0; i
< num_viewport
&& (start
< end
); i
++) {
436 ks_pcie_app_writel(ks_pcie
, OB_OFFSET_INDEX(i
),
437 lower_32_bits(start
) | OB_ENABLEN
);
438 ks_pcie_app_writel(ks_pcie
, OB_OFFSET_HI(i
),
439 upper_32_bits(start
));
440 start
+= OB_WIN_SIZE
* SZ_1M
;
443 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
444 val
|= OB_XLAT_EN_VAL
;
445 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
450 static void __iomem
*ks_pcie_other_map_bus(struct pci_bus
*bus
,
451 unsigned int devfn
, int where
)
453 struct dw_pcie_rp
*pp
= bus
->sysdata
;
454 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
455 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
459 * Checking whether the link is up here is a last line of defense
460 * against platforms that forward errors on the system bus as
461 * SError upon PCI configuration transactions issued when the link
462 * is down. This check is racy by definition and does not stop
463 * the system from triggering an SError if the link goes down
464 * after this check is performed.
466 if (!dw_pcie_link_up(pci
))
469 reg
= CFG_BUS(bus
->number
) | CFG_DEVICE(PCI_SLOT(devfn
)) |
470 CFG_FUNC(PCI_FUNC(devfn
));
471 if (!pci_is_root_bus(bus
->parent
))
473 ks_pcie_app_writel(ks_pcie
, CFG_SETUP
, reg
);
475 return pp
->va_cfg0_base
+ where
;
478 static struct pci_ops ks_child_pcie_ops
= {
479 .map_bus
= ks_pcie_other_map_bus
,
480 .read
= pci_generic_config_read
,
481 .write
= pci_generic_config_write
,
484 static struct pci_ops ks_pcie_ops
= {
485 .map_bus
= dw_pcie_own_conf_map_bus
,
486 .read
= pci_generic_config_read
,
487 .write
= pci_generic_config_write
,
491 * ks_pcie_link_up() - Check if link up
492 * @pci: A pointer to the dw_pcie structure which holds the DesignWare PCIe host
493 * controller driver information.
495 static int ks_pcie_link_up(struct dw_pcie
*pci
)
499 val
= dw_pcie_readl_dbi(pci
, PCIE_PORT_DEBUG0
);
500 val
&= PORT_LOGIC_LTSSM_STATE_MASK
;
501 return (val
== PORT_LOGIC_LTSSM_STATE_L0
);
504 static void ks_pcie_stop_link(struct dw_pcie
*pci
)
506 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
509 /* Disable Link training */
510 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
511 val
&= ~LTSSM_EN_VAL
;
512 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, val
);
515 static int ks_pcie_start_link(struct dw_pcie
*pci
)
517 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
520 /* Initiate Link Training */
521 val
= ks_pcie_app_readl(ks_pcie
, CMD_STATUS
);
522 ks_pcie_app_writel(ks_pcie
, CMD_STATUS
, LTSSM_EN_VAL
| val
);
527 static void ks_pcie_quirk(struct pci_dev
*dev
)
529 struct pci_bus
*bus
= dev
->bus
;
530 struct keystone_pcie
*ks_pcie
;
531 struct device
*bridge_dev
;
532 struct pci_dev
*bridge
;
535 static const struct pci_device_id rc_pci_devids
[] = {
536 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2HK
),
537 .class = PCI_CLASS_BRIDGE_PCI_NORMAL
, .class_mask
= ~0, },
538 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2E
),
539 .class = PCI_CLASS_BRIDGE_PCI_NORMAL
, .class_mask
= ~0, },
540 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2L
),
541 .class = PCI_CLASS_BRIDGE_PCI_NORMAL
, .class_mask
= ~0, },
542 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCIE_RC_K2G
),
543 .class = PCI_CLASS_BRIDGE_PCI_NORMAL
, .class_mask
= ~0, },
546 static const struct pci_device_id am6_pci_devids
[] = {
547 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_AM654X
),
548 .class = PCI_CLASS_BRIDGE_PCI
<< 8, .class_mask
= ~0, },
552 if (pci_is_root_bus(bus
))
555 /* look for the host bridge */
556 while (!pci_is_root_bus(bus
)) {
565 * Keystone PCI controller has a h/w limitation of
566 * 256 bytes maximum read request size. It can't handle
567 * anything higher than this. So force this limit on
568 * all downstream devices.
570 if (pci_match_id(rc_pci_devids
, bridge
)) {
571 if (pcie_get_readrq(dev
) > 256) {
572 dev_info(&dev
->dev
, "limiting MRRS to 256 bytes\n");
573 pcie_set_readrq(dev
, 256);
578 * Memory transactions fail with PCI controller in AM654 PG1.0
579 * when MRRS is set to more than 128 bytes. Force the MRRS to
580 * 128 bytes in all downstream devices.
582 if (pci_match_id(am6_pci_devids
, bridge
)) {
583 bridge_dev
= pci_get_host_bridge_device(dev
);
584 if (!bridge_dev
|| !bridge_dev
->parent
)
587 ks_pcie
= dev_get_drvdata(bridge_dev
->parent
);
591 val
= ks_pcie_app_readl(ks_pcie
, PID
);
594 if (val
!= AM6_PCI_PG1_RTL_VER
)
597 if (pcie_get_readrq(dev
) > 128) {
598 dev_info(&dev
->dev
, "limiting MRRS to 128 bytes\n");
599 pcie_set_readrq(dev
, 128);
603 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID
, PCI_ANY_ID
, ks_pcie_quirk
);
605 static void ks_pcie_msi_irq_handler(struct irq_desc
*desc
)
607 unsigned int irq
= desc
->irq_data
.hwirq
;
608 struct keystone_pcie
*ks_pcie
= irq_desc_get_handler_data(desc
);
609 u32 offset
= irq
- ks_pcie
->msi_host_irq
;
610 struct dw_pcie
*pci
= ks_pcie
->pci
;
611 struct dw_pcie_rp
*pp
= &pci
->pp
;
612 struct device
*dev
= pci
->dev
;
613 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
614 u32 vector
, reg
, pos
;
616 dev_dbg(dev
, "%s, irq %d\n", __func__
, irq
);
619 * The chained irq handler installation would have replaced normal
620 * interrupt driver handler so we need to take care of mask/unmask and
623 chained_irq_enter(chip
, desc
);
625 reg
= ks_pcie_app_readl(ks_pcie
, MSI_IRQ_STATUS(offset
));
627 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
628 * shows 1, 9, 17, 25 and so forth
630 for (pos
= 0; pos
< 4; pos
++) {
631 if (!(reg
& BIT(pos
)))
634 vector
= offset
+ (pos
<< 3);
635 dev_dbg(dev
, "irq: bit %d, vector %d\n", pos
, vector
);
636 generic_handle_domain_irq(pp
->irq_domain
, vector
);
639 chained_irq_exit(chip
, desc
);
643 * ks_pcie_intx_irq_handler() - Handle INTX interrupt
644 * @desc: Pointer to irq descriptor
646 * Traverse through pending INTX interrupts and invoke handler for each. Also
647 * takes care of interrupt controller level mask/ack operation.
649 static void ks_pcie_intx_irq_handler(struct irq_desc
*desc
)
651 unsigned int irq
= irq_desc_get_irq(desc
);
652 struct keystone_pcie
*ks_pcie
= irq_desc_get_handler_data(desc
);
653 struct dw_pcie
*pci
= ks_pcie
->pci
;
654 struct device
*dev
= pci
->dev
;
655 u32 irq_offset
= irq
- ks_pcie
->intx_host_irqs
[0];
656 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
658 dev_dbg(dev
, ": Handling INTX irq %d\n", irq
);
661 * The chained irq handler installation would have replaced normal
662 * interrupt driver handler so we need to take care of mask/unmask and
665 chained_irq_enter(chip
, desc
);
666 ks_pcie_handle_intx_irq(ks_pcie
, irq_offset
);
667 chained_irq_exit(chip
, desc
);
670 static int ks_pcie_config_msi_irq(struct keystone_pcie
*ks_pcie
)
672 struct device
*dev
= ks_pcie
->pci
->dev
;
673 struct device_node
*np
= ks_pcie
->np
;
674 struct device_node
*intc_np
;
675 struct irq_data
*irq_data
;
676 int irq_count
, irq
, ret
, i
;
678 if (!IS_ENABLED(CONFIG_PCI_MSI
))
681 intc_np
= of_get_child_by_name(np
, "msi-interrupt-controller");
685 dev_warn(dev
, "msi-interrupt-controller node is absent\n");
689 irq_count
= of_irq_count(intc_np
);
691 dev_err(dev
, "No IRQ entries in msi-interrupt-controller\n");
696 for (i
= 0; i
< irq_count
; i
++) {
697 irq
= irq_of_parse_and_map(intc_np
, i
);
703 if (!ks_pcie
->msi_host_irq
) {
704 irq_data
= irq_get_irq_data(irq
);
709 ks_pcie
->msi_host_irq
= irq_data
->hwirq
;
712 irq_set_chained_handler_and_data(irq
, ks_pcie_msi_irq_handler
,
716 of_node_put(intc_np
);
720 of_node_put(intc_np
);
724 static int ks_pcie_config_intx_irq(struct keystone_pcie
*ks_pcie
)
726 struct device
*dev
= ks_pcie
->pci
->dev
;
727 struct irq_domain
*intx_irq_domain
;
728 struct device_node
*np
= ks_pcie
->np
;
729 struct device_node
*intc_np
;
730 int irq_count
, irq
, ret
= 0, i
;
732 intc_np
= of_get_child_by_name(np
, "legacy-interrupt-controller");
735 * Since INTX interrupts are modeled as edge-interrupts in
736 * AM6, keep it disabled for now.
740 dev_warn(dev
, "legacy-interrupt-controller node is absent\n");
744 irq_count
= of_irq_count(intc_np
);
746 dev_err(dev
, "No IRQ entries in legacy-interrupt-controller\n");
751 for (i
= 0; i
< irq_count
; i
++) {
752 irq
= irq_of_parse_and_map(intc_np
, i
);
757 ks_pcie
->intx_host_irqs
[i
] = irq
;
759 irq_set_chained_handler_and_data(irq
,
760 ks_pcie_intx_irq_handler
,
764 intx_irq_domain
= irq_domain_add_linear(intc_np
, PCI_NUM_INTX
,
765 &ks_pcie_intx_irq_domain_ops
, NULL
);
766 if (!intx_irq_domain
) {
767 dev_err(dev
, "Failed to add irq domain for INTX irqs\n");
771 ks_pcie
->intx_irq_domain
= intx_irq_domain
;
773 for (i
= 0; i
< PCI_NUM_INTX
; i
++)
774 ks_pcie_app_writel(ks_pcie
, IRQ_ENABLE_SET(i
), INTx_EN
);
777 of_node_put(intc_np
);
783 * When a PCI device does not exist during config cycles, keystone host
784 * gets a bus error instead of returning 0xffffffff (PCI_ERROR_RESPONSE).
785 * This handler always returns 0 for this kind of fault.
787 static int ks_pcie_fault(unsigned long addr
, unsigned int fsr
,
788 struct pt_regs
*regs
)
790 unsigned long instr
= *(unsigned long *) instruction_pointer(regs
);
792 if ((instr
& 0x0e100090) == 0x00100090) {
793 int reg
= (instr
>> 12) & 15;
795 regs
->uregs
[reg
] = -1;
803 static int __init
ks_pcie_init_id(struct keystone_pcie
*ks_pcie
)
807 struct regmap
*devctrl_regs
;
808 struct dw_pcie
*pci
= ks_pcie
->pci
;
809 struct device
*dev
= pci
->dev
;
810 struct device_node
*np
= dev
->of_node
;
811 struct of_phandle_args args
;
812 unsigned int offset
= 0;
814 devctrl_regs
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-id");
815 if (IS_ERR(devctrl_regs
))
816 return PTR_ERR(devctrl_regs
);
818 /* Do not error out to maintain old DT compatibility */
819 ret
= of_parse_phandle_with_fixed_args(np
, "ti,syscon-pcie-id", 1, 0, &args
);
821 offset
= args
.args
[0];
823 ret
= regmap_read(devctrl_regs
, offset
, &id
);
827 dw_pcie_dbi_ro_wr_en(pci
);
828 dw_pcie_writew_dbi(pci
, PCI_VENDOR_ID
, id
& PCIE_VENDORID_MASK
);
829 dw_pcie_writew_dbi(pci
, PCI_DEVICE_ID
, id
>> PCIE_DEVICEID_SHIFT
);
830 dw_pcie_dbi_ro_wr_dis(pci
);
835 static int __init
ks_pcie_host_init(struct dw_pcie_rp
*pp
)
837 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
838 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
841 pp
->bridge
->ops
= &ks_pcie_ops
;
842 if (!ks_pcie
->is_am6
)
843 pp
->bridge
->child_ops
= &ks_child_pcie_ops
;
845 ret
= ks_pcie_config_intx_irq(ks_pcie
);
849 ret
= ks_pcie_config_msi_irq(ks_pcie
);
853 ks_pcie_stop_link(pci
);
854 ret
= ks_pcie_setup_rc_app_regs(ks_pcie
);
858 writew(PCI_IO_RANGE_TYPE_32
| (PCI_IO_RANGE_TYPE_32
<< 8),
859 pci
->dbi_base
+ PCI_IO_BASE
);
861 ret
= ks_pcie_init_id(ks_pcie
);
867 * PCIe access errors that result into OCP errors are caught by ARM as
870 hook_fault_code(17, ks_pcie_fault
, SIGBUS
, 0,
871 "Asynchronous external abort");
877 static const struct dw_pcie_host_ops ks_pcie_host_ops
= {
878 .init
= ks_pcie_host_init
,
879 .msi_init
= ks_pcie_msi_host_init
,
882 static const struct dw_pcie_host_ops ks_pcie_am654_host_ops
= {
883 .init
= ks_pcie_host_init
,
886 static irqreturn_t
ks_pcie_err_irq_handler(int irq
, void *priv
)
888 struct keystone_pcie
*ks_pcie
= priv
;
890 return ks_pcie_handle_error_irq(ks_pcie
);
893 static void ks_pcie_am654_write_dbi2(struct dw_pcie
*pci
, void __iomem
*base
,
894 u32 reg
, size_t size
, u32 val
)
896 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
898 ks_pcie_set_dbi_mode(ks_pcie
);
899 dw_pcie_write(base
+ reg
, size
, val
);
900 ks_pcie_clear_dbi_mode(ks_pcie
);
903 static const struct dw_pcie_ops ks_pcie_dw_pcie_ops
= {
904 .start_link
= ks_pcie_start_link
,
905 .stop_link
= ks_pcie_stop_link
,
906 .link_up
= ks_pcie_link_up
,
907 .write_dbi2
= ks_pcie_am654_write_dbi2
,
910 static void ks_pcie_am654_ep_init(struct dw_pcie_ep
*ep
)
912 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
915 ep
->page_size
= AM654_WIN_SIZE
;
916 flags
= PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_32
;
917 dw_pcie_writel_dbi2(pci
, PCI_BASE_ADDRESS_0
, APP_ADDR_SPACE_0
- 1);
918 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, flags
);
921 static void ks_pcie_am654_raise_intx_irq(struct keystone_pcie
*ks_pcie
)
923 struct dw_pcie
*pci
= ks_pcie
->pci
;
926 int_pin
= dw_pcie_readb_dbi(pci
, PCI_INTERRUPT_PIN
);
927 if (int_pin
== 0 || int_pin
> 4)
930 ks_pcie_app_writel(ks_pcie
, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin
),
932 ks_pcie_app_writel(ks_pcie
, PCIE_EP_IRQ_SET
, INT_ENABLE
);
934 ks_pcie_app_writel(ks_pcie
, PCIE_EP_IRQ_CLR
, INT_ENABLE
);
935 ks_pcie_app_writel(ks_pcie
, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin
),
939 static int ks_pcie_am654_raise_irq(struct dw_pcie_ep
*ep
, u8 func_no
,
940 unsigned int type
, u16 interrupt_num
)
942 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
943 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
947 ks_pcie_am654_raise_intx_irq(ks_pcie
);
950 dw_pcie_ep_raise_msi_irq(ep
, func_no
, interrupt_num
);
953 dw_pcie_ep_raise_msix_irq(ep
, func_no
, interrupt_num
);
956 dev_err(pci
->dev
, "UNKNOWN IRQ type\n");
963 static const struct pci_epc_features ks_pcie_am654_epc_features
= {
964 .linkup_notifier
= false,
966 .msix_capable
= true,
967 .bar
[BAR_0
] = { .type
= BAR_RESERVED
, },
968 .bar
[BAR_1
] = { .type
= BAR_RESERVED
, },
969 .bar
[BAR_2
] = { .type
= BAR_FIXED
, .fixed_size
= SZ_1M
, },
970 .bar
[BAR_3
] = { .type
= BAR_FIXED
, .fixed_size
= SZ_64K
, },
971 .bar
[BAR_4
] = { .type
= BAR_FIXED
, .fixed_size
= 256, },
972 .bar
[BAR_5
] = { .type
= BAR_FIXED
, .fixed_size
= SZ_1M
, },
976 static const struct pci_epc_features
*
977 ks_pcie_am654_get_features(struct dw_pcie_ep
*ep
)
979 return &ks_pcie_am654_epc_features
;
982 static const struct dw_pcie_ep_ops ks_pcie_am654_ep_ops
= {
983 .init
= ks_pcie_am654_ep_init
,
984 .raise_irq
= ks_pcie_am654_raise_irq
,
985 .get_features
= &ks_pcie_am654_get_features
,
988 static void ks_pcie_disable_phy(struct keystone_pcie
*ks_pcie
)
990 int num_lanes
= ks_pcie
->num_lanes
;
992 while (num_lanes
--) {
993 phy_power_off(ks_pcie
->phy
[num_lanes
]);
994 phy_exit(ks_pcie
->phy
[num_lanes
]);
998 static int ks_pcie_enable_phy(struct keystone_pcie
*ks_pcie
)
1002 int num_lanes
= ks_pcie
->num_lanes
;
1004 for (i
= 0; i
< num_lanes
; i
++) {
1005 ret
= phy_reset(ks_pcie
->phy
[i
]);
1009 ret
= phy_init(ks_pcie
->phy
[i
]);
1013 ret
= phy_power_on(ks_pcie
->phy
[i
]);
1015 phy_exit(ks_pcie
->phy
[i
]);
1024 phy_power_off(ks_pcie
->phy
[i
]);
1025 phy_exit(ks_pcie
->phy
[i
]);
1031 static int ks_pcie_set_mode(struct device
*dev
)
1033 struct device_node
*np
= dev
->of_node
;
1034 struct of_phandle_args args
;
1035 unsigned int offset
= 0;
1036 struct regmap
*syscon
;
1041 syscon
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-mode");
1045 /* Do not error out to maintain old DT compatibility */
1046 ret
= of_parse_phandle_with_fixed_args(np
, "ti,syscon-pcie-mode", 1, 0, &args
);
1048 offset
= args
.args
[0];
1050 mask
= KS_PCIE_DEV_TYPE_MASK
| KS_PCIE_SYSCLOCKOUTEN
;
1051 val
= KS_PCIE_DEV_TYPE(RC
) | KS_PCIE_SYSCLOCKOUTEN
;
1053 ret
= regmap_update_bits(syscon
, offset
, mask
, val
);
1055 dev_err(dev
, "failed to set pcie mode\n");
1062 static int ks_pcie_am654_set_mode(struct device
*dev
,
1063 enum dw_pcie_device_mode mode
)
1065 struct device_node
*np
= dev
->of_node
;
1066 struct of_phandle_args args
;
1067 unsigned int offset
= 0;
1068 struct regmap
*syscon
;
1073 syscon
= syscon_regmap_lookup_by_phandle(np
, "ti,syscon-pcie-mode");
1077 /* Do not error out to maintain old DT compatibility */
1078 ret
= of_parse_phandle_with_fixed_args(np
, "ti,syscon-pcie-mode", 1, 0, &args
);
1080 offset
= args
.args
[0];
1082 mask
= AM654_PCIE_DEV_TYPE_MASK
;
1085 case DW_PCIE_RC_TYPE
:
1088 case DW_PCIE_EP_TYPE
:
1092 dev_err(dev
, "INVALID device type %d\n", mode
);
1096 ret
= regmap_update_bits(syscon
, offset
, mask
, val
);
1098 dev_err(dev
, "failed to set pcie mode\n");
1105 static const struct ks_pcie_of_data ks_pcie_rc_of_data
= {
1106 .host_ops
= &ks_pcie_host_ops
,
1107 .mode
= DW_PCIE_RC_TYPE
,
1108 .version
= DW_PCIE_VER_365A
,
1111 static const struct ks_pcie_of_data ks_pcie_am654_rc_of_data
= {
1112 .host_ops
= &ks_pcie_am654_host_ops
,
1113 .mode
= DW_PCIE_RC_TYPE
,
1114 .version
= DW_PCIE_VER_490A
,
1117 static const struct ks_pcie_of_data ks_pcie_am654_ep_of_data
= {
1118 .ep_ops
= &ks_pcie_am654_ep_ops
,
1119 .mode
= DW_PCIE_EP_TYPE
,
1120 .version
= DW_PCIE_VER_490A
,
1123 static const struct of_device_id ks_pcie_of_match
[] = {
1126 .data
= &ks_pcie_rc_of_data
,
1127 .compatible
= "ti,keystone-pcie",
1130 .data
= &ks_pcie_am654_rc_of_data
,
1131 .compatible
= "ti,am654-pcie-rc",
1134 .data
= &ks_pcie_am654_ep_of_data
,
1135 .compatible
= "ti,am654-pcie-ep",
1140 static int ks_pcie_probe(struct platform_device
*pdev
)
1142 const struct dw_pcie_host_ops
*host_ops
;
1143 const struct dw_pcie_ep_ops
*ep_ops
;
1144 struct device
*dev
= &pdev
->dev
;
1145 struct device_node
*np
= dev
->of_node
;
1146 const struct ks_pcie_of_data
*data
;
1147 enum dw_pcie_device_mode mode
;
1148 struct dw_pcie
*pci
;
1149 struct keystone_pcie
*ks_pcie
;
1150 struct device_link
**link
;
1151 struct gpio_desc
*gpiod
;
1152 struct resource
*res
;
1163 data
= of_device_get_match_data(dev
);
1167 version
= data
->version
;
1168 host_ops
= data
->host_ops
;
1169 ep_ops
= data
->ep_ops
;
1172 ks_pcie
= devm_kzalloc(dev
, sizeof(*ks_pcie
), GFP_KERNEL
);
1176 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
1180 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "app");
1181 ks_pcie
->va_app_base
= devm_ioremap_resource(dev
, res
);
1182 if (IS_ERR(ks_pcie
->va_app_base
))
1183 return PTR_ERR(ks_pcie
->va_app_base
);
1185 ks_pcie
->app
= *res
;
1187 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbics");
1188 base
= devm_pci_remap_cfg_resource(dev
, res
);
1190 return PTR_ERR(base
);
1192 if (of_device_is_compatible(np
, "ti,am654-pcie-rc"))
1193 ks_pcie
->is_am6
= true;
1195 pci
->dbi_base
= base
;
1196 pci
->dbi_base2
= base
;
1198 pci
->ops
= &ks_pcie_dw_pcie_ops
;
1199 pci
->version
= version
;
1201 irq
= platform_get_irq(pdev
, 0);
1205 ret
= request_irq(irq
, ks_pcie_err_irq_handler
, IRQF_SHARED
,
1206 "ks-pcie-error-irq", ks_pcie
);
1208 dev_err(dev
, "failed to request error IRQ %d\n",
1213 ret
= of_property_read_u32(np
, "num-lanes", &num_lanes
);
1217 phy
= devm_kzalloc(dev
, sizeof(*phy
) * num_lanes
, GFP_KERNEL
);
1221 link
= devm_kzalloc(dev
, sizeof(*link
) * num_lanes
, GFP_KERNEL
);
1225 for (i
= 0; i
< num_lanes
; i
++) {
1226 snprintf(name
, sizeof(name
), "pcie-phy%d", i
);
1227 phy
[i
] = devm_phy_optional_get(dev
, name
);
1228 if (IS_ERR(phy
[i
])) {
1229 ret
= PTR_ERR(phy
[i
]);
1236 link
[i
] = device_link_add(dev
, &phy
[i
]->dev
, DL_FLAG_STATELESS
);
1245 ks_pcie
->link
= link
;
1246 ks_pcie
->num_lanes
= num_lanes
;
1249 gpiod
= devm_gpiod_get_optional(dev
, "reset",
1251 if (IS_ERR(gpiod
)) {
1252 ret
= PTR_ERR(gpiod
);
1253 if (ret
!= -EPROBE_DEFER
)
1254 dev_err(dev
, "Failed to get reset GPIO\n");
1258 /* Obtain references to the PHYs */
1259 for (i
= 0; i
< num_lanes
; i
++)
1260 phy_pm_runtime_get_sync(ks_pcie
->phy
[i
]);
1262 ret
= ks_pcie_enable_phy(ks_pcie
);
1264 /* Release references to the PHYs */
1265 for (i
= 0; i
< num_lanes
; i
++)
1266 phy_pm_runtime_put_sync(ks_pcie
->phy
[i
]);
1269 dev_err(dev
, "failed to enable phy\n");
1273 platform_set_drvdata(pdev
, ks_pcie
);
1274 pm_runtime_enable(dev
);
1275 ret
= pm_runtime_get_sync(dev
);
1277 dev_err(dev
, "pm_runtime_get_sync failed\n");
1281 if (dw_pcie_ver_is_ge(pci
, 480A
))
1282 ret
= ks_pcie_am654_set_mode(dev
, mode
);
1284 ret
= ks_pcie_set_mode(dev
);
1289 case DW_PCIE_RC_TYPE
:
1290 if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_HOST
)) {
1295 ret
= of_property_read_u32(np
, "num-viewport", &num_viewport
);
1297 dev_err(dev
, "unable to read *num-viewport* property\n");
1302 * "Power Sequencing and Reset Signal Timings" table in
1303 * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 2.0
1304 * indicates PERST# should be deasserted after minimum of 100us
1305 * once REFCLK is stable. The REFCLK to the connector in RC
1306 * mode is selected while enabling the PHY. So deassert PERST#
1310 usleep_range(100, 200);
1311 gpiod_set_value_cansleep(gpiod
, 1);
1314 ks_pcie
->num_viewport
= num_viewport
;
1315 pci
->pp
.ops
= host_ops
;
1316 ret
= dw_pcie_host_init(&pci
->pp
);
1320 case DW_PCIE_EP_TYPE
:
1321 if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_EP
)) {
1326 pci
->ep
.ops
= ep_ops
;
1327 ret
= dw_pcie_ep_init(&pci
->ep
);
1331 ret
= dw_pcie_ep_init_registers(&pci
->ep
);
1333 dev_err(dev
, "Failed to initialize DWC endpoint registers\n");
1337 pci_epc_init_notify(pci
->ep
.epc
);
1341 dev_err(dev
, "INVALID device type %d\n", mode
);
1344 ks_pcie_enable_error_irq(ks_pcie
);
1349 dw_pcie_ep_deinit(&pci
->ep
);
1351 pm_runtime_put(dev
);
1352 pm_runtime_disable(dev
);
1353 ks_pcie_disable_phy(ks_pcie
);
1356 while (--i
>= 0 && link
[i
])
1357 device_link_del(link
[i
]);
1362 static void ks_pcie_remove(struct platform_device
*pdev
)
1364 struct keystone_pcie
*ks_pcie
= platform_get_drvdata(pdev
);
1365 struct device_link
**link
= ks_pcie
->link
;
1366 int num_lanes
= ks_pcie
->num_lanes
;
1367 struct device
*dev
= &pdev
->dev
;
1369 pm_runtime_put(dev
);
1370 pm_runtime_disable(dev
);
1371 ks_pcie_disable_phy(ks_pcie
);
1373 device_link_del(link
[num_lanes
]);
1376 static struct platform_driver ks_pcie_driver
= {
1377 .probe
= ks_pcie_probe
,
1378 .remove
= ks_pcie_remove
,
1380 .name
= "keystone-pcie",
1381 .of_match_table
= ks_pcie_of_match
,
1384 builtin_platform_driver(ks_pcie_driver
);