Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / pci / controller / dwc / pcie-rcar-gen4.c
blobfc872dd35029c083da58144dce23cd1ce80f9374
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PCIe controller driver for Renesas R-Car Gen4 Series SoCs
4 * Copyright (C) 2022-2023 Renesas Electronics Corporation
6 * The r8a779g0 (R-Car V4H) controller requires a specific firmware to be
7 * provided, to initialize the PHY. Otherwise, the PCIe controller will not
8 * work.
9 */
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/iopoll.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/pci.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/reset.h>
23 #include "../../pci.h"
24 #include "pcie-designware.h"
26 /* Renesas-specific */
27 /* PCIe Mode Setting Register 0 */
28 #define PCIEMSR0 0x0000
29 #define APP_SRIS_MODE BIT(6)
30 #define DEVICE_TYPE_EP 0
31 #define DEVICE_TYPE_RC BIT(4)
32 #define BIFUR_MOD_SET_ON BIT(0)
34 /* PCIe Interrupt Status 0 */
35 #define PCIEINTSTS0 0x0084
37 /* PCIe Interrupt Status 0 Enable */
38 #define PCIEINTSTS0EN 0x0310
39 #define MSI_CTRL_INT BIT(26)
40 #define SMLH_LINK_UP BIT(7)
41 #define RDLH_LINK_UP BIT(6)
43 /* PCIe DMA Interrupt Status Enable */
44 #define PCIEDMAINTSTSEN 0x0314
45 #define PCIEDMAINTSTSEN_INIT GENMASK(15, 0)
47 /* Port Logic Registers 89 */
48 #define PRTLGC89 0x0b70
50 /* Port Logic Registers 90 */
51 #define PRTLGC90 0x0b74
53 /* PCIe Reset Control Register 1 */
54 #define PCIERSTCTRL1 0x0014
55 #define APP_HOLD_PHY_RST BIT(16)
56 #define APP_LTSSM_ENABLE BIT(0)
58 /* PCIe Power Management Control */
59 #define PCIEPWRMNGCTRL 0x0070
60 #define APP_CLK_REQ_N BIT(11)
61 #define APP_CLK_PM_EN BIT(10)
63 #define RCAR_NUM_SPEED_CHANGE_RETRIES 10
64 #define RCAR_MAX_LINK_SPEED 4
66 #define RCAR_GEN4_PCIE_EP_FUNC_DBI_OFFSET 0x1000
67 #define RCAR_GEN4_PCIE_EP_FUNC_DBI2_OFFSET 0x800
69 #define RCAR_GEN4_PCIE_FIRMWARE_NAME "rcar_gen4_pcie.bin"
70 #define RCAR_GEN4_PCIE_FIRMWARE_BASE_ADDR 0xc000
71 MODULE_FIRMWARE(RCAR_GEN4_PCIE_FIRMWARE_NAME);
73 struct rcar_gen4_pcie;
74 struct rcar_gen4_pcie_drvdata {
75 void (*additional_common_init)(struct rcar_gen4_pcie *rcar);
76 int (*ltssm_control)(struct rcar_gen4_pcie *rcar, bool enable);
77 enum dw_pcie_device_mode mode;
80 struct rcar_gen4_pcie {
81 struct dw_pcie dw;
82 void __iomem *base;
83 void __iomem *phy_base;
84 struct platform_device *pdev;
85 const struct rcar_gen4_pcie_drvdata *drvdata;
87 #define to_rcar_gen4_pcie(_dw) container_of(_dw, struct rcar_gen4_pcie, dw)
89 /* Common */
90 static int rcar_gen4_pcie_link_up(struct dw_pcie *dw)
92 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
93 u32 val, mask;
95 val = readl(rcar->base + PCIEINTSTS0);
96 mask = RDLH_LINK_UP | SMLH_LINK_UP;
98 return (val & mask) == mask;
102 * Manually initiate the speed change. Return 0 if change succeeded; otherwise
103 * -ETIMEDOUT.
105 static int rcar_gen4_pcie_speed_change(struct dw_pcie *dw)
107 u32 val;
108 int i;
110 val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
111 val &= ~PORT_LOGIC_SPEED_CHANGE;
112 dw_pcie_writel_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
114 val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
115 val |= PORT_LOGIC_SPEED_CHANGE;
116 dw_pcie_writel_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
118 for (i = 0; i < RCAR_NUM_SPEED_CHANGE_RETRIES; i++) {
119 val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
120 if (!(val & PORT_LOGIC_SPEED_CHANGE))
121 return 0;
122 usleep_range(10000, 11000);
125 return -ETIMEDOUT;
129 * Enable LTSSM of this controller and manually initiate the speed change.
130 * Always return 0.
132 static int rcar_gen4_pcie_start_link(struct dw_pcie *dw)
134 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
135 int i, changes, ret;
137 if (rcar->drvdata->ltssm_control) {
138 ret = rcar->drvdata->ltssm_control(rcar, true);
139 if (ret)
140 return ret;
144 * Require direct speed change with retrying here if the max_link_speed
145 * is PCIe Gen2 or higher.
147 changes = min_not_zero(dw->max_link_speed, RCAR_MAX_LINK_SPEED) - 1;
150 * Since dw_pcie_setup_rc() sets it once, PCIe Gen2 will be trained.
151 * So, this needs remaining times for up to PCIe Gen4 if RC mode.
153 if (changes && rcar->drvdata->mode == DW_PCIE_RC_TYPE)
154 changes--;
156 for (i = 0; i < changes; i++) {
157 /* It may not be connected in EP mode yet. So, break the loop */
158 if (rcar_gen4_pcie_speed_change(dw))
159 break;
162 return 0;
165 static void rcar_gen4_pcie_stop_link(struct dw_pcie *dw)
167 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
169 if (rcar->drvdata->ltssm_control)
170 rcar->drvdata->ltssm_control(rcar, false);
173 static int rcar_gen4_pcie_common_init(struct rcar_gen4_pcie *rcar)
175 struct dw_pcie *dw = &rcar->dw;
176 u32 val;
177 int ret;
179 ret = clk_bulk_prepare_enable(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
180 if (ret) {
181 dev_err(dw->dev, "Enabling core clocks failed\n");
182 return ret;
185 if (!reset_control_status(dw->core_rsts[DW_PCIE_PWR_RST].rstc))
186 reset_control_assert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
188 val = readl(rcar->base + PCIEMSR0);
189 if (rcar->drvdata->mode == DW_PCIE_RC_TYPE) {
190 val |= DEVICE_TYPE_RC;
191 } else if (rcar->drvdata->mode == DW_PCIE_EP_TYPE) {
192 val |= DEVICE_TYPE_EP;
193 } else {
194 ret = -EINVAL;
195 goto err_unprepare;
198 if (dw->num_lanes < 4)
199 val |= BIFUR_MOD_SET_ON;
201 writel(val, rcar->base + PCIEMSR0);
203 ret = reset_control_deassert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
204 if (ret)
205 goto err_unprepare;
207 if (rcar->drvdata->additional_common_init)
208 rcar->drvdata->additional_common_init(rcar);
210 return 0;
212 err_unprepare:
213 clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
215 return ret;
218 static void rcar_gen4_pcie_common_deinit(struct rcar_gen4_pcie *rcar)
220 struct dw_pcie *dw = &rcar->dw;
222 reset_control_assert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
223 clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
226 static int rcar_gen4_pcie_prepare(struct rcar_gen4_pcie *rcar)
228 struct device *dev = rcar->dw.dev;
229 int err;
231 pm_runtime_enable(dev);
232 err = pm_runtime_resume_and_get(dev);
233 if (err < 0) {
234 dev_err(dev, "Runtime resume failed\n");
235 pm_runtime_disable(dev);
238 return err;
241 static void rcar_gen4_pcie_unprepare(struct rcar_gen4_pcie *rcar)
243 struct device *dev = rcar->dw.dev;
245 pm_runtime_put(dev);
246 pm_runtime_disable(dev);
249 static int rcar_gen4_pcie_get_resources(struct rcar_gen4_pcie *rcar)
251 rcar->phy_base = devm_platform_ioremap_resource_byname(rcar->pdev, "phy");
252 if (IS_ERR(rcar->phy_base))
253 return PTR_ERR(rcar->phy_base);
255 /* Renesas-specific registers */
256 rcar->base = devm_platform_ioremap_resource_byname(rcar->pdev, "app");
258 return PTR_ERR_OR_ZERO(rcar->base);
261 static const struct dw_pcie_ops dw_pcie_ops = {
262 .start_link = rcar_gen4_pcie_start_link,
263 .stop_link = rcar_gen4_pcie_stop_link,
264 .link_up = rcar_gen4_pcie_link_up,
267 static struct rcar_gen4_pcie *rcar_gen4_pcie_alloc(struct platform_device *pdev)
269 struct device *dev = &pdev->dev;
270 struct rcar_gen4_pcie *rcar;
272 rcar = devm_kzalloc(dev, sizeof(*rcar), GFP_KERNEL);
273 if (!rcar)
274 return ERR_PTR(-ENOMEM);
276 rcar->dw.ops = &dw_pcie_ops;
277 rcar->dw.dev = dev;
278 rcar->pdev = pdev;
279 rcar->dw.edma.mf = EDMA_MF_EDMA_UNROLL;
280 dw_pcie_cap_set(&rcar->dw, REQ_RES);
281 platform_set_drvdata(pdev, rcar);
283 return rcar;
286 /* Host mode */
287 static int rcar_gen4_pcie_host_init(struct dw_pcie_rp *pp)
289 struct dw_pcie *dw = to_dw_pcie_from_pp(pp);
290 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
291 int ret;
292 u32 val;
294 gpiod_set_value_cansleep(dw->pe_rst, 1);
296 ret = rcar_gen4_pcie_common_init(rcar);
297 if (ret)
298 return ret;
301 * According to the section 3.5.7.2 "RC Mode" in DWC PCIe Dual Mode
302 * Rev.5.20a and 3.5.6.1 "RC mode" in DWC PCIe RC databook v5.20a, we
303 * should disable two BARs to avoid unnecessary memory assignment
304 * during device enumeration.
306 dw_pcie_writel_dbi2(dw, PCI_BASE_ADDRESS_0, 0x0);
307 dw_pcie_writel_dbi2(dw, PCI_BASE_ADDRESS_1, 0x0);
309 /* Enable MSI interrupt signal */
310 val = readl(rcar->base + PCIEINTSTS0EN);
311 val |= MSI_CTRL_INT;
312 writel(val, rcar->base + PCIEINTSTS0EN);
314 msleep(PCIE_T_PVPERL_MS); /* pe_rst requires 100msec delay */
316 gpiod_set_value_cansleep(dw->pe_rst, 0);
318 return 0;
321 static void rcar_gen4_pcie_host_deinit(struct dw_pcie_rp *pp)
323 struct dw_pcie *dw = to_dw_pcie_from_pp(pp);
324 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
326 gpiod_set_value_cansleep(dw->pe_rst, 1);
327 rcar_gen4_pcie_common_deinit(rcar);
330 static const struct dw_pcie_host_ops rcar_gen4_pcie_host_ops = {
331 .init = rcar_gen4_pcie_host_init,
332 .deinit = rcar_gen4_pcie_host_deinit,
335 static int rcar_gen4_add_dw_pcie_rp(struct rcar_gen4_pcie *rcar)
337 struct dw_pcie_rp *pp = &rcar->dw.pp;
339 if (!IS_ENABLED(CONFIG_PCIE_RCAR_GEN4_HOST))
340 return -ENODEV;
342 pp->num_vectors = MAX_MSI_IRQS;
343 pp->ops = &rcar_gen4_pcie_host_ops;
345 return dw_pcie_host_init(pp);
348 static void rcar_gen4_remove_dw_pcie_rp(struct rcar_gen4_pcie *rcar)
350 dw_pcie_host_deinit(&rcar->dw.pp);
353 /* Endpoint mode */
354 static void rcar_gen4_pcie_ep_pre_init(struct dw_pcie_ep *ep)
356 struct dw_pcie *dw = to_dw_pcie_from_ep(ep);
357 struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
358 int ret;
360 ret = rcar_gen4_pcie_common_init(rcar);
361 if (ret)
362 return;
364 writel(PCIEDMAINTSTSEN_INIT, rcar->base + PCIEDMAINTSTSEN);
367 static void rcar_gen4_pcie_ep_init(struct dw_pcie_ep *ep)
369 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
370 enum pci_barno bar;
372 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
373 dw_pcie_ep_reset_bar(pci, bar);
376 static void rcar_gen4_pcie_ep_deinit(struct rcar_gen4_pcie *rcar)
378 writel(0, rcar->base + PCIEDMAINTSTSEN);
379 rcar_gen4_pcie_common_deinit(rcar);
382 static int rcar_gen4_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
383 unsigned int type, u16 interrupt_num)
385 struct dw_pcie *dw = to_dw_pcie_from_ep(ep);
387 switch (type) {
388 case PCI_IRQ_INTX:
389 return dw_pcie_ep_raise_intx_irq(ep, func_no);
390 case PCI_IRQ_MSI:
391 return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
392 default:
393 dev_err(dw->dev, "Unknown IRQ type\n");
394 return -EINVAL;
397 return 0;
400 static const struct pci_epc_features rcar_gen4_pcie_epc_features = {
401 .linkup_notifier = false,
402 .msi_capable = true,
403 .msix_capable = false,
404 .bar[BAR_1] = { .type = BAR_RESERVED, },
405 .bar[BAR_3] = { .type = BAR_RESERVED, },
406 .bar[BAR_5] = { .type = BAR_RESERVED, },
407 .align = SZ_1M,
410 static const struct pci_epc_features*
411 rcar_gen4_pcie_ep_get_features(struct dw_pcie_ep *ep)
413 return &rcar_gen4_pcie_epc_features;
416 static unsigned int rcar_gen4_pcie_ep_get_dbi_offset(struct dw_pcie_ep *ep,
417 u8 func_no)
419 return func_no * RCAR_GEN4_PCIE_EP_FUNC_DBI_OFFSET;
422 static unsigned int rcar_gen4_pcie_ep_get_dbi2_offset(struct dw_pcie_ep *ep,
423 u8 func_no)
425 return func_no * RCAR_GEN4_PCIE_EP_FUNC_DBI2_OFFSET;
428 static const struct dw_pcie_ep_ops pcie_ep_ops = {
429 .pre_init = rcar_gen4_pcie_ep_pre_init,
430 .init = rcar_gen4_pcie_ep_init,
431 .raise_irq = rcar_gen4_pcie_ep_raise_irq,
432 .get_features = rcar_gen4_pcie_ep_get_features,
433 .get_dbi_offset = rcar_gen4_pcie_ep_get_dbi_offset,
434 .get_dbi2_offset = rcar_gen4_pcie_ep_get_dbi2_offset,
437 static int rcar_gen4_add_dw_pcie_ep(struct rcar_gen4_pcie *rcar)
439 struct dw_pcie_ep *ep = &rcar->dw.ep;
440 struct device *dev = rcar->dw.dev;
441 int ret;
443 if (!IS_ENABLED(CONFIG_PCIE_RCAR_GEN4_EP))
444 return -ENODEV;
446 ep->ops = &pcie_ep_ops;
448 ret = dw_pcie_ep_init(ep);
449 if (ret) {
450 rcar_gen4_pcie_ep_deinit(rcar);
451 return ret;
454 ret = dw_pcie_ep_init_registers(ep);
455 if (ret) {
456 dev_err(dev, "Failed to initialize DWC endpoint registers\n");
457 dw_pcie_ep_deinit(ep);
458 rcar_gen4_pcie_ep_deinit(rcar);
461 pci_epc_init_notify(ep->epc);
463 return ret;
466 static void rcar_gen4_remove_dw_pcie_ep(struct rcar_gen4_pcie *rcar)
468 dw_pcie_ep_deinit(&rcar->dw.ep);
469 rcar_gen4_pcie_ep_deinit(rcar);
472 /* Common */
473 static int rcar_gen4_add_dw_pcie(struct rcar_gen4_pcie *rcar)
475 rcar->drvdata = of_device_get_match_data(&rcar->pdev->dev);
476 if (!rcar->drvdata)
477 return -EINVAL;
479 switch (rcar->drvdata->mode) {
480 case DW_PCIE_RC_TYPE:
481 return rcar_gen4_add_dw_pcie_rp(rcar);
482 case DW_PCIE_EP_TYPE:
483 return rcar_gen4_add_dw_pcie_ep(rcar);
484 default:
485 return -EINVAL;
489 static int rcar_gen4_pcie_probe(struct platform_device *pdev)
491 struct rcar_gen4_pcie *rcar;
492 int err;
494 rcar = rcar_gen4_pcie_alloc(pdev);
495 if (IS_ERR(rcar))
496 return PTR_ERR(rcar);
498 err = rcar_gen4_pcie_get_resources(rcar);
499 if (err)
500 return err;
502 err = rcar_gen4_pcie_prepare(rcar);
503 if (err)
504 return err;
506 err = rcar_gen4_add_dw_pcie(rcar);
507 if (err)
508 goto err_unprepare;
510 return 0;
512 err_unprepare:
513 rcar_gen4_pcie_unprepare(rcar);
515 return err;
518 static void rcar_gen4_remove_dw_pcie(struct rcar_gen4_pcie *rcar)
520 switch (rcar->drvdata->mode) {
521 case DW_PCIE_RC_TYPE:
522 rcar_gen4_remove_dw_pcie_rp(rcar);
523 break;
524 case DW_PCIE_EP_TYPE:
525 rcar_gen4_remove_dw_pcie_ep(rcar);
526 break;
527 default:
528 break;
532 static void rcar_gen4_pcie_remove(struct platform_device *pdev)
534 struct rcar_gen4_pcie *rcar = platform_get_drvdata(pdev);
536 rcar_gen4_remove_dw_pcie(rcar);
537 rcar_gen4_pcie_unprepare(rcar);
540 static int r8a779f0_pcie_ltssm_control(struct rcar_gen4_pcie *rcar, bool enable)
542 u32 val;
544 val = readl(rcar->base + PCIERSTCTRL1);
545 if (enable) {
546 val |= APP_LTSSM_ENABLE;
547 val &= ~APP_HOLD_PHY_RST;
548 } else {
550 * Since the datasheet of R-Car doesn't mention how to assert
551 * the APP_HOLD_PHY_RST, don't assert it again. Otherwise,
552 * hang-up issue happened in the dw_edma_core_off() when
553 * the controller didn't detect a PCI device.
555 val &= ~APP_LTSSM_ENABLE;
557 writel(val, rcar->base + PCIERSTCTRL1);
559 return 0;
562 static void rcar_gen4_pcie_additional_common_init(struct rcar_gen4_pcie *rcar)
564 struct dw_pcie *dw = &rcar->dw;
565 u32 val;
567 val = dw_pcie_readl_dbi(dw, PCIE_PORT_LANE_SKEW);
568 val &= ~PORT_LANE_SKEW_INSERT_MASK;
569 if (dw->num_lanes < 4)
570 val |= BIT(6);
571 dw_pcie_writel_dbi(dw, PCIE_PORT_LANE_SKEW, val);
573 val = readl(rcar->base + PCIEPWRMNGCTRL);
574 val |= APP_CLK_REQ_N | APP_CLK_PM_EN;
575 writel(val, rcar->base + PCIEPWRMNGCTRL);
578 static void rcar_gen4_pcie_phy_reg_update_bits(struct rcar_gen4_pcie *rcar,
579 u32 offset, u32 mask, u32 val)
581 u32 tmp;
583 tmp = readl(rcar->phy_base + offset);
584 tmp &= ~mask;
585 tmp |= val;
586 writel(tmp, rcar->phy_base + offset);
590 * SoC datasheet suggests checking port logic register bits during firmware
591 * write. If read returns non-zero value, then this function returns -EAGAIN
592 * indicating that the write needs to be done again. If read returns zero,
593 * then return 0 to indicate success.
595 static int rcar_gen4_pcie_reg_test_bit(struct rcar_gen4_pcie *rcar,
596 u32 offset, u32 mask)
598 struct dw_pcie *dw = &rcar->dw;
600 if (dw_pcie_readl_dbi(dw, offset) & mask)
601 return -EAGAIN;
603 return 0;
606 static int rcar_gen4_pcie_download_phy_firmware(struct rcar_gen4_pcie *rcar)
608 /* The check_addr values are magical numbers in the datasheet */
609 static const u32 check_addr[] = {
610 0x00101018,
611 0x00101118,
612 0x00101021,
613 0x00101121,
615 struct dw_pcie *dw = &rcar->dw;
616 const struct firmware *fw;
617 unsigned int i, timeout;
618 u32 data;
619 int ret;
621 ret = request_firmware(&fw, RCAR_GEN4_PCIE_FIRMWARE_NAME, dw->dev);
622 if (ret) {
623 dev_err(dw->dev, "Failed to load firmware (%s): %d\n",
624 RCAR_GEN4_PCIE_FIRMWARE_NAME, ret);
625 return ret;
628 for (i = 0; i < (fw->size / 2); i++) {
629 data = fw->data[(i * 2) + 1] << 8 | fw->data[i * 2];
630 timeout = 100;
631 do {
632 dw_pcie_writel_dbi(dw, PRTLGC89, RCAR_GEN4_PCIE_FIRMWARE_BASE_ADDR + i);
633 dw_pcie_writel_dbi(dw, PRTLGC90, data);
634 if (!rcar_gen4_pcie_reg_test_bit(rcar, PRTLGC89, BIT(30)))
635 break;
636 if (!(--timeout)) {
637 ret = -ETIMEDOUT;
638 goto exit;
640 usleep_range(100, 200);
641 } while (1);
644 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x0f8, BIT(17), BIT(17));
646 for (i = 0; i < ARRAY_SIZE(check_addr); i++) {
647 timeout = 100;
648 do {
649 dw_pcie_writel_dbi(dw, PRTLGC89, check_addr[i]);
650 ret = rcar_gen4_pcie_reg_test_bit(rcar, PRTLGC89, BIT(30));
651 ret |= rcar_gen4_pcie_reg_test_bit(rcar, PRTLGC90, BIT(0));
652 if (!ret)
653 break;
654 if (!(--timeout)) {
655 ret = -ETIMEDOUT;
656 goto exit;
658 usleep_range(100, 200);
659 } while (1);
662 exit:
663 release_firmware(fw);
665 return ret;
668 static int rcar_gen4_pcie_ltssm_control(struct rcar_gen4_pcie *rcar, bool enable)
670 struct dw_pcie *dw = &rcar->dw;
671 u32 val;
672 int ret;
674 if (!enable) {
675 val = readl(rcar->base + PCIERSTCTRL1);
676 val &= ~APP_LTSSM_ENABLE;
677 writel(val, rcar->base + PCIERSTCTRL1);
679 return 0;
682 val = dw_pcie_readl_dbi(dw, PCIE_PORT_FORCE);
683 val |= PORT_FORCE_DO_DESKEW_FOR_SRIS;
684 dw_pcie_writel_dbi(dw, PCIE_PORT_FORCE, val);
686 val = readl(rcar->base + PCIEMSR0);
687 val |= APP_SRIS_MODE;
688 writel(val, rcar->base + PCIEMSR0);
691 * The R-Car Gen4 datasheet doesn't describe the PHY registers' name.
692 * But, the initialization procedure describes these offsets. So,
693 * this driver has magical offset numbers.
695 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x700, BIT(28), 0);
696 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x700, BIT(20), 0);
697 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x700, BIT(12), 0);
698 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x700, BIT(4), 0);
700 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x148, GENMASK(23, 22), BIT(22));
701 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x148, GENMASK(18, 16), GENMASK(17, 16));
702 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x148, GENMASK(7, 6), BIT(6));
703 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x148, GENMASK(2, 0), GENMASK(11, 0));
704 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x1d4, GENMASK(16, 15), GENMASK(16, 15));
705 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x514, BIT(26), BIT(26));
706 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x0f8, BIT(16), 0);
707 rcar_gen4_pcie_phy_reg_update_bits(rcar, 0x0f8, BIT(19), BIT(19));
709 val = readl(rcar->base + PCIERSTCTRL1);
710 val &= ~APP_HOLD_PHY_RST;
711 writel(val, rcar->base + PCIERSTCTRL1);
713 ret = readl_poll_timeout(rcar->phy_base + 0x0f8, val, !(val & BIT(18)), 100, 10000);
714 if (ret < 0)
715 return ret;
717 ret = rcar_gen4_pcie_download_phy_firmware(rcar);
718 if (ret)
719 return ret;
721 val = readl(rcar->base + PCIERSTCTRL1);
722 val |= APP_LTSSM_ENABLE;
723 writel(val, rcar->base + PCIERSTCTRL1);
725 return 0;
728 static struct rcar_gen4_pcie_drvdata drvdata_r8a779f0_pcie = {
729 .ltssm_control = r8a779f0_pcie_ltssm_control,
730 .mode = DW_PCIE_RC_TYPE,
733 static struct rcar_gen4_pcie_drvdata drvdata_r8a779f0_pcie_ep = {
734 .ltssm_control = r8a779f0_pcie_ltssm_control,
735 .mode = DW_PCIE_EP_TYPE,
738 static struct rcar_gen4_pcie_drvdata drvdata_rcar_gen4_pcie = {
739 .additional_common_init = rcar_gen4_pcie_additional_common_init,
740 .ltssm_control = rcar_gen4_pcie_ltssm_control,
741 .mode = DW_PCIE_RC_TYPE,
744 static struct rcar_gen4_pcie_drvdata drvdata_rcar_gen4_pcie_ep = {
745 .additional_common_init = rcar_gen4_pcie_additional_common_init,
746 .ltssm_control = rcar_gen4_pcie_ltssm_control,
747 .mode = DW_PCIE_EP_TYPE,
750 static const struct of_device_id rcar_gen4_pcie_of_match[] = {
752 .compatible = "renesas,r8a779f0-pcie",
753 .data = &drvdata_r8a779f0_pcie,
756 .compatible = "renesas,r8a779f0-pcie-ep",
757 .data = &drvdata_r8a779f0_pcie_ep,
760 .compatible = "renesas,rcar-gen4-pcie",
761 .data = &drvdata_rcar_gen4_pcie,
764 .compatible = "renesas,rcar-gen4-pcie-ep",
765 .data = &drvdata_rcar_gen4_pcie_ep,
769 MODULE_DEVICE_TABLE(of, rcar_gen4_pcie_of_match);
771 static struct platform_driver rcar_gen4_pcie_driver = {
772 .driver = {
773 .name = "pcie-rcar-gen4",
774 .of_match_table = rcar_gen4_pcie_of_match,
775 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
777 .probe = rcar_gen4_pcie_probe,
778 .remove = rcar_gen4_pcie_remove,
780 module_platform_driver(rcar_gen4_pcie_driver);
782 MODULE_DESCRIPTION("Renesas R-Car Gen4 PCIe controller driver");
783 MODULE_LICENSE("GPL");