Lines Matching full:pcie
6 * Description: Altera PCIe host controller driver
44 #define S10_RP_CFG_ADDR(pcie, reg) \ argument
45 (((pcie)->hip_base) + (reg) + (1 << 20))
46 #define S10_RP_SECONDARY(pcie) \ argument
47 readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
58 #define TLP_CFG_DW0(pcie, cfg) \ argument
61 #define TLP_CFG_DW1(pcie, tag, be) \ argument
62 (((PCI_DEVID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
97 int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value);
98 void (*tlp_write_pkt)(struct altera_pcie *pcie, u32 *headers,
100 bool (*get_link_status)(struct altera_pcie *pcie);
101 int (*rp_read_cfg)(struct altera_pcie *pcie, int where,
103 int (*rp_write_cfg)(struct altera_pcie *pcie, u8 busno,
110 u32 cap_offset; /* PCIe capability structure register offset */
123 static inline void cra_writel(struct altera_pcie *pcie, const u32 value, in cra_writel() argument
126 writel_relaxed(value, pcie->cra_base + reg); in cra_writel()
129 static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg) in cra_readl() argument
131 return readl_relaxed(pcie->cra_base + reg); in cra_readl()
134 static bool altera_pcie_link_up(struct altera_pcie *pcie) in altera_pcie_link_up() argument
136 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0); in altera_pcie_link_up()
139 static bool s10_altera_pcie_link_up(struct altera_pcie *pcie) in s10_altera_pcie_link_up() argument
141 void __iomem *addr = S10_RP_CFG_ADDR(pcie, in s10_altera_pcie_link_up()
142 pcie->pcie_data->cap_offset + in s10_altera_pcie_link_up()
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
155 * allocation by PCIe core.
167 static void tlp_write_tx(struct altera_pcie *pcie, in tlp_write_tx() argument
170 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0); in tlp_write_tx()
171 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1); in tlp_write_tx()
172 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL); in tlp_write_tx()
175 static void s10_tlp_write_tx(struct altera_pcie *pcie, u32 reg0, u32 ctrl) in s10_tlp_write_tx() argument
177 cra_writel(pcie, reg0, RP_TX_REG0); in s10_tlp_write_tx()
178 cra_writel(pcie, ctrl, S10_RP_TX_CNTRL); in s10_tlp_write_tx()
181 static bool altera_pcie_valid_device(struct altera_pcie *pcie, in altera_pcie_valid_device() argument
185 if (bus->number != pcie->root_bus_nr) { in altera_pcie_valid_device()
186 if (!pcie->pcie_data->ops->get_link_status(pcie)) in altera_pcie_valid_device()
191 if (bus->number == pcie->root_bus_nr && dev > 0) in altera_pcie_valid_device()
197 static int tlp_read_packet(struct altera_pcie *pcie, u32 *value) in tlp_read_packet() argument
210 ctrl = cra_readl(pcie, RP_RXCPL_STATUS); in tlp_read_packet()
212 reg0 = cra_readl(pcie, RP_RXCPL_REG0); in tlp_read_packet()
213 reg1 = cra_readl(pcie, RP_RXCPL_REG1); in tlp_read_packet()
236 static int s10_tlp_read_packet(struct altera_pcie *pcie, u32 *value) in s10_tlp_read_packet() argument
242 struct device *dev = &pcie->pdev->dev; in s10_tlp_read_packet()
245 ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS); in s10_tlp_read_packet()
248 dw[0] = cra_readl(pcie, S10_RP_RXCPL_REG); in s10_tlp_read_packet()
263 ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS); in s10_tlp_read_packet()
264 dw[count++] = cra_readl(pcie, S10_RP_RXCPL_REG); in s10_tlp_read_packet()
283 static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers, in tlp_write_packet() argument
291 tlp_write_tx(pcie, &tlp_rp_regdata); in tlp_write_packet()
297 tlp_write_tx(pcie, &tlp_rp_regdata); in tlp_write_packet()
307 tlp_write_tx(pcie, &tlp_rp_regdata); in tlp_write_packet()
310 static void s10_tlp_write_packet(struct altera_pcie *pcie, u32 *headers, in s10_tlp_write_packet() argument
313 s10_tlp_write_tx(pcie, headers[0], RP_TX_SOP); in s10_tlp_write_packet()
314 s10_tlp_write_tx(pcie, headers[1], 0); in s10_tlp_write_packet()
315 s10_tlp_write_tx(pcie, headers[2], 0); in s10_tlp_write_packet()
316 s10_tlp_write_tx(pcie, data, RP_TX_EOP); in s10_tlp_write_packet()
319 static void get_tlp_header(struct altera_pcie *pcie, u8 bus, u32 devfn, in get_tlp_header() argument
323 u8 cfg0 = read ? pcie->pcie_data->cfgrd0 : pcie->pcie_data->cfgwr0; in get_tlp_header()
324 u8 cfg1 = read ? pcie->pcie_data->cfgrd1 : pcie->pcie_data->cfgwr1; in get_tlp_header()
327 if (pcie->pcie_data->version == ALTERA_PCIE_V1) in get_tlp_header()
328 cfg = (bus == pcie->root_bus_nr) ? cfg0 : cfg1; in get_tlp_header()
330 cfg = (bus > S10_RP_SECONDARY(pcie)) ? cfg0 : cfg1; in get_tlp_header()
332 headers[0] = TLP_CFG_DW0(pcie, cfg); in get_tlp_header()
333 headers[1] = TLP_CFG_DW1(pcie, tag, byte_en); in get_tlp_header()
337 static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn, in tlp_cfg_dword_read() argument
342 get_tlp_header(pcie, bus, devfn, where, byte_en, true, in tlp_cfg_dword_read()
345 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, 0, false); in tlp_cfg_dword_read()
347 return pcie->pcie_data->ops->tlp_read_pkt(pcie, value); in tlp_cfg_dword_read()
350 static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn, in tlp_cfg_dword_write() argument
356 get_tlp_header(pcie, bus, devfn, where, byte_en, false, in tlp_cfg_dword_write()
361 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, in tlp_cfg_dword_write()
364 pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, in tlp_cfg_dword_write()
367 ret = pcie->pcie_data->ops->tlp_read_pkt(pcie, NULL); in tlp_cfg_dword_write()
375 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS)) in tlp_cfg_dword_write()
376 pcie->root_bus_nr = (u8)(value); in tlp_cfg_dword_write()
381 static int s10_rp_read_cfg(struct altera_pcie *pcie, int where, in s10_rp_read_cfg() argument
384 void __iomem *addr = S10_RP_CFG_ADDR(pcie, where); in s10_rp_read_cfg()
401 static int s10_rp_write_cfg(struct altera_pcie *pcie, u8 busno, in s10_rp_write_cfg() argument
404 void __iomem *addr = S10_RP_CFG_ADDR(pcie, where); in s10_rp_write_cfg()
422 if (busno == pcie->root_bus_nr && where == PCI_PRIMARY_BUS) in s10_rp_write_cfg()
423 pcie->root_bus_nr = value & 0xff; in s10_rp_write_cfg()
428 static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno, in _altera_pcie_cfg_read() argument
436 if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_read_cfg) in _altera_pcie_cfg_read()
437 return pcie->pcie_data->ops->rp_read_cfg(pcie, where, in _altera_pcie_cfg_read()
452 ret = tlp_cfg_dword_read(pcie, busno, devfn, in _altera_pcie_cfg_read()
472 static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno, in _altera_pcie_cfg_write() argument
480 if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_write_cfg) in _altera_pcie_cfg_write()
481 return pcie->pcie_data->ops->rp_write_cfg(pcie, busno, in _altera_pcie_cfg_write()
499 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK), in _altera_pcie_cfg_write()
506 struct altera_pcie *pcie = bus->sysdata; in altera_pcie_cfg_read() local
511 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) in altera_pcie_cfg_read()
514 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size, in altera_pcie_cfg_read()
521 struct altera_pcie *pcie = bus->sysdata; in altera_pcie_cfg_write() local
526 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) in altera_pcie_cfg_write()
529 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size, in altera_pcie_cfg_write()
538 static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno, in altera_read_cap_word() argument
544 ret = _altera_pcie_cfg_read(pcie, busno, devfn, in altera_read_cap_word()
545 pcie->pcie_data->cap_offset + offset, in altera_read_cap_word()
552 static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno, in altera_write_cap_word() argument
555 return _altera_pcie_cfg_write(pcie, busno, devfn, in altera_write_cap_word()
556 pcie->pcie_data->cap_offset + offset, in altera_write_cap_word()
561 static void altera_wait_link_retrain(struct altera_pcie *pcie) in altera_wait_link_retrain() argument
563 struct device *dev = &pcie->pdev->dev; in altera_wait_link_retrain()
570 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, in altera_wait_link_retrain()
585 if (pcie->pcie_data->ops->get_link_status(pcie)) in altera_wait_link_retrain()
596 static void altera_pcie_retrain(struct altera_pcie *pcie) in altera_pcie_retrain() argument
600 if (!pcie->pcie_data->ops->get_link_status(pcie)) in altera_pcie_retrain()
604 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but in altera_pcie_retrain()
607 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP, in altera_pcie_retrain()
612 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA, in altera_pcie_retrain()
615 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, in altera_pcie_retrain()
618 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, in altera_pcie_retrain()
621 altera_wait_link_retrain(pcie); in altera_pcie_retrain()
641 struct altera_pcie *pcie; in altera_pcie_isr() local
648 pcie = irq_desc_get_handler_data(desc); in altera_pcie_isr()
649 dev = &pcie->pdev->dev; in altera_pcie_isr()
651 while ((status = cra_readl(pcie, P2A_INT_STATUS) in altera_pcie_isr()
655 cra_writel(pcie, 1 << bit, P2A_INT_STATUS); in altera_pcie_isr()
657 ret = generic_handle_domain_irq(pcie->irq_domain, bit); in altera_pcie_isr()
666 static int altera_pcie_init_irq_domain(struct altera_pcie *pcie) in altera_pcie_init_irq_domain() argument
668 struct device *dev = &pcie->pdev->dev; in altera_pcie_init_irq_domain()
672 pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX, in altera_pcie_init_irq_domain()
673 &intx_domain_ops, pcie); in altera_pcie_init_irq_domain()
674 if (!pcie->irq_domain) { in altera_pcie_init_irq_domain()
682 static void altera_pcie_irq_teardown(struct altera_pcie *pcie) in altera_pcie_irq_teardown() argument
684 irq_set_chained_handler_and_data(pcie->irq, NULL, NULL); in altera_pcie_irq_teardown()
685 irq_domain_remove(pcie->irq_domain); in altera_pcie_irq_teardown()
686 irq_dispose_mapping(pcie->irq); in altera_pcie_irq_teardown()
689 static int altera_pcie_parse_dt(struct altera_pcie *pcie) in altera_pcie_parse_dt() argument
691 struct platform_device *pdev = pcie->pdev; in altera_pcie_parse_dt()
693 pcie->cra_base = devm_platform_ioremap_resource_byname(pdev, "Cra"); in altera_pcie_parse_dt()
694 if (IS_ERR(pcie->cra_base)) in altera_pcie_parse_dt()
695 return PTR_ERR(pcie->cra_base); in altera_pcie_parse_dt()
697 if (pcie->pcie_data->version == ALTERA_PCIE_V2) { in altera_pcie_parse_dt()
698 pcie->hip_base = in altera_pcie_parse_dt()
700 if (IS_ERR(pcie->hip_base)) in altera_pcie_parse_dt()
701 return PTR_ERR(pcie->hip_base); in altera_pcie_parse_dt()
705 pcie->irq = platform_get_irq(pdev, 0); in altera_pcie_parse_dt()
706 if (pcie->irq < 0) in altera_pcie_parse_dt()
707 return pcie->irq; in altera_pcie_parse_dt()
709 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie); in altera_pcie_parse_dt()
713 static void altera_pcie_host_init(struct altera_pcie *pcie) in altera_pcie_host_init() argument
715 altera_pcie_retrain(pcie); in altera_pcie_host_init()
753 {.compatible = "altr,pcie-root-port-1.0",
755 {.compatible = "altr,pcie-root-port-2.0",
763 struct altera_pcie *pcie; in altera_pcie_probe() local
768 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); in altera_pcie_probe()
772 pcie = pci_host_bridge_priv(bridge); in altera_pcie_probe()
773 pcie->pdev = pdev; in altera_pcie_probe()
774 platform_set_drvdata(pdev, pcie); in altera_pcie_probe()
780 pcie->pcie_data = data; in altera_pcie_probe()
782 ret = altera_pcie_parse_dt(pcie); in altera_pcie_probe()
788 ret = altera_pcie_init_irq_domain(pcie); in altera_pcie_probe()
795 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS); in altera_pcie_probe()
797 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE); in altera_pcie_probe()
798 altera_pcie_host_init(pcie); in altera_pcie_probe()
800 bridge->sysdata = pcie; in altera_pcie_probe()
801 bridge->busnr = pcie->root_bus_nr; in altera_pcie_probe()
809 struct altera_pcie *pcie = platform_get_drvdata(pdev); in altera_pcie_remove() local
810 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); in altera_pcie_remove()
814 altera_pcie_irq_teardown(pcie); in altera_pcie_remove()
821 .name = "altera-pcie",
828 MODULE_DESCRIPTION("Altera PCIe host controller driver");