mbox series

[v16,0/5] USB DWC3 host wake up support from system suspend

Message ID 1652379802-8318-1-git-send-email-quic_kriskura@quicinc.com
Headers show
Series USB DWC3 host wake up support from system suspend | expand

Message

Krishna Kurapati May 12, 2022, 6:23 p.m. UTC
Avoiding phy powerdown in host mode when wakeup capable devices are 
connected, so that it can be wake up by devices.
Keep usb30_prim gdsc active to retain controller status
during suspend/resume.

Changes in v16:
Added changes to power down the phy's during suspend only if dwc3
is not wakeup capable.

Changes in v15:
Added patch to enable wakeup for xhci-plat based on children wakeup status.
Used device_wakeup_path instead of device_children_wakeup_capable

Changes in v14:
Added patch for device_children_wakeup_capable.
Used device_children_wakeup_capable instead of usb_wakeup_enabled_descendants.
Fixed minor nit picks in v13 reported by Matthias.

Changes in v13:
Moved the dt bindings patch to start.
Changed dwc3_set_phy_speed_mode to dwc3_check_phy_speed_mode.
Check wakep-source property for dwc3 core node to set the
wakeup capability. Drop the device_init_wakeup call from
runtime suspend and resume.
Added GENPD_FLAG_RPM_ALWAYS_ON and set GENPD_FLAG_ALWAYS_ON if
wakeup is supported.

Changes in v12:
Squashed PATCH 1/5 and 2/5 of v11.
Added dt bindings and device tree entry for wakeup-source property
for dwc3 core node.
Dropped redundant phy_set_mode call.


Changes in v11:
Moving back to v8 version
https://patchwork.kernel.org/project/linux-arm-msm/cover/1624882097-23265-1-git-send-email-sanm@codeaurora.org
as we are getting interrupts during suspend
when enabling both DP hs phy irq and DM hs phy irq.
Moved the set phy mode function to dwc3/core.c from xhci-plat.c
We didn't find any other option other than accessing xhci from dwc.

Changes in v10:
PATCH 1/6: Change device_set_wakeup_capable to device_set_wakeup_enable
PATCH 2/6: Remove redundant else part in dwc3_resume_common
PATCH 4/6: Change the irg flags
PATCH 5/6: Set flag GENPD_FLAG_ALWAYS_ON
PATCH 6/6: Remove disable interrupts function and enable
interrupts in probe.


Changes in v9:
Checking with device_may_makeup property instead of phy_power_off flag.
Changed the IRQ flags and removed hs_phy_mode variable.

Changes in v8:
Moved the dwc3 suspend quirk code in dwc3/host.c to xhci-plat.c
Checking phy_power_off flag instead of usb_wakeup_enabled_descendants 
to keep gdsc active.

Changes in v7:
Change in commit text and message in PATCH 1/5 and PATCH 5/5
as per Matthias suggestion.
Added curly braces for if and else if sections in PATCH 4/5.

Changes in v6:
Addressed comments in host.c and core.c
Separated the patches in dwc3-qcom.c to make it simple.
Dropped wakeup-source change as it is not related to this series.

Changes in v5:
Added phy_power_off flag to check presence of wakeup capable devices.
Dropped patch[v4,4/5] as it is present linux-next.
Addressed comments in host.c and dwc3-qcom.c.

Changes in v4:
Addressed Matthias comments raised in v3.

Changes in v3:
Removed need_phy_for_wakeup flag and by default avoiding phy powerdown.
Addressed Matthias comments and added entry for DEV_SUPERSPEED.
Added suspend_quirk in dwc3 host and moved the dwc3_set_phy_speed_flags.
Added wakeup-source dt entry and reading in dwc-qcom.c glue driver.

Changes in v2:
Dropped the patch in clock to set GENPD_FLAG_ACTIVE_WAKEUP flag and 
setting in usb dwc3 driver.
Separated the core patch and glue driver patch.
Made need_phy_for_wakeup flag part of dwc structure and 
hs_phy_flags as unsgined int.
Adrressed the comment on device_init_wakeup call.
Corrected offset for reading portsc register.
Added pacth to support wakeup in xo shutdown case.

Sandeep Maheswaram (5):
  dt-bindings: usb: dwc3: Add wakeup-source property support
  usb: dwc3: core: Host wake up support from system suspend
  usb: dwc3: qcom: Add helper functions to enable,disable wake irqs
  usb: dwc3: qcom: Configure wakeup interrupts during suspend
  usb: dwc3: qcom: Keep power domain on to retain controller status

 .../devicetree/bindings/usb/snps,dwc3.yaml         |  5 ++
 drivers/usb/dwc3/core.c                            | 30 +++----
 drivers/usb/dwc3/core.h                            |  4 +
 drivers/usb/dwc3/dwc3-qcom.c                       | 91 +++++++++++++---------
 drivers/usb/dwc3/host.c                            | 24 ++++++
 5 files changed, 104 insertions(+), 50 deletions(-)

Comments

