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
45 /* TLP configuration type 0 and 1 */
46 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
47 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
48 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
49 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
50 #define TLP_PAYLOAD_SIZE 0x01
51 #define TLP_READ_TAG 0x1d
52 #define TLP_WRITE_TAG 0x10
53 #define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
54 #define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
55 #define TLP_CFG_DW2(bus, devfn, offset) \
56 (((bus) << 24) | ((devfn) << 16) | (offset))
57 #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
58 #define TLP_HDR_SIZE 3
66 struct platform_device
*pdev
;
67 void __iomem
*cra_base
;
70 struct irq_domain
*irq_domain
;
71 struct resource bus_range
;
72 struct list_head resources
;
75 struct tlp_rp_regpair_t
{
81 static void altera_pcie_retrain(struct pci_dev
*dev
)
83 u16 linkcap
, linkstat
;
86 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
87 * current speed is 2.5 GB/s.
89 pcie_capability_read_word(dev
, PCI_EXP_LNKCAP
, &linkcap
);
91 if ((linkcap
& PCI_EXP_LNKCAP_SLS
) <= PCI_EXP_LNKCAP_SLS_2_5GB
)
94 pcie_capability_read_word(dev
, PCI_EXP_LNKSTA
, &linkstat
);
95 if ((linkstat
& PCI_EXP_LNKSTA_CLS
) == PCI_EXP_LNKSTA_CLS_2_5GB
)
96 pcie_capability_set_word(dev
, PCI_EXP_LNKCTL
,
99 DECLARE_PCI_FIXUP_EARLY(0x1172, PCI_ANY_ID
, altera_pcie_retrain
);
102 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
103 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
104 * using these registers, so it can be reached by DMA from EP devices.
105 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
106 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
107 * should be hidden during enumeration to avoid the sizing and resource
108 * allocation by PCIe core.
110 static bool altera_pcie_hide_rc_bar(struct pci_bus
*bus
, unsigned int devfn
,
113 if (pci_is_root_bus(bus
) && (devfn
== 0) &&
114 (offset
== PCI_BASE_ADDRESS_0
))
120 static inline void cra_writel(struct altera_pcie
*pcie
, const u32 value
,
123 writel_relaxed(value
, pcie
->cra_base
+ reg
);
126 static inline u32
cra_readl(struct altera_pcie
*pcie
, const u32 reg
)
128 return readl_relaxed(pcie
->cra_base
+ reg
);
131 static void tlp_write_tx(struct altera_pcie
*pcie
,
132 struct tlp_rp_regpair_t
*tlp_rp_regdata
)
134 cra_writel(pcie
, tlp_rp_regdata
->reg0
, RP_TX_REG0
);
135 cra_writel(pcie
, tlp_rp_regdata
->reg1
, RP_TX_REG1
);
136 cra_writel(pcie
, tlp_rp_regdata
->ctrl
, RP_TX_CNTRL
);
139 static bool altera_pcie_link_is_up(struct altera_pcie
*pcie
)
141 return !!(cra_readl(pcie
, RP_LTSSM
) & LTSSM_L0
);
144 static bool altera_pcie_valid_config(struct altera_pcie
*pcie
,
145 struct pci_bus
*bus
, int dev
)
147 /* If there is no link, then there is no device */
148 if (bus
->number
!= pcie
->root_bus_nr
) {
149 if (!altera_pcie_link_is_up(pcie
))
153 /* access only one slot on each root port */
154 if (bus
->number
== pcie
->root_bus_nr
&& dev
> 0)
158 * Do not read more than one device on the bus directly attached
159 * to root port, root port can only attach to one downstream port.
161 if (bus
->primary
== pcie
->root_bus_nr
&& dev
> 0)
167 static int tlp_read_packet(struct altera_pcie
*pcie
, u32
*value
)
175 * Minimum 2 loops to read TLP headers and 1 loop to read data
178 for (i
= 0; i
< TLP_LOOP
; i
++) {
179 ctrl
= cra_readl(pcie
, RP_RXCPL_STATUS
);
180 if ((ctrl
& RP_RXCPL_SOP
) || (ctrl
& RP_RXCPL_EOP
) || sop
) {
181 reg0
= cra_readl(pcie
, RP_RXCPL_REG0
);
182 reg1
= cra_readl(pcie
, RP_RXCPL_REG1
);
184 if (ctrl
& RP_RXCPL_SOP
)
187 if (ctrl
& RP_RXCPL_EOP
) {
190 return PCIBIOS_SUCCESSFUL
;
199 static void tlp_write_packet(struct altera_pcie
*pcie
, u32
*headers
,
200 u32 data
, bool align
)
202 struct tlp_rp_regpair_t tlp_rp_regdata
;
204 tlp_rp_regdata
.reg0
= headers
[0];
205 tlp_rp_regdata
.reg1
= headers
[1];
206 tlp_rp_regdata
.ctrl
= RP_TX_SOP
;
207 tlp_write_tx(pcie
, &tlp_rp_regdata
);
210 tlp_rp_regdata
.reg0
= headers
[2];
211 tlp_rp_regdata
.reg1
= 0;
212 tlp_rp_regdata
.ctrl
= 0;
213 tlp_write_tx(pcie
, &tlp_rp_regdata
);
215 tlp_rp_regdata
.reg0
= data
;
216 tlp_rp_regdata
.reg1
= 0;
218 tlp_rp_regdata
.reg0
= headers
[2];
219 tlp_rp_regdata
.reg1
= data
;
222 tlp_rp_regdata
.ctrl
= RP_TX_EOP
;
223 tlp_write_tx(pcie
, &tlp_rp_regdata
);
226 static int tlp_cfg_dword_read(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
227 int where
, u8 byte_en
, u32
*value
)
229 u32 headers
[TLP_HDR_SIZE
];
231 if (bus
== pcie
->root_bus_nr
)
232 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0
);
234 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1
);
236 headers
[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie
->root_bus_nr
, devfn
),
237 TLP_READ_TAG
, byte_en
);
238 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
240 tlp_write_packet(pcie
, headers
, 0, false);
242 return tlp_read_packet(pcie
, value
);
245 static int tlp_cfg_dword_write(struct altera_pcie
*pcie
, u8 bus
, u32 devfn
,
246 int where
, u8 byte_en
, u32 value
)
248 u32 headers
[TLP_HDR_SIZE
];
251 if (bus
== pcie
->root_bus_nr
)
252 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0
);
254 headers
[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1
);
256 headers
[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie
->root_bus_nr
, devfn
),
257 TLP_WRITE_TAG
, byte_en
);
258 headers
[2] = TLP_CFG_DW2(bus
, devfn
, where
);
260 /* check alignment to Qword */
261 if ((where
& 0x7) == 0)
262 tlp_write_packet(pcie
, headers
, value
, true);
264 tlp_write_packet(pcie
, headers
, value
, false);
266 ret
= tlp_read_packet(pcie
, NULL
);
267 if (ret
!= PCIBIOS_SUCCESSFUL
)
271 * Monitor changes to PCI_PRIMARY_BUS register on root port
272 * and update local copy of root bus number accordingly.
274 if ((bus
== pcie
->root_bus_nr
) && (where
== PCI_PRIMARY_BUS
))
275 pcie
->root_bus_nr
= (u8
)(value
);
277 return PCIBIOS_SUCCESSFUL
;
280 static int altera_pcie_cfg_read(struct pci_bus
*bus
, unsigned int devfn
,
281 int where
, int size
, u32
*value
)
283 struct altera_pcie
*pcie
= bus
->sysdata
;
288 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
289 return PCIBIOS_BAD_REGISTER_NUMBER
;
291 if (!altera_pcie_valid_config(pcie
, bus
, PCI_SLOT(devfn
))) {
293 return PCIBIOS_DEVICE_NOT_FOUND
;
298 byte_en
= 1 << (where
& 3);
301 byte_en
= 3 << (where
& 3);
308 ret
= tlp_cfg_dword_read(pcie
, bus
->number
, devfn
,
309 (where
& ~DWORD_MASK
), byte_en
, &data
);
310 if (ret
!= PCIBIOS_SUCCESSFUL
)
315 *value
= (data
>> (8 * (where
& 0x3))) & 0xff;
318 *value
= (data
>> (8 * (where
& 0x2))) & 0xffff;
325 return PCIBIOS_SUCCESSFUL
;
328 static int altera_pcie_cfg_write(struct pci_bus
*bus
, unsigned int devfn
,
329 int where
, int size
, u32 value
)
331 struct altera_pcie
*pcie
= bus
->sysdata
;
333 u32 shift
= 8 * (where
& 3);
336 if (altera_pcie_hide_rc_bar(bus
, devfn
, where
))
337 return PCIBIOS_BAD_REGISTER_NUMBER
;
339 if (!altera_pcie_valid_config(pcie
, bus
, PCI_SLOT(devfn
)))
340 return PCIBIOS_DEVICE_NOT_FOUND
;
344 data32
= (value
& 0xff) << shift
;
345 byte_en
= 1 << (where
& 3);
348 data32
= (value
& 0xffff) << shift
;
349 byte_en
= 3 << (where
& 3);
357 return tlp_cfg_dword_write(pcie
, bus
->number
, devfn
,
358 (where
& ~DWORD_MASK
), byte_en
, data32
);
361 static struct pci_ops altera_pcie_ops
= {
362 .read
= altera_pcie_cfg_read
,
363 .write
= altera_pcie_cfg_write
,
366 static int altera_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
367 irq_hw_number_t hwirq
)
369 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
370 irq_set_chip_data(irq
, domain
->host_data
);
375 static const struct irq_domain_ops intx_domain_ops
= {
376 .map
= altera_pcie_intx_map
,
379 static void altera_pcie_isr(struct irq_desc
*desc
)
381 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
382 struct altera_pcie
*pcie
;
383 unsigned long status
;
387 chained_irq_enter(chip
, desc
);
388 pcie
= irq_desc_get_handler_data(desc
);
390 while ((status
= cra_readl(pcie
, P2A_INT_STATUS
)
391 & P2A_INT_STS_ALL
) != 0) {
392 for_each_set_bit(bit
, &status
, INTX_NUM
) {
393 /* clear interrupts */
394 cra_writel(pcie
, 1 << bit
, P2A_INT_STATUS
);
396 virq
= irq_find_mapping(pcie
->irq_domain
, bit
+ 1);
398 generic_handle_irq(virq
);
400 dev_err(&pcie
->pdev
->dev
,
401 "unexpected IRQ, INT%d\n", bit
);
405 chained_irq_exit(chip
, desc
);
408 static void altera_pcie_release_of_pci_ranges(struct altera_pcie
*pcie
)
410 pci_free_resource_list(&pcie
->resources
);
413 static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie
*pcie
)
415 int err
, res_valid
= 0;
416 struct device
*dev
= &pcie
->pdev
->dev
;
417 struct device_node
*np
= dev
->of_node
;
418 struct resource_entry
*win
;
420 err
= of_pci_get_host_bridge_resources(np
, 0, 0xff, &pcie
->resources
,
425 resource_list_for_each_entry(win
, &pcie
->resources
) {
426 struct resource
*parent
, *res
= win
->res
;
428 switch (resource_type(res
)) {
430 parent
= &iomem_resource
;
431 res_valid
|= !(res
->flags
& IORESOURCE_PREFETCH
);
437 err
= devm_request_resource(dev
, parent
, res
);
439 goto out_release_res
;
443 dev_err(dev
, "non-prefetchable memory resource required\n");
445 goto out_release_res
;
451 altera_pcie_release_of_pci_ranges(pcie
);
455 static int altera_pcie_init_irq_domain(struct altera_pcie
*pcie
)
457 struct device
*dev
= &pcie
->pdev
->dev
;
458 struct device_node
*node
= dev
->of_node
;
461 pcie
->irq_domain
= irq_domain_add_linear(node
, INTX_NUM
,
462 &intx_domain_ops
, pcie
);
463 if (!pcie
->irq_domain
) {
464 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
471 static int altera_pcie_parse_dt(struct altera_pcie
*pcie
)
473 struct resource
*cra
;
474 struct platform_device
*pdev
= pcie
->pdev
;
476 cra
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "Cra");
478 dev_err(&pdev
->dev
, "no Cra memory resource defined\n");
482 pcie
->cra_base
= devm_ioremap_resource(&pdev
->dev
, cra
);
483 if (IS_ERR(pcie
->cra_base
)) {
484 dev_err(&pdev
->dev
, "failed to map cra memory\n");
485 return PTR_ERR(pcie
->cra_base
);
489 pcie
->irq
= platform_get_irq(pdev
, 0);
490 if (pcie
->irq
<= 0) {
491 dev_err(&pdev
->dev
, "failed to get IRQ: %d\n", pcie
->irq
);
495 irq_set_chained_handler_and_data(pcie
->irq
, altera_pcie_isr
, pcie
);
500 static int altera_pcie_probe(struct platform_device
*pdev
)
502 struct altera_pcie
*pcie
;
504 struct pci_bus
*child
;
507 pcie
= devm_kzalloc(&pdev
->dev
, sizeof(*pcie
), GFP_KERNEL
);
513 ret
= altera_pcie_parse_dt(pcie
);
515 dev_err(&pdev
->dev
, "Parsing DT failed\n");
519 INIT_LIST_HEAD(&pcie
->resources
);
521 ret
= altera_pcie_parse_request_of_pci_ranges(pcie
);
523 dev_err(&pdev
->dev
, "Failed add resources\n");
527 ret
= altera_pcie_init_irq_domain(pcie
);
529 dev_err(&pdev
->dev
, "Failed creating IRQ Domain\n");
533 /* clear all interrupts */
534 cra_writel(pcie
, P2A_INT_STS_ALL
, P2A_INT_STATUS
);
535 /* enable all interrupts */
536 cra_writel(pcie
, P2A_INT_ENA_ALL
, P2A_INT_ENABLE
);
538 bus
= pci_scan_root_bus(&pdev
->dev
, pcie
->root_bus_nr
, &altera_pcie_ops
,
539 pcie
, &pcie
->resources
);
543 pci_fixup_irqs(pci_common_swizzle
, of_irq_parse_and_map_pci
);
544 pci_assign_unassigned_bus_resources(bus
);
546 /* Configure PCI Express setting. */
547 list_for_each_entry(child
, &bus
->children
, node
)
548 pcie_bus_configure_settings(child
);
550 pci_bus_add_devices(bus
);
552 platform_set_drvdata(pdev
, pcie
);
556 static const struct of_device_id altera_pcie_of_match
[] = {
557 { .compatible
= "altr,pcie-root-port-1.0", },
560 MODULE_DEVICE_TABLE(of
, altera_pcie_of_match
);
562 static struct platform_driver altera_pcie_driver
= {
563 .probe
= altera_pcie_probe
,
565 .name
= "altera-pcie",
566 .of_match_table
= altera_pcie_of_match
,
567 .suppress_bind_attrs
= true,
571 static int altera_pcie_init(void)
573 return platform_driver_register(&altera_pcie_driver
);
575 module_init(altera_pcie_init
);
577 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
578 MODULE_DESCRIPTION("Altera PCIe host controller driver");
579 MODULE_LICENSE("GPL v2");