Re: [PATCH 2/2] PCI: imx: Initial imx7d pm support

From: Lucas Stach
Date: Fri Jun 08 2018 - 10:33:59 EST


Am Dienstag, den 29.05.2018, 22:39 +0300 schrieb Leonard Crestez:
> On imx7d the phy is turned off in suspend and must be reset on resume.
> Right now lspci -v fails after a suspend/resume cycle, fix this by
> adding minimal suspend/resume code from the nxp vendor tree.
>
> This is currently only enabled for imx7 but the same sequence can be
> applied to other imx pcie variants.
>
> Tested on imx7d-sabresd with an Intel 5622ANHMW wireless pcie adapter.
>
> > The original author is mostly Richard Zhu <hongxing.zhu@xxxxxxx>, this
> patch adjusts the code to the upstream imx7d implemention using reset
> controls and power domains.
>
> > Signed-off-by: Leonard Crestez <leonard.crestez@xxxxxxx>
> ---
> Âdrivers/pci/dwc/pci-imx6.c | 94 ++++++++++++++++++++++++++++++++++++--
> Â1 file changed, 89 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/pci/dwc/pci-imx6.c b/drivers/pci/dwc/pci-imx6.c
> index 4818ef875f8a..ff6077eeb387 100644
> --- a/drivers/pci/dwc/pci-imx6.c
> +++ b/drivers/pci/dwc/pci-imx6.c
> @@ -540,10 +540,27 @@ static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
> Â
> > Â dev_err(dev, "Speed change timeout\n");
> > Â return -EINVAL;
> Â}
> Â
> +static void imx6_pcie_ltssm_enable(struct device *dev)
> +{
> > + struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
> +
> > + switch (imx6_pcie->variant) {
> > + case IMX6Q:
> > + case IMX6SX:
> > + case IMX6QP:
> > + regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
> > + ÂÂÂIMX6Q_GPR12_PCIE_CTL_2,
> > + ÂÂÂIMX6Q_GPR12_PCIE_CTL_2);
> > + break;
> > + case IMX7D:
> > + reset_control_deassert(imx6_pcie->apps_reset);
> > + }
> +}
> +
> Âstatic int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
> Â{
> > Â struct dw_pcie *pci = imx6_pcie->pci;
> > Â struct device *dev = pci->dev;
> > Â u32 tmp;
> @@ -558,15 +575,11 @@ static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
> > Â tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
> > Â tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
> > Â dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
> Â
> > Â /* Start LTSSM. */
> > - if (imx6_pcie->variant == IMX7D)
> > - reset_control_deassert(imx6_pcie->apps_reset);
> > - else
> > - regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
> > - ÂÂÂIMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
> > + imx6_pcie_ltssm_enable(dev);
> Â
> > Â ret = imx6_pcie_wait_for_link(imx6_pcie);
> > Â if (ret)
> > Â goto err_reset_phy;
> Â
> @@ -681,10 +694,80 @@ static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
> Â
> Âstatic const struct dw_pcie_ops dw_pcie_ops = {
> > Â .link_up = imx6_pcie_link_up,
> Â};
> Â
> +#ifdef CONFIG_PM_SLEEP
> +static int imx6_pcie_suspend_noirq(struct device *dev)
> +{
> > + struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
> +
> + if (imx6_pcie->variant == IMX7D) {

Instead of this large indented block, please just have an early return
at the start of this function, like:

if (imx6_pcie->variant != IMX7D)
return 0;

Same for the resume function.

> + /* Disable clks */
> > + clk_disable_unprepare(imx6_pcie->pcie);
> > + clk_disable_unprepare(imx6_pcie->pcie_phy);
> > + clk_disable_unprepare(imx6_pcie->pcie_bus);
> > + /* turn off external osc input */
> > + regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
> > + ÂÂÂIMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
> > + ÂÂÂIMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
> > + }
> +
> > + return 0;
> +}
> +
> +static void imx6_pcie_ltssm_disable(struct device *dev)
> +{
> > + struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
> +
> > + switch (imx6_pcie->variant) {
> > + case IMX6Q:
> > + case IMX6SX:
> > + case IMX6QP:
> > + regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
> > + ÂÂÂIMX6Q_GPR12_PCIE_CTL_2, 0);
> > + break;
> > + case IMX7D:
> > + reset_control_assert(imx6_pcie->apps_reset);
> > + break;
> > + }
> +}
> +
> +static int imx6_pcie_resume_noirq(struct device *dev)
> +{
> > + int ret = 0;
> > + struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
> > + struct pcie_port *pp = &imx6_pcie->pci->pp;
> +
> > + if (imx6_pcie->variant == IMX7D) {
> > + imx6_pcie_ltssm_disable(dev);

The LTSSM disable seems misplaced here. I would have expected it to be
in the suspend function.

> + imx6_pcie_assert_core_reset(imx6_pcie);
> > + imx6_pcie_init_phy(imx6_pcie);
> > + imx6_pcie_deassert_core_reset(imx6_pcie);
> +
> > + /*
> > + Â* controller maybe turn off, re-configure again
> > + Â*/
> > + dw_pcie_setup_rc(pp);
> +
> > + imx6_pcie_ltssm_enable(dev);
> +
> > + ret = imx6_pcie_wait_for_link(imx6_pcie);
> > + if (ret < 0)
> + pr_info("pcie link is down after resume.\n");

If the PHY has been powered down and LTSSM stopped I guess we need to
do the full imx6_pcie_establish_link() dance again here to reliably re-
establish the link. The above seems unsafe.

> + }
> +
> > + return 0;
> +}
> +
> +static const struct dev_pm_ops imx6_pcie_pm_ops = {
> > + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
> > + ÂÂÂÂÂÂimx6_pcie_resume_noirq)
> +};
> +#endif

This structure must be outside of the ifdef, or you'll break the build
on !CONFIG_PM_SLEEP.

> Âstatic int imx6_pcie_probe(struct platform_device *pdev)
> Â{
> > Â struct device *dev = &pdev->dev;
> > Â struct dw_pcie *pci;
> > Â struct imx6_pcie *imx6_pcie;
> @@ -847,10 +930,11 @@ static const struct of_device_id imx6_pcie_of_match[] = {
> Âstatic struct platform_driver imx6_pcie_driver = {
> > Â .driver = {
> > > Â .name = "imx6q-pcie",
> > Â .of_match_table = imx6_pcie_of_match,
> > Â .suppress_bind_attrs = true,
> > + .pm = &imx6_pcie_pm_ops,
> > Â },
> > Â .probeÂÂÂÂ= imx6_pcie_probe,
> > Â .shutdown = imx6_pcie_shutdown,
> Â};
> Â

Regards,
Lucas