Matthias Kaehlcke May 12, 2022, 10:40 p.m. UTC | #1
On Thu, May 12, 2022 at 11:53:19PM +0530, Krishna Kurapati wrote:
> From: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> 
> During suspend read the status of all port and set hs phy mode
> based on current speed. Use this hs phy mode to configure wakeup
> interrupts in qcom glue driver.
> 
> Check wakeup-source property for dwc3 core node to set the
> wakeup capability. Drop the device_init_wakeup call from
> runtime suspend and resume.
> 
> Also check during suspend if any wakeup capable devices are
> connected to the controller (directly or through hubs), if there
> are none set a flag to indicate that the PHY is powered
> down during suspend.
> 
> Signed-off-by: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
> ---
>  drivers/usb/dwc3/core.c | 30 +++++++++++++++++-------------
>  drivers/usb/dwc3/core.h |  4 ++++
>  drivers/usb/dwc3/host.c | 24 ++++++++++++++++++++++++
>  3 files changed, 45 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 01115df..8bcabc5 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -1785,6 +1785,7 @@ static int dwc3_probe(struct platform_device *pdev)
>  
>  	platform_set_drvdata(pdev, dwc);
>  	dwc3_cache_hwparams(dwc);
> +	device_init_wakeup(&pdev->dev, of_property_read_bool(dev->of_node, "wakeup-source"));
>  
>  	spin_lock_init(&dwc->lock);
>  	mutex_init(&dwc->mutex);
> @@ -1946,10 +1947,7 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
>  		dwc3_core_exit(dwc);
>  		break;
>  	case DWC3_GCTL_PRTCAP_HOST:
> -		if (!PMSG_IS_AUTO(msg)) {
> -			dwc3_core_exit(dwc);
> -			break;
> -		}
> +		dwc3_check_phy_speed_mode(dwc);
>  
>  		/* Let controller to suspend HSPHY before PHY driver suspends */
>  		if (dwc->dis_u2_susphy_quirk ||
> @@ -1965,6 +1963,15 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
>  
>  		phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
>  		phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
> +
> +		if (!PMSG_IS_AUTO(msg)) {
> +			if (device_may_wakeup(dwc->dev))

I think this should be device_can_wakeup(), i.e. hardware capability instead of
device policy. A drawback of powering the PHYs off is that it causes a high
power consumption of certain peripherals if VBUS is still supplied, so this
should be limited to platforms where the PHYs must be powered off (using wakeup
capability as a proxy for now).


> +				dwc->phy_power_off = false;
> +			else {
> +				dwc->phy_power_off = true;
> +				dwc3_core_exit(dwc);
> +			}
> +		}
>  		break;
>  	case DWC3_GCTL_PRTCAP_OTG:
>  		/* do nothing during runtime_suspend */
> @@ -2008,11 +2015,12 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
>  		break;
>  	case DWC3_GCTL_PRTCAP_HOST:
>  		if (!PMSG_IS_AUTO(msg)) {
> -			ret = dwc3_core_init_for_resume(dwc);
> -			if (ret)
> -				return ret;
> -			dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> -			break;
> +			if (dwc->phy_power_off) {
> +				ret = dwc3_core_init_for_resume(dwc);
> +				if (ret)
> +					return ret;
> +				dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> +			}
>  		}
>  		/* Restore GUSB2PHYCFG bits that were modified in suspend */
>  		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
> @@ -2084,8 +2092,6 @@ static int dwc3_runtime_suspend(struct device *dev)
>  	if (ret)
>  		return ret;
>  
> -	device_init_wakeup(dev, true);
> -
>  	return 0;
>  }
>  
> @@ -2094,8 +2100,6 @@ static int dwc3_runtime_resume(struct device *dev)
>  	struct dwc3     *dwc = dev_get_drvdata(dev);
>  	int		ret;
>  
> -	device_init_wakeup(dev, false);
> -
>  	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
>  	if (ret)
>  		return ret;
> diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
> index 81c486b..37397a8 100644
> --- a/drivers/usb/dwc3/core.h
> +++ b/drivers/usb/dwc3/core.h
> @@ -1155,6 +1155,9 @@ struct dwc3 {
>  
>  	bool			phys_ready;
>  
> +	unsigned int            hs_phy_mode;
> +	bool			phy_power_off;
> +
>  	struct ulpi		*ulpi;
>  	bool			ulpi_ready;
>  
> @@ -1539,6 +1542,7 @@ int dwc3_core_soft_reset(struct dwc3 *dwc);
>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
>  int dwc3_host_init(struct dwc3 *dwc);
>  void dwc3_host_exit(struct dwc3 *dwc);
> +void dwc3_check_phy_speed_mode(struct dwc3 *dwc);
>  #else
>  static inline int dwc3_host_init(struct dwc3 *dwc)
>  { return 0; }
> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
> index f56c30c..e19b40a 100644
> --- a/drivers/usb/dwc3/host.c
> +++ b/drivers/usb/dwc3/host.c
> @@ -12,6 +12,7 @@
>  #include <linux/platform_device.h>
>  
>  #include "core.h"
> +#include "../host/xhci.h"
>  
>  static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
>  					int irq, char *name)
> @@ -136,3 +137,26 @@ void dwc3_host_exit(struct dwc3 *dwc)
>  {
>  	platform_device_unregister(dwc->xhci);
>  }
> +
> +void dwc3_check_phy_speed_mode(struct dwc3 *dwc)
> +{
> +	int i, num_ports;
> +	u32 reg;
> +	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
> +	struct xhci_hcd	*xhci_hcd = hcd_to_xhci(hcd);
> +
> +	dwc->hs_phy_mode = 0;
> +
> +	reg = readl(&xhci_hcd->cap_regs->hcs_params1);
> +
> +	num_ports = HCS_MAX_PORTS(reg);
> +	for (i = 0; i < num_ports; i++) {
> +		reg = readl(&xhci_hcd->op_regs->port_status_base + i * NUM_PORT_REGS);
> +		if (reg & PORT_PE) {
> +			if (DEV_HIGHSPEED(reg) || DEV_FULLSPEED(reg))
> +				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_HS;
> +			else if (DEV_LOWSPEED(reg))
> +				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_LS;
> +		}
> +	}
> +}

