1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe endpoint controller driver.
4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
9 #include <linux/pci-epc.h>
10 #include <linux/platform_device.h>
11 #include <linux/sizes.h>
13 #include "pcie-cadence.h"
15 #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
16 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
17 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
19 static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn,
20 struct pci_epf_header *hdr)
22 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
23 struct cdns_pcie *pcie = &ep->pcie;
25 cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
26 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
27 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
28 cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
29 hdr->subclass_code | hdr->baseclass_code << 8);
30 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
31 hdr->cache_line_size);
32 cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
33 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
36 * Vendor ID can only be modified from function 0, all other functions
37 * use the same vendor ID as function 0.
40 /* Update the vendor IDs. */
41 u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
42 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
44 cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
50 static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn,
51 struct pci_epf_bar *epf_bar)
53 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
54 struct cdns_pcie_epf *epf = &ep->epf[fn];
55 struct cdns_pcie *pcie = &ep->pcie;
56 dma_addr_t bar_phys = epf_bar->phys_addr;
57 enum pci_barno bar = epf_bar->barno;
58 int flags = epf_bar->flags;
59 u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
62 /* BAR size is 2^(aperture + 7) */
63 sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
65 * roundup_pow_of_two() returns an unsigned long, which is not suited
68 sz = 1ULL << fls64(sz - 1);
69 aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
71 if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
72 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
74 bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
75 bool is_64bits = sz > SZ_2G;
77 if (is_64bits && (bar & 1))
80 if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
81 epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
83 if (is_64bits && is_prefetch)
84 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
86 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
88 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
90 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
93 addr0 = lower_32_bits(bar_phys);
94 addr1 = upper_32_bits(bar_phys);
95 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
97 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
101 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
104 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
108 cfg = cdns_pcie_readl(pcie, reg);
109 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
110 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
111 cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
112 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
113 cdns_pcie_writel(pcie, reg, cfg);
115 epf->epf_bar[bar] = epf_bar;
120 static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn,
121 struct pci_epf_bar *epf_bar)
123 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
124 struct cdns_pcie_epf *epf = &ep->epf[fn];
125 struct cdns_pcie *pcie = &ep->pcie;
126 enum pci_barno bar = epf_bar->barno;
127 u32 reg, cfg, b, ctrl;
130 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
133 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
137 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
138 cfg = cdns_pcie_readl(pcie, reg);
139 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
140 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
141 cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
142 cdns_pcie_writel(pcie, reg, cfg);
144 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
145 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
147 epf->epf_bar[bar] = NULL;
150 static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, phys_addr_t addr,
151 u64 pci_addr, size_t size)
153 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
154 struct cdns_pcie *pcie = &ep->pcie;
157 r = find_first_zero_bit(&ep->ob_region_map, BITS_PER_LONG);
158 if (r >= ep->max_regions - 1) {
159 dev_err(&epc->dev, "no free outbound region\n");
163 cdns_pcie_set_outbound_region(pcie, 0, fn, r, false, addr, pci_addr, size);
165 set_bit(r, &ep->ob_region_map);
166 ep->ob_addr[r] = addr;
171 static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn,
174 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
175 struct cdns_pcie *pcie = &ep->pcie;
178 for (r = 0; r < ep->max_regions - 1; r++)
179 if (ep->ob_addr[r] == addr)
182 if (r == ep->max_regions - 1)
185 cdns_pcie_reset_outbound_region(pcie, r);
188 clear_bit(r, &ep->ob_region_map);
191 static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 mmc)
193 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
194 struct cdns_pcie *pcie = &ep->pcie;
195 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
199 * Set the Multiple Message Capable bitfield into the Message Control
202 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
203 flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
204 flags |= PCI_MSI_FLAGS_64BIT;
205 flags &= ~PCI_MSI_FLAGS_MASKBIT;
206 cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
211 static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn)
213 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
214 struct cdns_pcie *pcie = &ep->pcie;
215 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
218 /* Validate that the MSI feature is actually enabled. */
219 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
220 if (!(flags & PCI_MSI_FLAGS_ENABLE))
224 * Get the Multiple Message Enable bitfield from the Message Control
227 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
232 static int cdns_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no)
234 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
235 struct cdns_pcie *pcie = &ep->pcie;
236 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
239 reg = cap + PCI_MSIX_FLAGS;
240 val = cdns_pcie_ep_fn_readw(pcie, func_no, reg);
241 if (!(val & PCI_MSIX_FLAGS_ENABLE))
244 val &= PCI_MSIX_FLAGS_QSIZE;
249 static int cdns_pcie_ep_set_msix(struct pci_epc *epc, u8 fn, u16 interrupts,
250 enum pci_barno bir, u32 offset)
252 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
253 struct cdns_pcie *pcie = &ep->pcie;
254 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
257 reg = cap + PCI_MSIX_FLAGS;
258 val = cdns_pcie_ep_fn_readw(pcie, fn, reg);
259 val &= ~PCI_MSIX_FLAGS_QSIZE;
261 cdns_pcie_ep_fn_writew(pcie, fn, reg, val);
263 /* Set MSIX BAR and offset */
264 reg = cap + PCI_MSIX_TABLE;
266 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
268 /* Set PBA BAR and offset. BAR must match MSIX BAR */
269 reg = cap + PCI_MSIX_PBA;
270 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
271 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
276 static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
277 u8 intx, bool is_asserted)
279 struct cdns_pcie *pcie = &ep->pcie;
287 /* Set the outbound region if needed. */
288 if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
289 ep->irq_pci_fn != fn)) {
290 /* First region was reserved for IRQ writes. */
291 cdns_pcie_set_outbound_region_for_normal_msg(pcie, 0, fn, 0,
293 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
298 ep->irq_pending |= BIT(intx);
299 msg_code = MSG_CODE_ASSERT_INTA + intx;
301 ep->irq_pending &= ~BIT(intx);
302 msg_code = MSG_CODE_DEASSERT_INTA + intx;
305 spin_lock_irqsave(&ep->lock, flags);
306 status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
307 if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
308 status ^= PCI_STATUS_INTERRUPT;
309 cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
311 spin_unlock_irqrestore(&ep->lock, flags);
313 offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
314 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
315 CDNS_PCIE_MSG_NO_DATA;
316 writel(0, ep->irq_cpu_addr + offset);
319 static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
323 cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
324 if (cmd & PCI_COMMAND_INTX_DISABLE)
327 cdns_pcie_ep_assert_intx(ep, fn, intx, true);
329 * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
332 cdns_pcie_ep_assert_intx(ep, fn, intx, false);
336 static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn,
339 struct cdns_pcie *pcie = &ep->pcie;
340 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
341 u16 flags, mme, data, data_mask;
343 u64 pci_addr, pci_addr_mask = 0xff;
345 /* Check whether the MSI feature has been enabled by the PCI host. */
346 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
347 if (!(flags & PCI_MSI_FLAGS_ENABLE))
350 /* Get the number of enabled MSIs */
351 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
352 msi_count = 1 << mme;
353 if (!interrupt_num || interrupt_num > msi_count)
356 /* Compute the data value to be written. */
357 data_mask = msi_count - 1;
358 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
359 data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
361 /* Get the PCI address where to write the data into. */
362 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
364 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
365 pci_addr &= GENMASK_ULL(63, 2);
367 /* Set the outbound region if needed. */
368 if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
369 ep->irq_pci_fn != fn)) {
370 /* First region was reserved for IRQ writes. */
371 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
374 pci_addr & ~pci_addr_mask,
376 ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
379 writel(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
384 static int cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep *ep, u8 fn,
387 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
388 u32 tbl_offset, msg_data, reg;
389 struct cdns_pcie *pcie = &ep->pcie;
390 struct pci_epf_msix_tbl *msix_tbl;
391 struct cdns_pcie_epf *epf;
392 u64 pci_addr_mask = 0xff;
397 /* Check whether the MSI-X feature has been enabled by the PCI host. */
398 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSIX_FLAGS);
399 if (!(flags & PCI_MSIX_FLAGS_ENABLE))
402 reg = cap + PCI_MSIX_TABLE;
403 tbl_offset = cdns_pcie_ep_fn_readl(pcie, fn, reg);
404 bir = tbl_offset & PCI_MSIX_TABLE_BIR;
405 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
408 msix_tbl = epf->epf_bar[bir]->addr + tbl_offset;
409 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
410 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
412 /* Set the outbound region if needed. */
413 if (ep->irq_pci_addr != (msg_addr & ~pci_addr_mask) ||
414 ep->irq_pci_fn != fn) {
415 /* First region was reserved for IRQ writes. */
416 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
419 msg_addr & ~pci_addr_mask,
421 ep->irq_pci_addr = (msg_addr & ~pci_addr_mask);
424 writel(msg_data, ep->irq_cpu_addr + (msg_addr & pci_addr_mask));
429 static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn,
430 enum pci_epc_irq_type type,
433 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
436 case PCI_EPC_IRQ_LEGACY:
437 return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
439 case PCI_EPC_IRQ_MSI:
440 return cdns_pcie_ep_send_msi_irq(ep, fn, interrupt_num);
442 case PCI_EPC_IRQ_MSIX:
443 return cdns_pcie_ep_send_msix_irq(ep, fn, interrupt_num);
452 static int cdns_pcie_ep_start(struct pci_epc *epc)
454 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
455 struct cdns_pcie *pcie = &ep->pcie;
456 struct device *dev = pcie->dev;
462 * BIT(0) is hardwired to 1, hence function 0 is always enabled
463 * and can't be disabled anyway.
466 list_for_each_entry(epf, &epc->pci_epf, list)
467 cfg |= BIT(epf->func_no);
468 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
470 ret = cdns_pcie_start_link(pcie);
472 dev_err(dev, "Failed to start link\n");
479 static const struct pci_epc_features cdns_pcie_epc_features = {
480 .linkup_notifier = false,
482 .msix_capable = true,
485 static const struct pci_epc_features*
486 cdns_pcie_ep_get_features(struct pci_epc *epc, u8 func_no)
488 return &cdns_pcie_epc_features;
491 static const struct pci_epc_ops cdns_pcie_epc_ops = {
492 .write_header = cdns_pcie_ep_write_header,
493 .set_bar = cdns_pcie_ep_set_bar,
494 .clear_bar = cdns_pcie_ep_clear_bar,
495 .map_addr = cdns_pcie_ep_map_addr,
496 .unmap_addr = cdns_pcie_ep_unmap_addr,
497 .set_msi = cdns_pcie_ep_set_msi,
498 .get_msi = cdns_pcie_ep_get_msi,
499 .set_msix = cdns_pcie_ep_set_msix,
500 .get_msix = cdns_pcie_ep_get_msix,
501 .raise_irq = cdns_pcie_ep_raise_irq,
502 .start = cdns_pcie_ep_start,
503 .get_features = cdns_pcie_ep_get_features,
507 int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
509 struct device *dev = ep->pcie.dev;
510 struct platform_device *pdev = to_platform_device(dev);
511 struct device_node *np = dev->of_node;
512 struct cdns_pcie *pcie = &ep->pcie;
513 struct resource *res;
519 pcie->reg_base = devm_platform_ioremap_resource_byname(pdev, "reg");
520 if (IS_ERR(pcie->reg_base)) {
521 dev_err(dev, "missing \"reg\"\n");
522 return PTR_ERR(pcie->reg_base);
525 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
527 dev_err(dev, "missing \"mem\"\n");
532 ret = of_property_read_u32(np, "cdns,max-outbound-regions",
535 dev_err(dev, "missing \"cdns,max-outbound-regions\"\n");
538 ep->ob_addr = devm_kcalloc(dev,
539 ep->max_regions, sizeof(*ep->ob_addr),
544 /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
545 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
547 epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
549 dev_err(dev, "failed to create epc device\n");
553 epc_set_drvdata(epc, ep);
555 if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
556 epc->max_functions = 1;
558 ep->epf = devm_kcalloc(dev, epc->max_functions, sizeof(*ep->epf),
563 ret = pci_epc_mem_init(epc, pcie->mem_res->start,
564 resource_size(pcie->mem_res), PAGE_SIZE);
566 dev_err(dev, "failed to initialize the memory space\n");
570 ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
572 if (!ep->irq_cpu_addr) {
573 dev_err(dev, "failed to reserve memory space for MSI\n");
577 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
578 /* Reserve region 0 for IRQs */
579 set_bit(0, &ep->ob_region_map);
581 if (ep->quirk_detect_quiet_flag)
582 cdns_pcie_detect_quiet_min_delay_set(&ep->pcie);
584 spin_lock_init(&ep->lock);
589 pci_epc_mem_exit(epc);