1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe endpoint driver for Renesas R-Car SoCs
4 * Copyright (c) 2020 Renesas Electronics Europe GmbH
6 * Author: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
9 #include <linux/delay.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/pci.h>
13 #include <linux/pci-epc.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
17 #include "pcie-rcar.h"
19 #define RCAR_EPC_MAX_FUNCTIONS 1
21 /* Structure representing the PCIe interface */
22 struct rcar_pcie_endpoint
{
23 struct rcar_pcie pcie
;
24 phys_addr_t
*ob_mapped_addr
;
25 struct pci_epc_mem_window
*ob_window
;
27 unsigned int bar_to_atu
[MAX_NR_INBOUND_MAPS
];
28 unsigned long *ib_window_map
;
33 static void rcar_pcie_ep_hw_init(struct rcar_pcie
*pcie
)
37 rcar_pci_write_reg(pcie
, 0, PCIETCTLR
);
39 /* Set endpoint mode */
40 rcar_pci_write_reg(pcie
, 0, PCIEMSR
);
42 /* Initialize default capabilities. */
43 rcar_rmw32(pcie
, REXPCAP(0), 0xff, PCI_CAP_ID_EXP
);
44 rcar_rmw32(pcie
, REXPCAP(PCI_EXP_FLAGS
),
45 PCI_EXP_FLAGS_TYPE
, PCI_EXP_TYPE_ENDPOINT
<< 4);
46 rcar_rmw32(pcie
, RCONF(PCI_HEADER_TYPE
), PCI_HEADER_TYPE_MASK
,
47 PCI_HEADER_TYPE_NORMAL
);
49 /* Write out the physical slot number = 0 */
50 rcar_rmw32(pcie
, REXPCAP(PCI_EXP_SLTCAP
), PCI_EXP_SLTCAP_PSN
, 0);
52 val
= rcar_pci_read_reg(pcie
, EXPCAP(1));
53 /* device supports fixed 128 bytes MPSS */
54 val
&= ~GENMASK(2, 0);
55 rcar_pci_write_reg(pcie
, val
, EXPCAP(1));
57 val
= rcar_pci_read_reg(pcie
, EXPCAP(2));
58 /* read requests size 128 bytes */
59 val
&= ~GENMASK(14, 12);
60 /* payload size 128 bytes */
61 val
&= ~GENMASK(7, 5);
62 rcar_pci_write_reg(pcie
, val
, EXPCAP(2));
64 /* Set target link speed to 5.0 GT/s */
65 rcar_rmw32(pcie
, EXPCAP(12), PCI_EXP_LNKSTA_CLS
,
66 PCI_EXP_LNKSTA_CLS_5_0GB
);
68 /* Set the completion timer timeout to the maximum 50ms. */
69 rcar_rmw32(pcie
, TLCTLR
+ 1, 0x3f, 50);
71 /* Terminate list of capabilities (Next Capability Offset=0) */
72 rcar_rmw32(pcie
, RVCCAP(0), 0xfff00000, 0);
74 /* flush modifications */
78 static int rcar_pcie_ep_get_window(struct rcar_pcie_endpoint
*ep
,
83 for (i
= 0; i
< ep
->num_ob_windows
; i
++)
84 if (ep
->ob_window
[i
].phys_base
== addr
)
90 static int rcar_pcie_parse_outbound_ranges(struct rcar_pcie_endpoint
*ep
,
91 struct platform_device
*pdev
)
93 struct rcar_pcie
*pcie
= &ep
->pcie
;
94 char outbound_name
[10];
98 ep
->num_ob_windows
= 0;
99 for (i
= 0; i
< RCAR_PCI_MAX_RESOURCES
; i
++) {
100 sprintf(outbound_name
, "memory%u", i
);
101 res
= platform_get_resource_byname(pdev
,
105 dev_err(pcie
->dev
, "missing outbound window %u\n", i
);
108 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
111 dev_err(pcie
->dev
, "Cannot request memory region %s.\n",
116 ep
->ob_window
[i
].phys_base
= res
->start
;
117 ep
->ob_window
[i
].size
= resource_size(res
);
118 /* controller doesn't support multiple allocation
119 * from same window, so set page_size to window size
121 ep
->ob_window
[i
].page_size
= resource_size(res
);
123 ep
->num_ob_windows
= i
;
128 static int rcar_pcie_ep_get_pdata(struct rcar_pcie_endpoint
*ep
,
129 struct platform_device
*pdev
)
131 struct rcar_pcie
*pcie
= &ep
->pcie
;
132 struct pci_epc_mem_window
*window
;
133 struct device
*dev
= pcie
->dev
;
137 err
= of_address_to_resource(dev
->of_node
, 0, &res
);
140 pcie
->base
= devm_ioremap_resource(dev
, &res
);
141 if (IS_ERR(pcie
->base
))
142 return PTR_ERR(pcie
->base
);
144 ep
->ob_window
= devm_kcalloc(dev
, RCAR_PCI_MAX_RESOURCES
,
145 sizeof(*window
), GFP_KERNEL
);
149 rcar_pcie_parse_outbound_ranges(ep
, pdev
);
151 err
= of_property_read_u8(dev
->of_node
, "max-functions",
153 if (err
< 0 || ep
->max_functions
> RCAR_EPC_MAX_FUNCTIONS
)
154 ep
->max_functions
= RCAR_EPC_MAX_FUNCTIONS
;
159 static int rcar_pcie_ep_write_header(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
160 struct pci_epf_header
*hdr
)
162 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
163 struct rcar_pcie
*pcie
= &ep
->pcie
;
169 val
= rcar_pci_read_reg(pcie
, IDSETR0
);
170 val
|= hdr
->deviceid
<< 16;
171 rcar_pci_write_reg(pcie
, val
, IDSETR0
);
174 val
|= hdr
->progif_code
<< 8;
175 val
|= hdr
->subclass_code
<< 16;
176 val
|= hdr
->baseclass_code
<< 24;
177 rcar_pci_write_reg(pcie
, val
, IDSETR1
);
180 val
= hdr
->subsys_vendor_id
;
182 val
= rcar_pci_read_reg(pcie
, SUBIDSETR
);
183 val
|= hdr
->subsys_id
<< 16;
184 rcar_pci_write_reg(pcie
, val
, SUBIDSETR
);
186 if (hdr
->interrupt_pin
> PCI_INTERRUPT_INTA
)
188 val
= rcar_pci_read_reg(pcie
, PCICONF(15));
189 val
|= (hdr
->interrupt_pin
<< 8);
190 rcar_pci_write_reg(pcie
, val
, PCICONF(15));
195 static int rcar_pcie_ep_set_bar(struct pci_epc
*epc
, u8 func_no
, u8 vfunc_no
,
196 struct pci_epf_bar
*epf_bar
)
198 int flags
= epf_bar
->flags
| LAR_ENABLE
| LAM_64BIT
;
199 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
200 u64 size
= 1ULL << fls64(epf_bar
->size
- 1);
201 dma_addr_t cpu_addr
= epf_bar
->phys_addr
;
202 enum pci_barno bar
= epf_bar
->barno
;
203 struct rcar_pcie
*pcie
= &ep
->pcie
;
208 idx
= find_first_zero_bit(ep
->ib_window_map
, ep
->num_ib_windows
);
209 if (idx
>= ep
->num_ib_windows
) {
210 dev_err(pcie
->dev
, "no free inbound window\n");
214 if ((flags
& PCI_BASE_ADDRESS_SPACE
) == PCI_BASE_ADDRESS_SPACE_IO
)
217 ep
->bar_to_atu
[bar
] = idx
;
218 /* use 64-bit BARs */
219 set_bit(idx
, ep
->ib_window_map
);
220 set_bit(idx
+ 1, ep
->ib_window_map
);
223 unsigned long nr_zeros
= __ffs64(cpu_addr
);
224 u64 alignment
= 1ULL << nr_zeros
;
226 size
= min(size
, alignment
);
229 size
= min(size
, 1ULL << 32);
231 mask
= roundup_pow_of_two(size
) - 1;
234 rcar_pcie_set_inbound(pcie
, cpu_addr
,
235 0x0, mask
| flags
, idx
, false);
237 err
= rcar_pcie_wait_for_phyrdy(pcie
);
239 dev_err(pcie
->dev
, "phy not ready\n");
246 static void rcar_pcie_ep_clear_bar(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
247 struct pci_epf_bar
*epf_bar
)
249 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
250 enum pci_barno bar
= epf_bar
->barno
;
251 u32 atu_index
= ep
->bar_to_atu
[bar
];
253 rcar_pcie_set_inbound(&ep
->pcie
, 0x0, 0x0, 0x0, bar
, false);
255 clear_bit(atu_index
, ep
->ib_window_map
);
256 clear_bit(atu_index
+ 1, ep
->ib_window_map
);
259 static int rcar_pcie_ep_set_msi(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
262 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
263 struct rcar_pcie
*pcie
= &ep
->pcie
;
266 flags
= rcar_pci_read_reg(pcie
, MSICAP(fn
));
267 flags
|= interrupts
<< MSICAP0_MMESCAP_OFFSET
;
268 rcar_pci_write_reg(pcie
, flags
, MSICAP(fn
));
273 static int rcar_pcie_ep_get_msi(struct pci_epc
*epc
, u8 fn
, u8 vfn
)
275 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
276 struct rcar_pcie
*pcie
= &ep
->pcie
;
279 flags
= rcar_pci_read_reg(pcie
, MSICAP(fn
));
280 if (!(flags
& MSICAP0_MSIE
))
283 return ((flags
& MSICAP0_MMESE_MASK
) >> MSICAP0_MMESE_OFFSET
);
286 static int rcar_pcie_ep_map_addr(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
287 phys_addr_t addr
, u64 pci_addr
, size_t size
)
289 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
290 struct rcar_pcie
*pcie
= &ep
->pcie
;
291 struct resource_entry win
;
296 /* check if we have a link. */
297 err
= rcar_pcie_wait_for_dl(pcie
);
299 dev_err(pcie
->dev
, "link not up\n");
303 window
= rcar_pcie_ep_get_window(ep
, addr
);
305 dev_err(pcie
->dev
, "failed to get corresponding window\n");
309 memset(&win
, 0x0, sizeof(win
));
310 memset(&res
, 0x0, sizeof(res
));
311 res
.start
= pci_addr
;
312 res
.end
= pci_addr
+ size
- 1;
313 res
.flags
= IORESOURCE_MEM
;
316 rcar_pcie_set_outbound(pcie
, window
, &win
);
318 ep
->ob_mapped_addr
[window
] = addr
;
323 static void rcar_pcie_ep_unmap_addr(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
326 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
327 struct resource_entry win
;
331 for (idx
= 0; idx
< ep
->num_ob_windows
; idx
++)
332 if (ep
->ob_mapped_addr
[idx
] == addr
)
335 if (idx
>= ep
->num_ob_windows
)
338 memset(&win
, 0x0, sizeof(win
));
339 memset(&res
, 0x0, sizeof(res
));
341 rcar_pcie_set_outbound(&ep
->pcie
, idx
, &win
);
343 ep
->ob_mapped_addr
[idx
] = 0;
346 static int rcar_pcie_ep_assert_intx(struct rcar_pcie_endpoint
*ep
,
349 struct rcar_pcie
*pcie
= &ep
->pcie
;
352 val
= rcar_pci_read_reg(pcie
, PCIEMSITXR
);
353 if ((val
& PCI_MSI_FLAGS_ENABLE
)) {
354 dev_err(pcie
->dev
, "MSI is enabled, cannot assert INTx\n");
358 val
= rcar_pci_read_reg(pcie
, PCICONF(1));
359 if ((val
& INTDIS
)) {
360 dev_err(pcie
->dev
, "INTx message transmission is disabled\n");
364 val
= rcar_pci_read_reg(pcie
, PCIEINTXR
);
365 if ((val
& ASTINTX
)) {
366 dev_err(pcie
->dev
, "INTx is already asserted\n");
371 rcar_pci_write_reg(pcie
, val
, PCIEINTXR
);
372 usleep_range(1000, 1001);
373 val
= rcar_pci_read_reg(pcie
, PCIEINTXR
);
375 rcar_pci_write_reg(pcie
, val
, PCIEINTXR
);
380 static int rcar_pcie_ep_assert_msi(struct rcar_pcie
*pcie
,
381 u8 fn
, u8 interrupt_num
)
386 /* Check MSI enable bit */
387 val
= rcar_pci_read_reg(pcie
, MSICAP(fn
));
388 if (!(val
& MSICAP0_MSIE
))
391 /* Get MSI numbers from MME */
392 msi_count
= ((val
& MSICAP0_MMESE_MASK
) >> MSICAP0_MMESE_OFFSET
);
393 msi_count
= 1 << msi_count
;
395 if (!interrupt_num
|| interrupt_num
> msi_count
)
398 val
= rcar_pci_read_reg(pcie
, PCIEMSITXR
);
399 rcar_pci_write_reg(pcie
, val
| (interrupt_num
- 1), PCIEMSITXR
);
404 static int rcar_pcie_ep_raise_irq(struct pci_epc
*epc
, u8 fn
, u8 vfn
,
405 unsigned int type
, u16 interrupt_num
)
407 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
411 return rcar_pcie_ep_assert_intx(ep
, fn
, 0);
414 return rcar_pcie_ep_assert_msi(&ep
->pcie
, fn
, interrupt_num
);
421 static int rcar_pcie_ep_start(struct pci_epc
*epc
)
423 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
425 rcar_pci_write_reg(&ep
->pcie
, MACCTLR_INIT_VAL
, MACCTLR
);
426 rcar_pci_write_reg(&ep
->pcie
, CFINIT
, PCIETCTLR
);
431 static void rcar_pcie_ep_stop(struct pci_epc
*epc
)
433 struct rcar_pcie_endpoint
*ep
= epc_get_drvdata(epc
);
435 rcar_pci_write_reg(&ep
->pcie
, 0, PCIETCTLR
);
438 static const struct pci_epc_features rcar_pcie_epc_features
= {
439 .linkup_notifier
= false,
441 .msix_capable
= false,
442 /* use 64-bit BARs so mark BAR[1,3,5] as reserved */
443 .bar
[BAR_0
] = { .type
= BAR_FIXED
, .fixed_size
= 128,
444 .only_64bit
= true, },
445 .bar
[BAR_1
] = { .type
= BAR_RESERVED
, },
446 .bar
[BAR_2
] = { .type
= BAR_FIXED
, .fixed_size
= 256,
447 .only_64bit
= true, },
448 .bar
[BAR_3
] = { .type
= BAR_RESERVED
, },
449 .bar
[BAR_4
] = { .type
= BAR_FIXED
, .fixed_size
= 256,
450 .only_64bit
= true, },
451 .bar
[BAR_5
] = { .type
= BAR_RESERVED
, },
454 static const struct pci_epc_features
*
455 rcar_pcie_ep_get_features(struct pci_epc
*epc
, u8 func_no
, u8 vfunc_no
)
457 return &rcar_pcie_epc_features
;
460 static const struct pci_epc_ops rcar_pcie_epc_ops
= {
461 .write_header
= rcar_pcie_ep_write_header
,
462 .set_bar
= rcar_pcie_ep_set_bar
,
463 .clear_bar
= rcar_pcie_ep_clear_bar
,
464 .set_msi
= rcar_pcie_ep_set_msi
,
465 .get_msi
= rcar_pcie_ep_get_msi
,
466 .map_addr
= rcar_pcie_ep_map_addr
,
467 .unmap_addr
= rcar_pcie_ep_unmap_addr
,
468 .raise_irq
= rcar_pcie_ep_raise_irq
,
469 .start
= rcar_pcie_ep_start
,
470 .stop
= rcar_pcie_ep_stop
,
471 .get_features
= rcar_pcie_ep_get_features
,
474 static const struct of_device_id rcar_pcie_ep_of_match
[] = {
475 { .compatible
= "renesas,r8a774c0-pcie-ep", },
476 { .compatible
= "renesas,rcar-gen3-pcie-ep" },
480 static int rcar_pcie_ep_probe(struct platform_device
*pdev
)
482 struct device
*dev
= &pdev
->dev
;
483 struct rcar_pcie_endpoint
*ep
;
484 struct rcar_pcie
*pcie
;
488 ep
= devm_kzalloc(dev
, sizeof(*ep
), GFP_KERNEL
);
495 pm_runtime_enable(dev
);
496 err
= pm_runtime_resume_and_get(dev
);
498 dev_err(dev
, "pm_runtime_resume_and_get failed\n");
502 err
= rcar_pcie_ep_get_pdata(ep
, pdev
);
504 dev_err(dev
, "failed to request resources: %d\n", err
);
508 ep
->num_ib_windows
= MAX_NR_INBOUND_MAPS
;
510 devm_kcalloc(dev
, BITS_TO_LONGS(ep
->num_ib_windows
),
511 sizeof(long), GFP_KERNEL
);
512 if (!ep
->ib_window_map
) {
514 dev_err(dev
, "failed to allocate memory for inbound map\n");
518 ep
->ob_mapped_addr
= devm_kcalloc(dev
, ep
->num_ob_windows
,
519 sizeof(*ep
->ob_mapped_addr
),
521 if (!ep
->ob_mapped_addr
) {
523 dev_err(dev
, "failed to allocate memory for outbound memory pointers\n");
527 epc
= devm_pci_epc_create(dev
, &rcar_pcie_epc_ops
);
529 dev_err(dev
, "failed to create epc device\n");
534 epc
->max_functions
= ep
->max_functions
;
535 epc_set_drvdata(epc
, ep
);
537 rcar_pcie_ep_hw_init(pcie
);
539 err
= pci_epc_multi_mem_init(epc
, ep
->ob_window
, ep
->num_ob_windows
);
541 dev_err(dev
, "failed to initialize the epc memory space\n");
545 pci_epc_init_notify(epc
);
553 pm_runtime_disable(dev
);
558 static struct platform_driver rcar_pcie_ep_driver
= {
560 .name
= "rcar-pcie-ep",
561 .of_match_table
= rcar_pcie_ep_of_match
,
562 .suppress_bind_attrs
= true,
564 .probe
= rcar_pcie_ep_probe
,
566 builtin_platform_driver(rcar_pcie_ep_driver
);