diff mbox series

[v3,1/1] usb: cdns3: add power lost support for system resume

Message ID 20210208174820.31182-1-Frank.Li@nxp.com (mailing list archive)
State New, archived
Headers show
Series [v3,1/1] usb: cdns3: add power lost support for system resume | expand

Commit Message

Frank Li Feb. 8, 2021, 5:48 p.m. UTC
If the controller lost its power during the system suspend, we need
to do all initialiation operations.

Signed-off-by: Peter Chen <peter.chen@nxp.com>
Signed-off-by: Frank Li <Frank.Li@nxp.com>
---

Change from v1 to v3. 
 - fix build error
 - remove ret variable at cdns_is_power_lost

 drivers/usb/cdns3/cdns3-gadget.c |  2 ++
 drivers/usb/cdns3/cdns3-imx.c    | 34 ++++++++++++++++++++++++++++++++
 drivers/usb/cdns3/cdns3-plat.c   | 22 ++++++++++++++++++++-
 drivers/usb/cdns3/core.c         | 29 ++++++++++++++++++++++++++-
 drivers/usb/cdns3/drd.c          | 16 +++++++++++++++
 drivers/usb/cdns3/drd.h          |  2 +-
 6 files changed, 102 insertions(+), 3 deletions(-)

Comments

Peter Chen Feb. 17, 2021, 10:39 a.m. UTC | #1
On 21-02-08 11:48:20, Frank Li wrote:
> If the controller lost its power during the system suspend, we need
> to do all initialiation operations.
> 
> Signed-off-by: Peter Chen <peter.chen@nxp.com>
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
> 
> Change from v1 to v3. 
>  - fix build error
>  - remove ret variable at cdns_is_power_lost
> 
>  drivers/usb/cdns3/cdns3-gadget.c |  2 ++
>  drivers/usb/cdns3/cdns3-imx.c    | 34 ++++++++++++++++++++++++++++++++

Please split imx part from this patch, and as a separate patch.
Pawel, please check if it will affect cdns3 ssp part, if not, I will
queue it.