I anticipate that it might raise concerns from maintainers that
dwc3_check_phy_speed_mode() accesses xHCI data structures and
registers directly. Could there be a generic HCD API that provides
this functionality (if implemented by the specific HCD)?
Matthias Kaehlcke May 12, 2022, 10:46 p.m. UTC | #2
On Thu, May 12, 2022 at 11:53:22PM +0530, Krishna Kurapati wrote:
> From: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> 
> Keep the power domain always on during runtime suspend or if the
> controller supports wakeup in order to retain controller status
> and to support wakeup from devices.
> 
> Signed-off-by: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
> ---
>  drivers/usb/dwc3/dwc3-qcom.c | 23 ++++++++++++++++-------
>  1 file changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/dwc3-qcom.c b/drivers/usb/dwc3/dwc3-qcom.c
> index 9804a19..1f9589a 100644
> --- a/drivers/usb/dwc3/dwc3-qcom.c
> +++ b/drivers/usb/dwc3/dwc3-qcom.c
> @@ -17,6 +17,7 @@
>  #include <linux/of_platform.h>
>  #include <linux/platform_device.h>
>  #include <linux/phy/phy.h>
> +#include <linux/pm_domain.h>
>  #include <linux/usb/of.h>
>  #include <linux/reset.h>
>  #include <linux/iopoll.h>
> @@ -718,12 +719,13 @@ dwc3_qcom_create_urs_usb_platdev(struct device *dev)
>  
>  static int dwc3_qcom_probe(struct platform_device *pdev)
>  {
> -	struct device_node	*np = pdev->dev.of_node;
> -	struct device		*dev = &pdev->dev;
> -	struct dwc3_qcom	*qcom;
> -	struct resource		*res, *parent_res = NULL;
> -	int			ret, i;
> -	bool			ignore_pipe_clk;
> +	struct device_node *np = pdev->dev.of_node;
> +	struct device *dev = &pdev->dev;
> +	struct dwc3_qcom *qcom;
> +	struct resource	*res, *parent_res = NULL;
> +	int ret, i;
> +	bool ignore_pipe_clk;
> +	struct generic_pm_domain *genpd;
>  
>  	qcom = devm_kzalloc(&pdev->dev, sizeof(*qcom), GFP_KERNEL);
>  	if (!qcom)
> @@ -732,6 +734,8 @@ static int dwc3_qcom_probe(struct platform_device *pdev)
>  	platform_set_drvdata(pdev, qcom);
>  	qcom->dev = &pdev->dev;
>  
> +	genpd = pd_to_genpd(qcom->dev->pm_domain);
> +
>  	if (has_acpi_companion(dev)) {
>  		qcom->acpi_pdata = acpi_device_get_match_data(dev);
>  		if (!qcom->acpi_pdata) {
> @@ -839,7 +843,12 @@ static int dwc3_qcom_probe(struct platform_device *pdev)
>  	if (ret)
>  		goto interconnect_exit;
>  
> -	device_init_wakeup(&pdev->dev, 1);
> +	genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
> +
> +	if (device_may_wakeup(&qcom->dwc3->dev)) {

This should probably be device_can_wakeup(), otherwise you'll have to set/clear
the flag before suspending (the wakeup policy might change after the device is
probed). Also I'm not sure if switching the domain off can impact the power
consumption of some connected peripherals, as is observed when the PHYs are
powered off (I could confirm this if needed).

> +		genpd->flags |= GENPD_FLAG_ALWAYS_ON;
> +		device_init_wakeup(&pdev->dev, true);
> +	}
>  	qcom->is_suspended = false;
>  	pm_runtime_set_active(dev);
>  	pm_runtime_enable(dev);
> -- 
> 2.7.4
>
Krishna Kurapati May 13, 2022, 3:58 a.m. UTC | #3
On 5/13/2022 4:10 AM, Matthias Kaehlcke wrote:
> On Thu, May 12, 2022 at 11:53:19PM +0530, Krishna Kurapati wrote:
>> From: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
>>
>> During suspend read the status of all port and set hs phy mode
>> based on current speed. Use this hs phy mode to configure wakeup
>> interrupts in qcom glue driver.
>>
>> Check wakeup-source property for dwc3 core node to set the
>> wakeup capability. Drop the device_init_wakeup call from
>> runtime suspend and resume.
>>
>> Also check during suspend if any wakeup capable devices are
>> connected to the controller (directly or through hubs), if there
>> are none set a flag to indicate that the PHY is powered
>> down during suspend.
>>
>> Signed-off-by: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
>> Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
>> ---
>>   drivers/usb/dwc3/core.c | 30 +++++++++++++++++-------------
>>   drivers/usb/dwc3/core.h |  4 ++++
>>   drivers/usb/dwc3/host.c | 24 ++++++++++++++++++++++++
>>   3 files changed, 45 insertions(+), 13 deletions(-)
>>
>> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
>> index 01115df..8bcabc5 100644
>> --- a/drivers/usb/dwc3/core.c
>> +++ b/drivers/usb/dwc3/core.c
>> @@ -1785,6 +1785,7 @@ static int dwc3_probe(struct platform_device *pdev)
>>   
>>   	platform_set_drvdata(pdev, dwc);
>>   	dwc3_cache_hwparams(dwc);
>> +	device_init_wakeup(&pdev->dev, of_property_read_bool(dev->of_node, "wakeup-source"));
>>   
>>   	spin_lock_init(&dwc->lock);
>>   	mutex_init(&dwc->mutex);
>> @@ -1946,10 +1947,7 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
>>   		dwc3_core_exit(dwc);
>>   		break;
>>   	case DWC3_GCTL_PRTCAP_HOST:
>> -		if (!PMSG_IS_AUTO(msg)) {
>> -			dwc3_core_exit(dwc);
>> -			break;
>> -		}
>> +		dwc3_check_phy_speed_mode(dwc);
>>   
>>   		/* Let controller to suspend HSPHY before PHY driver suspends */
>>   		if (dwc->dis_u2_susphy_quirk ||
>> @@ -1965,6 +1963,15 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
>>   
>>   		phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
>>   		phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
>> +
>> +		if (!PMSG_IS_AUTO(msg)) {
>> +			if (device_may_wakeup(dwc->dev))
> I think this should be device_can_wakeup(), i.e. hardware capability instead of
> device policy. A drawback of powering the PHYs off is that it causes a high
> power consumption of certain peripherals if VBUS is still supplied, so this
> should be limited to platforms where the PHYs must be powered off (using wakeup
> capability as a proxy for now).
Thnaks Mathias for the review. Will make this change in the next patchset.
>> +				dwc->phy_power_off = false;
>> +			else {
>> +				dwc->phy_power_off = true;
>> +				dwc3_core_exit(dwc);
>> +			}
>> +		}
>>   		break;
>>   	case DWC3_GCTL_PRTCAP_OTG:
>>   		/* do nothing during runtime_suspend */
>> @@ -2008,11 +2015,12 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
>>   		break;
>>   	case DWC3_GCTL_PRTCAP_HOST:
>>   		if (!PMSG_IS_AUTO(msg)) {
>> -			ret = dwc3_core_init_for_resume(dwc);
>> -			if (ret)
>> -				return ret;
>> -			dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
>> -			break;
>> +			if (dwc->phy_power_off) {
>> +				ret = dwc3_core_init_for_resume(dwc);
>> +				if (ret)
>> +					return ret;
>> +				dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
>> +			}
>>   		}
>>   		/* Restore GUSB2PHYCFG bits that were modified in suspend */
>>   		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
>> @@ -2084,8 +2092,6 @@ static int dwc3_runtime_suspend(struct device *dev)
>>   	if (ret)
>>   		return ret;
>>   
>> -	device_init_wakeup(dev, true);
>> -
>>   	return 0;
>>   }
>>   
>> @@ -2094,8 +2100,6 @@ static int dwc3_runtime_resume(struct device *dev)
>>   	struct dwc3     *dwc = dev_get_drvdata(dev);
>>   	int		ret;
>>   
>> -	device_init_wakeup(dev, false);
>> -
>>   	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
>>   	if (ret)
>>   		return ret;
>> diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
>> index 81c486b..37397a8 100644
>> --- a/drivers/usb/dwc3/core.h
>> +++ b/drivers/usb/dwc3/core.h
>> @@ -1155,6 +1155,9 @@ struct dwc3 {
>>   
>>   	bool			phys_ready;
>>   
>> +	unsigned int            hs_phy_mode;
>> +	bool			phy_power_off;
>> +
>>   	struct ulpi		*ulpi;
>>   	bool			ulpi_ready;
>>   
>> @@ -1539,6 +1542,7 @@ int dwc3_core_soft_reset(struct dwc3 *dwc);
>>   #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
>>   int dwc3_host_init(struct dwc3 *dwc);
>>   void dwc3_host_exit(struct dwc3 *dwc);
>> +void dwc3_check_phy_speed_mode(struct dwc3 *dwc);
>>   #else
>>   static inline int dwc3_host_init(struct dwc3 *dwc)
>>   { return 0; }
>> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
>> index f56c30c..e19b40a 100644
>> --- a/drivers/usb/dwc3/host.c
>> +++ b/drivers/usb/dwc3/host.c
>> @@ -12,6 +12,7 @@
>>   #include <linux/platform_device.h>
>>   
>>   #include "core.h"
>> +#include "../host/xhci.h"
>>   
>>   static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
>>   					int irq, char *name)
>> @@ -136,3 +137,26 @@ void dwc3_host_exit(struct dwc3 *dwc)
>>   {
>>   	platform_device_unregister(dwc->xhci);
>>   }
>> +
>> +void dwc3_check_phy_speed_mode(struct dwc3 *dwc)
>> +{
>> +	int i, num_ports;
>> +	u32 reg;
>> +	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
>> +	struct xhci_hcd	*xhci_hcd = hcd_to_xhci(hcd);
>> +
>> +	dwc->hs_phy_mode = 0;
>> +
>> +	reg = readl(&xhci_hcd->cap_regs->hcs_params1);
>> +
>> +	num_ports = HCS_MAX_PORTS(reg);
>> +	for (i = 0; i < num_ports; i++) {
>> +		reg = readl(&xhci_hcd->op_regs->port_status_base + i * NUM_PORT_REGS);
>> +		if (reg & PORT_PE) {
>> +			if (DEV_HIGHSPEED(reg) || DEV_FULLSPEED(reg))
>> +				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_HS;
>> +			else if (DEV_LOWSPEED(reg))
>> +				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_LS;
>> +		}
>> +	}
>> +}
> I anticipate that it might raise concerns from maintainers that
> dwc3_check_phy_speed_mode() accesses xHCI data structures and
> registers directly. Could there be a generic HCD API that provides
> this functionality (if implemented by the specific HCD)?

Hi Mathias, we are not sure if there is any such API present currently.

Hi Alan, can you help suggest any API (if present) that we can reuse 
here to avoid

xhci registers and structs here in dwc3.
Pavan Kondeti May 16, 2022, 3:04 p.m. UTC | #4
On Mon, May 16, 2022 at 10:13:27AM +0530, Pavan Kondeti wrote:
> On Fri, May 13, 2022 at 09:28:16AM +0530, Krishna Kurapati PSSNV wrote:
> > 
> > On 5/13/2022 4:10 AM, Matthias Kaehlcke wrote:
> > >On Thu, May 12, 2022 at 11:53:19PM +0530, Krishna Kurapati wrote:
> > >>From: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> > >>
> > >>During suspend read the status of all port and set hs phy mode
> > >>based on current speed. Use this hs phy mode to configure wakeup
> > >>interrupts in qcom glue driver.
> > >>
> > >>Check wakeup-source property for dwc3 core node to set the
> > >>wakeup capability. Drop the device_init_wakeup call from
> > >>runtime suspend and resume.
> > >>
> > >>Also check during suspend if any wakeup capable devices are
> > >>connected to the controller (directly or through hubs), if there
> > >>are none set a flag to indicate that the PHY is powered
> > >>down during suspend.
> > >>
> > >>Signed-off-by: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> > >>Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
> > >>---
> > >>  drivers/usb/dwc3/core.c | 30 +++++++++++++++++-------------
> > >>  drivers/usb/dwc3/core.h |  4 ++++
> > >>  drivers/usb/dwc3/host.c | 24 ++++++++++++++++++++++++
> > >>  3 files changed, 45 insertions(+), 13 deletions(-)
> > >>
> > >>diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> > >>index 01115df..8bcabc5 100644
> > >>--- a/drivers/usb/dwc3/core.c
> > >>+++ b/drivers/usb/dwc3/core.c
> > >>@@ -1785,6 +1785,7 @@ static int dwc3_probe(struct platform_device *pdev)
> > >>  	platform_set_drvdata(pdev, dwc);
> > >>  	dwc3_cache_hwparams(dwc);
> > >>+	device_init_wakeup(&pdev->dev, of_property_read_bool(dev->of_node, "wakeup-source"));
> > >>  	spin_lock_init(&dwc->lock);
> > >>  	mutex_init(&dwc->mutex);
> > >>@@ -1946,10 +1947,7 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
> > >>  		dwc3_core_exit(dwc);
> > >>  		break;
> > >>  	case DWC3_GCTL_PRTCAP_HOST:
> > >>-		if (!PMSG_IS_AUTO(msg)) {
> > >>-			dwc3_core_exit(dwc);
> > >>-			break;
> > >>-		}
> > >>+		dwc3_check_phy_speed_mode(dwc);
> > >>  		/* Let controller to suspend HSPHY before PHY driver suspends */
> > >>  		if (dwc->dis_u2_susphy_quirk ||
> > >>@@ -1965,6 +1963,15 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
> > >>  		phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
> > >>  		phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
> > >>+
> > >>+		if (!PMSG_IS_AUTO(msg)) {
> > >>+			if (device_may_wakeup(dwc->dev))
> > >I think this should be device_can_wakeup(), i.e. hardware capability instead of
> > >device policy. A drawback of powering the PHYs off is that it causes a high
> > >power consumption of certain peripherals if VBUS is still supplied, so this
> > >should be limited to platforms where the PHYs must be powered off (using wakeup
> > >capability as a proxy for now).
> > Thnaks Mathias for the review. Will make this change in the next patchset.
> > >>+				dwc->phy_power_off = false;
> > >>+			else {
> > >>+				dwc->phy_power_off = true;
> > >>+				dwc3_core_exit(dwc);
> > >>+			}
> > >>+		}
> > >>  		break;
> > >>  	case DWC3_GCTL_PRTCAP_OTG:
> > >>  		/* do nothing during runtime_suspend */
> > >>@@ -2008,11 +2015,12 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
> > >>  		break;
> > >>  	case DWC3_GCTL_PRTCAP_HOST:
> > >>  		if (!PMSG_IS_AUTO(msg)) {
> > >>-			ret = dwc3_core_init_for_resume(dwc);
> > >>-			if (ret)
> > >>-				return ret;
> > >>-			dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> > >>-			break;
> > >>+			if (dwc->phy_power_off) {
> > >>+				ret = dwc3_core_init_for_resume(dwc);
> > >>+				if (ret)
> > >>+					return ret;
> > >>+				dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> > >>+			}
> > >>  		}
> > >>  		/* Restore GUSB2PHYCFG bits that were modified in suspend */
> > >>  		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
> > >>@@ -2084,8 +2092,6 @@ static int dwc3_runtime_suspend(struct device *dev)
> > >>  	if (ret)
> > >>  		return ret;
> > >>-	device_init_wakeup(dev, true);
> > >>-
> > >>  	return 0;
> > >>  }
> > >>@@ -2094,8 +2100,6 @@ static int dwc3_runtime_resume(struct device *dev)
> > >>  	struct dwc3     *dwc = dev_get_drvdata(dev);
> > >>  	int		ret;
> > >>-	device_init_wakeup(dev, false);
> > >>-
> > >>  	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
> > >>  	if (ret)
> > >>  		return ret;
> > >>diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
> > >>index 81c486b..37397a8 100644
> > >>--- a/drivers/usb/dwc3/core.h
> > >>+++ b/drivers/usb/dwc3/core.h
> > >>@@ -1155,6 +1155,9 @@ struct dwc3 {
> > >>  	bool			phys_ready;
> > >>+	unsigned int            hs_phy_mode;
> > >>+	bool			phy_power_off;
> > >>+
> > >>  	struct ulpi		*ulpi;
> > >>  	bool			ulpi_ready;
> > >>@@ -1539,6 +1542,7 @@ int dwc3_core_soft_reset(struct dwc3 *dwc);
> > >>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
> > >>  int dwc3_host_init(struct dwc3 *dwc);
> > >>  void dwc3_host_exit(struct dwc3 *dwc);
> > >>+void dwc3_check_phy_speed_mode(struct dwc3 *dwc);
> > >>  #else
> > >>  static inline int dwc3_host_init(struct dwc3 *dwc)
> > >>  { return 0; }
> > >>diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
> > >>index f56c30c..e19b40a 100644
> > >>--- a/drivers/usb/dwc3/host.c
> > >>+++ b/drivers/usb/dwc3/host.c
> > >>@@ -12,6 +12,7 @@
> > >>  #include <linux/platform_device.h>
> > >>  #include "core.h"
> > >>+#include "../host/xhci.h"
> > >>  static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
> > >>  					int irq, char *name)
> > >>@@ -136,3 +137,26 @@ void dwc3_host_exit(struct dwc3 *dwc)
> > >>  {
> > >>  	platform_device_unregister(dwc->xhci);
> > >>  }
> > >>+
> > >>+void dwc3_check_phy_speed_mode(struct dwc3 *dwc)
> > >>+{
> > >>+	int i, num_ports;
> > >>+	u32 reg;
> > >>+	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
> > >>+	struct xhci_hcd	*xhci_hcd = hcd_to_xhci(hcd);
> > >>+
> > >>+	dwc->hs_phy_mode = 0;
> > >>+
> > >>+	reg = readl(&xhci_hcd->cap_regs->hcs_params1);
> > >>+
> > >>+	num_ports = HCS_MAX_PORTS(reg);
> > >>+	for (i = 0; i < num_ports; i++) {
> > >>+		reg = readl(&xhci_hcd->op_regs->port_status_base + i * NUM_PORT_REGS);
> > >>+		if (reg & PORT_PE) {
> > >>+			if (DEV_HIGHSPEED(reg) || DEV_FULLSPEED(reg))
> > >>+				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_HS;
> > >>+			else if (DEV_LOWSPEED(reg))
> > >>+				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_LS;
> > >>+		}
> > >>+	}
> > >>+}
> > >I anticipate that it might raise concerns from maintainers that
> > >dwc3_check_phy_speed_mode() accesses xHCI data structures and
> > >registers directly. Could there be a generic HCD API that provides
> > >this functionality (if implemented by the specific HCD)?
> > 
> > Hi Mathias, we are not sure if there is any such API present currently.
> > 
> > Hi Alan, can you help suggest any API (if present) that we can reuse here to
> > avoid
> > 
> > xhci registers and structs here in dwc3.
> > 
> 
> We can probably do something like below to query the speed. This avoids adding
> another API and does not touch the underlying registers.
> 
> Pls define enum usb_device_speed usb2_speed in dwc3 structure.
> 
> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
> index f29a264..fed1c58 100644
> --- a/drivers/usb/dwc3/host.c
> +++ b/drivers/usb/dwc3/host.c
> @@ -9,9 +9,29 @@
>  
>  #include <linux/acpi.h>
>  #include <linux/platform_device.h>
> +#include <linux/usb.h>
>  
>  #include "core.h"
>  
> +void dwc3_update_hs_phy_speed(struct dwc3 *dwc)
> +{
> +	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
> +	struct usb_device *udev;
> +
> +	/*
> +	 * It is possible to query the speed of all children of
> +	 * USB2.0 root hub via usb_hub_for_each_child(). DWC3 code
> +	 * currently supports only 1 port per controller. So
> +	 * this is sufficient.
> +	 */
> +	udev = usb_hub_find_child(hcd->self.root_hub, 1);
> +
> +	if (udev)
> +		dwc->usb2_speed = udev->speed;
> +	else
> +		dwc->usb2_speed = USB_SPEED_UNKNOWN;
> +}
> +
>  static int dwc3_host_get_irq(struct dwc3 *dwc)
>  {
>  	struct platform_device	*dwc3_pdev = to_platform_device(dwc->dev);
> 
> 
I am also thinking why dwc core needs to cache usb2_speed since dwc3-qcom glue
driver is the only sole user. We also require it only during suspend and does
not bother about dwc::usb2_speed correctness outside suspend. Lets move this
to dwc3-qcom suspend routines where we have to rely on USB2 speed for
configuring the D+/D- interrupt.

Thanks,
Pavan
Matthias Kaehlcke May 16, 2022, 11:13 p.m. UTC | #5
On Mon, May 16, 2022 at 08:34:45PM +0530, Pavan Kondeti wrote:
> On Mon, May 16, 2022 at 10:13:27AM +0530, Pavan Kondeti wrote:
> > On Fri, May 13, 2022 at 09:28:16AM +0530, Krishna Kurapati PSSNV wrote:
> > > 
> > > On 5/13/2022 4:10 AM, Matthias Kaehlcke wrote:
> > > >On Thu, May 12, 2022 at 11:53:19PM +0530, Krishna Kurapati wrote:
> > > >>From: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> > > >>
> > > >>During suspend read the status of all port and set hs phy mode
> > > >>based on current speed. Use this hs phy mode to configure wakeup
> > > >>interrupts in qcom glue driver.
> > > >>
> > > >>Check wakeup-source property for dwc3 core node to set the
> > > >>wakeup capability. Drop the device_init_wakeup call from
> > > >>runtime suspend and resume.
> > > >>
> > > >>Also check during suspend if any wakeup capable devices are
> > > >>connected to the controller (directly or through hubs), if there
> > > >>are none set a flag to indicate that the PHY is powered
> > > >>down during suspend.
> > > >>
> > > >>Signed-off-by: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> > > >>Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
> > > >>---
> > > >>  drivers/usb/dwc3/core.c | 30 +++++++++++++++++-------------
> > > >>  drivers/usb/dwc3/core.h |  4 ++++
> > > >>  drivers/usb/dwc3/host.c | 24 ++++++++++++++++++++++++
> > > >>  3 files changed, 45 insertions(+), 13 deletions(-)
> > > >>
> > > >>diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> > > >>index 01115df..8bcabc5 100644
> > > >>--- a/drivers/usb/dwc3/core.c
> > > >>+++ b/drivers/usb/dwc3/core.c
> > > >>@@ -1785,6 +1785,7 @@ static int dwc3_probe(struct platform_device *pdev)
> > > >>  	platform_set_drvdata(pdev, dwc);
> > > >>  	dwc3_cache_hwparams(dwc);
> > > >>+	device_init_wakeup(&pdev->dev, of_property_read_bool(dev->of_node, "wakeup-source"));
> > > >>  	spin_lock_init(&dwc->lock);
> > > >>  	mutex_init(&dwc->mutex);
> > > >>@@ -1946,10 +1947,7 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
> > > >>  		dwc3_core_exit(dwc);
> > > >>  		break;
> > > >>  	case DWC3_GCTL_PRTCAP_HOST:
> > > >>-		if (!PMSG_IS_AUTO(msg)) {
> > > >>-			dwc3_core_exit(dwc);
> > > >>-			break;
> > > >>-		}
> > > >>+		dwc3_check_phy_speed_mode(dwc);
> > > >>  		/* Let controller to suspend HSPHY before PHY driver suspends */
> > > >>  		if (dwc->dis_u2_susphy_quirk ||
> > > >>@@ -1965,6 +1963,15 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
> > > >>  		phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
> > > >>  		phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
> > > >>+
> > > >>+		if (!PMSG_IS_AUTO(msg)) {
> > > >>+			if (device_may_wakeup(dwc->dev))
> > > >I think this should be device_can_wakeup(), i.e. hardware capability instead of
> > > >device policy. A drawback of powering the PHYs off is that it causes a high
> > > >power consumption of certain peripherals if VBUS is still supplied, so this
> > > >should be limited to platforms where the PHYs must be powered off (using wakeup
> > > >capability as a proxy for now).
> > > Thnaks Mathias for the review. Will make this change in the next patchset.
> > > >>+				dwc->phy_power_off = false;
> > > >>+			else {
> > > >>+				dwc->phy_power_off = true;
> > > >>+				dwc3_core_exit(dwc);
> > > >>+			}
> > > >>+		}
> > > >>  		break;
> > > >>  	case DWC3_GCTL_PRTCAP_OTG:
> > > >>  		/* do nothing during runtime_suspend */
> > > >>@@ -2008,11 +2015,12 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
> > > >>  		break;
> > > >>  	case DWC3_GCTL_PRTCAP_HOST:
> > > >>  		if (!PMSG_IS_AUTO(msg)) {
> > > >>-			ret = dwc3_core_init_for_resume(dwc);
> > > >>-			if (ret)
> > > >>-				return ret;
> > > >>-			dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> > > >>-			break;
> > > >>+			if (dwc->phy_power_off) {
> > > >>+				ret = dwc3_core_init_for_resume(dwc);
> > > >>+				if (ret)
> > > >>+					return ret;
> > > >>+				dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
> > > >>+			}
> > > >>  		}
> > > >>  		/* Restore GUSB2PHYCFG bits that were modified in suspend */
> > > >>  		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
> > > >>@@ -2084,8 +2092,6 @@ static int dwc3_runtime_suspend(struct device *dev)
> > > >>  	if (ret)
> > > >>  		return ret;
> > > >>-	device_init_wakeup(dev, true);
> > > >>-
> > > >>  	return 0;
> > > >>  }
> > > >>@@ -2094,8 +2100,6 @@ static int dwc3_runtime_resume(struct device *dev)
> > > >>  	struct dwc3     *dwc = dev_get_drvdata(dev);
> > > >>  	int		ret;
> > > >>-	device_init_wakeup(dev, false);
> > > >>-
> > > >>  	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
> > > >>  	if (ret)
> > > >>  		return ret;
> > > >>diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
> > > >>index 81c486b..37397a8 100644
> > > >>--- a/drivers/usb/dwc3/core.h
> > > >>+++ b/drivers/usb/dwc3/core.h
> > > >>@@ -1155,6 +1155,9 @@ struct dwc3 {
> > > >>  	bool			phys_ready;
> > > >>+	unsigned int            hs_phy_mode;
> > > >>+	bool			phy_power_off;
> > > >>+
> > > >>  	struct ulpi		*ulpi;
> > > >>  	bool			ulpi_ready;
> > > >>@@ -1539,6 +1542,7 @@ int dwc3_core_soft_reset(struct dwc3 *dwc);
> > > >>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
> > > >>  int dwc3_host_init(struct dwc3 *dwc);
> > > >>  void dwc3_host_exit(struct dwc3 *dwc);
> > > >>+void dwc3_check_phy_speed_mode(struct dwc3 *dwc);
> > > >>  #else
> > > >>  static inline int dwc3_host_init(struct dwc3 *dwc)
> > > >>  { return 0; }
> > > >>diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
> > > >>index f56c30c..e19b40a 100644
> > > >>--- a/drivers/usb/dwc3/host.c
> > > >>+++ b/drivers/usb/dwc3/host.c
> > > >>@@ -12,6 +12,7 @@
> > > >>  #include <linux/platform_device.h>
> > > >>  #include "core.h"
> > > >>+#include "../host/xhci.h"
> > > >>  static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
> > > >>  					int irq, char *name)
> > > >>@@ -136,3 +137,26 @@ void dwc3_host_exit(struct dwc3 *dwc)
> > > >>  {
> > > >>  	platform_device_unregister(dwc->xhci);
> > > >>  }
> > > >>+
> > > >>+void dwc3_check_phy_speed_mode(struct dwc3 *dwc)
> > > >>+{
> > > >>+	int i, num_ports;
> > > >>+	u32 reg;
> > > >>+	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
> > > >>+	struct xhci_hcd	*xhci_hcd = hcd_to_xhci(hcd);
> > > >>+
> > > >>+	dwc->hs_phy_mode = 0;
> > > >>+
> > > >>+	reg = readl(&xhci_hcd->cap_regs->hcs_params1);
> > > >>+
> > > >>+	num_ports = HCS_MAX_PORTS(reg);
> > > >>+	for (i = 0; i < num_ports; i++) {
> > > >>+		reg = readl(&xhci_hcd->op_regs->port_status_base + i * NUM_PORT_REGS);
> > > >>+		if (reg & PORT_PE) {
> > > >>+			if (DEV_HIGHSPEED(reg) || DEV_FULLSPEED(reg))
> > > >>+				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_HS;
> > > >>+			else if (DEV_LOWSPEED(reg))
> > > >>+				dwc->hs_phy_mode |= PHY_MODE_USB_HOST_LS;
> > > >>+		}
> > > >>+	}
> > > >>+}
> > > >I anticipate that it might raise concerns from maintainers that
> > > >dwc3_check_phy_speed_mode() accesses xHCI data structures and
> > > >registers directly. Could there be a generic HCD API that provides
> > > >this functionality (if implemented by the specific HCD)?
> > > 
> > > Hi Mathias, we are not sure if there is any such API present currently.
> > > 
> > > Hi Alan, can you help suggest any API (if present) that we can reuse here to
> > > avoid
> > > 
> > > xhci registers and structs here in dwc3.
> > > 
> > 
> > We can probably do something like below to query the speed. This avoids adding
> > another API and does not touch the underlying registers.
> > 
> > Pls define enum usb_device_speed usb2_speed in dwc3 structure.
> > 
> > diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
> > index f29a264..fed1c58 100644
> > --- a/drivers/usb/dwc3/host.c
> > +++ b/drivers/usb/dwc3/host.c
> > @@ -9,9 +9,29 @@
> >  
> >  #include <linux/acpi.h>
> >  #include <linux/platform_device.h>
> > +#include <linux/usb.h>
> >  
> >  #include "core.h"
> >  
> > +void dwc3_update_hs_phy_speed(struct dwc3 *dwc)
> > +{
> > +	struct usb_hcd	*hcd = platform_get_drvdata(dwc->xhci);
> > +	struct usb_device *udev;
> > +
> > +	/*
> > +	 * It is possible to query the speed of all children of
> > +	 * USB2.0 root hub via usb_hub_for_each_child(). DWC3 code
> > +	 * currently supports only 1 port per controller. So
> > +	 * this is sufficient.
> > +	 */
> > +	udev = usb_hub_find_child(hcd->self.root_hub, 1);
> > +
> > +	if (udev)
> > +		dwc->usb2_speed = udev->speed;
> > +	else
> > +		dwc->usb2_speed = USB_SPEED_UNKNOWN;
> > +}
> > +
> >  static int dwc3_host_get_irq(struct dwc3 *dwc)
> >  {
> >  	struct platform_device	*dwc3_pdev = to_platform_device(dwc->dev);
> > 
> > 
> I am also thinking why dwc core needs to cache usb2_speed since dwc3-qcom glue
> driver is the only sole user. We also require it only during suspend and does
> not bother about dwc::usb2_speed correctness outside suspend. Lets move this
> to dwc3-qcom suspend routines where we have to rely on USB2 speed for
> configuring the D+/D- interrupt.

I like both of your suggestions. If something like dwc3_update_hs_phy_speed()
works properly here we should have finally sorted out all the layering issues
Felipe was unhappy about.