2 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/irqchip/chained_irq.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_pci.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
28 #define RP_TX_REG0 0x2000
29 #define RP_TX_REG1 0x2004
30 #define RP_TX_CNTRL 0x2008
33 #define RP_RXCPL_STATUS 0x2010
34 #define RP_RXCPL_EOP 0x2
35 #define RP_RXCPL_SOP 0x1
36 #define RP_RXCPL_REG0 0x2014
37 #define RP_RXCPL_REG1 0x2018
38 #define P2A_INT_STATUS 0x3060
39 #define P2A_INT_STS_ALL 0xf
40 #define P2A_INT_ENABLE 0x3070
41 #define P2A_INT_ENA_ALL 0xf
42 #define RP_LTSSM 0x3c64
43 #define RP_LTSSM_MASK 0x1f
46 /* TLP configuration type 0 and 1 */
47 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
48 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
49 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
50 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
51 #define TLP_PAYLOAD_SIZE 0x01
52 #define TLP_READ_TAG 0x1d
53 #define TLP_WRITE_TAG 0x10
54 #define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
55 #define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
56 #define TLP_CFG_DW2(bus, devfn, offset) \
57 (((bus) << 24) | ((devfn) << 16) | (offset))
58 #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
59 #define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
60 #define TLP_HDR_SIZE 3
69 struct platform_device
*pdev
;
70 void __iomem
*cra_base
;
73 struct irq_domain
*irq_domain
;
74 struct resource bus_range
;
75 struct list_head resources
;
78 struct tlp_rp_regpair_t
{
84 static inline void cra_writel(struct altera_pcie
*pcie
, const u32 value
,
87 writel_relaxed(value
, pcie
->cra_base
+ reg
);
90 static inline u32
cra_readl(struct altera_pcie
*pcie
, const u32 reg
)
92 return readl_relaxed(pcie
->cra_base
+ reg
);
95 static bool altera_pcie_link_is_up(struct altera_pcie
*pcie
)
97 return !!((cra_readl(pcie
, RP_LTSSM
) & RP_LTSSM_MASK
) == LTSSM_L0
);
100 static void altera_pcie_retrain(struct pci_dev
*dev
)
102 u16 linkcap
, linkstat
;
103 struct altera_pcie
*pcie
= dev
->bus
->sysdata
;
105 if (!altera_pcie_link_is_up(pcie
))
109 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
110 * current speed is 2.5 GB/s.
112 pcie_capability_read_word(dev
, PCI_EXP_LNKCAP
, &linkcap
);
114 if ((linkcap
& PCI_EXP_LNKCAP_SLS
) <= PCI_EXP_LNKCAP_SLS_2_5GB
)
117 pcie_capability_read_word(dev
, PCI_EXP_LNKSTA
, &linkstat
);
118 if ((linkstat
& PCI_EXP_LNKSTA_CLS
) == PCI_EXP_LNKSTA_CLS_2_5GB
)
119 pcie_capability_set_word(dev
, PCI_EXP_LNKCTL
,
122 DECLARE_PCI_FIXUP_EARLY(0x1172, PCI_ANY_ID
, altera_pcie_retrain
);
125 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
126 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
127 * using these registers, so it can be reached by DMA from EP devices.
128 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
129 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
130 * should be hidden during enumeration to avoid the sizing and resource
131 * allocation by PCIe core.
133 static bool altera_pcie_hide_rc_bar(struct pci_bus
*bus
, unsigned int devfn
,
136 if (pci_is_root_bus(bus
) && (devfn
== 0) &&
137 (offset
== PCI_BASE_ADDRESS_0
))
143 static void tlp_write_tx(struct altera_pcie
*pcie
,
144 struct tlp_rp_regpair_t
*tlp_rp_regdata
)
146 cra_writel(pcie
, tlp_rp_regdata
->reg0
, RP_TX_REG0
);
147 cra_writel(pcie
, tlp_rp_regdata
->reg1
, RP_TX_REG1
);
148 cra_writel(pcie
, tlp_rp_regdata
->ctrl
, RP_TX_CNTRL
);
151 static bool altera_pcie_valid_config(struct altera_pcie
*pcie
,
152 struct pci_bus
*bus
, int dev
)
154 /* If there is no link, then there is no device */
155 if (bus
->number
!= pcie
->root_bus_nr
) {
156 if (!altera_pcie_link_is_up(pcie
))
160 /* access only one slot on each root port */
161 if (bus
->number
== pcie
->root_bus_nr
&& dev
> 0)
165 * Do not read more than one device on the bus directly attached
166 * to root port, root port can only attach to one downstream port.
168 if (bus
->primary
== pcie
->root_bus_nr
&& dev
> 0)
174 static int tlp_read_packet(struct altera_pcie
*pcie
, u32
*value
)
183 * Minimum 2 loops to read TLP headers and 1 loop to read data
186 for (i
= 0; i
< TLP_LOOP
; i
++) {
187 ctrl
= cra_readl(pcie
, RP_RXCPL_STATUS
);
188 if ((ctrl
& RP_RXCPL_SOP
) || (ctrl
& RP_RXCPL_EOP
) || sop
) {
189 reg0
= cra_readl(pcie
, RP_RXCPL_REG0
);
190 reg1
= cra_readl(pcie
, RP_RXCPL_REG1
);
192 if (ctrl
& RP_RXCPL_SOP
) {
194 comp_status
= TLP_COMP_STATUS(reg1
);
197 if (ctrl
& RP_RXCPL_EOP
) {
199 return PCIBIOS_DEVICE_NOT_FOUND
;
204 return PCIBIOS_SUCCESSFUL
;
210 return PCIBIOS_DEVICE_NOT_FOUND
;
213 static void tlp_write_packet(struct altera_pcie
*pcie
, u32
*headers
,
214 u32 data
, bool align
)
216 struct tlp_rp_regpair_t tlp_rp_regdata
;
218 tlp_rp_regdata
.reg0
= headers
[0];
219 tlp_rp_regdata
.reg1
= headers
[1];
220 tlp_rp_regdata
.ctrl
= RP_TX_SOP
;
221 tlp_write_tx(pcie
, &tlp_rp_regdata
);
224 tlp_rp_regdata
.reg0
= headers
[2];
225 tlp_rp_regdata
.reg1
= 0;
226 tlp_rp_regdata
.ctrl
= 0;
227 tlp_write_tx(pcie
, &tlp_rp_regdata
);
229 tlp_rp_regdata
.reg0
= data
;
230 tlp_rp_regdata
.reg1
= 0;
232 tlp_rp_regdata
.reg0
= headers
[2];
233 tlp_rp_regdata
.reg1
= data
;
236 tlp_rp_regdata
.ctrl
= RP_TX_EOP
;
237 tlp_write_tx(pcie
, &tlp_rp_regdata
);
240 static int tlp_cfg_dword_read(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
241 int where
, u8 byte_en
, u32
*value
)
243 u32 headers
[TLP_HDR_SIZE
];
245 if (bus
== pcie
->root_bus_nr
)
246 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0
);
248 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1
);
250 headers
[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie
->root_bus_nr
, RP_DEVFN
),
251 TLP_READ_TAG
, byte_en
);
252 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
254 tlp_write_packet(pcie
, headers
, 0, false);
256 return tlp_read_packet(pcie
, value
);
259 static int tlp_cfg_dword_write(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
260 int where
, u8 byte_en
, u32 value
)
262 u32 headers
[TLP_HDR_SIZE
];
265 if (bus
== pcie
->root_bus_nr
)
266 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0
);
268 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1
);
270 headers
[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie
->root_bus_nr
, RP_DEVFN
),
271 TLP_WRITE_TAG
, byte_en
);
272 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
274 /* check alignment to Qword */
275 if ((where
& 0x7) == 0)
276 tlp_write_packet(pcie
, headers
, value
, true);
278 tlp_write_packet(pcie
, headers
, value
, false);
280 ret
= tlp_read_packet(pcie
, NULL
);
281 if (ret
!= PCIBIOS_SUCCESSFUL
)
285 * Monitor changes to PCI_PRIMARY_BUS register on root port
286 * and update local copy of root bus number accordingly.
288 if ((bus
== pcie
->root_bus_nr
) && (where
== PCI_PRIMARY_BUS
))
289 pcie
->root_bus_nr
= (u8
)(value
);
291 return PCIBIOS_SUCCESSFUL
;
294 static int altera_pcie_cfg_read(struct pci_bus
*bus
, unsigned int devfn
,
295 int where
, int size
, u32
*value
)
297 struct altera_pcie
*pcie
= bus
->sysdata
;
302 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
303 return PCIBIOS_BAD_REGISTER_NUMBER
;
305 if (!altera_pcie_valid_config(pcie
, bus
, PCI_SLOT(devfn
))) {
307 return PCIBIOS_DEVICE_NOT_FOUND
;
312 byte_en
= 1 << (where
& 3);
315 byte_en
= 3 << (where
& 3);
322 ret
= tlp_cfg_dword_read(pcie
, bus
->number
, devfn
,
323 (where
& ~DWORD_MASK
), byte_en
, &data
);
324 if (ret
!= PCIBIOS_SUCCESSFUL
)
329 *value
= (data
>> (8 * (where
& 0x3))) & 0xff;
332 *value
= (data
>> (8 * (where
& 0x2))) & 0xffff;
339 return PCIBIOS_SUCCESSFUL
;
342 static int altera_pcie_cfg_write(struct pci_bus
*bus
, unsigned int devfn
,
343 int where
, int size
, u32 value
)
345 struct altera_pcie
*pcie
= bus
->sysdata
;
347 u32 shift
= 8 * (where
& 3);
350 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
351 return PCIBIOS_BAD_REGISTER_NUMBER
;
353 if (!altera_pcie_valid_config(pcie
, bus
, PCI_SLOT(devfn
)))
354 return PCIBIOS_DEVICE_NOT_FOUND
;
358 data32
= (value
& 0xff) << shift
;
359 byte_en
= 1 << (where
& 3);
362 data32
= (value
& 0xffff) << shift
;
363 byte_en
= 3 << (where
& 3);
371 return tlp_cfg_dword_write(pcie
, bus
->number
, devfn
,
372 (where
& ~DWORD_MASK
), byte_en
, data32
);
375 static struct pci_ops altera_pcie_ops
= {
376 .read
= altera_pcie_cfg_read
,
377 .write
= altera_pcie_cfg_write
,
380 static int altera_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
381 irq_hw_number_t hwirq
)
383 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
384 irq_set_chip_data(irq
, domain
->host_data
);
389 static const struct irq_domain_ops intx_domain_ops
= {
390 .map
= altera_pcie_intx_map
,
393 static void altera_pcie_isr(struct irq_desc
*desc
)
395 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
396 struct altera_pcie
*pcie
;
397 unsigned long status
;
401 chained_irq_enter(chip
, desc
);
402 pcie
= irq_desc_get_handler_data(desc
);
404 while ((status
= cra_readl(pcie
, P2A_INT_STATUS
)
405 & P2A_INT_STS_ALL
) != 0) {
406 for_each_set_bit(bit
, &status
, INTX_NUM
) {
407 /* clear interrupts */
408 cra_writel(pcie
, 1 << bit
, P2A_INT_STATUS
);
410 virq
= irq_find_mapping(pcie
->irq_domain
, bit
+ 1);
412 generic_handle_irq(virq
);
414 dev_err(&pcie
->pdev
->dev
,
415 "unexpected IRQ, INT%d\n", bit
);
419 chained_irq_exit(chip
, desc
);
422 static void altera_pcie_release_of_pci_ranges(struct altera_pcie
*pcie
)
424 pci_free_resource_list(&pcie
->resources
);
427 static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie
*pcie
)
429 int err
, res_valid
= 0;
430 struct device
*dev
= &pcie
->pdev
->dev
;
431 struct device_node
*np
= dev
->of_node
;
432 struct resource_entry
*win
;
434 err
= of_pci_get_host_bridge_resources(np
, 0, 0xff, &pcie
->resources
,
439 resource_list_for_each_entry(win
, &pcie
->resources
) {
440 struct resource
*parent
, *res
= win
->res
;
442 switch (resource_type(res
)) {
444 parent
= &iomem_resource
;
445 res_valid
|= !(res
->flags
& IORESOURCE_PREFETCH
);
451 err
= devm_request_resource(dev
, parent
, res
);
453 goto out_release_res
;
457 dev_err(dev
, "non-prefetchable memory resource required\n");
459 goto out_release_res
;
465 altera_pcie_release_of_pci_ranges(pcie
);
469 static int altera_pcie_init_irq_domain(struct altera_pcie
*pcie
)
471 struct device
*dev
= &pcie
->pdev
->dev
;
472 struct device_node
*node
= dev
->of_node
;
475 pcie
->irq_domain
= irq_domain_add_linear(node
, INTX_NUM
+ 1,
476 &intx_domain_ops
, pcie
);
477 if (!pcie
->irq_domain
) {
478 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
485 static int altera_pcie_parse_dt(struct altera_pcie
*pcie
)
487 struct resource
*cra
;
488 struct platform_device
*pdev
= pcie
->pdev
;
490 cra
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "Cra");
492 dev_err(&pdev
->dev
, "no Cra memory resource defined\n");
496 pcie
->cra_base
= devm_ioremap_resource(&pdev
->dev
, cra
);
497 if (IS_ERR(pcie
->cra_base
)) {
498 dev_err(&pdev
->dev
, "failed to map cra memory\n");
499 return PTR_ERR(pcie
->cra_base
);
503 pcie
->irq
= platform_get_irq(pdev
, 0);
504 if (pcie
->irq
<= 0) {
505 dev_err(&pdev
->dev
, "failed to get IRQ: %d\n", pcie
->irq
);
509 irq_set_chained_handler_and_data(pcie
->irq
, altera_pcie_isr
, pcie
);
514 static int altera_pcie_probe(struct platform_device
*pdev
)
516 struct altera_pcie
*pcie
;
518 struct pci_bus
*child
;
521 pcie
= devm_kzalloc(&pdev
->dev
, sizeof(*pcie
), GFP_KERNEL
);
527 ret
= altera_pcie_parse_dt(pcie
);
529 dev_err(&pdev
->dev
, "Parsing DT failed\n");
533 INIT_LIST_HEAD(&pcie
->resources
);
535 ret
= altera_pcie_parse_request_of_pci_ranges(pcie
);
537 dev_err(&pdev
->dev
, "Failed add resources\n");
541 ret
= altera_pcie_init_irq_domain(pcie
);
543 dev_err(&pdev
->dev
, "Failed creating IRQ Domain\n");
547 /* clear all interrupts */
548 cra_writel(pcie
, P2A_INT_STS_ALL
, P2A_INT_STATUS
);
549 /* enable all interrupts */
550 cra_writel(pcie
, P2A_INT_ENA_ALL
, P2A_INT_ENABLE
);
552 bus
= pci_scan_root_bus(&pdev
->dev
, pcie
->root_bus_nr
, &altera_pcie_ops
,
553 pcie
, &pcie
->resources
);
557 pci_fixup_irqs(pci_common_swizzle
, of_irq_parse_and_map_pci
);
558 pci_assign_unassigned_bus_resources(bus
);
560 /* Configure PCI Express setting. */
561 list_for_each_entry(child
, &bus
->children
, node
)
562 pcie_bus_configure_settings(child
);
564 pci_bus_add_devices(bus
);
566 platform_set_drvdata(pdev
, pcie
);
570 static const struct of_device_id altera_pcie_of_match
[] = {
571 { .compatible
= "altr,pcie-root-port-1.0", },
574 MODULE_DEVICE_TABLE(of
, altera_pcie_of_match
);
576 static struct platform_driver altera_pcie_driver
= {
577 .probe
= altera_pcie_probe
,
579 .name
= "altera-pcie",
580 .of_match_table
= altera_pcie_of_match
,
581 .suppress_bind_attrs
= true,
585 static int altera_pcie_init(void)
587 return platform_driver_register(&altera_pcie_driver
);
589 module_init(altera_pcie_init
);
591 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
592 MODULE_DESCRIPTION("Altera PCIe host controller driver");
593 MODULE_LICENSE("GPL v2");