>  drivers/usb/cdns3/cdns3-plat.c   | 22 ++++++++++++++++++++-
>  drivers/usb/cdns3/core.c         | 29 ++++++++++++++++++++++++++-
>  drivers/usb/cdns3/drd.c          | 16 +++++++++++++++
>  drivers/usb/cdns3/drd.h          |  2 +-
>  6 files changed, 102 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> index 582bfeceedb4..44b7301b1888 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.c
> +++ b/drivers/usb/cdns3/cdns3-gadget.c
> @@ -3304,6 +3304,8 @@ static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
>  		return 0;
>  
>  	cdns3_gadget_config(priv_dev);
> +	if (hibernated)
> +		writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
>  
>  	return 0;
>  }
> diff --git a/drivers/usb/cdns3/cdns3-imx.c b/drivers/usb/cdns3/cdns3-imx.c
> index 8f88eec0b0ea..708b51cc5844 100644
> --- a/drivers/usb/cdns3/cdns3-imx.c
> +++ b/drivers/usb/cdns3/cdns3-imx.c
> @@ -361,6 +361,39 @@ static int cdns_imx_suspend(struct device *dev)
>  
>  	return 0;
>  }
> +
> +
> +/* Indicate if the controller was power lost before */
> +static inline bool cdns_imx_is_power_lost(struct cdns_imx *data)
> +{
> +	u32 value;
> +
> +	value = cdns_imx_readl(data, USB3_CORE_CTRL1);
> +	if ((value & SW_RESET_MASK) == ALL_SW_RESET)
> +		return true;
> +	else
> +		return false;
> +}
> +
> +static int cdns_imx_system_resume(struct device *dev)
> +{
> +	struct cdns_imx *data = dev_get_drvdata(dev);
> +	int ret;
> +
> +	ret = cdns_imx_resume(dev);
> +	if (ret)
> +		return ret;
> +
> +	if (cdns_imx_is_power_lost(data)) {
> +		dev_dbg(dev, "resume from power lost\n");
> +		ret = cdns_imx_noncore_init(data);
> +		if (ret)
> +			cdns_imx_suspend(dev);
> +	}
> +
> +	return ret;
> +}
> +
>  #else
>  static int cdns_imx_platform_suspend(struct device *dev,
>  	bool suspend, bool wakeup)
> @@ -372,6 +405,7 @@ static int cdns_imx_platform_suspend(struct device *dev,
>  
>  static const struct dev_pm_ops cdns_imx_pm_ops = {
>  	SET_RUNTIME_PM_OPS(cdns_imx_suspend, cdns_imx_resume, NULL)
> +	SET_SYSTEM_SLEEP_PM_OPS(cdns_imx_suspend, cdns_imx_system_resume)
>  };
>  
>  static const struct of_device_id cdns_imx_of_match[] = {
> diff --git a/drivers/usb/cdns3/cdns3-plat.c b/drivers/usb/cdns3/cdns3-plat.c
> index 4b18e1c6a4bb..e61da57fe31a 100644
> --- a/drivers/usb/cdns3/cdns3-plat.c
> +++ b/drivers/usb/cdns3/cdns3-plat.c
> @@ -19,6 +19,7 @@
>  
>  #include "core.h"
>  #include "gadget-export.h"
> +#include "drd.h"
>  
>  static int set_phy_power_on(struct cdns *cdns)
>  {
> @@ -236,6 +237,17 @@ static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
>  	if (!cdns->in_lpm)
>  		return 0;
>  
> +	if (cdns_power_is_lost(cdns)) {
> +		phy_exit(cdns->usb2_phy);
> +		ret = phy_init(cdns->usb2_phy);
> +		if (ret)
> +			return ret;

Add one blank line

Peter

> +		phy_exit(cdns->usb3_phy);
> +		ret = phy_init(cdns->usb3_phy);
> +		if (ret)
> +			return ret;
> +	}
> +
>  	ret = set_phy_power_on(cdns);
>  	if (ret)
>  		return ret;
> @@ -270,10 +282,18 @@ static int cdns3_plat_runtime_resume(struct device *dev)
>  static int cdns3_plat_suspend(struct device *dev)
>  {
>  	struct cdns *cdns = dev_get_drvdata(dev);
> +	int ret;
>  
>  	cdns_suspend(cdns);
>  
> -	return cdns3_controller_suspend(dev, PMSG_SUSPEND);
> +	ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
> +	if (ret)
> +		return ret;
> +
> +	if (device_may_wakeup(dev) && cdns->wakeup_irq)
> +		enable_irq_wake(cdns->wakeup_irq);
> +
> +	return ret;
>  }
>  
>  static int cdns3_plat_resume(struct device *dev)
> diff --git a/drivers/usb/cdns3/core.c b/drivers/usb/cdns3/core.c
> index 199713769289..5d486c8a9d99 100644
> --- a/drivers/usb/cdns3/core.c
> +++ b/drivers/usb/cdns3/core.c
> @@ -525,9 +525,36 @@ EXPORT_SYMBOL_GPL(cdns_suspend);
>  int cdns_resume(struct cdns *cdns, u8 set_active)
>  {
>  	struct device *dev = cdns->dev;
> +	enum usb_role real_role;
> +	bool role_changed = false;
> +	int ret;
> +
> +	if (cdns_power_is_lost(cdns)) {
> +		if (cdns->role_sw) {
> +			cdns->role = cdns_role_get(cdns->role_sw);
> +		} else {
> +			real_role = cdns_hw_role_state_machine(cdns);
> +			if (real_role != cdns->role) {
> +				ret = cdns_hw_role_switch(cdns);
> +				if (ret)
> +					return ret;
> +				role_changed = true;
> +			}
> +		}
> +
> +		if (!role_changed) {
> +			if (cdns->role == USB_ROLE_HOST)
> +				ret = cdns_drd_host_on(cdns);
> +			else if (cdns->role == USB_ROLE_DEVICE)
> +				ret = cdns_drd_gadget_on(cdns);
> +
> +			if (ret)
> +				return ret;
> +		}
> +	}
>  
>  	if (cdns->roles[cdns->role]->resume)
> -		cdns->roles[cdns->role]->resume(cdns, false);
> +		cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
>  
>  	if (set_active) {
>  		pm_runtime_disable(dev);
> diff --git a/drivers/usb/cdns3/drd.c b/drivers/usb/cdns3/drd.c
> index 605a413db727..1213c540deb3 100644
> --- a/drivers/usb/cdns3/drd.c
> +++ b/drivers/usb/cdns3/drd.c
> @@ -478,3 +478,19 @@ int cdns_drd_exit(struct cdns *cdns)
>  
>  	return 0;
>  }
> +
> +
> +/* Indicate the cdns3 core was power lost before */
> +bool cdns_power_is_lost(struct cdns *cdns)
> +{
> +	if (cdns->version == CDNS3_CONTROLLER_V1) {
> +		if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
> +			return true;
> +	} else {
> +		if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
> +			return true;
> +	}
> +	return false;
> +}
> +
> +EXPORT_SYMBOL_GPL(cdns_power_is_lost);
> diff --git a/drivers/usb/cdns3/drd.h b/drivers/usb/cdns3/drd.h
> index 9724acdecbbb..cbdf94f73ed9 100644
> --- a/drivers/usb/cdns3/drd.h
> +++ b/drivers/usb/cdns3/drd.h
> @@ -215,5 +215,5 @@ int cdns_drd_gadget_on(struct cdns *cdns);
>  void cdns_drd_gadget_off(struct cdns *cdns);
>  int cdns_drd_host_on(struct cdns *cdns);
>  void cdns_drd_host_off(struct cdns *cdns);
> -
> +bool cdns_power_is_lost(struct cdns *cdns);
>  #endif /* __LINUX_CDNS3_DRD */
> -- 
> 2.24.0.rc1
>
diff mbox series

Patch

diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
index 582bfeceedb4..44b7301b1888 100644
--- a/drivers/usb/cdns3/cdns3-gadget.c
+++ b/drivers/usb/cdns3/cdns3-gadget.c
@@ -3304,6 +3304,8 @@  static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
 		return 0;
 
 	cdns3_gadget_config(priv_dev);
+	if (hibernated)
+		writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
 
 	return 0;
 }
diff --git a/drivers/usb/cdns3/cdns3-imx.c b/drivers/usb/cdns3/cdns3-imx.c
index 8f88eec0b0ea..708b51cc5844 100644
--- a/drivers/usb/cdns3/cdns3-imx.c
+++ b/drivers/usb/cdns3/cdns3-imx.c
@@ -361,6 +361,39 @@  static int cdns_imx_suspend(struct device *dev)
 
 	return 0;
 }
+
+
+/* Indicate if the controller was power lost before */
+static inline bool cdns_imx_is_power_lost(struct cdns_imx *data)
+{
+	u32 value;
+
+	value = cdns_imx_readl(data, USB3_CORE_CTRL1);
+	if ((value & SW_RESET_MASK) == ALL_SW_RESET)
+		return true;
+	else
+		return false;
+}
+
+static int cdns_imx_system_resume(struct device *dev)
+{
+	struct cdns_imx *data = dev_get_drvdata(dev);
+	int ret;
+
+	ret = cdns_imx_resume(dev);
+	if (ret)
+		return ret;
+
+	if (cdns_imx_is_power_lost(data)) {
+		dev_dbg(dev, "resume from power lost\n");
+		ret = cdns_imx_noncore_init(data);
+		if (ret)
+			cdns_imx_suspend(dev);
+	}
+
+	return ret;
+}
+
 #else
 static int cdns_imx_platform_suspend(struct device *dev,
 	bool suspend, bool wakeup)
@@ -372,6 +405,7 @@  static int cdns_imx_platform_suspend(struct device *dev,
 
 static const struct dev_pm_ops cdns_imx_pm_ops = {
 	SET_RUNTIME_PM_OPS(cdns_imx_suspend, cdns_imx_resume, NULL)
+	SET_SYSTEM_SLEEP_PM_OPS(cdns_imx_suspend, cdns_imx_system_resume)
 };
 
 static const struct of_device_id cdns_imx_of_match[] = {
diff --git a/drivers/usb/cdns3/cdns3-plat.c b/drivers/usb/cdns3/cdns3-plat.c
index 4b18e1c6a4bb..e61da57fe31a 100644
--- a/drivers/usb/cdns3/cdns3-plat.c
+++ b/drivers/usb/cdns3/cdns3-plat.c
@@ -19,6 +19,7 @@ 
 
 #include "core.h"
 #include "gadget-export.h"
+#include "drd.h"
 
 static int set_phy_power_on(struct cdns *cdns)
 {
@@ -236,6 +237,17 @@  static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
 	if (!cdns->in_lpm)
 		return 0;
 
+	if (cdns_power_is_lost(cdns)) {
+		phy_exit(cdns->usb2_phy);
+		ret = phy_init(cdns->usb2_phy);
+		if (ret)
+			return ret;
+		phy_exit(cdns->usb3_phy);
+		ret = phy_init(cdns->usb3_phy);
+		if (ret)
+			return ret;
+	}
+
 	ret = set_phy_power_on(cdns);
 	if (ret)
 		return ret;
@@ -270,10 +282,18 @@  static int cdns3_plat_runtime_resume(struct device *dev)
 static int cdns3_plat_suspend(struct device *dev)
 {
 	struct cdns *cdns = dev_get_drvdata(dev);
+	int ret;
 
 	cdns_suspend(cdns);
 
-	return cdns3_controller_suspend(dev, PMSG_SUSPEND);
+	ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
+	if (ret)
+		return ret;
+
+	if (device_may_wakeup(dev) && cdns->wakeup_irq)
+		enable_irq_wake(cdns->wakeup_irq);
+
+	return ret;
 }
 
 static int cdns3_plat_resume(struct device *dev)
diff --git a/drivers/usb/cdns3/core.c b/drivers/usb/cdns3/core.c
index 199713769289..5d486c8a9d99 100644
--- a/drivers/usb/cdns3/core.c
+++ b/drivers/usb/cdns3/core.c
@@ -525,9 +525,36 @@  EXPORT_SYMBOL_GPL(cdns_suspend);
 int cdns_resume(struct cdns *cdns, u8 set_active)
 {
 	struct device *dev = cdns->dev;
+	enum usb_role real_role;
+	bool role_changed = false;
+	int ret;
+
+	if (cdns_power_is_lost(cdns)) {
+		if (cdns->role_sw) {
+			cdns->role = cdns_role_get(cdns->role_sw);
+		} else {
+			real_role = cdns_hw_role_state_machine(cdns);
+			if (real_role != cdns->role) {
+				ret = cdns_hw_role_switch(cdns);
+				if (ret)
+					return ret;
+				role_changed = true;
+			}
+		}
+
+		if (!role_changed) {
+			if (cdns->role == USB_ROLE_HOST)
+				ret = cdns_drd_host_on(cdns);
+			else if (cdns->role == USB_ROLE_DEVICE)
+				ret = cdns_drd_gadget_on(cdns);
+
+			if (ret)
+				return ret;
+		}
+	}
 
 	if (cdns->roles[cdns->role]->resume)
-		cdns->roles[cdns->role]->resume(cdns, false);
+		cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
 
 	if (set_active) {
 		pm_runtime_disable(dev);
diff --git a/drivers/usb/cdns3/drd.c b/drivers/usb/cdns3/drd.c
index 605a413db727..1213c540deb3 100644
--- a/drivers/usb/cdns3/drd.c
+++ b/drivers/usb/cdns3/drd.c
@@ -478,3 +478,19 @@  int cdns_drd_exit(struct cdns *cdns)
 
 	return 0;
 }
+
+
+/* Indicate the cdns3 core was power lost before */
+bool cdns_power_is_lost(struct cdns *cdns)
+{
+	if (cdns->version == CDNS3_CONTROLLER_V1) {
+		if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
+			return true;
+	} else {
+		if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
+			return true;
+	}
+	return false;
+}
+
+EXPORT_SYMBOL_GPL(cdns_power_is_lost);
diff --git a/drivers/usb/cdns3/drd.h b/drivers/usb/cdns3/drd.h
index 9724acdecbbb..cbdf94f73ed9 100644
--- a/drivers/usb/cdns3/drd.h
+++ b/drivers/usb/cdns3/drd.h
@@ -215,5 +215,5 @@  int cdns_drd_gadget_on(struct cdns *cdns);
 void cdns_drd_gadget_off(struct cdns *cdns);
 int cdns_drd_host_on(struct cdns *cdns);
 void cdns_drd_host_off(struct cdns *cdns);
-
+bool cdns_power_is_lost(struct cdns *cdns);
 #endif /* __LINUX_CDNS3_DRD */