Lines Matching +full:cdns +full:- +full:pcie +full:- +full:host

1 // SPDX-License-Identifier: GPL-2.0
3 // Cadence PCIe endpoint controller driver.
4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
9 #include <linux/pci-epc.h>
13 #include "pcie-cadence.h"
23 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_write_header() local
25 cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid); in cdns_pcie_ep_write_header()
26 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid); in cdns_pcie_ep_write_header()
27 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code); in cdns_pcie_ep_write_header()
28 cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE, in cdns_pcie_ep_write_header()
29 hdr->subclass_code | hdr->baseclass_code << 8); in cdns_pcie_ep_write_header()
30 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE, in cdns_pcie_ep_write_header()
31 hdr->cache_line_size); in cdns_pcie_ep_write_header()
32 cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id); in cdns_pcie_ep_write_header()
33 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin); in cdns_pcie_ep_write_header()
41 u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) | in cdns_pcie_ep_write_header()
42 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id); in cdns_pcie_ep_write_header()
44 cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id); in cdns_pcie_ep_write_header()
54 struct cdns_pcie_epf *epf = &ep->epf[fn]; in cdns_pcie_ep_set_bar()
55 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_set_bar() local
56 dma_addr_t bar_phys = epf_bar->phys_addr; in cdns_pcie_ep_set_bar()
57 enum pci_barno bar = epf_bar->barno; in cdns_pcie_ep_set_bar()
58 int flags = epf_bar->flags; in cdns_pcie_ep_set_bar()
63 sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE); in cdns_pcie_ep_set_bar()
68 sz = 1ULL << fls64(sz - 1); in cdns_pcie_ep_set_bar()
69 aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */ in cdns_pcie_ep_set_bar()
78 return -EINVAL; in cdns_pcie_ep_set_bar()
81 epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64; in cdns_pcie_ep_set_bar()
95 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), in cdns_pcie_ep_set_bar()
97 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), in cdns_pcie_ep_set_bar()
105 b = bar - BAR_4; in cdns_pcie_ep_set_bar()
108 cfg = cdns_pcie_readl(pcie, reg); in cdns_pcie_ep_set_bar()
113 cdns_pcie_writel(pcie, reg, cfg); in cdns_pcie_ep_set_bar()
115 epf->epf_bar[bar] = epf_bar; in cdns_pcie_ep_set_bar()
124 struct cdns_pcie_epf *epf = &ep->epf[fn]; in cdns_pcie_ep_clear_bar()
125 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_clear_bar() local
126 enum pci_barno bar = epf_bar->barno; in cdns_pcie_ep_clear_bar()
134 b = bar - BAR_4; in cdns_pcie_ep_clear_bar()
138 cfg = cdns_pcie_readl(pcie, reg); in cdns_pcie_ep_clear_bar()
142 cdns_pcie_writel(pcie, reg, cfg); in cdns_pcie_ep_clear_bar()
144 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0); in cdns_pcie_ep_clear_bar()
145 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0); in cdns_pcie_ep_clear_bar()
147 epf->epf_bar[bar] = NULL; in cdns_pcie_ep_clear_bar()
154 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_map_addr() local
157 r = find_first_zero_bit(&ep->ob_region_map, in cdns_pcie_ep_map_addr()
158 sizeof(ep->ob_region_map) * BITS_PER_LONG); in cdns_pcie_ep_map_addr()
159 if (r >= ep->max_regions - 1) { in cdns_pcie_ep_map_addr()
160 dev_err(&epc->dev, "no free outbound region\n"); in cdns_pcie_ep_map_addr()
161 return -EINVAL; in cdns_pcie_ep_map_addr()
164 cdns_pcie_set_outbound_region(pcie, 0, fn, r, false, addr, pci_addr, size); in cdns_pcie_ep_map_addr()
166 set_bit(r, &ep->ob_region_map); in cdns_pcie_ep_map_addr()
167 ep->ob_addr[r] = addr; in cdns_pcie_ep_map_addr()
176 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_unmap_addr() local
179 for (r = 0; r < ep->max_regions - 1; r++) in cdns_pcie_ep_unmap_addr()
180 if (ep->ob_addr[r] == addr) in cdns_pcie_ep_unmap_addr()
183 if (r == ep->max_regions - 1) in cdns_pcie_ep_unmap_addr()
186 cdns_pcie_reset_outbound_region(pcie, r); in cdns_pcie_ep_unmap_addr()
188 ep->ob_addr[r] = 0; in cdns_pcie_ep_unmap_addr()
189 clear_bit(r, &ep->ob_region_map); in cdns_pcie_ep_unmap_addr()
195 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_set_msi() local
203 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS); in cdns_pcie_ep_set_msi()
207 cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags); in cdns_pcie_ep_set_msi()
215 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_get_msi() local
220 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS); in cdns_pcie_ep_get_msi()
222 return -EINVAL; in cdns_pcie_ep_get_msi()
236 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_get_msix() local
241 val = cdns_pcie_ep_fn_readw(pcie, func_no, reg); in cdns_pcie_ep_get_msix()
243 return -EINVAL; in cdns_pcie_ep_get_msix()
254 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_set_msix() local
259 val = cdns_pcie_ep_fn_readw(pcie, fn, reg); in cdns_pcie_ep_set_msix()
262 cdns_pcie_ep_fn_writew(pcie, fn, reg, val); in cdns_pcie_ep_set_msix()
267 cdns_pcie_ep_fn_writel(pcie, fn, reg, val); in cdns_pcie_ep_set_msix()
272 cdns_pcie_ep_fn_writel(pcie, fn, reg, val); in cdns_pcie_ep_set_msix()
280 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_assert_intx() local
289 if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY || in cdns_pcie_ep_assert_intx()
290 ep->irq_pci_fn != fn)) { in cdns_pcie_ep_assert_intx()
292 cdns_pcie_set_outbound_region_for_normal_msg(pcie, 0, fn, 0, in cdns_pcie_ep_assert_intx()
293 ep->irq_phys_addr); in cdns_pcie_ep_assert_intx()
294 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY; in cdns_pcie_ep_assert_intx()
295 ep->irq_pci_fn = fn; in cdns_pcie_ep_assert_intx()
299 ep->irq_pending |= BIT(intx); in cdns_pcie_ep_assert_intx()
302 ep->irq_pending &= ~BIT(intx); in cdns_pcie_ep_assert_intx()
306 spin_lock_irqsave(&ep->lock, flags); in cdns_pcie_ep_assert_intx()
307 status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS); in cdns_pcie_ep_assert_intx()
308 if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) { in cdns_pcie_ep_assert_intx()
310 cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status); in cdns_pcie_ep_assert_intx()
312 spin_unlock_irqrestore(&ep->lock, flags); in cdns_pcie_ep_assert_intx()
317 writel(0, ep->irq_cpu_addr + offset); in cdns_pcie_ep_assert_intx()
324 cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND); in cdns_pcie_ep_send_legacy_irq()
326 return -EINVAL; in cdns_pcie_ep_send_legacy_irq()
340 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_send_msi_irq() local
346 /* Check whether the MSI feature has been enabled by the PCI host. */ in cdns_pcie_ep_send_msi_irq()
347 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS); in cdns_pcie_ep_send_msi_irq()
349 return -EINVAL; in cdns_pcie_ep_send_msi_irq()
355 return -EINVAL; in cdns_pcie_ep_send_msi_irq()
358 data_mask = msi_count - 1; in cdns_pcie_ep_send_msi_irq()
359 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64); in cdns_pcie_ep_send_msi_irq()
360 data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask); in cdns_pcie_ep_send_msi_irq()
363 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI); in cdns_pcie_ep_send_msi_irq()
365 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO); in cdns_pcie_ep_send_msi_irq()
369 if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) || in cdns_pcie_ep_send_msi_irq()
370 ep->irq_pci_fn != fn)) { in cdns_pcie_ep_send_msi_irq()
372 cdns_pcie_set_outbound_region(pcie, 0, fn, 0, in cdns_pcie_ep_send_msi_irq()
374 ep->irq_phys_addr, in cdns_pcie_ep_send_msi_irq()
377 ep->irq_pci_addr = (pci_addr & ~pci_addr_mask); in cdns_pcie_ep_send_msi_irq()
378 ep->irq_pci_fn = fn; in cdns_pcie_ep_send_msi_irq()
380 writel(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask)); in cdns_pcie_ep_send_msi_irq()
390 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_send_msix_irq() local
398 /* Check whether the MSI-X feature has been enabled by the PCI host. */ in cdns_pcie_ep_send_msix_irq()
399 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSIX_FLAGS); in cdns_pcie_ep_send_msix_irq()
401 return -EINVAL; in cdns_pcie_ep_send_msix_irq()
404 tbl_offset = cdns_pcie_ep_fn_readl(pcie, fn, reg); in cdns_pcie_ep_send_msix_irq()
408 epf = &ep->epf[fn]; in cdns_pcie_ep_send_msix_irq()
409 msix_tbl = epf->epf_bar[bir]->addr + tbl_offset; in cdns_pcie_ep_send_msix_irq()
410 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr; in cdns_pcie_ep_send_msix_irq()
411 msg_data = msix_tbl[(interrupt_num - 1)].msg_data; in cdns_pcie_ep_send_msix_irq()
414 if (ep->irq_pci_addr != (msg_addr & ~pci_addr_mask) || in cdns_pcie_ep_send_msix_irq()
415 ep->irq_pci_fn != fn) { in cdns_pcie_ep_send_msix_irq()
417 cdns_pcie_set_outbound_region(pcie, 0, fn, 0, in cdns_pcie_ep_send_msix_irq()
419 ep->irq_phys_addr, in cdns_pcie_ep_send_msix_irq()
422 ep->irq_pci_addr = (msg_addr & ~pci_addr_mask); in cdns_pcie_ep_send_msix_irq()
423 ep->irq_pci_fn = fn; in cdns_pcie_ep_send_msix_irq()
425 writel(msg_data, ep->irq_cpu_addr + (msg_addr & pci_addr_mask)); in cdns_pcie_ep_send_msix_irq()
450 return -EINVAL; in cdns_pcie_ep_raise_irq()
456 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_start() local
457 struct device *dev = pcie->dev; in cdns_pcie_ep_start()
467 list_for_each_entry(epf, &epc->pci_epf, list) in cdns_pcie_ep_start()
468 cfg |= BIT(epf->func_no); in cdns_pcie_ep_start()
469 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg); in cdns_pcie_ep_start()
471 ret = cdns_pcie_start_link(pcie); in cdns_pcie_ep_start()
510 struct device *dev = ep->pcie.dev; in cdns_pcie_ep_setup()
512 struct device_node *np = dev->of_node; in cdns_pcie_ep_setup()
513 struct cdns_pcie *pcie = &ep->pcie; in cdns_pcie_ep_setup() local
518 pcie->is_rc = false; in cdns_pcie_ep_setup()
520 pcie->reg_base = devm_platform_ioremap_resource_byname(pdev, "reg"); in cdns_pcie_ep_setup()
521 if (IS_ERR(pcie->reg_base)) { in cdns_pcie_ep_setup()
523 return PTR_ERR(pcie->reg_base); in cdns_pcie_ep_setup()
529 return -EINVAL; in cdns_pcie_ep_setup()
531 pcie->mem_res = res; in cdns_pcie_ep_setup()
533 ret = of_property_read_u32(np, "cdns,max-outbound-regions", in cdns_pcie_ep_setup()
534 &ep->max_regions); in cdns_pcie_ep_setup()
536 dev_err(dev, "missing \"cdns,max-outbound-regions\"\n"); in cdns_pcie_ep_setup()
539 ep->ob_addr = devm_kcalloc(dev, in cdns_pcie_ep_setup()
540 ep->max_regions, sizeof(*ep->ob_addr), in cdns_pcie_ep_setup()
542 if (!ep->ob_addr) in cdns_pcie_ep_setup()
543 return -ENOMEM; in cdns_pcie_ep_setup()
546 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0)); in cdns_pcie_ep_setup()
556 if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0) in cdns_pcie_ep_setup()
557 epc->max_functions = 1; in cdns_pcie_ep_setup()
559 ep->epf = devm_kcalloc(dev, epc->max_functions, sizeof(*ep->epf), in cdns_pcie_ep_setup()
561 if (!ep->epf) in cdns_pcie_ep_setup()
562 return -ENOMEM; in cdns_pcie_ep_setup()
564 ret = pci_epc_mem_init(epc, pcie->mem_res->start, in cdns_pcie_ep_setup()
565 resource_size(pcie->mem_res), PAGE_SIZE); in cdns_pcie_ep_setup()
571 ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr, in cdns_pcie_ep_setup()
573 if (!ep->irq_cpu_addr) { in cdns_pcie_ep_setup()
575 ret = -ENOMEM; in cdns_pcie_ep_setup()
578 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE; in cdns_pcie_ep_setup()
580 set_bit(0, &ep->ob_region_map); in cdns_pcie_ep_setup()
581 spin_lock_init(&ep->lock); in cdns_pcie_ep_setup()