1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
5 * Author: Ley Foon Tan <lftan@altera.com>
6 * Description: Altera PCIe host controller driver
9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/irqchip/chained_irq.h>
12 #include <linux/init.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_pci.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
22 #define RP_TX_REG0 0x2000
23 #define RP_TX_REG1 0x2004
24 #define RP_TX_CNTRL 0x2008
27 #define RP_RXCPL_STATUS 0x2010
28 #define RP_RXCPL_EOP 0x2
29 #define RP_RXCPL_SOP 0x1
30 #define RP_RXCPL_REG0 0x2014
31 #define RP_RXCPL_REG1 0x2018
32 #define P2A_INT_STATUS 0x3060
33 #define P2A_INT_STS_ALL 0xf
34 #define P2A_INT_ENABLE 0x3070
35 #define P2A_INT_ENA_ALL 0xf
36 #define RP_LTSSM 0x3c64
37 #define RP_LTSSM_MASK 0x1f
40 #define PCIE_CAP_OFFSET 0x80
41 /* TLP configuration type 0 and 1 */
42 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
43 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
44 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
45 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
46 #define TLP_PAYLOAD_SIZE 0x01
47 #define TLP_READ_TAG 0x1d
48 #define TLP_WRITE_TAG 0x10
50 #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
51 #define TLP_CFGRD_DW0(pcie, bus) \
52 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGRD0 \
53 : TLP_FMTTYPE_CFGRD1) << 24) | \
55 #define TLP_CFGWR_DW0(pcie, bus) \
56 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGWR0 \
57 : TLP_FMTTYPE_CFGWR1) << 24) | \
59 #define TLP_CFG_DW1(pcie, tag, be) \
60 (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
61 #define TLP_CFG_DW2(bus, devfn, offset) \
62 (((bus) << 24) | ((devfn) << 16) | (offset))
63 #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
64 #define TLP_HDR_SIZE 3
67 #define LINK_UP_TIMEOUT HZ
68 #define LINK_RETRAIN_TIMEOUT HZ
73 struct platform_device
*pdev
;
74 void __iomem
*cra_base
; /* DT Cra */
77 struct irq_domain
*irq_domain
;
78 struct resource bus_range
;
79 struct list_head resources
;
82 struct tlp_rp_regpair_t
{
88 static inline void cra_writel(struct altera_pcie
*pcie
, const u32 value
,
91 writel_relaxed(value
, pcie
->cra_base
+ reg
);
94 static inline u32
cra_readl(struct altera_pcie
*pcie
, const u32 reg
)
96 return readl_relaxed(pcie
->cra_base
+ reg
);
99 static bool altera_pcie_link_up(struct altera_pcie
*pcie
)
101 return !!((cra_readl(pcie
, RP_LTSSM
) & RP_LTSSM_MASK
) == LTSSM_L0
);
105 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
106 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
107 * using these registers, so it can be reached by DMA from EP devices.
108 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
109 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
110 * should be hidden during enumeration to avoid the sizing and resource
111 * allocation by PCIe core.
113 static bool altera_pcie_hide_rc_bar(struct pci_bus
*bus
, unsigned int devfn
,
116 if (pci_is_root_bus(bus
) && (devfn
== 0) &&
117 (offset
== PCI_BASE_ADDRESS_0
))
123 static void tlp_write_tx(struct altera_pcie
*pcie
,
124 struct tlp_rp_regpair_t
*tlp_rp_regdata
)
126 cra_writel(pcie
, tlp_rp_regdata
->reg0
, RP_TX_REG0
);
127 cra_writel(pcie
, tlp_rp_regdata
->reg1
, RP_TX_REG1
);
128 cra_writel(pcie
, tlp_rp_regdata
->ctrl
, RP_TX_CNTRL
);
131 static bool altera_pcie_valid_device(struct altera_pcie
*pcie
,
132 struct pci_bus
*bus
, int dev
)
134 /* If there is no link, then there is no device */
135 if (bus
->number
!= pcie
->root_bus_nr
) {
136 if (!altera_pcie_link_up(pcie
))
140 /* access only one slot on each root port */
141 if (bus
->number
== pcie
->root_bus_nr
&& dev
> 0)
147 static int tlp_read_packet(struct altera_pcie
*pcie
, u32
*value
)
156 * Minimum 2 loops to read TLP headers and 1 loop to read data
159 for (i
= 0; i
< TLP_LOOP
; i
++) {
160 ctrl
= cra_readl(pcie
, RP_RXCPL_STATUS
);
161 if ((ctrl
& RP_RXCPL_SOP
) || (ctrl
& RP_RXCPL_EOP
) || sop
) {
162 reg0
= cra_readl(pcie
, RP_RXCPL_REG0
);
163 reg1
= cra_readl(pcie
, RP_RXCPL_REG1
);
165 if (ctrl
& RP_RXCPL_SOP
) {
167 comp_status
= TLP_COMP_STATUS(reg1
);
170 if (ctrl
& RP_RXCPL_EOP
) {
172 return PCIBIOS_DEVICE_NOT_FOUND
;
177 return PCIBIOS_SUCCESSFUL
;
183 return PCIBIOS_DEVICE_NOT_FOUND
;
186 static void tlp_write_packet(struct altera_pcie
*pcie
, u32
*headers
,
187 u32 data
, bool align
)
189 struct tlp_rp_regpair_t tlp_rp_regdata
;
191 tlp_rp_regdata
.reg0
= headers
[0];
192 tlp_rp_regdata
.reg1
= headers
[1];
193 tlp_rp_regdata
.ctrl
= RP_TX_SOP
;
194 tlp_write_tx(pcie
, &tlp_rp_regdata
);
197 tlp_rp_regdata
.reg0
= headers
[2];
198 tlp_rp_regdata
.reg1
= 0;
199 tlp_rp_regdata
.ctrl
= 0;
200 tlp_write_tx(pcie
, &tlp_rp_regdata
);
202 tlp_rp_regdata
.reg0
= data
;
203 tlp_rp_regdata
.reg1
= 0;
205 tlp_rp_regdata
.reg0
= headers
[2];
206 tlp_rp_regdata
.reg1
= data
;
209 tlp_rp_regdata
.ctrl
= RP_TX_EOP
;
210 tlp_write_tx(pcie
, &tlp_rp_regdata
);
213 static int tlp_cfg_dword_read(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
214 int where
, u8 byte_en
, u32
*value
)
216 u32 headers
[TLP_HDR_SIZE
];
218 headers
[0] = TLP_CFGRD_DW0(pcie
, bus
);
219 headers
[1] = TLP_CFG_DW1(pcie
, TLP_READ_TAG
, byte_en
);
220 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
222 tlp_write_packet(pcie
, headers
, 0, false);
224 return tlp_read_packet(pcie
, value
);
227 static int tlp_cfg_dword_write(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
228 int where
, u8 byte_en
, u32 value
)
230 u32 headers
[TLP_HDR_SIZE
];
233 headers
[0] = TLP_CFGWR_DW0(pcie
, bus
);
234 headers
[1] = TLP_CFG_DW1(pcie
, TLP_WRITE_TAG
, byte_en
);
235 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
237 /* check alignment to Qword */
238 if ((where
& 0x7) == 0)
239 tlp_write_packet(pcie
, headers
, value
, true);
241 tlp_write_packet(pcie
, headers
, value
, false);
243 ret
= tlp_read_packet(pcie
, NULL
);
244 if (ret
!= PCIBIOS_SUCCESSFUL
)
248 * Monitor changes to PCI_PRIMARY_BUS register on root port
249 * and update local copy of root bus number accordingly.
251 if ((bus
== pcie
->root_bus_nr
) && (where
== PCI_PRIMARY_BUS
))
252 pcie
->root_bus_nr
= (u8
)(value
);
254 return PCIBIOS_SUCCESSFUL
;
257 static int _altera_pcie_cfg_read(struct altera_pcie
*pcie
, u8 busno
,
258 unsigned int devfn
, int where
, int size
,
267 byte_en
= 1 << (where
& 3);
270 byte_en
= 3 << (where
& 3);
277 ret
= tlp_cfg_dword_read(pcie
, busno
, devfn
,
278 (where
& ~DWORD_MASK
), byte_en
, &data
);
279 if (ret
!= PCIBIOS_SUCCESSFUL
)
284 *value
= (data
>> (8 * (where
& 0x3))) & 0xff;
287 *value
= (data
>> (8 * (where
& 0x2))) & 0xffff;
294 return PCIBIOS_SUCCESSFUL
;
297 static int _altera_pcie_cfg_write(struct altera_pcie
*pcie
, u8 busno
,
298 unsigned int devfn
, int where
, int size
,
302 u32 shift
= 8 * (where
& 3);
307 data32
= (value
& 0xff) << shift
;
308 byte_en
= 1 << (where
& 3);
311 data32
= (value
& 0xffff) << shift
;
312 byte_en
= 3 << (where
& 3);
320 return tlp_cfg_dword_write(pcie
, busno
, devfn
, (where
& ~DWORD_MASK
),
324 static int altera_pcie_cfg_read(struct pci_bus
*bus
, unsigned int devfn
,
325 int where
, int size
, u32
*value
)
327 struct altera_pcie
*pcie
= bus
->sysdata
;
329 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
330 return PCIBIOS_BAD_REGISTER_NUMBER
;
332 if (!altera_pcie_valid_device(pcie
, bus
, PCI_SLOT(devfn
))) {
334 return PCIBIOS_DEVICE_NOT_FOUND
;
337 return _altera_pcie_cfg_read(pcie
, bus
->number
, devfn
, where
, size
,
341 static int altera_pcie_cfg_write(struct pci_bus
*bus
, unsigned int devfn
,
342 int where
, int size
, u32 value
)
344 struct altera_pcie
*pcie
= bus
->sysdata
;
346 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
347 return PCIBIOS_BAD_REGISTER_NUMBER
;
349 if (!altera_pcie_valid_device(pcie
, bus
, PCI_SLOT(devfn
)))
350 return PCIBIOS_DEVICE_NOT_FOUND
;
352 return _altera_pcie_cfg_write(pcie
, bus
->number
, devfn
, where
, size
,
356 static struct pci_ops altera_pcie_ops
= {
357 .read
= altera_pcie_cfg_read
,
358 .write
= altera_pcie_cfg_write
,
361 static int altera_read_cap_word(struct altera_pcie
*pcie
, u8 busno
,
362 unsigned int devfn
, int offset
, u16
*value
)
367 ret
= _altera_pcie_cfg_read(pcie
, busno
, devfn
,
368 PCIE_CAP_OFFSET
+ offset
, sizeof(*value
),
374 static int altera_write_cap_word(struct altera_pcie
*pcie
, u8 busno
,
375 unsigned int devfn
, int offset
, u16 value
)
377 return _altera_pcie_cfg_write(pcie
, busno
, devfn
,
378 PCIE_CAP_OFFSET
+ offset
, sizeof(value
),
382 static void altera_wait_link_retrain(struct altera_pcie
*pcie
)
384 struct device
*dev
= &pcie
->pdev
->dev
;
386 unsigned long start_jiffies
;
388 /* Wait for link training end. */
389 start_jiffies
= jiffies
;
391 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
392 PCI_EXP_LNKSTA
, ®16
);
393 if (!(reg16
& PCI_EXP_LNKSTA_LT
))
396 if (time_after(jiffies
, start_jiffies
+ LINK_RETRAIN_TIMEOUT
)) {
397 dev_err(dev
, "link retrain timeout\n");
403 /* Wait for link is up */
404 start_jiffies
= jiffies
;
406 if (altera_pcie_link_up(pcie
))
409 if (time_after(jiffies
, start_jiffies
+ LINK_UP_TIMEOUT
)) {
410 dev_err(dev
, "link up timeout\n");
417 static void altera_pcie_retrain(struct altera_pcie
*pcie
)
419 u16 linkcap
, linkstat
, linkctl
;
421 if (!altera_pcie_link_up(pcie
))
425 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
426 * current speed is 2.5 GB/s.
428 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
, PCI_EXP_LNKCAP
,
430 if ((linkcap
& PCI_EXP_LNKCAP_SLS
) <= PCI_EXP_LNKCAP_SLS_2_5GB
)
433 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
, PCI_EXP_LNKSTA
,
435 if ((linkstat
& PCI_EXP_LNKSTA_CLS
) == PCI_EXP_LNKSTA_CLS_2_5GB
) {
436 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
437 PCI_EXP_LNKCTL
, &linkctl
);
438 linkctl
|= PCI_EXP_LNKCTL_RL
;
439 altera_write_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
440 PCI_EXP_LNKCTL
, linkctl
);
442 altera_wait_link_retrain(pcie
);
446 static int altera_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
447 irq_hw_number_t hwirq
)
449 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
450 irq_set_chip_data(irq
, domain
->host_data
);
454 static const struct irq_domain_ops intx_domain_ops
= {
455 .map
= altera_pcie_intx_map
,
456 .xlate
= pci_irqd_intx_xlate
,
459 static void altera_pcie_isr(struct irq_desc
*desc
)
461 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
462 struct altera_pcie
*pcie
;
464 unsigned long status
;
468 chained_irq_enter(chip
, desc
);
469 pcie
= irq_desc_get_handler_data(desc
);
470 dev
= &pcie
->pdev
->dev
;
472 while ((status
= cra_readl(pcie
, P2A_INT_STATUS
)
473 & P2A_INT_STS_ALL
) != 0) {
474 for_each_set_bit(bit
, &status
, PCI_NUM_INTX
) {
475 /* clear interrupts */
476 cra_writel(pcie
, 1 << bit
, P2A_INT_STATUS
);
478 virq
= irq_find_mapping(pcie
->irq_domain
, bit
);
480 generic_handle_irq(virq
);
482 dev_err(dev
, "unexpected IRQ, INT%d\n", bit
);
486 chained_irq_exit(chip
, desc
);
489 static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie
*pcie
)
491 int err
, res_valid
= 0;
492 struct device
*dev
= &pcie
->pdev
->dev
;
493 struct resource_entry
*win
;
495 err
= devm_of_pci_get_host_bridge_resources(dev
, 0, 0xff,
496 &pcie
->resources
, NULL
);
500 err
= devm_request_pci_bus_resources(dev
, &pcie
->resources
);
502 goto out_release_res
;
504 resource_list_for_each_entry(win
, &pcie
->resources
) {
505 struct resource
*res
= win
->res
;
507 if (resource_type(res
) == IORESOURCE_MEM
)
508 res_valid
|= !(res
->flags
& IORESOURCE_PREFETCH
);
514 dev_err(dev
, "non-prefetchable memory resource required\n");
518 pci_free_resource_list(&pcie
->resources
);
522 static int altera_pcie_init_irq_domain(struct altera_pcie
*pcie
)
524 struct device
*dev
= &pcie
->pdev
->dev
;
525 struct device_node
*node
= dev
->of_node
;
528 pcie
->irq_domain
= irq_domain_add_linear(node
, PCI_NUM_INTX
,
529 &intx_domain_ops
, pcie
);
530 if (!pcie
->irq_domain
) {
531 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
538 static int altera_pcie_parse_dt(struct altera_pcie
*pcie
)
540 struct device
*dev
= &pcie
->pdev
->dev
;
541 struct platform_device
*pdev
= pcie
->pdev
;
542 struct resource
*cra
;
544 cra
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "Cra");
545 pcie
->cra_base
= devm_ioremap_resource(dev
, cra
);
546 if (IS_ERR(pcie
->cra_base
))
547 return PTR_ERR(pcie
->cra_base
);
550 pcie
->irq
= platform_get_irq(pdev
, 0);
552 dev_err(dev
, "failed to get IRQ: %d\n", pcie
->irq
);
556 irq_set_chained_handler_and_data(pcie
->irq
, altera_pcie_isr
, pcie
);
560 static void altera_pcie_host_init(struct altera_pcie
*pcie
)
562 altera_pcie_retrain(pcie
);
565 static int altera_pcie_probe(struct platform_device
*pdev
)
567 struct device
*dev
= &pdev
->dev
;
568 struct altera_pcie
*pcie
;
570 struct pci_bus
*child
;
571 struct pci_host_bridge
*bridge
;
574 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
578 pcie
= pci_host_bridge_priv(bridge
);
581 ret
= altera_pcie_parse_dt(pcie
);
583 dev_err(dev
, "Parsing DT failed\n");
587 INIT_LIST_HEAD(&pcie
->resources
);
589 ret
= altera_pcie_parse_request_of_pci_ranges(pcie
);
591 dev_err(dev
, "Failed add resources\n");
595 ret
= altera_pcie_init_irq_domain(pcie
);
597 dev_err(dev
, "Failed creating IRQ Domain\n");
601 /* clear all interrupts */
602 cra_writel(pcie
, P2A_INT_STS_ALL
, P2A_INT_STATUS
);
603 /* enable all interrupts */
604 cra_writel(pcie
, P2A_INT_ENA_ALL
, P2A_INT_ENABLE
);
605 altera_pcie_host_init(pcie
);
607 list_splice_init(&pcie
->resources
, &bridge
->windows
);
608 bridge
->dev
.parent
= dev
;
609 bridge
->sysdata
= pcie
;
610 bridge
->busnr
= pcie
->root_bus_nr
;
611 bridge
->ops
= &altera_pcie_ops
;
612 bridge
->map_irq
= of_irq_parse_and_map_pci
;
613 bridge
->swizzle_irq
= pci_common_swizzle
;
615 ret
= pci_scan_root_bus_bridge(bridge
);
621 pci_assign_unassigned_bus_resources(bus
);
623 /* Configure PCI Express setting. */
624 list_for_each_entry(child
, &bus
->children
, node
)
625 pcie_bus_configure_settings(child
);
627 pci_bus_add_devices(bus
);
631 static const struct of_device_id altera_pcie_of_match
[] = {
632 { .compatible
= "altr,pcie-root-port-1.0", },
636 static struct platform_driver altera_pcie_driver
= {
637 .probe
= altera_pcie_probe
,
639 .name
= "altera-pcie",
640 .of_match_table
= altera_pcie_of_match
,
641 .suppress_bind_attrs
= true,
645 builtin_platform_driver(altera_pcie_driver
);