1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
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/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_pci.h>
18 #include <linux/pci.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
24 #define RP_TX_REG0 0x2000
25 #define RP_TX_REG1 0x2004
26 #define RP_TX_CNTRL 0x2008
29 #define RP_RXCPL_STATUS 0x2010
30 #define RP_RXCPL_EOP 0x2
31 #define RP_RXCPL_SOP 0x1
32 #define RP_RXCPL_REG0 0x2014
33 #define RP_RXCPL_REG1 0x2018
34 #define P2A_INT_STATUS 0x3060
35 #define P2A_INT_STS_ALL 0xf
36 #define P2A_INT_ENABLE 0x3070
37 #define P2A_INT_ENA_ALL 0xf
38 #define RP_LTSSM 0x3c64
39 #define RP_LTSSM_MASK 0x1f
42 #define S10_RP_TX_CNTRL 0x2004
43 #define S10_RP_RXCPL_REG 0x2008
44 #define S10_RP_RXCPL_STATUS 0x200C
45 #define S10_RP_CFG_ADDR(pcie, reg) \
46 (((pcie)->hip_base) + (reg) + (1 << 20))
47 #define S10_RP_SECONDARY(pcie) \
48 readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
50 /* TLP configuration type 0 and 1 */
51 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
52 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
53 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
54 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
55 #define TLP_PAYLOAD_SIZE 0x01
56 #define TLP_READ_TAG 0x1d
57 #define TLP_WRITE_TAG 0x10
59 #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
60 #define TLP_CFG_DW0(pcie, cfg) \
63 #define TLP_CFG_DW1(pcie, tag, be) \
64 (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
65 #define TLP_CFG_DW2(bus, devfn, offset) \
66 (((bus) << 24) | ((devfn) << 16) | (offset))
67 #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
68 #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
69 #define TLP_HDR_SIZE 3
72 #define LINK_UP_TIMEOUT HZ
73 #define LINK_RETRAIN_TIMEOUT HZ
77 #define S10_TLP_FMTTYPE_CFGRD0 0x05
78 #define S10_TLP_FMTTYPE_CFGRD1 0x04
79 #define S10_TLP_FMTTYPE_CFGWR0 0x45
80 #define S10_TLP_FMTTYPE_CFGWR1 0x44
82 enum altera_pcie_version {
88 struct platform_device *pdev;
89 void __iomem *cra_base;
90 void __iomem *hip_base;
93 struct irq_domain *irq_domain;
94 struct resource bus_range;
95 struct list_head resources;
96 const struct altera_pcie_data *pcie_data;
99 struct altera_pcie_ops {
100 int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value);
101 void (*tlp_write_pkt)(struct altera_pcie *pcie, u32 *headers,
102 u32 data, bool align);
103 bool (*get_link_status)(struct altera_pcie *pcie);
104 int (*rp_read_cfg)(struct altera_pcie *pcie, int where,
105 int size, u32 *value);
106 int (*rp_write_cfg)(struct altera_pcie *pcie, u8 busno,
107 int where, int size, u32 value);
110 struct altera_pcie_data {
111 const struct altera_pcie_ops *ops;
112 enum altera_pcie_version version;
113 u32 cap_offset; /* PCIe capability structure register offset */
120 struct tlp_rp_regpair_t {
126 static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
129 writel_relaxed(value, pcie->cra_base + reg);
132 static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
134 return readl_relaxed(pcie->cra_base + reg);
137 static bool altera_pcie_link_up(struct altera_pcie *pcie)
139 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
142 static bool s10_altera_pcie_link_up(struct altera_pcie *pcie)
144 void __iomem *addr = S10_RP_CFG_ADDR(pcie,
145 pcie->pcie_data->cap_offset +
148 return !!(readw(addr) & PCI_EXP_LNKSTA_DLLLA);
152 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
153 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
154 * using these registers, so it can be reached by DMA from EP devices.
155 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
156 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
157 * should be hidden during enumeration to avoid the sizing and resource
158 * allocation by PCIe core.
160 static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
163 if (pci_is_root_bus(bus) && (devfn == 0) &&
164 (offset == PCI_BASE_ADDRESS_0))
170 static void tlp_write_tx(struct altera_pcie *pcie,
171 struct tlp_rp_regpair_t *tlp_rp_regdata)
173 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
174 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
175 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
178 static void s10_tlp_write_tx(struct altera_pcie *pcie, u32 reg0, u32 ctrl)
180 cra_writel(pcie, reg0, RP_TX_REG0);
181 cra_writel(pcie, ctrl, S10_RP_TX_CNTRL);
184 static bool altera_pcie_valid_device(struct altera_pcie *pcie,
185 struct pci_bus *bus, int dev)
187 /* If there is no link, then there is no device */
188 if (bus->number != pcie->root_bus_nr) {
189 if (!pcie->pcie_data->ops->get_link_status(pcie))
193 /* access only one slot on each root port */
194 if (bus->number == pcie->root_bus_nr && dev > 0)
200 static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
209 * Minimum 2 loops to read TLP headers and 1 loop to read data
212 for (i = 0; i < TLP_LOOP; i++) {
213 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
214 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
215 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
216 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
218 if (ctrl & RP_RXCPL_SOP) {
220 comp_status = TLP_COMP_STATUS(reg1);
223 if (ctrl & RP_RXCPL_EOP) {
225 return PCIBIOS_DEVICE_NOT_FOUND;
230 return PCIBIOS_SUCCESSFUL;
236 return PCIBIOS_DEVICE_NOT_FOUND;
239 static int s10_tlp_read_packet(struct altera_pcie *pcie, u32 *value)
245 struct device *dev = &pcie->pdev->dev;
247 for (count = 0; count < TLP_LOOP; count++) {
248 ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
249 if (ctrl & RP_RXCPL_SOP) {
251 dw[0] = cra_readl(pcie, S10_RP_RXCPL_REG);
258 /* SOP detection failed, return error */
259 if (count == TLP_LOOP)
260 return PCIBIOS_DEVICE_NOT_FOUND;
265 while (count < ARRAY_SIZE(dw)) {
266 ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
267 dw[count++] = cra_readl(pcie, S10_RP_RXCPL_REG);
268 if (ctrl & RP_RXCPL_EOP) {
269 comp_status = TLP_COMP_STATUS(dw[1]);
271 return PCIBIOS_DEVICE_NOT_FOUND;
273 if (value && TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
277 return PCIBIOS_SUCCESSFUL;
281 dev_warn(dev, "Malformed TLP packet\n");
283 return PCIBIOS_DEVICE_NOT_FOUND;
286 static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
287 u32 data, bool align)
289 struct tlp_rp_regpair_t tlp_rp_regdata;
291 tlp_rp_regdata.reg0 = headers[0];
292 tlp_rp_regdata.reg1 = headers[1];
293 tlp_rp_regdata.ctrl = RP_TX_SOP;
294 tlp_write_tx(pcie, &tlp_rp_regdata);
297 tlp_rp_regdata.reg0 = headers[2];
298 tlp_rp_regdata.reg1 = 0;
299 tlp_rp_regdata.ctrl = 0;
300 tlp_write_tx(pcie, &tlp_rp_regdata);
302 tlp_rp_regdata.reg0 = data;
303 tlp_rp_regdata.reg1 = 0;
305 tlp_rp_regdata.reg0 = headers[2];
306 tlp_rp_regdata.reg1 = data;
309 tlp_rp_regdata.ctrl = RP_TX_EOP;
310 tlp_write_tx(pcie, &tlp_rp_regdata);
313 static void s10_tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
314 u32 data, bool dummy)
316 s10_tlp_write_tx(pcie, headers[0], RP_TX_SOP);
317 s10_tlp_write_tx(pcie, headers[1], 0);
318 s10_tlp_write_tx(pcie, headers[2], 0);
319 s10_tlp_write_tx(pcie, data, RP_TX_EOP);
322 static void get_tlp_header(struct altera_pcie *pcie, u8 bus, u32 devfn,
323 int where, u8 byte_en, bool read, u32 *headers)
326 u8 cfg0 = read ? pcie->pcie_data->cfgrd0 : pcie->pcie_data->cfgwr0;
327 u8 cfg1 = read ? pcie->pcie_data->cfgrd1 : pcie->pcie_data->cfgwr1;
328 u8 tag = read ? TLP_READ_TAG : TLP_WRITE_TAG;
330 if (pcie->pcie_data->version == ALTERA_PCIE_V1)
331 cfg = (bus == pcie->root_bus_nr) ? cfg0 : cfg1;
333 cfg = (bus > S10_RP_SECONDARY(pcie)) ? cfg0 : cfg1;
335 headers[0] = TLP_CFG_DW0(pcie, cfg);
336 headers[1] = TLP_CFG_DW1(pcie, tag, byte_en);
337 headers[2] = TLP_CFG_DW2(bus, devfn, where);
340 static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
341 int where, u8 byte_en, u32 *value)
343 u32 headers[TLP_HDR_SIZE];
345 get_tlp_header(pcie, bus, devfn, where, byte_en, true,
348 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, 0, false);
350 return pcie->pcie_data->ops->tlp_read_pkt(pcie, value);
353 static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
354 int where, u8 byte_en, u32 value)
356 u32 headers[TLP_HDR_SIZE];
359 get_tlp_header(pcie, bus, devfn, where, byte_en, false,
362 /* check alignment to Qword */
363 if ((where & 0x7) == 0)
364 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
367 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
370 ret = pcie->pcie_data->ops->tlp_read_pkt(pcie, NULL);
371 if (ret != PCIBIOS_SUCCESSFUL)
375 * Monitor changes to PCI_PRIMARY_BUS register on root port
376 * and update local copy of root bus number accordingly.
378 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
379 pcie->root_bus_nr = (u8)(value);
381 return PCIBIOS_SUCCESSFUL;
384 static int s10_rp_read_cfg(struct altera_pcie *pcie, int where,
385 int size, u32 *value)
387 void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
391 *value = readb(addr);
394 *value = readw(addr);
397 *value = readl(addr);
401 return PCIBIOS_SUCCESSFUL;
404 static int s10_rp_write_cfg(struct altera_pcie *pcie, u8 busno,
405 int where, int size, u32 value)
407 void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
422 * Monitor changes to PCI_PRIMARY_BUS register on root port
423 * and update local copy of root bus number accordingly.
425 if (busno == pcie->root_bus_nr && where == PCI_PRIMARY_BUS)
426 pcie->root_bus_nr = value & 0xff;
428 return PCIBIOS_SUCCESSFUL;
431 static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
432 unsigned int devfn, int where, int size,
439 if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_read_cfg)
440 return pcie->pcie_data->ops->rp_read_cfg(pcie, where,
445 byte_en = 1 << (where & 3);
448 byte_en = 3 << (where & 3);
455 ret = tlp_cfg_dword_read(pcie, busno, devfn,
456 (where & ~DWORD_MASK), byte_en, &data);
457 if (ret != PCIBIOS_SUCCESSFUL)
462 *value = (data >> (8 * (where & 0x3))) & 0xff;
465 *value = (data >> (8 * (where & 0x2))) & 0xffff;
472 return PCIBIOS_SUCCESSFUL;
475 static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
476 unsigned int devfn, int where, int size,
480 u32 shift = 8 * (where & 3);
483 if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_write_cfg)
484 return pcie->pcie_data->ops->rp_write_cfg(pcie, busno,
489 data32 = (value & 0xff) << shift;
490 byte_en = 1 << (where & 3);
493 data32 = (value & 0xffff) << shift;
494 byte_en = 3 << (where & 3);
502 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
506 static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
507 int where, int size, u32 *value)
509 struct altera_pcie *pcie = bus->sysdata;
511 if (altera_pcie_hide_rc_bar(bus, devfn, where))
512 return PCIBIOS_BAD_REGISTER_NUMBER;
514 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) {
516 return PCIBIOS_DEVICE_NOT_FOUND;
519 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
523 static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
524 int where, int size, u32 value)
526 struct altera_pcie *pcie = bus->sysdata;
528 if (altera_pcie_hide_rc_bar(bus, devfn, where))
529 return PCIBIOS_BAD_REGISTER_NUMBER;
531 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
532 return PCIBIOS_DEVICE_NOT_FOUND;
534 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
538 static struct pci_ops altera_pcie_ops = {
539 .read = altera_pcie_cfg_read,
540 .write = altera_pcie_cfg_write,
543 static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
544 unsigned int devfn, int offset, u16 *value)
549 ret = _altera_pcie_cfg_read(pcie, busno, devfn,
550 pcie->pcie_data->cap_offset + offset,
557 static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
558 unsigned int devfn, int offset, u16 value)
560 return _altera_pcie_cfg_write(pcie, busno, devfn,
561 pcie->pcie_data->cap_offset + offset,
566 static void altera_wait_link_retrain(struct altera_pcie *pcie)
568 struct device *dev = &pcie->pdev->dev;
570 unsigned long start_jiffies;
572 /* Wait for link training end. */
573 start_jiffies = jiffies;
575 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
576 PCI_EXP_LNKSTA, ®16);
577 if (!(reg16 & PCI_EXP_LNKSTA_LT))
580 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
581 dev_err(dev, "link retrain timeout\n");
587 /* Wait for link is up */
588 start_jiffies = jiffies;
590 if (pcie->pcie_data->ops->get_link_status(pcie))
593 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
594 dev_err(dev, "link up timeout\n");
601 static void altera_pcie_retrain(struct altera_pcie *pcie)
603 u16 linkcap, linkstat, linkctl;
605 if (!pcie->pcie_data->ops->get_link_status(pcie))
609 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
610 * current speed is 2.5 GB/s.
612 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
614 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
617 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
619 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
620 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
621 PCI_EXP_LNKCTL, &linkctl);
622 linkctl |= PCI_EXP_LNKCTL_RL;
623 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
624 PCI_EXP_LNKCTL, linkctl);
626 altera_wait_link_retrain(pcie);
630 static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
631 irq_hw_number_t hwirq)
633 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
634 irq_set_chip_data(irq, domain->host_data);
638 static const struct irq_domain_ops intx_domain_ops = {
639 .map = altera_pcie_intx_map,
640 .xlate = pci_irqd_intx_xlate,
643 static void altera_pcie_isr(struct irq_desc *desc)
645 struct irq_chip *chip = irq_desc_get_chip(desc);
646 struct altera_pcie *pcie;
648 unsigned long status;
652 chained_irq_enter(chip, desc);
653 pcie = irq_desc_get_handler_data(desc);
654 dev = &pcie->pdev->dev;
656 while ((status = cra_readl(pcie, P2A_INT_STATUS)
657 & P2A_INT_STS_ALL) != 0) {
658 for_each_set_bit(bit, &status, PCI_NUM_INTX) {
659 /* clear interrupts */
660 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
662 virq = irq_find_mapping(pcie->irq_domain, bit);
664 generic_handle_irq(virq);
666 dev_err(dev, "unexpected IRQ, INT%d\n", bit);
670 chained_irq_exit(chip, desc);
673 static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
675 int err, res_valid = 0;
676 struct device *dev = &pcie->pdev->dev;
677 struct resource_entry *win;
679 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
680 &pcie->resources, NULL);
684 err = devm_request_pci_bus_resources(dev, &pcie->resources);
686 goto out_release_res;
688 resource_list_for_each_entry(win, &pcie->resources) {
689 struct resource *res = win->res;
691 if (resource_type(res) == IORESOURCE_MEM)
692 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
698 dev_err(dev, "non-prefetchable memory resource required\n");
702 pci_free_resource_list(&pcie->resources);
706 static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
708 struct device *dev = &pcie->pdev->dev;
709 struct device_node *node = dev->of_node;
712 pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX,
713 &intx_domain_ops, pcie);
714 if (!pcie->irq_domain) {
715 dev_err(dev, "Failed to get a INTx IRQ domain\n");
722 static void altera_pcie_irq_teardown(struct altera_pcie *pcie)
724 irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
725 irq_domain_remove(pcie->irq_domain);
726 irq_dispose_mapping(pcie->irq);
729 static int altera_pcie_parse_dt(struct altera_pcie *pcie)
731 struct device *dev = &pcie->pdev->dev;
732 struct platform_device *pdev = pcie->pdev;
733 struct resource *cra;
734 struct resource *hip;
736 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
737 pcie->cra_base = devm_ioremap_resource(dev, cra);
738 if (IS_ERR(pcie->cra_base))
739 return PTR_ERR(pcie->cra_base);
741 if (pcie->pcie_data->version == ALTERA_PCIE_V2) {
742 hip = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Hip");
743 pcie->hip_base = devm_ioremap_resource(&pdev->dev, hip);
744 if (IS_ERR(pcie->hip_base))
745 return PTR_ERR(pcie->hip_base);
749 pcie->irq = platform_get_irq(pdev, 0);
751 dev_err(dev, "failed to get IRQ: %d\n", pcie->irq);
755 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
759 static void altera_pcie_host_init(struct altera_pcie *pcie)
761 altera_pcie_retrain(pcie);
764 static const struct altera_pcie_ops altera_pcie_ops_1_0 = {
765 .tlp_read_pkt = tlp_read_packet,
766 .tlp_write_pkt = tlp_write_packet,
767 .get_link_status = altera_pcie_link_up,
770 static const struct altera_pcie_ops altera_pcie_ops_2_0 = {
771 .tlp_read_pkt = s10_tlp_read_packet,
772 .tlp_write_pkt = s10_tlp_write_packet,
773 .get_link_status = s10_altera_pcie_link_up,
774 .rp_read_cfg = s10_rp_read_cfg,
775 .rp_write_cfg = s10_rp_write_cfg,
778 static const struct altera_pcie_data altera_pcie_1_0_data = {
779 .ops = &altera_pcie_ops_1_0,
781 .version = ALTERA_PCIE_V1,
782 .cfgrd0 = TLP_FMTTYPE_CFGRD0,
783 .cfgrd1 = TLP_FMTTYPE_CFGRD1,
784 .cfgwr0 = TLP_FMTTYPE_CFGWR0,
785 .cfgwr1 = TLP_FMTTYPE_CFGWR1,
788 static const struct altera_pcie_data altera_pcie_2_0_data = {
789 .ops = &altera_pcie_ops_2_0,
790 .version = ALTERA_PCIE_V2,
792 .cfgrd0 = S10_TLP_FMTTYPE_CFGRD0,
793 .cfgrd1 = S10_TLP_FMTTYPE_CFGRD1,
794 .cfgwr0 = S10_TLP_FMTTYPE_CFGWR0,
795 .cfgwr1 = S10_TLP_FMTTYPE_CFGWR1,
798 static const struct of_device_id altera_pcie_of_match[] = {
799 {.compatible = "altr,pcie-root-port-1.0",
800 .data = &altera_pcie_1_0_data },
801 {.compatible = "altr,pcie-root-port-2.0",
802 .data = &altera_pcie_2_0_data },
806 static int altera_pcie_probe(struct platform_device *pdev)
808 struct device *dev = &pdev->dev;
809 struct altera_pcie *pcie;
811 struct pci_bus *child;
812 struct pci_host_bridge *bridge;
814 const struct of_device_id *match;
816 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
820 pcie = pci_host_bridge_priv(bridge);
822 platform_set_drvdata(pdev, pcie);
824 match = of_match_device(altera_pcie_of_match, &pdev->dev);
828 pcie->pcie_data = match->data;
830 ret = altera_pcie_parse_dt(pcie);
832 dev_err(dev, "Parsing DT failed\n");
836 INIT_LIST_HEAD(&pcie->resources);
838 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
840 dev_err(dev, "Failed add resources\n");
844 ret = altera_pcie_init_irq_domain(pcie);
846 dev_err(dev, "Failed creating IRQ Domain\n");
850 /* clear all interrupts */
851 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
852 /* enable all interrupts */
853 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
854 altera_pcie_host_init(pcie);
856 list_splice_init(&pcie->resources, &bridge->windows);
857 bridge->dev.parent = dev;
858 bridge->sysdata = pcie;
859 bridge->busnr = pcie->root_bus_nr;
860 bridge->ops = &altera_pcie_ops;
861 bridge->map_irq = of_irq_parse_and_map_pci;
862 bridge->swizzle_irq = pci_common_swizzle;
864 ret = pci_scan_root_bus_bridge(bridge);
870 pci_assign_unassigned_bus_resources(bus);
872 /* Configure PCI Express setting. */
873 list_for_each_entry(child, &bus->children, node)
874 pcie_bus_configure_settings(child);
876 pci_bus_add_devices(bus);
880 static int altera_pcie_remove(struct platform_device *pdev)
882 struct altera_pcie *pcie = platform_get_drvdata(pdev);
883 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
885 pci_stop_root_bus(bridge->bus);
886 pci_remove_root_bus(bridge->bus);
887 pci_free_resource_list(&pcie->resources);
888 altera_pcie_irq_teardown(pcie);
893 static struct platform_driver altera_pcie_driver = {
894 .probe = altera_pcie_probe,
895 .remove = altera_pcie_remove,
897 .name = "altera-pcie",
898 .of_match_table = altera_pcie_of_match,
902 MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
903 module_platform_driver(altera_pcie_driver);
904 MODULE_LICENSE("GPL v2");