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/irqdomain.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/of_pci.h>
17 #include <linux/pci.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
23 #define RP_TX_REG0 0x2000
24 #define RP_TX_REG1 0x2004
25 #define RP_TX_CNTRL 0x2008
28 #define RP_RXCPL_STATUS 0x2010
29 #define RP_RXCPL_EOP 0x2
30 #define RP_RXCPL_SOP 0x1
31 #define RP_RXCPL_REG0 0x2014
32 #define RP_RXCPL_REG1 0x2018
33 #define P2A_INT_STATUS 0x3060
34 #define P2A_INT_STS_ALL 0xf
35 #define P2A_INT_ENABLE 0x3070
36 #define P2A_INT_ENA_ALL 0xf
37 #define RP_LTSSM 0x3c64
38 #define RP_LTSSM_MASK 0x1f
41 #define S10_RP_TX_CNTRL 0x2004
42 #define S10_RP_RXCPL_REG 0x2008
43 #define S10_RP_RXCPL_STATUS 0x200C
44 #define S10_RP_CFG_ADDR(pcie, reg) \
45 (((pcie)->hip_base) + (reg) + (1 << 20))
46 #define S10_RP_SECONDARY(pcie) \
47 readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
49 /* TLP configuration type 0 and 1 */
50 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
51 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
52 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
53 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
54 #define TLP_PAYLOAD_SIZE 0x01
55 #define TLP_READ_TAG 0x1d
56 #define TLP_WRITE_TAG 0x10
58 #define TLP_CFG_DW0(pcie, cfg) \
61 #define TLP_CFG_DW1(pcie, tag, be) \
62 (((PCI_DEVID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
63 #define TLP_CFG_DW2(bus, devfn, offset) \
64 (((bus) << 24) | ((devfn) << 16) | (offset))
65 #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
66 #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
67 #define TLP_HDR_SIZE 3
70 #define LINK_UP_TIMEOUT HZ
71 #define LINK_RETRAIN_TIMEOUT HZ
75 #define S10_TLP_FMTTYPE_CFGRD0 0x05
76 #define S10_TLP_FMTTYPE_CFGRD1 0x04
77 #define S10_TLP_FMTTYPE_CFGWR0 0x45
78 #define S10_TLP_FMTTYPE_CFGWR1 0x44
80 enum altera_pcie_version
{
86 struct platform_device
*pdev
;
87 void __iomem
*cra_base
;
88 void __iomem
*hip_base
;
91 struct irq_domain
*irq_domain
;
92 struct resource bus_range
;
93 const struct altera_pcie_data
*pcie_data
;
96 struct altera_pcie_ops
{
97 int (*tlp_read_pkt
)(struct altera_pcie
*pcie
, u32
*value
);
98 void (*tlp_write_pkt
)(struct altera_pcie
*pcie
, u32
*headers
,
99 u32 data
, bool align
);
100 bool (*get_link_status
)(struct altera_pcie
*pcie
);
101 int (*rp_read_cfg
)(struct altera_pcie
*pcie
, int where
,
102 int size
, u32
*value
);
103 int (*rp_write_cfg
)(struct altera_pcie
*pcie
, u8 busno
,
104 int where
, int size
, u32 value
);
107 struct altera_pcie_data
{
108 const struct altera_pcie_ops
*ops
;
109 enum altera_pcie_version version
;
110 u32 cap_offset
; /* PCIe capability structure register offset */
117 struct tlp_rp_regpair_t
{
123 static inline void cra_writel(struct altera_pcie
*pcie
, const u32 value
,
126 writel_relaxed(value
, pcie
->cra_base
+ reg
);
129 static inline u32
cra_readl(struct altera_pcie
*pcie
, const u32 reg
)
131 return readl_relaxed(pcie
->cra_base
+ reg
);
134 static bool altera_pcie_link_up(struct altera_pcie
*pcie
)
136 return !!((cra_readl(pcie
, RP_LTSSM
) & RP_LTSSM_MASK
) == LTSSM_L0
);
139 static bool s10_altera_pcie_link_up(struct altera_pcie
*pcie
)
141 void __iomem
*addr
= S10_RP_CFG_ADDR(pcie
,
142 pcie
->pcie_data
->cap_offset
+
145 return !!(readw(addr
) & PCI_EXP_LNKSTA_DLLLA
);
149 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
150 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
151 * using these registers, so it can be reached by DMA from EP devices.
152 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
153 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
154 * should be hidden during enumeration to avoid the sizing and resource
155 * allocation by PCIe core.
157 static bool altera_pcie_hide_rc_bar(struct pci_bus
*bus
, unsigned int devfn
,
160 if (pci_is_root_bus(bus
) && (devfn
== 0) &&
161 (offset
== PCI_BASE_ADDRESS_0
))
167 static void tlp_write_tx(struct altera_pcie
*pcie
,
168 struct tlp_rp_regpair_t
*tlp_rp_regdata
)
170 cra_writel(pcie
, tlp_rp_regdata
->reg0
, RP_TX_REG0
);
171 cra_writel(pcie
, tlp_rp_regdata
->reg1
, RP_TX_REG1
);
172 cra_writel(pcie
, tlp_rp_regdata
->ctrl
, RP_TX_CNTRL
);
175 static void s10_tlp_write_tx(struct altera_pcie
*pcie
, u32 reg0
, u32 ctrl
)
177 cra_writel(pcie
, reg0
, RP_TX_REG0
);
178 cra_writel(pcie
, ctrl
, S10_RP_TX_CNTRL
);
181 static bool altera_pcie_valid_device(struct altera_pcie
*pcie
,
182 struct pci_bus
*bus
, int dev
)
184 /* If there is no link, then there is no device */
185 if (bus
->number
!= pcie
->root_bus_nr
) {
186 if (!pcie
->pcie_data
->ops
->get_link_status(pcie
))
190 /* access only one slot on each root port */
191 if (bus
->number
== pcie
->root_bus_nr
&& dev
> 0)
197 static int tlp_read_packet(struct altera_pcie
*pcie
, u32
*value
)
206 * Minimum 2 loops to read TLP headers and 1 loop to read data
209 for (i
= 0; i
< TLP_LOOP
; i
++) {
210 ctrl
= cra_readl(pcie
, RP_RXCPL_STATUS
);
211 if ((ctrl
& RP_RXCPL_SOP
) || (ctrl
& RP_RXCPL_EOP
) || sop
) {
212 reg0
= cra_readl(pcie
, RP_RXCPL_REG0
);
213 reg1
= cra_readl(pcie
, RP_RXCPL_REG1
);
215 if (ctrl
& RP_RXCPL_SOP
) {
217 comp_status
= TLP_COMP_STATUS(reg1
);
220 if (ctrl
& RP_RXCPL_EOP
) {
222 return PCIBIOS_DEVICE_NOT_FOUND
;
227 return PCIBIOS_SUCCESSFUL
;
233 return PCIBIOS_DEVICE_NOT_FOUND
;
236 static int s10_tlp_read_packet(struct altera_pcie
*pcie
, u32
*value
)
242 struct device
*dev
= &pcie
->pdev
->dev
;
244 for (count
= 0; count
< TLP_LOOP
; count
++) {
245 ctrl
= cra_readl(pcie
, S10_RP_RXCPL_STATUS
);
246 if (ctrl
& RP_RXCPL_SOP
) {
248 dw
[0] = cra_readl(pcie
, S10_RP_RXCPL_REG
);
255 /* SOP detection failed, return error */
256 if (count
== TLP_LOOP
)
257 return PCIBIOS_DEVICE_NOT_FOUND
;
262 while (count
< ARRAY_SIZE(dw
)) {
263 ctrl
= cra_readl(pcie
, S10_RP_RXCPL_STATUS
);
264 dw
[count
++] = cra_readl(pcie
, S10_RP_RXCPL_REG
);
265 if (ctrl
& RP_RXCPL_EOP
) {
266 comp_status
= TLP_COMP_STATUS(dw
[1]);
268 return PCIBIOS_DEVICE_NOT_FOUND
;
270 if (value
&& TLP_BYTE_COUNT(dw
[1]) == sizeof(u32
) &&
274 return PCIBIOS_SUCCESSFUL
;
278 dev_warn(dev
, "Malformed TLP packet\n");
280 return PCIBIOS_DEVICE_NOT_FOUND
;
283 static void tlp_write_packet(struct altera_pcie
*pcie
, u32
*headers
,
284 u32 data
, bool align
)
286 struct tlp_rp_regpair_t tlp_rp_regdata
;
288 tlp_rp_regdata
.reg0
= headers
[0];
289 tlp_rp_regdata
.reg1
= headers
[1];
290 tlp_rp_regdata
.ctrl
= RP_TX_SOP
;
291 tlp_write_tx(pcie
, &tlp_rp_regdata
);
294 tlp_rp_regdata
.reg0
= headers
[2];
295 tlp_rp_regdata
.reg1
= 0;
296 tlp_rp_regdata
.ctrl
= 0;
297 tlp_write_tx(pcie
, &tlp_rp_regdata
);
299 tlp_rp_regdata
.reg0
= data
;
300 tlp_rp_regdata
.reg1
= 0;
302 tlp_rp_regdata
.reg0
= headers
[2];
303 tlp_rp_regdata
.reg1
= data
;
306 tlp_rp_regdata
.ctrl
= RP_TX_EOP
;
307 tlp_write_tx(pcie
, &tlp_rp_regdata
);
310 static void s10_tlp_write_packet(struct altera_pcie
*pcie
, u32
*headers
,
311 u32 data
, bool dummy
)
313 s10_tlp_write_tx(pcie
, headers
[0], RP_TX_SOP
);
314 s10_tlp_write_tx(pcie
, headers
[1], 0);
315 s10_tlp_write_tx(pcie
, headers
[2], 0);
316 s10_tlp_write_tx(pcie
, data
, RP_TX_EOP
);
319 static void get_tlp_header(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
320 int where
, u8 byte_en
, bool read
, u32
*headers
)
323 u8 cfg0
= read
? pcie
->pcie_data
->cfgrd0
: pcie
->pcie_data
->cfgwr0
;
324 u8 cfg1
= read
? pcie
->pcie_data
->cfgrd1
: pcie
->pcie_data
->cfgwr1
;
325 u8 tag
= read
? TLP_READ_TAG
: TLP_WRITE_TAG
;
327 if (pcie
->pcie_data
->version
== ALTERA_PCIE_V1
)
328 cfg
= (bus
== pcie
->root_bus_nr
) ? cfg0
: cfg1
;
330 cfg
= (bus
> S10_RP_SECONDARY(pcie
)) ? cfg0
: cfg1
;
332 headers
[0] = TLP_CFG_DW0(pcie
, cfg
);
333 headers
[1] = TLP_CFG_DW1(pcie
, tag
, byte_en
);
334 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
337 static int tlp_cfg_dword_read(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
338 int where
, u8 byte_en
, u32
*value
)
340 u32 headers
[TLP_HDR_SIZE
];
342 get_tlp_header(pcie
, bus
, devfn
, where
, byte_en
, true,
345 pcie
->pcie_data
->ops
->tlp_write_pkt(pcie
, headers
, 0, false);
347 return pcie
->pcie_data
->ops
->tlp_read_pkt(pcie
, value
);
350 static int tlp_cfg_dword_write(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
351 int where
, u8 byte_en
, u32 value
)
353 u32 headers
[TLP_HDR_SIZE
];
356 get_tlp_header(pcie
, bus
, devfn
, where
, byte_en
, false,
359 /* check alignment to Qword */
360 if ((where
& 0x7) == 0)
361 pcie
->pcie_data
->ops
->tlp_write_pkt(pcie
, headers
,
364 pcie
->pcie_data
->ops
->tlp_write_pkt(pcie
, headers
,
367 ret
= pcie
->pcie_data
->ops
->tlp_read_pkt(pcie
, NULL
);
368 if (ret
!= PCIBIOS_SUCCESSFUL
)
372 * Monitor changes to PCI_PRIMARY_BUS register on root port
373 * and update local copy of root bus number accordingly.
375 if ((bus
== pcie
->root_bus_nr
) && (where
== PCI_PRIMARY_BUS
))
376 pcie
->root_bus_nr
= (u8
)(value
);
378 return PCIBIOS_SUCCESSFUL
;
381 static int s10_rp_read_cfg(struct altera_pcie
*pcie
, int where
,
382 int size
, u32
*value
)
384 void __iomem
*addr
= S10_RP_CFG_ADDR(pcie
, where
);
388 *value
= readb(addr
);
391 *value
= readw(addr
);
394 *value
= readl(addr
);
398 return PCIBIOS_SUCCESSFUL
;
401 static int s10_rp_write_cfg(struct altera_pcie
*pcie
, u8 busno
,
402 int where
, int size
, u32 value
)
404 void __iomem
*addr
= S10_RP_CFG_ADDR(pcie
, where
);
419 * Monitor changes to PCI_PRIMARY_BUS register on root port
420 * and update local copy of root bus number accordingly.
422 if (busno
== pcie
->root_bus_nr
&& where
== PCI_PRIMARY_BUS
)
423 pcie
->root_bus_nr
= value
& 0xff;
425 return PCIBIOS_SUCCESSFUL
;
428 static int _altera_pcie_cfg_read(struct altera_pcie
*pcie
, u8 busno
,
429 unsigned int devfn
, int where
, int size
,
436 if (busno
== pcie
->root_bus_nr
&& pcie
->pcie_data
->ops
->rp_read_cfg
)
437 return pcie
->pcie_data
->ops
->rp_read_cfg(pcie
, where
,
442 byte_en
= 1 << (where
& 3);
445 byte_en
= 3 << (where
& 3);
452 ret
= tlp_cfg_dword_read(pcie
, busno
, devfn
,
453 (where
& ~DWORD_MASK
), byte_en
, &data
);
454 if (ret
!= PCIBIOS_SUCCESSFUL
)
459 *value
= (data
>> (8 * (where
& 0x3))) & 0xff;
462 *value
= (data
>> (8 * (where
& 0x2))) & 0xffff;
469 return PCIBIOS_SUCCESSFUL
;
472 static int _altera_pcie_cfg_write(struct altera_pcie
*pcie
, u8 busno
,
473 unsigned int devfn
, int where
, int size
,
477 u32 shift
= 8 * (where
& 3);
480 if (busno
== pcie
->root_bus_nr
&& pcie
->pcie_data
->ops
->rp_write_cfg
)
481 return pcie
->pcie_data
->ops
->rp_write_cfg(pcie
, busno
,
486 data32
= (value
& 0xff) << shift
;
487 byte_en
= 1 << (where
& 3);
490 data32
= (value
& 0xffff) << shift
;
491 byte_en
= 3 << (where
& 3);
499 return tlp_cfg_dword_write(pcie
, busno
, devfn
, (where
& ~DWORD_MASK
),
503 static int altera_pcie_cfg_read(struct pci_bus
*bus
, unsigned int devfn
,
504 int where
, int size
, u32
*value
)
506 struct altera_pcie
*pcie
= bus
->sysdata
;
508 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
509 return PCIBIOS_BAD_REGISTER_NUMBER
;
511 if (!altera_pcie_valid_device(pcie
, bus
, PCI_SLOT(devfn
)))
512 return PCIBIOS_DEVICE_NOT_FOUND
;
514 return _altera_pcie_cfg_read(pcie
, bus
->number
, devfn
, where
, size
,
518 static int altera_pcie_cfg_write(struct pci_bus
*bus
, unsigned int devfn
,
519 int where
, int size
, u32 value
)
521 struct altera_pcie
*pcie
= bus
->sysdata
;
523 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
524 return PCIBIOS_BAD_REGISTER_NUMBER
;
526 if (!altera_pcie_valid_device(pcie
, bus
, PCI_SLOT(devfn
)))
527 return PCIBIOS_DEVICE_NOT_FOUND
;
529 return _altera_pcie_cfg_write(pcie
, bus
->number
, devfn
, where
, size
,
533 static struct pci_ops altera_pcie_ops
= {
534 .read
= altera_pcie_cfg_read
,
535 .write
= altera_pcie_cfg_write
,
538 static int altera_read_cap_word(struct altera_pcie
*pcie
, u8 busno
,
539 unsigned int devfn
, int offset
, u16
*value
)
544 ret
= _altera_pcie_cfg_read(pcie
, busno
, devfn
,
545 pcie
->pcie_data
->cap_offset
+ offset
,
552 static int altera_write_cap_word(struct altera_pcie
*pcie
, u8 busno
,
553 unsigned int devfn
, int offset
, u16 value
)
555 return _altera_pcie_cfg_write(pcie
, busno
, devfn
,
556 pcie
->pcie_data
->cap_offset
+ offset
,
561 static void altera_wait_link_retrain(struct altera_pcie
*pcie
)
563 struct device
*dev
= &pcie
->pdev
->dev
;
565 unsigned long start_jiffies
;
567 /* Wait for link training end. */
568 start_jiffies
= jiffies
;
570 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
571 PCI_EXP_LNKSTA
, ®16
);
572 if (!(reg16
& PCI_EXP_LNKSTA_LT
))
575 if (time_after(jiffies
, start_jiffies
+ LINK_RETRAIN_TIMEOUT
)) {
576 dev_err(dev
, "link retrain timeout\n");
582 /* Wait for link is up */
583 start_jiffies
= jiffies
;
585 if (pcie
->pcie_data
->ops
->get_link_status(pcie
))
588 if (time_after(jiffies
, start_jiffies
+ LINK_UP_TIMEOUT
)) {
589 dev_err(dev
, "link up timeout\n");
596 static void altera_pcie_retrain(struct altera_pcie
*pcie
)
598 u16 linkcap
, linkstat
, linkctl
;
600 if (!pcie
->pcie_data
->ops
->get_link_status(pcie
))
604 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
605 * current speed is 2.5 GB/s.
607 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
, PCI_EXP_LNKCAP
,
609 if ((linkcap
& PCI_EXP_LNKCAP_SLS
) <= PCI_EXP_LNKCAP_SLS_2_5GB
)
612 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
, PCI_EXP_LNKSTA
,
614 if ((linkstat
& PCI_EXP_LNKSTA_CLS
) == PCI_EXP_LNKSTA_CLS_2_5GB
) {
615 altera_read_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
616 PCI_EXP_LNKCTL
, &linkctl
);
617 linkctl
|= PCI_EXP_LNKCTL_RL
;
618 altera_write_cap_word(pcie
, pcie
->root_bus_nr
, RP_DEVFN
,
619 PCI_EXP_LNKCTL
, linkctl
);
621 altera_wait_link_retrain(pcie
);
625 static int altera_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
626 irq_hw_number_t hwirq
)
628 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
629 irq_set_chip_data(irq
, domain
->host_data
);
633 static const struct irq_domain_ops intx_domain_ops
= {
634 .map
= altera_pcie_intx_map
,
635 .xlate
= pci_irqd_intx_xlate
,
638 static void altera_pcie_isr(struct irq_desc
*desc
)
640 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
641 struct altera_pcie
*pcie
;
643 unsigned long status
;
647 chained_irq_enter(chip
, desc
);
648 pcie
= irq_desc_get_handler_data(desc
);
649 dev
= &pcie
->pdev
->dev
;
651 while ((status
= cra_readl(pcie
, P2A_INT_STATUS
)
652 & P2A_INT_STS_ALL
) != 0) {
653 for_each_set_bit(bit
, &status
, PCI_NUM_INTX
) {
654 /* clear interrupts */
655 cra_writel(pcie
, 1 << bit
, P2A_INT_STATUS
);
657 ret
= generic_handle_domain_irq(pcie
->irq_domain
, bit
);
659 dev_err_ratelimited(dev
, "unexpected IRQ, INT%d\n", bit
);
663 chained_irq_exit(chip
, desc
);
666 static int altera_pcie_init_irq_domain(struct altera_pcie
*pcie
)
668 struct device
*dev
= &pcie
->pdev
->dev
;
669 struct device_node
*node
= dev
->of_node
;
672 pcie
->irq_domain
= irq_domain_add_linear(node
, PCI_NUM_INTX
,
673 &intx_domain_ops
, pcie
);
674 if (!pcie
->irq_domain
) {
675 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
682 static void altera_pcie_irq_teardown(struct altera_pcie
*pcie
)
684 irq_set_chained_handler_and_data(pcie
->irq
, NULL
, NULL
);
685 irq_domain_remove(pcie
->irq_domain
);
686 irq_dispose_mapping(pcie
->irq
);
689 static int altera_pcie_parse_dt(struct altera_pcie
*pcie
)
691 struct platform_device
*pdev
= pcie
->pdev
;
693 pcie
->cra_base
= devm_platform_ioremap_resource_byname(pdev
, "Cra");
694 if (IS_ERR(pcie
->cra_base
))
695 return PTR_ERR(pcie
->cra_base
);
697 if (pcie
->pcie_data
->version
== ALTERA_PCIE_V2
) {
699 devm_platform_ioremap_resource_byname(pdev
, "Hip");
700 if (IS_ERR(pcie
->hip_base
))
701 return PTR_ERR(pcie
->hip_base
);
705 pcie
->irq
= platform_get_irq(pdev
, 0);
709 irq_set_chained_handler_and_data(pcie
->irq
, altera_pcie_isr
, pcie
);
713 static void altera_pcie_host_init(struct altera_pcie
*pcie
)
715 altera_pcie_retrain(pcie
);
718 static const struct altera_pcie_ops altera_pcie_ops_1_0
= {
719 .tlp_read_pkt
= tlp_read_packet
,
720 .tlp_write_pkt
= tlp_write_packet
,
721 .get_link_status
= altera_pcie_link_up
,
724 static const struct altera_pcie_ops altera_pcie_ops_2_0
= {
725 .tlp_read_pkt
= s10_tlp_read_packet
,
726 .tlp_write_pkt
= s10_tlp_write_packet
,
727 .get_link_status
= s10_altera_pcie_link_up
,
728 .rp_read_cfg
= s10_rp_read_cfg
,
729 .rp_write_cfg
= s10_rp_write_cfg
,
732 static const struct altera_pcie_data altera_pcie_1_0_data
= {
733 .ops
= &altera_pcie_ops_1_0
,
735 .version
= ALTERA_PCIE_V1
,
736 .cfgrd0
= TLP_FMTTYPE_CFGRD0
,
737 .cfgrd1
= TLP_FMTTYPE_CFGRD1
,
738 .cfgwr0
= TLP_FMTTYPE_CFGWR0
,
739 .cfgwr1
= TLP_FMTTYPE_CFGWR1
,
742 static const struct altera_pcie_data altera_pcie_2_0_data
= {
743 .ops
= &altera_pcie_ops_2_0
,
744 .version
= ALTERA_PCIE_V2
,
746 .cfgrd0
= S10_TLP_FMTTYPE_CFGRD0
,
747 .cfgrd1
= S10_TLP_FMTTYPE_CFGRD1
,
748 .cfgwr0
= S10_TLP_FMTTYPE_CFGWR0
,
749 .cfgwr1
= S10_TLP_FMTTYPE_CFGWR1
,
752 static const struct of_device_id altera_pcie_of_match
[] = {
753 {.compatible
= "altr,pcie-root-port-1.0",
754 .data
= &altera_pcie_1_0_data
},
755 {.compatible
= "altr,pcie-root-port-2.0",
756 .data
= &altera_pcie_2_0_data
},
760 static int altera_pcie_probe(struct platform_device
*pdev
)
762 struct device
*dev
= &pdev
->dev
;
763 struct altera_pcie
*pcie
;
764 struct pci_host_bridge
*bridge
;
766 const struct altera_pcie_data
*data
;
768 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
772 pcie
= pci_host_bridge_priv(bridge
);
774 platform_set_drvdata(pdev
, pcie
);
776 data
= of_device_get_match_data(&pdev
->dev
);
780 pcie
->pcie_data
= data
;
782 ret
= altera_pcie_parse_dt(pcie
);
784 dev_err(dev
, "Parsing DT failed\n");
788 ret
= altera_pcie_init_irq_domain(pcie
);
790 dev_err(dev
, "Failed creating IRQ Domain\n");
794 /* clear all interrupts */
795 cra_writel(pcie
, P2A_INT_STS_ALL
, P2A_INT_STATUS
);
796 /* enable all interrupts */
797 cra_writel(pcie
, P2A_INT_ENA_ALL
, P2A_INT_ENABLE
);
798 altera_pcie_host_init(pcie
);
800 bridge
->sysdata
= pcie
;
801 bridge
->busnr
= pcie
->root_bus_nr
;
802 bridge
->ops
= &altera_pcie_ops
;
804 return pci_host_probe(bridge
);
807 static void altera_pcie_remove(struct platform_device
*pdev
)
809 struct altera_pcie
*pcie
= platform_get_drvdata(pdev
);
810 struct pci_host_bridge
*bridge
= pci_host_bridge_from_priv(pcie
);
812 pci_stop_root_bus(bridge
->bus
);
813 pci_remove_root_bus(bridge
->bus
);
814 altera_pcie_irq_teardown(pcie
);
817 static struct platform_driver altera_pcie_driver
= {
818 .probe
= altera_pcie_probe
,
819 .remove
= altera_pcie_remove
,
821 .name
= "altera-pcie",
822 .of_match_table
= altera_pcie_of_match
,
826 MODULE_DEVICE_TABLE(of
, altera_pcie_of_match
);
827 module_platform_driver(altera_pcie_driver
);
828 MODULE_DESCRIPTION("Altera PCIe host controller driver");
829 MODULE_LICENSE("GPL v2");