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/pm_runtime.h>
12 #include <linux/sizes.h>
14 #include "pcie-cadence.h"
16 #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
17 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
18 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
20 static int cdns_pcie_ep_write_header(struct pci_epc
*epc
, u8 fn
,
21 struct pci_epf_header
*hdr
)
23 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
24 struct cdns_pcie
*pcie
= &ep
->pcie
;
26 cdns_pcie_ep_fn_writew(pcie
, fn
, PCI_DEVICE_ID
, hdr
->deviceid
);
27 cdns_pcie_ep_fn_writeb(pcie
, fn
, PCI_REVISION_ID
, hdr
->revid
);
28 cdns_pcie_ep_fn_writeb(pcie
, fn
, PCI_CLASS_PROG
, hdr
->progif_code
);
29 cdns_pcie_ep_fn_writew(pcie
, fn
, PCI_CLASS_DEVICE
,
30 hdr
->subclass_code
| hdr
->baseclass_code
<< 8);
31 cdns_pcie_ep_fn_writeb(pcie
, fn
, PCI_CACHE_LINE_SIZE
,
32 hdr
->cache_line_size
);
33 cdns_pcie_ep_fn_writew(pcie
, fn
, PCI_SUBSYSTEM_ID
, hdr
->subsys_id
);
34 cdns_pcie_ep_fn_writeb(pcie
, fn
, PCI_INTERRUPT_PIN
, hdr
->interrupt_pin
);
37 * Vendor ID can only be modified from function 0, all other functions
38 * use the same vendor ID as function 0.
41 /* Update the vendor IDs. */
42 u32 id
= CDNS_PCIE_LM_ID_VENDOR(hdr
->vendorid
) |
43 CDNS_PCIE_LM_ID_SUBSYS(hdr
->subsys_vendor_id
);
45 cdns_pcie_writel(pcie
, CDNS_PCIE_LM_ID
, id
);
51 static int cdns_pcie_ep_set_bar(struct pci_epc
*epc
, u8 fn
,
52 struct pci_epf_bar
*epf_bar
)
54 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
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
);
118 static void cdns_pcie_ep_clear_bar(struct pci_epc
*epc
, u8 fn
,
119 struct pci_epf_bar
*epf_bar
)
121 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
122 struct cdns_pcie
*pcie
= &ep
->pcie
;
123 enum pci_barno bar
= epf_bar
->barno
;
124 u32 reg
, cfg
, b
, ctrl
;
127 reg
= CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn
);
130 reg
= CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn
);
134 ctrl
= CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED
;
135 cfg
= cdns_pcie_readl(pcie
, reg
);
136 cfg
&= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b
) |
137 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b
));
138 cfg
|= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b
, ctrl
);
139 cdns_pcie_writel(pcie
, reg
, cfg
);
141 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn
, bar
), 0);
142 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn
, bar
), 0);
145 static int cdns_pcie_ep_map_addr(struct pci_epc
*epc
, u8 fn
, phys_addr_t addr
,
146 u64 pci_addr
, size_t size
)
148 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
149 struct cdns_pcie
*pcie
= &ep
->pcie
;
152 r
= find_first_zero_bit(&ep
->ob_region_map
,
153 sizeof(ep
->ob_region_map
) * BITS_PER_LONG
);
154 if (r
>= ep
->max_regions
- 1) {
155 dev_err(&epc
->dev
, "no free outbound region\n");
159 cdns_pcie_set_outbound_region(pcie
, fn
, r
, false, addr
, pci_addr
, size
);
161 set_bit(r
, &ep
->ob_region_map
);
162 ep
->ob_addr
[r
] = addr
;
167 static void cdns_pcie_ep_unmap_addr(struct pci_epc
*epc
, u8 fn
,
170 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
171 struct cdns_pcie
*pcie
= &ep
->pcie
;
174 for (r
= 0; r
< ep
->max_regions
- 1; r
++)
175 if (ep
->ob_addr
[r
] == addr
)
178 if (r
== ep
->max_regions
- 1)
181 cdns_pcie_reset_outbound_region(pcie
, r
);
184 clear_bit(r
, &ep
->ob_region_map
);
187 static int cdns_pcie_ep_set_msi(struct pci_epc
*epc
, u8 fn
, u8 mmc
)
189 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
190 struct cdns_pcie
*pcie
= &ep
->pcie
;
191 u32 cap
= CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET
;
195 * Set the Multiple Message Capable bitfield into the Message Control
198 flags
= cdns_pcie_ep_fn_readw(pcie
, fn
, cap
+ PCI_MSI_FLAGS
);
199 flags
= (flags
& ~PCI_MSI_FLAGS_QMASK
) | (mmc
<< 1);
200 flags
|= PCI_MSI_FLAGS_64BIT
;
201 flags
&= ~PCI_MSI_FLAGS_MASKBIT
;
202 cdns_pcie_ep_fn_writew(pcie
, fn
, cap
+ PCI_MSI_FLAGS
, flags
);
207 static int cdns_pcie_ep_get_msi(struct pci_epc
*epc
, u8 fn
)
209 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
210 struct cdns_pcie
*pcie
= &ep
->pcie
;
211 u32 cap
= CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET
;
214 /* Validate that the MSI feature is actually enabled. */
215 flags
= cdns_pcie_ep_fn_readw(pcie
, fn
, cap
+ PCI_MSI_FLAGS
);
216 if (!(flags
& PCI_MSI_FLAGS_ENABLE
))
220 * Get the Multiple Message Enable bitfield from the Message Control
223 mme
= (flags
& PCI_MSI_FLAGS_QSIZE
) >> 4;
228 static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep
*ep
, u8 fn
,
229 u8 intx
, bool is_asserted
)
231 struct cdns_pcie
*pcie
= &ep
->pcie
;
238 /* Set the outbound region if needed. */
239 if (unlikely(ep
->irq_pci_addr
!= CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY
||
240 ep
->irq_pci_fn
!= fn
)) {
241 /* First region was reserved for IRQ writes. */
242 cdns_pcie_set_outbound_region_for_normal_msg(pcie
, fn
, 0,
244 ep
->irq_pci_addr
= CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY
;
249 ep
->irq_pending
|= BIT(intx
);
250 msg_code
= MSG_CODE_ASSERT_INTA
+ intx
;
252 ep
->irq_pending
&= ~BIT(intx
);
253 msg_code
= MSG_CODE_DEASSERT_INTA
+ intx
;
256 status
= cdns_pcie_ep_fn_readw(pcie
, fn
, PCI_STATUS
);
257 if (((status
& PCI_STATUS_INTERRUPT
) != 0) ^ (ep
->irq_pending
!= 0)) {
258 status
^= PCI_STATUS_INTERRUPT
;
259 cdns_pcie_ep_fn_writew(pcie
, fn
, PCI_STATUS
, status
);
262 offset
= CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL
) |
263 CDNS_PCIE_NORMAL_MSG_CODE(msg_code
) |
264 CDNS_PCIE_MSG_NO_DATA
;
265 writel(0, ep
->irq_cpu_addr
+ offset
);
268 static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep
*ep
, u8 fn
, u8 intx
)
272 cmd
= cdns_pcie_ep_fn_readw(&ep
->pcie
, fn
, PCI_COMMAND
);
273 if (cmd
& PCI_COMMAND_INTX_DISABLE
)
276 cdns_pcie_ep_assert_intx(ep
, fn
, intx
, true);
278 * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
279 * from drivers/pci/dwc/pci-dra7xx.c
282 cdns_pcie_ep_assert_intx(ep
, fn
, intx
, false);
286 static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep
*ep
, u8 fn
,
289 struct cdns_pcie
*pcie
= &ep
->pcie
;
290 u32 cap
= CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET
;
291 u16 flags
, mme
, data
, data_mask
;
293 u64 pci_addr
, pci_addr_mask
= 0xff;
295 /* Check whether the MSI feature has been enabled by the PCI host. */
296 flags
= cdns_pcie_ep_fn_readw(pcie
, fn
, cap
+ PCI_MSI_FLAGS
);
297 if (!(flags
& PCI_MSI_FLAGS_ENABLE
))
300 /* Get the number of enabled MSIs */
301 mme
= (flags
& PCI_MSI_FLAGS_QSIZE
) >> 4;
302 msi_count
= 1 << mme
;
303 if (!interrupt_num
|| interrupt_num
> msi_count
)
306 /* Compute the data value to be written. */
307 data_mask
= msi_count
- 1;
308 data
= cdns_pcie_ep_fn_readw(pcie
, fn
, cap
+ PCI_MSI_DATA_64
);
309 data
= (data
& ~data_mask
) | ((interrupt_num
- 1) & data_mask
);
311 /* Get the PCI address where to write the data into. */
312 pci_addr
= cdns_pcie_ep_fn_readl(pcie
, fn
, cap
+ PCI_MSI_ADDRESS_HI
);
314 pci_addr
|= cdns_pcie_ep_fn_readl(pcie
, fn
, cap
+ PCI_MSI_ADDRESS_LO
);
315 pci_addr
&= GENMASK_ULL(63, 2);
317 /* Set the outbound region if needed. */
318 if (unlikely(ep
->irq_pci_addr
!= (pci_addr
& ~pci_addr_mask
) ||
319 ep
->irq_pci_fn
!= fn
)) {
320 /* First region was reserved for IRQ writes. */
321 cdns_pcie_set_outbound_region(pcie
, fn
, 0,
324 pci_addr
& ~pci_addr_mask
,
326 ep
->irq_pci_addr
= (pci_addr
& ~pci_addr_mask
);
329 writel(data
, ep
->irq_cpu_addr
+ (pci_addr
& pci_addr_mask
));
334 static int cdns_pcie_ep_raise_irq(struct pci_epc
*epc
, u8 fn
,
335 enum pci_epc_irq_type type
,
338 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
341 case PCI_EPC_IRQ_LEGACY
:
342 return cdns_pcie_ep_send_legacy_irq(ep
, fn
, 0);
344 case PCI_EPC_IRQ_MSI
:
345 return cdns_pcie_ep_send_msi_irq(ep
, fn
, interrupt_num
);
354 static int cdns_pcie_ep_start(struct pci_epc
*epc
)
356 struct cdns_pcie_ep
*ep
= epc_get_drvdata(epc
);
357 struct cdns_pcie
*pcie
= &ep
->pcie
;
362 * BIT(0) is hardwired to 1, hence function 0 is always enabled
363 * and can't be disabled anyway.
366 list_for_each_entry(epf
, &epc
->pci_epf
, list
)
367 cfg
|= BIT(epf
->func_no
);
368 cdns_pcie_writel(pcie
, CDNS_PCIE_LM_EP_FUNC_CFG
, cfg
);
373 static const struct pci_epc_features cdns_pcie_epc_features
= {
374 .linkup_notifier
= false,
376 .msix_capable
= false,
379 static const struct pci_epc_features
*
380 cdns_pcie_ep_get_features(struct pci_epc
*epc
, u8 func_no
)
382 return &cdns_pcie_epc_features
;
385 static const struct pci_epc_ops cdns_pcie_epc_ops
= {
386 .write_header
= cdns_pcie_ep_write_header
,
387 .set_bar
= cdns_pcie_ep_set_bar
,
388 .clear_bar
= cdns_pcie_ep_clear_bar
,
389 .map_addr
= cdns_pcie_ep_map_addr
,
390 .unmap_addr
= cdns_pcie_ep_unmap_addr
,
391 .set_msi
= cdns_pcie_ep_set_msi
,
392 .get_msi
= cdns_pcie_ep_get_msi
,
393 .raise_irq
= cdns_pcie_ep_raise_irq
,
394 .start
= cdns_pcie_ep_start
,
395 .get_features
= cdns_pcie_ep_get_features
,
399 int cdns_pcie_ep_setup(struct cdns_pcie_ep
*ep
)
401 struct device
*dev
= ep
->pcie
.dev
;
402 struct platform_device
*pdev
= to_platform_device(dev
);
403 struct device_node
*np
= dev
->of_node
;
404 struct cdns_pcie
*pcie
= &ep
->pcie
;
405 struct resource
*res
;
411 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "reg");
412 pcie
->reg_base
= devm_ioremap_resource(dev
, res
);
413 if (IS_ERR(pcie
->reg_base
)) {
414 dev_err(dev
, "missing \"reg\"\n");
415 return PTR_ERR(pcie
->reg_base
);
418 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem");
420 dev_err(dev
, "missing \"mem\"\n");
425 ret
= of_property_read_u32(np
, "cdns,max-outbound-regions",
428 dev_err(dev
, "missing \"cdns,max-outbound-regions\"\n");
431 ep
->ob_addr
= devm_kcalloc(dev
,
432 ep
->max_regions
, sizeof(*ep
->ob_addr
),
437 /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
438 cdns_pcie_writel(pcie
, CDNS_PCIE_LM_EP_FUNC_CFG
, BIT(0));
440 epc
= devm_pci_epc_create(dev
, &cdns_pcie_epc_ops
);
442 dev_err(dev
, "failed to create epc device\n");
447 epc_set_drvdata(epc
, ep
);
449 if (of_property_read_u8(np
, "max-functions", &epc
->max_functions
) < 0)
450 epc
->max_functions
= 1;
452 ret
= pci_epc_mem_init(epc
, pcie
->mem_res
->start
,
453 resource_size(pcie
->mem_res
));
455 dev_err(dev
, "failed to initialize the memory space\n");
459 ep
->irq_cpu_addr
= pci_epc_mem_alloc_addr(epc
, &ep
->irq_phys_addr
,
461 if (!ep
->irq_cpu_addr
) {
462 dev_err(dev
, "failed to reserve memory space for MSI\n");
466 ep
->irq_pci_addr
= CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE
;
467 /* Reserve region 0 for IRQs */
468 set_bit(0, &ep
->ob_region_map
);
473 pci_epc_mem_exit(epc
);
476 pm_runtime_put_sync(dev
);