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/irqreturn.h>
18 #include <linux/module.h>
20 #include <linux/of_pci.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
24 #include "pcie-designware.h"
25 #include "pci-keystone.h"
27 /* Application register defines */
28 #define LTSSM_EN_VAL 1
29 #define LTSSM_STATE_MASK 0x1f
30 #define LTSSM_STATE_L0 0x11
31 #define DBI_CS2_EN_VAL 0x20
32 #define OB_XLAT_EN_VAL 2
34 /* Application registers */
35 #define CMD_STATUS 0x004
36 #define CFG_SETUP 0x008
38 #define CFG_PCIM_WIN_SZ_IDX 3
39 #define CFG_PCIM_WIN_CNT 32
40 #define SPACE0_REMOTE_CFG_OFFSET 0x1000
41 #define OB_OFFSET_INDEX(n) (0x200 + (8 * n))
42 #define OB_OFFSET_HI(n) (0x204 + (8 * n))
44 /* IRQ register defines */
46 #define IRQ_STATUS 0x184
47 #define IRQ_ENABLE_SET 0x188
48 #define IRQ_ENABLE_CLR 0x18c
51 #define MSI0_IRQ_STATUS 0x104
52 #define MSI0_IRQ_ENABLE_SET 0x108
53 #define MSI0_IRQ_ENABLE_CLR 0x10c
54 #define IRQ_STATUS 0x184
55 #define MSI_IRQ_OFFSET 4
58 #define ERR_AER BIT(5) /* ECRC error */
59 #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
60 #define ERR_CORR BIT(3) /* Correctable error */
61 #define ERR_NONFATAL BIT(2) /* Non-fatal error */
62 #define ERR_FATAL BIT(1) /* Fatal error */
63 #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
64 #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
65 ERR_NONFATAL | ERR_FATAL | ERR_SYS)
66 #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
67 #define ERR_IRQ_STATUS_RAW 0x1c0
68 #define ERR_IRQ_STATUS 0x1c4
69 #define ERR_IRQ_ENABLE_SET 0x1c8
70 #define ERR_IRQ_ENABLE_CLR 0x1cc
72 /* Config space registers */
75 #define to_keystone_pcie(x) container_of(x, struct keystone_pcie, pp)
77 static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
80 *reg_offset = offset % 8;
81 *bit_pos = offset >> 3;
84 phys_addr_t ks_dw_pcie_get_msi_addr(struct pcie_port *pp)
86 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
88 return ks_pcie->app.start + MSI_IRQ;
91 void ks_dw_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
93 struct pcie_port *pp = &ks_pcie->pp;
94 struct device *dev = pp->dev;
98 pending = readl(ks_pcie->va_app_base + MSI0_IRQ_STATUS + (offset << 4));
101 * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
102 * shows 1, 9, 17, 25 and so forth
104 for (src = 0; src < 4; src++) {
105 if (BIT(src) & pending) {
106 vector = offset + (src << 3);
107 virq = irq_linear_revmap(pp->irq_domain, vector);
108 dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
110 generic_handle_irq(virq);
115 static void ks_dw_pcie_msi_irq_ack(struct irq_data *d)
117 u32 offset, reg_offset, bit_pos;
118 struct keystone_pcie *ks_pcie;
119 struct msi_desc *msi;
120 struct pcie_port *pp;
122 msi = irq_data_get_msi_desc(d);
123 pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
124 ks_pcie = to_keystone_pcie(pp);
125 offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
126 update_reg_offset_bit_pos(offset, ®_offset, &bit_pos);
129 ks_pcie->va_app_base + MSI0_IRQ_STATUS + (reg_offset << 4));
130 writel(reg_offset + MSI_IRQ_OFFSET, ks_pcie->va_app_base + IRQ_EOI);
133 void ks_dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
135 u32 reg_offset, bit_pos;
136 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
138 update_reg_offset_bit_pos(irq, ®_offset, &bit_pos);
140 ks_pcie->va_app_base + MSI0_IRQ_ENABLE_SET + (reg_offset << 4));
143 void ks_dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
145 u32 reg_offset, bit_pos;
146 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
148 update_reg_offset_bit_pos(irq, ®_offset, &bit_pos);
150 ks_pcie->va_app_base + MSI0_IRQ_ENABLE_CLR + (reg_offset << 4));
153 static void ks_dw_pcie_msi_irq_mask(struct irq_data *d)
155 struct keystone_pcie *ks_pcie;
156 struct msi_desc *msi;
157 struct pcie_port *pp;
160 msi = irq_data_get_msi_desc(d);
161 pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
162 ks_pcie = to_keystone_pcie(pp);
163 offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
165 /* Mask the end point if PVM implemented */
166 if (IS_ENABLED(CONFIG_PCI_MSI)) {
167 if (msi->msi_attrib.maskbit)
171 ks_dw_pcie_msi_clear_irq(pp, offset);
174 static void ks_dw_pcie_msi_irq_unmask(struct irq_data *d)
176 struct keystone_pcie *ks_pcie;
177 struct msi_desc *msi;
178 struct pcie_port *pp;
181 msi = irq_data_get_msi_desc(d);
182 pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
183 ks_pcie = to_keystone_pcie(pp);
184 offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
186 /* Mask the end point if PVM implemented */
187 if (IS_ENABLED(CONFIG_PCI_MSI)) {
188 if (msi->msi_attrib.maskbit)
189 pci_msi_unmask_irq(d);
192 ks_dw_pcie_msi_set_irq(pp, offset);
195 static struct irq_chip ks_dw_pcie_msi_irq_chip = {
196 .name = "Keystone-PCIe-MSI-IRQ",
197 .irq_ack = ks_dw_pcie_msi_irq_ack,
198 .irq_mask = ks_dw_pcie_msi_irq_mask,
199 .irq_unmask = ks_dw_pcie_msi_irq_unmask,
202 static int ks_dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
203 irq_hw_number_t hwirq)
205 irq_set_chip_and_handler(irq, &ks_dw_pcie_msi_irq_chip,
207 irq_set_chip_data(irq, domain->host_data);
212 static const struct irq_domain_ops ks_dw_pcie_msi_domain_ops = {
213 .map = ks_dw_pcie_msi_map,
216 int ks_dw_pcie_msi_host_init(struct pcie_port *pp, struct msi_controller *chip)
218 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
219 struct device *dev = pp->dev;
222 pp->irq_domain = irq_domain_add_linear(ks_pcie->msi_intc_np,
224 &ks_dw_pcie_msi_domain_ops,
226 if (!pp->irq_domain) {
227 dev_err(dev, "irq domain init failed\n");
231 for (i = 0; i < MAX_MSI_IRQS; i++)
232 irq_create_mapping(pp->irq_domain, i);
237 void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
241 for (i = 0; i < MAX_LEGACY_IRQS; i++)
242 writel(0x1, ks_pcie->va_app_base + IRQ_ENABLE_SET + (i << 4));
245 void ks_dw_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie, int offset)
247 struct pcie_port *pp = &ks_pcie->pp;
248 struct device *dev = pp->dev;
252 pending = readl(ks_pcie->va_app_base + IRQ_STATUS + (offset << 4));
254 if (BIT(0) & pending) {
255 virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
256 dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
257 generic_handle_irq(virq);
260 /* EOI the INTx interrupt */
261 writel(offset, ks_pcie->va_app_base + IRQ_EOI);
264 void ks_dw_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
266 writel(ERR_IRQ_ALL, ks_pcie->va_app_base + ERR_IRQ_ENABLE_SET);
269 irqreturn_t ks_dw_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
273 status = readl(ks_pcie->va_app_base + ERR_IRQ_STATUS_RAW) &
278 if (status & ERR_FATAL_IRQ)
279 dev_err(ks_pcie->pp.dev, "fatal error (status %#010x)\n",
282 /* Ack the IRQ; status bits are RW1C */
283 writel(status, ks_pcie->va_app_base + ERR_IRQ_STATUS);
287 static void ks_dw_pcie_ack_legacy_irq(struct irq_data *d)
291 static void ks_dw_pcie_mask_legacy_irq(struct irq_data *d)
295 static void ks_dw_pcie_unmask_legacy_irq(struct irq_data *d)
299 static struct irq_chip ks_dw_pcie_legacy_irq_chip = {
300 .name = "Keystone-PCI-Legacy-IRQ",
301 .irq_ack = ks_dw_pcie_ack_legacy_irq,
302 .irq_mask = ks_dw_pcie_mask_legacy_irq,
303 .irq_unmask = ks_dw_pcie_unmask_legacy_irq,
306 static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain *d,
307 unsigned int irq, irq_hw_number_t hw_irq)
309 irq_set_chip_and_handler(irq, &ks_dw_pcie_legacy_irq_chip,
311 irq_set_chip_data(irq, d->host_data);
316 static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops = {
317 .map = ks_dw_pcie_init_legacy_irq_map,
318 .xlate = irq_domain_xlate_onetwocell,
322 * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
325 * Since modification of dbi_cs2 involves different clock domain, read the
326 * status back to ensure the transition is complete.
328 static void ks_dw_pcie_set_dbi_mode(void __iomem *reg_virt)
332 writel(DBI_CS2_EN_VAL | readl(reg_virt + CMD_STATUS),
333 reg_virt + CMD_STATUS);
336 val = readl(reg_virt + CMD_STATUS);
337 } while (!(val & DBI_CS2_EN_VAL));
341 * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
343 * Since modification of dbi_cs2 involves different clock domain, read the
344 * status back to ensure the transition is complete.
346 static void ks_dw_pcie_clear_dbi_mode(void __iomem *reg_virt)
350 writel(~DBI_CS2_EN_VAL & readl(reg_virt + CMD_STATUS),
351 reg_virt + CMD_STATUS);
354 val = readl(reg_virt + CMD_STATUS);
355 } while (val & DBI_CS2_EN_VAL);
358 void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
360 struct pcie_port *pp = &ks_pcie->pp;
361 u32 start = pp->mem->start, end = pp->mem->end;
364 /* Disable BARs for inbound access */
365 ks_dw_pcie_set_dbi_mode(ks_pcie->va_app_base);
366 dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, 0);
367 dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_1, 0);
368 ks_dw_pcie_clear_dbi_mode(ks_pcie->va_app_base);
370 /* Set outbound translation size per window division */
371 writel(CFG_PCIM_WIN_SZ_IDX & 0x7, ks_pcie->va_app_base + OB_SIZE);
373 tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
375 /* Using Direct 1:1 mapping of RC <-> PCI memory space */
376 for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
377 writel(start | 1, ks_pcie->va_app_base + OB_OFFSET_INDEX(i));
378 writel(0, ks_pcie->va_app_base + OB_OFFSET_HI(i));
382 /* Enable OB translation */
383 writel(OB_XLAT_EN_VAL | readl(ks_pcie->va_app_base + CMD_STATUS),
384 ks_pcie->va_app_base + CMD_STATUS);
388 * ks_pcie_cfg_setup() - Set up configuration space address for a device
390 * @ks_pcie: ptr to keystone_pcie structure
391 * @bus: Bus number the device is residing on
392 * @devfn: device, function number info
394 * Forms and returns the address of configuration space mapped in PCIESS
395 * address space 0. Also configures CFG_SETUP for remote configuration space
398 * The address space has two regions to access configuration - local and remote.
399 * We access local region for bus 0 (as RC is attached on bus 0) and remote
400 * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
401 * we will do TYPE 0 access as it will be on our secondary bus (logical).
402 * CFG_SETUP is needed only for remote configuration access.
404 static void __iomem *ks_pcie_cfg_setup(struct keystone_pcie *ks_pcie, u8 bus,
407 u8 device = PCI_SLOT(devfn), function = PCI_FUNC(devfn);
408 struct pcie_port *pp = &ks_pcie->pp;
414 regval = (bus << 16) | (device << 8) | function;
417 * Since Bus#1 will be a virtual bus, we need to have TYPE0
424 writel(regval, ks_pcie->va_app_base + CFG_SETUP);
425 return pp->va_cfg0_base;
428 int ks_dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
429 unsigned int devfn, int where, int size, u32 *val)
431 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
432 u8 bus_num = bus->number;
435 addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
437 return dw_pcie_cfg_read(addr + where, size, val);
440 int ks_dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
441 unsigned int devfn, int where, int size, u32 val)
443 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
444 u8 bus_num = bus->number;
447 addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
449 return dw_pcie_cfg_write(addr + where, size, val);
453 * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
455 * This sets BAR0 to enable inbound access for MSI_IRQ register
457 void ks_dw_pcie_v3_65_scan_bus(struct pcie_port *pp)
459 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
461 /* Configure and set up BAR0 */
462 ks_dw_pcie_set_dbi_mode(ks_pcie->va_app_base);
465 dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, 1);
466 dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, SZ_4K - 1);
468 ks_dw_pcie_clear_dbi_mode(ks_pcie->va_app_base);
471 * For BAR0, just setting bus address for inbound writes (MSI) should
472 * be sufficient. Use physical address to avoid any conflicts.
474 dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
478 * ks_dw_pcie_link_up() - Check if link up
480 int ks_dw_pcie_link_up(struct pcie_port *pp)
484 val = dw_pcie_readl_rc(pp, DEBUG0);
485 return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
488 void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
492 /* Disable Link training */
493 val = readl(ks_pcie->va_app_base + CMD_STATUS);
494 val &= ~LTSSM_EN_VAL;
495 writel(LTSSM_EN_VAL | val, ks_pcie->va_app_base + CMD_STATUS);
497 /* Initiate Link Training */
498 val = readl(ks_pcie->va_app_base + CMD_STATUS);
499 writel(LTSSM_EN_VAL | val, ks_pcie->va_app_base + CMD_STATUS);
503 * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
505 * Ioremap the register resources, initialize legacy irq domain
506 * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
507 * PCI host controller.
509 int __init ks_dw_pcie_host_init(struct keystone_pcie *ks_pcie,
510 struct device_node *msi_intc_np)
512 struct pcie_port *pp = &ks_pcie->pp;
513 struct device *dev = pp->dev;
514 struct platform_device *pdev = to_platform_device(dev);
515 struct resource *res;
517 /* Index 0 is the config reg. space address */
518 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
519 pp->dbi_base = devm_ioremap_resource(dev, res);
520 if (IS_ERR(pp->dbi_base))
521 return PTR_ERR(pp->dbi_base);
524 * We set these same and is used in pcie rd/wr_other_conf
527 pp->va_cfg0_base = pp->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
528 pp->va_cfg1_base = pp->va_cfg0_base;
530 /* Index 1 is the application reg. space address */
531 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
532 ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
533 if (IS_ERR(ks_pcie->va_app_base))
534 return PTR_ERR(ks_pcie->va_app_base);
538 /* Create legacy IRQ domain */
539 ks_pcie->legacy_irq_domain =
540 irq_domain_add_linear(ks_pcie->legacy_intc_np,
542 &ks_dw_pcie_legacy_irq_domain_ops,
544 if (!ks_pcie->legacy_irq_domain) {
545 dev_err(dev, "Failed to add irq domain for legacy irqs\n");
549 return dw_pcie_host_init(pp);