diff mbox series

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

Message ID 20210202170121.29777-1-Frank.Li@nxp.com
State Superseded
Headers show
Series [1/1] usb: cdns3: add power lost support for system resume | expand

Commit Message

Frank Li Feb. 2, 2021, 5:01 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>
---
 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

Pawel Laszczak Feb. 3, 2021, 4:57 a.m. UTC | #1
Hi Frank,

>
>
>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>
>---
> 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(-)
>
>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..8b04e030df4d 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)
>+{
>+	int ret = false;

You don't need this variable. 

>+
>+	if (cdns->version == CDNS3_CONTROLLER_V1) {
>+		if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
>+			ret = true;


return true;

>+	} else {
>+		if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
>+			ret = true;

retrun true;

>+	}
>+	return ret;

return false;

Regards,
Pawel Laszczak

>+}
>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
Peter Chen Feb. 6, 2021, 7:48 a.m. UTC | #2
On 21-02-03 12:19:05, kernel test robot wrote:
> Hi Frank,

> 

> I love your patch! Yet something to improve:

> 

> [auto build test ERROR on peter.chen-usb/for-usb-next]

> [also build test ERROR on next-20210125]

> [cannot apply to v5.11-rc6]

> [If your patch is applied to the wrong git tree, kindly drop us a note.

> And when submitting patch, we suggest to use '--base' as documented in

> https://git-scm.com/docs/git-format-patch]

> 

> url:    https://github.com/0day-ci/linux/commits/Frank-Li/usb-cdns3-add-power-lost-support-for-system-resume/20210203-011535

> base:   https://git.kernel.org/pub/scm/linux/kernel/git/peter.chen/usb.git for-usb-next

> config: riscv-allmodconfig (attached as .config)

> compiler: riscv64-linux-gcc (GCC) 9.3.0

> reproduce (this is a W=1 build):

>         wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross

>         chmod +x ~/bin/make.cross

>         # https://github.com/0day-ci/linux/commit/a41cbdf2c0138a8c317d84fa967c0ebcc5396dd3

>         git remote add linux-review https://github.com/0day-ci/linux

>         git fetch --no-tags linux-review Frank-Li/usb-cdns3-add-power-lost-support-for-system-resume/20210203-011535

>         git checkout a41cbdf2c0138a8c317d84fa967c0ebcc5396dd3

>         # save the attached .config to linux build tree

>         COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=riscv 

> 

> If you fix the issue, kindly add following tag as appropriate

> Reported-by: kernel test robot <lkp@intel.com>

> 

> All errors (new ones prefixed by >>, old ones prefixed by <<):

> 

> >> ERROR: modpost: "cdns_power_is_lost" [drivers/usb/cdns3/cdns3.ko] undefined!

> 


Frank, would you please fix this build error as well as Pawel's
comments, then, send again.

-- 

Thanks,
Peter Chen
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..8b04e030df4d 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)
+{
+	int ret = false;
+
+	if (cdns->version == CDNS3_CONTROLLER_V1) {
+		if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
+			ret = true;
+	} else {
+		if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
+			ret = true;
+	}
+	return ret;
+}
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 */