2 * Designware application register space functions for Keystone PCI controller
4 * Copyright (C) 2013-2014 Texas Instruments., Ltd.
7 * Author: Murali Karicheri <m-karicheri2@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
19 #include <linux/of_pci.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
23 #include "pcie-designware.h"
24 #include "pci-keystone.h"
26 /* Application register defines */
27 #define LTSSM_EN_VAL 1
28 #define LTSSM_STATE_MASK 0x1f
29 #define LTSSM_STATE_L0 0x11
30 #define DBI_CS2_EN_VAL 0x20
31 #define OB_XLAT_EN_VAL 2
33 /* Application registers */
34 #define CMD_STATUS 0x004
35 #define CFG_SETUP 0x008
37 #define CFG_PCIM_WIN_SZ_IDX 3
38 #define CFG_PCIM_WIN_CNT 32
39 #define SPACE0_REMOTE_CFG_OFFSET 0x1000
40 #define OB_OFFSET_INDEX(n) (0x200 + (8 * n))
41 #define OB_OFFSET_HI(n) (0x204 + (8 * n))
43 /* IRQ register defines */
45 #define IRQ_STATUS 0x184
46 #define IRQ_ENABLE_SET 0x188
47 #define IRQ_ENABLE_CLR 0x18c
50 #define MSI0_IRQ_STATUS 0x104
51 #define MSI0_IRQ_ENABLE_SET 0x108
52 #define MSI0_IRQ_ENABLE_CLR 0x10c
53 #define IRQ_STATUS 0x184
54 #define MSI_IRQ_OFFSET 4
56 /* Config space registers */
59 #define to_keystone_pcie(x) container_of(x, struct keystone_pcie, pp)
61 static inline struct pcie_port
*sys_to_pcie(struct pci_sys_data
*sys
)
63 return sys
->private_data
;
66 static inline void update_reg_offset_bit_pos(u32 offset
, u32
*reg_offset
,
69 *reg_offset
= offset
% 8;
70 *bit_pos
= offset
>> 3;
73 phys_addr_t
ks_dw_pcie_get_msi_addr(struct pcie_port
*pp
)
75 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
77 return ks_pcie
->app
.start
+ MSI_IRQ
;
80 void ks_dw_pcie_handle_msi_irq(struct keystone_pcie
*ks_pcie
, int offset
)
82 struct pcie_port
*pp
= &ks_pcie
->pp
;
86 pending
= readl(ks_pcie
->va_app_base
+ MSI0_IRQ_STATUS
+ (offset
<< 4));
89 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
90 * shows 1, 9, 17, 25 and so forth
92 for (src
= 0; src
< 4; src
++) {
93 if (BIT(src
) & pending
) {
94 vector
= offset
+ (src
<< 3);
95 virq
= irq_linear_revmap(pp
->irq_domain
, vector
);
96 dev_dbg(pp
->dev
, "irq: bit %d, vector %d, virq %d\n",
98 generic_handle_irq(virq
);
103 static void ks_dw_pcie_msi_irq_ack(struct irq_data
*d
)
105 u32 offset
, reg_offset
, bit_pos
;
106 struct keystone_pcie
*ks_pcie
;
107 struct msi_desc
*msi
;
108 struct pcie_port
*pp
;
110 msi
= irq_data_get_msi_desc(d
);
111 pp
= sys_to_pcie(msi_desc_to_pci_sysdata(msi
));
112 ks_pcie
= to_keystone_pcie(pp
);
113 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
114 update_reg_offset_bit_pos(offset
, ®_offset
, &bit_pos
);
117 ks_pcie
->va_app_base
+ MSI0_IRQ_STATUS
+ (reg_offset
<< 4));
118 writel(reg_offset
+ MSI_IRQ_OFFSET
, ks_pcie
->va_app_base
+ IRQ_EOI
);
121 void ks_dw_pcie_msi_set_irq(struct pcie_port
*pp
, int irq
)
123 u32 reg_offset
, bit_pos
;
124 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
126 update_reg_offset_bit_pos(irq
, ®_offset
, &bit_pos
);
128 ks_pcie
->va_app_base
+ MSI0_IRQ_ENABLE_SET
+ (reg_offset
<< 4));
131 void ks_dw_pcie_msi_clear_irq(struct pcie_port
*pp
, int irq
)
133 u32 reg_offset
, bit_pos
;
134 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
136 update_reg_offset_bit_pos(irq
, ®_offset
, &bit_pos
);
138 ks_pcie
->va_app_base
+ MSI0_IRQ_ENABLE_CLR
+ (reg_offset
<< 4));
141 static void ks_dw_pcie_msi_irq_mask(struct irq_data
*d
)
143 struct keystone_pcie
*ks_pcie
;
144 struct msi_desc
*msi
;
145 struct pcie_port
*pp
;
148 msi
= irq_data_get_msi_desc(d
);
149 pp
= sys_to_pcie(msi_desc_to_pci_sysdata(msi
));
150 ks_pcie
= to_keystone_pcie(pp
);
151 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
153 /* Mask the end point if PVM implemented */
154 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
155 if (msi
->msi_attrib
.maskbit
)
159 ks_dw_pcie_msi_clear_irq(pp
, offset
);
162 static void ks_dw_pcie_msi_irq_unmask(struct irq_data
*d
)
164 struct keystone_pcie
*ks_pcie
;
165 struct msi_desc
*msi
;
166 struct pcie_port
*pp
;
169 msi
= irq_data_get_msi_desc(d
);
170 pp
= sys_to_pcie(msi_desc_to_pci_sysdata(msi
));
171 ks_pcie
= to_keystone_pcie(pp
);
172 offset
= d
->irq
- irq_linear_revmap(pp
->irq_domain
, 0);
174 /* Mask the end point if PVM implemented */
175 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
176 if (msi
->msi_attrib
.maskbit
)
177 pci_msi_unmask_irq(d
);
180 ks_dw_pcie_msi_set_irq(pp
, offset
);
183 static struct irq_chip ks_dw_pcie_msi_irq_chip
= {
184 .name
= "Keystone-PCIe-MSI-IRQ",
185 .irq_ack
= ks_dw_pcie_msi_irq_ack
,
186 .irq_mask
= ks_dw_pcie_msi_irq_mask
,
187 .irq_unmask
= ks_dw_pcie_msi_irq_unmask
,
190 static int ks_dw_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
191 irq_hw_number_t hwirq
)
193 irq_set_chip_and_handler(irq
, &ks_dw_pcie_msi_irq_chip
,
195 irq_set_chip_data(irq
, domain
->host_data
);
200 static const struct irq_domain_ops ks_dw_pcie_msi_domain_ops
= {
201 .map
= ks_dw_pcie_msi_map
,
204 int ks_dw_pcie_msi_host_init(struct pcie_port
*pp
, struct msi_controller
*chip
)
206 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
209 pp
->irq_domain
= irq_domain_add_linear(ks_pcie
->msi_intc_np
,
211 &ks_dw_pcie_msi_domain_ops
,
213 if (!pp
->irq_domain
) {
214 dev_err(pp
->dev
, "irq domain init failed\n");
218 for (i
= 0; i
< MAX_MSI_IRQS
; i
++)
219 irq_create_mapping(pp
->irq_domain
, i
);
224 void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie
*ks_pcie
)
228 for (i
= 0; i
< MAX_LEGACY_IRQS
; i
++)
229 writel(0x1, ks_pcie
->va_app_base
+ IRQ_ENABLE_SET
+ (i
<< 4));
232 void ks_dw_pcie_handle_legacy_irq(struct keystone_pcie
*ks_pcie
, int offset
)
234 struct pcie_port
*pp
= &ks_pcie
->pp
;
238 pending
= readl(ks_pcie
->va_app_base
+ IRQ_STATUS
+ (offset
<< 4));
240 if (BIT(0) & pending
) {
241 virq
= irq_linear_revmap(ks_pcie
->legacy_irq_domain
, offset
);
242 dev_dbg(pp
->dev
, ": irq: irq_offset %d, virq %d\n", offset
,
244 generic_handle_irq(virq
);
247 /* EOI the INTx interrupt */
248 writel(offset
, ks_pcie
->va_app_base
+ IRQ_EOI
);
251 static void ks_dw_pcie_ack_legacy_irq(struct irq_data
*d
)
255 static void ks_dw_pcie_mask_legacy_irq(struct irq_data
*d
)
259 static void ks_dw_pcie_unmask_legacy_irq(struct irq_data
*d
)
263 static struct irq_chip ks_dw_pcie_legacy_irq_chip
= {
264 .name
= "Keystone-PCI-Legacy-IRQ",
265 .irq_ack
= ks_dw_pcie_ack_legacy_irq
,
266 .irq_mask
= ks_dw_pcie_mask_legacy_irq
,
267 .irq_unmask
= ks_dw_pcie_unmask_legacy_irq
,
270 static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain
*d
,
271 unsigned int irq
, irq_hw_number_t hw_irq
)
273 irq_set_chip_and_handler(irq
, &ks_dw_pcie_legacy_irq_chip
,
275 irq_set_chip_data(irq
, d
->host_data
);
280 static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops
= {
281 .map
= ks_dw_pcie_init_legacy_irq_map
,
282 .xlate
= irq_domain_xlate_onetwocell
,
286 * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
289 * Since modification of dbi_cs2 involves different clock domain, read the
290 * status back to ensure the transition is complete.
292 static void ks_dw_pcie_set_dbi_mode(void __iomem
*reg_virt
)
296 writel(DBI_CS2_EN_VAL
| readl(reg_virt
+ CMD_STATUS
),
297 reg_virt
+ CMD_STATUS
);
300 val
= readl(reg_virt
+ CMD_STATUS
);
301 } while (!(val
& DBI_CS2_EN_VAL
));
305 * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
307 * Since modification of dbi_cs2 involves different clock domain, read the
308 * status back to ensure the transition is complete.
310 static void ks_dw_pcie_clear_dbi_mode(void __iomem
*reg_virt
)
314 writel(~DBI_CS2_EN_VAL
& readl(reg_virt
+ CMD_STATUS
),
315 reg_virt
+ CMD_STATUS
);
318 val
= readl(reg_virt
+ CMD_STATUS
);
319 } while (val
& DBI_CS2_EN_VAL
);
322 void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie
*ks_pcie
)
324 struct pcie_port
*pp
= &ks_pcie
->pp
;
325 u32 start
= pp
->mem
->start
, end
= pp
->mem
->end
;
328 /* Disable BARs for inbound access */
329 ks_dw_pcie_set_dbi_mode(ks_pcie
->va_app_base
);
330 writel(0, pp
->dbi_base
+ PCI_BASE_ADDRESS_0
);
331 writel(0, pp
->dbi_base
+ PCI_BASE_ADDRESS_1
);
332 ks_dw_pcie_clear_dbi_mode(ks_pcie
->va_app_base
);
334 /* Set outbound translation size per window division */
335 writel(CFG_PCIM_WIN_SZ_IDX
& 0x7, ks_pcie
->va_app_base
+ OB_SIZE
);
337 tr_size
= (1 << (CFG_PCIM_WIN_SZ_IDX
& 0x7)) * SZ_1M
;
339 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
340 for (i
= 0; (i
< CFG_PCIM_WIN_CNT
) && (start
< end
); i
++) {
341 writel(start
| 1, ks_pcie
->va_app_base
+ OB_OFFSET_INDEX(i
));
342 writel(0, ks_pcie
->va_app_base
+ OB_OFFSET_HI(i
));
346 /* Enable OB translation */
347 writel(OB_XLAT_EN_VAL
| readl(ks_pcie
->va_app_base
+ CMD_STATUS
),
348 ks_pcie
->va_app_base
+ CMD_STATUS
);
352 * ks_pcie_cfg_setup() - Set up configuration space address for a device
354 * @ks_pcie: ptr to keystone_pcie structure
355 * @bus: Bus number the device is residing on
356 * @devfn: device, function number info
358 * Forms and returns the address of configuration space mapped in PCIESS
359 * address space 0. Also configures CFG_SETUP for remote configuration space
362 * The address space has two regions to access configuration - local and remote.
363 * We access local region for bus 0 (as RC is attached on bus 0) and remote
364 * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
365 * we will do TYPE 0 access as it will be on our secondary bus (logical).
366 * CFG_SETUP is needed only for remote configuration access.
368 static void __iomem
*ks_pcie_cfg_setup(struct keystone_pcie
*ks_pcie
, u8 bus
,
371 u8 device
= PCI_SLOT(devfn
), function
= PCI_FUNC(devfn
);
372 struct pcie_port
*pp
= &ks_pcie
->pp
;
378 regval
= (bus
<< 16) | (device
<< 8) | function
;
381 * Since Bus#1 will be a virtual bus, we need to have TYPE0
388 writel(regval
, ks_pcie
->va_app_base
+ CFG_SETUP
);
389 return pp
->va_cfg0_base
;
392 int ks_dw_pcie_rd_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
393 unsigned int devfn
, int where
, int size
, u32
*val
)
395 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
396 u8 bus_num
= bus
->number
;
399 addr
= ks_pcie_cfg_setup(ks_pcie
, bus_num
, devfn
);
401 return dw_pcie_cfg_read(addr
+ where
, size
, val
);
404 int ks_dw_pcie_wr_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
405 unsigned int devfn
, int where
, int size
, u32 val
)
407 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
408 u8 bus_num
= bus
->number
;
411 addr
= ks_pcie_cfg_setup(ks_pcie
, bus_num
, devfn
);
413 return dw_pcie_cfg_write(addr
+ where
, size
, val
);
417 * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
419 * This sets BAR0 to enable inbound access for MSI_IRQ register
421 void ks_dw_pcie_v3_65_scan_bus(struct pcie_port
*pp
)
423 struct keystone_pcie
*ks_pcie
= to_keystone_pcie(pp
);
425 /* Configure and set up BAR0 */
426 ks_dw_pcie_set_dbi_mode(ks_pcie
->va_app_base
);
429 writel(1, pp
->dbi_base
+ PCI_BASE_ADDRESS_0
);
430 writel(SZ_4K
- 1, pp
->dbi_base
+ PCI_BASE_ADDRESS_0
);
432 ks_dw_pcie_clear_dbi_mode(ks_pcie
->va_app_base
);
435 * For BAR0, just setting bus address for inbound writes (MSI) should
436 * be sufficient. Use physical address to avoid any conflicts.
438 writel(ks_pcie
->app
.start
, pp
->dbi_base
+ PCI_BASE_ADDRESS_0
);
442 * ks_dw_pcie_link_up() - Check if link up
444 int ks_dw_pcie_link_up(struct pcie_port
*pp
)
446 u32 val
= readl(pp
->dbi_base
+ DEBUG0
);
448 return (val
& LTSSM_STATE_MASK
) == LTSSM_STATE_L0
;
451 void ks_dw_pcie_initiate_link_train(struct keystone_pcie
*ks_pcie
)
455 /* Disable Link training */
456 val
= readl(ks_pcie
->va_app_base
+ CMD_STATUS
);
457 val
&= ~LTSSM_EN_VAL
;
458 writel(LTSSM_EN_VAL
| val
, ks_pcie
->va_app_base
+ CMD_STATUS
);
460 /* Initiate Link Training */
461 val
= readl(ks_pcie
->va_app_base
+ CMD_STATUS
);
462 writel(LTSSM_EN_VAL
| val
, ks_pcie
->va_app_base
+ CMD_STATUS
);
466 * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
468 * Ioremap the register resources, initialize legacy irq domain
469 * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
470 * PCI host controller.
472 int __init
ks_dw_pcie_host_init(struct keystone_pcie
*ks_pcie
,
473 struct device_node
*msi_intc_np
)
475 struct pcie_port
*pp
= &ks_pcie
->pp
;
476 struct platform_device
*pdev
= to_platform_device(pp
->dev
);
477 struct resource
*res
;
479 /* Index 0 is the config reg. space address */
480 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
481 pp
->dbi_base
= devm_ioremap_resource(pp
->dev
, res
);
482 if (IS_ERR(pp
->dbi_base
))
483 return PTR_ERR(pp
->dbi_base
);
486 * We set these same and is used in pcie rd/wr_other_conf
489 pp
->va_cfg0_base
= pp
->dbi_base
+ SPACE0_REMOTE_CFG_OFFSET
;
490 pp
->va_cfg1_base
= pp
->va_cfg0_base
;
492 /* Index 1 is the application reg. space address */
493 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
494 ks_pcie
->va_app_base
= devm_ioremap_resource(pp
->dev
, res
);
495 if (IS_ERR(ks_pcie
->va_app_base
))
496 return PTR_ERR(ks_pcie
->va_app_base
);
500 /* Create legacy IRQ domain */
501 ks_pcie
->legacy_irq_domain
=
502 irq_domain_add_linear(ks_pcie
->legacy_intc_np
,
504 &ks_dw_pcie_legacy_irq_domain_ops
,
506 if (!ks_pcie
->legacy_irq_domain
) {
507 dev_err(pp
->dev
, "Failed to add irq domain for legacy irqs\n");
511 return dw_pcie_host_init(pp
);