1 // SPDX-License-Identifier: GPL-2.0
3 * DesignWare application register space functions for Keystone PCI controller
5 * Copyright (C) 2013-2014 Texas Instruments., Ltd.
8 * Author: Murali Karicheri <m-karicheri2@ti.com>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/irqreturn.h>
14 #include <linux/module.h>
16 #include <linux/of_pci.h>
17 #include <linux/pci.h>
18 #include <linux/platform_device.h>
20 #include "pcie-designware.h"
21 #include "pci-keystone.h"
23 /* Application register defines */
24 #define LTSSM_EN_VAL 1
25 #define LTSSM_STATE_MASK 0x1f
26 #define LTSSM_STATE_L0 0x11
27 #define DBI_CS2_EN_VAL 0x20
28 #define OB_XLAT_EN_VAL 2
30 /* Application registers */
31 #define CMD_STATUS 0x004
32 #define CFG_SETUP 0x008
34 #define CFG_PCIM_WIN_SZ_IDX 3
35 #define CFG_PCIM_WIN_CNT 32
36 #define SPACE0_REMOTE_CFG_OFFSET 0x1000
37 #define OB_OFFSET_INDEX(n) (0x200 + (8 * n))
38 #define OB_OFFSET_HI(n) (0x204 + (8 * n))
40 /* IRQ register defines */
42 #define IRQ_STATUS 0x184
43 #define IRQ_ENABLE_SET 0x188
44 #define IRQ_ENABLE_CLR 0x18c
47 #define MSI0_IRQ_STATUS 0x104
48 #define MSI0_IRQ_ENABLE_SET 0x108
49 #define MSI0_IRQ_ENABLE_CLR 0x10c
50 #define IRQ_STATUS 0x184
51 #define MSI_IRQ_OFFSET 4
54 #define ERR_AER BIT(5) /* ECRC error */
55 #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
56 #define ERR_CORR BIT(3) /* Correctable error */
57 #define ERR_NONFATAL BIT(2) /* Non-fatal error */
58 #define ERR_FATAL BIT(1) /* Fatal error */
59 #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
60 #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
61 ERR_NONFATAL | ERR_FATAL | ERR_SYS)
62 #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
63 #define ERR_IRQ_STATUS_RAW 0x1c0
64 #define ERR_IRQ_STATUS 0x1c4
65 #define ERR_IRQ_ENABLE_SET 0x1c8
66 #define ERR_IRQ_ENABLE_CLR 0x1cc
68 /* Config space registers */
71 #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
73 static inline void update_reg_offset_bit_pos(u32 offset
, u32
*reg_offset
,
76 *reg_offset
= offset
% 8;
77 *bit_pos
= offset
>> 3;
80 phys_addr_t
ks_dw_pcie_get_msi_addr(struct pcie_port
*pp
)
82 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
83 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
85 return ks_pcie
->app
.start
+ MSI_IRQ
;
88 static u32
ks_dw_app_readl(struct keystone_pcie
*ks_pcie
, u32 offset
)
90 return readl(ks_pcie
->va_app_base
+ offset
);
93 static void ks_dw_app_writel(struct keystone_pcie
*ks_pcie
, u32 offset
, u32 val
)
95 writel(val
, ks_pcie
->va_app_base
+ offset
);
98 void ks_dw_pcie_handle_msi_irq(struct keystone_pcie
*ks_pcie
, int offset
)
100 struct dw_pcie
*pci
= ks_pcie
->pci
;
101 struct pcie_port
*pp
= &pci
->pp
;
102 struct device
*dev
= pci
->dev
;
106 pending
= ks_dw_app_readl(ks_pcie
, MSI0_IRQ_STATUS
+ (offset
<< 4));
109 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
110 * shows 1, 9, 17, 25 and so forth
112 for (src
= 0; src
< 4; src
++) {
113 if (BIT(src
) & pending
) {
114 vector
= offset
+ (src
<< 3);
115 virq
= irq_linear_revmap(pp
->irq_domain
, vector
);
116 dev_dbg(dev
, "irq: bit %d, vector %d, virq %d\n",
118 generic_handle_irq(virq
);
123 static void ks_dw_pcie_msi_irq_ack(struct irq_data
*d
)
125 u32 offset
, reg_offset
, bit_pos
;
126 struct keystone_pcie
*ks_pcie
;
127 struct msi_desc
*msi
;
128 struct pcie_port
*pp
;
131 msi
= irq_data_get_msi_desc(d
);
132 pp
= (struct pcie_port
*) msi_desc_to_pci_sysdata(msi
);
133 pci
= to_dw_pcie_from_pp(pp
);
134 ks_pcie
= to_keystone_pcie(pci
);
135 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
136 update_reg_offset_bit_pos(offset
, ®_offset
, &bit_pos
);
138 ks_dw_app_writel(ks_pcie
, MSI0_IRQ_STATUS
+ (reg_offset
<< 4),
140 ks_dw_app_writel(ks_pcie
, IRQ_EOI
, reg_offset
+ MSI_IRQ_OFFSET
);
143 void ks_dw_pcie_msi_set_irq(struct pcie_port
*pp
, int irq
)
145 u32 reg_offset
, bit_pos
;
146 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
147 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
149 update_reg_offset_bit_pos(irq
, ®_offset
, &bit_pos
);
150 ks_dw_app_writel(ks_pcie
, MSI0_IRQ_ENABLE_SET
+ (reg_offset
<< 4),
154 void ks_dw_pcie_msi_clear_irq(struct pcie_port
*pp
, int irq
)
156 u32 reg_offset
, bit_pos
;
157 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
158 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
160 update_reg_offset_bit_pos(irq
, ®_offset
, &bit_pos
);
161 ks_dw_app_writel(ks_pcie
, MSI0_IRQ_ENABLE_CLR
+ (reg_offset
<< 4),
165 static void ks_dw_pcie_msi_irq_mask(struct irq_data
*d
)
167 struct msi_desc
*msi
;
168 struct pcie_port
*pp
;
171 msi
= irq_data_get_msi_desc(d
);
172 pp
= (struct pcie_port
*) msi_desc_to_pci_sysdata(msi
);
173 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
175 /* Mask the end point if PVM implemented */
176 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
177 if (msi
->msi_attrib
.maskbit
)
181 ks_dw_pcie_msi_clear_irq(pp
, offset
);
184 static void ks_dw_pcie_msi_irq_unmask(struct irq_data
*d
)
186 struct msi_desc
*msi
;
187 struct pcie_port
*pp
;
190 msi
= irq_data_get_msi_desc(d
);
191 pp
= (struct pcie_port
*) msi_desc_to_pci_sysdata(msi
);
192 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
194 /* Mask the end point if PVM implemented */
195 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
196 if (msi
->msi_attrib
.maskbit
)
197 pci_msi_unmask_irq(d
);
200 ks_dw_pcie_msi_set_irq(pp
, offset
);
203 static struct irq_chip ks_dw_pcie_msi_irq_chip
= {
204 .name
= "Keystone-PCIe-MSI-IRQ",
205 .irq_ack
= ks_dw_pcie_msi_irq_ack
,
206 .irq_mask
= ks_dw_pcie_msi_irq_mask
,
207 .irq_unmask
= ks_dw_pcie_msi_irq_unmask
,
210 static int ks_dw_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
211 irq_hw_number_t hwirq
)
213 irq_set_chip_and_handler(irq
, &ks_dw_pcie_msi_irq_chip
,
215 irq_set_chip_data(irq
, domain
->host_data
);
220 static const struct irq_domain_ops ks_dw_pcie_msi_domain_ops
= {
221 .map
= ks_dw_pcie_msi_map
,
224 int ks_dw_pcie_msi_host_init(struct pcie_port
*pp
, struct msi_controller
*chip
)
226 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
227 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
228 struct device
*dev
= pci
->dev
;
231 pp
->irq_domain
= irq_domain_add_linear(ks_pcie
->msi_intc_np
,
233 &ks_dw_pcie_msi_domain_ops
,
235 if (!pp
->irq_domain
) {
236 dev_err(dev
, "irq domain init failed\n");
240 for (i
= 0; i
< MAX_MSI_IRQS
; i
++)
241 irq_create_mapping(pp
->irq_domain
, i
);
246 void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie
*ks_pcie
)
250 for (i
= 0; i
< PCI_NUM_INTX
; i
++)
251 ks_dw_app_writel(ks_pcie
, IRQ_ENABLE_SET
+ (i
<< 4), 0x1);
254 void ks_dw_pcie_handle_legacy_irq(struct keystone_pcie
*ks_pcie
, int offset
)
256 struct dw_pcie
*pci
= ks_pcie
->pci
;
257 struct device
*dev
= pci
->dev
;
261 pending
= ks_dw_app_readl(ks_pcie
, IRQ_STATUS
+ (offset
<< 4));
263 if (BIT(0) & pending
) {
264 virq
= irq_linear_revmap(ks_pcie
->legacy_irq_domain
, offset
);
265 dev_dbg(dev
, ": irq: irq_offset %d, virq %d\n", offset
, virq
);
266 generic_handle_irq(virq
);
269 /* EOI the INTx interrupt */
270 ks_dw_app_writel(ks_pcie
, IRQ_EOI
, offset
);
273 void ks_dw_pcie_enable_error_irq(struct keystone_pcie
*ks_pcie
)
275 ks_dw_app_writel(ks_pcie
, ERR_IRQ_ENABLE_SET
, ERR_IRQ_ALL
);
278 irqreturn_t
ks_dw_pcie_handle_error_irq(struct keystone_pcie
*ks_pcie
)
282 status
= ks_dw_app_readl(ks_pcie
, ERR_IRQ_STATUS_RAW
) & ERR_IRQ_ALL
;
286 if (status
& ERR_FATAL_IRQ
)
287 dev_err(ks_pcie
->pci
->dev
, "fatal error (status %#010x)\n",
290 /* Ack the IRQ; status bits are RW1C */
291 ks_dw_app_writel(ks_pcie
, ERR_IRQ_STATUS
, status
);
295 static void ks_dw_pcie_ack_legacy_irq(struct irq_data
*d
)
299 static void ks_dw_pcie_mask_legacy_irq(struct irq_data
*d
)
303 static void ks_dw_pcie_unmask_legacy_irq(struct irq_data
*d
)
307 static struct irq_chip ks_dw_pcie_legacy_irq_chip
= {
308 .name
= "Keystone-PCI-Legacy-IRQ",
309 .irq_ack
= ks_dw_pcie_ack_legacy_irq
,
310 .irq_mask
= ks_dw_pcie_mask_legacy_irq
,
311 .irq_unmask
= ks_dw_pcie_unmask_legacy_irq
,
314 static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain
*d
,
315 unsigned int irq
, irq_hw_number_t hw_irq
)
317 irq_set_chip_and_handler(irq
, &ks_dw_pcie_legacy_irq_chip
,
319 irq_set_chip_data(irq
, d
->host_data
);
324 static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops
= {
325 .map
= ks_dw_pcie_init_legacy_irq_map
,
326 .xlate
= irq_domain_xlate_onetwocell
,
330 * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
333 * Since modification of dbi_cs2 involves different clock domain, read the
334 * status back to ensure the transition is complete.
336 static void ks_dw_pcie_set_dbi_mode(struct keystone_pcie
*ks_pcie
)
340 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
341 ks_dw_app_writel(ks_pcie
, CMD_STATUS
, DBI_CS2_EN_VAL
| val
);
344 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
345 } while (!(val
& DBI_CS2_EN_VAL
));
349 * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
351 * Since modification of dbi_cs2 involves different clock domain, read the
352 * status back to ensure the transition is complete.
354 static void ks_dw_pcie_clear_dbi_mode(struct keystone_pcie
*ks_pcie
)
358 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
359 ks_dw_app_writel(ks_pcie
, CMD_STATUS
, ~DBI_CS2_EN_VAL
& val
);
362 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
363 } while (val
& DBI_CS2_EN_VAL
);
366 void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie
*ks_pcie
)
368 struct dw_pcie
*pci
= ks_pcie
->pci
;
369 struct pcie_port
*pp
= &pci
->pp
;
370 u32 start
= pp
->mem
->start
, end
= pp
->mem
->end
;
374 /* Disable BARs for inbound access */
375 ks_dw_pcie_set_dbi_mode(ks_pcie
);
376 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 0);
377 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_1
, 0);
378 ks_dw_pcie_clear_dbi_mode(ks_pcie
);
380 /* Set outbound translation size per window division */
381 ks_dw_app_writel(ks_pcie
, OB_SIZE
, CFG_PCIM_WIN_SZ_IDX
& 0x7);
383 tr_size
= (1 << (CFG_PCIM_WIN_SZ_IDX
& 0x7)) * SZ_1M
;
385 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
386 for (i
= 0; (i
< CFG_PCIM_WIN_CNT
) && (start
< end
); i
++) {
387 ks_dw_app_writel(ks_pcie
, OB_OFFSET_INDEX(i
), start
| 1);
388 ks_dw_app_writel(ks_pcie
, OB_OFFSET_HI(i
), 0);
392 /* Enable OB translation */
393 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
394 ks_dw_app_writel(ks_pcie
, CMD_STATUS
, OB_XLAT_EN_VAL
| val
);
398 * ks_pcie_cfg_setup() - Set up configuration space address for a device
400 * @ks_pcie: ptr to keystone_pcie structure
401 * @bus: Bus number the device is residing on
402 * @devfn: device, function number info
404 * Forms and returns the address of configuration space mapped in PCIESS
405 * address space 0. Also configures CFG_SETUP for remote configuration space
408 * The address space has two regions to access configuration - local and remote.
409 * We access local region for bus 0 (as RC is attached on bus 0) and remote
410 * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
411 * we will do TYPE 0 access as it will be on our secondary bus (logical).
412 * CFG_SETUP is needed only for remote configuration access.
414 static void __iomem
*ks_pcie_cfg_setup(struct keystone_pcie
*ks_pcie
, u8 bus
,
417 u8 device
= PCI_SLOT(devfn
), function
= PCI_FUNC(devfn
);
418 struct dw_pcie
*pci
= ks_pcie
->pci
;
419 struct pcie_port
*pp
= &pci
->pp
;
423 return pci
->dbi_base
;
425 regval
= (bus
<< 16) | (device
<< 8) | function
;
428 * Since Bus#1 will be a virtual bus, we need to have TYPE0
435 ks_dw_app_writel(ks_pcie
, CFG_SETUP
, regval
);
436 return pp
->va_cfg0_base
;
439 int ks_dw_pcie_rd_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
440 unsigned int devfn
, int where
, int size
, u32
*val
)
442 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
443 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
444 u8 bus_num
= bus
->number
;
447 addr
= ks_pcie_cfg_setup(ks_pcie
, bus_num
, devfn
);
449 return dw_pcie_read(addr
+ where
, size
, val
);
452 int ks_dw_pcie_wr_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
453 unsigned int devfn
, int where
, int size
, u32 val
)
455 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
456 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
457 u8 bus_num
= bus
->number
;
460 addr
= ks_pcie_cfg_setup(ks_pcie
, bus_num
, devfn
);
462 return dw_pcie_write(addr
+ where
, size
, val
);
466 * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
468 * This sets BAR0 to enable inbound access for MSI_IRQ register
470 void ks_dw_pcie_v3_65_scan_bus(struct pcie_port
*pp
)
472 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
473 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pci
);
475 /* Configure and set up BAR0 */
476 ks_dw_pcie_set_dbi_mode(ks_pcie
);
479 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, 1);
480 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, SZ_4K
- 1);
482 ks_dw_pcie_clear_dbi_mode(ks_pcie
);
485 * For BAR0, just setting bus address for inbound writes (MSI) should
486 * be sufficient. Use physical address to avoid any conflicts.
488 dw_pcie_writel_dbi(pci
, PCI_BASE_ADDRESS_0
, ks_pcie
->app
.start
);
492 * ks_dw_pcie_link_up() - Check if link up
494 int ks_dw_pcie_link_up(struct dw_pcie
*pci
)
498 val
= dw_pcie_readl_dbi(pci
, DEBUG0
);
499 return (val
& LTSSM_STATE_MASK
) == LTSSM_STATE_L0
;
502 void ks_dw_pcie_initiate_link_train(struct keystone_pcie
*ks_pcie
)
506 /* Disable Link training */
507 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
508 val
&= ~LTSSM_EN_VAL
;
509 ks_dw_app_writel(ks_pcie
, CMD_STATUS
, LTSSM_EN_VAL
| val
);
511 /* Initiate Link Training */
512 val
= ks_dw_app_readl(ks_pcie
, CMD_STATUS
);
513 ks_dw_app_writel(ks_pcie
, CMD_STATUS
, LTSSM_EN_VAL
| val
);
517 * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
519 * Ioremap the register resources, initialize legacy irq domain
520 * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
521 * PCI host controller.
523 int __init
ks_dw_pcie_host_init(struct keystone_pcie
*ks_pcie
,
524 struct device_node
*msi_intc_np
)
526 struct dw_pcie
*pci
= ks_pcie
->pci
;
527 struct pcie_port
*pp
= &pci
->pp
;
528 struct device
*dev
= pci
->dev
;
529 struct platform_device
*pdev
= to_platform_device(dev
);
530 struct resource
*res
;
532 /* Index 0 is the config reg. space address */
533 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
534 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, res
);
535 if (IS_ERR(pci
->dbi_base
))
536 return PTR_ERR(pci
->dbi_base
);
539 * We set these same and is used in pcie rd/wr_other_conf
542 pp
->va_cfg0_base
= pci
->dbi_base
+ SPACE0_REMOTE_CFG_OFFSET
;
543 pp
->va_cfg1_base
= pp
->va_cfg0_base
;
545 /* Index 1 is the application reg. space address */
546 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
547 ks_pcie
->va_app_base
= devm_ioremap_resource(dev
, res
);
548 if (IS_ERR(ks_pcie
->va_app_base
))
549 return PTR_ERR(ks_pcie
->va_app_base
);
553 /* Create legacy IRQ domain */
554 ks_pcie
->legacy_irq_domain
=
555 irq_domain_add_linear(ks_pcie
->legacy_intc_np
,
557 &ks_dw_pcie_legacy_irq_domain_ops
,
559 if (!ks_pcie
->legacy_irq_domain
) {
560 dev_err(dev
, "Failed to add irq domain for legacy irqs\n");
564 return dw_pcie_host_init(pp
);