All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH usb-next v10 0/8] initialize (multiple) PHYs for a HCD
@ 2018-02-18 18:44 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

The goal of this series is to initialize multiple PHYs on a USB host
controller, which is needed on Amlogic Meson GXL and GXM SoCs.
- Amlogic Meson GXL and GXM SoCs come with a dwc3 IP block which has two
  or three USB2 ports enabled on the internal root-hub. The SoCs also
  provide separate USB2 PHYs, one per port. All USB2 PHYs (which are
  internally "connected" to the dwc3 roothub) need to be powered on,
  otherwise USB devices cannot be enumerated (even if just one PHY is
  disabled and if the device is plugged into another, enabled port)
- dwc3 already takes one USB2 and one USB3 PHY and initializes these
  correctly
- some other HCI platform drivers (like ehci-platform.c, xhci-mtk.c and
  ohci-platform.c) do not have a limitation on the number of PHYs - they
  support any number of USB PHYs (typically one PHY per actual port)

This series adds a new "PHY wrapper". This can be configured through
devicetree by passing a "phys" property (with any number of PHY handles)
to the USB controller.
Additionally it this integrates this new PHY wrapper into hcd.c which
automatically enables it for all USB controller drivers (tested on an
Amlogic Meson GXL SoC which uses a dwc3 controller). Since this leaves
some duplicate code (which now becomes obsolete!) in various drivers
this is being cleaned up as well.


Changes since v9 at [12]:
- changed "skip_phy_initialization" from PATCH #2 into a bitfield and
  moved it below the "remove_phy" bitfield as suggested by Alan Stern
  (thanks!)
- rebased against usb-next (which is currently identical to v4.16-rc1)

Changes since v8 at [10]:
- add new patch #2 which allows disabling "USB PHY management" in the
  USB HCD core driver. this also affects patch #4 which now checks the
  skip_phy_initialization flag before calling
  usb_phy_roothub_{init,power_on} (other functions don't need to be
  guarded since they are null-safe - see patch #3)
- integrated my other series 'remove driver-specific "multiple PHY"
  handling' from [11] into this one so the result of this "generic"
  code (= removing duplicate code in various drivers) can be seen
  directly. affects patches #5-8. (note: patch #1 "usb: mtu3: remove
  custom USB PHY handling" from that series was dropped, because it
  broke device mode support - Chunfeng Yun will come up with a
  separate patch later. patch #5 "usb: chipidea: do not set the "phy"
  field in struct usb_hcd" from that series was rewritten and is now
  integrated into the new patch #2 from this series)
- collected all {Reviewed,Tested,Acked}-by
- dropped RfC prefix since this v8 was out for review for two weeks
  and it only got Reviewed-by's and Tested-by's since then

Changes since v7 at [8]:
- updated the description in the cover-letter
- wrapped the only call usb_phy_roothub_power_{on,off} in
  hcd_bus_{resume,suspend} during system-suspend (excluding it from
  being called during runtime/auto suspend). this also fixes a problem
  on Meson GXL which does not detect that a new device has been
  connected if the PHYs are powered off. Thanks to Manu Gautam for
  suggesting this! (affects patch #3)
- after a discussion about the dt-bindings of this patch I went back to
  simply specifying a "phys" property (without a corresponding phy-names
  property) to pass all PHYs directly in the node of the USB controller.
  From my understanding the root hub node was "artificial" and we should
  describe it's properties as part of the controller, rather than
  introducing a sub-node. see also the discussion with Arnd in [9]
  (this affects patch #1 and #2)
- dropped patch #4 ("dt-bindings: usb: xhci: include the roothub and a
  device in the example") because we're back to just specifying a "phys"
  property directly within the controller.
  NOTE: some HCI platform drivers are using the "phys" property as well.
  With this series phy_{init,power_on,power_off,exit} will be called
  twice for each PHY when using a ehci-platform.c or xhci-mtk.c USB
  controller (because the controller driver and our new, generic code
  now manage the PHYs). the PHY framework handles this gracefully (by
  ref-counting all operations). a follow-up series will be sent which
  removes the custom handling from the affected drivers (so only the
  new PHY wrapper will manage the PHYs state after that follow-up
  series).
- fixed an unnecessary whitespace change spotted by Alan Stern (thanks!)
  in patch #3
- removed all Tested-by's since we're changing code again

Changes since v6 at [6]:
- fixed unnecessary whitespace change (noticed by Alan Stern - thanks)
- added PATCH #4 to clarify (with an example) how I understood how Rob
  wanted the dt-binding to look like (see [0]). please wait for an ACK
  from Rob on this one to make sure that we're not introducing some
  broken binding (as discussed with Arnd: [7])
- added Alan Stern's ACK on PATCH #3
- added Neil's Tested-by on PATCH #2 and #3

Changes since RfC v5 at [5]:
- dropped RfC prefix
- removed noisy dev_err if no roothub node was found (spotted by
  Xiaolong Ye's kbuild test robot - thank you for that!)
- moved the call to usb_phy_roothub_power_off() within
  hcd_bus_suspend() to make sure that the PHYs are turned off
  if the "race with a root-hub wakeup event" condition is met (in
  this case the PHYs are turned on again, with the old code we did
  break the PHYs internal ref-counting because we never turned the
  PHYs off before turning them on again in case of that special
  "race with a root-hub wakeup event").
  additionally we're not handling the status returned by
  usb_phy_roothub_power_off() anymore (the bus is already turned off
  and we tried to turn off all PHYs as well - only the PHYs which
  failed to power off will stay in the current state).
  thanks to Alan Stern for the suggestion
- removed return value from usb_phy_roothub_power_off() because none
  of my code uses it anymore. thanks to Alan Stern for the suggestion

Changes since v4 at [4]:
- renamed the subject of the cover-letter (old name was:
  "initialize (multiple) PHYs in xhci-plat")
- back into RFC status (see below for the reasons)
- dropped Tested-by from Chunfeng Yun (same reasons as RFC status)
- reworded cover-letter and commit messages from "platform-roothub"
  to "roothub PHY wrapper"
- moved code from drivers/usb/host/platform-roothub.* to
  drivers/usb/core/phy.* and the changes to
  drivers/usb/host/xhci-plat.c to drivers/usb/core/hcd.c as suggested
  by Mathias Nyman (as a benefit this will enable the new logic for
  non-xHCI controllers as well - however this was not tested yet)
- rename the structs, function names, etc from platform_roothub_* to
  usb_phy_roothub*

Changes since RFCv3 at [3]:
- moved the DT binding change from patch #3 to patch #1 as suggested
  by Rob Herring (and slightly adjusted the commit message to account
  for that)
- added Tested-by from Chunfeng Yun (who confirmed that the whole
  concept and implementation works fine on Mediatek SoCs - many thanks
  again!) to patch #2
- added Rob Herring's ACK to patches 1 and 3
- dropped RFC status (RFCv3 -> PATCH v4)

Changes since RFCv2 at [2]:
- split phy_{init,exit} and phy_power_{on,off} handling. up until RFCv2
  I called phy_init plus phy_power_on in platform_roothub_power_on and
  phy_power_off plus phy_exit in platform_roothub_power_off. However,
  Chunfeng Yun (a Mediatek SoC developer - many thanks for testing my
  series and providing great feedback) reported that only using
  phy_power_off (and omitting phy_exit) during system suspend fixes an
  issue where USB devices would be re-enumerated when resuming. His
  original problem description: "In order to keep link state on mt8173,
  we just power off all phys(not exit) when system enter suspend, then
  power on them again (needn't init, otherwise device will be
  disconnected) when system resume, this can avoid re-enumerating
  device.". This fix affects patch #2 and #3 as we now have
  platform_roothub_init (which calls phy_init internally),
  platform_roothub_power_on (which calls phy_power_on internally),
  platform_roothub_power_off (which calls phy_power_off internally) and
  platform_roothub_exit (which calls phy_exit internally). suspend and
  resume only call platform_roothub_power_{on,off} to prevent the issue
  described by Chunfeng Yun (unfortunately I cannot test this because
  the Amlogic platform currently does not support system suspend).
- dropped two struct forward declarations from platform-roothub.h which
  are not used in the header file (thanks to Chunfeng Yun for spotting
  this)

Changes since RFCv1 at [1]:
- split the usb-xhci dt-binding documentation into a separate patch
- fixed a typo ("usb-phy" -> "phys" in the dt-binding example)
- rebased to apply against latest usb-next


[0] http://lists.infradead.org/pipermail/linux-amlogic/2016-November/001818.html
[1] http://marc.info/?l=linux-usb&m=148414866303604&w=2
[2] https://www.spinics.net/lists/linux-usb/msg158967.html
[3] https://www.spinics.net/lists/devicetree/msg190426.html
[4] http://lists.infradead.org/pipermail/linux-amlogic/2017-September/004685.html
[5] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/004924.html
[6] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005049.html
[7] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005121.html
[8] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005124.html
[9] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005192.html
[10] http://lists.infradead.org/pipermail/linux-amlogic/2018-January/006274.html
[11] https://marc.info/?l=linux-usb&m=151683941207898&w=2
[12] http://lists.infradead.org/pipermail/linux-amlogic/2018-February/006412.html

Martin Blumenstingl (8):
  dt-bindings: usb: add the documentation for USB HCDs
  usb: add a flag to skip PHY initialization to struct usb_hcd
  usb: core: add a wrapper for the USB PHYs on the HCD
  usb: core: hcd: integrate the PHY wrapper into the HCD core
  usb: host: xhci-mtk: remove custom USB PHY handling
  usb: host: ehci-platform: remove custom USB PHY handling
  usb: host: ohci-platform: remove custom USB PHY handling
  usb: core: hcd: remove support for initializing a single PHY

 .../devicetree/bindings/usb/mediatek,mtk-xhci.txt  |   5 +-
 .../devicetree/bindings/usb/mediatek,mtu3.txt      |   5 +-
 Documentation/devicetree/bindings/usb/usb-ehci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-hcd.txt  |   9 ++
 Documentation/devicetree/bindings/usb/usb-ohci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-uhci.txt |   3 +
 Documentation/devicetree/bindings/usb/usb-xhci.txt |   5 +
 drivers/usb/chipidea/host.c                        |   6 +-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hcd.c                             |  64 ++++-----
 drivers/usb/core/phy.c                             | 158 +++++++++++++++++++++
 drivers/usb/core/phy.h                             |   7 +
 drivers/usb/host/ehci-fsl.c                        |   2 +
 drivers/usb/host/ehci-platform.c                   |  55 +------
 drivers/usb/host/ehci-tegra.c                      |   1 +
 drivers/usb/host/ohci-omap.c                       |   1 +
 drivers/usb/host/ohci-platform.c                   |  56 +-------
 drivers/usb/host/xhci-mtk.c                        |  98 +------------
 drivers/usb/host/xhci-plat.c                       |   1 +
 include/linux/usb/hcd.h                            |   8 +-
 20 files changed, 253 insertions(+), 245 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

-- 
2.16.1

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 0/8] initialize (multiple) PHYs for a HCD
@ 2018-02-18 18:44 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-arm-kernel

The goal of this series is to initialize multiple PHYs on a USB host
controller, which is needed on Amlogic Meson GXL and GXM SoCs.
- Amlogic Meson GXL and GXM SoCs come with a dwc3 IP block which has two
  or three USB2 ports enabled on the internal root-hub. The SoCs also
  provide separate USB2 PHYs, one per port. All USB2 PHYs (which are
  internally "connected" to the dwc3 roothub) need to be powered on,
  otherwise USB devices cannot be enumerated (even if just one PHY is
  disabled and if the device is plugged into another, enabled port)
- dwc3 already takes one USB2 and one USB3 PHY and initializes these
  correctly
- some other HCI platform drivers (like ehci-platform.c, xhci-mtk.c and
  ohci-platform.c) do not have a limitation on the number of PHYs - they
  support any number of USB PHYs (typically one PHY per actual port)

This series adds a new "PHY wrapper". This can be configured through
devicetree by passing a "phys" property (with any number of PHY handles)
to the USB controller.
Additionally it this integrates this new PHY wrapper into hcd.c which
automatically enables it for all USB controller drivers (tested on an
Amlogic Meson GXL SoC which uses a dwc3 controller). Since this leaves
some duplicate code (which now becomes obsolete!) in various drivers
this is being cleaned up as well.


Changes since v9 at [12]:
- changed "skip_phy_initialization" from PATCH #2 into a bitfield and
  moved it below the "remove_phy" bitfield as suggested by Alan Stern
  (thanks!)
- rebased against usb-next (which is currently identical to v4.16-rc1)

Changes since v8 at [10]:
- add new patch #2 which allows disabling "USB PHY management" in the
  USB HCD core driver. this also affects patch #4 which now checks the
  skip_phy_initialization flag before calling
  usb_phy_roothub_{init,power_on} (other functions don't need to be
  guarded since they are null-safe - see patch #3)
- integrated my other series 'remove driver-specific "multiple PHY"
  handling' from [11] into this one so the result of this "generic"
  code (= removing duplicate code in various drivers) can be seen
  directly. affects patches #5-8. (note: patch #1 "usb: mtu3: remove
  custom USB PHY handling" from that series was dropped, because it
  broke device mode support - Chunfeng Yun will come up with a
  separate patch later. patch #5 "usb: chipidea: do not set the "phy"
  field in struct usb_hcd" from that series was rewritten and is now
  integrated into the new patch #2 from this series)
- collected all {Reviewed,Tested,Acked}-by
- dropped RfC prefix since this v8 was out for review for two weeks
  and it only got Reviewed-by's and Tested-by's since then

Changes since v7 at [8]:
- updated the description in the cover-letter
- wrapped the only call usb_phy_roothub_power_{on,off} in
  hcd_bus_{resume,suspend} during system-suspend (excluding it from
  being called during runtime/auto suspend). this also fixes a problem
  on Meson GXL which does not detect that a new device has been
  connected if the PHYs are powered off. Thanks to Manu Gautam for
  suggesting this! (affects patch #3)
- after a discussion about the dt-bindings of this patch I went back to
  simply specifying a "phys" property (without a corresponding phy-names
  property) to pass all PHYs directly in the node of the USB controller.
  From my understanding the root hub node was "artificial" and we should
  describe it's properties as part of the controller, rather than
  introducing a sub-node. see also the discussion with Arnd in [9]
  (this affects patch #1 and #2)
- dropped patch #4 ("dt-bindings: usb: xhci: include the roothub and a
  device in the example") because we're back to just specifying a "phys"
  property directly within the controller.
  NOTE: some HCI platform drivers are using the "phys" property as well.
  With this series phy_{init,power_on,power_off,exit} will be called
  twice for each PHY when using a ehci-platform.c or xhci-mtk.c USB
  controller (because the controller driver and our new, generic code
  now manage the PHYs). the PHY framework handles this gracefully (by
  ref-counting all operations). a follow-up series will be sent which
  removes the custom handling from the affected drivers (so only the
  new PHY wrapper will manage the PHYs state after that follow-up
  series).
- fixed an unnecessary whitespace change spotted by Alan Stern (thanks!)
  in patch #3
- removed all Tested-by's since we're changing code again

Changes since v6 at [6]:
- fixed unnecessary whitespace change (noticed by Alan Stern - thanks)
- added PATCH #4 to clarify (with an example) how I understood how Rob
  wanted the dt-binding to look like (see [0]). please wait for an ACK
  from Rob on this one to make sure that we're not introducing some
  broken binding (as discussed with Arnd: [7])
- added Alan Stern's ACK on PATCH #3
- added Neil's Tested-by on PATCH #2 and #3

Changes since RfC v5 at [5]:
- dropped RfC prefix
- removed noisy dev_err if no roothub node was found (spotted by
  Xiaolong Ye's kbuild test robot - thank you for that!)
- moved the call to usb_phy_roothub_power_off() within
  hcd_bus_suspend() to make sure that the PHYs are turned off
  if the "race with a root-hub wakeup event" condition is met (in
  this case the PHYs are turned on again, with the old code we did
  break the PHYs internal ref-counting because we never turned the
  PHYs off before turning them on again in case of that special
  "race with a root-hub wakeup event").
  additionally we're not handling the status returned by
  usb_phy_roothub_power_off() anymore (the bus is already turned off
  and we tried to turn off all PHYs as well - only the PHYs which
  failed to power off will stay in the current state).
  thanks to Alan Stern for the suggestion
- removed return value from usb_phy_roothub_power_off() because none
  of my code uses it anymore. thanks to Alan Stern for the suggestion

Changes since v4 at [4]:
- renamed the subject of the cover-letter (old name was:
  "initialize (multiple) PHYs in xhci-plat")
- back into RFC status (see below for the reasons)
- dropped Tested-by from Chunfeng Yun (same reasons as RFC status)
- reworded cover-letter and commit messages from "platform-roothub"
  to "roothub PHY wrapper"
- moved code from drivers/usb/host/platform-roothub.* to
  drivers/usb/core/phy.* and the changes to
  drivers/usb/host/xhci-plat.c to drivers/usb/core/hcd.c as suggested
  by Mathias Nyman (as a benefit this will enable the new logic for
  non-xHCI controllers as well - however this was not tested yet)
- rename the structs, function names, etc from platform_roothub_* to
  usb_phy_roothub*

Changes since RFCv3 at [3]:
- moved the DT binding change from patch #3 to patch #1 as suggested
  by Rob Herring (and slightly adjusted the commit message to account
  for that)
- added Tested-by from Chunfeng Yun (who confirmed that the whole
  concept and implementation works fine on Mediatek SoCs - many thanks
  again!) to patch #2
- added Rob Herring's ACK to patches 1 and 3
- dropped RFC status (RFCv3 -> PATCH v4)

Changes since RFCv2 at [2]:
- split phy_{init,exit} and phy_power_{on,off} handling. up until RFCv2
  I called phy_init plus phy_power_on in platform_roothub_power_on and
  phy_power_off plus phy_exit in platform_roothub_power_off. However,
  Chunfeng Yun (a Mediatek SoC developer - many thanks for testing my
  series and providing great feedback) reported that only using
  phy_power_off (and omitting phy_exit) during system suspend fixes an
  issue where USB devices would be re-enumerated when resuming. His
  original problem description: "In order to keep link state on mt8173,
  we just power off all phys(not exit) when system enter suspend, then
  power on them again (needn't init, otherwise device will be
  disconnected) when system resume, this can avoid re-enumerating
  device.". This fix affects patch #2 and #3 as we now have
  platform_roothub_init (which calls phy_init internally),
  platform_roothub_power_on (which calls phy_power_on internally),
  platform_roothub_power_off (which calls phy_power_off internally) and
  platform_roothub_exit (which calls phy_exit internally). suspend and
  resume only call platform_roothub_power_{on,off} to prevent the issue
  described by Chunfeng Yun (unfortunately I cannot test this because
  the Amlogic platform currently does not support system suspend).
- dropped two struct forward declarations from platform-roothub.h which
  are not used in the header file (thanks to Chunfeng Yun for spotting
  this)

Changes since RFCv1 at [1]:
- split the usb-xhci dt-binding documentation into a separate patch
- fixed a typo ("usb-phy" -> "phys" in the dt-binding example)
- rebased to apply against latest usb-next


[0] http://lists.infradead.org/pipermail/linux-amlogic/2016-November/001818.html
[1] http://marc.info/?l=linux-usb&m=148414866303604&w=2
[2] https://www.spinics.net/lists/linux-usb/msg158967.html
[3] https://www.spinics.net/lists/devicetree/msg190426.html
[4] http://lists.infradead.org/pipermail/linux-amlogic/2017-September/004685.html
[5] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/004924.html
[6] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005049.html
[7] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005121.html
[8] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005124.html
[9] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005192.html
[10] http://lists.infradead.org/pipermail/linux-amlogic/2018-January/006274.html
[11] https://marc.info/?l=linux-usb&m=151683941207898&w=2
[12] http://lists.infradead.org/pipermail/linux-amlogic/2018-February/006412.html

Martin Blumenstingl (8):
  dt-bindings: usb: add the documentation for USB HCDs
  usb: add a flag to skip PHY initialization to struct usb_hcd
  usb: core: add a wrapper for the USB PHYs on the HCD
  usb: core: hcd: integrate the PHY wrapper into the HCD core
  usb: host: xhci-mtk: remove custom USB PHY handling
  usb: host: ehci-platform: remove custom USB PHY handling
  usb: host: ohci-platform: remove custom USB PHY handling
  usb: core: hcd: remove support for initializing a single PHY

 .../devicetree/bindings/usb/mediatek,mtk-xhci.txt  |   5 +-
 .../devicetree/bindings/usb/mediatek,mtu3.txt      |   5 +-
 Documentation/devicetree/bindings/usb/usb-ehci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-hcd.txt  |   9 ++
 Documentation/devicetree/bindings/usb/usb-ohci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-uhci.txt |   3 +
 Documentation/devicetree/bindings/usb/usb-xhci.txt |   5 +
 drivers/usb/chipidea/host.c                        |   6 +-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hcd.c                             |  64 ++++-----
 drivers/usb/core/phy.c                             | 158 +++++++++++++++++++++
 drivers/usb/core/phy.h                             |   7 +
 drivers/usb/host/ehci-fsl.c                        |   2 +
 drivers/usb/host/ehci-platform.c                   |  55 +------
 drivers/usb/host/ehci-tegra.c                      |   1 +
 drivers/usb/host/ohci-omap.c                       |   1 +
 drivers/usb/host/ohci-platform.c                   |  56 +-------
 drivers/usb/host/xhci-mtk.c                        |  98 +------------
 drivers/usb/host/xhci-plat.c                       |   1 +
 include/linux/usb/hcd.h                            |   8 +-
 20 files changed, 253 insertions(+), 245 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

-- 
2.16.1

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 0/8] initialize (multiple) PHYs for a HCD
@ 2018-02-18 18:44 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linus-amlogic

The goal of this series is to initialize multiple PHYs on a USB host
controller, which is needed on Amlogic Meson GXL and GXM SoCs.
- Amlogic Meson GXL and GXM SoCs come with a dwc3 IP block which has two
  or three USB2 ports enabled on the internal root-hub. The SoCs also
  provide separate USB2 PHYs, one per port. All USB2 PHYs (which are
  internally "connected" to the dwc3 roothub) need to be powered on,
  otherwise USB devices cannot be enumerated (even if just one PHY is
  disabled and if the device is plugged into another, enabled port)
- dwc3 already takes one USB2 and one USB3 PHY and initializes these
  correctly
- some other HCI platform drivers (like ehci-platform.c, xhci-mtk.c and
  ohci-platform.c) do not have a limitation on the number of PHYs - they
  support any number of USB PHYs (typically one PHY per actual port)

This series adds a new "PHY wrapper". This can be configured through
devicetree by passing a "phys" property (with any number of PHY handles)
to the USB controller.
Additionally it this integrates this new PHY wrapper into hcd.c which
automatically enables it for all USB controller drivers (tested on an
Amlogic Meson GXL SoC which uses a dwc3 controller). Since this leaves
some duplicate code (which now becomes obsolete!) in various drivers
this is being cleaned up as well.


Changes since v9 at [12]:
- changed "skip_phy_initialization" from PATCH #2 into a bitfield and
  moved it below the "remove_phy" bitfield as suggested by Alan Stern
  (thanks!)
- rebased against usb-next (which is currently identical to v4.16-rc1)

Changes since v8 at [10]:
- add new patch #2 which allows disabling "USB PHY management" in the
  USB HCD core driver. this also affects patch #4 which now checks the
  skip_phy_initialization flag before calling
  usb_phy_roothub_{init,power_on} (other functions don't need to be
  guarded since they are null-safe - see patch #3)
- integrated my other series 'remove driver-specific "multiple PHY"
  handling' from [11] into this one so the result of this "generic"
  code (= removing duplicate code in various drivers) can be seen
  directly. affects patches #5-8. (note: patch #1 "usb: mtu3: remove
  custom USB PHY handling" from that series was dropped, because it
  broke device mode support - Chunfeng Yun will come up with a
  separate patch later. patch #5 "usb: chipidea: do not set the "phy"
  field in struct usb_hcd" from that series was rewritten and is now
  integrated into the new patch #2 from this series)
- collected all {Reviewed,Tested,Acked}-by
- dropped RfC prefix since this v8 was out for review for two weeks
  and it only got Reviewed-by's and Tested-by's since then

Changes since v7 at [8]:
- updated the description in the cover-letter
- wrapped the only call usb_phy_roothub_power_{on,off} in
  hcd_bus_{resume,suspend} during system-suspend (excluding it from
  being called during runtime/auto suspend). this also fixes a problem
  on Meson GXL which does not detect that a new device has been
  connected if the PHYs are powered off. Thanks to Manu Gautam for
  suggesting this! (affects patch #3)
- after a discussion about the dt-bindings of this patch I went back to
  simply specifying a "phys" property (without a corresponding phy-names
  property) to pass all PHYs directly in the node of the USB controller.
  From my understanding the root hub node was "artificial" and we should
  describe it's properties as part of the controller, rather than
  introducing a sub-node. see also the discussion with Arnd in [9]
  (this affects patch #1 and #2)
- dropped patch #4 ("dt-bindings: usb: xhci: include the roothub and a
  device in the example") because we're back to just specifying a "phys"
  property directly within the controller.
  NOTE: some HCI platform drivers are using the "phys" property as well.
  With this series phy_{init,power_on,power_off,exit} will be called
  twice for each PHY when using a ehci-platform.c or xhci-mtk.c USB
  controller (because the controller driver and our new, generic code
  now manage the PHYs). the PHY framework handles this gracefully (by
  ref-counting all operations). a follow-up series will be sent which
  removes the custom handling from the affected drivers (so only the
  new PHY wrapper will manage the PHYs state after that follow-up
  series).
- fixed an unnecessary whitespace change spotted by Alan Stern (thanks!)
  in patch #3
- removed all Tested-by's since we're changing code again

Changes since v6 at [6]:
- fixed unnecessary whitespace change (noticed by Alan Stern - thanks)
- added PATCH #4 to clarify (with an example) how I understood how Rob
  wanted the dt-binding to look like (see [0]). please wait for an ACK
  from Rob on this one to make sure that we're not introducing some
  broken binding (as discussed with Arnd: [7])
- added Alan Stern's ACK on PATCH #3
- added Neil's Tested-by on PATCH #2 and #3

Changes since RfC v5 at [5]:
- dropped RfC prefix
- removed noisy dev_err if no roothub node was found (spotted by
  Xiaolong Ye's kbuild test robot - thank you for that!)
- moved the call to usb_phy_roothub_power_off() within
  hcd_bus_suspend() to make sure that the PHYs are turned off
  if the "race with a root-hub wakeup event" condition is met (in
  this case the PHYs are turned on again, with the old code we did
  break the PHYs internal ref-counting because we never turned the
  PHYs off before turning them on again in case of that special
  "race with a root-hub wakeup event").
  additionally we're not handling the status returned by
  usb_phy_roothub_power_off() anymore (the bus is already turned off
  and we tried to turn off all PHYs as well - only the PHYs which
  failed to power off will stay in the current state).
  thanks to Alan Stern for the suggestion
- removed return value from usb_phy_roothub_power_off() because none
  of my code uses it anymore. thanks to Alan Stern for the suggestion

Changes since v4 at [4]:
- renamed the subject of the cover-letter (old name was:
  "initialize (multiple) PHYs in xhci-plat")
- back into RFC status (see below for the reasons)
- dropped Tested-by from Chunfeng Yun (same reasons as RFC status)
- reworded cover-letter and commit messages from "platform-roothub"
  to "roothub PHY wrapper"
- moved code from drivers/usb/host/platform-roothub.* to
  drivers/usb/core/phy.* and the changes to
  drivers/usb/host/xhci-plat.c to drivers/usb/core/hcd.c as suggested
  by Mathias Nyman (as a benefit this will enable the new logic for
  non-xHCI controllers as well - however this was not tested yet)
- rename the structs, function names, etc from platform_roothub_* to
  usb_phy_roothub*

Changes since RFCv3 at [3]:
- moved the DT binding change from patch #3 to patch #1 as suggested
  by Rob Herring (and slightly adjusted the commit message to account
  for that)
- added Tested-by from Chunfeng Yun (who confirmed that the whole
  concept and implementation works fine on Mediatek SoCs - many thanks
  again!) to patch #2
- added Rob Herring's ACK to patches 1 and 3
- dropped RFC status (RFCv3 -> PATCH v4)

Changes since RFCv2 at [2]:
- split phy_{init,exit} and phy_power_{on,off} handling. up until RFCv2
  I called phy_init plus phy_power_on in platform_roothub_power_on and
  phy_power_off plus phy_exit in platform_roothub_power_off. However,
  Chunfeng Yun (a Mediatek SoC developer - many thanks for testing my
  series and providing great feedback) reported that only using
  phy_power_off (and omitting phy_exit) during system suspend fixes an
  issue where USB devices would be re-enumerated when resuming. His
  original problem description: "In order to keep link state on mt8173,
  we just power off all phys(not exit) when system enter suspend, then
  power on them again (needn't init, otherwise device will be
  disconnected) when system resume, this can avoid re-enumerating
  device.". This fix affects patch #2 and #3 as we now have
  platform_roothub_init (which calls phy_init internally),
  platform_roothub_power_on (which calls phy_power_on internally),
  platform_roothub_power_off (which calls phy_power_off internally) and
  platform_roothub_exit (which calls phy_exit internally). suspend and
  resume only call platform_roothub_power_{on,off} to prevent the issue
  described by Chunfeng Yun (unfortunately I cannot test this because
  the Amlogic platform currently does not support system suspend).
- dropped two struct forward declarations from platform-roothub.h which
  are not used in the header file (thanks to Chunfeng Yun for spotting
  this)

Changes since RFCv1 at [1]:
- split the usb-xhci dt-binding documentation into a separate patch
- fixed a typo ("usb-phy" -> "phys" in the dt-binding example)
- rebased to apply against latest usb-next


[0] http://lists.infradead.org/pipermail/linux-amlogic/2016-November/001818.html
[1] http://marc.info/?l=linux-usb&m=148414866303604&w=2
[2] https://www.spinics.net/lists/linux-usb/msg158967.html
[3] https://www.spinics.net/lists/devicetree/msg190426.html
[4] http://lists.infradead.org/pipermail/linux-amlogic/2017-September/004685.html
[5] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/004924.html
[6] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005049.html
[7] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005121.html
[8] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005124.html
[9] http://lists.infradead.org/pipermail/linux-amlogic/2017-October/005192.html
[10] http://lists.infradead.org/pipermail/linux-amlogic/2018-January/006274.html
[11] https://marc.info/?l=linux-usb&m=151683941207898&w=2
[12] http://lists.infradead.org/pipermail/linux-amlogic/2018-February/006412.html

Martin Blumenstingl (8):
  dt-bindings: usb: add the documentation for USB HCDs
  usb: add a flag to skip PHY initialization to struct usb_hcd
  usb: core: add a wrapper for the USB PHYs on the HCD
  usb: core: hcd: integrate the PHY wrapper into the HCD core
  usb: host: xhci-mtk: remove custom USB PHY handling
  usb: host: ehci-platform: remove custom USB PHY handling
  usb: host: ohci-platform: remove custom USB PHY handling
  usb: core: hcd: remove support for initializing a single PHY

 .../devicetree/bindings/usb/mediatek,mtk-xhci.txt  |   5 +-
 .../devicetree/bindings/usb/mediatek,mtu3.txt      |   5 +-
 Documentation/devicetree/bindings/usb/usb-ehci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-hcd.txt  |   9 ++
 Documentation/devicetree/bindings/usb/usb-ohci.txt |   6 +-
 Documentation/devicetree/bindings/usb/usb-uhci.txt |   3 +
 Documentation/devicetree/bindings/usb/usb-xhci.txt |   5 +
 drivers/usb/chipidea/host.c                        |   6 +-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hcd.c                             |  64 ++++-----
 drivers/usb/core/phy.c                             | 158 +++++++++++++++++++++
 drivers/usb/core/phy.h                             |   7 +
 drivers/usb/host/ehci-fsl.c                        |   2 +
 drivers/usb/host/ehci-platform.c                   |  55 +------
 drivers/usb/host/ehci-tegra.c                      |   1 +
 drivers/usb/host/ohci-omap.c                       |   1 +
 drivers/usb/host/ohci-platform.c                   |  56 +-------
 drivers/usb/host/xhci-mtk.c                        |  98 +------------
 drivers/usb/host/xhci-plat.c                       |   1 +
 include/linux/usb/hcd.h                            |   8 +-
 20 files changed, 253 insertions(+), 245 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

-- 
2.16.1

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 1/8] dt-bindings: usb: add the documentation for USB HCDs
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

A USB HCD may have several PHYs which need to be configured before the
the HCD starts working.
This adds the documentation for such a USB HCD as well as a reference to
the new "usb-hcd.txt" from all bindings that implement a USB HCD which
support one USB PHY per port.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
Reviewed-by: Rob Herring <robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
---
 Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt | 5 ++++-
 Documentation/devicetree/bindings/usb/mediatek,mtu3.txt     | 5 ++++-
 Documentation/devicetree/bindings/usb/usb-ehci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-hcd.txt           | 9 +++++++++
 Documentation/devicetree/bindings/usb/usb-ohci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-uhci.txt          | 3 +++
 Documentation/devicetree/bindings/usb/usb-xhci.txt          | 5 +++++
 7 files changed, 33 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
index 88d9f4a4b280..266c2d917a28 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
@@ -32,7 +32,7 @@ Required properties:
 	"mcu_ck": mcu_bus clock for register access,
 	"dma_ck": dma_bus clock for data transfer by DMA
 
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
 
 Optional properties:
  - wakeup-source : enable USB remote wakeup;
@@ -52,6 +52,9 @@ Optional properties:
 	See: Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
  - imod-interval-ns: default interrupt moderation interval is 5000ns
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 usb30: usb@11270000 {
 	compatible = "mediatek,mt8173-xhci";
diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
index d589a1ef96a1..3382b5cb471d 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
@@ -17,7 +17,7 @@ Required properties:
  - clock-names : must contain "sys_ck" for clock of controller,
 	the following clocks are optional:
 	"ref_ck", "mcu_ck" and "dam_ck";
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
  - dr_mode : should be one of "host", "peripheral" or "otg",
 	refer to usb/generic.txt
 
@@ -53,6 +53,9 @@ Optional properties:
  - mediatek,u3p-dis-msk : mask to disable u3ports, bit0 for u3port0,
 	bit1 for u3port1, ... etc;
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Sub-nodes:
 The xhci should be added as subnode to mtu3 as shown in the following example
 if host mode is enabled. The DT binding details of xhci can be found in:
diff --git a/Documentation/devicetree/bindings/usb/usb-ehci.txt b/Documentation/devicetree/bindings/usb/usb-ehci.txt
index 3efde12b5d68..0f1b75386207 100644
--- a/Documentation/devicetree/bindings/usb/usb-ehci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ehci.txt
@@ -16,10 +16,12 @@ Optional properties:
  - has-transaction-translator : boolean, set this if EHCI have a Transaction
 				Translator built into the root hub.
  - clocks : a list of phandle + clock specifier pairs
- - phys : phandle + phy specifier pair
- - phy-names : "usb"
+ - phys : see usb-hcd.txt in the current directory
  - resets : phandle + reset specifier pair
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example (Sequoia 440EPx):
     ehci@e0000300 {
 	   compatible = "ibm,usb-ehci-440epx", "usb-ehci";
diff --git a/Documentation/devicetree/bindings/usb/usb-hcd.txt b/Documentation/devicetree/bindings/usb/usb-hcd.txt
new file mode 100644
index 000000000000..50529b838c9c
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/usb-hcd.txt
@@ -0,0 +1,9 @@
+Generic USB HCD (Host Controller Device) Properties
+
+Optional properties:
+- phys: a list of all USB PHYs on this HCD
+
+Example:
+	&usb1 {
+		phys = <&usb2_phy1>, <&usb3_phy1>;
+	};
diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
index 09e70c875bc6..a8d2103d1f3d 100644
--- a/Documentation/devicetree/bindings/usb/usb-ohci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
@@ -13,10 +13,12 @@ Optional properties:
 - remote-wakeup-connected: remote wakeup is wired on the platform
 - num-ports : u32, to override the detected port count
 - clocks : a list of phandle + clock specifier pairs
-- phys : phandle + phy specifier pair
-- phy-names : "usb"
+- phys : see usb-hcd.txt in the current directory
 - resets : a list of phandle + reset specifier pairs
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	ohci0: usb@1c14400 {
diff --git a/Documentation/devicetree/bindings/usb/usb-uhci.txt b/Documentation/devicetree/bindings/usb/usb-uhci.txt
index 298133416c97..cc2e6f7d602e 100644
--- a/Documentation/devicetree/bindings/usb/usb-uhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-uhci.txt
@@ -6,6 +6,9 @@ Required properties:
 - reg : Should contain 1 register ranges(address and length)
 - interrupts : UHCI controller interrupt
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	uhci@d8007b00 {
diff --git a/Documentation/devicetree/bindings/usb/usb-xhci.txt b/Documentation/devicetree/bindings/usb/usb-xhci.txt
index e2ea59bbca93..2f7663bb69ff 100644
--- a/Documentation/devicetree/bindings/usb/usb-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-xhci.txt
@@ -32,6 +32,11 @@ Optional properties:
   - usb3-lpm-capable: determines if platform is USB3 LPM capable
   - quirk-broken-port-ped: set if the controller has broken port disable mechanism
   - imod-interval-ns: default interrupt moderation interval is 5000ns
+  - phys : see usb-hcd.txt in the current directory
+
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 
 Example:
 	usb@f0931000 {
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,1/8] dt-bindings: usb: add the documentation for USB HCDs
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

A USB HCD may have several PHYs which need to be configured before the
the HCD starts working.
This adds the documentation for such a USB HCD as well as a reference to
the new "usb-hcd.txt" from all bindings that implement a USB HCD which
support one USB PHY per port.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt | 5 ++++-
 Documentation/devicetree/bindings/usb/mediatek,mtu3.txt     | 5 ++++-
 Documentation/devicetree/bindings/usb/usb-ehci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-hcd.txt           | 9 +++++++++
 Documentation/devicetree/bindings/usb/usb-ohci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-uhci.txt          | 3 +++
 Documentation/devicetree/bindings/usb/usb-xhci.txt          | 5 +++++
 7 files changed, 33 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
index 88d9f4a4b280..266c2d917a28 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
@@ -32,7 +32,7 @@ Required properties:
 	"mcu_ck": mcu_bus clock for register access,
 	"dma_ck": dma_bus clock for data transfer by DMA
 
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
 
 Optional properties:
  - wakeup-source : enable USB remote wakeup;
@@ -52,6 +52,9 @@ Optional properties:
 	See: Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
  - imod-interval-ns: default interrupt moderation interval is 5000ns
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 usb30: usb@11270000 {
 	compatible = "mediatek,mt8173-xhci";
diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
index d589a1ef96a1..3382b5cb471d 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
@@ -17,7 +17,7 @@ Required properties:
  - clock-names : must contain "sys_ck" for clock of controller,
 	the following clocks are optional:
 	"ref_ck", "mcu_ck" and "dam_ck";
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
  - dr_mode : should be one of "host", "peripheral" or "otg",
 	refer to usb/generic.txt
 
@@ -53,6 +53,9 @@ Optional properties:
  - mediatek,u3p-dis-msk : mask to disable u3ports, bit0 for u3port0,
 	bit1 for u3port1, ... etc;
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Sub-nodes:
 The xhci should be added as subnode to mtu3 as shown in the following example
 if host mode is enabled. The DT binding details of xhci can be found in:
diff --git a/Documentation/devicetree/bindings/usb/usb-ehci.txt b/Documentation/devicetree/bindings/usb/usb-ehci.txt
index 3efde12b5d68..0f1b75386207 100644
--- a/Documentation/devicetree/bindings/usb/usb-ehci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ehci.txt
@@ -16,10 +16,12 @@ Optional properties:
  - has-transaction-translator : boolean, set this if EHCI have a Transaction
 				Translator built into the root hub.
  - clocks : a list of phandle + clock specifier pairs
- - phys : phandle + phy specifier pair
- - phy-names : "usb"
+ - phys : see usb-hcd.txt in the current directory
  - resets : phandle + reset specifier pair
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example (Sequoia 440EPx):
     ehci@e0000300 {
 	   compatible = "ibm,usb-ehci-440epx", "usb-ehci";
diff --git a/Documentation/devicetree/bindings/usb/usb-hcd.txt b/Documentation/devicetree/bindings/usb/usb-hcd.txt
new file mode 100644
index 000000000000..50529b838c9c
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/usb-hcd.txt
@@ -0,0 +1,9 @@
+Generic USB HCD (Host Controller Device) Properties
+
+Optional properties:
+- phys: a list of all USB PHYs on this HCD
+
+Example:
+	&usb1 {
+		phys = <&usb2_phy1>, <&usb3_phy1>;
+	};
diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
index 09e70c875bc6..a8d2103d1f3d 100644
--- a/Documentation/devicetree/bindings/usb/usb-ohci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
@@ -13,10 +13,12 @@ Optional properties:
 - remote-wakeup-connected: remote wakeup is wired on the platform
 - num-ports : u32, to override the detected port count
 - clocks : a list of phandle + clock specifier pairs
-- phys : phandle + phy specifier pair
-- phy-names : "usb"
+- phys : see usb-hcd.txt in the current directory
 - resets : a list of phandle + reset specifier pairs
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	ohci0: usb@1c14400 {
diff --git a/Documentation/devicetree/bindings/usb/usb-uhci.txt b/Documentation/devicetree/bindings/usb/usb-uhci.txt
index 298133416c97..cc2e6f7d602e 100644
--- a/Documentation/devicetree/bindings/usb/usb-uhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-uhci.txt
@@ -6,6 +6,9 @@ Required properties:
 - reg : Should contain 1 register ranges(address and length)
 - interrupts : UHCI controller interrupt
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	uhci@d8007b00 {
diff --git a/Documentation/devicetree/bindings/usb/usb-xhci.txt b/Documentation/devicetree/bindings/usb/usb-xhci.txt
index e2ea59bbca93..2f7663bb69ff 100644
--- a/Documentation/devicetree/bindings/usb/usb-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-xhci.txt
@@ -32,6 +32,11 @@ Optional properties:
   - usb3-lpm-capable: determines if platform is USB3 LPM capable
   - quirk-broken-port-ped: set if the controller has broken port disable mechanism
   - imod-interval-ns: default interrupt moderation interval is 5000ns
+  - phys : see usb-hcd.txt in the current directory
+
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 
 Example:
 	usb@f0931000 {

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 1/8] dt-bindings: usb: add the documentation for USB HCDs
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-arm-kernel

A USB HCD may have several PHYs which need to be configured before the
the HCD starts working.
This adds the documentation for such a USB HCD as well as a reference to
the new "usb-hcd.txt" from all bindings that implement a USB HCD which
support one USB PHY per port.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt | 5 ++++-
 Documentation/devicetree/bindings/usb/mediatek,mtu3.txt     | 5 ++++-
 Documentation/devicetree/bindings/usb/usb-ehci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-hcd.txt           | 9 +++++++++
 Documentation/devicetree/bindings/usb/usb-ohci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-uhci.txt          | 3 +++
 Documentation/devicetree/bindings/usb/usb-xhci.txt          | 5 +++++
 7 files changed, 33 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
index 88d9f4a4b280..266c2d917a28 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
@@ -32,7 +32,7 @@ Required properties:
 	"mcu_ck": mcu_bus clock for register access,
 	"dma_ck": dma_bus clock for data transfer by DMA
 
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
 
 Optional properties:
  - wakeup-source : enable USB remote wakeup;
@@ -52,6 +52,9 @@ Optional properties:
 	See: Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
  - imod-interval-ns: default interrupt moderation interval is 5000ns
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 usb30: usb at 11270000 {
 	compatible = "mediatek,mt8173-xhci";
diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
index d589a1ef96a1..3382b5cb471d 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
@@ -17,7 +17,7 @@ Required properties:
  - clock-names : must contain "sys_ck" for clock of controller,
 	the following clocks are optional:
 	"ref_ck", "mcu_ck" and "dam_ck";
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
  - dr_mode : should be one of "host", "peripheral" or "otg",
 	refer to usb/generic.txt
 
@@ -53,6 +53,9 @@ Optional properties:
  - mediatek,u3p-dis-msk : mask to disable u3ports, bit0 for u3port0,
 	bit1 for u3port1, ... etc;
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Sub-nodes:
 The xhci should be added as subnode to mtu3 as shown in the following example
 if host mode is enabled. The DT binding details of xhci can be found in:
diff --git a/Documentation/devicetree/bindings/usb/usb-ehci.txt b/Documentation/devicetree/bindings/usb/usb-ehci.txt
index 3efde12b5d68..0f1b75386207 100644
--- a/Documentation/devicetree/bindings/usb/usb-ehci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ehci.txt
@@ -16,10 +16,12 @@ Optional properties:
  - has-transaction-translator : boolean, set this if EHCI have a Transaction
 				Translator built into the root hub.
  - clocks : a list of phandle + clock specifier pairs
- - phys : phandle + phy specifier pair
- - phy-names : "usb"
+ - phys : see usb-hcd.txt in the current directory
  - resets : phandle + reset specifier pair
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example (Sequoia 440EPx):
     ehci at e0000300 {
 	   compatible = "ibm,usb-ehci-440epx", "usb-ehci";
diff --git a/Documentation/devicetree/bindings/usb/usb-hcd.txt b/Documentation/devicetree/bindings/usb/usb-hcd.txt
new file mode 100644
index 000000000000..50529b838c9c
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/usb-hcd.txt
@@ -0,0 +1,9 @@
+Generic USB HCD (Host Controller Device) Properties
+
+Optional properties:
+- phys: a list of all USB PHYs on this HCD
+
+Example:
+	&usb1 {
+		phys = <&usb2_phy1>, <&usb3_phy1>;
+	};
diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
index 09e70c875bc6..a8d2103d1f3d 100644
--- a/Documentation/devicetree/bindings/usb/usb-ohci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
@@ -13,10 +13,12 @@ Optional properties:
 - remote-wakeup-connected: remote wakeup is wired on the platform
 - num-ports : u32, to override the detected port count
 - clocks : a list of phandle + clock specifier pairs
-- phys : phandle + phy specifier pair
-- phy-names : "usb"
+- phys : see usb-hcd.txt in the current directory
 - resets : a list of phandle + reset specifier pairs
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	ohci0: usb at 1c14400 {
diff --git a/Documentation/devicetree/bindings/usb/usb-uhci.txt b/Documentation/devicetree/bindings/usb/usb-uhci.txt
index 298133416c97..cc2e6f7d602e 100644
--- a/Documentation/devicetree/bindings/usb/usb-uhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-uhci.txt
@@ -6,6 +6,9 @@ Required properties:
 - reg : Should contain 1 register ranges(address and length)
 - interrupts : UHCI controller interrupt
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	uhci at d8007b00 {
diff --git a/Documentation/devicetree/bindings/usb/usb-xhci.txt b/Documentation/devicetree/bindings/usb/usb-xhci.txt
index e2ea59bbca93..2f7663bb69ff 100644
--- a/Documentation/devicetree/bindings/usb/usb-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-xhci.txt
@@ -32,6 +32,11 @@ Optional properties:
   - usb3-lpm-capable: determines if platform is USB3 LPM capable
   - quirk-broken-port-ped: set if the controller has broken port disable mechanism
   - imod-interval-ns: default interrupt moderation interval is 5000ns
+  - phys : see usb-hcd.txt in the current directory
+
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 
 Example:
 	usb at f0931000 {
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 1/8] dt-bindings: usb: add the documentation for USB HCDs
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linus-amlogic

A USB HCD may have several PHYs which need to be configured before the
the HCD starts working.
This adds the documentation for such a USB HCD as well as a reference to
the new "usb-hcd.txt" from all bindings that implement a USB HCD which
support one USB PHY per port.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt | 5 ++++-
 Documentation/devicetree/bindings/usb/mediatek,mtu3.txt     | 5 ++++-
 Documentation/devicetree/bindings/usb/usb-ehci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-hcd.txt           | 9 +++++++++
 Documentation/devicetree/bindings/usb/usb-ohci.txt          | 6 ++++--
 Documentation/devicetree/bindings/usb/usb-uhci.txt          | 3 +++
 Documentation/devicetree/bindings/usb/usb-xhci.txt          | 5 +++++
 7 files changed, 33 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/usb-hcd.txt

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
index 88d9f4a4b280..266c2d917a28 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtk-xhci.txt
@@ -32,7 +32,7 @@ Required properties:
 	"mcu_ck": mcu_bus clock for register access,
 	"dma_ck": dma_bus clock for data transfer by DMA
 
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
 
 Optional properties:
  - wakeup-source : enable USB remote wakeup;
@@ -52,6 +52,9 @@ Optional properties:
 	See: Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
  - imod-interval-ns: default interrupt moderation interval is 5000ns
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 usb30: usb at 11270000 {
 	compatible = "mediatek,mt8173-xhci";
diff --git a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
index d589a1ef96a1..3382b5cb471d 100644
--- a/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
+++ b/Documentation/devicetree/bindings/usb/mediatek,mtu3.txt
@@ -17,7 +17,7 @@ Required properties:
  - clock-names : must contain "sys_ck" for clock of controller,
 	the following clocks are optional:
 	"ref_ck", "mcu_ck" and "dam_ck";
- - phys : a list of phandle + phy specifier pairs
+ - phys : see usb-hcd.txt in the current directory
  - dr_mode : should be one of "host", "peripheral" or "otg",
 	refer to usb/generic.txt
 
@@ -53,6 +53,9 @@ Optional properties:
  - mediatek,u3p-dis-msk : mask to disable u3ports, bit0 for u3port0,
 	bit1 for u3port1, ... etc;
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Sub-nodes:
 The xhci should be added as subnode to mtu3 as shown in the following example
 if host mode is enabled. The DT binding details of xhci can be found in:
diff --git a/Documentation/devicetree/bindings/usb/usb-ehci.txt b/Documentation/devicetree/bindings/usb/usb-ehci.txt
index 3efde12b5d68..0f1b75386207 100644
--- a/Documentation/devicetree/bindings/usb/usb-ehci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ehci.txt
@@ -16,10 +16,12 @@ Optional properties:
  - has-transaction-translator : boolean, set this if EHCI have a Transaction
 				Translator built into the root hub.
  - clocks : a list of phandle + clock specifier pairs
- - phys : phandle + phy specifier pair
- - phy-names : "usb"
+ - phys : see usb-hcd.txt in the current directory
  - resets : phandle + reset specifier pair
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example (Sequoia 440EPx):
     ehci at e0000300 {
 	   compatible = "ibm,usb-ehci-440epx", "usb-ehci";
diff --git a/Documentation/devicetree/bindings/usb/usb-hcd.txt b/Documentation/devicetree/bindings/usb/usb-hcd.txt
new file mode 100644
index 000000000000..50529b838c9c
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/usb-hcd.txt
@@ -0,0 +1,9 @@
+Generic USB HCD (Host Controller Device) Properties
+
+Optional properties:
+- phys: a list of all USB PHYs on this HCD
+
+Example:
+	&usb1 {
+		phys = <&usb2_phy1>, <&usb3_phy1>;
+	};
diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
index 09e70c875bc6..a8d2103d1f3d 100644
--- a/Documentation/devicetree/bindings/usb/usb-ohci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
@@ -13,10 +13,12 @@ Optional properties:
 - remote-wakeup-connected: remote wakeup is wired on the platform
 - num-ports : u32, to override the detected port count
 - clocks : a list of phandle + clock specifier pairs
-- phys : phandle + phy specifier pair
-- phy-names : "usb"
+- phys : see usb-hcd.txt in the current directory
 - resets : a list of phandle + reset specifier pairs
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	ohci0: usb at 1c14400 {
diff --git a/Documentation/devicetree/bindings/usb/usb-uhci.txt b/Documentation/devicetree/bindings/usb/usb-uhci.txt
index 298133416c97..cc2e6f7d602e 100644
--- a/Documentation/devicetree/bindings/usb/usb-uhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-uhci.txt
@@ -6,6 +6,9 @@ Required properties:
 - reg : Should contain 1 register ranges(address and length)
 - interrupts : UHCI controller interrupt
 
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 Example:
 
 	uhci at d8007b00 {
diff --git a/Documentation/devicetree/bindings/usb/usb-xhci.txt b/Documentation/devicetree/bindings/usb/usb-xhci.txt
index e2ea59bbca93..2f7663bb69ff 100644
--- a/Documentation/devicetree/bindings/usb/usb-xhci.txt
+++ b/Documentation/devicetree/bindings/usb/usb-xhci.txt
@@ -32,6 +32,11 @@ Optional properties:
   - usb3-lpm-capable: determines if platform is USB3 LPM capable
   - quirk-broken-port-ped: set if the controller has broken port disable mechanism
   - imod-interval-ns: default interrupt moderation interval is 5000ns
+  - phys : see usb-hcd.txt in the current directory
+
+additionally the properties from usb-hcd.txt (in the current directory) are
+supported.
+
 
 Example:
 	usb at f0931000 {
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

The USB HCD core driver parses the device-tree node for "phys" and
"usb-phys" properties. It also manages the power state of these PHYs
automatically.
However, drivers may opt-out of this behavior by setting "phy" or
"usb_phy" in struct usb_hcd to a non-null value. An example where this
is required is the "Qualcomm USB2 controller", implemented by the
chipidea driver. The hardware requires that the PHY is only powered on
after the "reset completed" event from the controller is received.

A follow-up patch will allow the USB HCD core driver to manage more than
one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
so drivers can opt-out of any PHY management provided by the USB HCD
core driver.

This also updates the existing drivers so they use the new flag if they
want to opt out of the PHY management provided by the USB HCD core
driver. This means that for these drivers the new "multiple PHY"
handling (which will be added in a follow-up patch) will be disabled as
well.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
---
 drivers/usb/chipidea/host.c      | 6 ++----
 drivers/usb/core/hcd.c           | 4 ++--
 drivers/usb/host/ehci-fsl.c      | 2 ++
 drivers/usb/host/ehci-platform.c | 4 ++--
 drivers/usb/host/ehci-tegra.c    | 1 +
 drivers/usb/host/ohci-omap.c     | 1 +
 drivers/usb/host/ohci-platform.c | 4 ++--
 drivers/usb/host/xhci-plat.c     | 1 +
 include/linux/usb/hcd.h          | 6 ++++++
 9 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
index 19d60ed7e41f..af45aa3222b5 100644
--- a/drivers/usb/chipidea/host.c
+++ b/drivers/usb/chipidea/host.c
@@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
 
 	hcd->power_budget = ci->platdata->power_budget;
 	hcd->tpl_support = ci->platdata->tpl_support;
-	if (ci->phy)
-		hcd->phy = ci->phy;
-	else
-		hcd->usb_phy = ci->usb_phy;
+	if (ci->phy || ci->usb_phy)
+		hcd->skip_phy_initialization = 1;
 
 	ehci = hcd_to_ehci(hcd);
 	ehci->caps = ci->hw_bank.cap;
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index fc32391a34d5..f2307470a31e 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	int retval;
 	struct usb_device *rhdev;
 
-	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
+	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
 		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
 
 		if (IS_ERR(phy)) {
@@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
+	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
 		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
 
 		if (IS_ERR(phy)) {
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index c5094cb88cd5..0a9fd2022acf 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
 			retval = -ENODEV;
 			goto err2;
 		}
+
+		hcd->skip_phy_initialization = 1;
 	}
 #endif
 	return retval;
diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b065a960adc2..b91eea8c73ae 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 					goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
index c809f7d2f08f..a6f4389f7e88 100644
--- a/drivers/usb/host/ehci-tegra.c
+++ b/drivers/usb/host/ehci-tegra.c
@@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
 		goto cleanup_clk_en;
 	}
 	hcd->usb_phy = u_phy;
+	hcd->skip_phy_initialization = 1;
 
 	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
 		"nvidia,needs-double-reset");
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 0201c49bc4fc..d8d35d456456 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 		} else {
 			return -EPROBE_DEFER;
 		}
+		hcd->skip_phy_initialization = 1;
 		ohci->start_hnp = start_hnp;
 	}
 #endif
diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 1e6c954f4b3f..62ef36a9333f 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 				goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 6f038306c14d..6700e5ee82ad 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
 		ret = usb_phy_init(hcd->usb_phy);
 		if (ret)
 			goto put_usb3_hcd;
+		hcd->skip_phy_initialization = 1;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 176900528822..693502c84c04 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -151,6 +151,12 @@ struct usb_hcd {
 	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
 	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
 	unsigned		remove_phy:1;	/* auto-remove USB phy */
+	/*
+	 * do not manage the PHY state in the HCD core, instead let the driver
+	 * handle this (for example if the PHY can only be turned on after a
+	 * specific event)
+	 */
+	unsigned		skip_phy_initialization:1;
 
 	/* The next flag is a stopgap, to be removed when all the HCDs
 	 * support the new root-hub polling mechanism. */
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

The USB HCD core driver parses the device-tree node for "phys" and
"usb-phys" properties. It also manages the power state of these PHYs
automatically.
However, drivers may opt-out of this behavior by setting "phy" or
"usb_phy" in struct usb_hcd to a non-null value. An example where this
is required is the "Qualcomm USB2 controller", implemented by the
chipidea driver. The hardware requires that the PHY is only powered on
after the "reset completed" event from the controller is received.

A follow-up patch will allow the USB HCD core driver to manage more than
one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
so drivers can opt-out of any PHY management provided by the USB HCD
core driver.

This also updates the existing drivers so they use the new flag if they
want to opt out of the PHY management provided by the USB HCD core
driver. This means that for these drivers the new "multiple PHY"
handling (which will be added in a follow-up patch) will be disabled as
well.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/chipidea/host.c      | 6 ++----
 drivers/usb/core/hcd.c           | 4 ++--
 drivers/usb/host/ehci-fsl.c      | 2 ++
 drivers/usb/host/ehci-platform.c | 4 ++--
 drivers/usb/host/ehci-tegra.c    | 1 +
 drivers/usb/host/ohci-omap.c     | 1 +
 drivers/usb/host/ohci-platform.c | 4 ++--
 drivers/usb/host/xhci-plat.c     | 1 +
 include/linux/usb/hcd.h          | 6 ++++++
 9 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
index 19d60ed7e41f..af45aa3222b5 100644
--- a/drivers/usb/chipidea/host.c
+++ b/drivers/usb/chipidea/host.c
@@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
 
 	hcd->power_budget = ci->platdata->power_budget;
 	hcd->tpl_support = ci->platdata->tpl_support;
-	if (ci->phy)
-		hcd->phy = ci->phy;
-	else
-		hcd->usb_phy = ci->usb_phy;
+	if (ci->phy || ci->usb_phy)
+		hcd->skip_phy_initialization = 1;
 
 	ehci = hcd_to_ehci(hcd);
 	ehci->caps = ci->hw_bank.cap;
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index fc32391a34d5..f2307470a31e 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	int retval;
 	struct usb_device *rhdev;
 
-	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
+	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
 		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
 
 		if (IS_ERR(phy)) {
@@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
+	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
 		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
 
 		if (IS_ERR(phy)) {
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index c5094cb88cd5..0a9fd2022acf 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
 			retval = -ENODEV;
 			goto err2;
 		}
+
+		hcd->skip_phy_initialization = 1;
 	}
 #endif
 	return retval;
diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b065a960adc2..b91eea8c73ae 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 					goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
index c809f7d2f08f..a6f4389f7e88 100644
--- a/drivers/usb/host/ehci-tegra.c
+++ b/drivers/usb/host/ehci-tegra.c
@@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
 		goto cleanup_clk_en;
 	}
 	hcd->usb_phy = u_phy;
+	hcd->skip_phy_initialization = 1;
 
 	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
 		"nvidia,needs-double-reset");
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 0201c49bc4fc..d8d35d456456 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 		} else {
 			return -EPROBE_DEFER;
 		}
+		hcd->skip_phy_initialization = 1;
 		ohci->start_hnp = start_hnp;
 	}
 #endif
diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 1e6c954f4b3f..62ef36a9333f 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 				goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 6f038306c14d..6700e5ee82ad 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
 		ret = usb_phy_init(hcd->usb_phy);
 		if (ret)
 			goto put_usb3_hcd;
+		hcd->skip_phy_initialization = 1;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 176900528822..693502c84c04 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -151,6 +151,12 @@ struct usb_hcd {
 	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
 	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
 	unsigned		remove_phy:1;	/* auto-remove USB phy */
+	/*
+	 * do not manage the PHY state in the HCD core, instead let the driver
+	 * handle this (for example if the PHY can only be turned on after a
+	 * specific event)
+	 */
+	unsigned		skip_phy_initialization:1;
 
 	/* The next flag is a stopgap, to be removed when all the HCDs
 	 * support the new root-hub polling mechanism. */

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-arm-kernel

The USB HCD core driver parses the device-tree node for "phys" and
"usb-phys" properties. It also manages the power state of these PHYs
automatically.
However, drivers may opt-out of this behavior by setting "phy" or
"usb_phy" in struct usb_hcd to a non-null value. An example where this
is required is the "Qualcomm USB2 controller", implemented by the
chipidea driver. The hardware requires that the PHY is only powered on
after the "reset completed" event from the controller is received.

A follow-up patch will allow the USB HCD core driver to manage more than
one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
so drivers can opt-out of any PHY management provided by the USB HCD
core driver.

This also updates the existing drivers so they use the new flag if they
want to opt out of the PHY management provided by the USB HCD core
driver. This means that for these drivers the new "multiple PHY"
handling (which will be added in a follow-up patch) will be disabled as
well.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/chipidea/host.c      | 6 ++----
 drivers/usb/core/hcd.c           | 4 ++--
 drivers/usb/host/ehci-fsl.c      | 2 ++
 drivers/usb/host/ehci-platform.c | 4 ++--
 drivers/usb/host/ehci-tegra.c    | 1 +
 drivers/usb/host/ohci-omap.c     | 1 +
 drivers/usb/host/ohci-platform.c | 4 ++--
 drivers/usb/host/xhci-plat.c     | 1 +
 include/linux/usb/hcd.h          | 6 ++++++
 9 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
index 19d60ed7e41f..af45aa3222b5 100644
--- a/drivers/usb/chipidea/host.c
+++ b/drivers/usb/chipidea/host.c
@@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
 
 	hcd->power_budget = ci->platdata->power_budget;
 	hcd->tpl_support = ci->platdata->tpl_support;
-	if (ci->phy)
-		hcd->phy = ci->phy;
-	else
-		hcd->usb_phy = ci->usb_phy;
+	if (ci->phy || ci->usb_phy)
+		hcd->skip_phy_initialization = 1;
 
 	ehci = hcd_to_ehci(hcd);
 	ehci->caps = ci->hw_bank.cap;
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index fc32391a34d5..f2307470a31e 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	int retval;
 	struct usb_device *rhdev;
 
-	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
+	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
 		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
 
 		if (IS_ERR(phy)) {
@@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
+	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
 		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
 
 		if (IS_ERR(phy)) {
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index c5094cb88cd5..0a9fd2022acf 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
 			retval = -ENODEV;
 			goto err2;
 		}
+
+		hcd->skip_phy_initialization = 1;
 	}
 #endif
 	return retval;
diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b065a960adc2..b91eea8c73ae 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 					goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
index c809f7d2f08f..a6f4389f7e88 100644
--- a/drivers/usb/host/ehci-tegra.c
+++ b/drivers/usb/host/ehci-tegra.c
@@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
 		goto cleanup_clk_en;
 	}
 	hcd->usb_phy = u_phy;
+	hcd->skip_phy_initialization = 1;
 
 	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
 		"nvidia,needs-double-reset");
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 0201c49bc4fc..d8d35d456456 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 		} else {
 			return -EPROBE_DEFER;
 		}
+		hcd->skip_phy_initialization = 1;
 		ohci->start_hnp = start_hnp;
 	}
 #endif
diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 1e6c954f4b3f..62ef36a9333f 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 				goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 6f038306c14d..6700e5ee82ad 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
 		ret = usb_phy_init(hcd->usb_phy);
 		if (ret)
 			goto put_usb3_hcd;
+		hcd->skip_phy_initialization = 1;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 176900528822..693502c84c04 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -151,6 +151,12 @@ struct usb_hcd {
 	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
 	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
 	unsigned		remove_phy:1;	/* auto-remove USB phy */
+	/*
+	 * do not manage the PHY state in the HCD core, instead let the driver
+	 * handle this (for example if the PHY can only be turned on after a
+	 * specific event)
+	 */
+	unsigned		skip_phy_initialization:1;
 
 	/* The next flag is a stopgap, to be removed when all the HCDs
 	 * support the new root-hub polling mechanism. */
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linus-amlogic

The USB HCD core driver parses the device-tree node for "phys" and
"usb-phys" properties. It also manages the power state of these PHYs
automatically.
However, drivers may opt-out of this behavior by setting "phy" or
"usb_phy" in struct usb_hcd to a non-null value. An example where this
is required is the "Qualcomm USB2 controller", implemented by the
chipidea driver. The hardware requires that the PHY is only powered on
after the "reset completed" event from the controller is received.

A follow-up patch will allow the USB HCD core driver to manage more than
one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
so drivers can opt-out of any PHY management provided by the USB HCD
core driver.

This also updates the existing drivers so they use the new flag if they
want to opt out of the PHY management provided by the USB HCD core
driver. This means that for these drivers the new "multiple PHY"
handling (which will be added in a follow-up patch) will be disabled as
well.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/chipidea/host.c      | 6 ++----
 drivers/usb/core/hcd.c           | 4 ++--
 drivers/usb/host/ehci-fsl.c      | 2 ++
 drivers/usb/host/ehci-platform.c | 4 ++--
 drivers/usb/host/ehci-tegra.c    | 1 +
 drivers/usb/host/ohci-omap.c     | 1 +
 drivers/usb/host/ohci-platform.c | 4 ++--
 drivers/usb/host/xhci-plat.c     | 1 +
 include/linux/usb/hcd.h          | 6 ++++++
 9 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
index 19d60ed7e41f..af45aa3222b5 100644
--- a/drivers/usb/chipidea/host.c
+++ b/drivers/usb/chipidea/host.c
@@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
 
 	hcd->power_budget = ci->platdata->power_budget;
 	hcd->tpl_support = ci->platdata->tpl_support;
-	if (ci->phy)
-		hcd->phy = ci->phy;
-	else
-		hcd->usb_phy = ci->usb_phy;
+	if (ci->phy || ci->usb_phy)
+		hcd->skip_phy_initialization = 1;
 
 	ehci = hcd_to_ehci(hcd);
 	ehci->caps = ci->hw_bank.cap;
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index fc32391a34d5..f2307470a31e 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	int retval;
 	struct usb_device *rhdev;
 
-	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
+	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
 		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
 
 		if (IS_ERR(phy)) {
@@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
+	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
 		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
 
 		if (IS_ERR(phy)) {
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index c5094cb88cd5..0a9fd2022acf 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
 			retval = -ENODEV;
 			goto err2;
 		}
+
+		hcd->skip_phy_initialization = 1;
 	}
 #endif
 	return retval;
diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b065a960adc2..b91eea8c73ae 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 					goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
index c809f7d2f08f..a6f4389f7e88 100644
--- a/drivers/usb/host/ehci-tegra.c
+++ b/drivers/usb/host/ehci-tegra.c
@@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
 		goto cleanup_clk_en;
 	}
 	hcd->usb_phy = u_phy;
+	hcd->skip_phy_initialization = 1;
 
 	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
 		"nvidia,needs-double-reset");
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 0201c49bc4fc..d8d35d456456 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 		} else {
 			return -EPROBE_DEFER;
 		}
+		hcd->skip_phy_initialization = 1;
 		ohci->start_hnp = start_hnp;
 	}
 #endif
diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 1e6c954f4b3f..62ef36a9333f 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
 			if (IS_ERR(priv->phys[phy_num])) {
 				err = PTR_ERR(priv->phys[phy_num]);
 				goto err_put_hcd;
-			} else if (!hcd->phy) {
+			} else {
 				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->phy = priv->phys[phy_num];
+				hcd->skip_phy_initialization = 1;
 			}
 		}
 
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 6f038306c14d..6700e5ee82ad 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
 		ret = usb_phy_init(hcd->usb_phy);
 		if (ret)
 			goto put_usb3_hcd;
+		hcd->skip_phy_initialization = 1;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 176900528822..693502c84c04 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -151,6 +151,12 @@ struct usb_hcd {
 	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
 	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
 	unsigned		remove_phy:1;	/* auto-remove USB phy */
+	/*
+	 * do not manage the PHY state in the HCD core, instead let the driver
+	 * handle this (for example if the PHY can only be turned on after a
+	 * specific event)
+	 */
+	unsigned		skip_phy_initialization:1;
 
 	/* The next flag is a stopgap, to be removed when all the HCDs
 	 * support the new root-hub polling mechanism. */
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Chunfeng Yun, Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

Many SoC platforms have separate devices for the USB PHY which are
registered through the generic PHY framework. These PHYs have to be
enabled to make the USB controller actually work. They also have to be
disabled again on shutdown/suspend.

Currently (at least) the following HCI platform drivers are using custom
code to obtain all PHYs via devicetree for the roothub/controller and
disable/enable them when required:
- ehci-platform.c has ehci_platform_power_{on,off}
- xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
- ohci-platform.c has ohci_platform_power_{on,off}

With this new wrapper the USB PHYs can be specified directly in the
USB controller's devicetree node (just like on the drivers listed
above). This allows SoCs like the Amlogic Meson GXL family to operate
correctly once this is wired up correctly. These SoCs use a dwc3
controller and require all USB PHYs to be initialized (if one of the USB
PHYs it not initialized then none of USB port works at all).

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 drivers/usb/core/Makefile |   2 +-
 drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/phy.h    |   7 ++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 92c9cefb4317..18e874b0441e 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -6,7 +6,7 @@
 usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
 usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
 usbcore-y += devio.o notify.o generic.o quirks.o devices.o
-usbcore-y += port.o
+usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
new file mode 100644
index 000000000000..09b7c43c0ea4
--- /dev/null
+++ b/drivers/usb/core/phy.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A wrapper for multiple PHYs which passes all phy_* function calls to
+ * multiple (actual) PHY devices. This is comes handy when initializing
+ * all PHYs on a HCD and to keep them all in the same state.
+ *
+ * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/phy/phy.h>
+#include <linux/of.h>
+
+#include "phy.h"
+
+struct usb_phy_roothub {
+	struct phy		*phy;
+	struct list_head	list;
+};
+
+static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&roothub_entry->list);
+
+	return roothub_entry;
+}
+
+static int usb_phy_roothub_add_phy(struct device *dev, int index,
+				   struct list_head *list)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
+
+	if (IS_ERR_OR_NULL(phy)) {
+		if (!phy || PTR_ERR(phy) == -ENODEV)
+			return 0;
+		else
+			return PTR_ERR(phy);
+	}
+
+	roothub_entry = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(roothub_entry))
+		return PTR_ERR(roothub_entry);
+
+	roothub_entry->phy = phy;
+
+	list_add_tail(&roothub_entry->list, list);
+
+	return 0;
+}
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+{
+	struct usb_phy_roothub *phy_roothub;
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int i, num_phys, err;
+
+	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
+					      "#phy-cells");
+	if (num_phys <= 0)
+		return NULL;
+
+	phy_roothub = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(phy_roothub))
+		return phy_roothub;
+
+	for (i = 0; i < num_phys; i++) {
+		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
+		if (err)
+			goto err_out;
+	}
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_init(roothub_entry->phy);
+		if (err)
+			goto err_exit_phys;
+	}
+
+	return phy_roothub;
+
+err_exit_phys:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_exit(roothub_entry->phy);
+
+err_out:
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
+
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err, ret = 0;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_exit(roothub_entry->phy);
+		if (err)
+			ret = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_power_on(roothub_entry->phy);
+		if (err)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_power_off(roothub_entry->phy);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
+
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	if (!phy_roothub)
+		return;
+
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+		phy_power_off(roothub_entry->phy);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
new file mode 100644
index 000000000000..6fde59bfbff8
--- /dev/null
+++ b/drivers/usb/core/phy.h
@@ -0,0 +1,7 @@
+struct usb_phy_roothub;
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl, Chunfeng Yun

Many SoC platforms have separate devices for the USB PHY which are
registered through the generic PHY framework. These PHYs have to be
enabled to make the USB controller actually work. They also have to be
disabled again on shutdown/suspend.

Currently (at least) the following HCI platform drivers are using custom
code to obtain all PHYs via devicetree for the roothub/controller and
disable/enable them when required:
- ehci-platform.c has ehci_platform_power_{on,off}
- xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
- ohci-platform.c has ohci_platform_power_{on,off}

With this new wrapper the USB PHYs can be specified directly in the
USB controller's devicetree node (just like on the drivers listed
above). This allows SoCs like the Amlogic Meson GXL family to operate
correctly once this is wired up correctly. These SoCs use a dwc3
controller and require all USB PHYs to be initialized (if one of the USB
PHYs it not initialized then none of USB port works at all).

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
Cc: Neil Armstrong <narmstrong@baylibre.com>
Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/usb/core/Makefile |   2 +-
 drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/phy.h    |   7 ++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 92c9cefb4317..18e874b0441e 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -6,7 +6,7 @@
 usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
 usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
 usbcore-y += devio.o notify.o generic.o quirks.o devices.o
-usbcore-y += port.o
+usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
new file mode 100644
index 000000000000..09b7c43c0ea4
--- /dev/null
+++ b/drivers/usb/core/phy.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A wrapper for multiple PHYs which passes all phy_* function calls to
+ * multiple (actual) PHY devices. This is comes handy when initializing
+ * all PHYs on a HCD and to keep them all in the same state.
+ *
+ * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/phy/phy.h>
+#include <linux/of.h>
+
+#include "phy.h"
+
+struct usb_phy_roothub {
+	struct phy		*phy;
+	struct list_head	list;
+};
+
+static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&roothub_entry->list);
+
+	return roothub_entry;
+}
+
+static int usb_phy_roothub_add_phy(struct device *dev, int index,
+				   struct list_head *list)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
+
+	if (IS_ERR_OR_NULL(phy)) {
+		if (!phy || PTR_ERR(phy) == -ENODEV)
+			return 0;
+		else
+			return PTR_ERR(phy);
+	}
+
+	roothub_entry = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(roothub_entry))
+		return PTR_ERR(roothub_entry);
+
+	roothub_entry->phy = phy;
+
+	list_add_tail(&roothub_entry->list, list);
+
+	return 0;
+}
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+{
+	struct usb_phy_roothub *phy_roothub;
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int i, num_phys, err;
+
+	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
+					      "#phy-cells");
+	if (num_phys <= 0)
+		return NULL;
+
+	phy_roothub = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(phy_roothub))
+		return phy_roothub;
+
+	for (i = 0; i < num_phys; i++) {
+		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
+		if (err)
+			goto err_out;
+	}
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_init(roothub_entry->phy);
+		if (err)
+			goto err_exit_phys;
+	}
+
+	return phy_roothub;
+
+err_exit_phys:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_exit(roothub_entry->phy);
+
+err_out:
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
+
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err, ret = 0;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_exit(roothub_entry->phy);
+		if (err)
+			ret = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_power_on(roothub_entry->phy);
+		if (err)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_power_off(roothub_entry->phy);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
+
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	if (!phy_roothub)
+		return;
+
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+		phy_power_off(roothub_entry->phy);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
new file mode 100644
index 000000000000..6fde59bfbff8
--- /dev/null
+++ b/drivers/usb/core/phy.h
@@ -0,0 +1,7 @@
+struct usb_phy_roothub;
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linux-arm-kernel

Many SoC platforms have separate devices for the USB PHY which are
registered through the generic PHY framework. These PHYs have to be
enabled to make the USB controller actually work. They also have to be
disabled again on shutdown/suspend.

Currently (at least) the following HCI platform drivers are using custom
code to obtain all PHYs via devicetree for the roothub/controller and
disable/enable them when required:
- ehci-platform.c has ehci_platform_power_{on,off}
- xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
- ohci-platform.c has ohci_platform_power_{on,off}

With this new wrapper the USB PHYs can be specified directly in the
USB controller's devicetree node (just like on the drivers listed
above). This allows SoCs like the Amlogic Meson GXL family to operate
correctly once this is wired up correctly. These SoCs use a dwc3
controller and require all USB PHYs to be initialized (if one of the USB
PHYs it not initialized then none of USB port works at all).

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
Cc: Neil Armstrong <narmstrong@baylibre.com>
Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/usb/core/Makefile |   2 +-
 drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/phy.h    |   7 ++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 92c9cefb4317..18e874b0441e 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -6,7 +6,7 @@
 usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
 usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
 usbcore-y += devio.o notify.o generic.o quirks.o devices.o
-usbcore-y += port.o
+usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
new file mode 100644
index 000000000000..09b7c43c0ea4
--- /dev/null
+++ b/drivers/usb/core/phy.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A wrapper for multiple PHYs which passes all phy_* function calls to
+ * multiple (actual) PHY devices. This is comes handy when initializing
+ * all PHYs on a HCD and to keep them all in the same state.
+ *
+ * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/phy/phy.h>
+#include <linux/of.h>
+
+#include "phy.h"
+
+struct usb_phy_roothub {
+	struct phy		*phy;
+	struct list_head	list;
+};
+
+static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&roothub_entry->list);
+
+	return roothub_entry;
+}
+
+static int usb_phy_roothub_add_phy(struct device *dev, int index,
+				   struct list_head *list)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
+
+	if (IS_ERR_OR_NULL(phy)) {
+		if (!phy || PTR_ERR(phy) == -ENODEV)
+			return 0;
+		else
+			return PTR_ERR(phy);
+	}
+
+	roothub_entry = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(roothub_entry))
+		return PTR_ERR(roothub_entry);
+
+	roothub_entry->phy = phy;
+
+	list_add_tail(&roothub_entry->list, list);
+
+	return 0;
+}
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+{
+	struct usb_phy_roothub *phy_roothub;
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int i, num_phys, err;
+
+	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
+					      "#phy-cells");
+	if (num_phys <= 0)
+		return NULL;
+
+	phy_roothub = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(phy_roothub))
+		return phy_roothub;
+
+	for (i = 0; i < num_phys; i++) {
+		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
+		if (err)
+			goto err_out;
+	}
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_init(roothub_entry->phy);
+		if (err)
+			goto err_exit_phys;
+	}
+
+	return phy_roothub;
+
+err_exit_phys:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_exit(roothub_entry->phy);
+
+err_out:
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
+
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err, ret = 0;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_exit(roothub_entry->phy);
+		if (err)
+			ret = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_power_on(roothub_entry->phy);
+		if (err)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_power_off(roothub_entry->phy);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
+
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	if (!phy_roothub)
+		return;
+
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+		phy_power_off(roothub_entry->phy);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
new file mode 100644
index 000000000000..6fde59bfbff8
--- /dev/null
+++ b/drivers/usb/core/phy.h
@@ -0,0 +1,7 @@
+struct usb_phy_roothub;
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-02-18 18:44     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:44 UTC (permalink / raw)
  To: linus-amlogic

Many SoC platforms have separate devices for the USB PHY which are
registered through the generic PHY framework. These PHYs have to be
enabled to make the USB controller actually work. They also have to be
disabled again on shutdown/suspend.

Currently (at least) the following HCI platform drivers are using custom
code to obtain all PHYs via devicetree for the roothub/controller and
disable/enable them when required:
- ehci-platform.c has ehci_platform_power_{on,off}
- xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
- ohci-platform.c has ohci_platform_power_{on,off}

With this new wrapper the USB PHYs can be specified directly in the
USB controller's devicetree node (just like on the drivers listed
above). This allows SoCs like the Amlogic Meson GXL family to operate
correctly once this is wired up correctly. These SoCs use a dwc3
controller and require all USB PHYs to be initialized (if one of the USB
PHYs it not initialized then none of USB port works at all).

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
Cc: Neil Armstrong <narmstrong@baylibre.com>
Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/usb/core/Makefile |   2 +-
 drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/phy.h    |   7 ++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 92c9cefb4317..18e874b0441e 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -6,7 +6,7 @@
 usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
 usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
 usbcore-y += devio.o notify.o generic.o quirks.o devices.o
-usbcore-y += port.o
+usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
new file mode 100644
index 000000000000..09b7c43c0ea4
--- /dev/null
+++ b/drivers/usb/core/phy.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A wrapper for multiple PHYs which passes all phy_* function calls to
+ * multiple (actual) PHY devices. This is comes handy when initializing
+ * all PHYs on a HCD and to keep them all in the same state.
+ *
+ * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/phy/phy.h>
+#include <linux/of.h>
+
+#include "phy.h"
+
+struct usb_phy_roothub {
+	struct phy		*phy;
+	struct list_head	list;
+};
+
+static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&roothub_entry->list);
+
+	return roothub_entry;
+}
+
+static int usb_phy_roothub_add_phy(struct device *dev, int index,
+				   struct list_head *list)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
+
+	if (IS_ERR_OR_NULL(phy)) {
+		if (!phy || PTR_ERR(phy) == -ENODEV)
+			return 0;
+		else
+			return PTR_ERR(phy);
+	}
+
+	roothub_entry = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(roothub_entry))
+		return PTR_ERR(roothub_entry);
+
+	roothub_entry->phy = phy;
+
+	list_add_tail(&roothub_entry->list, list);
+
+	return 0;
+}
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+{
+	struct usb_phy_roothub *phy_roothub;
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int i, num_phys, err;
+
+	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
+					      "#phy-cells");
+	if (num_phys <= 0)
+		return NULL;
+
+	phy_roothub = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(phy_roothub))
+		return phy_roothub;
+
+	for (i = 0; i < num_phys; i++) {
+		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
+		if (err)
+			goto err_out;
+	}
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_init(roothub_entry->phy);
+		if (err)
+			goto err_exit_phys;
+	}
+
+	return phy_roothub;
+
+err_exit_phys:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_exit(roothub_entry->phy);
+
+err_out:
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
+
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err, ret = 0;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_exit(roothub_entry->phy);
+		if (err)
+			ret = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_power_on(roothub_entry->phy);
+		if (err)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_power_off(roothub_entry->phy);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
+
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	if (!phy_roothub)
+		return;
+
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+		phy_power_off(roothub_entry->phy);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
new file mode 100644
index 000000000000..6fde59bfbff8
--- /dev/null
+++ b/drivers/usb/core/phy.h
@@ -0,0 +1,7 @@
+struct usb_phy_roothub;
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 4/8] usb: core: hcd: integrate the PHY wrapper into the HCD core
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

This integrates the PHY wrapper into the core hcd infrastructure.
Multiple PHYs which are part of the HCD's device tree node are now
managed (= powered on/off when needed), by the new usb_phy_roothub code.

Suspend and resume is also supported, however not for
runtime/auto-suspend (which is triggered for example when no devices are
connected to the USB bus). This is needed on some SoCs (for example
Amlogic Meson GXL) because if the PHYs are disabled during auto-suspend
then devices which are plugged in afterwards are not seen by the host.

One example where this is required is the Amlogic GXL and GXM SoCs:
They are using a dwc3 USB controller with up to three ports enabled on
the internal roothub. Each port has it's own PHY which must be enabled
(if one of the PHYs is left disabled then none of the USB ports works at
all).
The new logic works on the Amlogic GXL and GXM SoCs because the dwc3
driver internally creates a xhci-hcd which then registers a HCD which
then triggers our new PHY wrapper.

USB controller drivers can opt out of this by setting
"skip_phy_initialization" in struct usb_hcd to true. This is identical
to how it works for a single USB PHY, so the "multiple PHY" handling is
disabled for drivers that opted out of the management logic of a single
PHY.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
Acked-by: Alan Stern <stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org>
Acked-by: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
---
 drivers/usb/core/hcd.c  | 31 +++++++++++++++++++++++++++++++
 include/linux/usb/hcd.h |  1 +
 2 files changed, 32 insertions(+)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index f2307470a31e..32797c25ac3b 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -37,6 +37,7 @@
 #include <linux/usb/otg.h>
 
 #include "usb.h"
+#include "phy.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -2260,6 +2261,9 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
 		hcd->state = HC_STATE_SUSPENDED;
 
+		if (!PMSG_IS_AUTO(msg))
+			usb_phy_roothub_power_off(hcd->phy_roothub);
+
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
 			char	buffer[6];
@@ -2296,6 +2300,13 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
 		return 0;
 	}
+
+	if (!PMSG_IS_AUTO(msg)) {
+		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (status)
+			return status;
+	}
+
 	if (!hcd->driver->bus_resume)
 		return -ENOENT;
 	if (HCD_RH_RUNNING(hcd))
@@ -2333,6 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
+		usb_phy_roothub_power_off(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
@@ -2769,6 +2781,18 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
+	if (!hcd->skip_phy_initialization) {
+		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		if (IS_ERR(hcd->phy_roothub)) {
+			retval = PTR_ERR(hcd->phy_roothub);
+			goto err_phy_roothub_init;
+		}
+
+		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (retval)
+			goto err_usb_phy_roothub_power_on;
+	}
+
 	dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
 
 	/* Keep old behaviour if authorized_default is not in [0, 1]. */
@@ -2933,6 +2957,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_register_bus:
 	hcd_buffer_destroy(hcd);
 err_create_buf:
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+err_usb_phy_roothub_power_on:
+	usb_phy_roothub_exit(hcd->phy_roothub);
+err_phy_roothub_init:
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
@@ -3017,6 +3045,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_deregister_bus(&hcd->self);
 	hcd_buffer_destroy(hcd);
 
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+	usb_phy_roothub_exit(hcd->phy_roothub);
+
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 693502c84c04..a042675e03ba 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -104,6 +104,7 @@ struct usb_hcd {
 	 */
 	struct usb_phy		*usb_phy;
 	struct phy		*phy;
+	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
 	 * change while the host controller is running.  Always use
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,4/8] usb: core: hcd: integrate the PHY wrapper into the HCD core
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

This integrates the PHY wrapper into the core hcd infrastructure.
Multiple PHYs which are part of the HCD's device tree node are now
managed (= powered on/off when needed), by the new usb_phy_roothub code.

Suspend and resume is also supported, however not for
runtime/auto-suspend (which is triggered for example when no devices are
connected to the USB bus). This is needed on some SoCs (for example
Amlogic Meson GXL) because if the PHYs are disabled during auto-suspend
then devices which are plugged in afterwards are not seen by the host.

One example where this is required is the Amlogic GXL and GXM SoCs:
They are using a dwc3 USB controller with up to three ports enabled on
the internal roothub. Each port has it's own PHY which must be enabled
(if one of the PHYs is left disabled then none of the USB ports works at
all).
The new logic works on the Amlogic GXL and GXM SoCs because the dwc3
driver internally creates a xhci-hcd which then registers a HCD which
then triggers our new PHY wrapper.

USB controller drivers can opt out of this by setting
"skip_phy_initialization" in struct usb_hcd to true. This is identical
to how it works for a single USB PHY, so the "multiple PHY" handling is
disabled for drivers that opted out of the management logic of a single
PHY.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 drivers/usb/core/hcd.c  | 31 +++++++++++++++++++++++++++++++
 include/linux/usb/hcd.h |  1 +
 2 files changed, 32 insertions(+)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index f2307470a31e..32797c25ac3b 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -37,6 +37,7 @@
 #include <linux/usb/otg.h>
 
 #include "usb.h"
+#include "phy.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -2260,6 +2261,9 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
 		hcd->state = HC_STATE_SUSPENDED;
 
+		if (!PMSG_IS_AUTO(msg))
+			usb_phy_roothub_power_off(hcd->phy_roothub);
+
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
 			char	buffer[6];
@@ -2296,6 +2300,13 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
 		return 0;
 	}
+
+	if (!PMSG_IS_AUTO(msg)) {
+		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (status)
+			return status;
+	}
+
 	if (!hcd->driver->bus_resume)
 		return -ENOENT;
 	if (HCD_RH_RUNNING(hcd))
@@ -2333,6 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
+		usb_phy_roothub_power_off(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
@@ -2769,6 +2781,18 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
+	if (!hcd->skip_phy_initialization) {
+		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		if (IS_ERR(hcd->phy_roothub)) {
+			retval = PTR_ERR(hcd->phy_roothub);
+			goto err_phy_roothub_init;
+		}
+
+		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (retval)
+			goto err_usb_phy_roothub_power_on;
+	}
+
 	dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
 
 	/* Keep old behaviour if authorized_default is not in [0, 1]. */
@@ -2933,6 +2957,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_register_bus:
 	hcd_buffer_destroy(hcd);
 err_create_buf:
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+err_usb_phy_roothub_power_on:
+	usb_phy_roothub_exit(hcd->phy_roothub);
+err_phy_roothub_init:
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
@@ -3017,6 +3045,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_deregister_bus(&hcd->self);
 	hcd_buffer_destroy(hcd);
 
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+	usb_phy_roothub_exit(hcd->phy_roothub);
+
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 693502c84c04..a042675e03ba 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -104,6 +104,7 @@ struct usb_hcd {
 	 */
 	struct usb_phy		*usb_phy;
 	struct phy		*phy;
+	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
 	 * change while the host controller is running.  Always use

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 4/8] usb: core: hcd: integrate the PHY wrapper into the HCD core
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

This integrates the PHY wrapper into the core hcd infrastructure.
Multiple PHYs which are part of the HCD's device tree node are now
managed (= powered on/off when needed), by the new usb_phy_roothub code.

Suspend and resume is also supported, however not for
runtime/auto-suspend (which is triggered for example when no devices are
connected to the USB bus). This is needed on some SoCs (for example
Amlogic Meson GXL) because if the PHYs are disabled during auto-suspend
then devices which are plugged in afterwards are not seen by the host.

One example where this is required is the Amlogic GXL and GXM SoCs:
They are using a dwc3 USB controller with up to three ports enabled on
the internal roothub. Each port has it's own PHY which must be enabled
(if one of the PHYs is left disabled then none of the USB ports works at
all).
The new logic works on the Amlogic GXL and GXM SoCs because the dwc3
driver internally creates a xhci-hcd which then registers a HCD which
then triggers our new PHY wrapper.

USB controller drivers can opt out of this by setting
"skip_phy_initialization" in struct usb_hcd to true. This is identical
to how it works for a single USB PHY, so the "multiple PHY" handling is
disabled for drivers that opted out of the management logic of a single
PHY.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 drivers/usb/core/hcd.c  | 31 +++++++++++++++++++++++++++++++
 include/linux/usb/hcd.h |  1 +
 2 files changed, 32 insertions(+)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index f2307470a31e..32797c25ac3b 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -37,6 +37,7 @@
 #include <linux/usb/otg.h>
 
 #include "usb.h"
+#include "phy.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -2260,6 +2261,9 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
 		hcd->state = HC_STATE_SUSPENDED;
 
+		if (!PMSG_IS_AUTO(msg))
+			usb_phy_roothub_power_off(hcd->phy_roothub);
+
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
 			char	buffer[6];
@@ -2296,6 +2300,13 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
 		return 0;
 	}
+
+	if (!PMSG_IS_AUTO(msg)) {
+		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (status)
+			return status;
+	}
+
 	if (!hcd->driver->bus_resume)
 		return -ENOENT;
 	if (HCD_RH_RUNNING(hcd))
@@ -2333,6 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
+		usb_phy_roothub_power_off(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
@@ -2769,6 +2781,18 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
+	if (!hcd->skip_phy_initialization) {
+		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		if (IS_ERR(hcd->phy_roothub)) {
+			retval = PTR_ERR(hcd->phy_roothub);
+			goto err_phy_roothub_init;
+		}
+
+		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (retval)
+			goto err_usb_phy_roothub_power_on;
+	}
+
 	dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
 
 	/* Keep old behaviour if authorized_default is not in [0, 1]. */
@@ -2933,6 +2957,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_register_bus:
 	hcd_buffer_destroy(hcd);
 err_create_buf:
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+err_usb_phy_roothub_power_on:
+	usb_phy_roothub_exit(hcd->phy_roothub);
+err_phy_roothub_init:
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
@@ -3017,6 +3045,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_deregister_bus(&hcd->self);
 	hcd_buffer_destroy(hcd);
 
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+	usb_phy_roothub_exit(hcd->phy_roothub);
+
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 693502c84c04..a042675e03ba 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -104,6 +104,7 @@ struct usb_hcd {
 	 */
 	struct usb_phy		*usb_phy;
 	struct phy		*phy;
+	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
 	 * change while the host controller is running.  Always use
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 4/8] usb: core: hcd: integrate the PHY wrapper into the HCD core
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linus-amlogic

This integrates the PHY wrapper into the core hcd infrastructure.
Multiple PHYs which are part of the HCD's device tree node are now
managed (= powered on/off when needed), by the new usb_phy_roothub code.

Suspend and resume is also supported, however not for
runtime/auto-suspend (which is triggered for example when no devices are
connected to the USB bus). This is needed on some SoCs (for example
Amlogic Meson GXL) because if the PHYs are disabled during auto-suspend
then devices which are plugged in afterwards are not seen by the host.

One example where this is required is the Amlogic GXL and GXM SoCs:
They are using a dwc3 USB controller with up to three ports enabled on
the internal roothub. Each port has it's own PHY which must be enabled
(if one of the PHYs is left disabled then none of the USB ports works at
all).
The new logic works on the Amlogic GXL and GXM SoCs because the dwc3
driver internally creates a xhci-hcd which then registers a HCD which
then triggers our new PHY wrapper.

USB controller drivers can opt out of this by setting
"skip_phy_initialization" in struct usb_hcd to true. This is identical
to how it works for a single USB PHY, so the "multiple PHY" handling is
disabled for drivers that opted out of the management logic of a single
PHY.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
---
 drivers/usb/core/hcd.c  | 31 +++++++++++++++++++++++++++++++
 include/linux/usb/hcd.h |  1 +
 2 files changed, 32 insertions(+)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index f2307470a31e..32797c25ac3b 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -37,6 +37,7 @@
 #include <linux/usb/otg.h>
 
 #include "usb.h"
+#include "phy.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -2260,6 +2261,9 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
 		hcd->state = HC_STATE_SUSPENDED;
 
+		if (!PMSG_IS_AUTO(msg))
+			usb_phy_roothub_power_off(hcd->phy_roothub);
+
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
 			char	buffer[6];
@@ -2296,6 +2300,13 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
 		return 0;
 	}
+
+	if (!PMSG_IS_AUTO(msg)) {
+		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (status)
+			return status;
+	}
+
 	if (!hcd->driver->bus_resume)
 		return -ENOENT;
 	if (HCD_RH_RUNNING(hcd))
@@ -2333,6 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
+		usb_phy_roothub_power_off(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
@@ -2769,6 +2781,18 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
+	if (!hcd->skip_phy_initialization) {
+		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		if (IS_ERR(hcd->phy_roothub)) {
+			retval = PTR_ERR(hcd->phy_roothub);
+			goto err_phy_roothub_init;
+		}
+
+		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
+		if (retval)
+			goto err_usb_phy_roothub_power_on;
+	}
+
 	dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
 
 	/* Keep old behaviour if authorized_default is not in [0, 1]. */
@@ -2933,6 +2957,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_register_bus:
 	hcd_buffer_destroy(hcd);
 err_create_buf:
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+err_usb_phy_roothub_power_on:
+	usb_phy_roothub_exit(hcd->phy_roothub);
+err_phy_roothub_init:
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
@@ -3017,6 +3045,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_deregister_bus(&hcd->self);
 	hcd_buffer_destroy(hcd);
 
+	usb_phy_roothub_power_off(hcd->phy_roothub);
+	usb_phy_roothub_exit(hcd->phy_roothub);
+
 	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
 		phy_power_off(hcd->phy);
 		phy_exit(hcd->phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index 693502c84c04..a042675e03ba 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -104,6 +104,7 @@ struct usb_hcd {
 	 */
 	struct usb_phy		*usb_phy;
 	struct phy		*phy;
+	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
 	 * change while the host controller is running.  Always use
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 5/8] usb: host: xhci-mtk: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the xhci-mtk driver as the core
HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
---
 drivers/usb/host/xhci-mtk.c | 98 +--------------------------------------------
 1 file changed, 2 insertions(+), 96 deletions(-)

diff --git a/drivers/usb/host/xhci-mtk.c b/drivers/usb/host/xhci-mtk.c
index b0ab4d5e2751..7334da9e9779 100644
--- a/drivers/usb/host/xhci-mtk.c
+++ b/drivers/usb/host/xhci-mtk.c
@@ -14,7 +14,6 @@
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -352,62 +351,6 @@ static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
 
 static struct hc_driver __read_mostly xhci_mtk_hc_driver;
 
-static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_init(mtk->phys[i]);
-		if (ret)
-			goto exit_phy;
-	}
-	return 0;
-
-exit_phy:
-	for (; i > 0; i--)
-		phy_exit(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_exit(mtk->phys[i]);
-
-	return 0;
-}
-
-static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_power_on(mtk->phys[i]);
-		if (ret)
-			goto power_off_phy;
-	}
-	return 0;
-
-power_off_phy:
-	for (; i > 0; i--)
-		phy_power_off(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
-{
-	unsigned int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_power_off(mtk->phys[i]);
-}
-
 static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
 {
 	int ret;
@@ -488,8 +431,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	struct xhci_hcd *xhci;
 	struct resource *res;
 	struct usb_hcd *hcd;
-	struct phy *phy;
-	int phy_num;
 	int ret = -ENODEV;
 	int irq;
 
@@ -529,16 +470,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	mtk->num_phys = of_count_phandle_with_args(node,
-			"phys", "#phy-cells");
-	if (mtk->num_phys > 0) {
-		mtk->phys = devm_kcalloc(dev, mtk->num_phys,
-					sizeof(*mtk->phys), GFP_KERNEL);
-		if (!mtk->phys)
-			return -ENOMEM;
-	} else {
-		mtk->num_phys = 0;
-	}
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 	device_enable_async_suspend(dev);
@@ -596,23 +527,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		mtk->has_ippc = false;
 	}
 
-	for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
-		phy = devm_of_phy_get_by_index(dev, node, phy_num);
-		if (IS_ERR(phy)) {
-			ret = PTR_ERR(phy);
-			goto put_usb2_hcd;
-		}
-		mtk->phys[phy_num] = phy;
-	}
-
-	ret = xhci_mtk_phy_init(mtk);
-	if (ret)
-		goto put_usb2_hcd;
-
-	ret = xhci_mtk_phy_power_on(mtk);
-	if (ret)
-		goto exit_phys;
-
 	device_init_wakeup(dev, true);
 
 	xhci = hcd_to_xhci(hcd);
@@ -630,7 +544,7 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 			dev_name(dev), hcd);
 	if (!xhci->shared_hcd) {
 		ret = -ENOMEM;
-		goto power_off_phys;
+		goto disable_device_wakeup;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
@@ -653,13 +567,9 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	xhci_mtk_sch_exit(mtk);
 	usb_put_hcd(xhci->shared_hcd);
 
-power_off_phys:
-	xhci_mtk_phy_power_off(mtk);
+disable_device_wakeup:
 	device_init_wakeup(dev, false);
 
-exit_phys:
-	xhci_mtk_phy_exit(mtk);
-
 put_usb2_hcd:
 	usb_put_hcd(hcd);
 
@@ -682,8 +592,6 @@ static int xhci_mtk_remove(struct platform_device *dev)
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
 
 	usb_remove_hcd(xhci->shared_hcd);
-	xhci_mtk_phy_power_off(mtk);
-	xhci_mtk_phy_exit(mtk);
 	device_init_wakeup(&dev->dev, false);
 
 	usb_remove_hcd(hcd);
@@ -718,7 +626,6 @@ static int __maybe_unused xhci_mtk_suspend(struct device *dev)
 	del_timer_sync(&xhci->shared_hcd->rh_timer);
 
 	xhci_mtk_host_disable(mtk);
-	xhci_mtk_phy_power_off(mtk);
 	xhci_mtk_clks_disable(mtk);
 	usb_wakeup_set(mtk, true);
 	return 0;
@@ -732,7 +639,6 @@ static int __maybe_unused xhci_mtk_resume(struct device *dev)
 
 	usb_wakeup_set(mtk, false);
 	xhci_mtk_clks_enable(mtk);
-	xhci_mtk_phy_power_on(mtk);
 	xhci_mtk_host_enable(mtk);
 
 	xhci_dbg(xhci, "%s: restart port polling\n", __func__);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,5/8] usb: host: xhci-mtk: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the xhci-mtk driver as the core
HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/host/xhci-mtk.c | 98 +--------------------------------------------
 1 file changed, 2 insertions(+), 96 deletions(-)

diff --git a/drivers/usb/host/xhci-mtk.c b/drivers/usb/host/xhci-mtk.c
index b0ab4d5e2751..7334da9e9779 100644
--- a/drivers/usb/host/xhci-mtk.c
+++ b/drivers/usb/host/xhci-mtk.c
@@ -14,7 +14,6 @@
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -352,62 +351,6 @@ static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
 
 static struct hc_driver __read_mostly xhci_mtk_hc_driver;
 
-static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_init(mtk->phys[i]);
-		if (ret)
-			goto exit_phy;
-	}
-	return 0;
-
-exit_phy:
-	for (; i > 0; i--)
-		phy_exit(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_exit(mtk->phys[i]);
-
-	return 0;
-}
-
-static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_power_on(mtk->phys[i]);
-		if (ret)
-			goto power_off_phy;
-	}
-	return 0;
-
-power_off_phy:
-	for (; i > 0; i--)
-		phy_power_off(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
-{
-	unsigned int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_power_off(mtk->phys[i]);
-}
-
 static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
 {
 	int ret;
@@ -488,8 +431,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	struct xhci_hcd *xhci;
 	struct resource *res;
 	struct usb_hcd *hcd;
-	struct phy *phy;
-	int phy_num;
 	int ret = -ENODEV;
 	int irq;
 
@@ -529,16 +470,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	mtk->num_phys = of_count_phandle_with_args(node,
-			"phys", "#phy-cells");
-	if (mtk->num_phys > 0) {
-		mtk->phys = devm_kcalloc(dev, mtk->num_phys,
-					sizeof(*mtk->phys), GFP_KERNEL);
-		if (!mtk->phys)
-			return -ENOMEM;
-	} else {
-		mtk->num_phys = 0;
-	}
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 	device_enable_async_suspend(dev);
@@ -596,23 +527,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		mtk->has_ippc = false;
 	}
 
-	for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
-		phy = devm_of_phy_get_by_index(dev, node, phy_num);
-		if (IS_ERR(phy)) {
-			ret = PTR_ERR(phy);
-			goto put_usb2_hcd;
-		}
-		mtk->phys[phy_num] = phy;
-	}
-
-	ret = xhci_mtk_phy_init(mtk);
-	if (ret)
-		goto put_usb2_hcd;
-
-	ret = xhci_mtk_phy_power_on(mtk);
-	if (ret)
-		goto exit_phys;
-
 	device_init_wakeup(dev, true);
 
 	xhci = hcd_to_xhci(hcd);
@@ -630,7 +544,7 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 			dev_name(dev), hcd);
 	if (!xhci->shared_hcd) {
 		ret = -ENOMEM;
-		goto power_off_phys;
+		goto disable_device_wakeup;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
@@ -653,13 +567,9 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	xhci_mtk_sch_exit(mtk);
 	usb_put_hcd(xhci->shared_hcd);
 
-power_off_phys:
-	xhci_mtk_phy_power_off(mtk);
+disable_device_wakeup:
 	device_init_wakeup(dev, false);
 
-exit_phys:
-	xhci_mtk_phy_exit(mtk);
-
 put_usb2_hcd:
 	usb_put_hcd(hcd);
 
@@ -682,8 +592,6 @@ static int xhci_mtk_remove(struct platform_device *dev)
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
 
 	usb_remove_hcd(xhci->shared_hcd);
-	xhci_mtk_phy_power_off(mtk);
-	xhci_mtk_phy_exit(mtk);
 	device_init_wakeup(&dev->dev, false);
 
 	usb_remove_hcd(hcd);
@@ -718,7 +626,6 @@ static int __maybe_unused xhci_mtk_suspend(struct device *dev)
 	del_timer_sync(&xhci->shared_hcd->rh_timer);
 
 	xhci_mtk_host_disable(mtk);
-	xhci_mtk_phy_power_off(mtk);
 	xhci_mtk_clks_disable(mtk);
 	usb_wakeup_set(mtk, true);
 	return 0;
@@ -732,7 +639,6 @@ static int __maybe_unused xhci_mtk_resume(struct device *dev)
 
 	usb_wakeup_set(mtk, false);
 	xhci_mtk_clks_enable(mtk);
-	xhci_mtk_phy_power_on(mtk);
 	xhci_mtk_host_enable(mtk);
 
 	xhci_dbg(xhci, "%s: restart port polling\n", __func__);

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 5/8] usb: host: xhci-mtk: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the xhci-mtk driver as the core
HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/host/xhci-mtk.c | 98 +--------------------------------------------
 1 file changed, 2 insertions(+), 96 deletions(-)

diff --git a/drivers/usb/host/xhci-mtk.c b/drivers/usb/host/xhci-mtk.c
index b0ab4d5e2751..7334da9e9779 100644
--- a/drivers/usb/host/xhci-mtk.c
+++ b/drivers/usb/host/xhci-mtk.c
@@ -14,7 +14,6 @@
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -352,62 +351,6 @@ static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
 
 static struct hc_driver __read_mostly xhci_mtk_hc_driver;
 
-static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_init(mtk->phys[i]);
-		if (ret)
-			goto exit_phy;
-	}
-	return 0;
-
-exit_phy:
-	for (; i > 0; i--)
-		phy_exit(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_exit(mtk->phys[i]);
-
-	return 0;
-}
-
-static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_power_on(mtk->phys[i]);
-		if (ret)
-			goto power_off_phy;
-	}
-	return 0;
-
-power_off_phy:
-	for (; i > 0; i--)
-		phy_power_off(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
-{
-	unsigned int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_power_off(mtk->phys[i]);
-}
-
 static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
 {
 	int ret;
@@ -488,8 +431,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	struct xhci_hcd *xhci;
 	struct resource *res;
 	struct usb_hcd *hcd;
-	struct phy *phy;
-	int phy_num;
 	int ret = -ENODEV;
 	int irq;
 
@@ -529,16 +470,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	mtk->num_phys = of_count_phandle_with_args(node,
-			"phys", "#phy-cells");
-	if (mtk->num_phys > 0) {
-		mtk->phys = devm_kcalloc(dev, mtk->num_phys,
-					sizeof(*mtk->phys), GFP_KERNEL);
-		if (!mtk->phys)
-			return -ENOMEM;
-	} else {
-		mtk->num_phys = 0;
-	}
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 	device_enable_async_suspend(dev);
@@ -596,23 +527,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		mtk->has_ippc = false;
 	}
 
-	for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
-		phy = devm_of_phy_get_by_index(dev, node, phy_num);
-		if (IS_ERR(phy)) {
-			ret = PTR_ERR(phy);
-			goto put_usb2_hcd;
-		}
-		mtk->phys[phy_num] = phy;
-	}
-
-	ret = xhci_mtk_phy_init(mtk);
-	if (ret)
-		goto put_usb2_hcd;
-
-	ret = xhci_mtk_phy_power_on(mtk);
-	if (ret)
-		goto exit_phys;
-
 	device_init_wakeup(dev, true);
 
 	xhci = hcd_to_xhci(hcd);
@@ -630,7 +544,7 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 			dev_name(dev), hcd);
 	if (!xhci->shared_hcd) {
 		ret = -ENOMEM;
-		goto power_off_phys;
+		goto disable_device_wakeup;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
@@ -653,13 +567,9 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	xhci_mtk_sch_exit(mtk);
 	usb_put_hcd(xhci->shared_hcd);
 
-power_off_phys:
-	xhci_mtk_phy_power_off(mtk);
+disable_device_wakeup:
 	device_init_wakeup(dev, false);
 
-exit_phys:
-	xhci_mtk_phy_exit(mtk);
-
 put_usb2_hcd:
 	usb_put_hcd(hcd);
 
@@ -682,8 +592,6 @@ static int xhci_mtk_remove(struct platform_device *dev)
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
 
 	usb_remove_hcd(xhci->shared_hcd);
-	xhci_mtk_phy_power_off(mtk);
-	xhci_mtk_phy_exit(mtk);
 	device_init_wakeup(&dev->dev, false);
 
 	usb_remove_hcd(hcd);
@@ -718,7 +626,6 @@ static int __maybe_unused xhci_mtk_suspend(struct device *dev)
 	del_timer_sync(&xhci->shared_hcd->rh_timer);
 
 	xhci_mtk_host_disable(mtk);
-	xhci_mtk_phy_power_off(mtk);
 	xhci_mtk_clks_disable(mtk);
 	usb_wakeup_set(mtk, true);
 	return 0;
@@ -732,7 +639,6 @@ static int __maybe_unused xhci_mtk_resume(struct device *dev)
 
 	usb_wakeup_set(mtk, false);
 	xhci_mtk_clks_enable(mtk);
-	xhci_mtk_phy_power_on(mtk);
 	xhci_mtk_host_enable(mtk);
 
 	xhci_dbg(xhci, "%s: restart port polling\n", __func__);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 5/8] usb: host: xhci-mtk: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linus-amlogic

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the xhci-mtk driver as the core
HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/host/xhci-mtk.c | 98 +--------------------------------------------
 1 file changed, 2 insertions(+), 96 deletions(-)

diff --git a/drivers/usb/host/xhci-mtk.c b/drivers/usb/host/xhci-mtk.c
index b0ab4d5e2751..7334da9e9779 100644
--- a/drivers/usb/host/xhci-mtk.c
+++ b/drivers/usb/host/xhci-mtk.c
@@ -14,7 +14,6 @@
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -352,62 +351,6 @@ static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
 
 static struct hc_driver __read_mostly xhci_mtk_hc_driver;
 
-static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_init(mtk->phys[i]);
-		if (ret)
-			goto exit_phy;
-	}
-	return 0;
-
-exit_phy:
-	for (; i > 0; i--)
-		phy_exit(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_exit(mtk->phys[i]);
-
-	return 0;
-}
-
-static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
-{
-	int i;
-	int ret;
-
-	for (i = 0; i < mtk->num_phys; i++) {
-		ret = phy_power_on(mtk->phys[i]);
-		if (ret)
-			goto power_off_phy;
-	}
-	return 0;
-
-power_off_phy:
-	for (; i > 0; i--)
-		phy_power_off(mtk->phys[i - 1]);
-
-	return ret;
-}
-
-static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
-{
-	unsigned int i;
-
-	for (i = 0; i < mtk->num_phys; i++)
-		phy_power_off(mtk->phys[i]);
-}
-
 static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
 {
 	int ret;
@@ -488,8 +431,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	struct xhci_hcd *xhci;
 	struct resource *res;
 	struct usb_hcd *hcd;
-	struct phy *phy;
-	int phy_num;
 	int ret = -ENODEV;
 	int irq;
 
@@ -529,16 +470,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	mtk->num_phys = of_count_phandle_with_args(node,
-			"phys", "#phy-cells");
-	if (mtk->num_phys > 0) {
-		mtk->phys = devm_kcalloc(dev, mtk->num_phys,
-					sizeof(*mtk->phys), GFP_KERNEL);
-		if (!mtk->phys)
-			return -ENOMEM;
-	} else {
-		mtk->num_phys = 0;
-	}
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 	device_enable_async_suspend(dev);
@@ -596,23 +527,6 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 		mtk->has_ippc = false;
 	}
 
-	for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
-		phy = devm_of_phy_get_by_index(dev, node, phy_num);
-		if (IS_ERR(phy)) {
-			ret = PTR_ERR(phy);
-			goto put_usb2_hcd;
-		}
-		mtk->phys[phy_num] = phy;
-	}
-
-	ret = xhci_mtk_phy_init(mtk);
-	if (ret)
-		goto put_usb2_hcd;
-
-	ret = xhci_mtk_phy_power_on(mtk);
-	if (ret)
-		goto exit_phys;
-
 	device_init_wakeup(dev, true);
 
 	xhci = hcd_to_xhci(hcd);
@@ -630,7 +544,7 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 			dev_name(dev), hcd);
 	if (!xhci->shared_hcd) {
 		ret = -ENOMEM;
-		goto power_off_phys;
+		goto disable_device_wakeup;
 	}
 
 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
@@ -653,13 +567,9 @@ static int xhci_mtk_probe(struct platform_device *pdev)
 	xhci_mtk_sch_exit(mtk);
 	usb_put_hcd(xhci->shared_hcd);
 
-power_off_phys:
-	xhci_mtk_phy_power_off(mtk);
+disable_device_wakeup:
 	device_init_wakeup(dev, false);
 
-exit_phys:
-	xhci_mtk_phy_exit(mtk);
-
 put_usb2_hcd:
 	usb_put_hcd(hcd);
 
@@ -682,8 +592,6 @@ static int xhci_mtk_remove(struct platform_device *dev)
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
 
 	usb_remove_hcd(xhci->shared_hcd);
-	xhci_mtk_phy_power_off(mtk);
-	xhci_mtk_phy_exit(mtk);
 	device_init_wakeup(&dev->dev, false);
 
 	usb_remove_hcd(hcd);
@@ -718,7 +626,6 @@ static int __maybe_unused xhci_mtk_suspend(struct device *dev)
 	del_timer_sync(&xhci->shared_hcd->rh_timer);
 
 	xhci_mtk_host_disable(mtk);
-	xhci_mtk_phy_power_off(mtk);
 	xhci_mtk_clks_disable(mtk);
 	usb_wakeup_set(mtk, true);
 	return 0;
@@ -732,7 +639,6 @@ static int __maybe_unused xhci_mtk_resume(struct device *dev)
 
 	usb_wakeup_set(mtk, false);
 	xhci_mtk_clks_enable(mtk);
-	xhci_mtk_phy_power_on(mtk);
 	xhci_mtk_host_enable(mtk);
 
 	xhci_dbg(xhci, "%s: restart port polling\n", __func__);
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 6/8] usb: host: ehci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, arnd-r2nGTMty4D4,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA
  Cc: mark.rutland-5wv7dgnIgG8, devicetree-u79uwXL29TY76Z2rM5mHXA,
	Peter.Chen-3arQi8VN3Tc, Martin Blumenstingl,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ehci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
Acked-by: Alan Stern <stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org>
---
 drivers/usb/host/ehci-platform.c | 55 +++-------------------------------------
 1 file changed, 4 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b91eea8c73ae..4c306fb6b069 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -27,7 +27,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
 #include <linux/usb.h>
@@ -44,8 +43,6 @@
 struct ehci_platform_priv {
 	struct clk *clks[EHCI_MAX_CLKS];
 	struct reset_control *rsts;
-	struct phy **phys;
-	int num_phys;
 	bool reset_on_resume;
 };
 
@@ -80,7 +77,7 @@ static int ehci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -88,24 +85,8 @@ static int ehci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -117,12 +98,7 @@ static void ehci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -149,7 +125,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ehci_platform_priv *priv;
 	struct ehci_hcd *ehci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -202,29 +178,6 @@ static int ehci_platform_probe(struct platform_device *dev)
 					  "has-transaction-translator"))
 			hcd->has_tt = 1;
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-					goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -306,7 +259,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ehci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,6/8] usb: host: ehci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ehci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ehci-platform.c | 55 +++-------------------------------------
 1 file changed, 4 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b91eea8c73ae..4c306fb6b069 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -27,7 +27,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
 #include <linux/usb.h>
@@ -44,8 +43,6 @@
 struct ehci_platform_priv {
 	struct clk *clks[EHCI_MAX_CLKS];
 	struct reset_control *rsts;
-	struct phy **phys;
-	int num_phys;
 	bool reset_on_resume;
 };
 
@@ -80,7 +77,7 @@ static int ehci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -88,24 +85,8 @@ static int ehci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -117,12 +98,7 @@ static void ehci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -149,7 +125,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ehci_platform_priv *priv;
 	struct ehci_hcd *ehci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -202,29 +178,6 @@ static int ehci_platform_probe(struct platform_device *dev)
 					  "has-transaction-translator"))
 			hcd->has_tt = 1;
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-					goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -306,7 +259,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ehci_platform_defaults)
 		dev->dev.platform_data = NULL;
 

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 6/8] usb: host: ehci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ehci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ehci-platform.c | 55 +++-------------------------------------
 1 file changed, 4 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b91eea8c73ae..4c306fb6b069 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -27,7 +27,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
 #include <linux/usb.h>
@@ -44,8 +43,6 @@
 struct ehci_platform_priv {
 	struct clk *clks[EHCI_MAX_CLKS];
 	struct reset_control *rsts;
-	struct phy **phys;
-	int num_phys;
 	bool reset_on_resume;
 };
 
@@ -80,7 +77,7 @@ static int ehci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -88,24 +85,8 @@ static int ehci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -117,12 +98,7 @@ static void ehci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -149,7 +125,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ehci_platform_priv *priv;
 	struct ehci_hcd *ehci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -202,29 +178,6 @@ static int ehci_platform_probe(struct platform_device *dev)
 					  "has-transaction-translator"))
 			hcd->has_tt = 1;
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-					goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -306,7 +259,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ehci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 6/8] usb: host: ehci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45     ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linus-amlogic

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ehci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ehci-platform.c | 55 +++-------------------------------------
 1 file changed, 4 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index b91eea8c73ae..4c306fb6b069 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -27,7 +27,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
 #include <linux/usb.h>
@@ -44,8 +43,6 @@
 struct ehci_platform_priv {
 	struct clk *clks[EHCI_MAX_CLKS];
 	struct reset_control *rsts;
-	struct phy **phys;
-	int num_phys;
 	bool reset_on_resume;
 };
 
@@ -80,7 +77,7 @@ static int ehci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -88,24 +85,8 @@ static int ehci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -117,12 +98,7 @@ static void ehci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -149,7 +125,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ehci_platform_priv *priv;
 	struct ehci_hcd *ehci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -202,29 +178,6 @@ static int ehci_platform_probe(struct platform_device *dev)
 					  "has-transaction-translator"))
 			hcd->has_tt = 1;
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-					goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -306,7 +259,7 @@ static int ehci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ehci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 7/8] usb: host: ohci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: mark.rutland, devicetree, Peter.Chen, Martin Blumenstingl,
	narmstrong, yixun.lan, robh+dt, jonathanh, linux, matthias.bgg,
	thierry.reding, linux-mediatek, linux-amlogic, linux-tegra,
	stern, linux-omap, linux-arm-kernel

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ohci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ohci-platform.c | 56 ++++------------------------------------
 1 file changed, 5 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 62ef36a9333f..65a1c3fdc88c 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -21,7 +21,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/err.h>
-#include <linux/phy/phy.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
@@ -38,8 +38,6 @@
 struct ohci_platform_priv {
 	struct clk *clks[OHCI_MAX_CLKS];
 	struct reset_control *resets;
-	struct phy **phys;
-	int num_phys;
 };
 
 static const char hcd_name[] = "ohci-platform";
@@ -48,7 +46,7 @@ static int ohci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -56,24 +54,8 @@ static int ohci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -85,12 +67,7 @@ static void ohci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -117,7 +94,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ohci_platform_priv *priv;
 	struct ohci_hcd *ohci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -169,29 +146,6 @@ static int ohci_platform_probe(struct platform_device *dev)
 		of_property_read_u32(dev->dev.of_node, "num-ports",
 				     &ohci->num_ports);
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-				goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -277,7 +231,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ohci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,7/8] usb: host: ohci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ohci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ohci-platform.c | 56 ++++------------------------------------
 1 file changed, 5 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 62ef36a9333f..65a1c3fdc88c 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -21,7 +21,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/err.h>
-#include <linux/phy/phy.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
@@ -38,8 +38,6 @@
 struct ohci_platform_priv {
 	struct clk *clks[OHCI_MAX_CLKS];
 	struct reset_control *resets;
-	struct phy **phys;
-	int num_phys;
 };
 
 static const char hcd_name[] = "ohci-platform";
@@ -48,7 +46,7 @@ static int ohci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -56,24 +54,8 @@ static int ohci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -85,12 +67,7 @@ static void ohci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -117,7 +94,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ohci_platform_priv *priv;
 	struct ohci_hcd *ohci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -169,29 +146,6 @@ static int ohci_platform_probe(struct platform_device *dev)
 		of_property_read_u32(dev->dev.of_node, "num-ports",
 				     &ohci->num_ports);
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-				goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -277,7 +231,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ohci_platform_defaults)
 		dev->dev.platform_data = NULL;
 

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 7/8] usb: host: ohci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ohci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ohci-platform.c | 56 ++++------------------------------------
 1 file changed, 5 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 62ef36a9333f..65a1c3fdc88c 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -21,7 +21,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/err.h>
-#include <linux/phy/phy.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
@@ -38,8 +38,6 @@
 struct ohci_platform_priv {
 	struct clk *clks[OHCI_MAX_CLKS];
 	struct reset_control *resets;
-	struct phy **phys;
-	int num_phys;
 };
 
 static const char hcd_name[] = "ohci-platform";
@@ -48,7 +46,7 @@ static int ohci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -56,24 +54,8 @@ static int ohci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -85,12 +67,7 @@ static void ohci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -117,7 +94,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ohci_platform_priv *priv;
 	struct ohci_hcd *ohci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -169,29 +146,6 @@ static int ohci_platform_probe(struct platform_device *dev)
 		of_property_read_u32(dev->dev.of_node, "num-ports",
 				     &ohci->num_ports);
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-				goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -277,7 +231,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ohci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 7/8] usb: host: ohci-platform: remove custom USB PHY handling
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linus-amlogic

The new PHY wrapper is now wired up in the core HCD code. This means
that PHYs are now controlled (initialized, enabled, disabled, exited)
without requiring any host-driver specific code.
Remove the custom USB PHY handling from the ohci-platform driver as the
core HCD code now handles this.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---
 drivers/usb/host/ohci-platform.c | 56 ++++------------------------------------
 1 file changed, 5 insertions(+), 51 deletions(-)

diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 62ef36a9333f..65a1c3fdc88c 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -21,7 +21,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/err.h>
-#include <linux/phy/phy.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
@@ -38,8 +38,6 @@
 struct ohci_platform_priv {
 	struct clk *clks[OHCI_MAX_CLKS];
 	struct reset_control *resets;
-	struct phy **phys;
-	int num_phys;
 };
 
 static const char hcd_name[] = "ohci-platform";
@@ -48,7 +46,7 @@ static int ohci_platform_power_on(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, ret, phy_num;
+	int clk, ret;
 
 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
 		ret = clk_prepare_enable(priv->clks[clk]);
@@ -56,24 +54,8 @@ static int ohci_platform_power_on(struct platform_device *dev)
 			goto err_disable_clks;
 	}
 
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		ret = phy_init(priv->phys[phy_num]);
-		if (ret)
-			goto err_exit_phy;
-		ret = phy_power_on(priv->phys[phy_num]);
-		if (ret) {
-			phy_exit(priv->phys[phy_num]);
-			goto err_exit_phy;
-		}
-	}
-
 	return 0;
 
-err_exit_phy:
-	while (--phy_num >= 0) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
 err_disable_clks:
 	while (--clk >= 0)
 		clk_disable_unprepare(priv->clks[clk]);
@@ -85,12 +67,7 @@ static void ohci_platform_power_off(struct platform_device *dev)
 {
 	struct usb_hcd *hcd = platform_get_drvdata(dev);
 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
-	int clk, phy_num;
-
-	for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-		phy_power_off(priv->phys[phy_num]);
-		phy_exit(priv->phys[phy_num]);
-	}
+	int clk;
 
 	for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
 		if (priv->clks[clk])
@@ -117,7 +94,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 	struct ohci_platform_priv *priv;
 	struct ohci_hcd *ohci;
-	int err, irq, phy_num, clk = 0;
+	int err, irq, clk = 0;
 
 	if (usb_disabled())
 		return -ENODEV;
@@ -169,29 +146,6 @@ static int ohci_platform_probe(struct platform_device *dev)
 		of_property_read_u32(dev->dev.of_node, "num-ports",
 				     &ohci->num_ports);
 
-		priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
-				"phys", "#phy-cells");
-
-		if (priv->num_phys > 0) {
-			priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
-					    sizeof(struct phy *), GFP_KERNEL);
-			if (!priv->phys)
-				return -ENOMEM;
-		} else
-			priv->num_phys = 0;
-
-		for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
-			priv->phys[phy_num] = devm_of_phy_get_by_index(
-					&dev->dev, dev->dev.of_node, phy_num);
-			if (IS_ERR(priv->phys[phy_num])) {
-				err = PTR_ERR(priv->phys[phy_num]);
-				goto err_put_hcd;
-			} else {
-				/* Avoiding phy_get() in usb_add_hcd() */
-				hcd->skip_phy_initialization = 1;
-			}
-		}
-
 		for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 			if (IS_ERR(priv->clks[clk])) {
@@ -277,7 +231,7 @@ static int ohci_platform_probe(struct platform_device *dev)
 err_put_clks:
 	while (--clk >= 0)
 		clk_put(priv->clks[clk]);
-err_put_hcd:
+
 	if (pdata == &ohci_platform_defaults)
 		dev->dev.platform_data = NULL;
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 8/8] usb: core: hcd: remove support for initializing a single PHY
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: mark.rutland, devicetree, Peter.Chen, Martin Blumenstingl,
	narmstrong, yixun.lan, robh+dt, jonathanh, linux, matthias.bgg,
	thierry.reding, linux-mediatek, linux-amlogic, linux-tegra,
	stern, linux-omap, linux-arm-kernel

With the new PHY wrapper in place we can now handle multiple PHYs.
Remove the code which handles only one generic PHY as this is now
covered (with support for multiple PHYs as well as suspend/resume
support) by the new PHY wrapper.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c  | 37 -------------------------------------
 include/linux/usb/hcd.h |  1 -
 2 files changed, 38 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 32797c25ac3b..5a92d8f7c484 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2757,30 +2757,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
-		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
-
-		if (IS_ERR(phy)) {
-			retval = PTR_ERR(phy);
-			if (retval == -EPROBE_DEFER)
-				goto err_phy;
-		} else {
-			retval = phy_init(phy);
-			if (retval) {
-				phy_put(phy);
-				goto err_phy;
-			}
-			retval = phy_power_on(phy);
-			if (retval) {
-				phy_exit(phy);
-				phy_put(phy);
-				goto err_phy;
-			}
-			hcd->phy = phy;
-			hcd->remove_phy = 1;
-		}
-	}
-
 	if (!hcd->skip_phy_initialization) {
 		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
@@ -2961,13 +2937,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
 err_phy_roothub_init:
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
-err_phy:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
@@ -3048,12 +3017,6 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 	usb_phy_roothub_exit(hcd->phy_roothub);
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index a042675e03ba..aef50cb2ed1b 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -103,7 +103,6 @@ struct usb_hcd {
 	 * other external phys should be software-transparent
 	 */
 	struct usb_phy		*usb_phy;
-	struct phy		*phy;
 	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,8/8] usb: core: hcd: remove support for initializing a single PHY
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Martin Blumenstingl

With the new PHY wrapper in place we can now handle multiple PHYs.
Remove the code which handles only one generic PHY as this is now
covered (with support for multiple PHYs as well as suspend/resume
support) by the new PHY wrapper.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c  | 37 -------------------------------------
 include/linux/usb/hcd.h |  1 -
 2 files changed, 38 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 32797c25ac3b..5a92d8f7c484 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2757,30 +2757,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
-		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
-
-		if (IS_ERR(phy)) {
-			retval = PTR_ERR(phy);
-			if (retval == -EPROBE_DEFER)
-				goto err_phy;
-		} else {
-			retval = phy_init(phy);
-			if (retval) {
-				phy_put(phy);
-				goto err_phy;
-			}
-			retval = phy_power_on(phy);
-			if (retval) {
-				phy_exit(phy);
-				phy_put(phy);
-				goto err_phy;
-			}
-			hcd->phy = phy;
-			hcd->remove_phy = 1;
-		}
-	}
-
 	if (!hcd->skip_phy_initialization) {
 		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
@@ -2961,13 +2937,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
 err_phy_roothub_init:
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
-err_phy:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
@@ -3048,12 +3017,6 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 	usb_phy_roothub_exit(hcd->phy_roothub);
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index a042675e03ba..aef50cb2ed1b 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -103,7 +103,6 @@ struct usb_hcd {
 	 * other external phys should be software-transparent
 	 */
 	struct usb_phy		*usb_phy;
-	struct phy		*phy;
 	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 8/8] usb: core: hcd: remove support for initializing a single PHY
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

With the new PHY wrapper in place we can now handle multiple PHYs.
Remove the code which handles only one generic PHY as this is now
covered (with support for multiple PHYs as well as suspend/resume
support) by the new PHY wrapper.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c  | 37 -------------------------------------
 include/linux/usb/hcd.h |  1 -
 2 files changed, 38 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 32797c25ac3b..5a92d8f7c484 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2757,30 +2757,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
-		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
-
-		if (IS_ERR(phy)) {
-			retval = PTR_ERR(phy);
-			if (retval == -EPROBE_DEFER)
-				goto err_phy;
-		} else {
-			retval = phy_init(phy);
-			if (retval) {
-				phy_put(phy);
-				goto err_phy;
-			}
-			retval = phy_power_on(phy);
-			if (retval) {
-				phy_exit(phy);
-				phy_put(phy);
-				goto err_phy;
-			}
-			hcd->phy = phy;
-			hcd->remove_phy = 1;
-		}
-	}
-
 	if (!hcd->skip_phy_initialization) {
 		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
@@ -2961,13 +2937,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
 err_phy_roothub_init:
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
-err_phy:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
@@ -3048,12 +3017,6 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 	usb_phy_roothub_exit(hcd->phy_roothub);
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index a042675e03ba..aef50cb2ed1b 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -103,7 +103,6 @@ struct usb_hcd {
 	 * other external phys should be software-transparent
 	 */
 	struct usb_phy		*usb_phy;
-	struct phy		*phy;
 	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 8/8] usb: core: hcd: remove support for initializing a single PHY
@ 2018-02-18 18:45   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-02-18 18:45 UTC (permalink / raw)
  To: linus-amlogic

With the new PHY wrapper in place we can now handle multiple PHYs.
Remove the code which handles only one generic PHY as this is now
covered (with support for multiple PHYs as well as suspend/resume
support) by the new PHY wrapper.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c  | 37 -------------------------------------
 include/linux/usb/hcd.h |  1 -
 2 files changed, 38 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 32797c25ac3b..5a92d8f7c484 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2757,30 +2757,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 		}
 	}
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
-		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
-
-		if (IS_ERR(phy)) {
-			retval = PTR_ERR(phy);
-			if (retval == -EPROBE_DEFER)
-				goto err_phy;
-		} else {
-			retval = phy_init(phy);
-			if (retval) {
-				phy_put(phy);
-				goto err_phy;
-			}
-			retval = phy_power_on(phy);
-			if (retval) {
-				phy_exit(phy);
-				phy_put(phy);
-				goto err_phy;
-			}
-			hcd->phy = phy;
-			hcd->remove_phy = 1;
-		}
-	}
-
 	if (!hcd->skip_phy_initialization) {
 		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
@@ -2961,13 +2937,6 @@ int usb_add_hcd(struct usb_hcd *hcd,
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
 err_phy_roothub_init:
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
-err_phy:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
@@ -3048,12 +3017,6 @@ void usb_remove_hcd(struct usb_hcd *hcd)
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 	usb_phy_roothub_exit(hcd->phy_roothub);
 
-	if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
-		phy_power_off(hcd->phy);
-		phy_exit(hcd->phy);
-		phy_put(hcd->phy);
-		hcd->phy = NULL;
-	}
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index a042675e03ba..aef50cb2ed1b 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -103,7 +103,6 @@ struct usb_hcd {
 	 * other external phys should be software-transparent
 	 */
 	struct usb_phy		*usb_phy;
-	struct phy		*phy;
 	struct usb_phy_roothub	*phy_roothub;
 
 	/* Flags that need to be manipulated atomically because they can
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-26  3:26       ` Peter Chen
  0 siblings, 0 replies; 115+ messages in thread
From: Peter Chen @ 2018-02-26  3:26 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: mark.rutland, devicetree, yixun.lan, arnd, felipe.balbi,
	Peter.Chen, gregkh, linux-usb, narmstrong, robh+dt, jonathanh,
	mathias.nyman, matthias.bgg, thierry.reding, linux-mediatek,
	linux, linux-amlogic, linux-tegra, stern, linux-omap,
	linux-arm-kernel

On Sun, Feb 18, 2018 at 07:44:58PM +0100, Martin Blumenstingl wrote:
> The USB HCD core driver parses the device-tree node for "phys" and
> "usb-phys" properties. It also manages the power state of these PHYs
> automatically.
> However, drivers may opt-out of this behavior by setting "phy" or
> "usb_phy" in struct usb_hcd to a non-null value. An example where this
> is required is the "Qualcomm USB2 controller", implemented by the
> chipidea driver. The hardware requires that the PHY is only powered on
> after the "reset completed" event from the controller is received.
> 
> A follow-up patch will allow the USB HCD core driver to manage more than
> one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
> so drivers can opt-out of any PHY management provided by the USB HCD
> core driver.
> 
> This also updates the existing drivers so they use the new flag if they
> want to opt out of the PHY management provided by the USB HCD core
> driver. This means that for these drivers the new "multiple PHY"
> handling (which will be added in a follow-up patch) will be disabled as
> well.
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> ---
>  drivers/usb/chipidea/host.c      | 6 ++----
>  drivers/usb/core/hcd.c           | 4 ++--
>  drivers/usb/host/ehci-fsl.c      | 2 ++
>  drivers/usb/host/ehci-platform.c | 4 ++--
>  drivers/usb/host/ehci-tegra.c    | 1 +
>  drivers/usb/host/ohci-omap.c     | 1 +
>  drivers/usb/host/ohci-platform.c | 4 ++--
>  drivers/usb/host/xhci-plat.c     | 1 +
>  include/linux/usb/hcd.h          | 6 ++++++
>  9 files changed, 19 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
> index 19d60ed7e41f..af45aa3222b5 100644
> --- a/drivers/usb/chipidea/host.c
> +++ b/drivers/usb/chipidea/host.c
> @@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
>  
>  	hcd->power_budget = ci->platdata->power_budget;
>  	hcd->tpl_support = ci->platdata->tpl_support;
> -	if (ci->phy)
> -		hcd->phy = ci->phy;
> -	else
> -		hcd->usb_phy = ci->usb_phy;
> +	if (ci->phy || ci->usb_phy)
> +		hcd->skip_phy_initialization = 1;
>  
>  	ehci = hcd_to_ehci(hcd);
>  	ehci->caps = ci->hw_bank.cap;
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index fc32391a34d5..f2307470a31e 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  	int retval;
>  	struct usb_device *rhdev;
>  
> -	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
> +	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
>  		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
>  
>  		if (IS_ERR(phy)) {
> @@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  		}
>  	}
>  
> -	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
> +	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
>  		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
>  
>  		if (IS_ERR(phy)) {
> diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
> index c5094cb88cd5..0a9fd2022acf 100644
> --- a/drivers/usb/host/ehci-fsl.c
> +++ b/drivers/usb/host/ehci-fsl.c
> @@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
>  			retval = -ENODEV;
>  			goto err2;
>  		}
> +
> +		hcd->skip_phy_initialization = 1;
>  	}
>  #endif
>  	return retval;
> diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
> index b065a960adc2..b91eea8c73ae 100644
> --- a/drivers/usb/host/ehci-platform.c
> +++ b/drivers/usb/host/ehci-platform.c
> @@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  					goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
> index c809f7d2f08f..a6f4389f7e88 100644
> --- a/drivers/usb/host/ehci-tegra.c
> +++ b/drivers/usb/host/ehci-tegra.c
> @@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
>  		goto cleanup_clk_en;
>  	}
>  	hcd->usb_phy = u_phy;
> +	hcd->skip_phy_initialization = 1;
>  
>  	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
>  		"nvidia,needs-double-reset");
> diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
> index 0201c49bc4fc..d8d35d456456 100644
> --- a/drivers/usb/host/ohci-omap.c
> +++ b/drivers/usb/host/ohci-omap.c
> @@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
>  		} else {
>  			return -EPROBE_DEFER;
>  		}
> +		hcd->skip_phy_initialization = 1;
>  		ohci->start_hnp = start_hnp;
>  	}
>  #endif
> diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
> index 1e6c954f4b3f..62ef36a9333f 100644
> --- a/drivers/usb/host/ohci-platform.c
> +++ b/drivers/usb/host/ohci-platform.c
> @@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  				goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
> index 6f038306c14d..6700e5ee82ad 100644
> --- a/drivers/usb/host/xhci-plat.c
> +++ b/drivers/usb/host/xhci-plat.c
> @@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
>  		ret = usb_phy_init(hcd->usb_phy);
>  		if (ret)
>  			goto put_usb3_hcd;
> +		hcd->skip_phy_initialization = 1;
>  	}
>  
>  	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
> diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
> index 176900528822..693502c84c04 100644
> --- a/include/linux/usb/hcd.h
> +++ b/include/linux/usb/hcd.h
> @@ -151,6 +151,12 @@ struct usb_hcd {
>  	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
>  	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
>  	unsigned		remove_phy:1;	/* auto-remove USB phy */
> +	/*
> +	 * do not manage the PHY state in the HCD core, instead let the driver
> +	 * handle this (for example if the PHY can only be turned on after a
> +	 * specific event)
> +	 */
> +	unsigned		skip_phy_initialization:1;
>  
>  	/* The next flag is a stopgap, to be removed when all the HCDs
>  	 * support the new root-hub polling mechanism. */
> -- 

For chipidea part:

Acked-by: Peter Chen <peter.chen@nxp.com>

-- 

Best Regards,
Peter Chen

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-26  3:26       ` Peter Chen
  0 siblings, 0 replies; 115+ messages in thread
From: Peter Chen @ 2018-02-26  3:26 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan

On Sun, Feb 18, 2018 at 07:44:58PM +0100, Martin Blumenstingl wrote:
> The USB HCD core driver parses the device-tree node for "phys" and
> "usb-phys" properties. It also manages the power state of these PHYs
> automatically.
> However, drivers may opt-out of this behavior by setting "phy" or
> "usb_phy" in struct usb_hcd to a non-null value. An example where this
> is required is the "Qualcomm USB2 controller", implemented by the
> chipidea driver. The hardware requires that the PHY is only powered on
> after the "reset completed" event from the controller is received.
> 
> A follow-up patch will allow the USB HCD core driver to manage more than
> one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
> so drivers can opt-out of any PHY management provided by the USB HCD
> core driver.
> 
> This also updates the existing drivers so they use the new flag if they
> want to opt out of the PHY management provided by the USB HCD core
> driver. This means that for these drivers the new "multiple PHY"
> handling (which will be added in a follow-up patch) will be disabled as
> well.
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> ---
>  drivers/usb/chipidea/host.c      | 6 ++----
>  drivers/usb/core/hcd.c           | 4 ++--
>  drivers/usb/host/ehci-fsl.c      | 2 ++
>  drivers/usb/host/ehci-platform.c | 4 ++--
>  drivers/usb/host/ehci-tegra.c    | 1 +
>  drivers/usb/host/ohci-omap.c     | 1 +
>  drivers/usb/host/ohci-platform.c | 4 ++--
>  drivers/usb/host/xhci-plat.c     | 1 +
>  include/linux/usb/hcd.h          | 6 ++++++
>  9 files changed, 19 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
> index 19d60ed7e41f..af45aa3222b5 100644
> --- a/drivers/usb/chipidea/host.c
> +++ b/drivers/usb/chipidea/host.c
> @@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
>  
>  	hcd->power_budget = ci->platdata->power_budget;
>  	hcd->tpl_support = ci->platdata->tpl_support;
> -	if (ci->phy)
> -		hcd->phy = ci->phy;
> -	else
> -		hcd->usb_phy = ci->usb_phy;
> +	if (ci->phy || ci->usb_phy)
> +		hcd->skip_phy_initialization = 1;
>  
>  	ehci = hcd_to_ehci(hcd);
>  	ehci->caps = ci->hw_bank.cap;
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index fc32391a34d5..f2307470a31e 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  	int retval;
>  	struct usb_device *rhdev;
>  
> -	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
> +	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
>  		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
>  
>  		if (IS_ERR(phy)) {
> @@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  		}
>  	}
>  
> -	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
> +	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
>  		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
>  
>  		if (IS_ERR(phy)) {
> diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
> index c5094cb88cd5..0a9fd2022acf 100644
> --- a/drivers/usb/host/ehci-fsl.c
> +++ b/drivers/usb/host/ehci-fsl.c
> @@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
>  			retval = -ENODEV;
>  			goto err2;
>  		}
> +
> +		hcd->skip_phy_initialization = 1;
>  	}
>  #endif
>  	return retval;
> diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
> index b065a960adc2..b91eea8c73ae 100644
> --- a/drivers/usb/host/ehci-platform.c
> +++ b/drivers/usb/host/ehci-platform.c
> @@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  					goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
> index c809f7d2f08f..a6f4389f7e88 100644
> --- a/drivers/usb/host/ehci-tegra.c
> +++ b/drivers/usb/host/ehci-tegra.c
> @@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
>  		goto cleanup_clk_en;
>  	}
>  	hcd->usb_phy = u_phy;
> +	hcd->skip_phy_initialization = 1;
>  
>  	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
>  		"nvidia,needs-double-reset");
> diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
> index 0201c49bc4fc..d8d35d456456 100644
> --- a/drivers/usb/host/ohci-omap.c
> +++ b/drivers/usb/host/ohci-omap.c
> @@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
>  		} else {
>  			return -EPROBE_DEFER;
>  		}
> +		hcd->skip_phy_initialization = 1;
>  		ohci->start_hnp = start_hnp;
>  	}
>  #endif
> diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
> index 1e6c954f4b3f..62ef36a9333f 100644
> --- a/drivers/usb/host/ohci-platform.c
> +++ b/drivers/usb/host/ohci-platform.c
> @@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  				goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
> index 6f038306c14d..6700e5ee82ad 100644
> --- a/drivers/usb/host/xhci-plat.c
> +++ b/drivers/usb/host/xhci-plat.c
> @@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
>  		ret = usb_phy_init(hcd->usb_phy);
>  		if (ret)
>  			goto put_usb3_hcd;
> +		hcd->skip_phy_initialization = 1;
>  	}
>  
>  	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
> diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
> index 176900528822..693502c84c04 100644
> --- a/include/linux/usb/hcd.h
> +++ b/include/linux/usb/hcd.h
> @@ -151,6 +151,12 @@ struct usb_hcd {
>  	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
>  	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
>  	unsigned		remove_phy:1;	/* auto-remove USB phy */
> +	/*
> +	 * do not manage the PHY state in the HCD core, instead let the driver
> +	 * handle this (for example if the PHY can only be turned on after a
> +	 * specific event)
> +	 */
> +	unsigned		skip_phy_initialization:1;
>  
>  	/* The next flag is a stopgap, to be removed when all the HCDs
>  	 * support the new root-hub polling mechanism. */
> -- 

For chipidea part:

Acked-by: Peter Chen <peter.chen@nxp.com>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-26  3:26       ` Peter Chen
  0 siblings, 0 replies; 115+ messages in thread
From: Peter Chen @ 2018-02-26  3:26 UTC (permalink / raw)
  To: linux-arm-kernel

On Sun, Feb 18, 2018 at 07:44:58PM +0100, Martin Blumenstingl wrote:
> The USB HCD core driver parses the device-tree node for "phys" and
> "usb-phys" properties. It also manages the power state of these PHYs
> automatically.
> However, drivers may opt-out of this behavior by setting "phy" or
> "usb_phy" in struct usb_hcd to a non-null value. An example where this
> is required is the "Qualcomm USB2 controller", implemented by the
> chipidea driver. The hardware requires that the PHY is only powered on
> after the "reset completed" event from the controller is received.
> 
> A follow-up patch will allow the USB HCD core driver to manage more than
> one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
> so drivers can opt-out of any PHY management provided by the USB HCD
> core driver.
> 
> This also updates the existing drivers so they use the new flag if they
> want to opt out of the PHY management provided by the USB HCD core
> driver. This means that for these drivers the new "multiple PHY"
> handling (which will be added in a follow-up patch) will be disabled as
> well.
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> ---
>  drivers/usb/chipidea/host.c      | 6 ++----
>  drivers/usb/core/hcd.c           | 4 ++--
>  drivers/usb/host/ehci-fsl.c      | 2 ++
>  drivers/usb/host/ehci-platform.c | 4 ++--
>  drivers/usb/host/ehci-tegra.c    | 1 +
>  drivers/usb/host/ohci-omap.c     | 1 +
>  drivers/usb/host/ohci-platform.c | 4 ++--
>  drivers/usb/host/xhci-plat.c     | 1 +
>  include/linux/usb/hcd.h          | 6 ++++++
>  9 files changed, 19 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
> index 19d60ed7e41f..af45aa3222b5 100644
> --- a/drivers/usb/chipidea/host.c
> +++ b/drivers/usb/chipidea/host.c
> @@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
>  
>  	hcd->power_budget = ci->platdata->power_budget;
>  	hcd->tpl_support = ci->platdata->tpl_support;
> -	if (ci->phy)
> -		hcd->phy = ci->phy;
> -	else
> -		hcd->usb_phy = ci->usb_phy;
> +	if (ci->phy || ci->usb_phy)
> +		hcd->skip_phy_initialization = 1;
>  
>  	ehci = hcd_to_ehci(hcd);
>  	ehci->caps = ci->hw_bank.cap;
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index fc32391a34d5..f2307470a31e 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  	int retval;
>  	struct usb_device *rhdev;
>  
> -	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
> +	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
>  		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
>  
>  		if (IS_ERR(phy)) {
> @@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  		}
>  	}
>  
> -	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
> +	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
>  		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
>  
>  		if (IS_ERR(phy)) {
> diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
> index c5094cb88cd5..0a9fd2022acf 100644
> --- a/drivers/usb/host/ehci-fsl.c
> +++ b/drivers/usb/host/ehci-fsl.c
> @@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
>  			retval = -ENODEV;
>  			goto err2;
>  		}
> +
> +		hcd->skip_phy_initialization = 1;
>  	}
>  #endif
>  	return retval;
> diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
> index b065a960adc2..b91eea8c73ae 100644
> --- a/drivers/usb/host/ehci-platform.c
> +++ b/drivers/usb/host/ehci-platform.c
> @@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  					goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
> index c809f7d2f08f..a6f4389f7e88 100644
> --- a/drivers/usb/host/ehci-tegra.c
> +++ b/drivers/usb/host/ehci-tegra.c
> @@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
>  		goto cleanup_clk_en;
>  	}
>  	hcd->usb_phy = u_phy;
> +	hcd->skip_phy_initialization = 1;
>  
>  	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
>  		"nvidia,needs-double-reset");
> diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
> index 0201c49bc4fc..d8d35d456456 100644
> --- a/drivers/usb/host/ohci-omap.c
> +++ b/drivers/usb/host/ohci-omap.c
> @@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
>  		} else {
>  			return -EPROBE_DEFER;
>  		}
> +		hcd->skip_phy_initialization = 1;
>  		ohci->start_hnp = start_hnp;
>  	}
>  #endif
> diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
> index 1e6c954f4b3f..62ef36a9333f 100644
> --- a/drivers/usb/host/ohci-platform.c
> +++ b/drivers/usb/host/ohci-platform.c
> @@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  				goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
> index 6f038306c14d..6700e5ee82ad 100644
> --- a/drivers/usb/host/xhci-plat.c
> +++ b/drivers/usb/host/xhci-plat.c
> @@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
>  		ret = usb_phy_init(hcd->usb_phy);
>  		if (ret)
>  			goto put_usb3_hcd;
> +		hcd->skip_phy_initialization = 1;
>  	}
>  
>  	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
> diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
> index 176900528822..693502c84c04 100644
> --- a/include/linux/usb/hcd.h
> +++ b/include/linux/usb/hcd.h
> @@ -151,6 +151,12 @@ struct usb_hcd {
>  	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
>  	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
>  	unsigned		remove_phy:1;	/* auto-remove USB phy */
> +	/*
> +	 * do not manage the PHY state in the HCD core, instead let the driver
> +	 * handle this (for example if the PHY can only be turned on after a
> +	 * specific event)
> +	 */
> +	unsigned		skip_phy_initialization:1;
>  
>  	/* The next flag is a stopgap, to be removed when all the HCDs
>  	 * support the new root-hub polling mechanism. */
> -- 

For chipidea part:

Acked-by: Peter Chen <peter.chen@nxp.com>

-- 

Best Regards,
Peter Chen

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd
@ 2018-02-26  3:26       ` Peter Chen
  0 siblings, 0 replies; 115+ messages in thread
From: Peter Chen @ 2018-02-26  3:26 UTC (permalink / raw)
  To: linus-amlogic

On Sun, Feb 18, 2018 at 07:44:58PM +0100, Martin Blumenstingl wrote:
> The USB HCD core driver parses the device-tree node for "phys" and
> "usb-phys" properties. It also manages the power state of these PHYs
> automatically.
> However, drivers may opt-out of this behavior by setting "phy" or
> "usb_phy" in struct usb_hcd to a non-null value. An example where this
> is required is the "Qualcomm USB2 controller", implemented by the
> chipidea driver. The hardware requires that the PHY is only powered on
> after the "reset completed" event from the controller is received.
> 
> A follow-up patch will allow the USB HCD core driver to manage more than
> one PHY. Add a new "skip_phy_initialization" bitflag to struct usb_hcd
> so drivers can opt-out of any PHY management provided by the USB HCD
> core driver.
> 
> This also updates the existing drivers so they use the new flag if they
> want to opt out of the PHY management provided by the USB HCD core
> driver. This means that for these drivers the new "multiple PHY"
> handling (which will be added in a follow-up patch) will be disabled as
> well.
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> ---
>  drivers/usb/chipidea/host.c      | 6 ++----
>  drivers/usb/core/hcd.c           | 4 ++--
>  drivers/usb/host/ehci-fsl.c      | 2 ++
>  drivers/usb/host/ehci-platform.c | 4 ++--
>  drivers/usb/host/ehci-tegra.c    | 1 +
>  drivers/usb/host/ohci-omap.c     | 1 +
>  drivers/usb/host/ohci-platform.c | 4 ++--
>  drivers/usb/host/xhci-plat.c     | 1 +
>  include/linux/usb/hcd.h          | 6 ++++++
>  9 files changed, 19 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
> index 19d60ed7e41f..af45aa3222b5 100644
> --- a/drivers/usb/chipidea/host.c
> +++ b/drivers/usb/chipidea/host.c
> @@ -124,10 +124,8 @@ static int host_start(struct ci_hdrc *ci)
>  
>  	hcd->power_budget = ci->platdata->power_budget;
>  	hcd->tpl_support = ci->platdata->tpl_support;
> -	if (ci->phy)
> -		hcd->phy = ci->phy;
> -	else
> -		hcd->usb_phy = ci->usb_phy;
> +	if (ci->phy || ci->usb_phy)
> +		hcd->skip_phy_initialization = 1;
>  
>  	ehci = hcd_to_ehci(hcd);
>  	ehci->caps = ci->hw_bank.cap;
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index fc32391a34d5..f2307470a31e 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2727,7 +2727,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  	int retval;
>  	struct usb_device *rhdev;
>  
> -	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
> +	if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->skip_phy_initialization) {
>  		struct usb_phy *phy = usb_get_phy_dev(hcd->self.sysdev, 0);
>  
>  		if (IS_ERR(phy)) {
> @@ -2745,7 +2745,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  		}
>  	}
>  
> -	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
> +	if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->skip_phy_initialization) {
>  		struct phy *phy = phy_get(hcd->self.sysdev, "usb");
>  
>  		if (IS_ERR(phy)) {
> diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
> index c5094cb88cd5..0a9fd2022acf 100644
> --- a/drivers/usb/host/ehci-fsl.c
> +++ b/drivers/usb/host/ehci-fsl.c
> @@ -155,6 +155,8 @@ static int fsl_ehci_drv_probe(struct platform_device *pdev)
>  			retval = -ENODEV;
>  			goto err2;
>  		}
> +
> +		hcd->skip_phy_initialization = 1;
>  	}
>  #endif
>  	return retval;
> diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
> index b065a960adc2..b91eea8c73ae 100644
> --- a/drivers/usb/host/ehci-platform.c
> +++ b/drivers/usb/host/ehci-platform.c
> @@ -219,9 +219,9 @@ static int ehci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  					goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
> index c809f7d2f08f..a6f4389f7e88 100644
> --- a/drivers/usb/host/ehci-tegra.c
> +++ b/drivers/usb/host/ehci-tegra.c
> @@ -461,6 +461,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
>  		goto cleanup_clk_en;
>  	}
>  	hcd->usb_phy = u_phy;
> +	hcd->skip_phy_initialization = 1;
>  
>  	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
>  		"nvidia,needs-double-reset");
> diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
> index 0201c49bc4fc..d8d35d456456 100644
> --- a/drivers/usb/host/ohci-omap.c
> +++ b/drivers/usb/host/ohci-omap.c
> @@ -230,6 +230,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
>  		} else {
>  			return -EPROBE_DEFER;
>  		}
> +		hcd->skip_phy_initialization = 1;
>  		ohci->start_hnp = start_hnp;
>  	}
>  #endif
> diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
> index 1e6c954f4b3f..62ef36a9333f 100644
> --- a/drivers/usb/host/ohci-platform.c
> +++ b/drivers/usb/host/ohci-platform.c
> @@ -186,9 +186,9 @@ static int ohci_platform_probe(struct platform_device *dev)
>  			if (IS_ERR(priv->phys[phy_num])) {
>  				err = PTR_ERR(priv->phys[phy_num]);
>  				goto err_put_hcd;
> -			} else if (!hcd->phy) {
> +			} else {
>  				/* Avoiding phy_get() in usb_add_hcd() */
> -				hcd->phy = priv->phys[phy_num];
> +				hcd->skip_phy_initialization = 1;
>  			}
>  		}
>  
> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
> index 6f038306c14d..6700e5ee82ad 100644
> --- a/drivers/usb/host/xhci-plat.c
> +++ b/drivers/usb/host/xhci-plat.c
> @@ -284,6 +284,7 @@ static int xhci_plat_probe(struct platform_device *pdev)
>  		ret = usb_phy_init(hcd->usb_phy);
>  		if (ret)
>  			goto put_usb3_hcd;
> +		hcd->skip_phy_initialization = 1;
>  	}
>  
>  	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
> diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
> index 176900528822..693502c84c04 100644
> --- a/include/linux/usb/hcd.h
> +++ b/include/linux/usb/hcd.h
> @@ -151,6 +151,12 @@ struct usb_hcd {
>  	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
>  	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
>  	unsigned		remove_phy:1;	/* auto-remove USB phy */
> +	/*
> +	 * do not manage the PHY state in the HCD core, instead let the driver
> +	 * handle this (for example if the PHY can only be turned on after a
> +	 * specific event)
> +	 */
> +	unsigned		skip_phy_initialization:1;
>  
>  	/* The next flag is a stopgap, to be removed when all the HCDs
>  	 * support the new root-hub polling mechanism. */
> -- 

For chipidea part:

Acked-by: Peter Chen <peter.chen@nxp.com>

-- 

Best Regards,
Peter Chen

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-16 14:32       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-16 14:32 UTC (permalink / raw)
  To: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi
  Cc: mark.rutland, devicetree, Chunfeng Yun, Gerlach, Dave,
	Peter.Chen, Keerthy, narmstrong, yixun.lan, robh+dt, jonathanh,
	linux, matthias.bgg, thierry.reding, linux-mediatek,
	linux-amlogic, linux-tegra, stern, KISHON VIJAY ABRAHAM,
	linux-omap, linux-arm-kernel

+some TI folks

Hi Martin,

On 18/02/18 20:44, Martin Blumenstingl wrote:
> Many SoC platforms have separate devices for the USB PHY which are
> registered through the generic PHY framework. These PHYs have to be
> enabled to make the USB controller actually work. They also have to be
> disabled again on shutdown/suspend.
> 
> Currently (at least) the following HCI platform drivers are using custom
> code to obtain all PHYs via devicetree for the roothub/controller and
> disable/enable them when required:
> - ehci-platform.c has ehci_platform_power_{on,off}
> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> - ohci-platform.c has ohci_platform_power_{on,off}
> 
> With this new wrapper the USB PHYs can be specified directly in the
> USB controller's devicetree node (just like on the drivers listed
> above). This allows SoCs like the Amlogic Meson GXL family to operate
> correctly once this is wired up correctly. These SoCs use a dwc3
> controller and require all USB PHYs to be initialized (if one of the USB
> PHYs it not initialized then none of USB port works at all).
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> Cc: Neil Armstrong <narmstrong@baylibre.com>
> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>

This patch is breaking low power cases on TI SoCs when USB is in host mode.
I'll explain why below.

> ---
>  drivers/usb/core/Makefile |   2 +-
>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h    |   7 ++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/core/phy.c
>  create mode 100644 drivers/usb/core/phy.h
> 
> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 92c9cefb4317..18e874b0441e 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -6,7 +6,7 @@
>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> -usbcore-y += port.o
> +usbcore-y += phy.o port.o
>  
>  usbcore-$(CONFIG_OF)		+= of.o
>  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> new file mode 100644
> index 000000000000..09b7c43c0ea4
> --- /dev/null
> +++ b/drivers/usb/core/phy.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> + * multiple (actual) PHY devices. This is comes handy when initializing
> + * all PHYs on a HCD and to keep them all in the same state.
> + *
> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> + */
> +
> +#include <linux/device.h>
> +#include <linux/list.h>
> +#include <linux/phy/phy.h>
> +#include <linux/of.h>
> +
> +#include "phy.h"
> +
> +struct usb_phy_roothub {
> +	struct phy		*phy;
> +	struct list_head	list;
> +};
> +
> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	return roothub_entry;
> +}
> +
> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> +				   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> +
> +	if (IS_ERR_OR_NULL(phy)) {
> +		if (!phy || PTR_ERR(phy) == -ENODEV)
> +			return 0;
> +		else
> +			return PTR_ERR(phy);
> +	}
> +
> +	roothub_entry = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(roothub_entry))
> +		return PTR_ERR(roothub_entry);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> +{
> +	struct usb_phy_roothub *phy_roothub;
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int i, num_phys, err;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(phy_roothub))
> +		return phy_roothub;
> +
> +	for (i = 0; i < num_phys; i++) {
> +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_init(roothub_entry->phy);

The phy_init() function actually enables the PHY clocks.
It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().

> +		if (err)
> +			goto err_exit_phys;
> +	}
> +
> +	return phy_roothub;
> +
> +err_exit_phys:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_exit(roothub_entry->phy);
> +
> +err_out:
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> +
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err, ret = 0;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_exit(roothub_entry->phy);
> +		if (err)
> +			ret = ret;
> +	}

phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().

With that there is nothing else being done here. Shouldn't we be doing the equivalent of
usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?

> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	return 0;
> +
> +err_out:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_power_off(roothub_entry->phy);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> +
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	if (!phy_roothub)
> +		return;
> +
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +		phy_power_off(roothub_entry->phy);

Not doing the phy_exit() here leaves the clocks enabled on our SoC and
we're no longer able to reach low power states on system suspend.

> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> new file mode 100644
> index 000000000000..6fde59bfbff8
> --- /dev/null
> +++ b/drivers/usb/core/phy.h
> @@ -0,0 +1,7 @@
> +struct usb_phy_roothub;
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 

The following patch fixes the issue for me. Let me know what you think and I can post it officially.

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43..23232d3 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
-	}
-
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_init(roothub_entry->phy);
-		if (err)
-			goto err_exit_phys;
+			return ERR_PTR(err);
 	}
 
 	return phy_roothub;
-
-err_exit_phys:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
-		phy_exit(roothub_entry->phy);
-
-err_out:
-	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
@@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return 0;
 
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_exit(roothub_entry->phy);
-		if (err)
-			ret = ret;
-	}
+	/* TODO: usb_phy_roothub_del_phy */
+	/* TODO: usb_phy_roothub_free */
 
 	return ret;
 }
@@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_power_on(roothub_entry->phy);
+		err = phy_init(roothub_entry->phy);
 		if (err)
 			goto err_out;
+		err = phy_power_on(roothub_entry->phy);
+		if (err) {
+			phy_exit(roothub_entry->phy);
+			goto err_out;
+		}
 	}
 
 	return 0;
 
 err_out:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 
 	return err;
 }
@@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return;
 
-	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);


-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-16 14:32       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-16 14:32 UTC (permalink / raw)
  To: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi
  Cc: linux-omap, linux-tegra, linux-mediatek, linux-arm-kernel,
	devicetree, jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, narmstrong, linux-amlogic,
	yixun.lan, Chunfeng Yun, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

+some TI folks

Hi Martin,

On 18/02/18 20:44, Martin Blumenstingl wrote:
> Many SoC platforms have separate devices for the USB PHY which are
> registered through the generic PHY framework. These PHYs have to be
> enabled to make the USB controller actually work. They also have to be
> disabled again on shutdown/suspend.
> 
> Currently (at least) the following HCI platform drivers are using custom
> code to obtain all PHYs via devicetree for the roothub/controller and
> disable/enable them when required:
> - ehci-platform.c has ehci_platform_power_{on,off}
> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> - ohci-platform.c has ohci_platform_power_{on,off}
> 
> With this new wrapper the USB PHYs can be specified directly in the
> USB controller's devicetree node (just like on the drivers listed
> above). This allows SoCs like the Amlogic Meson GXL family to operate
> correctly once this is wired up correctly. These SoCs use a dwc3
> controller and require all USB PHYs to be initialized (if one of the USB
> PHYs it not initialized then none of USB port works at all).
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> Cc: Neil Armstrong <narmstrong@baylibre.com>
> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>

This patch is breaking low power cases on TI SoCs when USB is in host mode.
I'll explain why below.

> ---
>  drivers/usb/core/Makefile |   2 +-
>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h    |   7 ++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/core/phy.c
>  create mode 100644 drivers/usb/core/phy.h
> 
> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 92c9cefb4317..18e874b0441e 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -6,7 +6,7 @@
>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> -usbcore-y += port.o
> +usbcore-y += phy.o port.o
>  
>  usbcore-$(CONFIG_OF)		+= of.o
>  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> new file mode 100644
> index 000000000000..09b7c43c0ea4
> --- /dev/null
> +++ b/drivers/usb/core/phy.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> + * multiple (actual) PHY devices. This is comes handy when initializing
> + * all PHYs on a HCD and to keep them all in the same state.
> + *
> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> + */
> +
> +#include <linux/device.h>
> +#include <linux/list.h>
> +#include <linux/phy/phy.h>
> +#include <linux/of.h>
> +
> +#include "phy.h"
> +
> +struct usb_phy_roothub {
> +	struct phy		*phy;
> +	struct list_head	list;
> +};
> +
> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	return roothub_entry;
> +}
> +
> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> +				   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> +
> +	if (IS_ERR_OR_NULL(phy)) {
> +		if (!phy || PTR_ERR(phy) == -ENODEV)
> +			return 0;
> +		else
> +			return PTR_ERR(phy);
> +	}
> +
> +	roothub_entry = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(roothub_entry))
> +		return PTR_ERR(roothub_entry);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> +{
> +	struct usb_phy_roothub *phy_roothub;
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int i, num_phys, err;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(phy_roothub))
> +		return phy_roothub;
> +
> +	for (i = 0; i < num_phys; i++) {
> +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_init(roothub_entry->phy);

The phy_init() function actually enables the PHY clocks.
It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().

> +		if (err)
> +			goto err_exit_phys;
> +	}
> +
> +	return phy_roothub;
> +
> +err_exit_phys:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_exit(roothub_entry->phy);
> +
> +err_out:
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> +
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err, ret = 0;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_exit(roothub_entry->phy);
> +		if (err)
> +			ret = ret;
> +	}

phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().

With that there is nothing else being done here. Shouldn't we be doing the equivalent of
usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?

> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	return 0;
> +
> +err_out:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_power_off(roothub_entry->phy);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> +
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	if (!phy_roothub)
> +		return;
> +
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +		phy_power_off(roothub_entry->phy);

Not doing the phy_exit() here leaves the clocks enabled on our SoC and
we're no longer able to reach low power states on system suspend.

> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> new file mode 100644
> index 000000000000..6fde59bfbff8
> --- /dev/null
> +++ b/drivers/usb/core/phy.h
> @@ -0,0 +1,7 @@
> +struct usb_phy_roothub;
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 

The following patch fixes the issue for me. Let me know what you think and I can post it officially.

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43..23232d3 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
-	}
-
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_init(roothub_entry->phy);
-		if (err)
-			goto err_exit_phys;
+			return ERR_PTR(err);
 	}
 
 	return phy_roothub;
-
-err_exit_phys:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
-		phy_exit(roothub_entry->phy);
-
-err_out:
-	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
@@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return 0;
 
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_exit(roothub_entry->phy);
-		if (err)
-			ret = ret;
-	}
+	/* TODO: usb_phy_roothub_del_phy */
+	/* TODO: usb_phy_roothub_free */
 
 	return ret;
 }
@@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_power_on(roothub_entry->phy);
+		err = phy_init(roothub_entry->phy);
 		if (err)
 			goto err_out;
+		err = phy_power_on(roothub_entry->phy);
+		if (err) {
+			phy_exit(roothub_entry->phy);
+			goto err_out;
+		}
 	}
 
 	return 0;
 
 err_out:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 
 	return err;
 }
@@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return;
 
-	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-16 14:32       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-16 14:32 UTC (permalink / raw)
  To: linux-arm-kernel

+some TI folks

Hi Martin,

On 18/02/18 20:44, Martin Blumenstingl wrote:
> Many SoC platforms have separate devices for the USB PHY which are
> registered through the generic PHY framework. These PHYs have to be
> enabled to make the USB controller actually work. They also have to be
> disabled again on shutdown/suspend.
> 
> Currently (at least) the following HCI platform drivers are using custom
> code to obtain all PHYs via devicetree for the roothub/controller and
> disable/enable them when required:
> - ehci-platform.c has ehci_platform_power_{on,off}
> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> - ohci-platform.c has ohci_platform_power_{on,off}
> 
> With this new wrapper the USB PHYs can be specified directly in the
> USB controller's devicetree node (just like on the drivers listed
> above). This allows SoCs like the Amlogic Meson GXL family to operate
> correctly once this is wired up correctly. These SoCs use a dwc3
> controller and require all USB PHYs to be initialized (if one of the USB
> PHYs it not initialized then none of USB port works at all).
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> Cc: Neil Armstrong <narmstrong@baylibre.com>
> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>

This patch is breaking low power cases on TI SoCs when USB is in host mode.
I'll explain why below.

> ---
>  drivers/usb/core/Makefile |   2 +-
>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h    |   7 ++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/core/phy.c
>  create mode 100644 drivers/usb/core/phy.h
> 
> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 92c9cefb4317..18e874b0441e 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -6,7 +6,7 @@
>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> -usbcore-y += port.o
> +usbcore-y += phy.o port.o
>  
>  usbcore-$(CONFIG_OF)		+= of.o
>  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> new file mode 100644
> index 000000000000..09b7c43c0ea4
> --- /dev/null
> +++ b/drivers/usb/core/phy.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> + * multiple (actual) PHY devices. This is comes handy when initializing
> + * all PHYs on a HCD and to keep them all in the same state.
> + *
> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> + */
> +
> +#include <linux/device.h>
> +#include <linux/list.h>
> +#include <linux/phy/phy.h>
> +#include <linux/of.h>
> +
> +#include "phy.h"
> +
> +struct usb_phy_roothub {
> +	struct phy		*phy;
> +	struct list_head	list;
> +};
> +
> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	return roothub_entry;
> +}
> +
> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> +				   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> +
> +	if (IS_ERR_OR_NULL(phy)) {
> +		if (!phy || PTR_ERR(phy) == -ENODEV)
> +			return 0;
> +		else
> +			return PTR_ERR(phy);
> +	}
> +
> +	roothub_entry = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(roothub_entry))
> +		return PTR_ERR(roothub_entry);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> +{
> +	struct usb_phy_roothub *phy_roothub;
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int i, num_phys, err;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(phy_roothub))
> +		return phy_roothub;
> +
> +	for (i = 0; i < num_phys; i++) {
> +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_init(roothub_entry->phy);

The phy_init() function actually enables the PHY clocks.
It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().

> +		if (err)
> +			goto err_exit_phys;
> +	}
> +
> +	return phy_roothub;
> +
> +err_exit_phys:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_exit(roothub_entry->phy);
> +
> +err_out:
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> +
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err, ret = 0;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_exit(roothub_entry->phy);
> +		if (err)
> +			ret = ret;
> +	}

phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().

With that there is nothing else being done here. Shouldn't we be doing the equivalent of
usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?

> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	return 0;
> +
> +err_out:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_power_off(roothub_entry->phy);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> +
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	if (!phy_roothub)
> +		return;
> +
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +		phy_power_off(roothub_entry->phy);

Not doing the phy_exit() here leaves the clocks enabled on our SoC and
we're no longer able to reach low power states on system suspend.

> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> new file mode 100644
> index 000000000000..6fde59bfbff8
> --- /dev/null
> +++ b/drivers/usb/core/phy.h
> @@ -0,0 +1,7 @@
> +struct usb_phy_roothub;
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 

The following patch fixes the issue for me. Let me know what you think and I can post it officially.

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43..23232d3 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
-	}
-
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_init(roothub_entry->phy);
-		if (err)
-			goto err_exit_phys;
+			return ERR_PTR(err);
 	}
 
 	return phy_roothub;
-
-err_exit_phys:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
-		phy_exit(roothub_entry->phy);
-
-err_out:
-	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
@@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return 0;
 
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_exit(roothub_entry->phy);
-		if (err)
-			ret = ret;
-	}
+	/* TODO: usb_phy_roothub_del_phy */
+	/* TODO: usb_phy_roothub_free */
 
 	return ret;
 }
@@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_power_on(roothub_entry->phy);
+		err = phy_init(roothub_entry->phy);
 		if (err)
 			goto err_out;
+		err = phy_power_on(roothub_entry->phy);
+		if (err) {
+			phy_exit(roothub_entry->phy);
+			goto err_out;
+		}
 	}
 
 	return 0;
 
 err_out:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 
 	return err;
 }
@@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return;
 
-	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);


-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-16 14:32       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-16 14:32 UTC (permalink / raw)
  To: linus-amlogic

+some TI folks

Hi Martin,

On 18/02/18 20:44, Martin Blumenstingl wrote:
> Many SoC platforms have separate devices for the USB PHY which are
> registered through the generic PHY framework. These PHYs have to be
> enabled to make the USB controller actually work. They also have to be
> disabled again on shutdown/suspend.
> 
> Currently (at least) the following HCI platform drivers are using custom
> code to obtain all PHYs via devicetree for the roothub/controller and
> disable/enable them when required:
> - ehci-platform.c has ehci_platform_power_{on,off}
> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> - ohci-platform.c has ohci_platform_power_{on,off}
> 
> With this new wrapper the USB PHYs can be specified directly in the
> USB controller's devicetree node (just like on the drivers listed
> above). This allows SoCs like the Amlogic Meson GXL family to operate
> correctly once this is wired up correctly. These SoCs use a dwc3
> controller and require all USB PHYs to be initialized (if one of the USB
> PHYs it not initialized then none of USB port works at all).
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> Cc: Neil Armstrong <narmstrong@baylibre.com>
> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>

This patch is breaking low power cases on TI SoCs when USB is in host mode.
I'll explain why below.

> ---
>  drivers/usb/core/Makefile |   2 +-
>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h    |   7 ++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/core/phy.c
>  create mode 100644 drivers/usb/core/phy.h
> 
> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 92c9cefb4317..18e874b0441e 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -6,7 +6,7 @@
>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> -usbcore-y += port.o
> +usbcore-y += phy.o port.o
>  
>  usbcore-$(CONFIG_OF)		+= of.o
>  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> new file mode 100644
> index 000000000000..09b7c43c0ea4
> --- /dev/null
> +++ b/drivers/usb/core/phy.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> + * multiple (actual) PHY devices. This is comes handy when initializing
> + * all PHYs on a HCD and to keep them all in the same state.
> + *
> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> + */
> +
> +#include <linux/device.h>
> +#include <linux/list.h>
> +#include <linux/phy/phy.h>
> +#include <linux/of.h>
> +
> +#include "phy.h"
> +
> +struct usb_phy_roothub {
> +	struct phy		*phy;
> +	struct list_head	list;
> +};
> +
> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	return roothub_entry;
> +}
> +
> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> +				   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> +
> +	if (IS_ERR_OR_NULL(phy)) {
> +		if (!phy || PTR_ERR(phy) == -ENODEV)
> +			return 0;
> +		else
> +			return PTR_ERR(phy);
> +	}
> +
> +	roothub_entry = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(roothub_entry))
> +		return PTR_ERR(roothub_entry);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> +{
> +	struct usb_phy_roothub *phy_roothub;
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int i, num_phys, err;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(phy_roothub))
> +		return phy_roothub;
> +
> +	for (i = 0; i < num_phys; i++) {
> +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_init(roothub_entry->phy);

The phy_init() function actually enables the PHY clocks.
It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().

> +		if (err)
> +			goto err_exit_phys;
> +	}
> +
> +	return phy_roothub;
> +
> +err_exit_phys:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_exit(roothub_entry->phy);
> +
> +err_out:
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> +
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err, ret = 0;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_exit(roothub_entry->phy);
> +		if (err)
> +			ret = ret;
> +	}

phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().

With that there is nothing else being done here. Shouldn't we be doing the equivalent of
usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?

> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	return 0;
> +
> +err_out:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_power_off(roothub_entry->phy);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> +
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	if (!phy_roothub)
> +		return;
> +
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +		phy_power_off(roothub_entry->phy);

Not doing the phy_exit() here leaves the clocks enabled on our SoC and
we're no longer able to reach low power states on system suspend.

> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> new file mode 100644
> index 000000000000..6fde59bfbff8
> --- /dev/null
> +++ b/drivers/usb/core/phy.h
> @@ -0,0 +1,7 @@
> +struct usb_phy_roothub;
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 

The following patch fixes the issue for me. Let me know what you think and I can post it officially.

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43..23232d3 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
-	}
-
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_init(roothub_entry->phy);
-		if (err)
-			goto err_exit_phys;
+			return ERR_PTR(err);
 	}
 
 	return phy_roothub;
-
-err_exit_phys:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
-		phy_exit(roothub_entry->phy);
-
-err_out:
-	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
@@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return 0;
 
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_exit(roothub_entry->phy);
-		if (err)
-			ret = ret;
-	}
+	/* TODO: usb_phy_roothub_del_phy */
+	/* TODO: usb_phy_roothub_free */
 
 	return ret;
 }
@@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_power_on(roothub_entry->phy);
+		err = phy_init(roothub_entry->phy);
 		if (err)
 			goto err_out;
+		err = phy_power_on(roothub_entry->phy);
+		if (err) {
+			phy_exit(roothub_entry->phy);
+			goto err_out;
+		}
 	}
 
 	return 0;
 
 err_out:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 
 	return err;
 }
@@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return;
 
-	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);


-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-18 22:29           ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-18 22:29 UTC (permalink / raw)
  To: Roger Quadros, Chunfeng Yun
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

Hi Roger,

On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> +some TI folks
>
> Hi Martin,
>
> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> Many SoC platforms have separate devices for the USB PHY which are
>> registered through the generic PHY framework. These PHYs have to be
>> enabled to make the USB controller actually work. They also have to be
>> disabled again on shutdown/suspend.
>>
>> Currently (at least) the following HCI platform drivers are using custom
>> code to obtain all PHYs via devicetree for the roothub/controller and
>> disable/enable them when required:
>> - ehci-platform.c has ehci_platform_power_{on,off}
>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> - ohci-platform.c has ohci_platform_power_{on,off}
>>
>> With this new wrapper the USB PHYs can be specified directly in the
>> USB controller's devicetree node (just like on the drivers listed
>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> correctly once this is wired up correctly. These SoCs use a dwc3
>> controller and require all USB PHYs to be initialized (if one of the USB
>> PHYs it not initialized then none of USB port works at all).
>>
>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
based on your explanation and reading the TI PHY drivers I am assuming
that the affected SoCs are using the "phy-omap-usb2" driver

>> ---
>>  drivers/usb/core/Makefile |   2 +-
>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>  drivers/usb/core/phy.h    |   7 ++
>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/usb/core/phy.c
>>  create mode 100644 drivers/usb/core/phy.h
>>
>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> index 92c9cefb4317..18e874b0441e 100644
>> --- a/drivers/usb/core/Makefile
>> +++ b/drivers/usb/core/Makefile
>> @@ -6,7 +6,7 @@
>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> -usbcore-y += port.o
>> +usbcore-y += phy.o port.o
>>
>>  usbcore-$(CONFIG_OF)         += of.o
>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> new file mode 100644
>> index 000000000000..09b7c43c0ea4
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.c
>> @@ -0,0 +1,158 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> + * all PHYs on a HCD and to keep them all in the same state.
>> + *
>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>> + */
>> +
>> +#include <linux/device.h>
>> +#include <linux/list.h>
>> +#include <linux/phy/phy.h>
>> +#include <linux/of.h>
>> +
>> +#include "phy.h"
>> +
>> +struct usb_phy_roothub {
>> +     struct phy              *phy;
>> +     struct list_head        list;
>> +};
>> +
>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +     if (!roothub_entry)
>> +             return ERR_PTR(-ENOMEM);
>> +
>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> +
>> +     return roothub_entry;
>> +}
>> +
>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> +                                struct list_head *list)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> +
>> +     if (IS_ERR_OR_NULL(phy)) {
>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> +                     return 0;
>> +             else
>> +                     return PTR_ERR(phy);
>> +     }
>> +
>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(roothub_entry))
>> +             return PTR_ERR(roothub_entry);
>> +
>> +     roothub_entry->phy = phy;
>> +
>> +     list_add_tail(&roothub_entry->list, list);
>> +
>> +     return 0;
>> +}
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *phy_roothub;
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int i, num_phys, err;
>> +
>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> +                                           "#phy-cells");
>> +     if (num_phys <= 0)
>> +             return NULL;
>> +
>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(phy_roothub))
>> +             return phy_roothub;
>> +
>> +     for (i = 0; i < num_phys; i++) {
>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_init(roothub_entry->phy);
>
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
do you mean that phy_init should be moved to usb_phy_roothub_power_on
(just before phy_power_on is called within usb_phy_roothub_power_on)?

an earlier version of my patch did exactly this, but it caused
problems during a suspend/resume cycle on Mediatek devices
Chunfeng Yun reported that issue here [0], quote from that mail for
easier reading:
"In order to keep link state on mt8173, we just power off all phys(not
exit) when system enter suspend, then power on them again (needn't
init, otherwise device will be disconnected) when system resume, this
can avoid re-enumerating device."

>> +             if (err)
>> +                     goto err_exit_phys;
>> +     }
>> +
>> +     return phy_roothub;
>> +
>> +err_exit_phys:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_exit(roothub_entry->phy);
>> +
>> +err_out:
>> +     return ERR_PTR(err);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> +
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err, ret = 0;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_exit(roothub_entry->phy);
>> +             if (err)
>> +                     ret = ret;
>> +     }
>
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
if I understood Chunfeng Yun correctly this will require
re-enumeration of the USB devices after a suspend/resume cycle on
Mediatek SoCs

> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>
>> +
>> +     return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_power_on(roothub_entry->phy);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     return 0;
>> +
>> +err_out:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_power_off(roothub_entry->phy);
>> +
>> +     return err;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> +
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     if (!phy_roothub)
>> +             return;
>> +
>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> +             phy_power_off(roothub_entry->phy);
>
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
I'm not sure where this problem should be solved:
- set skip_phy_initialization in struct usb_hcd to 1 for the affected
TI platforms
- fix this in the usb_phy_roothub code
- fix this in the PHY driver
- somewhere else

>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> new file mode 100644
>> index 000000000000..6fde59bfbff8
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.h
>> @@ -0,0 +1,7 @@
>> +struct usb_phy_roothub;
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>
>
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
Chunfeng: could you please test whether this patch breaks the
suspend/resume cycle on your Mediatek platforms? see [0] where I used
a similar approach with a much older version of the "initialize
multiple PHYs per HCD" patch

> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>         struct usb_phy_roothub *phy_roothub;
> -       struct usb_phy_roothub *roothub_entry;
> -       struct list_head *head;
>         int i, num_phys, err;
>
>         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>         for (i = 0; i < num_phys; i++) {
>                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>                 if (err)
> -                       goto err_out;
> -       }
> -
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_init(roothub_entry->phy);
> -               if (err)
> -                       goto err_exit_phys;
> +                       return ERR_PTR(err);
>         }
>
>         return phy_roothub;
> -
> -err_exit_phys:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -               phy_exit(roothub_entry->phy);
> -
> -err_out:
> -       return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return 0;
>
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_exit(roothub_entry->phy);
> -               if (err)
> -                       ret = ret;
> -       }
> +       /* TODO: usb_phy_roothub_del_phy */
> +       /* TODO: usb_phy_roothub_free */
>
>         return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>         head = &phy_roothub->list;
>
>         list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_power_on(roothub_entry->phy);
> +               err = phy_init(roothub_entry->phy);
>                 if (err)
>                         goto err_out;
> +               err = phy_power_on(roothub_entry->phy);
> +               if (err) {
> +                       phy_exit(roothub_entry->phy);
> +                       goto err_out;
> +               }
>         }
>
>         return 0;
>
>  err_out:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>
>         return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return;
>
> -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin


[0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-18 22:29           ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-18 22:29 UTC (permalink / raw)
  To: Roger Quadros, Chunfeng Yun
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Roger,

On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> +some TI folks
>
> Hi Martin,
>
> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> Many SoC platforms have separate devices for the USB PHY which are
>> registered through the generic PHY framework. These PHYs have to be
>> enabled to make the USB controller actually work. They also have to be
>> disabled again on shutdown/suspend.
>>
>> Currently (at least) the following HCI platform drivers are using custom
>> code to obtain all PHYs via devicetree for the roothub/controller and
>> disable/enable them when required:
>> - ehci-platform.c has ehci_platform_power_{on,off}
>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> - ohci-platform.c has ohci_platform_power_{on,off}
>>
>> With this new wrapper the USB PHYs can be specified directly in the
>> USB controller's devicetree node (just like on the drivers listed
>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> correctly once this is wired up correctly. These SoCs use a dwc3
>> controller and require all USB PHYs to be initialized (if one of the USB
>> PHYs it not initialized then none of USB port works at all).
>>
>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
based on your explanation and reading the TI PHY drivers I am assuming
that the affected SoCs are using the "phy-omap-usb2" driver

>> ---
>>  drivers/usb/core/Makefile |   2 +-
>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>  drivers/usb/core/phy.h    |   7 ++
>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/usb/core/phy.c
>>  create mode 100644 drivers/usb/core/phy.h
>>
>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> index 92c9cefb4317..18e874b0441e 100644
>> --- a/drivers/usb/core/Makefile
>> +++ b/drivers/usb/core/Makefile
>> @@ -6,7 +6,7 @@
>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> -usbcore-y += port.o
>> +usbcore-y += phy.o port.o
>>
>>  usbcore-$(CONFIG_OF)         += of.o
>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> new file mode 100644
>> index 000000000000..09b7c43c0ea4
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.c
>> @@ -0,0 +1,158 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> + * all PHYs on a HCD and to keep them all in the same state.
>> + *
>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> + */
>> +
>> +#include <linux/device.h>
>> +#include <linux/list.h>
>> +#include <linux/phy/phy.h>
>> +#include <linux/of.h>
>> +
>> +#include "phy.h"
>> +
>> +struct usb_phy_roothub {
>> +     struct phy              *phy;
>> +     struct list_head        list;
>> +};
>> +
>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +     if (!roothub_entry)
>> +             return ERR_PTR(-ENOMEM);
>> +
>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> +
>> +     return roothub_entry;
>> +}
>> +
>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> +                                struct list_head *list)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> +
>> +     if (IS_ERR_OR_NULL(phy)) {
>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> +                     return 0;
>> +             else
>> +                     return PTR_ERR(phy);
>> +     }
>> +
>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(roothub_entry))
>> +             return PTR_ERR(roothub_entry);
>> +
>> +     roothub_entry->phy = phy;
>> +
>> +     list_add_tail(&roothub_entry->list, list);
>> +
>> +     return 0;
>> +}
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *phy_roothub;
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int i, num_phys, err;
>> +
>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> +                                           "#phy-cells");
>> +     if (num_phys <= 0)
>> +             return NULL;
>> +
>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(phy_roothub))
>> +             return phy_roothub;
>> +
>> +     for (i = 0; i < num_phys; i++) {
>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_init(roothub_entry->phy);
>
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
do you mean that phy_init should be moved to usb_phy_roothub_power_on
(just before phy_power_on is called within usb_phy_roothub_power_on)?

an earlier version of my patch did exactly this, but it caused
problems during a suspend/resume cycle on Mediatek devices
Chunfeng Yun reported that issue here [0], quote from that mail for
easier reading:
"In order to keep link state on mt8173, we just power off all phys(not
exit) when system enter suspend, then power on them again (needn't
init, otherwise device will be disconnected) when system resume, this
can avoid re-enumerating device."

>> +             if (err)
>> +                     goto err_exit_phys;
>> +     }
>> +
>> +     return phy_roothub;
>> +
>> +err_exit_phys:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_exit(roothub_entry->phy);
>> +
>> +err_out:
>> +     return ERR_PTR(err);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> +
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err, ret = 0;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_exit(roothub_entry->phy);
>> +             if (err)
>> +                     ret = ret;
>> +     }
>
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
if I understood Chunfeng Yun correctly this will require
re-enumeration of the USB devices after a suspend/resume cycle on
Mediatek SoCs

> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>
>> +
>> +     return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_power_on(roothub_entry->phy);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     return 0;
>> +
>> +err_out:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_power_off(roothub_entry->phy);
>> +
>> +     return err;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> +
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     if (!phy_roothub)
>> +             return;
>> +
>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> +             phy_power_off(roothub_entry->phy);
>
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
I'm not sure where this problem should be solved:
- set skip_phy_initialization in struct usb_hcd to 1 for the affected
TI platforms
- fix this in the usb_phy_roothub code
- fix this in the PHY driver
- somewhere else

>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> new file mode 100644
>> index 000000000000..6fde59bfbff8
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.h
>> @@ -0,0 +1,7 @@
>> +struct usb_phy_roothub;
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>
>
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
Chunfeng: could you please test whether this patch breaks the
suspend/resume cycle on your Mediatek platforms? see [0] where I used
a similar approach with a much older version of the "initialize
multiple PHYs per HCD" patch

> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>         struct usb_phy_roothub *phy_roothub;
> -       struct usb_phy_roothub *roothub_entry;
> -       struct list_head *head;
>         int i, num_phys, err;
>
>         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>         for (i = 0; i < num_phys; i++) {
>                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>                 if (err)
> -                       goto err_out;
> -       }
> -
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_init(roothub_entry->phy);
> -               if (err)
> -                       goto err_exit_phys;
> +                       return ERR_PTR(err);
>         }
>
>         return phy_roothub;
> -
> -err_exit_phys:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -               phy_exit(roothub_entry->phy);
> -
> -err_out:
> -       return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return 0;
>
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_exit(roothub_entry->phy);
> -               if (err)
> -                       ret = ret;
> -       }
> +       /* TODO: usb_phy_roothub_del_phy */
> +       /* TODO: usb_phy_roothub_free */
>
>         return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>         head = &phy_roothub->list;
>
>         list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_power_on(roothub_entry->phy);
> +               err = phy_init(roothub_entry->phy);
>                 if (err)
>                         goto err_out;
> +               err = phy_power_on(roothub_entry->phy);
> +               if (err) {
> +                       phy_exit(roothub_entry->phy);
> +                       goto err_out;
> +               }
>         }
>
>         return 0;
>
>  err_out:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>
>         return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return;
>
> -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin


[0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-18 22:29           ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-18 22:29 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Roger,

On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> +some TI folks
>
> Hi Martin,
>
> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> Many SoC platforms have separate devices for the USB PHY which are
>> registered through the generic PHY framework. These PHYs have to be
>> enabled to make the USB controller actually work. They also have to be
>> disabled again on shutdown/suspend.
>>
>> Currently (at least) the following HCI platform drivers are using custom
>> code to obtain all PHYs via devicetree for the roothub/controller and
>> disable/enable them when required:
>> - ehci-platform.c has ehci_platform_power_{on,off}
>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> - ohci-platform.c has ohci_platform_power_{on,off}
>>
>> With this new wrapper the USB PHYs can be specified directly in the
>> USB controller's devicetree node (just like on the drivers listed
>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> correctly once this is wired up correctly. These SoCs use a dwc3
>> controller and require all USB PHYs to be initialized (if one of the USB
>> PHYs it not initialized then none of USB port works at all).
>>
>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
based on your explanation and reading the TI PHY drivers I am assuming
that the affected SoCs are using the "phy-omap-usb2" driver

>> ---
>>  drivers/usb/core/Makefile |   2 +-
>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>  drivers/usb/core/phy.h    |   7 ++
>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/usb/core/phy.c
>>  create mode 100644 drivers/usb/core/phy.h
>>
>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> index 92c9cefb4317..18e874b0441e 100644
>> --- a/drivers/usb/core/Makefile
>> +++ b/drivers/usb/core/Makefile
>> @@ -6,7 +6,7 @@
>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> -usbcore-y += port.o
>> +usbcore-y += phy.o port.o
>>
>>  usbcore-$(CONFIG_OF)         += of.o
>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> new file mode 100644
>> index 000000000000..09b7c43c0ea4
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.c
>> @@ -0,0 +1,158 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> + * all PHYs on a HCD and to keep them all in the same state.
>> + *
>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> + */
>> +
>> +#include <linux/device.h>
>> +#include <linux/list.h>
>> +#include <linux/phy/phy.h>
>> +#include <linux/of.h>
>> +
>> +#include "phy.h"
>> +
>> +struct usb_phy_roothub {
>> +     struct phy              *phy;
>> +     struct list_head        list;
>> +};
>> +
>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +     if (!roothub_entry)
>> +             return ERR_PTR(-ENOMEM);
>> +
>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> +
>> +     return roothub_entry;
>> +}
>> +
>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> +                                struct list_head *list)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> +
>> +     if (IS_ERR_OR_NULL(phy)) {
>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> +                     return 0;
>> +             else
>> +                     return PTR_ERR(phy);
>> +     }
>> +
>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(roothub_entry))
>> +             return PTR_ERR(roothub_entry);
>> +
>> +     roothub_entry->phy = phy;
>> +
>> +     list_add_tail(&roothub_entry->list, list);
>> +
>> +     return 0;
>> +}
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *phy_roothub;
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int i, num_phys, err;
>> +
>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> +                                           "#phy-cells");
>> +     if (num_phys <= 0)
>> +             return NULL;
>> +
>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(phy_roothub))
>> +             return phy_roothub;
>> +
>> +     for (i = 0; i < num_phys; i++) {
>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_init(roothub_entry->phy);
>
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
do you mean that phy_init should be moved to usb_phy_roothub_power_on
(just before phy_power_on is called within usb_phy_roothub_power_on)?

an earlier version of my patch did exactly this, but it caused
problems during a suspend/resume cycle on Mediatek devices
Chunfeng Yun reported that issue here [0], quote from that mail for
easier reading:
"In order to keep link state on mt8173, we just power off all phys(not
exit) when system enter suspend, then power on them again (needn't
init, otherwise device will be disconnected) when system resume, this
can avoid re-enumerating device."

>> +             if (err)
>> +                     goto err_exit_phys;
>> +     }
>> +
>> +     return phy_roothub;
>> +
>> +err_exit_phys:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_exit(roothub_entry->phy);
>> +
>> +err_out:
>> +     return ERR_PTR(err);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> +
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err, ret = 0;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_exit(roothub_entry->phy);
>> +             if (err)
>> +                     ret = ret;
>> +     }
>
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
if I understood Chunfeng Yun correctly this will require
re-enumeration of the USB devices after a suspend/resume cycle on
Mediatek SoCs

> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>
>> +
>> +     return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_power_on(roothub_entry->phy);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     return 0;
>> +
>> +err_out:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_power_off(roothub_entry->phy);
>> +
>> +     return err;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> +
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     if (!phy_roothub)
>> +             return;
>> +
>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> +             phy_power_off(roothub_entry->phy);
>
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
I'm not sure where this problem should be solved:
- set skip_phy_initialization in struct usb_hcd to 1 for the affected
TI platforms
- fix this in the usb_phy_roothub code
- fix this in the PHY driver
- somewhere else

>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> new file mode 100644
>> index 000000000000..6fde59bfbff8
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.h
>> @@ -0,0 +1,7 @@
>> +struct usb_phy_roothub;
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>
>
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
Chunfeng: could you please test whether this patch breaks the
suspend/resume cycle on your Mediatek platforms? see [0] where I used
a similar approach with a much older version of the "initialize
multiple PHYs per HCD" patch

> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>         struct usb_phy_roothub *phy_roothub;
> -       struct usb_phy_roothub *roothub_entry;
> -       struct list_head *head;
>         int i, num_phys, err;
>
>         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>         for (i = 0; i < num_phys; i++) {
>                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>                 if (err)
> -                       goto err_out;
> -       }
> -
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_init(roothub_entry->phy);
> -               if (err)
> -                       goto err_exit_phys;
> +                       return ERR_PTR(err);
>         }
>
>         return phy_roothub;
> -
> -err_exit_phys:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -               phy_exit(roothub_entry->phy);
> -
> -err_out:
> -       return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return 0;
>
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_exit(roothub_entry->phy);
> -               if (err)
> -                       ret = ret;
> -       }
> +       /* TODO: usb_phy_roothub_del_phy */
> +       /* TODO: usb_phy_roothub_free */
>
>         return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>         head = &phy_roothub->list;
>
>         list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_power_on(roothub_entry->phy);
> +               err = phy_init(roothub_entry->phy);
>                 if (err)
>                         goto err_out;
> +               err = phy_power_on(roothub_entry->phy);
> +               if (err) {
> +                       phy_exit(roothub_entry->phy);
> +                       goto err_out;
> +               }
>         }
>
>         return 0;
>
>  err_out:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>
>         return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return;
>
> -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin


[0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-18 22:29           ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-18 22:29 UTC (permalink / raw)
  To: linus-amlogic

Hi Roger,

On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> +some TI folks
>
> Hi Martin,
>
> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> Many SoC platforms have separate devices for the USB PHY which are
>> registered through the generic PHY framework. These PHYs have to be
>> enabled to make the USB controller actually work. They also have to be
>> disabled again on shutdown/suspend.
>>
>> Currently (at least) the following HCI platform drivers are using custom
>> code to obtain all PHYs via devicetree for the roothub/controller and
>> disable/enable them when required:
>> - ehci-platform.c has ehci_platform_power_{on,off}
>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> - ohci-platform.c has ohci_platform_power_{on,off}
>>
>> With this new wrapper the USB PHYs can be specified directly in the
>> USB controller's devicetree node (just like on the drivers listed
>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> correctly once this is wired up correctly. These SoCs use a dwc3
>> controller and require all USB PHYs to be initialized (if one of the USB
>> PHYs it not initialized then none of USB port works at all).
>>
>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
based on your explanation and reading the TI PHY drivers I am assuming
that the affected SoCs are using the "phy-omap-usb2" driver

>> ---
>>  drivers/usb/core/Makefile |   2 +-
>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>  drivers/usb/core/phy.h    |   7 ++
>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/usb/core/phy.c
>>  create mode 100644 drivers/usb/core/phy.h
>>
>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> index 92c9cefb4317..18e874b0441e 100644
>> --- a/drivers/usb/core/Makefile
>> +++ b/drivers/usb/core/Makefile
>> @@ -6,7 +6,7 @@
>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> -usbcore-y += port.o
>> +usbcore-y += phy.o port.o
>>
>>  usbcore-$(CONFIG_OF)         += of.o
>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> new file mode 100644
>> index 000000000000..09b7c43c0ea4
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.c
>> @@ -0,0 +1,158 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> + * all PHYs on a HCD and to keep them all in the same state.
>> + *
>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> + */
>> +
>> +#include <linux/device.h>
>> +#include <linux/list.h>
>> +#include <linux/phy/phy.h>
>> +#include <linux/of.h>
>> +
>> +#include "phy.h"
>> +
>> +struct usb_phy_roothub {
>> +     struct phy              *phy;
>> +     struct list_head        list;
>> +};
>> +
>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +     if (!roothub_entry)
>> +             return ERR_PTR(-ENOMEM);
>> +
>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> +
>> +     return roothub_entry;
>> +}
>> +
>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> +                                struct list_head *list)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> +
>> +     if (IS_ERR_OR_NULL(phy)) {
>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> +                     return 0;
>> +             else
>> +                     return PTR_ERR(phy);
>> +     }
>> +
>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(roothub_entry))
>> +             return PTR_ERR(roothub_entry);
>> +
>> +     roothub_entry->phy = phy;
>> +
>> +     list_add_tail(&roothub_entry->list, list);
>> +
>> +     return 0;
>> +}
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *phy_roothub;
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int i, num_phys, err;
>> +
>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> +                                           "#phy-cells");
>> +     if (num_phys <= 0)
>> +             return NULL;
>> +
>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(phy_roothub))
>> +             return phy_roothub;
>> +
>> +     for (i = 0; i < num_phys; i++) {
>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_init(roothub_entry->phy);
>
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
do you mean that phy_init should be moved to usb_phy_roothub_power_on
(just before phy_power_on is called within usb_phy_roothub_power_on)?

an earlier version of my patch did exactly this, but it caused
problems during a suspend/resume cycle on Mediatek devices
Chunfeng Yun reported that issue here [0], quote from that mail for
easier reading:
"In order to keep link state on mt8173, we just power off all phys(not
exit) when system enter suspend, then power on them again (needn't
init, otherwise device will be disconnected) when system resume, this
can avoid re-enumerating device."

>> +             if (err)
>> +                     goto err_exit_phys;
>> +     }
>> +
>> +     return phy_roothub;
>> +
>> +err_exit_phys:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_exit(roothub_entry->phy);
>> +
>> +err_out:
>> +     return ERR_PTR(err);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> +
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err, ret = 0;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_exit(roothub_entry->phy);
>> +             if (err)
>> +                     ret = ret;
>> +     }
>
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
if I understood Chunfeng Yun correctly this will require
re-enumeration of the USB devices after a suspend/resume cycle on
Mediatek SoCs

> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>
>> +
>> +     return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_power_on(roothub_entry->phy);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     return 0;
>> +
>> +err_out:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_power_off(roothub_entry->phy);
>> +
>> +     return err;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> +
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     if (!phy_roothub)
>> +             return;
>> +
>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> +             phy_power_off(roothub_entry->phy);
>
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
I'm not sure where this problem should be solved:
- set skip_phy_initialization in struct usb_hcd to 1 for the affected
TI platforms
- fix this in the usb_phy_roothub code
- fix this in the PHY driver
- somewhere else

>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> new file mode 100644
>> index 000000000000..6fde59bfbff8
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.h
>> @@ -0,0 +1,7 @@
>> +struct usb_phy_roothub;
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>
>
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
Chunfeng: could you please test whether this patch breaks the
suspend/resume cycle on your Mediatek platforms? see [0] where I used
a similar approach with a much older version of the "initialize
multiple PHYs per HCD" patch

> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>         struct usb_phy_roothub *phy_roothub;
> -       struct usb_phy_roothub *roothub_entry;
> -       struct list_head *head;
>         int i, num_phys, err;
>
>         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>         for (i = 0; i < num_phys; i++) {
>                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>                 if (err)
> -                       goto err_out;
> -       }
> -
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_init(roothub_entry->phy);
> -               if (err)
> -                       goto err_exit_phys;
> +                       return ERR_PTR(err);
>         }
>
>         return phy_roothub;
> -
> -err_exit_phys:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -               phy_exit(roothub_entry->phy);
> -
> -err_out:
> -       return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return 0;
>
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_exit(roothub_entry->phy);
> -               if (err)
> -                       ret = ret;
> -       }
> +       /* TODO: usb_phy_roothub_del_phy */
> +       /* TODO: usb_phy_roothub_free */
>
>         return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>         head = &phy_roothub->list;
>
>         list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_power_on(roothub_entry->phy);
> +               err = phy_init(roothub_entry->phy);
>                 if (err)
>                         goto err_out;
> +               err = phy_power_on(roothub_entry->phy);
> +               if (err) {
> +                       phy_exit(roothub_entry->phy);
> +                       goto err_out;
> +               }
>         }
>
>         return 0;
>
>  err_out:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>
>         return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return;
>
> -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin


[0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  5:43           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  5:43 UTC (permalink / raw)
  To: Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc,
	narmstrong-rdvid1DuHRBWk0Htik3J/w,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	Martin Blumenstingl, yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ,
	robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

On Fri, 2018-03-16 at 16:32 +0200, Roger Quadros wrote:
> +some TI folks
> 
> Hi Martin,
> 
> On 18/02/18 20:44, Martin Blumenstingl wrote:
> > Many SoC platforms have separate devices for the USB PHY which are
> > registered through the generic PHY framework. These PHYs have to be
> > enabled to make the USB controller actually work. They also have to be
> > disabled again on shutdown/suspend.
> > 
> > Currently (at least) the following HCI platform drivers are using custom
> > code to obtain all PHYs via devicetree for the roothub/controller and
> > disable/enable them when required:
> > - ehci-platform.c has ehci_platform_power_{on,off}
> > - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> > - ohci-platform.c has ohci_platform_power_{on,off}
> > 
> > With this new wrapper the USB PHYs can be specified directly in the
> > USB controller's devicetree node (just like on the drivers listed
> > above). This allows SoCs like the Amlogic Meson GXL family to operate
> > correctly once this is wired up correctly. These SoCs use a dwc3
> > controller and require all USB PHYs to be initialized (if one of the USB
> > PHYs it not initialized then none of USB port works at all).
> > 
> > Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> > Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> > Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> > Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> 
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
> 
> > ---
> >  drivers/usb/core/Makefile |   2 +-
> >  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/usb/core/phy.h    |   7 ++
> >  3 files changed, 166 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/usb/core/phy.c
> >  create mode 100644 drivers/usb/core/phy.h
> > 
> > diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> > index 92c9cefb4317..18e874b0441e 100644
> > --- a/drivers/usb/core/Makefile
> > +++ b/drivers/usb/core/Makefile
> > @@ -6,7 +6,7 @@
> >  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> > -usbcore-y += port.o
> > +usbcore-y += phy.o port.o
> >  
> >  usbcore-$(CONFIG_OF)		+= of.o
> >  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > new file mode 100644
> > index 000000000000..09b7c43c0ea4
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.c
> > @@ -0,0 +1,158 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * A wrapper for multiple PHYs which passes all phy_* function calls to
> > + * multiple (actual) PHY devices. This is comes handy when initializing
> > + * all PHYs on a HCD and to keep them all in the same state.
> > + *
> > + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> > + */
> > +
> > +#include <linux/device.h>
> > +#include <linux/list.h>
> > +#include <linux/phy/phy.h>
> > +#include <linux/of.h>
> > +
> > +#include "phy.h"
> > +
> > +struct usb_phy_roothub {
> > +	struct phy		*phy;
> > +	struct list_head	list;
> > +};
> > +
> > +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> > +	if (!roothub_entry)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	INIT_LIST_HEAD(&roothub_entry->list);
> > +
> > +	return roothub_entry;
> > +}
> > +
> > +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> > +				   struct list_head *list)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> > +
> > +	if (IS_ERR_OR_NULL(phy)) {
> > +		if (!phy || PTR_ERR(phy) == -ENODEV)
> > +			return 0;
> > +		else
> > +			return PTR_ERR(phy);
> > +	}
> > +
> > +	roothub_entry = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(roothub_entry))
> > +		return PTR_ERR(roothub_entry);
> > +
> > +	roothub_entry->phy = phy;
> > +
> > +	list_add_tail(&roothub_entry->list, list);
> > +
> > +	return 0;
> > +}
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *phy_roothub;
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int i, num_phys, err;
> > +
> > +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > +					      "#phy-cells");
> > +	if (num_phys <= 0)
> > +		return NULL;
> > +
> > +	phy_roothub = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(phy_roothub))
> > +		return phy_roothub;
> > +
> > +	for (i = 0; i < num_phys; i++) {
> > +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_init(roothub_entry->phy);
> 
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> 
> > +		if (err)
> > +			goto err_exit_phys;
> > +	}
> > +
> > +	return phy_roothub;
> > +
> > +err_exit_phys:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_exit(roothub_entry->phy);
> > +
> > +err_out:
> > +	return ERR_PTR(err);
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> > +
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err, ret = 0;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_exit(roothub_entry->phy);
> > +		if (err)
> > +			ret = ret;
> > +	}
> 
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> 
> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> 
> > +
> > +	return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_power_on(roothub_entry->phy);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	return 0;
> > +
> > +err_out:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_power_off(roothub_entry->phy);
> > +
> > +	return err;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> > +
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	if (!phy_roothub)
> > +		return;
> > +
> > +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +		phy_power_off(roothub_entry->phy);
> 
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
Maybe you can modify phy driver if low power state is only blocked by
enabled clocks, try to disable clocks in phy_power_off, and enable them
again in phy_power_on()
> 
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> > diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> > new file mode 100644
> > index 000000000000..6fde59bfbff8
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.h
> > @@ -0,0 +1,7 @@
> > +struct usb_phy_roothub;
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> > 
> 
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>  	struct usb_phy_roothub *phy_roothub;
> -	struct usb_phy_roothub *roothub_entry;
> -	struct list_head *head;
>  	int i, num_phys, err;
>  
>  	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> -			goto err_out;
> -	}
> -
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_init(roothub_entry->phy);
> -		if (err)
> -			goto err_exit_phys;
> +			return ERR_PTR(err);
>  	}
>  
>  	return phy_roothub;
> -
> -err_exit_phys:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -		phy_exit(roothub_entry->phy);
> -
> -err_out:
> -	return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>  
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return 0;
>  
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_exit(roothub_entry->phy);
> -		if (err)
> -			ret = ret;
> -	}
> +	/* TODO: usb_phy_roothub_del_phy */
> +	/* TODO: usb_phy_roothub_free */
>  
>  	return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>  	head = &phy_roothub->list;
>  
>  	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_power_on(roothub_entry->phy);
> +		err = phy_init(roothub_entry->phy);
>  		if (err)
>  			goto err_out;
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err) {
> +			phy_exit(roothub_entry->phy);
> +			goto err_out;
> +		}
>  	}
>  
>  	return 0;
>  
>  err_out:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  
>  	return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return;
>  
> -	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  5:43           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  5:43 UTC (permalink / raw)
  To: Roger Quadros
  Cc: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	narmstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

On Fri, 2018-03-16 at 16:32 +0200, Roger Quadros wrote:
> +some TI folks
> 
> Hi Martin,
> 
> On 18/02/18 20:44, Martin Blumenstingl wrote:
> > Many SoC platforms have separate devices for the USB PHY which are
> > registered through the generic PHY framework. These PHYs have to be
> > enabled to make the USB controller actually work. They also have to be
> > disabled again on shutdown/suspend.
> > 
> > Currently (at least) the following HCI platform drivers are using custom
> > code to obtain all PHYs via devicetree for the roothub/controller and
> > disable/enable them when required:
> > - ehci-platform.c has ehci_platform_power_{on,off}
> > - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> > - ohci-platform.c has ohci_platform_power_{on,off}
> > 
> > With this new wrapper the USB PHYs can be specified directly in the
> > USB controller's devicetree node (just like on the drivers listed
> > above). This allows SoCs like the Amlogic Meson GXL family to operate
> > correctly once this is wired up correctly. These SoCs use a dwc3
> > controller and require all USB PHYs to be initialized (if one of the USB
> > PHYs it not initialized then none of USB port works at all).
> > 
> > Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> > Cc: Neil Armstrong <narmstrong@baylibre.com>
> > Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> 
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
> 
> > ---
> >  drivers/usb/core/Makefile |   2 +-
> >  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/usb/core/phy.h    |   7 ++
> >  3 files changed, 166 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/usb/core/phy.c
> >  create mode 100644 drivers/usb/core/phy.h
> > 
> > diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> > index 92c9cefb4317..18e874b0441e 100644
> > --- a/drivers/usb/core/Makefile
> > +++ b/drivers/usb/core/Makefile
> > @@ -6,7 +6,7 @@
> >  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> > -usbcore-y += port.o
> > +usbcore-y += phy.o port.o
> >  
> >  usbcore-$(CONFIG_OF)		+= of.o
> >  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > new file mode 100644
> > index 000000000000..09b7c43c0ea4
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.c
> > @@ -0,0 +1,158 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * A wrapper for multiple PHYs which passes all phy_* function calls to
> > + * multiple (actual) PHY devices. This is comes handy when initializing
> > + * all PHYs on a HCD and to keep them all in the same state.
> > + *
> > + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > + */
> > +
> > +#include <linux/device.h>
> > +#include <linux/list.h>
> > +#include <linux/phy/phy.h>
> > +#include <linux/of.h>
> > +
> > +#include "phy.h"
> > +
> > +struct usb_phy_roothub {
> > +	struct phy		*phy;
> > +	struct list_head	list;
> > +};
> > +
> > +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> > +	if (!roothub_entry)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	INIT_LIST_HEAD(&roothub_entry->list);
> > +
> > +	return roothub_entry;
> > +}
> > +
> > +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> > +				   struct list_head *list)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> > +
> > +	if (IS_ERR_OR_NULL(phy)) {
> > +		if (!phy || PTR_ERR(phy) == -ENODEV)
> > +			return 0;
> > +		else
> > +			return PTR_ERR(phy);
> > +	}
> > +
> > +	roothub_entry = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(roothub_entry))
> > +		return PTR_ERR(roothub_entry);
> > +
> > +	roothub_entry->phy = phy;
> > +
> > +	list_add_tail(&roothub_entry->list, list);
> > +
> > +	return 0;
> > +}
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *phy_roothub;
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int i, num_phys, err;
> > +
> > +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > +					      "#phy-cells");
> > +	if (num_phys <= 0)
> > +		return NULL;
> > +
> > +	phy_roothub = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(phy_roothub))
> > +		return phy_roothub;
> > +
> > +	for (i = 0; i < num_phys; i++) {
> > +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_init(roothub_entry->phy);
> 
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> 
> > +		if (err)
> > +			goto err_exit_phys;
> > +	}
> > +
> > +	return phy_roothub;
> > +
> > +err_exit_phys:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_exit(roothub_entry->phy);
> > +
> > +err_out:
> > +	return ERR_PTR(err);
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> > +
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err, ret = 0;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_exit(roothub_entry->phy);
> > +		if (err)
> > +			ret = ret;
> > +	}
> 
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> 
> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> 
> > +
> > +	return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_power_on(roothub_entry->phy);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	return 0;
> > +
> > +err_out:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_power_off(roothub_entry->phy);
> > +
> > +	return err;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> > +
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	if (!phy_roothub)
> > +		return;
> > +
> > +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +		phy_power_off(roothub_entry->phy);
> 
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
Maybe you can modify phy driver if low power state is only blocked by
enabled clocks, try to disable clocks in phy_power_off, and enable them
again in phy_power_on()
> 
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> > diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> > new file mode 100644
> > index 000000000000..6fde59bfbff8
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.h
> > @@ -0,0 +1,7 @@
> > +struct usb_phy_roothub;
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> > 
> 
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>  	struct usb_phy_roothub *phy_roothub;
> -	struct usb_phy_roothub *roothub_entry;
> -	struct list_head *head;
>  	int i, num_phys, err;
>  
>  	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> -			goto err_out;
> -	}
> -
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_init(roothub_entry->phy);
> -		if (err)
> -			goto err_exit_phys;
> +			return ERR_PTR(err);
>  	}
>  
>  	return phy_roothub;
> -
> -err_exit_phys:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -		phy_exit(roothub_entry->phy);
> -
> -err_out:
> -	return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>  
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return 0;
>  
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_exit(roothub_entry->phy);
> -		if (err)
> -			ret = ret;
> -	}
> +	/* TODO: usb_phy_roothub_del_phy */
> +	/* TODO: usb_phy_roothub_free */
>  
>  	return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>  	head = &phy_roothub->list;
>  
>  	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_power_on(roothub_entry->phy);
> +		err = phy_init(roothub_entry->phy);
>  		if (err)
>  			goto err_out;
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err) {
> +			phy_exit(roothub_entry->phy);
> +			goto err_out;
> +		}
>  	}
>  
>  	return 0;
>  
>  err_out:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  
>  	return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return;
>  
> -	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> 
>
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  5:43           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  5:43 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, 2018-03-16 at 16:32 +0200, Roger Quadros wrote:
> +some TI folks
> 
> Hi Martin,
> 
> On 18/02/18 20:44, Martin Blumenstingl wrote:
> > Many SoC platforms have separate devices for the USB PHY which are
> > registered through the generic PHY framework. These PHYs have to be
> > enabled to make the USB controller actually work. They also have to be
> > disabled again on shutdown/suspend.
> > 
> > Currently (at least) the following HCI platform drivers are using custom
> > code to obtain all PHYs via devicetree for the roothub/controller and
> > disable/enable them when required:
> > - ehci-platform.c has ehci_platform_power_{on,off}
> > - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> > - ohci-platform.c has ohci_platform_power_{on,off}
> > 
> > With this new wrapper the USB PHYs can be specified directly in the
> > USB controller's devicetree node (just like on the drivers listed
> > above). This allows SoCs like the Amlogic Meson GXL family to operate
> > correctly once this is wired up correctly. These SoCs use a dwc3
> > controller and require all USB PHYs to be initialized (if one of the USB
> > PHYs it not initialized then none of USB port works at all).
> > 
> > Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> > Cc: Neil Armstrong <narmstrong@baylibre.com>
> > Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> 
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
> 
> > ---
> >  drivers/usb/core/Makefile |   2 +-
> >  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/usb/core/phy.h    |   7 ++
> >  3 files changed, 166 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/usb/core/phy.c
> >  create mode 100644 drivers/usb/core/phy.h
> > 
> > diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> > index 92c9cefb4317..18e874b0441e 100644
> > --- a/drivers/usb/core/Makefile
> > +++ b/drivers/usb/core/Makefile
> > @@ -6,7 +6,7 @@
> >  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> > -usbcore-y += port.o
> > +usbcore-y += phy.o port.o
> >  
> >  usbcore-$(CONFIG_OF)		+= of.o
> >  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > new file mode 100644
> > index 000000000000..09b7c43c0ea4
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.c
> > @@ -0,0 +1,158 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * A wrapper for multiple PHYs which passes all phy_* function calls to
> > + * multiple (actual) PHY devices. This is comes handy when initializing
> > + * all PHYs on a HCD and to keep them all in the same state.
> > + *
> > + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > + */
> > +
> > +#include <linux/device.h>
> > +#include <linux/list.h>
> > +#include <linux/phy/phy.h>
> > +#include <linux/of.h>
> > +
> > +#include "phy.h"
> > +
> > +struct usb_phy_roothub {
> > +	struct phy		*phy;
> > +	struct list_head	list;
> > +};
> > +
> > +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> > +	if (!roothub_entry)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	INIT_LIST_HEAD(&roothub_entry->list);
> > +
> > +	return roothub_entry;
> > +}
> > +
> > +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> > +				   struct list_head *list)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> > +
> > +	if (IS_ERR_OR_NULL(phy)) {
> > +		if (!phy || PTR_ERR(phy) == -ENODEV)
> > +			return 0;
> > +		else
> > +			return PTR_ERR(phy);
> > +	}
> > +
> > +	roothub_entry = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(roothub_entry))
> > +		return PTR_ERR(roothub_entry);
> > +
> > +	roothub_entry->phy = phy;
> > +
> > +	list_add_tail(&roothub_entry->list, list);
> > +
> > +	return 0;
> > +}
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *phy_roothub;
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int i, num_phys, err;
> > +
> > +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > +					      "#phy-cells");
> > +	if (num_phys <= 0)
> > +		return NULL;
> > +
> > +	phy_roothub = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(phy_roothub))
> > +		return phy_roothub;
> > +
> > +	for (i = 0; i < num_phys; i++) {
> > +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_init(roothub_entry->phy);
> 
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> 
> > +		if (err)
> > +			goto err_exit_phys;
> > +	}
> > +
> > +	return phy_roothub;
> > +
> > +err_exit_phys:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_exit(roothub_entry->phy);
> > +
> > +err_out:
> > +	return ERR_PTR(err);
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> > +
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err, ret = 0;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_exit(roothub_entry->phy);
> > +		if (err)
> > +			ret = ret;
> > +	}
> 
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> 
> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> 
> > +
> > +	return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_power_on(roothub_entry->phy);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	return 0;
> > +
> > +err_out:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_power_off(roothub_entry->phy);
> > +
> > +	return err;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> > +
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	if (!phy_roothub)
> > +		return;
> > +
> > +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +		phy_power_off(roothub_entry->phy);
> 
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
Maybe you can modify phy driver if low power state is only blocked by
enabled clocks, try to disable clocks in phy_power_off, and enable them
again in phy_power_on()
> 
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> > diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> > new file mode 100644
> > index 000000000000..6fde59bfbff8
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.h
> > @@ -0,0 +1,7 @@
> > +struct usb_phy_roothub;
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> > 
> 
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>  	struct usb_phy_roothub *phy_roothub;
> -	struct usb_phy_roothub *roothub_entry;
> -	struct list_head *head;
>  	int i, num_phys, err;
>  
>  	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> -			goto err_out;
> -	}
> -
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_init(roothub_entry->phy);
> -		if (err)
> -			goto err_exit_phys;
> +			return ERR_PTR(err);
>  	}
>  
>  	return phy_roothub;
> -
> -err_exit_phys:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -		phy_exit(roothub_entry->phy);
> -
> -err_out:
> -	return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>  
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return 0;
>  
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_exit(roothub_entry->phy);
> -		if (err)
> -			ret = ret;
> -	}
> +	/* TODO: usb_phy_roothub_del_phy */
> +	/* TODO: usb_phy_roothub_free */
>  
>  	return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>  	head = &phy_roothub->list;
>  
>  	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_power_on(roothub_entry->phy);
> +		err = phy_init(roothub_entry->phy);
>  		if (err)
>  			goto err_out;
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err) {
> +			phy_exit(roothub_entry->phy);
> +			goto err_out;
> +		}
>  	}
>  
>  	return 0;
>  
>  err_out:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  
>  	return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return;
>  
> -	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  5:43           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  5:43 UTC (permalink / raw)
  To: linus-amlogic

On Fri, 2018-03-16 at 16:32 +0200, Roger Quadros wrote:
> +some TI folks
> 
> Hi Martin,
> 
> On 18/02/18 20:44, Martin Blumenstingl wrote:
> > Many SoC platforms have separate devices for the USB PHY which are
> > registered through the generic PHY framework. These PHYs have to be
> > enabled to make the USB controller actually work. They also have to be
> > disabled again on shutdown/suspend.
> > 
> > Currently (at least) the following HCI platform drivers are using custom
> > code to obtain all PHYs via devicetree for the roothub/controller and
> > disable/enable them when required:
> > - ehci-platform.c has ehci_platform_power_{on,off}
> > - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> > - ohci-platform.c has ohci_platform_power_{on,off}
> > 
> > With this new wrapper the USB PHYs can be specified directly in the
> > USB controller's devicetree node (just like on the drivers listed
> > above). This allows SoCs like the Amlogic Meson GXL family to operate
> > correctly once this is wired up correctly. These SoCs use a dwc3
> > controller and require all USB PHYs to be initialized (if one of the USB
> > PHYs it not initialized then none of USB port works at all).
> > 
> > Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> > Cc: Neil Armstrong <narmstrong@baylibre.com>
> > Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> 
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
> 
> > ---
> >  drivers/usb/core/Makefile |   2 +-
> >  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/usb/core/phy.h    |   7 ++
> >  3 files changed, 166 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/usb/core/phy.c
> >  create mode 100644 drivers/usb/core/phy.h
> > 
> > diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> > index 92c9cefb4317..18e874b0441e 100644
> > --- a/drivers/usb/core/Makefile
> > +++ b/drivers/usb/core/Makefile
> > @@ -6,7 +6,7 @@
> >  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> > -usbcore-y += port.o
> > +usbcore-y += phy.o port.o
> >  
> >  usbcore-$(CONFIG_OF)		+= of.o
> >  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > new file mode 100644
> > index 000000000000..09b7c43c0ea4
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.c
> > @@ -0,0 +1,158 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * A wrapper for multiple PHYs which passes all phy_* function calls to
> > + * multiple (actual) PHY devices. This is comes handy when initializing
> > + * all PHYs on a HCD and to keep them all in the same state.
> > + *
> > + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > + */
> > +
> > +#include <linux/device.h>
> > +#include <linux/list.h>
> > +#include <linux/phy/phy.h>
> > +#include <linux/of.h>
> > +
> > +#include "phy.h"
> > +
> > +struct usb_phy_roothub {
> > +	struct phy		*phy;
> > +	struct list_head	list;
> > +};
> > +
> > +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> > +	if (!roothub_entry)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	INIT_LIST_HEAD(&roothub_entry->list);
> > +
> > +	return roothub_entry;
> > +}
> > +
> > +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> > +				   struct list_head *list)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> > +
> > +	if (IS_ERR_OR_NULL(phy)) {
> > +		if (!phy || PTR_ERR(phy) == -ENODEV)
> > +			return 0;
> > +		else
> > +			return PTR_ERR(phy);
> > +	}
> > +
> > +	roothub_entry = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(roothub_entry))
> > +		return PTR_ERR(roothub_entry);
> > +
> > +	roothub_entry->phy = phy;
> > +
> > +	list_add_tail(&roothub_entry->list, list);
> > +
> > +	return 0;
> > +}
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *phy_roothub;
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int i, num_phys, err;
> > +
> > +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > +					      "#phy-cells");
> > +	if (num_phys <= 0)
> > +		return NULL;
> > +
> > +	phy_roothub = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(phy_roothub))
> > +		return phy_roothub;
> > +
> > +	for (i = 0; i < num_phys; i++) {
> > +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_init(roothub_entry->phy);
> 
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> 
> > +		if (err)
> > +			goto err_exit_phys;
> > +	}
> > +
> > +	return phy_roothub;
> > +
> > +err_exit_phys:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_exit(roothub_entry->phy);
> > +
> > +err_out:
> > +	return ERR_PTR(err);
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> > +
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err, ret = 0;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_exit(roothub_entry->phy);
> > +		if (err)
> > +			ret = ret;
> > +	}
> 
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> 
> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> 
> > +
> > +	return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_power_on(roothub_entry->phy);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	return 0;
> > +
> > +err_out:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_power_off(roothub_entry->phy);
> > +
> > +	return err;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> > +
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	if (!phy_roothub)
> > +		return;
> > +
> > +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +		phy_power_off(roothub_entry->phy);
> 
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
Maybe you can modify phy driver if low power state is only blocked by
enabled clocks, try to disable clocks in phy_power_off, and enable them
again in phy_power_on()
> 
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> > diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> > new file mode 100644
> > index 000000000000..6fde59bfbff8
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.h
> > @@ -0,0 +1,7 @@
> > +struct usb_phy_roothub;
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> > 
> 
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>  	struct usb_phy_roothub *phy_roothub;
> -	struct usb_phy_roothub *roothub_entry;
> -	struct list_head *head;
>  	int i, num_phys, err;
>  
>  	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> -			goto err_out;
> -	}
> -
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_init(roothub_entry->phy);
> -		if (err)
> -			goto err_exit_phys;
> +			return ERR_PTR(err);
>  	}
>  
>  	return phy_roothub;
> -
> -err_exit_phys:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -		phy_exit(roothub_entry->phy);
> -
> -err_out:
> -	return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>  
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return 0;
>  
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_exit(roothub_entry->phy);
> -		if (err)
> -			ret = ret;
> -	}
> +	/* TODO: usb_phy_roothub_del_phy */
> +	/* TODO: usb_phy_roothub_free */
>  
>  	return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>  	head = &phy_roothub->list;
>  
>  	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_power_on(roothub_entry->phy);
> +		err = phy_init(roothub_entry->phy);
>  		if (err)
>  			goto err_out;
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err) {
> +			phy_exit(roothub_entry->phy);
> +			goto err_out;
> +		}
>  	}
>  
>  	return 0;
>  
>  err_out:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  
>  	return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return;
>  
> -	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  6:02               ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  6:02 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Roger Quadros,
	Gerlach, Dave, gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

Hi,
On Sun, 2018-03-18 at 23:29 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> > +some TI folks
> >
> > Hi Martin,
> >
> > On 18/02/18 20:44, Martin Blumenstingl wrote:
> >> Many SoC platforms have separate devices for the USB PHY which are
> >> registered through the generic PHY framework. These PHYs have to be
> >> enabled to make the USB controller actually work. They also have to be
> >> disabled again on shutdown/suspend.
> >>
> >> Currently (at least) the following HCI platform drivers are using custom
> >> code to obtain all PHYs via devicetree for the roothub/controller and
> >> disable/enable them when required:
> >> - ehci-platform.c has ehci_platform_power_{on,off}
> >> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >> - ohci-platform.c has ohci_platform_power_{on,off}
> >>
> >> With this new wrapper the USB PHYs can be specified directly in the
> >> USB controller's devicetree node (just like on the drivers listed
> >> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >> correctly once this is wired up correctly. These SoCs use a dwc3
> >> controller and require all USB PHYs to be initialized (if one of the USB
> >> PHYs it not initialized then none of USB port works at all).
> >>
> >> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> >> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> >> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >
> > This patch is breaking low power cases on TI SoCs when USB is in host mode.
> > I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
[...]
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >> +{
> >> +     struct usb_phy_roothub *phy_roothub;
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int i, num_phys, err;
> >> +
> >> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >> +                                           "#phy-cells");
> >> +     if (num_phys <= 0)
> >> +             return NULL;
> >> +
> >> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >> +     if (IS_ERR(phy_roothub))
> >> +             return phy_roothub;
> >> +
> >> +     for (i = 0; i < num_phys; i++) {
> >> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_init(roothub_entry->phy);
> >
> > The phy_init() function actually enables the PHY clocks.
> > It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 
> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
> >> +             if (err)
> >> +                     goto err_exit_phys;
> >> +     }
> >> +
> >> +     return phy_roothub;
> >> +
> >> +err_exit_phys:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_exit(roothub_entry->phy);
> >> +
> >> +err_out:
> >> +     return ERR_PTR(err);
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >> +
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err, ret = 0;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_exit(roothub_entry->phy);
> >> +             if (err)
> >> +                     ret = ret;
> >> +     }
> >
> > phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
You are right
> 
> > With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> > usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >
> >> +
> >> +     return ret;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_power_on(roothub_entry->phy);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     return 0;
> >> +
> >> +err_out:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_power_off(roothub_entry->phy);
> >> +
> >> +     return err;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >> +
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +
> >> +     if (!phy_roothub)
> >> +             return;
> >> +
> >> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >> +             phy_power_off(roothub_entry->phy);
> >
> > Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> > we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms
> - fix this in the usb_phy_roothub code
> - fix this in the PHY driver
> - somewhere else
> 
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >> new file mode 100644
> >> index 000000000000..6fde59bfbff8
> >> --- /dev/null
> >> +++ b/drivers/usb/core/phy.h
> >> @@ -0,0 +1,7 @@
> >> +struct usb_phy_roothub;
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>
> >
> > The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> Chunfeng: could you please test whether this patch breaks the
> suspend/resume cycle on your Mediatek platforms? see [0] where I used
> a similar approach with a much older version of the "initialize
> multiple PHYs per HCD" patch
If add Roger's one, it do disconnect plugged devices on MTK platforms,
due to re-initialize u2 phys when resume,as you mentioned above
> 
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > index 09b7c43..23232d3 100644
> > --- a/drivers/usb/core/phy.c
> > +++ b/drivers/usb/core/phy.c
> > @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >  {
> >         struct usb_phy_roothub *phy_roothub;
> > -       struct usb_phy_roothub *roothub_entry;
> > -       struct list_head *head;
> >         int i, num_phys, err;
> >
> >         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >         for (i = 0; i < num_phys; i++) {
> >                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >                 if (err)
> > -                       goto err_out;
> > -       }
> > -
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_init(roothub_entry->phy);
> > -               if (err)
> > -                       goto err_exit_phys;
> > +                       return ERR_PTR(err);
> >         }
> >
> >         return phy_roothub;
> > -
> > -err_exit_phys:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > -               phy_exit(roothub_entry->phy);
> > -
> > -err_out:
> > -       return ERR_PTR(err);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >
> > @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return 0;
> >
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_exit(roothub_entry->phy);
> > -               if (err)
> > -                       ret = ret;
> > -       }
> > +       /* TODO: usb_phy_roothub_del_phy */
> > +       /* TODO: usb_phy_roothub_free */
> >
> >         return ret;
> >  }
> > @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >         head = &phy_roothub->list;
> >
> >         list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_power_on(roothub_entry->phy);
> > +               err = phy_init(roothub_entry->phy);
> >                 if (err)
> >                         goto err_out;
> > +               err = phy_power_on(roothub_entry->phy);
> > +               if (err) {
> > +                       phy_exit(roothub_entry->phy);
> > +                       goto err_out;
> > +               }
> >         }
> >
> >         return 0;
> >
> >  err_out:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >
> >         return err;
> >  }
> > @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return;
> >
> > -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
> 
> 
> [0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  6:02               ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  6:02 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: Roger Quadros, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi,
On Sun, 2018-03-18 at 23:29 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> > +some TI folks
> >
> > Hi Martin,
> >
> > On 18/02/18 20:44, Martin Blumenstingl wrote:
> >> Many SoC platforms have separate devices for the USB PHY which are
> >> registered through the generic PHY framework. These PHYs have to be
> >> enabled to make the USB controller actually work. They also have to be
> >> disabled again on shutdown/suspend.
> >>
> >> Currently (at least) the following HCI platform drivers are using custom
> >> code to obtain all PHYs via devicetree for the roothub/controller and
> >> disable/enable them when required:
> >> - ehci-platform.c has ehci_platform_power_{on,off}
> >> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >> - ohci-platform.c has ohci_platform_power_{on,off}
> >>
> >> With this new wrapper the USB PHYs can be specified directly in the
> >> USB controller's devicetree node (just like on the drivers listed
> >> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >> correctly once this is wired up correctly. These SoCs use a dwc3
> >> controller and require all USB PHYs to be initialized (if one of the USB
> >> PHYs it not initialized then none of USB port works at all).
> >>
> >> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >
> > This patch is breaking low power cases on TI SoCs when USB is in host mode.
> > I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
[...]
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >> +{
> >> +     struct usb_phy_roothub *phy_roothub;
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int i, num_phys, err;
> >> +
> >> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >> +                                           "#phy-cells");
> >> +     if (num_phys <= 0)
> >> +             return NULL;
> >> +
> >> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >> +     if (IS_ERR(phy_roothub))
> >> +             return phy_roothub;
> >> +
> >> +     for (i = 0; i < num_phys; i++) {
> >> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_init(roothub_entry->phy);
> >
> > The phy_init() function actually enables the PHY clocks.
> > It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 
> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
> >> +             if (err)
> >> +                     goto err_exit_phys;
> >> +     }
> >> +
> >> +     return phy_roothub;
> >> +
> >> +err_exit_phys:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_exit(roothub_entry->phy);
> >> +
> >> +err_out:
> >> +     return ERR_PTR(err);
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >> +
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err, ret = 0;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_exit(roothub_entry->phy);
> >> +             if (err)
> >> +                     ret = ret;
> >> +     }
> >
> > phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
You are right
> 
> > With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> > usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >
> >> +
> >> +     return ret;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_power_on(roothub_entry->phy);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     return 0;
> >> +
> >> +err_out:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_power_off(roothub_entry->phy);
> >> +
> >> +     return err;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >> +
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +
> >> +     if (!phy_roothub)
> >> +             return;
> >> +
> >> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >> +             phy_power_off(roothub_entry->phy);
> >
> > Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> > we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms
> - fix this in the usb_phy_roothub code
> - fix this in the PHY driver
> - somewhere else
> 
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >> new file mode 100644
> >> index 000000000000..6fde59bfbff8
> >> --- /dev/null
> >> +++ b/drivers/usb/core/phy.h
> >> @@ -0,0 +1,7 @@
> >> +struct usb_phy_roothub;
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>
> >
> > The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> Chunfeng: could you please test whether this patch breaks the
> suspend/resume cycle on your Mediatek platforms? see [0] where I used
> a similar approach with a much older version of the "initialize
> multiple PHYs per HCD" patch
If add Roger's one, it do disconnect plugged devices on MTK platforms,
due to re-initialize u2 phys when resume,as you mentioned above
> 
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > index 09b7c43..23232d3 100644
> > --- a/drivers/usb/core/phy.c
> > +++ b/drivers/usb/core/phy.c
> > @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >  {
> >         struct usb_phy_roothub *phy_roothub;
> > -       struct usb_phy_roothub *roothub_entry;
> > -       struct list_head *head;
> >         int i, num_phys, err;
> >
> >         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >         for (i = 0; i < num_phys; i++) {
> >                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >                 if (err)
> > -                       goto err_out;
> > -       }
> > -
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_init(roothub_entry->phy);
> > -               if (err)
> > -                       goto err_exit_phys;
> > +                       return ERR_PTR(err);
> >         }
> >
> >         return phy_roothub;
> > -
> > -err_exit_phys:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > -               phy_exit(roothub_entry->phy);
> > -
> > -err_out:
> > -       return ERR_PTR(err);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >
> > @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return 0;
> >
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_exit(roothub_entry->phy);
> > -               if (err)
> > -                       ret = ret;
> > -       }
> > +       /* TODO: usb_phy_roothub_del_phy */
> > +       /* TODO: usb_phy_roothub_free */
> >
> >         return ret;
> >  }
> > @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >         head = &phy_roothub->list;
> >
> >         list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_power_on(roothub_entry->phy);
> > +               err = phy_init(roothub_entry->phy);
> >                 if (err)
> >                         goto err_out;
> > +               err = phy_power_on(roothub_entry->phy);
> > +               if (err) {
> > +                       phy_exit(roothub_entry->phy);
> > +                       goto err_out;
> > +               }
> >         }
> >
> >         return 0;
> >
> >  err_out:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >
> >         return err;
> >  }
> > @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return;
> >
> > -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
> 
> 
> [0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  6:02               ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  6:02 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,
On Sun, 2018-03-18 at 23:29 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> > +some TI folks
> >
> > Hi Martin,
> >
> > On 18/02/18 20:44, Martin Blumenstingl wrote:
> >> Many SoC platforms have separate devices for the USB PHY which are
> >> registered through the generic PHY framework. These PHYs have to be
> >> enabled to make the USB controller actually work. They also have to be
> >> disabled again on shutdown/suspend.
> >>
> >> Currently (at least) the following HCI platform drivers are using custom
> >> code to obtain all PHYs via devicetree for the roothub/controller and
> >> disable/enable them when required:
> >> - ehci-platform.c has ehci_platform_power_{on,off}
> >> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >> - ohci-platform.c has ohci_platform_power_{on,off}
> >>
> >> With this new wrapper the USB PHYs can be specified directly in the
> >> USB controller's devicetree node (just like on the drivers listed
> >> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >> correctly once this is wired up correctly. These SoCs use a dwc3
> >> controller and require all USB PHYs to be initialized (if one of the USB
> >> PHYs it not initialized then none of USB port works at all).
> >>
> >> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >
> > This patch is breaking low power cases on TI SoCs when USB is in host mode.
> > I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
[...]
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >> +{
> >> +     struct usb_phy_roothub *phy_roothub;
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int i, num_phys, err;
> >> +
> >> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >> +                                           "#phy-cells");
> >> +     if (num_phys <= 0)
> >> +             return NULL;
> >> +
> >> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >> +     if (IS_ERR(phy_roothub))
> >> +             return phy_roothub;
> >> +
> >> +     for (i = 0; i < num_phys; i++) {
> >> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_init(roothub_entry->phy);
> >
> > The phy_init() function actually enables the PHY clocks.
> > It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 
> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
> >> +             if (err)
> >> +                     goto err_exit_phys;
> >> +     }
> >> +
> >> +     return phy_roothub;
> >> +
> >> +err_exit_phys:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_exit(roothub_entry->phy);
> >> +
> >> +err_out:
> >> +     return ERR_PTR(err);
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >> +
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err, ret = 0;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_exit(roothub_entry->phy);
> >> +             if (err)
> >> +                     ret = ret;
> >> +     }
> >
> > phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
You are right
> 
> > With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> > usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >
> >> +
> >> +     return ret;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_power_on(roothub_entry->phy);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     return 0;
> >> +
> >> +err_out:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_power_off(roothub_entry->phy);
> >> +
> >> +     return err;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >> +
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +
> >> +     if (!phy_roothub)
> >> +             return;
> >> +
> >> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >> +             phy_power_off(roothub_entry->phy);
> >
> > Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> > we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms
> - fix this in the usb_phy_roothub code
> - fix this in the PHY driver
> - somewhere else
> 
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >> new file mode 100644
> >> index 000000000000..6fde59bfbff8
> >> --- /dev/null
> >> +++ b/drivers/usb/core/phy.h
> >> @@ -0,0 +1,7 @@
> >> +struct usb_phy_roothub;
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>
> >
> > The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> Chunfeng: could you please test whether this patch breaks the
> suspend/resume cycle on your Mediatek platforms? see [0] where I used
> a similar approach with a much older version of the "initialize
> multiple PHYs per HCD" patch
If add Roger's one, it do disconnect plugged devices on MTK platforms,
due to re-initialize u2 phys when resume,as you mentioned above
> 
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > index 09b7c43..23232d3 100644
> > --- a/drivers/usb/core/phy.c
> > +++ b/drivers/usb/core/phy.c
> > @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >  {
> >         struct usb_phy_roothub *phy_roothub;
> > -       struct usb_phy_roothub *roothub_entry;
> > -       struct list_head *head;
> >         int i, num_phys, err;
> >
> >         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >         for (i = 0; i < num_phys; i++) {
> >                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >                 if (err)
> > -                       goto err_out;
> > -       }
> > -
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_init(roothub_entry->phy);
> > -               if (err)
> > -                       goto err_exit_phys;
> > +                       return ERR_PTR(err);
> >         }
> >
> >         return phy_roothub;
> > -
> > -err_exit_phys:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > -               phy_exit(roothub_entry->phy);
> > -
> > -err_out:
> > -       return ERR_PTR(err);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >
> > @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return 0;
> >
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_exit(roothub_entry->phy);
> > -               if (err)
> > -                       ret = ret;
> > -       }
> > +       /* TODO: usb_phy_roothub_del_phy */
> > +       /* TODO: usb_phy_roothub_free */
> >
> >         return ret;
> >  }
> > @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >         head = &phy_roothub->list;
> >
> >         list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_power_on(roothub_entry->phy);
> > +               err = phy_init(roothub_entry->phy);
> >                 if (err)
> >                         goto err_out;
> > +               err = phy_power_on(roothub_entry->phy);
> > +               if (err) {
> > +                       phy_exit(roothub_entry->phy);
> > +                       goto err_out;
> > +               }
> >         }
> >
> >         return 0;
> >
> >  err_out:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >
> >         return err;
> >  }
> > @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return;
> >
> > -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
> 
> 
> [0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  6:02               ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-19  6:02 UTC (permalink / raw)
  To: linus-amlogic

Hi,
On Sun, 2018-03-18 at 23:29 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> > +some TI folks
> >
> > Hi Martin,
> >
> > On 18/02/18 20:44, Martin Blumenstingl wrote:
> >> Many SoC platforms have separate devices for the USB PHY which are
> >> registered through the generic PHY framework. These PHYs have to be
> >> enabled to make the USB controller actually work. They also have to be
> >> disabled again on shutdown/suspend.
> >>
> >> Currently (at least) the following HCI platform drivers are using custom
> >> code to obtain all PHYs via devicetree for the roothub/controller and
> >> disable/enable them when required:
> >> - ehci-platform.c has ehci_platform_power_{on,off}
> >> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >> - ohci-platform.c has ohci_platform_power_{on,off}
> >>
> >> With this new wrapper the USB PHYs can be specified directly in the
> >> USB controller's devicetree node (just like on the drivers listed
> >> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >> correctly once this is wired up correctly. These SoCs use a dwc3
> >> controller and require all USB PHYs to be initialized (if one of the USB
> >> PHYs it not initialized then none of USB port works at all).
> >>
> >> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >
> > This patch is breaking low power cases on TI SoCs when USB is in host mode.
> > I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
[...]
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >> +{
> >> +     struct usb_phy_roothub *phy_roothub;
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int i, num_phys, err;
> >> +
> >> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >> +                                           "#phy-cells");
> >> +     if (num_phys <= 0)
> >> +             return NULL;
> >> +
> >> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >> +     if (IS_ERR(phy_roothub))
> >> +             return phy_roothub;
> >> +
> >> +     for (i = 0; i < num_phys; i++) {
> >> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_init(roothub_entry->phy);
> >
> > The phy_init() function actually enables the PHY clocks.
> > It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 
> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
> >> +             if (err)
> >> +                     goto err_exit_phys;
> >> +     }
> >> +
> >> +     return phy_roothub;
> >> +
> >> +err_exit_phys:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_exit(roothub_entry->phy);
> >> +
> >> +err_out:
> >> +     return ERR_PTR(err);
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >> +
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err, ret = 0;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_exit(roothub_entry->phy);
> >> +             if (err)
> >> +                     ret = ret;
> >> +     }
> >
> > phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
You are right
> 
> > With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> > usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >
> >> +
> >> +     return ret;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_power_on(roothub_entry->phy);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     return 0;
> >> +
> >> +err_out:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_power_off(roothub_entry->phy);
> >> +
> >> +     return err;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >> +
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +
> >> +     if (!phy_roothub)
> >> +             return;
> >> +
> >> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >> +             phy_power_off(roothub_entry->phy);
> >
> > Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> > we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms
> - fix this in the usb_phy_roothub code
> - fix this in the PHY driver
> - somewhere else
> 
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >> new file mode 100644
> >> index 000000000000..6fde59bfbff8
> >> --- /dev/null
> >> +++ b/drivers/usb/core/phy.h
> >> @@ -0,0 +1,7 @@
> >> +struct usb_phy_roothub;
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>
> >
> > The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> Chunfeng: could you please test whether this patch breaks the
> suspend/resume cycle on your Mediatek platforms? see [0] where I used
> a similar approach with a much older version of the "initialize
> multiple PHYs per HCD" patch
If add Roger's one, it do disconnect plugged devices on MTK platforms,
due to re-initialize u2 phys when resume,as you mentioned above
> 
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > index 09b7c43..23232d3 100644
> > --- a/drivers/usb/core/phy.c
> > +++ b/drivers/usb/core/phy.c
> > @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >  {
> >         struct usb_phy_roothub *phy_roothub;
> > -       struct usb_phy_roothub *roothub_entry;
> > -       struct list_head *head;
> >         int i, num_phys, err;
> >
> >         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >         for (i = 0; i < num_phys; i++) {
> >                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >                 if (err)
> > -                       goto err_out;
> > -       }
> > -
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_init(roothub_entry->phy);
> > -               if (err)
> > -                       goto err_exit_phys;
> > +                       return ERR_PTR(err);
> >         }
> >
> >         return phy_roothub;
> > -
> > -err_exit_phys:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > -               phy_exit(roothub_entry->phy);
> > -
> > -err_out:
> > -       return ERR_PTR(err);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >
> > @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return 0;
> >
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_exit(roothub_entry->phy);
> > -               if (err)
> > -                       ret = ret;
> > -       }
> > +       /* TODO: usb_phy_roothub_del_phy */
> > +       /* TODO: usb_phy_roothub_free */
> >
> >         return ret;
> >  }
> > @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >         head = &phy_roothub->list;
> >
> >         list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_power_on(roothub_entry->phy);
> > +               err = phy_init(roothub_entry->phy);
> >                 if (err)
> >                         goto err_out;
> > +               err = phy_power_on(roothub_entry->phy);
> > +               if (err) {
> > +                       phy_exit(roothub_entry->phy);
> > +                       goto err_out;
> > +               }
> >         }
> >
> >         return 0;
> >
> >  err_out:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >
> >         return err;
> >  }
> > @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return;
> >
> > -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
> 
> 
> [0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  8:49               ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-19  8:49 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

Hi,

On 19/03/18 00:29, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>> +some TI folks
>>
>> Hi Martin,
>>
>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>> Many SoC platforms have separate devices for the USB PHY which are
>>> registered through the generic PHY framework. These PHYs have to be
>>> enabled to make the USB controller actually work. They also have to be
>>> disabled again on shutdown/suspend.
>>>
>>> Currently (at least) the following HCI platform drivers are using custom
>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>> disable/enable them when required:
>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>
>>> With this new wrapper the USB PHYs can be specified directly in the
>>> USB controller's devicetree node (just like on the drivers listed
>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>> controller and require all USB PHYs to be initialized (if one of the USB
>>> PHYs it not initialized then none of USB port works at all).
>>>
>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>
>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"

>>> ---
>>>  drivers/usb/core/Makefile |   2 +-
>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>  drivers/usb/core/phy.h    |   7 ++
>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>  create mode 100644 drivers/usb/core/phy.c
>>>  create mode 100644 drivers/usb/core/phy.h
>>>
>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>> index 92c9cefb4317..18e874b0441e 100644
>>> --- a/drivers/usb/core/Makefile
>>> +++ b/drivers/usb/core/Makefile
>>> @@ -6,7 +6,7 @@
>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>> -usbcore-y += port.o
>>> +usbcore-y += phy.o port.o
>>>
>>>  usbcore-$(CONFIG_OF)         += of.o
>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>> new file mode 100644
>>> index 000000000000..09b7c43c0ea4
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.c
>>> @@ -0,0 +1,158 @@
>>> +// SPDX-License-Identifier: GPL-2.0+
>>> +/*
>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>> + * all PHYs on a HCD and to keep them all in the same state.
>>> + *
>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>> + */
>>> +
>>> +#include <linux/device.h>
>>> +#include <linux/list.h>
>>> +#include <linux/phy/phy.h>
>>> +#include <linux/of.h>
>>> +
>>> +#include "phy.h"
>>> +
>>> +struct usb_phy_roothub {
>>> +     struct phy              *phy;
>>> +     struct list_head        list;
>>> +};
>>> +
>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>> +     if (!roothub_entry)
>>> +             return ERR_PTR(-ENOMEM);
>>> +
>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>> +
>>> +     return roothub_entry;
>>> +}
>>> +
>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>> +                                struct list_head *list)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>> +
>>> +     if (IS_ERR_OR_NULL(phy)) {
>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>> +                     return 0;
>>> +             else
>>> +                     return PTR_ERR(phy);
>>> +     }
>>> +
>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(roothub_entry))
>>> +             return PTR_ERR(roothub_entry);
>>> +
>>> +     roothub_entry->phy = phy;
>>> +
>>> +     list_add_tail(&roothub_entry->list, list);
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *phy_roothub;
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int i, num_phys, err;
>>> +
>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>> +                                           "#phy-cells");
>>> +     if (num_phys <= 0)
>>> +             return NULL;
>>> +
>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(phy_roothub))
>>> +             return phy_roothub;
>>> +
>>> +     for (i = 0; i < num_phys; i++) {
>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_init(roothub_entry->phy);
>>
>> The phy_init() function actually enables the PHY clocks.
>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 

Yes.

> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
>>> +             if (err)
>>> +                     goto err_exit_phys;
>>> +     }
>>> +
>>> +     return phy_roothub;
>>> +
>>> +err_exit_phys:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_exit(roothub_entry->phy);
>>> +
>>> +err_out:
>>> +     return ERR_PTR(err);
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>> +
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err, ret = 0;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_exit(roothub_entry->phy);
>>> +             if (err)
>>> +                     ret = ret;
>>> +     }
>>
>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
> 

OK. I suppose that there are 2 cases
1) Mediatek's case: USB controller context retained across suspend/resume.
Remote wakeup probably required.
No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
during suspend/resume to keep PHY link active.

2) TI's case: low power important: USB context is lost, OK to re-enumerate.
phy_exit()/phy_init() must be called during suspend/resume.


>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>
>>> +
>>> +     return ret;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_power_on(roothub_entry->phy);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     return 0;
>>> +
>>> +err_out:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_power_off(roothub_entry->phy);
>>> +
>>> +     return err;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>> +
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     if (!phy_roothub)
>>> +             return;
>>> +
>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>> +             phy_power_off(roothub_entry->phy);
>>
>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms

Many TI platforms are affected, omap5*, dra7*, am43*

> - fix this in the usb_phy_roothub code

I'd vote for fixing it in the usb_phy_roothub code. How?
How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?

> - fix this in the PHY driver

There is nothing to fix in the PHY driver. It is doing what it is supposed to do.

> - somewhere else
> 
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>> new file mode 100644
>>> index 000000000000..6fde59bfbff8
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.h
>>> @@ -0,0 +1,7 @@
>>> +struct usb_phy_roothub;
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>
>>

<snip>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  8:49               ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-19  8:49 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi,

On 19/03/18 00:29, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> +some TI folks
>>
>> Hi Martin,
>>
>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>> Many SoC platforms have separate devices for the USB PHY which are
>>> registered through the generic PHY framework. These PHYs have to be
>>> enabled to make the USB controller actually work. They also have to be
>>> disabled again on shutdown/suspend.
>>>
>>> Currently (at least) the following HCI platform drivers are using custom
>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>> disable/enable them when required:
>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>
>>> With this new wrapper the USB PHYs can be specified directly in the
>>> USB controller's devicetree node (just like on the drivers listed
>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>> controller and require all USB PHYs to be initialized (if one of the USB
>>> PHYs it not initialized then none of USB port works at all).
>>>
>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>
>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"

>>> ---
>>>  drivers/usb/core/Makefile |   2 +-
>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>  drivers/usb/core/phy.h    |   7 ++
>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>  create mode 100644 drivers/usb/core/phy.c
>>>  create mode 100644 drivers/usb/core/phy.h
>>>
>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>> index 92c9cefb4317..18e874b0441e 100644
>>> --- a/drivers/usb/core/Makefile
>>> +++ b/drivers/usb/core/Makefile
>>> @@ -6,7 +6,7 @@
>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>> -usbcore-y += port.o
>>> +usbcore-y += phy.o port.o
>>>
>>>  usbcore-$(CONFIG_OF)         += of.o
>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>> new file mode 100644
>>> index 000000000000..09b7c43c0ea4
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.c
>>> @@ -0,0 +1,158 @@
>>> +// SPDX-License-Identifier: GPL-2.0+
>>> +/*
>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>> + * all PHYs on a HCD and to keep them all in the same state.
>>> + *
>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> + */
>>> +
>>> +#include <linux/device.h>
>>> +#include <linux/list.h>
>>> +#include <linux/phy/phy.h>
>>> +#include <linux/of.h>
>>> +
>>> +#include "phy.h"
>>> +
>>> +struct usb_phy_roothub {
>>> +     struct phy              *phy;
>>> +     struct list_head        list;
>>> +};
>>> +
>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>> +     if (!roothub_entry)
>>> +             return ERR_PTR(-ENOMEM);
>>> +
>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>> +
>>> +     return roothub_entry;
>>> +}
>>> +
>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>> +                                struct list_head *list)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>> +
>>> +     if (IS_ERR_OR_NULL(phy)) {
>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>> +                     return 0;
>>> +             else
>>> +                     return PTR_ERR(phy);
>>> +     }
>>> +
>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(roothub_entry))
>>> +             return PTR_ERR(roothub_entry);
>>> +
>>> +     roothub_entry->phy = phy;
>>> +
>>> +     list_add_tail(&roothub_entry->list, list);
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *phy_roothub;
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int i, num_phys, err;
>>> +
>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>> +                                           "#phy-cells");
>>> +     if (num_phys <= 0)
>>> +             return NULL;
>>> +
>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(phy_roothub))
>>> +             return phy_roothub;
>>> +
>>> +     for (i = 0; i < num_phys; i++) {
>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_init(roothub_entry->phy);
>>
>> The phy_init() function actually enables the PHY clocks.
>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 

Yes.

> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
>>> +             if (err)
>>> +                     goto err_exit_phys;
>>> +     }
>>> +
>>> +     return phy_roothub;
>>> +
>>> +err_exit_phys:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_exit(roothub_entry->phy);
>>> +
>>> +err_out:
>>> +     return ERR_PTR(err);
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>> +
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err, ret = 0;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_exit(roothub_entry->phy);
>>> +             if (err)
>>> +                     ret = ret;
>>> +     }
>>
>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
> 

OK. I suppose that there are 2 cases
1) Mediatek's case: USB controller context retained across suspend/resume.
Remote wakeup probably required.
No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
during suspend/resume to keep PHY link active.

2) TI's case: low power important: USB context is lost, OK to re-enumerate.
phy_exit()/phy_init() must be called during suspend/resume.


>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>
>>> +
>>> +     return ret;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_power_on(roothub_entry->phy);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     return 0;
>>> +
>>> +err_out:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_power_off(roothub_entry->phy);
>>> +
>>> +     return err;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>> +
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     if (!phy_roothub)
>>> +             return;
>>> +
>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>> +             phy_power_off(roothub_entry->phy);
>>
>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms

Many TI platforms are affected, omap5*, dra7*, am43*

> - fix this in the usb_phy_roothub code

I'd vote for fixing it in the usb_phy_roothub code. How?
How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?

> - fix this in the PHY driver

There is nothing to fix in the PHY driver. It is doing what it is supposed to do.

> - somewhere else
> 
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>> new file mode 100644
>>> index 000000000000..6fde59bfbff8
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.h
>>> @@ -0,0 +1,7 @@
>>> +struct usb_phy_roothub;
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>
>>

<snip>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  8:49               ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-19  8:49 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On 19/03/18 00:29, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> +some TI folks
>>
>> Hi Martin,
>>
>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>> Many SoC platforms have separate devices for the USB PHY which are
>>> registered through the generic PHY framework. These PHYs have to be
>>> enabled to make the USB controller actually work. They also have to be
>>> disabled again on shutdown/suspend.
>>>
>>> Currently (at least) the following HCI platform drivers are using custom
>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>> disable/enable them when required:
>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>
>>> With this new wrapper the USB PHYs can be specified directly in the
>>> USB controller's devicetree node (just like on the drivers listed
>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>> controller and require all USB PHYs to be initialized (if one of the USB
>>> PHYs it not initialized then none of USB port works at all).
>>>
>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>
>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"

>>> ---
>>>  drivers/usb/core/Makefile |   2 +-
>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>  drivers/usb/core/phy.h    |   7 ++
>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>  create mode 100644 drivers/usb/core/phy.c
>>>  create mode 100644 drivers/usb/core/phy.h
>>>
>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>> index 92c9cefb4317..18e874b0441e 100644
>>> --- a/drivers/usb/core/Makefile
>>> +++ b/drivers/usb/core/Makefile
>>> @@ -6,7 +6,7 @@
>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>> -usbcore-y += port.o
>>> +usbcore-y += phy.o port.o
>>>
>>>  usbcore-$(CONFIG_OF)         += of.o
>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>> new file mode 100644
>>> index 000000000000..09b7c43c0ea4
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.c
>>> @@ -0,0 +1,158 @@
>>> +// SPDX-License-Identifier: GPL-2.0+
>>> +/*
>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>> + * all PHYs on a HCD and to keep them all in the same state.
>>> + *
>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> + */
>>> +
>>> +#include <linux/device.h>
>>> +#include <linux/list.h>
>>> +#include <linux/phy/phy.h>
>>> +#include <linux/of.h>
>>> +
>>> +#include "phy.h"
>>> +
>>> +struct usb_phy_roothub {
>>> +     struct phy              *phy;
>>> +     struct list_head        list;
>>> +};
>>> +
>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>> +     if (!roothub_entry)
>>> +             return ERR_PTR(-ENOMEM);
>>> +
>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>> +
>>> +     return roothub_entry;
>>> +}
>>> +
>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>> +                                struct list_head *list)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>> +
>>> +     if (IS_ERR_OR_NULL(phy)) {
>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>> +                     return 0;
>>> +             else
>>> +                     return PTR_ERR(phy);
>>> +     }
>>> +
>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(roothub_entry))
>>> +             return PTR_ERR(roothub_entry);
>>> +
>>> +     roothub_entry->phy = phy;
>>> +
>>> +     list_add_tail(&roothub_entry->list, list);
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *phy_roothub;
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int i, num_phys, err;
>>> +
>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>> +                                           "#phy-cells");
>>> +     if (num_phys <= 0)
>>> +             return NULL;
>>> +
>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(phy_roothub))
>>> +             return phy_roothub;
>>> +
>>> +     for (i = 0; i < num_phys; i++) {
>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_init(roothub_entry->phy);
>>
>> The phy_init() function actually enables the PHY clocks.
>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 

Yes.

> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
>>> +             if (err)
>>> +                     goto err_exit_phys;
>>> +     }
>>> +
>>> +     return phy_roothub;
>>> +
>>> +err_exit_phys:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_exit(roothub_entry->phy);
>>> +
>>> +err_out:
>>> +     return ERR_PTR(err);
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>> +
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err, ret = 0;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_exit(roothub_entry->phy);
>>> +             if (err)
>>> +                     ret = ret;
>>> +     }
>>
>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
> 

OK. I suppose that there are 2 cases
1) Mediatek's case: USB controller context retained across suspend/resume.
Remote wakeup probably required.
No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
during suspend/resume to keep PHY link active.

2) TI's case: low power important: USB context is lost, OK to re-enumerate.
phy_exit()/phy_init() must be called during suspend/resume.


>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>
>>> +
>>> +     return ret;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_power_on(roothub_entry->phy);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     return 0;
>>> +
>>> +err_out:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_power_off(roothub_entry->phy);
>>> +
>>> +     return err;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>> +
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     if (!phy_roothub)
>>> +             return;
>>> +
>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>> +             phy_power_off(roothub_entry->phy);
>>
>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms

Many TI platforms are affected, omap5*, dra7*, am43*

> - fix this in the usb_phy_roothub code

I'd vote for fixing it in the usb_phy_roothub code. How?
How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?

> - fix this in the PHY driver

There is nothing to fix in the PHY driver. It is doing what it is supposed to do.

> - somewhere else
> 
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>> new file mode 100644
>>> index 000000000000..6fde59bfbff8
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.h
>>> @@ -0,0 +1,7 @@
>>> +struct usb_phy_roothub;
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>
>>

<snip>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19  8:49               ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-19  8:49 UTC (permalink / raw)
  To: linus-amlogic

Hi,

On 19/03/18 00:29, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> +some TI folks
>>
>> Hi Martin,
>>
>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>> Many SoC platforms have separate devices for the USB PHY which are
>>> registered through the generic PHY framework. These PHYs have to be
>>> enabled to make the USB controller actually work. They also have to be
>>> disabled again on shutdown/suspend.
>>>
>>> Currently (at least) the following HCI platform drivers are using custom
>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>> disable/enable them when required:
>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>
>>> With this new wrapper the USB PHYs can be specified directly in the
>>> USB controller's devicetree node (just like on the drivers listed
>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>> controller and require all USB PHYs to be initialized (if one of the USB
>>> PHYs it not initialized then none of USB port works at all).
>>>
>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>
>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"

>>> ---
>>>  drivers/usb/core/Makefile |   2 +-
>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>  drivers/usb/core/phy.h    |   7 ++
>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>  create mode 100644 drivers/usb/core/phy.c
>>>  create mode 100644 drivers/usb/core/phy.h
>>>
>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>> index 92c9cefb4317..18e874b0441e 100644
>>> --- a/drivers/usb/core/Makefile
>>> +++ b/drivers/usb/core/Makefile
>>> @@ -6,7 +6,7 @@
>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>> -usbcore-y += port.o
>>> +usbcore-y += phy.o port.o
>>>
>>>  usbcore-$(CONFIG_OF)         += of.o
>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>> new file mode 100644
>>> index 000000000000..09b7c43c0ea4
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.c
>>> @@ -0,0 +1,158 @@
>>> +// SPDX-License-Identifier: GPL-2.0+
>>> +/*
>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>> + * all PHYs on a HCD and to keep them all in the same state.
>>> + *
>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> + */
>>> +
>>> +#include <linux/device.h>
>>> +#include <linux/list.h>
>>> +#include <linux/phy/phy.h>
>>> +#include <linux/of.h>
>>> +
>>> +#include "phy.h"
>>> +
>>> +struct usb_phy_roothub {
>>> +     struct phy              *phy;
>>> +     struct list_head        list;
>>> +};
>>> +
>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>> +     if (!roothub_entry)
>>> +             return ERR_PTR(-ENOMEM);
>>> +
>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>> +
>>> +     return roothub_entry;
>>> +}
>>> +
>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>> +                                struct list_head *list)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>> +
>>> +     if (IS_ERR_OR_NULL(phy)) {
>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>> +                     return 0;
>>> +             else
>>> +                     return PTR_ERR(phy);
>>> +     }
>>> +
>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(roothub_entry))
>>> +             return PTR_ERR(roothub_entry);
>>> +
>>> +     roothub_entry->phy = phy;
>>> +
>>> +     list_add_tail(&roothub_entry->list, list);
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *phy_roothub;
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int i, num_phys, err;
>>> +
>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>> +                                           "#phy-cells");
>>> +     if (num_phys <= 0)
>>> +             return NULL;
>>> +
>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(phy_roothub))
>>> +             return phy_roothub;
>>> +
>>> +     for (i = 0; i < num_phys; i++) {
>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_init(roothub_entry->phy);
>>
>> The phy_init() function actually enables the PHY clocks.
>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 

Yes.

> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
>>> +             if (err)
>>> +                     goto err_exit_phys;
>>> +     }
>>> +
>>> +     return phy_roothub;
>>> +
>>> +err_exit_phys:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_exit(roothub_entry->phy);
>>> +
>>> +err_out:
>>> +     return ERR_PTR(err);
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>> +
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err, ret = 0;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_exit(roothub_entry->phy);
>>> +             if (err)
>>> +                     ret = ret;
>>> +     }
>>
>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
> 

OK. I suppose that there are 2 cases
1) Mediatek's case: USB controller context retained across suspend/resume.
Remote wakeup probably required.
No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
during suspend/resume to keep PHY link active.

2) TI's case: low power important: USB context is lost, OK to re-enumerate.
phy_exit()/phy_init() must be called during suspend/resume.


>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>
>>> +
>>> +     return ret;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_power_on(roothub_entry->phy);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     return 0;
>>> +
>>> +err_out:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_power_off(roothub_entry->phy);
>>> +
>>> +     return err;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>> +
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     if (!phy_roothub)
>>> +             return;
>>> +
>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>> +             phy_power_off(roothub_entry->phy);
>>
>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms

Many TI platforms are affected, omap5*, dra7*, am43*

> - fix this in the usb_phy_roothub code

I'd vote for fixing it in the usb_phy_roothub code. How?
How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?

> - fix this in the PHY driver

There is nothing to fix in the PHY driver. It is doing what it is supposed to do.

> - somewhere else
> 
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>> new file mode 100644
>>> index 000000000000..6fde59bfbff8
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.h
>>> @@ -0,0 +1,7 @@
>>> +struct usb_phy_roothub;
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>
>>

<snip>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19 16:12                   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-19 16:12 UTC (permalink / raw)
  To: Roger Quadros, Chunfeng Yun
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

Hi Roger,

On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> Hi,
>
> On 19/03/18 00:29, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>> +some TI folks
>>>
>>> Hi Martin,
>>>
>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>> registered through the generic PHY framework. These PHYs have to be
>>>> enabled to make the USB controller actually work. They also have to be
>>>> disabled again on shutdown/suspend.
>>>>
>>>> Currently (at least) the following HCI platform drivers are using custom
>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>> disable/enable them when required:
>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>
>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>> USB controller's devicetree node (just like on the drivers listed
>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>> PHYs it not initialized then none of USB port works at all).
>>>>
>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>
>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>> I'll explain why below.
>> based on your explanation and reading the TI PHY drivers I am assuming
>> that the affected SoCs are using the "phy-omap-usb2" driver
>>
> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
I missed that, thanks

>>>> ---
>>>>  drivers/usb/core/Makefile |   2 +-
>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>
>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>> index 92c9cefb4317..18e874b0441e 100644
>>>> --- a/drivers/usb/core/Makefile
>>>> +++ b/drivers/usb/core/Makefile
>>>> @@ -6,7 +6,7 @@
>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>> -usbcore-y += port.o
>>>> +usbcore-y += phy.o port.o
>>>>
>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>> new file mode 100644
>>>> index 000000000000..09b7c43c0ea4
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.c
>>>> @@ -0,0 +1,158 @@
>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>> +/*
>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>> + *
>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>> + */
>>>> +
>>>> +#include <linux/device.h>
>>>> +#include <linux/list.h>
>>>> +#include <linux/phy/phy.h>
>>>> +#include <linux/of.h>
>>>> +
>>>> +#include "phy.h"
>>>> +
>>>> +struct usb_phy_roothub {
>>>> +     struct phy              *phy;
>>>> +     struct list_head        list;
>>>> +};
>>>> +
>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>> +     if (!roothub_entry)
>>>> +             return ERR_PTR(-ENOMEM);
>>>> +
>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>> +
>>>> +     return roothub_entry;
>>>> +}
>>>> +
>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>> +                                struct list_head *list)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>> +
>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>> +                     return 0;
>>>> +             else
>>>> +                     return PTR_ERR(phy);
>>>> +     }
>>>> +
>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(roothub_entry))
>>>> +             return PTR_ERR(roothub_entry);
>>>> +
>>>> +     roothub_entry->phy = phy;
>>>> +
>>>> +     list_add_tail(&roothub_entry->list, list);
>>>> +
>>>> +     return 0;
>>>> +}
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *phy_roothub;
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int i, num_phys, err;
>>>> +
>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>> +                                           "#phy-cells");
>>>> +     if (num_phys <= 0)
>>>> +             return NULL;
>>>> +
>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(phy_roothub))
>>>> +             return phy_roothub;
>>>> +
>>>> +     for (i = 0; i < num_phys; i++) {
>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_init(roothub_entry->phy);
>>>
>>> The phy_init() function actually enables the PHY clocks.
>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>
>
> Yes.
>
>> an earlier version of my patch did exactly this, but it caused
>> problems during a suspend/resume cycle on Mediatek devices
>> Chunfeng Yun reported that issue here [0], quote from that mail for
>> easier reading:
>> "In order to keep link state on mt8173, we just power off all phys(not
>> exit) when system enter suspend, then power on them again (needn't
>> init, otherwise device will be disconnected) when system resume, this
>> can avoid re-enumerating device."
>>
>>>> +             if (err)
>>>> +                     goto err_exit_phys;
>>>> +     }
>>>> +
>>>> +     return phy_roothub;
>>>> +
>>>> +err_exit_phys:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_exit(roothub_entry->phy);
>>>> +
>>>> +err_out:
>>>> +     return ERR_PTR(err);
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>> +
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err, ret = 0;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_exit(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     ret = ret;
>>>> +     }
>>>
>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> if I understood Chunfeng Yun correctly this will require
>> re-enumeration of the USB devices after a suspend/resume cycle on
>> Mediatek SoCs
>>
>
> OK. I suppose that there are 2 cases
> 1) Mediatek's case: USB controller context retained across suspend/resume.
> Remote wakeup probably required.
> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> during suspend/resume to keep PHY link active.
Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
that the parent of the USB controller can be marked as "wakeup-source"

> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> phy_exit()/phy_init() must be called during suspend/resume.
ACK

>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>
>>>> +
>>>> +     return ret;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_power_on(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     return 0;
>>>> +
>>>> +err_out:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>> +
>>>> +     return err;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>> +
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return;
>>>> +
>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>
>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>> we're no longer able to reach low power states on system suspend.
>> I'm not sure where this problem should be solved:
>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> TI platforms
>
> Many TI platforms are affected, omap5*, dra7*, am43*
>
>> - fix this in the usb_phy_roothub code
>
> I'd vote for fixing it in the usb_phy_roothub code. How?
> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
@Chunfeng: can you confirm Roger's idea that we could call phy_exit if
the controller is *NOT* marked as "wakeup-source"?
I am also not sure if it would work, since the "wakeup-source"
property is defined on the USB controller's parent node in case of the
Mediatek MTU3 (Mediatek USB3.0 DRD) controller

>> - fix this in the PHY driver
>
> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
I actually wonder if phy_ops should have explicit suspend/resume support:
- assuming we define two new callbacks: .suspend and .resume
- the PHY framework could call .power_off by default if .suspend is not defined
- the PHY framework could call .power_on by default if .resume is not defined
- drivers could set .suspend and .resume on their own, allowing more
fine-grained control by for example *only* stopping the clock (but not
re-initializing the registers, etc.)

@Roger: what do you think about this?
Kishon (the PHY framework maintainer) is also CC'ed - I would like to
hear his opinion too

>> - somewhere else
>>
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>> new file mode 100644
>>>> index 000000000000..6fde59bfbff8
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.h
>>>> @@ -0,0 +1,7 @@
>>>> +struct usb_phy_roothub;
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>
>>>
>
> <snip>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19 16:12                   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-19 16:12 UTC (permalink / raw)
  To: Roger Quadros, Chunfeng Yun
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Roger,

On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> Hi,
>
> On 19/03/18 00:29, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>> +some TI folks
>>>
>>> Hi Martin,
>>>
>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>> registered through the generic PHY framework. These PHYs have to be
>>>> enabled to make the USB controller actually work. They also have to be
>>>> disabled again on shutdown/suspend.
>>>>
>>>> Currently (at least) the following HCI platform drivers are using custom
>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>> disable/enable them when required:
>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>
>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>> USB controller's devicetree node (just like on the drivers listed
>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>> PHYs it not initialized then none of USB port works at all).
>>>>
>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>
>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>> I'll explain why below.
>> based on your explanation and reading the TI PHY drivers I am assuming
>> that the affected SoCs are using the "phy-omap-usb2" driver
>>
> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
I missed that, thanks

>>>> ---
>>>>  drivers/usb/core/Makefile |   2 +-
>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>
>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>> index 92c9cefb4317..18e874b0441e 100644
>>>> --- a/drivers/usb/core/Makefile
>>>> +++ b/drivers/usb/core/Makefile
>>>> @@ -6,7 +6,7 @@
>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>> -usbcore-y += port.o
>>>> +usbcore-y += phy.o port.o
>>>>
>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>> new file mode 100644
>>>> index 000000000000..09b7c43c0ea4
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.c
>>>> @@ -0,0 +1,158 @@
>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>> +/*
>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>> + *
>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> + */
>>>> +
>>>> +#include <linux/device.h>
>>>> +#include <linux/list.h>
>>>> +#include <linux/phy/phy.h>
>>>> +#include <linux/of.h>
>>>> +
>>>> +#include "phy.h"
>>>> +
>>>> +struct usb_phy_roothub {
>>>> +     struct phy              *phy;
>>>> +     struct list_head        list;
>>>> +};
>>>> +
>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>> +     if (!roothub_entry)
>>>> +             return ERR_PTR(-ENOMEM);
>>>> +
>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>> +
>>>> +     return roothub_entry;
>>>> +}
>>>> +
>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>> +                                struct list_head *list)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>> +
>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>> +                     return 0;
>>>> +             else
>>>> +                     return PTR_ERR(phy);
>>>> +     }
>>>> +
>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(roothub_entry))
>>>> +             return PTR_ERR(roothub_entry);
>>>> +
>>>> +     roothub_entry->phy = phy;
>>>> +
>>>> +     list_add_tail(&roothub_entry->list, list);
>>>> +
>>>> +     return 0;
>>>> +}
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *phy_roothub;
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int i, num_phys, err;
>>>> +
>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>> +                                           "#phy-cells");
>>>> +     if (num_phys <= 0)
>>>> +             return NULL;
>>>> +
>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(phy_roothub))
>>>> +             return phy_roothub;
>>>> +
>>>> +     for (i = 0; i < num_phys; i++) {
>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_init(roothub_entry->phy);
>>>
>>> The phy_init() function actually enables the PHY clocks.
>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>
>
> Yes.
>
>> an earlier version of my patch did exactly this, but it caused
>> problems during a suspend/resume cycle on Mediatek devices
>> Chunfeng Yun reported that issue here [0], quote from that mail for
>> easier reading:
>> "In order to keep link state on mt8173, we just power off all phys(not
>> exit) when system enter suspend, then power on them again (needn't
>> init, otherwise device will be disconnected) when system resume, this
>> can avoid re-enumerating device."
>>
>>>> +             if (err)
>>>> +                     goto err_exit_phys;
>>>> +     }
>>>> +
>>>> +     return phy_roothub;
>>>> +
>>>> +err_exit_phys:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_exit(roothub_entry->phy);
>>>> +
>>>> +err_out:
>>>> +     return ERR_PTR(err);
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>> +
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err, ret = 0;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_exit(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     ret = ret;
>>>> +     }
>>>
>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> if I understood Chunfeng Yun correctly this will require
>> re-enumeration of the USB devices after a suspend/resume cycle on
>> Mediatek SoCs
>>
>
> OK. I suppose that there are 2 cases
> 1) Mediatek's case: USB controller context retained across suspend/resume.
> Remote wakeup probably required.
> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> during suspend/resume to keep PHY link active.
Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
that the parent of the USB controller can be marked as "wakeup-source"

> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> phy_exit()/phy_init() must be called during suspend/resume.
ACK

>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>
>>>> +
>>>> +     return ret;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_power_on(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     return 0;
>>>> +
>>>> +err_out:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>> +
>>>> +     return err;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>> +
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return;
>>>> +
>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>
>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>> we're no longer able to reach low power states on system suspend.
>> I'm not sure where this problem should be solved:
>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> TI platforms
>
> Many TI platforms are affected, omap5*, dra7*, am43*
>
>> - fix this in the usb_phy_roothub code
>
> I'd vote for fixing it in the usb_phy_roothub code. How?
> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
@Chunfeng: can you confirm Roger's idea that we could call phy_exit if
the controller is *NOT* marked as "wakeup-source"?
I am also not sure if it would work, since the "wakeup-source"
property is defined on the USB controller's parent node in case of the
Mediatek MTU3 (Mediatek USB3.0 DRD) controller

>> - fix this in the PHY driver
>
> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
I actually wonder if phy_ops should have explicit suspend/resume support:
- assuming we define two new callbacks: .suspend and .resume
- the PHY framework could call .power_off by default if .suspend is not defined
- the PHY framework could call .power_on by default if .resume is not defined
- drivers could set .suspend and .resume on their own, allowing more
fine-grained control by for example *only* stopping the clock (but not
re-initializing the registers, etc.)

@Roger: what do you think about this?
Kishon (the PHY framework maintainer) is also CC'ed - I would like to
hear his opinion too

>> - somewhere else
>>
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>> new file mode 100644
>>>> index 000000000000..6fde59bfbff8
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.h
>>>> @@ -0,0 +1,7 @@
>>>> +struct usb_phy_roothub;
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>
>>>
>
> <snip>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19 16:12                   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-19 16:12 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Roger,

On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> Hi,
>
> On 19/03/18 00:29, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>> +some TI folks
>>>
>>> Hi Martin,
>>>
>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>> registered through the generic PHY framework. These PHYs have to be
>>>> enabled to make the USB controller actually work. They also have to be
>>>> disabled again on shutdown/suspend.
>>>>
>>>> Currently (at least) the following HCI platform drivers are using custom
>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>> disable/enable them when required:
>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>
>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>> USB controller's devicetree node (just like on the drivers listed
>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>> PHYs it not initialized then none of USB port works at all).
>>>>
>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>
>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>> I'll explain why below.
>> based on your explanation and reading the TI PHY drivers I am assuming
>> that the affected SoCs are using the "phy-omap-usb2" driver
>>
> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
I missed that, thanks

>>>> ---
>>>>  drivers/usb/core/Makefile |   2 +-
>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>
>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>> index 92c9cefb4317..18e874b0441e 100644
>>>> --- a/drivers/usb/core/Makefile
>>>> +++ b/drivers/usb/core/Makefile
>>>> @@ -6,7 +6,7 @@
>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>> -usbcore-y += port.o
>>>> +usbcore-y += phy.o port.o
>>>>
>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>> new file mode 100644
>>>> index 000000000000..09b7c43c0ea4
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.c
>>>> @@ -0,0 +1,158 @@
>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>> +/*
>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>> + *
>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> + */
>>>> +
>>>> +#include <linux/device.h>
>>>> +#include <linux/list.h>
>>>> +#include <linux/phy/phy.h>
>>>> +#include <linux/of.h>
>>>> +
>>>> +#include "phy.h"
>>>> +
>>>> +struct usb_phy_roothub {
>>>> +     struct phy              *phy;
>>>> +     struct list_head        list;
>>>> +};
>>>> +
>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>> +     if (!roothub_entry)
>>>> +             return ERR_PTR(-ENOMEM);
>>>> +
>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>> +
>>>> +     return roothub_entry;
>>>> +}
>>>> +
>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>> +                                struct list_head *list)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>> +
>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>> +                     return 0;
>>>> +             else
>>>> +                     return PTR_ERR(phy);
>>>> +     }
>>>> +
>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(roothub_entry))
>>>> +             return PTR_ERR(roothub_entry);
>>>> +
>>>> +     roothub_entry->phy = phy;
>>>> +
>>>> +     list_add_tail(&roothub_entry->list, list);
>>>> +
>>>> +     return 0;
>>>> +}
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *phy_roothub;
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int i, num_phys, err;
>>>> +
>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>> +                                           "#phy-cells");
>>>> +     if (num_phys <= 0)
>>>> +             return NULL;
>>>> +
>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(phy_roothub))
>>>> +             return phy_roothub;
>>>> +
>>>> +     for (i = 0; i < num_phys; i++) {
>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_init(roothub_entry->phy);
>>>
>>> The phy_init() function actually enables the PHY clocks.
>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>
>
> Yes.
>
>> an earlier version of my patch did exactly this, but it caused
>> problems during a suspend/resume cycle on Mediatek devices
>> Chunfeng Yun reported that issue here [0], quote from that mail for
>> easier reading:
>> "In order to keep link state on mt8173, we just power off all phys(not
>> exit) when system enter suspend, then power on them again (needn't
>> init, otherwise device will be disconnected) when system resume, this
>> can avoid re-enumerating device."
>>
>>>> +             if (err)
>>>> +                     goto err_exit_phys;
>>>> +     }
>>>> +
>>>> +     return phy_roothub;
>>>> +
>>>> +err_exit_phys:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_exit(roothub_entry->phy);
>>>> +
>>>> +err_out:
>>>> +     return ERR_PTR(err);
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>> +
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err, ret = 0;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_exit(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     ret = ret;
>>>> +     }
>>>
>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> if I understood Chunfeng Yun correctly this will require
>> re-enumeration of the USB devices after a suspend/resume cycle on
>> Mediatek SoCs
>>
>
> OK. I suppose that there are 2 cases
> 1) Mediatek's case: USB controller context retained across suspend/resume.
> Remote wakeup probably required.
> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> during suspend/resume to keep PHY link active.
Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
that the parent of the USB controller can be marked as "wakeup-source"

> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> phy_exit()/phy_init() must be called during suspend/resume.
ACK

>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>
>>>> +
>>>> +     return ret;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_power_on(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     return 0;
>>>> +
>>>> +err_out:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>> +
>>>> +     return err;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>> +
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return;
>>>> +
>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>
>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>> we're no longer able to reach low power states on system suspend.
>> I'm not sure where this problem should be solved:
>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> TI platforms
>
> Many TI platforms are affected, omap5*, dra7*, am43*
>
>> - fix this in the usb_phy_roothub code
>
> I'd vote for fixing it in the usb_phy_roothub code. How?
> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
@Chunfeng: can you confirm Roger's idea that we could call phy_exit if
the controller is *NOT* marked as "wakeup-source"?
I am also not sure if it would work, since the "wakeup-source"
property is defined on the USB controller's parent node in case of the
Mediatek MTU3 (Mediatek USB3.0 DRD) controller

>> - fix this in the PHY driver
>
> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
I actually wonder if phy_ops should have explicit suspend/resume support:
- assuming we define two new callbacks: .suspend and .resume
- the PHY framework could call .power_off by default if .suspend is not defined
- the PHY framework could call .power_on by default if .resume is not defined
- drivers could set .suspend and .resume on their own, allowing more
fine-grained control by for example *only* stopping the clock (but not
re-initializing the registers, etc.)

@Roger: what do you think about this?
Kishon (the PHY framework maintainer) is also CC'ed - I would like to
hear his opinion too

>> - somewhere else
>>
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>> new file mode 100644
>>>> index 000000000000..6fde59bfbff8
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.h
>>>> @@ -0,0 +1,7 @@
>>>> +struct usb_phy_roothub;
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>
>>>
>
> <snip>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-19 16:12                   ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-19 16:12 UTC (permalink / raw)
  To: linus-amlogic

Hi Roger,

On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> Hi,
>
> On 19/03/18 00:29, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>> +some TI folks
>>>
>>> Hi Martin,
>>>
>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>> registered through the generic PHY framework. These PHYs have to be
>>>> enabled to make the USB controller actually work. They also have to be
>>>> disabled again on shutdown/suspend.
>>>>
>>>> Currently (at least) the following HCI platform drivers are using custom
>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>> disable/enable them when required:
>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>
>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>> USB controller's devicetree node (just like on the drivers listed
>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>> PHYs it not initialized then none of USB port works at all).
>>>>
>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>
>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>> I'll explain why below.
>> based on your explanation and reading the TI PHY drivers I am assuming
>> that the affected SoCs are using the "phy-omap-usb2" driver
>>
> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
I missed that, thanks

>>>> ---
>>>>  drivers/usb/core/Makefile |   2 +-
>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>
>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>> index 92c9cefb4317..18e874b0441e 100644
>>>> --- a/drivers/usb/core/Makefile
>>>> +++ b/drivers/usb/core/Makefile
>>>> @@ -6,7 +6,7 @@
>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>> -usbcore-y += port.o
>>>> +usbcore-y += phy.o port.o
>>>>
>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>> new file mode 100644
>>>> index 000000000000..09b7c43c0ea4
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.c
>>>> @@ -0,0 +1,158 @@
>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>> +/*
>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>> + *
>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> + */
>>>> +
>>>> +#include <linux/device.h>
>>>> +#include <linux/list.h>
>>>> +#include <linux/phy/phy.h>
>>>> +#include <linux/of.h>
>>>> +
>>>> +#include "phy.h"
>>>> +
>>>> +struct usb_phy_roothub {
>>>> +     struct phy              *phy;
>>>> +     struct list_head        list;
>>>> +};
>>>> +
>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>> +     if (!roothub_entry)
>>>> +             return ERR_PTR(-ENOMEM);
>>>> +
>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>> +
>>>> +     return roothub_entry;
>>>> +}
>>>> +
>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>> +                                struct list_head *list)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>> +
>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>> +                     return 0;
>>>> +             else
>>>> +                     return PTR_ERR(phy);
>>>> +     }
>>>> +
>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(roothub_entry))
>>>> +             return PTR_ERR(roothub_entry);
>>>> +
>>>> +     roothub_entry->phy = phy;
>>>> +
>>>> +     list_add_tail(&roothub_entry->list, list);
>>>> +
>>>> +     return 0;
>>>> +}
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *phy_roothub;
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int i, num_phys, err;
>>>> +
>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>> +                                           "#phy-cells");
>>>> +     if (num_phys <= 0)
>>>> +             return NULL;
>>>> +
>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(phy_roothub))
>>>> +             return phy_roothub;
>>>> +
>>>> +     for (i = 0; i < num_phys; i++) {
>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_init(roothub_entry->phy);
>>>
>>> The phy_init() function actually enables the PHY clocks.
>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>
>
> Yes.
>
>> an earlier version of my patch did exactly this, but it caused
>> problems during a suspend/resume cycle on Mediatek devices
>> Chunfeng Yun reported that issue here [0], quote from that mail for
>> easier reading:
>> "In order to keep link state on mt8173, we just power off all phys(not
>> exit) when system enter suspend, then power on them again (needn't
>> init, otherwise device will be disconnected) when system resume, this
>> can avoid re-enumerating device."
>>
>>>> +             if (err)
>>>> +                     goto err_exit_phys;
>>>> +     }
>>>> +
>>>> +     return phy_roothub;
>>>> +
>>>> +err_exit_phys:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_exit(roothub_entry->phy);
>>>> +
>>>> +err_out:
>>>> +     return ERR_PTR(err);
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>> +
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err, ret = 0;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_exit(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     ret = ret;
>>>> +     }
>>>
>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> if I understood Chunfeng Yun correctly this will require
>> re-enumeration of the USB devices after a suspend/resume cycle on
>> Mediatek SoCs
>>
>
> OK. I suppose that there are 2 cases
> 1) Mediatek's case: USB controller context retained across suspend/resume.
> Remote wakeup probably required.
> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> during suspend/resume to keep PHY link active.
Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
that the parent of the USB controller can be marked as "wakeup-source"

> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> phy_exit()/phy_init() must be called during suspend/resume.
ACK

>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>
>>>> +
>>>> +     return ret;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_power_on(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     return 0;
>>>> +
>>>> +err_out:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>> +
>>>> +     return err;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>> +
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return;
>>>> +
>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>
>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>> we're no longer able to reach low power states on system suspend.
>> I'm not sure where this problem should be solved:
>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> TI platforms
>
> Many TI platforms are affected, omap5*, dra7*, am43*
>
>> - fix this in the usb_phy_roothub code
>
> I'd vote for fixing it in the usb_phy_roothub code. How?
> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
@Chunfeng: can you confirm Roger's idea that we could call phy_exit if
the controller is *NOT* marked as "wakeup-source"?
I am also not sure if it would work, since the "wakeup-source"
property is defined on the USB controller's parent node in case of the
Mediatek MTU3 (Mediatek USB3.0 DRD) controller

>> - fix this in the PHY driver
>
> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
I actually wonder if phy_ops should have explicit suspend/resume support:
- assuming we define two new callbacks: .suspend and .resume
- the PHY framework could call .power_off by default if .suspend is not defined
- the PHY framework could call .power_on by default if .resume is not defined
- drivers could set .suspend and .resume on their own, allowing more
fine-grained control by for example *only* stopping the clock (but not
re-initializing the registers, etc.)

@Roger: what do you think about this?
Kishon (the PHY framework maintainer) is also CC'ed - I would like to
hear his opinion too

>> - somewhere else
>>
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>> new file mode 100644
>>>> index 000000000000..6fde59bfbff8
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.h
>>>> @@ -0,0 +1,7 @@
>>>> +struct usb_phy_roothub;
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>
>>>
>
> <snip>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20  7:54                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20  7:54 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Roger Quadros,
	Gerlach, Dave, gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

Hi Martin,

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> >>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> >>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
In fact, xhci-mtk driver always keeps link state across suspend/resume,
no matter "wakeup-source" is marked or not, it's just used to
enable/disable to send wakeup signal to SPM.

> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
I'm afraid it doesn't work.
If re-initialize phys when system resume, it will cause usb controller
to be re-initialized too, but the driver doesn't support it currently.

> 
> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20  7:54                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20  7:54 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: Roger Quadros, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Martin,

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
In fact, xhci-mtk driver always keeps link state across suspend/resume,
no matter "wakeup-source" is marked or not, it's just used to
enable/disable to send wakeup signal to SPM.

> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
I'm afraid it doesn't work.
If re-initialize phys when system resume, it will cause usb controller
to be re-initialized too, but the driver doesn't support it currently.

> 
> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20  7:54                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20  7:54 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Martin,

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
In fact, xhci-mtk driver always keeps link state across suspend/resume,
no matter "wakeup-source" is marked or not, it's just used to
enable/disable to send wakeup signal to SPM.

> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
I'm afraid it doesn't work.
If re-initialize phys when system resume, it will cause usb controller
to be re-initialized too, but the driver doesn't support it currently.

> 
> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20  7:54                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20  7:54 UTC (permalink / raw)
  To: linus-amlogic

Hi Martin,

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
In fact, xhci-mtk driver always keeps link state across suspend/resume,
no matter "wakeup-source" is marked or not, it's just used to
enable/disable to send wakeup signal to SPM.

> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
I'm afraid it doesn't work.
If re-initialize phys when system resume, it will cause usb controller
to be re-initialized too, but the driver doesn't support it currently.

> 
> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 10:55                       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 10:55 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun, KISHON VIJAY ABRAHAM
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

On 19/03/18 18:12, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller

That is about wakeup-source and not about whether remote wakeup should be enabled or not.
i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.

> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?

Looks fine to me. However one thing is still not clear. How does the PHY driver
decide if it needs to do a full power off or partial power off?
It doesn't know about the roothub's device_may_wakeup() status.

> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
>>> - somewhere else
>>>
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>> new file mode 100644
>>>>> index 000000000000..6fde59bfbff8
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.h
>>>>> @@ -0,0 +1,7 @@
>>>>> +struct usb_phy_roothub;
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>
>>>>
>>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 10:55                       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 10:55 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun, KISHON VIJAY ABRAHAM
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave

On 19/03/18 18:12, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller

That is about wakeup-source and not about whether remote wakeup should be enabled or not.
i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.

> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?

Looks fine to me. However one thing is still not clear. How does the PHY driver
decide if it needs to do a full power off or partial power off?
It doesn't know about the roothub's device_may_wakeup() status.

> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
>>> - somewhere else
>>>
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>> new file mode 100644
>>>>> index 000000000000..6fde59bfbff8
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.h
>>>>> @@ -0,0 +1,7 @@
>>>>> +struct usb_phy_roothub;
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>
>>>>
>>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 10:55                       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 10:55 UTC (permalink / raw)
  To: linux-arm-kernel

On 19/03/18 18:12, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller

That is about wakeup-source and not about whether remote wakeup should be enabled or not.
i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.

> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?

Looks fine to me. However one thing is still not clear. How does the PHY driver
decide if it needs to do a full power off or partial power off?
It doesn't know about the roothub's device_may_wakeup() status.

> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
>>> - somewhere else
>>>
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>> new file mode 100644
>>>>> index 000000000000..6fde59bfbff8
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.h
>>>>> @@ -0,0 +1,7 @@
>>>>> +struct usb_phy_roothub;
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>
>>>>
>>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 10:55                       ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 10:55 UTC (permalink / raw)
  To: linus-amlogic

On 19/03/18 18:12, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller

That is about wakeup-source and not about whether remote wakeup should be enabled or not.
i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.

> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?

Looks fine to me. However one thing is still not clear. How does the PHY driver
decide if it needs to do a full power off or partial power off?
It doesn't know about the roothub's device_may_wakeup() status.

> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
>>> - somewhere else
>>>
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>> new file mode 100644
>>>>> index 000000000000..6fde59bfbff8
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.h
>>>>> @@ -0,0 +1,7 @@
>>>>> +struct usb_phy_roothub;
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>
>>>>
>>

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:02                           ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 11:02 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun, KISHON VIJAY ABRAHAM
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

On 20/03/18 12:55, Roger Quadros wrote:
> On 19/03/18 18:12, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
> That is about wakeup-source and not about whether remote wakeup should be enabled or not.

correction. It indeed means enable/disable wakeup :).

> i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.
> 
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
> 
> Looks fine to me. However one thing is still not clear. How does the PHY driver
> decide if it needs to do a full power off or partial power off?
> It doesn't know about the roothub's device_may_wakeup() status.
> 
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>>>> - somewhere else
>>>>
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>> new file mode 100644
>>>>>> index 000000000000..6fde59bfbff8
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>> @@ -0,0 +1,7 @@
>>>>>> +struct usb_phy_roothub;
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>
>>>>>
>>>
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:02                           ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 11:02 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun, KISHON VIJAY ABRAHAM
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave

On 20/03/18 12:55, Roger Quadros wrote:
> On 19/03/18 18:12, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
> That is about wakeup-source and not about whether remote wakeup should be enabled or not.

correction. It indeed means enable/disable wakeup :).

> i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.
> 
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
> 
> Looks fine to me. However one thing is still not clear. How does the PHY driver
> decide if it needs to do a full power off or partial power off?
> It doesn't know about the roothub's device_may_wakeup() status.
> 
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>>>> - somewhere else
>>>>
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>> new file mode 100644
>>>>>> index 000000000000..6fde59bfbff8
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>> @@ -0,0 +1,7 @@
>>>>>> +struct usb_phy_roothub;
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>
>>>>>
>>>
>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:02                           ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 11:02 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/03/18 12:55, Roger Quadros wrote:
> On 19/03/18 18:12, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
> That is about wakeup-source and not about whether remote wakeup should be enabled or not.

correction. It indeed means enable/disable wakeup :).

> i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.
> 
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
> 
> Looks fine to me. However one thing is still not clear. How does the PHY driver
> decide if it needs to do a full power off or partial power off?
> It doesn't know about the roothub's device_may_wakeup() status.
> 
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>>>> - somewhere else
>>>>
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>> new file mode 100644
>>>>>> index 000000000000..6fde59bfbff8
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>> @@ -0,0 +1,7 @@
>>>>>> +struct usb_phy_roothub;
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>
>>>>>
>>>
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:02                           ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-20 11:02 UTC (permalink / raw)
  To: linus-amlogic

On 20/03/18 12:55, Roger Quadros wrote:
> On 19/03/18 18:12, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
> That is about wakeup-source and not about whether remote wakeup should be enabled or not.

correction. It indeed means enable/disable wakeup :).

> i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.
> 
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
> 
> Looks fine to me. However one thing is still not clear. How does the PHY driver
> decide if it needs to do a full power off or partial power off?
> It doesn't know about the roothub's device_may_wakeup() status.
> 
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>>>> - somewhere else
>>>>
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>> new file mode 100644
>>>>>> index 000000000000..6fde59bfbff8
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>> @@ -0,0 +1,7 @@
>>>>>> +struct usb_phy_roothub;
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>
>>>>>
>>>
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:27                     ` Kishon Vijay Abraham I
  0 siblings, 0 replies; 115+ messages in thread
From: Kishon Vijay Abraham I @ 2018-03-20 11:27 UTC (permalink / raw)
  To: Martin Blumenstingl, Roger Quadros, Chunfeng Yun
  Cc: mark.rutland, Peter.Chen, Neil Armstrong, linux-tegra,
	thierry.reding, linux, felipe.balbi, jonathanh, stern,
	devicetree, arnd, yixun.lan, robh+dt, linux-mediatek,
	matthias.bgg, linux-amlogic, linux-omap, linux-arm-kernel,
	Gerlach, Dave, Keerthy, linux-usb, mathias.nyman, gregkh

Hi,

On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:

Not in favor of adding explicit suspend/resume ops since PM framework already
has those. I think we should let PHY drivers manage suspend/resume on its own
(after creating the dependency between the controller device and PHY using
device_link_add).

Thanks
Kishon

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:27                     ` Kishon Vijay Abraham I
  0 siblings, 0 replies; 115+ messages in thread
From: Kishon Vijay Abraham I @ 2018-03-20 11:27 UTC (permalink / raw)
  To: Martin Blumenstingl, Roger Quadros, Chunfeng Yun
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave

Hi,

On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:

Not in favor of adding explicit suspend/resume ops since PM framework already
has those. I think we should let PHY drivers manage suspend/resume on its own
(after creating the dependency between the controller device and PHY using
device_link_add).

Thanks
Kishon
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:27                     ` Kishon Vijay Abraham I
  0 siblings, 0 replies; 115+ messages in thread
From: Kishon Vijay Abraham I @ 2018-03-20 11:27 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:

Not in favor of adding explicit suspend/resume ops since PM framework already
has those. I think we should let PHY drivers manage suspend/resume on its own
(after creating the dependency between the controller device and PHY using
device_link_add).

Thanks
Kishon

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 11:27                     ` Kishon Vijay Abraham I
  0 siblings, 0 replies; 115+ messages in thread
From: Kishon Vijay Abraham I @ 2018-03-20 11:27 UTC (permalink / raw)
  To: linus-amlogic

Hi,

On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,7 @@
>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>> -usbcore-y += port.o
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:

Not in favor of adding explicit suspend/resume ops since PM framework already
has those. I think we should let PHY drivers manage suspend/resume on its own
(after creating the dependency between the controller device and PHY using
device_link_add).

Thanks
Kishon

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 12:04                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 12:04 UTC (permalink / raw)
  To: Martin Blumenstingl, Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

Hi Martin & Roger:

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> >>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> >>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
devices by device_init_wakeup(dev, true),but not dependent on
"wakeup-source" property, so maybe we can use device_can_wakeup() to
decide whether call phy_exit()/init() or not.

> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 12:04                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 12:04 UTC (permalink / raw)
  To: Martin Blumenstingl, Roger Quadros
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Martin & Roger:

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
devices by device_init_wakeup(dev, true),but not dependent on
"wakeup-source" property, so maybe we can use device_can_wakeup() to
decide whether call phy_exit()/init() or not.

> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 12:04                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 12:04 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Martin & Roger:

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
devices by device_init_wakeup(dev, true),but not dependent on
"wakeup-source" property, so maybe we can use device_can_wakeup() to
decide whether call phy_exit()/init() or not.

> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 12:04                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 12:04 UTC (permalink / raw)
  To: linus-amlogic

Hi Martin & Roger:

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,7 @@
> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>> -usbcore-y += port.o
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
devices by device_init_wakeup(dev, true),but not dependent on
"wakeup-source" property, so maybe we can use device_can_wakeup() to
decide whether call phy_exit()/init() or not.

> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 21:57                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 21:57 UTC (permalink / raw)
  To: Kishon Vijay Abraham I
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz, Chunfeng Yun,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Roger Quadros,
	Gerlach, Dave, gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA, linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	arnd-r2nGTMty4D4, Keerthy

Hello Kishon,

On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon-l0cyMroinI0@public.gmane.org> wrote:
> Hi,
>
> On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>
> Not in favor of adding explicit suspend/resume ops since PM framework already
> has those. I think we should let PHY drivers manage suspend/resume on its own
> (after creating the dependency between the controller device and PHY using
> device_link_add).
even better if we can re-use some existing code!

the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
support suspend/resume yet, so unfortunately I cannot test this.
besides that I have zero experience with suspend/resume logic.

I'll try to read more about that topic, but I definitely need someone
who could help testing if I have a patch ready
in any case: I think implementing this should not be done for v4.17
anymore - maybe we can find a small solution for v4.17 and switch to
your proposed solution later (assuming nobody has arguments against
that)


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 21:57                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 21:57 UTC (permalink / raw)
  To: Kishon Vijay Abraham I
  Cc: Roger Quadros, Chunfeng Yun, linux-usb, mathias.nyman, arnd,
	gregkh, felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave

Hello Kishon,

On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> Hi,
>
> On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>
> Not in favor of adding explicit suspend/resume ops since PM framework already
> has those. I think we should let PHY drivers manage suspend/resume on its own
> (after creating the dependency between the controller device and PHY using
> device_link_add).
even better if we can re-use some existing code!

the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
support suspend/resume yet, so unfortunately I cannot test this.
besides that I have zero experience with suspend/resume logic.

I'll try to read more about that topic, but I definitely need someone
who could help testing if I have a patch ready
in any case: I think implementing this should not be done for v4.17
anymore - maybe we can find a small solution for v4.17 and switch to
your proposed solution later (assuming nobody has arguments against
that)


Regards
Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 21:57                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 21:57 UTC (permalink / raw)
  To: linux-arm-kernel

Hello Kishon,

On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> Hi,
>
> On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>
> Not in favor of adding explicit suspend/resume ops since PM framework already
> has those. I think we should let PHY drivers manage suspend/resume on its own
> (after creating the dependency between the controller device and PHY using
> device_link_add).
even better if we can re-use some existing code!

the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
support suspend/resume yet, so unfortunately I cannot test this.
besides that I have zero experience with suspend/resume logic.

I'll try to read more about that topic, but I definitely need someone
who could help testing if I have a patch ready
in any case: I think implementing this should not be done for v4.17
anymore - maybe we can find a small solution for v4.17 and switch to
your proposed solution later (assuming nobody has arguments against
that)


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 21:57                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 21:57 UTC (permalink / raw)
  To: linus-amlogic

Hello Kishon,

On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> Hi,
>
> On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,7 @@
>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>> -usbcore-y += port.o
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>
> Not in favor of adding explicit suspend/resume ops since PM framework already
> has those. I think we should let PHY drivers manage suspend/resume on its own
> (after creating the dependency between the controller device and PHY using
> device_link_add).
even better if we can re-use some existing code!

the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
support suspend/resume yet, so unfortunately I cannot test this.
besides that I have zero experience with suspend/resume logic.

I'll try to read more about that topic, but I definitely need someone
who could help testing if I have a patch ready
in any case: I think implementing this should not be done for v4.17
anymore - maybe we can find a small solution for v4.17 and switch to
your proposed solution later (assuming nobody has arguments against
that)


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 22:01                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 22:01 UTC (permalink / raw)
  To: Chunfeng Yun, Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

[-- Attachment #1: Type: text/plain, Size: 14640 bytes --]

Hi Roger, Hi Chunfeng,

On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
> Hi Martin & Roger:
>
> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>> > Hi,
>> >
>> > On 19/03/18 00:29, Martin Blumenstingl wrote:
>> >> Hi Roger,
>> >>
>> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>> >>> +some TI folks
>> >>>
>> >>> Hi Martin,
>> >>>
>> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> >>>> Many SoC platforms have separate devices for the USB PHY which are
>> >>>> registered through the generic PHY framework. These PHYs have to be
>> >>>> enabled to make the USB controller actually work. They also have to be
>> >>>> disabled again on shutdown/suspend.
>> >>>>
>> >>>> Currently (at least) the following HCI platform drivers are using custom
>> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
>> >>>> disable/enable them when required:
>> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
>> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
>> >>>>
>> >>>> With this new wrapper the USB PHYs can be specified directly in the
>> >>>> USB controller's devicetree node (just like on the drivers listed
>> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
>> >>>> controller and require all USB PHYs to be initialized (if one of the USB
>> >>>> PHYs it not initialized then none of USB port works at all).
>> >>>>
>> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>> >>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>> >>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>> >>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>> >>>
>> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> >>> I'll explain why below.
>> >> based on your explanation and reading the TI PHY drivers I am assuming
>> >> that the affected SoCs are using the "phy-omap-usb2" driver
>> >>
>> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>> >>>> ---
>> >>>>  drivers/usb/core/Makefile |   2 +-
>> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>> >>>>  drivers/usb/core/phy.h    |   7 ++
>> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>> >>>>  create mode 100644 drivers/usb/core/phy.c
>> >>>>  create mode 100644 drivers/usb/core/phy.h
>> >>>>
>> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> >>>> index 92c9cefb4317..18e874b0441e 100644
>> >>>> --- a/drivers/usb/core/Makefile
>> >>>> +++ b/drivers/usb/core/Makefile
>> >>>> @@ -6,7 +6,7 @@
>> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> >>>> -usbcore-y += port.o
>> >>>> +usbcore-y += phy.o port.o
>> >>>>
>> >>>>  usbcore-$(CONFIG_OF)         += of.o
>> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> >>>> new file mode 100644
>> >>>> index 000000000000..09b7c43c0ea4
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.c
>> >>>> @@ -0,0 +1,158 @@
>> >>>> +// SPDX-License-Identifier: GPL-2.0+
>> >>>> +/*
>> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> >>>> + * all PHYs on a HCD and to keep them all in the same state.
>> >>>> + *
>> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>> >>>> + */
>> >>>> +
>> >>>> +#include <linux/device.h>
>> >>>> +#include <linux/list.h>
>> >>>> +#include <linux/phy/phy.h>
>> >>>> +#include <linux/of.h>
>> >>>> +
>> >>>> +#include "phy.h"
>> >>>> +
>> >>>> +struct usb_phy_roothub {
>> >>>> +     struct phy              *phy;
>> >>>> +     struct list_head        list;
>> >>>> +};
>> >>>> +
>> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> >>>> +     if (!roothub_entry)
>> >>>> +             return ERR_PTR(-ENOMEM);
>> >>>> +
>> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> >>>> +
>> >>>> +     return roothub_entry;
>> >>>> +}
>> >>>> +
>> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> >>>> +                                struct list_head *list)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> >>>> +
>> >>>> +     if (IS_ERR_OR_NULL(phy)) {
>> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> >>>> +                     return 0;
>> >>>> +             else
>> >>>> +                     return PTR_ERR(phy);
>> >>>> +     }
>> >>>> +
>> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(roothub_entry))
>> >>>> +             return PTR_ERR(roothub_entry);
>> >>>> +
>> >>>> +     roothub_entry->phy = phy;
>> >>>> +
>> >>>> +     list_add_tail(&roothub_entry->list, list);
>> >>>> +
>> >>>> +     return 0;
>> >>>> +}
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *phy_roothub;
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int i, num_phys, err;
>> >>>> +
>> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> >>>> +                                           "#phy-cells");
>> >>>> +     if (num_phys <= 0)
>> >>>> +             return NULL;
>> >>>> +
>> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(phy_roothub))
>> >>>> +             return phy_roothub;
>> >>>> +
>> >>>> +     for (i = 0; i < num_phys; i++) {
>> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_init(roothub_entry->phy);
>> >>>
>> >>> The phy_init() function actually enables the PHY clocks.
>> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>> >>
>> >
>> > Yes.
>> >
>> >> an earlier version of my patch did exactly this, but it caused
>> >> problems during a suspend/resume cycle on Mediatek devices
>> >> Chunfeng Yun reported that issue here [0], quote from that mail for
>> >> easier reading:
>> >> "In order to keep link state on mt8173, we just power off all phys(not
>> >> exit) when system enter suspend, then power on them again (needn't
>> >> init, otherwise device will be disconnected) when system resume, this
>> >> can avoid re-enumerating device."
>> >>
>> >>>> +             if (err)
>> >>>> +                     goto err_exit_phys;
>> >>>> +     }
>> >>>> +
>> >>>> +     return phy_roothub;
>> >>>> +
>> >>>> +err_exit_phys:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_exit(roothub_entry->phy);
>> >>>> +
>> >>>> +err_out:
>> >>>> +     return ERR_PTR(err);
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> >>>> +
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err, ret = 0;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_exit(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     ret = ret;
>> >>>> +     }
>> >>>
>> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> >> if I understood Chunfeng Yun correctly this will require
>> >> re-enumeration of the USB devices after a suspend/resume cycle on
>> >> Mediatek SoCs
>> >>
>> >
>> > OK. I suppose that there are 2 cases
>> > 1) Mediatek's case: USB controller context retained across suspend/resume.
>> > Remote wakeup probably required.
>> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> > during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> > phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>> >>>
>> >>>> +
>> >>>> +     return ret;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_power_on(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     return 0;
>> >>>> +
>> >>>> +err_out:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>> +
>> >>>> +     return err;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> >>>> +
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return;
>> >>>> +
>> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>
>> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> >>> we're no longer able to reach low power states on system suspend.
>> >> I'm not sure where this problem should be solved:
>> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> >> TI platforms
>> >
>> > Many TI platforms are affected, omap5*, dra7*, am43*
>> >
>> >> - fix this in the usb_phy_roothub code
>> >
>> > I'd vote for fixing it in the usb_phy_roothub code. How?
>> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> devices by device_init_wakeup(dev, true),but not dependent on
> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> decide whether call phy_exit()/init() or not.
the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
yet, so I cannot test this
based on this suggestion I threw up two patches which are *compile
tested only* based on Greg's usb-next branch
you can find them here: [0] (as well as attached to this mail)

@Chunfeng: can you please test this on one of your Mediatek SoCs?
@Roger: can you please test this on a TI SoC?

(apologies in advance if these patches don't work)

please note that I won't have access to my computer until Saturday.
if these patches need to be rewritten/replaced/etc. then feel free to
send your own version to the list

>> >> - fix this in the PHY driver
>> >
>> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>> >> - somewhere else
>> >>
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> >>>> new file mode 100644
>> >>>> index 000000000000..6fde59bfbff8
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.h
>> >>>> @@ -0,0 +1,7 @@
>> >>>> +struct usb_phy_roothub;
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>> >>>>
>> >>>
>> >
>> > <snip>
>> >
>> > --
>> > cheers,
>> > -roger
>> >
>> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>>
>>
>> Regards
>> Martin
>
>


Regards
Martin


[0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1

[-- Attachment #2: 0001-usb-core-split-usb_phy_roothub_-init-alloc.patch --]
[-- Type: text/x-patch, Size: 5244 bytes --]

From 78afe3dfbf21dd521076d1fd625096fa440f5195 Mon Sep 17 00:00:00 2001
From: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Date: Tue, 20 Mar 2018 22:36:39 +0100
Subject: [PATCH 1/2] usb: core: split usb_phy_roothub_{init,alloc}

Before this patch usb_phy_roothub_init served two purposes (from a
caller's point of view - like hcd.c):
- parsing the PHYs and allocating the list entries
- calling phy_init on each list entry

While this worked so far it has one disadvantage: if we need to call
phy_init for each PHY instance then the existing code cannot be re-used.
Solve this by splitting off usb_phy_roothub_alloc which only parses the
PHYs and allocates the list entries.
usb_phy_roothub_init then gets a struct usb_phy_roothub and only calls
phy_init on each PHY instance (along with the corresponding cleanup if
that failed somewhere).

This is a preparation step for adding proper suspend support for some
hardware that requires phy_exit to be called during suspend and phy_init
to be called during resume.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c | 10 +++++++---
 drivers/usb/core/phy.c | 45 ++++++++++++++++++++++-----------------------
 drivers/usb/core/phy.h |  4 +++-
 3 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 5a92d8f7c484..89b10d859047 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2758,12 +2758,16 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	}
 
 	if (!hcd->skip_phy_initialization) {
-		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
 			retval = PTR_ERR(hcd->phy_roothub);
-			goto err_phy_roothub_init;
+			goto err_phy_roothub_alloc;
 		}
 
+		retval = usb_phy_roothub_init(hcd->phy_roothub);
+		if (retval)
+			goto err_phy_roothub_alloc;
+
 		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
 		if (retval)
 			goto err_usb_phy_roothub_power_on;
@@ -2936,7 +2940,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
-err_phy_roothub_init:
+err_phy_roothub_alloc:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43c0ea4..50a80dbe776c 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -19,19 +19,6 @@ struct usb_phy_roothub {
 	struct list_head	list;
 };
 
-static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
-{
-	struct usb_phy_roothub *roothub_entry;
-
-	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
-	if (!roothub_entry)
-		return ERR_PTR(-ENOMEM);
-
-	INIT_LIST_HEAD(&roothub_entry->list);
-
-	return roothub_entry;
-}
-
 static int usb_phy_roothub_add_phy(struct device *dev, int index,
 				   struct list_head *list)
 {
@@ -56,11 +43,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 	return 0;
 }
 
-struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -68,16 +53,31 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	if (num_phys <= 0)
 		return NULL;
 
-	phy_roothub = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(phy_roothub))
-		return phy_roothub;
+	phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
+	if (!phy_roothub)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&phy_roothub->list);
 
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
+			return ERR_PTR(err);
 	}
 
+	return phy_roothub;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
+
+int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
@@ -86,14 +86,13 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 			goto err_exit_phys;
 	}
 
-	return phy_roothub;
+	return 0;
 
 err_exit_phys:
 	list_for_each_entry_continue_reverse(roothub_entry, head, list)
 		phy_exit(roothub_entry->phy);
 
-err_out:
-	return ERR_PTR(err);
+	return err;
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
index 6fde59bfbff8..eb31253201ad 100644
--- a/drivers/usb/core/phy.h
+++ b/drivers/usb/core/phy.h
@@ -1,6 +1,8 @@
 struct usb_phy_roothub;
 
-struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev);
+
+int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub);
 int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
 
 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
-- 
2.16.2


[-- Attachment #3: 0002-usb-core-use-phy_exit-during-suspend-if-wake-up-is-n.patch --]
[-- Type: text/x-patch, Size: 4460 bytes --]

From 1367f49ff49cec249bd9ba95e5395dd1760eee8b Mon Sep 17 00:00:00 2001
From: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Date: Tue, 20 Mar 2018 22:47:38 +0100
Subject: [PATCH 2/2] usb: core: use phy_exit during suspend if wake up is not
 supported

If the USB controller can wake up the system (which is the case for
example with the Mediatek USB3 IP) then we must not call phy_exit during
suspend to ensure that the USB controller doesn't have to re-enumerate
the devices during resume.
However, if the USB controller cannot wake up the system (which is the
case for example on various TI platforms using a dwc3 controller) then
we must call phy_exit during suspend. Otherwise the PHY driver keeps the
clocks enabled, which prevents the system from entering the suspend
state.

Solve this by introducing two new functions in the PHY wrapper which are
dedicated to the suspend and resume handling.
If the controller can wake up the system the new usb_phy_roothub_suspend
function will simply call usb_phy_roothub_power_off. However, if wake up
is not supported by the controller it will also call
usb_phy_roothub_exit.
The also new usb_phy_roothub_resume function takes care of calling
usb_phy_roothub_init (if the controller can't wake up the system) in
addition to usb_phy_roothub_power_on.

Fixes: 07dbff0ddbd86c ("usb: core: add a wrapper for the USB PHYs on the HCD")
Fixes: 178a0bce05cbc1 ("usb: core: hcd: integrate the PHY wrapper into the HCD core")
Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c |  6 +++---
 drivers/usb/core/phy.c | 29 +++++++++++++++++++++++++++++
 drivers/usb/core/phy.h |  5 +++++
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 89b10d859047..6d4a4195ca00 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_power_off(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_power_off(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 50a80dbe776c..2eca371684fa 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -155,3 +155,32 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 		phy_power_off(roothub_entry->phy);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
+
+int usb_phy_roothub_suspend(struct device *dev,
+			    struct usb_phy_roothub *phy_roothub)
+{
+	usb_phy_roothub_power_off(phy_roothub);
+
+	/* keep the PHYs initialized so the device can wake up the system */
+	if (device_can_wakeup(dev))
+		return 0;
+
+	return usb_phy_roothub_exit(phy_roothub);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
+
+int usb_phy_roothub_resume(struct device *dev,
+			   struct usb_phy_roothub *phy_roothub)
+{
+	int err;
+
+	/* if the device can't wake up the system _exit was called */
+	if (device_can_wakeup(dev)) {
+		err = usb_phy_roothub_init(phy_roothub);
+		if (err)
+			return err;
+	}
+
+	return usb_phy_roothub_power_on(phy_roothub);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
index eb31253201ad..601aefd41878 100644
--- a/drivers/usb/core/phy.h
+++ b/drivers/usb/core/phy.h
@@ -7,3 +7,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
 
 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
 void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_suspend(struct device *dev,
+			    struct usb_phy_roothub *phy_roothub);
+int usb_phy_roothub_resume(struct device *dev,
+			   struct usb_phy_roothub *phy_roothub);
-- 
2.16.2


[-- Attachment #4: Type: text/plain, Size: 200 bytes --]

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 22:01                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 22:01 UTC (permalink / raw)
  To: Chunfeng Yun, Roger Quadros
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Roger, Hi Chunfeng,

On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> Hi Martin & Roger:
>
> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> > Hi,
>> >
>> > On 19/03/18 00:29, Martin Blumenstingl wrote:
>> >> Hi Roger,
>> >>
>> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> >>> +some TI folks
>> >>>
>> >>> Hi Martin,
>> >>>
>> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> >>>> Many SoC platforms have separate devices for the USB PHY which are
>> >>>> registered through the generic PHY framework. These PHYs have to be
>> >>>> enabled to make the USB controller actually work. They also have to be
>> >>>> disabled again on shutdown/suspend.
>> >>>>
>> >>>> Currently (at least) the following HCI platform drivers are using custom
>> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
>> >>>> disable/enable them when required:
>> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
>> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
>> >>>>
>> >>>> With this new wrapper the USB PHYs can be specified directly in the
>> >>>> USB controller's devicetree node (just like on the drivers listed
>> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
>> >>>> controller and require all USB PHYs to be initialized (if one of the USB
>> >>>> PHYs it not initialized then none of USB port works at all).
>> >>>>
>> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>> >>>
>> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> >>> I'll explain why below.
>> >> based on your explanation and reading the TI PHY drivers I am assuming
>> >> that the affected SoCs are using the "phy-omap-usb2" driver
>> >>
>> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>> >>>> ---
>> >>>>  drivers/usb/core/Makefile |   2 +-
>> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>> >>>>  drivers/usb/core/phy.h    |   7 ++
>> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>> >>>>  create mode 100644 drivers/usb/core/phy.c
>> >>>>  create mode 100644 drivers/usb/core/phy.h
>> >>>>
>> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> >>>> index 92c9cefb4317..18e874b0441e 100644
>> >>>> --- a/drivers/usb/core/Makefile
>> >>>> +++ b/drivers/usb/core/Makefile
>> >>>> @@ -6,7 +6,7 @@
>> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> >>>> -usbcore-y += port.o
>> >>>> +usbcore-y += phy.o port.o
>> >>>>
>> >>>>  usbcore-$(CONFIG_OF)         += of.o
>> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> >>>> new file mode 100644
>> >>>> index 000000000000..09b7c43c0ea4
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.c
>> >>>> @@ -0,0 +1,158 @@
>> >>>> +// SPDX-License-Identifier: GPL-2.0+
>> >>>> +/*
>> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> >>>> + * all PHYs on a HCD and to keep them all in the same state.
>> >>>> + *
>> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> + */
>> >>>> +
>> >>>> +#include <linux/device.h>
>> >>>> +#include <linux/list.h>
>> >>>> +#include <linux/phy/phy.h>
>> >>>> +#include <linux/of.h>
>> >>>> +
>> >>>> +#include "phy.h"
>> >>>> +
>> >>>> +struct usb_phy_roothub {
>> >>>> +     struct phy              *phy;
>> >>>> +     struct list_head        list;
>> >>>> +};
>> >>>> +
>> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> >>>> +     if (!roothub_entry)
>> >>>> +             return ERR_PTR(-ENOMEM);
>> >>>> +
>> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> >>>> +
>> >>>> +     return roothub_entry;
>> >>>> +}
>> >>>> +
>> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> >>>> +                                struct list_head *list)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> >>>> +
>> >>>> +     if (IS_ERR_OR_NULL(phy)) {
>> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> >>>> +                     return 0;
>> >>>> +             else
>> >>>> +                     return PTR_ERR(phy);
>> >>>> +     }
>> >>>> +
>> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(roothub_entry))
>> >>>> +             return PTR_ERR(roothub_entry);
>> >>>> +
>> >>>> +     roothub_entry->phy = phy;
>> >>>> +
>> >>>> +     list_add_tail(&roothub_entry->list, list);
>> >>>> +
>> >>>> +     return 0;
>> >>>> +}
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *phy_roothub;
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int i, num_phys, err;
>> >>>> +
>> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> >>>> +                                           "#phy-cells");
>> >>>> +     if (num_phys <= 0)
>> >>>> +             return NULL;
>> >>>> +
>> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(phy_roothub))
>> >>>> +             return phy_roothub;
>> >>>> +
>> >>>> +     for (i = 0; i < num_phys; i++) {
>> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_init(roothub_entry->phy);
>> >>>
>> >>> The phy_init() function actually enables the PHY clocks.
>> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>> >>
>> >
>> > Yes.
>> >
>> >> an earlier version of my patch did exactly this, but it caused
>> >> problems during a suspend/resume cycle on Mediatek devices
>> >> Chunfeng Yun reported that issue here [0], quote from that mail for
>> >> easier reading:
>> >> "In order to keep link state on mt8173, we just power off all phys(not
>> >> exit) when system enter suspend, then power on them again (needn't
>> >> init, otherwise device will be disconnected) when system resume, this
>> >> can avoid re-enumerating device."
>> >>
>> >>>> +             if (err)
>> >>>> +                     goto err_exit_phys;
>> >>>> +     }
>> >>>> +
>> >>>> +     return phy_roothub;
>> >>>> +
>> >>>> +err_exit_phys:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_exit(roothub_entry->phy);
>> >>>> +
>> >>>> +err_out:
>> >>>> +     return ERR_PTR(err);
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> >>>> +
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err, ret = 0;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_exit(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     ret = ret;
>> >>>> +     }
>> >>>
>> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> >> if I understood Chunfeng Yun correctly this will require
>> >> re-enumeration of the USB devices after a suspend/resume cycle on
>> >> Mediatek SoCs
>> >>
>> >
>> > OK. I suppose that there are 2 cases
>> > 1) Mediatek's case: USB controller context retained across suspend/resume.
>> > Remote wakeup probably required.
>> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> > during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> > phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>> >>>
>> >>>> +
>> >>>> +     return ret;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_power_on(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     return 0;
>> >>>> +
>> >>>> +err_out:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>> +
>> >>>> +     return err;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> >>>> +
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return;
>> >>>> +
>> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>
>> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> >>> we're no longer able to reach low power states on system suspend.
>> >> I'm not sure where this problem should be solved:
>> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> >> TI platforms
>> >
>> > Many TI platforms are affected, omap5*, dra7*, am43*
>> >
>> >> - fix this in the usb_phy_roothub code
>> >
>> > I'd vote for fixing it in the usb_phy_roothub code. How?
>> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> devices by device_init_wakeup(dev, true),but not dependent on
> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> decide whether call phy_exit()/init() or not.
the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
yet, so I cannot test this
based on this suggestion I threw up two patches which are *compile
tested only* based on Greg's usb-next branch
you can find them here: [0] (as well as attached to this mail)

@Chunfeng: can you please test this on one of your Mediatek SoCs?
@Roger: can you please test this on a TI SoC?

(apologies in advance if these patches don't work)

please note that I won't have access to my computer until Saturday.
if these patches need to be rewritten/replaced/etc. then feel free to
send your own version to the list

>> >> - fix this in the PHY driver
>> >
>> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>> >> - somewhere else
>> >>
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> >>>> new file mode 100644
>> >>>> index 000000000000..6fde59bfbff8
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.h
>> >>>> @@ -0,0 +1,7 @@
>> >>>> +struct usb_phy_roothub;
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>> >>>>
>> >>>
>> >
>> > <snip>
>> >
>> > --
>> > cheers,
>> > -roger
>> >
>> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>>
>>
>> Regards
>> Martin
>
>


Regards
Martin


[0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1

From 1367f49ff49cec249bd9ba95e5395dd1760eee8b Mon Sep 17 00:00:00 2001
From: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Date: Tue, 20 Mar 2018 22:47:38 +0100
Subject: [PATCH 2/2] usb: core: use phy_exit during suspend if wake up is not
 supported

If the USB controller can wake up the system (which is the case for
example with the Mediatek USB3 IP) then we must not call phy_exit during
suspend to ensure that the USB controller doesn't have to re-enumerate
the devices during resume.
However, if the USB controller cannot wake up the system (which is the
case for example on various TI platforms using a dwc3 controller) then
we must call phy_exit during suspend. Otherwise the PHY driver keeps the
clocks enabled, which prevents the system from entering the suspend
state.

Solve this by introducing two new functions in the PHY wrapper which are
dedicated to the suspend and resume handling.
If the controller can wake up the system the new usb_phy_roothub_suspend
function will simply call usb_phy_roothub_power_off. However, if wake up
is not supported by the controller it will also call
usb_phy_roothub_exit.
The also new usb_phy_roothub_resume function takes care of calling
usb_phy_roothub_init (if the controller can't wake up the system) in
addition to usb_phy_roothub_power_on.

Fixes: 07dbff0ddbd86c ("usb: core: add a wrapper for the USB PHYs on the HCD")
Fixes: 178a0bce05cbc1 ("usb: core: hcd: integrate the PHY wrapper into the HCD core")
Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c |  6 +++---
 drivers/usb/core/phy.c | 29 +++++++++++++++++++++++++++++
 drivers/usb/core/phy.h |  5 +++++
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 89b10d859047..6d4a4195ca00 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_power_off(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_power_on(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_power_off(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 50a80dbe776c..2eca371684fa 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -155,3 +155,32 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 		phy_power_off(roothub_entry->phy);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
+
+int usb_phy_roothub_suspend(struct device *dev,
+			    struct usb_phy_roothub *phy_roothub)
+{
+	usb_phy_roothub_power_off(phy_roothub);
+
+	/* keep the PHYs initialized so the device can wake up the system */
+	if (device_can_wakeup(dev))
+		return 0;
+
+	return usb_phy_roothub_exit(phy_roothub);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
+
+int usb_phy_roothub_resume(struct device *dev,
+			   struct usb_phy_roothub *phy_roothub)
+{
+	int err;
+
+	/* if the device can't wake up the system _exit was called */
+	if (device_can_wakeup(dev)) {
+		err = usb_phy_roothub_init(phy_roothub);
+		if (err)
+			return err;
+	}
+
+	return usb_phy_roothub_power_on(phy_roothub);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
index eb31253201ad..601aefd41878 100644
--- a/drivers/usb/core/phy.h
+++ b/drivers/usb/core/phy.h
@@ -7,3 +7,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
 
 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
 void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_suspend(struct device *dev,
+			    struct usb_phy_roothub *phy_roothub);
+int usb_phy_roothub_resume(struct device *dev,
+			   struct usb_phy_roothub *phy_roothub);
-- 
2.16.2


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 22:01                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 22:01 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Roger, Hi Chunfeng,

On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> Hi Martin & Roger:
>
> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> > Hi,
>> >
>> > On 19/03/18 00:29, Martin Blumenstingl wrote:
>> >> Hi Roger,
>> >>
>> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> >>> +some TI folks
>> >>>
>> >>> Hi Martin,
>> >>>
>> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> >>>> Many SoC platforms have separate devices for the USB PHY which are
>> >>>> registered through the generic PHY framework. These PHYs have to be
>> >>>> enabled to make the USB controller actually work. They also have to be
>> >>>> disabled again on shutdown/suspend.
>> >>>>
>> >>>> Currently (at least) the following HCI platform drivers are using custom
>> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
>> >>>> disable/enable them when required:
>> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
>> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
>> >>>>
>> >>>> With this new wrapper the USB PHYs can be specified directly in the
>> >>>> USB controller's devicetree node (just like on the drivers listed
>> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
>> >>>> controller and require all USB PHYs to be initialized (if one of the USB
>> >>>> PHYs it not initialized then none of USB port works at all).
>> >>>>
>> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>> >>>
>> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> >>> I'll explain why below.
>> >> based on your explanation and reading the TI PHY drivers I am assuming
>> >> that the affected SoCs are using the "phy-omap-usb2" driver
>> >>
>> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>> >>>> ---
>> >>>>  drivers/usb/core/Makefile |   2 +-
>> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>> >>>>  drivers/usb/core/phy.h    |   7 ++
>> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>> >>>>  create mode 100644 drivers/usb/core/phy.c
>> >>>>  create mode 100644 drivers/usb/core/phy.h
>> >>>>
>> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> >>>> index 92c9cefb4317..18e874b0441e 100644
>> >>>> --- a/drivers/usb/core/Makefile
>> >>>> +++ b/drivers/usb/core/Makefile
>> >>>> @@ -6,7 +6,7 @@
>> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> >>>> -usbcore-y += port.o
>> >>>> +usbcore-y += phy.o port.o
>> >>>>
>> >>>>  usbcore-$(CONFIG_OF)         += of.o
>> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> >>>> new file mode 100644
>> >>>> index 000000000000..09b7c43c0ea4
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.c
>> >>>> @@ -0,0 +1,158 @@
>> >>>> +// SPDX-License-Identifier: GPL-2.0+
>> >>>> +/*
>> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> >>>> + * all PHYs on a HCD and to keep them all in the same state.
>> >>>> + *
>> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> + */
>> >>>> +
>> >>>> +#include <linux/device.h>
>> >>>> +#include <linux/list.h>
>> >>>> +#include <linux/phy/phy.h>
>> >>>> +#include <linux/of.h>
>> >>>> +
>> >>>> +#include "phy.h"
>> >>>> +
>> >>>> +struct usb_phy_roothub {
>> >>>> +     struct phy              *phy;
>> >>>> +     struct list_head        list;
>> >>>> +};
>> >>>> +
>> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> >>>> +     if (!roothub_entry)
>> >>>> +             return ERR_PTR(-ENOMEM);
>> >>>> +
>> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> >>>> +
>> >>>> +     return roothub_entry;
>> >>>> +}
>> >>>> +
>> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> >>>> +                                struct list_head *list)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> >>>> +
>> >>>> +     if (IS_ERR_OR_NULL(phy)) {
>> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> >>>> +                     return 0;
>> >>>> +             else
>> >>>> +                     return PTR_ERR(phy);
>> >>>> +     }
>> >>>> +
>> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(roothub_entry))
>> >>>> +             return PTR_ERR(roothub_entry);
>> >>>> +
>> >>>> +     roothub_entry->phy = phy;
>> >>>> +
>> >>>> +     list_add_tail(&roothub_entry->list, list);
>> >>>> +
>> >>>> +     return 0;
>> >>>> +}
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *phy_roothub;
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int i, num_phys, err;
>> >>>> +
>> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> >>>> +                                           "#phy-cells");
>> >>>> +     if (num_phys <= 0)
>> >>>> +             return NULL;
>> >>>> +
>> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(phy_roothub))
>> >>>> +             return phy_roothub;
>> >>>> +
>> >>>> +     for (i = 0; i < num_phys; i++) {
>> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_init(roothub_entry->phy);
>> >>>
>> >>> The phy_init() function actually enables the PHY clocks.
>> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>> >>
>> >
>> > Yes.
>> >
>> >> an earlier version of my patch did exactly this, but it caused
>> >> problems during a suspend/resume cycle on Mediatek devices
>> >> Chunfeng Yun reported that issue here [0], quote from that mail for
>> >> easier reading:
>> >> "In order to keep link state on mt8173, we just power off all phys(not
>> >> exit) when system enter suspend, then power on them again (needn't
>> >> init, otherwise device will be disconnected) when system resume, this
>> >> can avoid re-enumerating device."
>> >>
>> >>>> +             if (err)
>> >>>> +                     goto err_exit_phys;
>> >>>> +     }
>> >>>> +
>> >>>> +     return phy_roothub;
>> >>>> +
>> >>>> +err_exit_phys:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_exit(roothub_entry->phy);
>> >>>> +
>> >>>> +err_out:
>> >>>> +     return ERR_PTR(err);
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> >>>> +
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err, ret = 0;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_exit(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     ret = ret;
>> >>>> +     }
>> >>>
>> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> >> if I understood Chunfeng Yun correctly this will require
>> >> re-enumeration of the USB devices after a suspend/resume cycle on
>> >> Mediatek SoCs
>> >>
>> >
>> > OK. I suppose that there are 2 cases
>> > 1) Mediatek's case: USB controller context retained across suspend/resume.
>> > Remote wakeup probably required.
>> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> > during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> > phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>> >>>
>> >>>> +
>> >>>> +     return ret;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_power_on(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     return 0;
>> >>>> +
>> >>>> +err_out:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>> +
>> >>>> +     return err;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> >>>> +
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return;
>> >>>> +
>> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>
>> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> >>> we're no longer able to reach low power states on system suspend.
>> >> I'm not sure where this problem should be solved:
>> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> >> TI platforms
>> >
>> > Many TI platforms are affected, omap5*, dra7*, am43*
>> >
>> >> - fix this in the usb_phy_roothub code
>> >
>> > I'd vote for fixing it in the usb_phy_roothub code. How?
>> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> devices by device_init_wakeup(dev, true),but not dependent on
> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> decide whether call phy_exit()/init() or not.
the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
yet, so I cannot test this
based on this suggestion I threw up two patches which are *compile
tested only* based on Greg's usb-next branch
you can find them here: [0] (as well as attached to this mail)

@Chunfeng: can you please test this on one of your Mediatek SoCs?
@Roger: can you please test this on a TI SoC?

(apologies in advance if these patches don't work)

please note that I won't have access to my computer until Saturday.
if these patches need to be rewritten/replaced/etc. then feel free to
send your own version to the list

>> >> - fix this in the PHY driver
>> >
>> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>> >> - somewhere else
>> >>
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> >>>> new file mode 100644
>> >>>> index 000000000000..6fde59bfbff8
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.h
>> >>>> @@ -0,0 +1,7 @@
>> >>>> +struct usb_phy_roothub;
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>> >>>>
>> >>>
>> >
>> > <snip>
>> >
>> > --
>> > cheers,
>> > -roger
>> >
>> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>>
>>
>> Regards
>> Martin
>
>


Regards
Martin


[0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-usb-core-split-usb_phy_roothub_-init-alloc.patch
Type: text/x-patch
Size: 5244 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180320/57365d90/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-usb-core-use-phy_exit-during-suspend-if-wake-up-is-n.patch
Type: text/x-patch
Size: 4460 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180320/57365d90/attachment-0003.bin>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 22:01                         ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-20 22:01 UTC (permalink / raw)
  To: linus-amlogic

Hi Roger, Hi Chunfeng,

On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> Hi Martin & Roger:
>
> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> > Hi,
>> >
>> > On 19/03/18 00:29, Martin Blumenstingl wrote:
>> >> Hi Roger,
>> >>
>> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> >>> +some TI folks
>> >>>
>> >>> Hi Martin,
>> >>>
>> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> >>>> Many SoC platforms have separate devices for the USB PHY which are
>> >>>> registered through the generic PHY framework. These PHYs have to be
>> >>>> enabled to make the USB controller actually work. They also have to be
>> >>>> disabled again on shutdown/suspend.
>> >>>>
>> >>>> Currently (at least) the following HCI platform drivers are using custom
>> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
>> >>>> disable/enable them when required:
>> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
>> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
>> >>>>
>> >>>> With this new wrapper the USB PHYs can be specified directly in the
>> >>>> USB controller's devicetree node (just like on the drivers listed
>> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
>> >>>> controller and require all USB PHYs to be initialized (if one of the USB
>> >>>> PHYs it not initialized then none of USB port works at all).
>> >>>>
>> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>> >>>
>> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> >>> I'll explain why below.
>> >> based on your explanation and reading the TI PHY drivers I am assuming
>> >> that the affected SoCs are using the "phy-omap-usb2" driver
>> >>
>> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>> >>>> ---
>> >>>>  drivers/usb/core/Makefile |   2 +-
>> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>> >>>>  drivers/usb/core/phy.h    |   7 ++
>> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>> >>>>  create mode 100644 drivers/usb/core/phy.c
>> >>>>  create mode 100644 drivers/usb/core/phy.h
>> >>>>
>> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> >>>> index 92c9cefb4317..18e874b0441e 100644
>> >>>> --- a/drivers/usb/core/Makefile
>> >>>> +++ b/drivers/usb/core/Makefile
>> >>>> @@ -6,7 +6,7 @@
>> >>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>> >>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>> >>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>> >>>> -usbcore-y += port.o
>> >>>> +usbcore-y += phy.o port.o
>> >>>>
>> >>>>  usbcore-$(CONFIG_OF)         += of.o
>> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> >>>> new file mode 100644
>> >>>> index 000000000000..09b7c43c0ea4
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.c
>> >>>> @@ -0,0 +1,158 @@
>> >>>> +// SPDX-License-Identifier: GPL-2.0+
>> >>>> +/*
>> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> >>>> + * all PHYs on a HCD and to keep them all in the same state.
>> >>>> + *
>> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> + */
>> >>>> +
>> >>>> +#include <linux/device.h>
>> >>>> +#include <linux/list.h>
>> >>>> +#include <linux/phy/phy.h>
>> >>>> +#include <linux/of.h>
>> >>>> +
>> >>>> +#include "phy.h"
>> >>>> +
>> >>>> +struct usb_phy_roothub {
>> >>>> +     struct phy              *phy;
>> >>>> +     struct list_head        list;
>> >>>> +};
>> >>>> +
>> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> >>>> +     if (!roothub_entry)
>> >>>> +             return ERR_PTR(-ENOMEM);
>> >>>> +
>> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> >>>> +
>> >>>> +     return roothub_entry;
>> >>>> +}
>> >>>> +
>> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> >>>> +                                struct list_head *list)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> >>>> +
>> >>>> +     if (IS_ERR_OR_NULL(phy)) {
>> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> >>>> +                     return 0;
>> >>>> +             else
>> >>>> +                     return PTR_ERR(phy);
>> >>>> +     }
>> >>>> +
>> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(roothub_entry))
>> >>>> +             return PTR_ERR(roothub_entry);
>> >>>> +
>> >>>> +     roothub_entry->phy = phy;
>> >>>> +
>> >>>> +     list_add_tail(&roothub_entry->list, list);
>> >>>> +
>> >>>> +     return 0;
>> >>>> +}
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *phy_roothub;
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int i, num_phys, err;
>> >>>> +
>> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> >>>> +                                           "#phy-cells");
>> >>>> +     if (num_phys <= 0)
>> >>>> +             return NULL;
>> >>>> +
>> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(phy_roothub))
>> >>>> +             return phy_roothub;
>> >>>> +
>> >>>> +     for (i = 0; i < num_phys; i++) {
>> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_init(roothub_entry->phy);
>> >>>
>> >>> The phy_init() function actually enables the PHY clocks.
>> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>> >>
>> >
>> > Yes.
>> >
>> >> an earlier version of my patch did exactly this, but it caused
>> >> problems during a suspend/resume cycle on Mediatek devices
>> >> Chunfeng Yun reported that issue here [0], quote from that mail for
>> >> easier reading:
>> >> "In order to keep link state on mt8173, we just power off all phys(not
>> >> exit) when system enter suspend, then power on them again (needn't
>> >> init, otherwise device will be disconnected) when system resume, this
>> >> can avoid re-enumerating device."
>> >>
>> >>>> +             if (err)
>> >>>> +                     goto err_exit_phys;
>> >>>> +     }
>> >>>> +
>> >>>> +     return phy_roothub;
>> >>>> +
>> >>>> +err_exit_phys:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_exit(roothub_entry->phy);
>> >>>> +
>> >>>> +err_out:
>> >>>> +     return ERR_PTR(err);
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> >>>> +
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err, ret = 0;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_exit(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     ret = ret;
>> >>>> +     }
>> >>>
>> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> >> if I understood Chunfeng Yun correctly this will require
>> >> re-enumeration of the USB devices after a suspend/resume cycle on
>> >> Mediatek SoCs
>> >>
>> >
>> > OK. I suppose that there are 2 cases
>> > 1) Mediatek's case: USB controller context retained across suspend/resume.
>> > Remote wakeup probably required.
>> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> > during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> > phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>> >>>
>> >>>> +
>> >>>> +     return ret;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_power_on(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     return 0;
>> >>>> +
>> >>>> +err_out:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>> +
>> >>>> +     return err;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> >>>> +
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return;
>> >>>> +
>> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>
>> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> >>> we're no longer able to reach low power states on system suspend.
>> >> I'm not sure where this problem should be solved:
>> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> >> TI platforms
>> >
>> > Many TI platforms are affected, omap5*, dra7*, am43*
>> >
>> >> - fix this in the usb_phy_roothub code
>> >
>> > I'd vote for fixing it in the usb_phy_roothub code. How?
>> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> devices by device_init_wakeup(dev, true),but not dependent on
> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> decide whether call phy_exit()/init() or not.
the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
yet, so I cannot test this
based on this suggestion I threw up two patches which are *compile
tested only* based on Greg's usb-next branch
you can find them here: [0] (as well as attached to this mail)

@Chunfeng: can you please test this on one of your Mediatek SoCs?
@Roger: can you please test this on a TI SoC?

(apologies in advance if these patches don't work)

please note that I won't have access to my computer until Saturday.
if these patches need to be rewritten/replaced/etc. then feel free to
send your own version to the list

>> >> - fix this in the PHY driver
>> >
>> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>> >> - somewhere else
>> >>
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> >>>> new file mode 100644
>> >>>> index 000000000000..6fde59bfbff8
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.h
>> >>>> @@ -0,0 +1,7 @@
>> >>>> +struct usb_phy_roothub;
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>> >>>>
>> >>>
>> >
>> > <snip>
>> >
>> > --
>> > cheers,
>> > -roger
>> >
>> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>>
>>
>> Regards
>> Martin
>
>


Regards
Martin


[0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-usb-core-split-usb_phy_roothub_-init-alloc.patch
Type: text/x-patch
Size: 5244 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20180320/57365d90/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-usb-core-use-phy_exit-during-suspend-if-wake-up-is-n.patch
Type: text/x-patch
Size: 4460 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20180320/57365d90/attachment-0001.bin>

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 23:47                           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 23:47 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: mark.rutland, Peter.Chen, Neil Armstrong, linux-tegra,
	thierry.reding, Kishon Vijay Abraham I, mathias.nyman, jonathanh,
	stern, devicetree, felipe.balbi, yixun.lan, robh+dt,
	linux-mediatek, matthias.bgg, linux-amlogic, linux-omap,
	linux-arm-kernel, Roger Quadros, Gerlach, Dave, gregkh,
	linux-usb, linux, arnd, Keerthy

Hi Martin,

On Tue, 2018-03-20 at 22:57 +0100, Martin Blumenstingl wrote:
> Hello Kishon,
> 
> On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> > Hi,
> >
> > On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>> Hi,
> >>>
> >>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>> Hi Roger,
> >>>>
> >>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>> +some TI folks
> >>>>>
> >>>>> Hi Martin,
> >>>>>
> >>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>> disabled again on shutdown/suspend.
> >>>>>>
> >>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>> disable/enable them when required:
> >>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>
> >>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>
> >>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>
> >>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>> I'll explain why below.
> >>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>
> >>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >> I missed that, thanks
> >>
> >>>>>> ---
> >>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>
> >>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>> --- a/drivers/usb/core/Makefile
> >>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>> @@ -6,7 +6,7 @@
> >>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>> -usbcore-y += port.o
> >>>>>> +usbcore-y += phy.o port.o
> >>>>>>
> >>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>> new file mode 100644
> >>>>>> index 000000000000..09b7c43c0ea4
> >>>>>> --- /dev/null
> >>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>> @@ -0,0 +1,158 @@
> >>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>> +/*
> >>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>> + *
> >>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> + */
> >>>>>> +
> >>>>>> +#include <linux/device.h>
> >>>>>> +#include <linux/list.h>
> >>>>>> +#include <linux/phy/phy.h>
> >>>>>> +#include <linux/of.h>
> >>>>>> +
> >>>>>> +#include "phy.h"
> >>>>>> +
> >>>>>> +struct usb_phy_roothub {
> >>>>>> +     struct phy              *phy;
> >>>>>> +     struct list_head        list;
> >>>>>> +};
> >>>>>> +
> >>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>> +     if (!roothub_entry)
> >>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>> +
> >>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>> +
> >>>>>> +     return roothub_entry;
> >>>>>> +}
> >>>>>> +
> >>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>> +                                struct list_head *list)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>> +
> >>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>> +                     return 0;
> >>>>>> +             else
> >>>>>> +                     return PTR_ERR(phy);
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>> +
> >>>>>> +     roothub_entry->phy = phy;
> >>>>>> +
> >>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +}
> >>>>>> +
> >>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int i, num_phys, err;
> >>>>>> +
> >>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>> +                                           "#phy-cells");
> >>>>>> +     if (num_phys <= 0)
> >>>>>> +             return NULL;
> >>>>>> +
> >>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>> +             return phy_roothub;
> >>>>>> +
> >>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>
> >>>>> The phy_init() function actually enables the PHY clocks.
> >>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>
> >>>
> >>> Yes.
> >>>
> >>>> an earlier version of my patch did exactly this, but it caused
> >>>> problems during a suspend/resume cycle on Mediatek devices
> >>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>> easier reading:
> >>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>> exit) when system enter suspend, then power on them again (needn't
> >>>> init, otherwise device will be disconnected) when system resume, this
> >>>> can avoid re-enumerating device."
> >>>>
> >>>>>> +             if (err)
> >>>>>> +                     goto err_exit_phys;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return phy_roothub;
> >>>>>> +
> >>>>>> +err_exit_phys:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     return ERR_PTR(err);
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err, ret = 0;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     ret = ret;
> >>>>>> +     }
> >>>>>
> >>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>> if I understood Chunfeng Yun correctly this will require
> >>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>> Mediatek SoCs
> >>>>
> >>>
> >>> OK. I suppose that there are 2 cases
> >>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>> Remote wakeup probably required.
> >>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>> during suspend/resume to keep PHY link active.
> >> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >> that the parent of the USB controller can be marked as "wakeup-source"
> >>
> >>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>> phy_exit()/phy_init() must be called during suspend/resume.
> >> ACK
> >>
> >>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>
> >>>>>> +
> >>>>>> +     return ret;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>> +
> >>>>>> +     return err;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>> +
> >>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return;
> >>>>>> +
> >>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>
> >>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>> we're no longer able to reach low power states on system suspend.
> >>>> I'm not sure where this problem should be solved:
> >>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>> TI platforms
> >>>
> >>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>
> >>>> - fix this in the usb_phy_roothub code
> >>>
> >>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >> the controller is *NOT* marked as "wakeup-source"?
> >> I am also not sure if it would work, since the "wakeup-source"
> >> property is defined on the USB controller's parent node in case of the
> >> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>
> >>>> - fix this in the PHY driver
> >>>
> >>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >> I actually wonder if phy_ops should have explicit suspend/resume support:
> >
> > Not in favor of adding explicit suspend/resume ops since PM framework already
> > has those. I think we should let PHY drivers manage suspend/resume on its own
> > (after creating the dependency between the controller device and PHY using
> > device_link_add).
> even better if we can re-use some existing code!
> 
> the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
> support suspend/resume yet, so unfortunately I cannot test this.
> besides that I have zero experience with suspend/resume logic.
> 
> I'll try to read more about that topic, but I definitely need someone
> who could help testing if I have a patch ready
> in any case: I think implementing this should not be done for v4.17
> anymore - maybe we can find a small solution for v4.17 and switch to
> your proposed solution later (assuming nobody has arguments against
> that)
> 
I'll test it.

Thanks
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 23:47                           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 23:47 UTC (permalink / raw)
  To: Martin Blumenstingl
  Cc: Kishon Vijay Abraham I, Roger Quadros, linux-usb, mathias.nyman,
	arnd, gregkh, felipe.balbi, linux-omap, linux-tegra,
	linux-mediatek, linux-arm-kernel, devicetree, jonathanh,
	thierry.reding, stern, linux, Peter.Chen, matthias.bgg,
	mark.rutland, robh+dt, Neil Armstrong, linux-amlogic, yixun.lan,
	Keerthy, Gerlach, Dave

Hi Martin,

On Tue, 2018-03-20 at 22:57 +0100, Martin Blumenstingl wrote:
> Hello Kishon,
> 
> On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> > Hi,
> >
> > On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>> Hi,
> >>>
> >>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>> Hi Roger,
> >>>>
> >>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>> +some TI folks
> >>>>>
> >>>>> Hi Martin,
> >>>>>
> >>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>> disabled again on shutdown/suspend.
> >>>>>>
> >>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>> disable/enable them when required:
> >>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>
> >>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>
> >>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>
> >>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>> I'll explain why below.
> >>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>
> >>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >> I missed that, thanks
> >>
> >>>>>> ---
> >>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>
> >>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>> --- a/drivers/usb/core/Makefile
> >>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>> @@ -6,7 +6,7 @@
> >>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>> -usbcore-y += port.o
> >>>>>> +usbcore-y += phy.o port.o
> >>>>>>
> >>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>> new file mode 100644
> >>>>>> index 000000000000..09b7c43c0ea4
> >>>>>> --- /dev/null
> >>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>> @@ -0,0 +1,158 @@
> >>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>> +/*
> >>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>> + *
> >>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> + */
> >>>>>> +
> >>>>>> +#include <linux/device.h>
> >>>>>> +#include <linux/list.h>
> >>>>>> +#include <linux/phy/phy.h>
> >>>>>> +#include <linux/of.h>
> >>>>>> +
> >>>>>> +#include "phy.h"
> >>>>>> +
> >>>>>> +struct usb_phy_roothub {
> >>>>>> +     struct phy              *phy;
> >>>>>> +     struct list_head        list;
> >>>>>> +};
> >>>>>> +
> >>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>> +     if (!roothub_entry)
> >>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>> +
> >>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>> +
> >>>>>> +     return roothub_entry;
> >>>>>> +}
> >>>>>> +
> >>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>> +                                struct list_head *list)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>> +
> >>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>> +                     return 0;
> >>>>>> +             else
> >>>>>> +                     return PTR_ERR(phy);
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>> +
> >>>>>> +     roothub_entry->phy = phy;
> >>>>>> +
> >>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +}
> >>>>>> +
> >>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int i, num_phys, err;
> >>>>>> +
> >>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>> +                                           "#phy-cells");
> >>>>>> +     if (num_phys <= 0)
> >>>>>> +             return NULL;
> >>>>>> +
> >>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>> +             return phy_roothub;
> >>>>>> +
> >>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>
> >>>>> The phy_init() function actually enables the PHY clocks.
> >>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>
> >>>
> >>> Yes.
> >>>
> >>>> an earlier version of my patch did exactly this, but it caused
> >>>> problems during a suspend/resume cycle on Mediatek devices
> >>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>> easier reading:
> >>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>> exit) when system enter suspend, then power on them again (needn't
> >>>> init, otherwise device will be disconnected) when system resume, this
> >>>> can avoid re-enumerating device."
> >>>>
> >>>>>> +             if (err)
> >>>>>> +                     goto err_exit_phys;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return phy_roothub;
> >>>>>> +
> >>>>>> +err_exit_phys:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     return ERR_PTR(err);
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err, ret = 0;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     ret = ret;
> >>>>>> +     }
> >>>>>
> >>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>> if I understood Chunfeng Yun correctly this will require
> >>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>> Mediatek SoCs
> >>>>
> >>>
> >>> OK. I suppose that there are 2 cases
> >>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>> Remote wakeup probably required.
> >>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>> during suspend/resume to keep PHY link active.
> >> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >> that the parent of the USB controller can be marked as "wakeup-source"
> >>
> >>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>> phy_exit()/phy_init() must be called during suspend/resume.
> >> ACK
> >>
> >>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>
> >>>>>> +
> >>>>>> +     return ret;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>> +
> >>>>>> +     return err;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>> +
> >>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return;
> >>>>>> +
> >>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>
> >>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>> we're no longer able to reach low power states on system suspend.
> >>>> I'm not sure where this problem should be solved:
> >>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>> TI platforms
> >>>
> >>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>
> >>>> - fix this in the usb_phy_roothub code
> >>>
> >>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >> the controller is *NOT* marked as "wakeup-source"?
> >> I am also not sure if it would work, since the "wakeup-source"
> >> property is defined on the USB controller's parent node in case of the
> >> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>
> >>>> - fix this in the PHY driver
> >>>
> >>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >> I actually wonder if phy_ops should have explicit suspend/resume support:
> >
> > Not in favor of adding explicit suspend/resume ops since PM framework already
> > has those. I think we should let PHY drivers manage suspend/resume on its own
> > (after creating the dependency between the controller device and PHY using
> > device_link_add).
> even better if we can re-use some existing code!
> 
> the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
> support suspend/resume yet, so unfortunately I cannot test this.
> besides that I have zero experience with suspend/resume logic.
> 
> I'll try to read more about that topic, but I definitely need someone
> who could help testing if I have a patch ready
> in any case: I think implementing this should not be done for v4.17
> anymore - maybe we can find a small solution for v4.17 and switch to
> your proposed solution later (assuming nobody has arguments against
> that)
> 
I'll test it.

Thanks
> 
> Regards
> Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 23:47                           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 23:47 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Martin,

On Tue, 2018-03-20 at 22:57 +0100, Martin Blumenstingl wrote:
> Hello Kishon,
> 
> On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> > Hi,
> >
> > On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>> Hi,
> >>>
> >>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>> Hi Roger,
> >>>>
> >>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>> +some TI folks
> >>>>>
> >>>>> Hi Martin,
> >>>>>
> >>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>> disabled again on shutdown/suspend.
> >>>>>>
> >>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>> disable/enable them when required:
> >>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>
> >>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>
> >>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>
> >>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>> I'll explain why below.
> >>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>
> >>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >> I missed that, thanks
> >>
> >>>>>> ---
> >>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>
> >>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>> --- a/drivers/usb/core/Makefile
> >>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>> @@ -6,7 +6,7 @@
> >>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>> -usbcore-y += port.o
> >>>>>> +usbcore-y += phy.o port.o
> >>>>>>
> >>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>> new file mode 100644
> >>>>>> index 000000000000..09b7c43c0ea4
> >>>>>> --- /dev/null
> >>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>> @@ -0,0 +1,158 @@
> >>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>> +/*
> >>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>> + *
> >>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> + */
> >>>>>> +
> >>>>>> +#include <linux/device.h>
> >>>>>> +#include <linux/list.h>
> >>>>>> +#include <linux/phy/phy.h>
> >>>>>> +#include <linux/of.h>
> >>>>>> +
> >>>>>> +#include "phy.h"
> >>>>>> +
> >>>>>> +struct usb_phy_roothub {
> >>>>>> +     struct phy              *phy;
> >>>>>> +     struct list_head        list;
> >>>>>> +};
> >>>>>> +
> >>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>> +     if (!roothub_entry)
> >>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>> +
> >>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>> +
> >>>>>> +     return roothub_entry;
> >>>>>> +}
> >>>>>> +
> >>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>> +                                struct list_head *list)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>> +
> >>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>> +                     return 0;
> >>>>>> +             else
> >>>>>> +                     return PTR_ERR(phy);
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>> +
> >>>>>> +     roothub_entry->phy = phy;
> >>>>>> +
> >>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +}
> >>>>>> +
> >>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int i, num_phys, err;
> >>>>>> +
> >>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>> +                                           "#phy-cells");
> >>>>>> +     if (num_phys <= 0)
> >>>>>> +             return NULL;
> >>>>>> +
> >>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>> +             return phy_roothub;
> >>>>>> +
> >>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>
> >>>>> The phy_init() function actually enables the PHY clocks.
> >>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>
> >>>
> >>> Yes.
> >>>
> >>>> an earlier version of my patch did exactly this, but it caused
> >>>> problems during a suspend/resume cycle on Mediatek devices
> >>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>> easier reading:
> >>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>> exit) when system enter suspend, then power on them again (needn't
> >>>> init, otherwise device will be disconnected) when system resume, this
> >>>> can avoid re-enumerating device."
> >>>>
> >>>>>> +             if (err)
> >>>>>> +                     goto err_exit_phys;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return phy_roothub;
> >>>>>> +
> >>>>>> +err_exit_phys:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     return ERR_PTR(err);
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err, ret = 0;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     ret = ret;
> >>>>>> +     }
> >>>>>
> >>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>> if I understood Chunfeng Yun correctly this will require
> >>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>> Mediatek SoCs
> >>>>
> >>>
> >>> OK. I suppose that there are 2 cases
> >>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>> Remote wakeup probably required.
> >>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>> during suspend/resume to keep PHY link active.
> >> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >> that the parent of the USB controller can be marked as "wakeup-source"
> >>
> >>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>> phy_exit()/phy_init() must be called during suspend/resume.
> >> ACK
> >>
> >>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>
> >>>>>> +
> >>>>>> +     return ret;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>> +
> >>>>>> +     return err;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>> +
> >>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return;
> >>>>>> +
> >>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>
> >>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>> we're no longer able to reach low power states on system suspend.
> >>>> I'm not sure where this problem should be solved:
> >>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>> TI platforms
> >>>
> >>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>
> >>>> - fix this in the usb_phy_roothub code
> >>>
> >>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >> the controller is *NOT* marked as "wakeup-source"?
> >> I am also not sure if it would work, since the "wakeup-source"
> >> property is defined on the USB controller's parent node in case of the
> >> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>
> >>>> - fix this in the PHY driver
> >>>
> >>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >> I actually wonder if phy_ops should have explicit suspend/resume support:
> >
> > Not in favor of adding explicit suspend/resume ops since PM framework already
> > has those. I think we should let PHY drivers manage suspend/resume on its own
> > (after creating the dependency between the controller device and PHY using
> > device_link_add).
> even better if we can re-use some existing code!
> 
> the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
> support suspend/resume yet, so unfortunately I cannot test this.
> besides that I have zero experience with suspend/resume logic.
> 
> I'll try to read more about that topic, but I definitely need someone
> who could help testing if I have a patch ready
> in any case: I think implementing this should not be done for v4.17
> anymore - maybe we can find a small solution for v4.17 and switch to
> your proposed solution later (assuming nobody has arguments against
> that)
> 
I'll test it.

Thanks
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-20 23:47                           ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-20 23:47 UTC (permalink / raw)
  To: linus-amlogic

Hi Martin,

On Tue, 2018-03-20 at 22:57 +0100, Martin Blumenstingl wrote:
> Hello Kishon,
> 
> On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> > Hi,
> >
> > On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>> Hi,
> >>>
> >>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>> Hi Roger,
> >>>>
> >>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>> +some TI folks
> >>>>>
> >>>>> Hi Martin,
> >>>>>
> >>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>> disabled again on shutdown/suspend.
> >>>>>>
> >>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>> disable/enable them when required:
> >>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>
> >>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>
> >>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>
> >>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>> I'll explain why below.
> >>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>
> >>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >> I missed that, thanks
> >>
> >>>>>> ---
> >>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>
> >>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>> --- a/drivers/usb/core/Makefile
> >>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>> @@ -6,7 +6,7 @@
> >>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>> -usbcore-y += port.o
> >>>>>> +usbcore-y += phy.o port.o
> >>>>>>
> >>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>> new file mode 100644
> >>>>>> index 000000000000..09b7c43c0ea4
> >>>>>> --- /dev/null
> >>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>> @@ -0,0 +1,158 @@
> >>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>> +/*
> >>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>> + *
> >>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> + */
> >>>>>> +
> >>>>>> +#include <linux/device.h>
> >>>>>> +#include <linux/list.h>
> >>>>>> +#include <linux/phy/phy.h>
> >>>>>> +#include <linux/of.h>
> >>>>>> +
> >>>>>> +#include "phy.h"
> >>>>>> +
> >>>>>> +struct usb_phy_roothub {
> >>>>>> +     struct phy              *phy;
> >>>>>> +     struct list_head        list;
> >>>>>> +};
> >>>>>> +
> >>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>> +     if (!roothub_entry)
> >>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>> +
> >>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>> +
> >>>>>> +     return roothub_entry;
> >>>>>> +}
> >>>>>> +
> >>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>> +                                struct list_head *list)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>> +
> >>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>> +                     return 0;
> >>>>>> +             else
> >>>>>> +                     return PTR_ERR(phy);
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>> +
> >>>>>> +     roothub_entry->phy = phy;
> >>>>>> +
> >>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +}
> >>>>>> +
> >>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int i, num_phys, err;
> >>>>>> +
> >>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>> +                                           "#phy-cells");
> >>>>>> +     if (num_phys <= 0)
> >>>>>> +             return NULL;
> >>>>>> +
> >>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>> +             return phy_roothub;
> >>>>>> +
> >>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>
> >>>>> The phy_init() function actually enables the PHY clocks.
> >>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>
> >>>
> >>> Yes.
> >>>
> >>>> an earlier version of my patch did exactly this, but it caused
> >>>> problems during a suspend/resume cycle on Mediatek devices
> >>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>> easier reading:
> >>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>> exit) when system enter suspend, then power on them again (needn't
> >>>> init, otherwise device will be disconnected) when system resume, this
> >>>> can avoid re-enumerating device."
> >>>>
> >>>>>> +             if (err)
> >>>>>> +                     goto err_exit_phys;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return phy_roothub;
> >>>>>> +
> >>>>>> +err_exit_phys:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     return ERR_PTR(err);
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err, ret = 0;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     ret = ret;
> >>>>>> +     }
> >>>>>
> >>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>> if I understood Chunfeng Yun correctly this will require
> >>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>> Mediatek SoCs
> >>>>
> >>>
> >>> OK. I suppose that there are 2 cases
> >>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>> Remote wakeup probably required.
> >>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>> during suspend/resume to keep PHY link active.
> >> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >> that the parent of the USB controller can be marked as "wakeup-source"
> >>
> >>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>> phy_exit()/phy_init() must be called during suspend/resume.
> >> ACK
> >>
> >>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>
> >>>>>> +
> >>>>>> +     return ret;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>> +
> >>>>>> +     return err;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>> +
> >>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return;
> >>>>>> +
> >>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>
> >>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>> we're no longer able to reach low power states on system suspend.
> >>>> I'm not sure where this problem should be solved:
> >>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>> TI platforms
> >>>
> >>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>
> >>>> - fix this in the usb_phy_roothub code
> >>>
> >>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >> the controller is *NOT* marked as "wakeup-source"?
> >> I am also not sure if it would work, since the "wakeup-source"
> >> property is defined on the USB controller's parent node in case of the
> >> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>
> >>>> - fix this in the PHY driver
> >>>
> >>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >> I actually wonder if phy_ops should have explicit suspend/resume support:
> >
> > Not in favor of adding explicit suspend/resume ops since PM framework already
> > has those. I think we should let PHY drivers manage suspend/resume on its own
> > (after creating the dependency between the controller device and PHY using
> > device_link_add).
> even better if we can re-use some existing code!
> 
> the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
> support suspend/resume yet, so unfortunately I cannot test this.
> besides that I have zero experience with suspend/resume logic.
> 
> I'll try to read more about that topic, but I definitely need someone
> who could help testing if I have a patch ready
> in any case: I think implementing this should not be done for v4.17
> anymore - maybe we can find a small solution for v4.17 and switch to
> your proposed solution later (assuming nobody has arguments against
> that)
> 
I'll test it.

Thanks
> 
> Regards
> Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-21 11:30                             ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-21 11:30 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	Keerthy, linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r

Martin,

On 21/03/18 00:01, Martin Blumenstingl wrote:
> Hi Roger, Hi Chunfeng,
> 
> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
>> Hi Martin & Roger:
>>
>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>> Hi,
>>>>
>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>>> +some TI folks
>>>>>>
>>>>>> Hi Martin,
>>>>>>
>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>> disabled again on shutdown/suspend.
>>>>>>>
>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>> disable/enable them when required:
>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>
>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>
>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>>>
>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>> I'll explain why below.
>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>
>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>> I missed that, thanks
>>>
>>>>>>> ---
>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>
>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>> -usbcore-y += port.o
>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>
>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>> @@ -0,0 +1,158 @@
>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>> +/*
>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>> + *
>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>> + */
>>>>>>> +
>>>>>>> +#include <linux/device.h>
>>>>>>> +#include <linux/list.h>
>>>>>>> +#include <linux/phy/phy.h>
>>>>>>> +#include <linux/of.h>
>>>>>>> +
>>>>>>> +#include "phy.h"
>>>>>>> +
>>>>>>> +struct usb_phy_roothub {
>>>>>>> +     struct phy              *phy;
>>>>>>> +     struct list_head        list;
>>>>>>> +};
>>>>>>> +
>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>> +     if (!roothub_entry)
>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>> +
>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>> +
>>>>>>> +     return roothub_entry;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>> +                                struct list_head *list)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>> +
>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>> +                     return 0;
>>>>>>> +             else
>>>>>>> +                     return PTR_ERR(phy);
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>> +
>>>>>>> +     roothub_entry->phy = phy;
>>>>>>> +
>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int i, num_phys, err;
>>>>>>> +
>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>> +                                           "#phy-cells");
>>>>>>> +     if (num_phys <= 0)
>>>>>>> +             return NULL;
>>>>>>> +
>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>> +             return phy_roothub;
>>>>>>> +
>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>
>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>
>>>>
>>>> Yes.
>>>>
>>>>> an earlier version of my patch did exactly this, but it caused
>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>> easier reading:
>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>> can avoid re-enumerating device."
>>>>>
>>>>>>> +             if (err)
>>>>>>> +                     goto err_exit_phys;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return phy_roothub;
>>>>>>> +
>>>>>>> +err_exit_phys:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     return ERR_PTR(err);
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err, ret = 0;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     ret = ret;
>>>>>>> +     }
>>>>>>
>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>> if I understood Chunfeng Yun correctly this will require
>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>> Mediatek SoCs
>>>>>
>>>>
>>>> OK. I suppose that there are 2 cases
>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>> Remote wakeup probably required.
>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>> during suspend/resume to keep PHY link active.
>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>
>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>> ACK
>>>
>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>
>>>>>>> +
>>>>>>> +     return ret;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>> +
>>>>>>> +     return err;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>> +
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return;
>>>>>>> +
>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>
>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>> we're no longer able to reach low power states on system suspend.
>>>>> I'm not sure where this problem should be solved:
>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>> TI platforms
>>>>
>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>
>>>>> - fix this in the usb_phy_roothub code
>>>>
>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>> the controller is *NOT* marked as "wakeup-source"?
>>> I am also not sure if it would work, since the "wakeup-source"
>>> property is defined on the USB controller's parent node in case of the
>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>
>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>> devices by device_init_wakeup(dev, true),but not dependent on
>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>> decide whether call phy_exit()/init() or not.
> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> yet, so I cannot test this
> based on this suggestion I threw up two patches which are *compile
> tested only* based on Greg's usb-next branch
> you can find them here: [0] (as well as attached to this mail)
> 
> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> @Roger: can you please test this on a TI SoC?
> 
> (apologies in advance if these patches don't work)
> 
> please note that I won't have access to my computer until Saturday.
> if these patches need to be rewritten/replaced/etc. then feel free to
> send your own version to the list

Had to do the following to build

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..2884607 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)


And the following to fix runtime issues

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..8598906 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);


Here are my obervations
- if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
low power states.

- if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
and so phy_power_off won't be called either.

This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.

This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
What does /sys/bus/usb/device/usb<?>/power/wakeup say?

Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
Chunfeng, why did you need this patch?

[1] - https://patchwork.kernel.org/patch/10298641/

What options do we have now to fix the original issue?

Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
of doing phy_power_off() as well as phy_exit() during suspend.

Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
flag in hcd to use it?

> 
>>>>> - fix this in the PHY driver
>>>>
>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>> - assuming we define two new callbacks: .suspend and .resume
>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>> - the PHY framework could call .power_on by default if .resume is not defined
>>> - drivers could set .suspend and .resume on their own, allowing more
>>> fine-grained control by for example *only* stopping the clock (but not
>>> re-initializing the registers, etc.)
>>>
>>> @Roger: what do you think about this?
>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>> hear his opinion too
>>>
>>>>> - somewhere else
>>>>>
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>> @@ -0,0 +1,7 @@
>>>>>>> +struct usb_phy_roothub;
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>
>>>>>>
>>>>

> 
> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-21 11:30                             ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-21 11:30 UTC (permalink / raw)
  To: Martin Blumenstingl, Chunfeng Yun
  Cc: linux-usb, mathias.nyman, arnd, gregkh, felipe.balbi, linux-omap,
	linux-tegra, linux-mediatek, linux-arm-kernel, devicetree,
	jonathanh, thierry.reding, stern, linux, Peter.Chen,
	matthias.bgg, mark.rutland, robh+dt, Neil Armstrong,
	linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Martin,

On 21/03/18 00:01, Martin Blumenstingl wrote:
> Hi Roger, Hi Chunfeng,
> 
> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>> Hi Martin & Roger:
>>
>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>> Hi,
>>>>
>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> +some TI folks
>>>>>>
>>>>>> Hi Martin,
>>>>>>
>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>> disabled again on shutdown/suspend.
>>>>>>>
>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>> disable/enable them when required:
>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>
>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>
>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>
>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>> I'll explain why below.
>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>
>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>> I missed that, thanks
>>>
>>>>>>> ---
>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>
>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>> -usbcore-y += port.o
>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>
>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>> @@ -0,0 +1,158 @@
>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>> +/*
>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>> + *
>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> + */
>>>>>>> +
>>>>>>> +#include <linux/device.h>
>>>>>>> +#include <linux/list.h>
>>>>>>> +#include <linux/phy/phy.h>
>>>>>>> +#include <linux/of.h>
>>>>>>> +
>>>>>>> +#include "phy.h"
>>>>>>> +
>>>>>>> +struct usb_phy_roothub {
>>>>>>> +     struct phy              *phy;
>>>>>>> +     struct list_head        list;
>>>>>>> +};
>>>>>>> +
>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>> +     if (!roothub_entry)
>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>> +
>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>> +
>>>>>>> +     return roothub_entry;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>> +                                struct list_head *list)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>> +
>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>> +                     return 0;
>>>>>>> +             else
>>>>>>> +                     return PTR_ERR(phy);
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>> +
>>>>>>> +     roothub_entry->phy = phy;
>>>>>>> +
>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int i, num_phys, err;
>>>>>>> +
>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>> +                                           "#phy-cells");
>>>>>>> +     if (num_phys <= 0)
>>>>>>> +             return NULL;
>>>>>>> +
>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>> +             return phy_roothub;
>>>>>>> +
>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>
>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>
>>>>
>>>> Yes.
>>>>
>>>>> an earlier version of my patch did exactly this, but it caused
>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>> easier reading:
>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>> can avoid re-enumerating device."
>>>>>
>>>>>>> +             if (err)
>>>>>>> +                     goto err_exit_phys;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return phy_roothub;
>>>>>>> +
>>>>>>> +err_exit_phys:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     return ERR_PTR(err);
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err, ret = 0;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     ret = ret;
>>>>>>> +     }
>>>>>>
>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>> if I understood Chunfeng Yun correctly this will require
>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>> Mediatek SoCs
>>>>>
>>>>
>>>> OK. I suppose that there are 2 cases
>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>> Remote wakeup probably required.
>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>> during suspend/resume to keep PHY link active.
>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>
>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>> ACK
>>>
>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>
>>>>>>> +
>>>>>>> +     return ret;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>> +
>>>>>>> +     return err;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>> +
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return;
>>>>>>> +
>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>
>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>> we're no longer able to reach low power states on system suspend.
>>>>> I'm not sure where this problem should be solved:
>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>> TI platforms
>>>>
>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>
>>>>> - fix this in the usb_phy_roothub code
>>>>
>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>> the controller is *NOT* marked as "wakeup-source"?
>>> I am also not sure if it would work, since the "wakeup-source"
>>> property is defined on the USB controller's parent node in case of the
>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>
>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>> devices by device_init_wakeup(dev, true),but not dependent on
>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>> decide whether call phy_exit()/init() or not.
> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> yet, so I cannot test this
> based on this suggestion I threw up two patches which are *compile
> tested only* based on Greg's usb-next branch
> you can find them here: [0] (as well as attached to this mail)
> 
> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> @Roger: can you please test this on a TI SoC?
> 
> (apologies in advance if these patches don't work)
> 
> please note that I won't have access to my computer until Saturday.
> if these patches need to be rewritten/replaced/etc. then feel free to
> send your own version to the list

Had to do the following to build



Here are my obervations
- if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
low power states.

- if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
and so phy_power_off won't be called either.

This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.

This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
What does /sys/bus/usb/device/usb<?>/power/wakeup say?

Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
Chunfeng, why did you need this patch?

[1] - https://patchwork.kernel.org/patch/10298641/

What options do we have now to fix the original issue?

Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
of doing phy_power_off() as well as phy_exit() during suspend.

Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
flag in hcd to use it?

> 
>>>>> - fix this in the PHY driver
>>>>
>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>> - assuming we define two new callbacks: .suspend and .resume
>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>> - the PHY framework could call .power_on by default if .resume is not defined
>>> - drivers could set .suspend and .resume on their own, allowing more
>>> fine-grained control by for example *only* stopping the clock (but not
>>> re-initializing the registers, etc.)
>>>
>>> @Roger: what do you think about this?
>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>> hear his opinion too
>>>
>>>>> - somewhere else
>>>>>
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>> @@ -0,0 +1,7 @@
>>>>>>> +struct usb_phy_roothub;
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>
>>>>>>
>>>>

> 
> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..2884607 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)


And the following to fix runtime issues

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..8598906 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-21 11:30                             ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-21 11:30 UTC (permalink / raw)
  To: linux-arm-kernel

Martin,

On 21/03/18 00:01, Martin Blumenstingl wrote:
> Hi Roger, Hi Chunfeng,
> 
> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>> Hi Martin & Roger:
>>
>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>> Hi,
>>>>
>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> +some TI folks
>>>>>>
>>>>>> Hi Martin,
>>>>>>
>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>> disabled again on shutdown/suspend.
>>>>>>>
>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>> disable/enable them when required:
>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>
>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>
>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>
>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>> I'll explain why below.
>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>
>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>> I missed that, thanks
>>>
>>>>>>> ---
>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>
>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>> -usbcore-y += port.o
>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>
>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>> @@ -0,0 +1,158 @@
>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>> +/*
>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>> + *
>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> + */
>>>>>>> +
>>>>>>> +#include <linux/device.h>
>>>>>>> +#include <linux/list.h>
>>>>>>> +#include <linux/phy/phy.h>
>>>>>>> +#include <linux/of.h>
>>>>>>> +
>>>>>>> +#include "phy.h"
>>>>>>> +
>>>>>>> +struct usb_phy_roothub {
>>>>>>> +     struct phy              *phy;
>>>>>>> +     struct list_head        list;
>>>>>>> +};
>>>>>>> +
>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>> +     if (!roothub_entry)
>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>> +
>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>> +
>>>>>>> +     return roothub_entry;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>> +                                struct list_head *list)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>> +
>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>> +                     return 0;
>>>>>>> +             else
>>>>>>> +                     return PTR_ERR(phy);
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>> +
>>>>>>> +     roothub_entry->phy = phy;
>>>>>>> +
>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int i, num_phys, err;
>>>>>>> +
>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>> +                                           "#phy-cells");
>>>>>>> +     if (num_phys <= 0)
>>>>>>> +             return NULL;
>>>>>>> +
>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>> +             return phy_roothub;
>>>>>>> +
>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>
>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>
>>>>
>>>> Yes.
>>>>
>>>>> an earlier version of my patch did exactly this, but it caused
>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>> easier reading:
>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>> can avoid re-enumerating device."
>>>>>
>>>>>>> +             if (err)
>>>>>>> +                     goto err_exit_phys;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return phy_roothub;
>>>>>>> +
>>>>>>> +err_exit_phys:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     return ERR_PTR(err);
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err, ret = 0;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     ret = ret;
>>>>>>> +     }
>>>>>>
>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>> if I understood Chunfeng Yun correctly this will require
>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>> Mediatek SoCs
>>>>>
>>>>
>>>> OK. I suppose that there are 2 cases
>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>> Remote wakeup probably required.
>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>> during suspend/resume to keep PHY link active.
>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>
>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>> ACK
>>>
>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>
>>>>>>> +
>>>>>>> +     return ret;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>> +
>>>>>>> +     return err;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>> +
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return;
>>>>>>> +
>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>
>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>> we're no longer able to reach low power states on system suspend.
>>>>> I'm not sure where this problem should be solved:
>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>> TI platforms
>>>>
>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>
>>>>> - fix this in the usb_phy_roothub code
>>>>
>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>> the controller is *NOT* marked as "wakeup-source"?
>>> I am also not sure if it would work, since the "wakeup-source"
>>> property is defined on the USB controller's parent node in case of the
>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>
>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>> devices by device_init_wakeup(dev, true),but not dependent on
>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>> decide whether call phy_exit()/init() or not.
> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> yet, so I cannot test this
> based on this suggestion I threw up two patches which are *compile
> tested only* based on Greg's usb-next branch
> you can find them here: [0] (as well as attached to this mail)
> 
> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> @Roger: can you please test this on a TI SoC?
> 
> (apologies in advance if these patches don't work)
> 
> please note that I won't have access to my computer until Saturday.
> if these patches need to be rewritten/replaced/etc. then feel free to
> send your own version to the list

Had to do the following to build

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..2884607 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)


And the following to fix runtime issues

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..8598906 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);


Here are my obervations
- if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
low power states.

- if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
and so phy_power_off won't be called either.

This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.

This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
What does /sys/bus/usb/device/usb<?>/power/wakeup say?

Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
Chunfeng, why did you need this patch?

[1] - https://patchwork.kernel.org/patch/10298641/

What options do we have now to fix the original issue?

Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
of doing phy_power_off() as well as phy_exit() during suspend.

Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
flag in hcd to use it?

> 
>>>>> - fix this in the PHY driver
>>>>
>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>> - assuming we define two new callbacks: .suspend and .resume
>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>> - the PHY framework could call .power_on by default if .resume is not defined
>>> - drivers could set .suspend and .resume on their own, allowing more
>>> fine-grained control by for example *only* stopping the clock (but not
>>> re-initializing the registers, etc.)
>>>
>>> @Roger: what do you think about this?
>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>> hear his opinion too
>>>
>>>>> - somewhere else
>>>>>
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>> @@ -0,0 +1,7 @@
>>>>>>> +struct usb_phy_roothub;
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>
>>>>>>
>>>>

> 
> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-21 11:30                             ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-21 11:30 UTC (permalink / raw)
  To: linus-amlogic

Martin,

On 21/03/18 00:01, Martin Blumenstingl wrote:
> Hi Roger, Hi Chunfeng,
> 
> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>> Hi Martin & Roger:
>>
>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>> Hi,
>>>>
>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> +some TI folks
>>>>>>
>>>>>> Hi Martin,
>>>>>>
>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>> disabled again on shutdown/suspend.
>>>>>>>
>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>> disable/enable them when required:
>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>
>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>
>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>
>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>> I'll explain why below.
>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>
>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>> I missed that, thanks
>>>
>>>>>>> ---
>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>
>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>> -usbcore-y += port.o
>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>
>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>> @@ -0,0 +1,158 @@
>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>> +/*
>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>> + *
>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> + */
>>>>>>> +
>>>>>>> +#include <linux/device.h>
>>>>>>> +#include <linux/list.h>
>>>>>>> +#include <linux/phy/phy.h>
>>>>>>> +#include <linux/of.h>
>>>>>>> +
>>>>>>> +#include "phy.h"
>>>>>>> +
>>>>>>> +struct usb_phy_roothub {
>>>>>>> +     struct phy              *phy;
>>>>>>> +     struct list_head        list;
>>>>>>> +};
>>>>>>> +
>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>> +     if (!roothub_entry)
>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>> +
>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>> +
>>>>>>> +     return roothub_entry;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>> +                                struct list_head *list)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>> +
>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>> +                     return 0;
>>>>>>> +             else
>>>>>>> +                     return PTR_ERR(phy);
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>> +
>>>>>>> +     roothub_entry->phy = phy;
>>>>>>> +
>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int i, num_phys, err;
>>>>>>> +
>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>> +                                           "#phy-cells");
>>>>>>> +     if (num_phys <= 0)
>>>>>>> +             return NULL;
>>>>>>> +
>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>> +             return phy_roothub;
>>>>>>> +
>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>
>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>
>>>>
>>>> Yes.
>>>>
>>>>> an earlier version of my patch did exactly this, but it caused
>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>> easier reading:
>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>> can avoid re-enumerating device."
>>>>>
>>>>>>> +             if (err)
>>>>>>> +                     goto err_exit_phys;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return phy_roothub;
>>>>>>> +
>>>>>>> +err_exit_phys:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     return ERR_PTR(err);
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err, ret = 0;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     ret = ret;
>>>>>>> +     }
>>>>>>
>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>> if I understood Chunfeng Yun correctly this will require
>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>> Mediatek SoCs
>>>>>
>>>>
>>>> OK. I suppose that there are 2 cases
>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>> Remote wakeup probably required.
>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>> during suspend/resume to keep PHY link active.
>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>
>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>> ACK
>>>
>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>
>>>>>>> +
>>>>>>> +     return ret;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>> +
>>>>>>> +     return err;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>> +
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return;
>>>>>>> +
>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>
>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>> we're no longer able to reach low power states on system suspend.
>>>>> I'm not sure where this problem should be solved:
>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>> TI platforms
>>>>
>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>
>>>>> - fix this in the usb_phy_roothub code
>>>>
>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>> the controller is *NOT* marked as "wakeup-source"?
>>> I am also not sure if it would work, since the "wakeup-source"
>>> property is defined on the USB controller's parent node in case of the
>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>
>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>> devices by device_init_wakeup(dev, true),but not dependent on
>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>> decide whether call phy_exit()/init() or not.
> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> yet, so I cannot test this
> based on this suggestion I threw up two patches which are *compile
> tested only* based on Greg's usb-next branch
> you can find them here: [0] (as well as attached to this mail)
> 
> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> @Roger: can you please test this on a TI SoC?
> 
> (apologies in advance if these patches don't work)
> 
> please note that I won't have access to my computer until Saturday.
> if these patches need to be rewritten/replaced/etc. then feel free to
> send your own version to the list

Had to do the following to build

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..2884607 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)


And the following to fix runtime issues

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..8598906 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);


Here are my obervations
- if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
low power states.

- if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
and so phy_power_off won't be called either.

This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.

This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
What does /sys/bus/usb/device/usb<?>/power/wakeup say?

Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
Chunfeng, why did you need this patch?

[1] - https://patchwork.kernel.org/patch/10298641/

What options do we have now to fix the original issue?

Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
of doing phy_power_off() as well as phy_exit() during suspend.

Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
flag in hcd to use it?

> 
>>>>> - fix this in the PHY driver
>>>>
>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>> - assuming we define two new callbacks: .suspend and .resume
>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>> - the PHY framework could call .power_on by default if .resume is not defined
>>> - drivers could set .suspend and .resume on their own, allowing more
>>> fine-grained control by for example *only* stopping the clock (but not
>>> re-initializing the registers, etc.)
>>>
>>> @Roger: what do you think about this?
>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>> hear his opinion too
>>>
>>>>> - somewhere else
>>>>>
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>> @@ -0,0 +1,7 @@
>>>>>>> +struct usb_phy_roothub;
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>
>>>>>>
>>>>

> 
> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> 

-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22  8:10                                 ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-22  8:10 UTC (permalink / raw)
  To: Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	Martin Blumenstingl, yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ,
	robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

Hi,
On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> Martin,
> 
> On 21/03/18 00:01, Martin Blumenstingl wrote:
> > Hi Roger, Hi Chunfeng,
> > 
> > On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
> >> Hi Martin & Roger:
> >>
> >> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>> Hi Roger,
> >>>
> >>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>>> Hi,
> >>>>
> >>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>>>>> +some TI folks
> >>>>>>
> >>>>>> Hi Martin,
> >>>>>>
> >>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>> disabled again on shutdown/suspend.
> >>>>>>>
> >>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>> disable/enable them when required:
> >>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>
> >>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>
> >>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> >>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> >>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >>>>>>
> >>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>> I'll explain why below.
> >>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>
> >>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>> I missed that, thanks
> >>>
> >>>>>>> ---
> >>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>> -usbcore-y += port.o
> >>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>
> >>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>> @@ -0,0 +1,158 @@
> >>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +/*
> >>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>> + *
> >>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>>>>> + */
> >>>>>>> +
> >>>>>>> +#include <linux/device.h>
> >>>>>>> +#include <linux/list.h>
> >>>>>>> +#include <linux/phy/phy.h>
> >>>>>>> +#include <linux/of.h>
> >>>>>>> +
> >>>>>>> +#include "phy.h"
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub {
> >>>>>>> +     struct phy              *phy;
> >>>>>>> +     struct list_head        list;
> >>>>>>> +};
> >>>>>>> +
> >>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>> +     if (!roothub_entry)
> >>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>> +
> >>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>> +
> >>>>>>> +     return roothub_entry;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>> +                                struct list_head *list)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>> +
> >>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>> +                     return 0;
> >>>>>>> +             else
> >>>>>>> +                     return PTR_ERR(phy);
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>> +
> >>>>>>> +     roothub_entry->phy = phy;
> >>>>>>> +
> >>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int i, num_phys, err;
> >>>>>>> +
> >>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>> +                                           "#phy-cells");
> >>>>>>> +     if (num_phys <= 0)
> >>>>>>> +             return NULL;
> >>>>>>> +
> >>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>> +             return phy_roothub;
> >>>>>>> +
> >>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>
> >>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>
> >>>>
> >>>> Yes.
> >>>>
> >>>>> an earlier version of my patch did exactly this, but it caused
> >>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>> easier reading:
> >>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>> can avoid re-enumerating device."
> >>>>>
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_exit_phys;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return phy_roothub;
> >>>>>>> +
> >>>>>>> +err_exit_phys:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     return ERR_PTR(err);
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err, ret = 0;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     ret = ret;
> >>>>>>> +     }
> >>>>>>
> >>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>> if I understood Chunfeng Yun correctly this will require
> >>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>> Mediatek SoCs
> >>>>>
> >>>>
> >>>> OK. I suppose that there are 2 cases
> >>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>> Remote wakeup probably required.
> >>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>> during suspend/resume to keep PHY link active.
> >>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>
> >>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>> ACK
> >>>
> >>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>
> >>>>>>> +
> >>>>>>> +     return ret;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +     return err;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>> +
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>
> >>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>> we're no longer able to reach low power states on system suspend.
> >>>>> I'm not sure where this problem should be solved:
> >>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>> TI platforms
> >>>>
> >>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>
> >>>>> - fix this in the usb_phy_roothub code
> >>>>
> >>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>> the controller is *NOT* marked as "wakeup-source"?
> >>> I am also not sure if it would work, since the "wakeup-source"
> >>> property is defined on the USB controller's parent node in case of the
> >>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>
> >> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >> devices by device_init_wakeup(dev, true),but not dependent on
> >> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >> decide whether call phy_exit()/init() or not.
> > the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> > yet, so I cannot test this
> > based on this suggestion I threw up two patches which are *compile
> > tested only* based on Greg's usb-next branch
> > you can find them here: [0] (as well as attached to this mail)
> > 
> > @Chunfeng: can you please test this on one of your Mediatek SoCs?
> > @Roger: can you please test this on a TI SoC?
> > 
> > (apologies in advance if these patches don't work)
> > 
> > please note that I won't have access to my computer until Saturday.
> > if these patches need to be rewritten/replaced/etc. then feel free to
> > send your own version to the list
> 
> Had to do the following to build
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);

Try to use hcd->self.controller instead of &rhdev->dev;

>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
ditto
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
ditto
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> 
> 
> And the following to fix runtime issues
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
It's ok
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> 
> 
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
> 
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
> 
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
You maybe use a wrong device.
> 
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
They are also "disabled" state;
I think we should use wakeup state of hcd controller, but not
usb_device's, please test it, thanks.

> 
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
> 
> [1] - https://patchwork.kernel.org/patch/10298641/
> 
> What options do we have now to fix the original issue?
> 
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
> 
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?

> 
> > 
> >>>>> - fix this in the PHY driver
> >>>>
> >>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >>> I actually wonder if phy_ops should have explicit suspend/resume support:
> >>> - assuming we define two new callbacks: .suspend and .resume
> >>> - the PHY framework could call .power_off by default if .suspend is not defined
> >>> - the PHY framework could call .power_on by default if .resume is not defined
> >>> - drivers could set .suspend and .resume on their own, allowing more
> >>> fine-grained control by for example *only* stopping the clock (but not
> >>> re-initializing the registers, etc.)
> >>>
> >>> @Roger: what do you think about this?
> >>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> >>> hear his opinion too
> >>>
> >>>>> - somewhere else
> >>>>>
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..6fde59bfbff8
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.h
> >>>>>>> @@ -0,0 +1,7 @@
> >>>>>>> +struct usb_phy_roothub;
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>>>>
> >>>>>>
> >>>>
> 
> > 
> > [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> > 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22  8:10                                 ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-22  8:10 UTC (permalink / raw)
  To: Roger Quadros
  Cc: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi,
On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> Martin,
> 
> On 21/03/18 00:01, Martin Blumenstingl wrote:
> > Hi Roger, Hi Chunfeng,
> > 
> > On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >> Hi Martin & Roger:
> >>
> >> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>> Hi Roger,
> >>>
> >>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>> Hi,
> >>>>
> >>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> +some TI folks
> >>>>>>
> >>>>>> Hi Martin,
> >>>>>>
> >>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>> disabled again on shutdown/suspend.
> >>>>>>>
> >>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>> disable/enable them when required:
> >>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>
> >>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>
> >>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>
> >>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>> I'll explain why below.
> >>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>
> >>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>> I missed that, thanks
> >>>
> >>>>>>> ---
> >>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>> -usbcore-y += port.o
> >>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>
> >>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>> @@ -0,0 +1,158 @@
> >>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +/*
> >>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>> + *
> >>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> + */
> >>>>>>> +
> >>>>>>> +#include <linux/device.h>
> >>>>>>> +#include <linux/list.h>
> >>>>>>> +#include <linux/phy/phy.h>
> >>>>>>> +#include <linux/of.h>
> >>>>>>> +
> >>>>>>> +#include "phy.h"
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub {
> >>>>>>> +     struct phy              *phy;
> >>>>>>> +     struct list_head        list;
> >>>>>>> +};
> >>>>>>> +
> >>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>> +     if (!roothub_entry)
> >>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>> +
> >>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>> +
> >>>>>>> +     return roothub_entry;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>> +                                struct list_head *list)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>> +
> >>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>> +                     return 0;
> >>>>>>> +             else
> >>>>>>> +                     return PTR_ERR(phy);
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>> +
> >>>>>>> +     roothub_entry->phy = phy;
> >>>>>>> +
> >>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int i, num_phys, err;
> >>>>>>> +
> >>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>> +                                           "#phy-cells");
> >>>>>>> +     if (num_phys <= 0)
> >>>>>>> +             return NULL;
> >>>>>>> +
> >>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>> +             return phy_roothub;
> >>>>>>> +
> >>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>
> >>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>
> >>>>
> >>>> Yes.
> >>>>
> >>>>> an earlier version of my patch did exactly this, but it caused
> >>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>> easier reading:
> >>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>> can avoid re-enumerating device."
> >>>>>
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_exit_phys;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return phy_roothub;
> >>>>>>> +
> >>>>>>> +err_exit_phys:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     return ERR_PTR(err);
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err, ret = 0;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     ret = ret;
> >>>>>>> +     }
> >>>>>>
> >>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>> if I understood Chunfeng Yun correctly this will require
> >>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>> Mediatek SoCs
> >>>>>
> >>>>
> >>>> OK. I suppose that there are 2 cases
> >>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>> Remote wakeup probably required.
> >>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>> during suspend/resume to keep PHY link active.
> >>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>
> >>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>> ACK
> >>>
> >>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>
> >>>>>>> +
> >>>>>>> +     return ret;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +     return err;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>> +
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>
> >>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>> we're no longer able to reach low power states on system suspend.
> >>>>> I'm not sure where this problem should be solved:
> >>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>> TI platforms
> >>>>
> >>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>
> >>>>> - fix this in the usb_phy_roothub code
> >>>>
> >>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>> the controller is *NOT* marked as "wakeup-source"?
> >>> I am also not sure if it would work, since the "wakeup-source"
> >>> property is defined on the USB controller's parent node in case of the
> >>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>
> >> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >> devices by device_init_wakeup(dev, true),but not dependent on
> >> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >> decide whether call phy_exit()/init() or not.
> > the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> > yet, so I cannot test this
> > based on this suggestion I threw up two patches which are *compile
> > tested only* based on Greg's usb-next branch
> > you can find them here: [0] (as well as attached to this mail)
> > 
> > @Chunfeng: can you please test this on one of your Mediatek SoCs?
> > @Roger: can you please test this on a TI SoC?
> > 
> > (apologies in advance if these patches don't work)
> > 
> > please note that I won't have access to my computer until Saturday.
> > if these patches need to be rewritten/replaced/etc. then feel free to
> > send your own version to the list
> 
> Had to do the following to build
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);

Try to use hcd->self.controller instead of &rhdev->dev;

>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
ditto
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
ditto
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> 
> 
> And the following to fix runtime issues
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
It's ok
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> 
> 
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
> 
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
> 
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
You maybe use a wrong device.
> 
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
They are also "disabled" state;
I think we should use wakeup state of hcd controller, but not
usb_device's, please test it, thanks.

> 
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
> 
> [1] - https://patchwork.kernel.org/patch/10298641/
> 
> What options do we have now to fix the original issue?
> 
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
> 
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?

> 
> > 
> >>>>> - fix this in the PHY driver
> >>>>
> >>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >>> I actually wonder if phy_ops should have explicit suspend/resume support:
> >>> - assuming we define two new callbacks: .suspend and .resume
> >>> - the PHY framework could call .power_off by default if .suspend is not defined
> >>> - the PHY framework could call .power_on by default if .resume is not defined
> >>> - drivers could set .suspend and .resume on their own, allowing more
> >>> fine-grained control by for example *only* stopping the clock (but not
> >>> re-initializing the registers, etc.)
> >>>
> >>> @Roger: what do you think about this?
> >>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> >>> hear his opinion too
> >>>
> >>>>> - somewhere else
> >>>>>
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..6fde59bfbff8
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.h
> >>>>>>> @@ -0,0 +1,7 @@
> >>>>>>> +struct usb_phy_roothub;
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>>>>
> >>>>>>
> >>>>
> 
> > 
> > [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> > 
>
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22  8:10                                 ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-22  8:10 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,
On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> Martin,
> 
> On 21/03/18 00:01, Martin Blumenstingl wrote:
> > Hi Roger, Hi Chunfeng,
> > 
> > On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >> Hi Martin & Roger:
> >>
> >> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>> Hi Roger,
> >>>
> >>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>> Hi,
> >>>>
> >>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> +some TI folks
> >>>>>>
> >>>>>> Hi Martin,
> >>>>>>
> >>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>> disabled again on shutdown/suspend.
> >>>>>>>
> >>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>> disable/enable them when required:
> >>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>
> >>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>
> >>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>
> >>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>> I'll explain why below.
> >>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>
> >>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>> I missed that, thanks
> >>>
> >>>>>>> ---
> >>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>> -usbcore-y += port.o
> >>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>
> >>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>> @@ -0,0 +1,158 @@
> >>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +/*
> >>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>> + *
> >>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> + */
> >>>>>>> +
> >>>>>>> +#include <linux/device.h>
> >>>>>>> +#include <linux/list.h>
> >>>>>>> +#include <linux/phy/phy.h>
> >>>>>>> +#include <linux/of.h>
> >>>>>>> +
> >>>>>>> +#include "phy.h"
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub {
> >>>>>>> +     struct phy              *phy;
> >>>>>>> +     struct list_head        list;
> >>>>>>> +};
> >>>>>>> +
> >>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>> +     if (!roothub_entry)
> >>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>> +
> >>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>> +
> >>>>>>> +     return roothub_entry;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>> +                                struct list_head *list)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>> +
> >>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>> +                     return 0;
> >>>>>>> +             else
> >>>>>>> +                     return PTR_ERR(phy);
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>> +
> >>>>>>> +     roothub_entry->phy = phy;
> >>>>>>> +
> >>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int i, num_phys, err;
> >>>>>>> +
> >>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>> +                                           "#phy-cells");
> >>>>>>> +     if (num_phys <= 0)
> >>>>>>> +             return NULL;
> >>>>>>> +
> >>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>> +             return phy_roothub;
> >>>>>>> +
> >>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>
> >>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>
> >>>>
> >>>> Yes.
> >>>>
> >>>>> an earlier version of my patch did exactly this, but it caused
> >>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>> easier reading:
> >>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>> can avoid re-enumerating device."
> >>>>>
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_exit_phys;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return phy_roothub;
> >>>>>>> +
> >>>>>>> +err_exit_phys:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     return ERR_PTR(err);
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err, ret = 0;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     ret = ret;
> >>>>>>> +     }
> >>>>>>
> >>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>> if I understood Chunfeng Yun correctly this will require
> >>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>> Mediatek SoCs
> >>>>>
> >>>>
> >>>> OK. I suppose that there are 2 cases
> >>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>> Remote wakeup probably required.
> >>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>> during suspend/resume to keep PHY link active.
> >>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>
> >>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>> ACK
> >>>
> >>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>
> >>>>>>> +
> >>>>>>> +     return ret;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +     return err;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>> +
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>
> >>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>> we're no longer able to reach low power states on system suspend.
> >>>>> I'm not sure where this problem should be solved:
> >>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>> TI platforms
> >>>>
> >>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>
> >>>>> - fix this in the usb_phy_roothub code
> >>>>
> >>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>> the controller is *NOT* marked as "wakeup-source"?
> >>> I am also not sure if it would work, since the "wakeup-source"
> >>> property is defined on the USB controller's parent node in case of the
> >>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>
> >> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >> devices by device_init_wakeup(dev, true),but not dependent on
> >> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >> decide whether call phy_exit()/init() or not.
> > the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> > yet, so I cannot test this
> > based on this suggestion I threw up two patches which are *compile
> > tested only* based on Greg's usb-next branch
> > you can find them here: [0] (as well as attached to this mail)
> > 
> > @Chunfeng: can you please test this on one of your Mediatek SoCs?
> > @Roger: can you please test this on a TI SoC?
> > 
> > (apologies in advance if these patches don't work)
> > 
> > please note that I won't have access to my computer until Saturday.
> > if these patches need to be rewritten/replaced/etc. then feel free to
> > send your own version to the list
> 
> Had to do the following to build
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);

Try to use hcd->self.controller instead of &rhdev->dev;

>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
ditto
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
ditto
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> 
> 
> And the following to fix runtime issues
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
It's ok
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> 
> 
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
> 
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
> 
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
You maybe use a wrong device.
> 
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
They are also "disabled" state;
I think we should use wakeup state of hcd controller, but not
usb_device's, please test it, thanks.

> 
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
> 
> [1] - https://patchwork.kernel.org/patch/10298641/
> 
> What options do we have now to fix the original issue?
> 
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
> 
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?

> 
> > 
> >>>>> - fix this in the PHY driver
> >>>>
> >>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >>> I actually wonder if phy_ops should have explicit suspend/resume support:
> >>> - assuming we define two new callbacks: .suspend and .resume
> >>> - the PHY framework could call .power_off by default if .suspend is not defined
> >>> - the PHY framework could call .power_on by default if .resume is not defined
> >>> - drivers could set .suspend and .resume on their own, allowing more
> >>> fine-grained control by for example *only* stopping the clock (but not
> >>> re-initializing the registers, etc.)
> >>>
> >>> @Roger: what do you think about this?
> >>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> >>> hear his opinion too
> >>>
> >>>>> - somewhere else
> >>>>>
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..6fde59bfbff8
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.h
> >>>>>>> @@ -0,0 +1,7 @@
> >>>>>>> +struct usb_phy_roothub;
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>>>>
> >>>>>>
> >>>>
> 
> > 
> > [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> > 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22  8:10                                 ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-22  8:10 UTC (permalink / raw)
  To: linus-amlogic

Hi,
On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> Martin,
> 
> On 21/03/18 00:01, Martin Blumenstingl wrote:
> > Hi Roger, Hi Chunfeng,
> > 
> > On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >> Hi Martin & Roger:
> >>
> >> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>> Hi Roger,
> >>>
> >>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>> Hi,
> >>>>
> >>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> +some TI folks
> >>>>>>
> >>>>>> Hi Martin,
> >>>>>>
> >>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>> disabled again on shutdown/suspend.
> >>>>>>>
> >>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>> disable/enable them when required:
> >>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>
> >>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>
> >>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>
> >>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>> I'll explain why below.
> >>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>
> >>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>> I missed that, thanks
> >>>
> >>>>>>> ---
> >>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>> -usbcore-y += port.o
> >>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>
> >>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>> @@ -0,0 +1,158 @@
> >>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +/*
> >>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>> + *
> >>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> + */
> >>>>>>> +
> >>>>>>> +#include <linux/device.h>
> >>>>>>> +#include <linux/list.h>
> >>>>>>> +#include <linux/phy/phy.h>
> >>>>>>> +#include <linux/of.h>
> >>>>>>> +
> >>>>>>> +#include "phy.h"
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub {
> >>>>>>> +     struct phy              *phy;
> >>>>>>> +     struct list_head        list;
> >>>>>>> +};
> >>>>>>> +
> >>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>> +     if (!roothub_entry)
> >>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>> +
> >>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>> +
> >>>>>>> +     return roothub_entry;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>> +                                struct list_head *list)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>> +
> >>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>> +                     return 0;
> >>>>>>> +             else
> >>>>>>> +                     return PTR_ERR(phy);
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>> +
> >>>>>>> +     roothub_entry->phy = phy;
> >>>>>>> +
> >>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int i, num_phys, err;
> >>>>>>> +
> >>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>> +                                           "#phy-cells");
> >>>>>>> +     if (num_phys <= 0)
> >>>>>>> +             return NULL;
> >>>>>>> +
> >>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>> +             return phy_roothub;
> >>>>>>> +
> >>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>
> >>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>
> >>>>
> >>>> Yes.
> >>>>
> >>>>> an earlier version of my patch did exactly this, but it caused
> >>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>> easier reading:
> >>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>> can avoid re-enumerating device."
> >>>>>
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_exit_phys;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return phy_roothub;
> >>>>>>> +
> >>>>>>> +err_exit_phys:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     return ERR_PTR(err);
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err, ret = 0;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     ret = ret;
> >>>>>>> +     }
> >>>>>>
> >>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>> if I understood Chunfeng Yun correctly this will require
> >>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>> Mediatek SoCs
> >>>>>
> >>>>
> >>>> OK. I suppose that there are 2 cases
> >>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>> Remote wakeup probably required.
> >>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>> during suspend/resume to keep PHY link active.
> >>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>
> >>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>> ACK
> >>>
> >>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>
> >>>>>>> +
> >>>>>>> +     return ret;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +     return err;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>> +
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>
> >>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>> we're no longer able to reach low power states on system suspend.
> >>>>> I'm not sure where this problem should be solved:
> >>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>> TI platforms
> >>>>
> >>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>
> >>>>> - fix this in the usb_phy_roothub code
> >>>>
> >>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>> the controller is *NOT* marked as "wakeup-source"?
> >>> I am also not sure if it would work, since the "wakeup-source"
> >>> property is defined on the USB controller's parent node in case of the
> >>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>
> >> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >> devices by device_init_wakeup(dev, true),but not dependent on
> >> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >> decide whether call phy_exit()/init() or not.
> > the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> > yet, so I cannot test this
> > based on this suggestion I threw up two patches which are *compile
> > tested only* based on Greg's usb-next branch
> > you can find them here: [0] (as well as attached to this mail)
> > 
> > @Chunfeng: can you please test this on one of your Mediatek SoCs?
> > @Roger: can you please test this on a TI SoC?
> > 
> > (apologies in advance if these patches don't work)
> > 
> > please note that I won't have access to my computer until Saturday.
> > if these patches need to be rewritten/replaced/etc. then feel free to
> > send your own version to the list
> 
> Had to do the following to build
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);

Try to use hcd->self.controller instead of &rhdev->dev;

>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
ditto
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
ditto
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> 
> 
> And the following to fix runtime issues
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
It's ok
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> 
> 
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
> 
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
> 
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
You maybe use a wrong device.
> 
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
They are also "disabled" state;
I think we should use wakeup state of hcd controller, but not
usb_device's, please test it, thanks.

> 
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
> 
> [1] - https://patchwork.kernel.org/patch/10298641/
> 
> What options do we have now to fix the original issue?
> 
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
> 
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?

> 
> > 
> >>>>> - fix this in the PHY driver
> >>>>
> >>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >>> I actually wonder if phy_ops should have explicit suspend/resume support:
> >>> - assuming we define two new callbacks: .suspend and .resume
> >>> - the PHY framework could call .power_off by default if .suspend is not defined
> >>> - the PHY framework could call .power_on by default if .resume is not defined
> >>> - drivers could set .suspend and .resume on their own, allowing more
> >>> fine-grained control by for example *only* stopping the clock (but not
> >>> re-initializing the registers, etc.)
> >>>
> >>> @Roger: what do you think about this?
> >>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> >>> hear his opinion too
> >>>
> >>>>> - somewhere else
> >>>>>
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..6fde59bfbff8
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.h
> >>>>>>> @@ -0,0 +1,7 @@
> >>>>>>> +struct usb_phy_roothub;
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>>>>
> >>>>>>
> >>>>
> 
> > 
> > [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> > 
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22 12:41                                   ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-22 12:41 UTC (permalink / raw)
  To: Chunfeng Yun
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	Martin Blumenstingl, yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ,
	robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

On 22/03/18 10:10, Chunfeng Yun wrote:
> Hi,
> On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
>> Martin,
>>
>> On 21/03/18 00:01, Martin Blumenstingl wrote:
>>> Hi Roger, Hi Chunfeng,
>>>
>>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
>>>> Hi Martin & Roger:
>>>>
>>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>>> Hi Roger,
>>>>>>>
>>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>>>>> +some TI folks
>>>>>>>>
>>>>>>>> Hi Martin,
>>>>>>>>
>>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>>
>>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>>> disable/enable them when required:
>>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>>
>>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>>
>>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>>>>>
>>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>>> I'll explain why below.
>>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>>
>>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>>> I missed that, thanks
>>>>>
>>>>>>>>> ---
>>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>>
>>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>>> -usbcore-y += port.o
>>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>>
>>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>>> new file mode 100644
>>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>>> --- /dev/null
>>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>>> +/*
>>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>>> + *
>>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>>>> + */
>>>>>>>>> +
>>>>>>>>> +#include <linux/device.h>
>>>>>>>>> +#include <linux/list.h>
>>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>>> +#include <linux/of.h>
>>>>>>>>> +
>>>>>>>>> +#include "phy.h"
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub {
>>>>>>>>> +     struct phy              *phy;
>>>>>>>>> +     struct list_head        list;
>>>>>>>>> +};
>>>>>>>>> +
>>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>>> +     if (!roothub_entry)
>>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>>> +
>>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>>> +
>>>>>>>>> +     return roothub_entry;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>>> +                                struct list_head *list)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>>> +
>>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>>> +                     return 0;
>>>>>>>>> +             else
>>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>>> +
>>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>>> +
>>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int i, num_phys, err;
>>>>>>>>> +
>>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>>> +                                           "#phy-cells");
>>>>>>>>> +     if (num_phys <= 0)
>>>>>>>>> +             return NULL;
>>>>>>>>> +
>>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>>> +             return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>>
>>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>>
>>>>>>
>>>>>> Yes.
>>>>>>
>>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>>> easier reading:
>>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>>> can avoid re-enumerating device."
>>>>>>>
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_exit_phys;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +err_exit_phys:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     return ERR_PTR(err);
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err, ret = 0;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     ret = ret;
>>>>>>>>> +     }
>>>>>>>>
>>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>>> Mediatek SoCs
>>>>>>>
>>>>>>
>>>>>> OK. I suppose that there are 2 cases
>>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>>> Remote wakeup probably required.
>>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>>> during suspend/resume to keep PHY link active.
>>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>>
>>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>>> ACK
>>>>>
>>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>>
>>>>>>>>> +
>>>>>>>>> +     return ret;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +     return err;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>>> +
>>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>
>>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>>> I'm not sure where this problem should be solved:
>>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>>> TI platforms
>>>>>>
>>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>>
>>>>>>> - fix this in the usb_phy_roothub code
>>>>>>
>>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>>> the controller is *NOT* marked as "wakeup-source"?
>>>>> I am also not sure if it would work, since the "wakeup-source"
>>>>> property is defined on the USB controller's parent node in case of the
>>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>>
>>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>>> devices by device_init_wakeup(dev, true),but not dependent on
>>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>>> decide whether call phy_exit()/init() or not.
>>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>>> yet, so I cannot test this
>>> based on this suggestion I threw up two patches which are *compile
>>> tested only* based on Greg's usb-next branch
>>> you can find them here: [0] (as well as attached to this mail)
>>>
>>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>>> @Roger: can you please test this on a TI SoC?
>>>
>>> (apologies in advance if these patches don't work)
>>>
>>> please note that I won't have access to my computer until Saturday.
>>> if these patches need to be rewritten/replaced/etc. then feel free to
>>> send your own version to the list
>>
>> Had to do the following to build
>>
>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>> index 6d4a419..2884607 100644
>> --- a/drivers/usb/core/hcd.c
>> +++ b/drivers/usb/core/hcd.c
>> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>>  		hcd->state = HC_STATE_SUSPENDED;
>>  
>>  		if (!PMSG_IS_AUTO(msg))
>> -			usb_phy_roothub_suspend(hcd->phy_roothub);
>> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> 
> Try to use hcd->self.controller instead of &rhdev->dev;

Actually it should be hcd->self.sysdev.
> 
>>  
>>  		/* Did we race with a root-hub wakeup event? */
>>  		if (rhdev->do_remote_wakeup) {
>> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  	}
>>  
>>  	if (!PMSG_IS_AUTO(msg)) {
>> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
>> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		if (status)
>>  			return status;
>>  	}
>> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  		}
>>  	} else {
>>  		hcd->state = old_state;
>> -		usb_phy_roothub_suspend(hcd->phy_roothub);
>> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>>  				"resume", status);
>>  		if (status != -ESHUTDOWN)
>>
>>
>> And the following to fix runtime issues
>>
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> index 2eca371..8598906 100644
>> --- a/drivers/usb/core/phy.c
>> +++ b/drivers/usb/core/phy.c
>> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>  			return PTR_ERR(phy);
>>  	}
>>  
>> -	roothub_entry = usb_phy_roothub_alloc(dev);
>> -	if (IS_ERR(roothub_entry))
>> -		return PTR_ERR(roothub_entry);
>> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +	if (!roothub_entry)
>> +		return -ENOMEM;
>>  
>>  	roothub_entry->phy = phy;
>>  
>> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>>  	usb_phy_roothub_power_off(phy_roothub);
>>  
>>  	/* keep the PHYs initialized so the device can wake up the system */
>> -	if (device_can_wakeup(dev))
>> +	if (device_may_wakeup(dev))
> It's ok

I had to additionally fix usb_phy_roothub_resume() from
	if (device_can_wakeup(dev))
to
	if (!device_may_wakeup(dev))

>>  		return 0;
>>  
>>  	return usb_phy_roothub_exit(phy_roothub);
>>
>>
>> Here are my obervations
>> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
>> low power states.
>>
>> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
>> and so phy_power_off won't be called either.
>>
>> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> You maybe use a wrong device.

Yes, after using the correct device I don't see the problem.

Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
Once you confirm it works for you I can send the 2 patches officially.

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..04cc453 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..25fe729 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);
@@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
 	int err;
 
 	/* if the device can't wake up the system _exit was called */
-	if (device_can_wakeup(dev)) {
+	if (!device_may_wakeup(dev)) {
 		err = usb_phy_roothub_init(phy_roothub);
 		if (err)
 			return err;
-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22 12:41                                   ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-22 12:41 UTC (permalink / raw)
  To: Chunfeng Yun
  Cc: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

On 22/03/18 10:10, Chunfeng Yun wrote:
> Hi,
> On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
>> Martin,
>>
>> On 21/03/18 00:01, Martin Blumenstingl wrote:
>>> Hi Roger, Hi Chunfeng,
>>>
>>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>>> Hi Martin & Roger:
>>>>
>>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>>> Hi Roger,
>>>>>>>
>>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>>> +some TI folks
>>>>>>>>
>>>>>>>> Hi Martin,
>>>>>>>>
>>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>>
>>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>>> disable/enable them when required:
>>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>>
>>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>>
>>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>>
>>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>>> I'll explain why below.
>>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>>
>>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>>> I missed that, thanks
>>>>>
>>>>>>>>> ---
>>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>>
>>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>>> -usbcore-y += port.o
>>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>>
>>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>>> new file mode 100644
>>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>>> --- /dev/null
>>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>>> +/*
>>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>>> + *
>>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> + */
>>>>>>>>> +
>>>>>>>>> +#include <linux/device.h>
>>>>>>>>> +#include <linux/list.h>
>>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>>> +#include <linux/of.h>
>>>>>>>>> +
>>>>>>>>> +#include "phy.h"
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub {
>>>>>>>>> +     struct phy              *phy;
>>>>>>>>> +     struct list_head        list;
>>>>>>>>> +};
>>>>>>>>> +
>>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>>> +     if (!roothub_entry)
>>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>>> +
>>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>>> +
>>>>>>>>> +     return roothub_entry;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>>> +                                struct list_head *list)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>>> +
>>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>>> +                     return 0;
>>>>>>>>> +             else
>>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>>> +
>>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>>> +
>>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int i, num_phys, err;
>>>>>>>>> +
>>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>>> +                                           "#phy-cells");
>>>>>>>>> +     if (num_phys <= 0)
>>>>>>>>> +             return NULL;
>>>>>>>>> +
>>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>>> +             return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>>
>>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>>
>>>>>>
>>>>>> Yes.
>>>>>>
>>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>>> easier reading:
>>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>>> can avoid re-enumerating device."
>>>>>>>
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_exit_phys;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +err_exit_phys:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     return ERR_PTR(err);
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err, ret = 0;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     ret = ret;
>>>>>>>>> +     }
>>>>>>>>
>>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>>> Mediatek SoCs
>>>>>>>
>>>>>>
>>>>>> OK. I suppose that there are 2 cases
>>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>>> Remote wakeup probably required.
>>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>>> during suspend/resume to keep PHY link active.
>>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>>
>>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>>> ACK
>>>>>
>>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>>
>>>>>>>>> +
>>>>>>>>> +     return ret;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +     return err;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>>> +
>>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>
>>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>>> I'm not sure where this problem should be solved:
>>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>>> TI platforms
>>>>>>
>>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>>
>>>>>>> - fix this in the usb_phy_roothub code
>>>>>>
>>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>>> the controller is *NOT* marked as "wakeup-source"?
>>>>> I am also not sure if it would work, since the "wakeup-source"
>>>>> property is defined on the USB controller's parent node in case of the
>>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>>
>>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>>> devices by device_init_wakeup(dev, true),but not dependent on
>>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>>> decide whether call phy_exit()/init() or not.
>>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>>> yet, so I cannot test this
>>> based on this suggestion I threw up two patches which are *compile
>>> tested only* based on Greg's usb-next branch
>>> you can find them here: [0] (as well as attached to this mail)
>>>
>>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>>> @Roger: can you please test this on a TI SoC?
>>>
>>> (apologies in advance if these patches don't work)
>>>
>>> please note that I won't have access to my computer until Saturday.
>>> if these patches need to be rewritten/replaced/etc. then feel free to
>>> send your own version to the list
>>
>> Had to do the following to build
>>
>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>> index 6d4a419..2884607 100644
>> --- a/drivers/usb/core/hcd.c
>> +++ b/drivers/usb/core/hcd.c
>> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>>  		hcd->state = HC_STATE_SUSPENDED;
>>  
>>  		if (!PMSG_IS_AUTO(msg))
>> -			usb_phy_roothub_suspend(hcd->phy_roothub);
>> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> 
> Try to use hcd->self.controller instead of &rhdev->dev;

Actually it should be hcd->self.sysdev.
> 
>>  
>>  		/* Did we race with a root-hub wakeup event? */
>>  		if (rhdev->do_remote_wakeup) {
>> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  	}
>>  
>>  	if (!PMSG_IS_AUTO(msg)) {
>> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
>> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		if (status)
>>  			return status;
>>  	}
>> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  		}
>>  	} else {
>>  		hcd->state = old_state;
>> -		usb_phy_roothub_suspend(hcd->phy_roothub);
>> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>>  				"resume", status);
>>  		if (status != -ESHUTDOWN)
>>
>>
>> And the following to fix runtime issues
>>
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> index 2eca371..8598906 100644
>> --- a/drivers/usb/core/phy.c
>> +++ b/drivers/usb/core/phy.c
>> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>  			return PTR_ERR(phy);
>>  	}
>>  
>> -	roothub_entry = usb_phy_roothub_alloc(dev);
>> -	if (IS_ERR(roothub_entry))
>> -		return PTR_ERR(roothub_entry);
>> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +	if (!roothub_entry)
>> +		return -ENOMEM;
>>  
>>  	roothub_entry->phy = phy;
>>  
>> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>>  	usb_phy_roothub_power_off(phy_roothub);
>>  
>>  	/* keep the PHYs initialized so the device can wake up the system */
>> -	if (device_can_wakeup(dev))
>> +	if (device_may_wakeup(dev))
> It's ok

I had to additionally fix usb_phy_roothub_resume() from
	if (device_can_wakeup(dev))
to
	if (!device_may_wakeup(dev))

>>  		return 0;
>>  
>>  	return usb_phy_roothub_exit(phy_roothub);
>>
>>
>> Here are my obervations
>> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
>> low power states.
>>
>> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
>> and so phy_power_off won't be called either.
>>
>> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> You maybe use a wrong device.

Yes, after using the correct device I don't see the problem.

Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
Once you confirm it works for you I can send the 2 patches officially.

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..04cc453 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..25fe729 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);
@@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
 	int err;
 
 	/* if the device can't wake up the system _exit was called */
-	if (device_can_wakeup(dev)) {
+	if (!device_may_wakeup(dev)) {
 		err = usb_phy_roothub_init(phy_roothub);
 		if (err)
 			return err;

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22 12:41                                   ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-22 12:41 UTC (permalink / raw)
  To: linux-arm-kernel

On 22/03/18 10:10, Chunfeng Yun wrote:
> Hi,
> On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
>> Martin,
>>
>> On 21/03/18 00:01, Martin Blumenstingl wrote:
>>> Hi Roger, Hi Chunfeng,
>>>
>>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>>> Hi Martin & Roger:
>>>>
>>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>>> Hi Roger,
>>>>>>>
>>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>>> +some TI folks
>>>>>>>>
>>>>>>>> Hi Martin,
>>>>>>>>
>>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>>
>>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>>> disable/enable them when required:
>>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>>
>>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>>
>>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>>
>>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>>> I'll explain why below.
>>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>>
>>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>>> I missed that, thanks
>>>>>
>>>>>>>>> ---
>>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>>
>>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>>> -usbcore-y += port.o
>>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>>
>>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>>> new file mode 100644
>>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>>> --- /dev/null
>>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>>> +/*
>>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>>> + *
>>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> + */
>>>>>>>>> +
>>>>>>>>> +#include <linux/device.h>
>>>>>>>>> +#include <linux/list.h>
>>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>>> +#include <linux/of.h>
>>>>>>>>> +
>>>>>>>>> +#include "phy.h"
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub {
>>>>>>>>> +     struct phy              *phy;
>>>>>>>>> +     struct list_head        list;
>>>>>>>>> +};
>>>>>>>>> +
>>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>>> +     if (!roothub_entry)
>>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>>> +
>>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>>> +
>>>>>>>>> +     return roothub_entry;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>>> +                                struct list_head *list)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>>> +
>>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>>> +                     return 0;
>>>>>>>>> +             else
>>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>>> +
>>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>>> +
>>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int i, num_phys, err;
>>>>>>>>> +
>>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>>> +                                           "#phy-cells");
>>>>>>>>> +     if (num_phys <= 0)
>>>>>>>>> +             return NULL;
>>>>>>>>> +
>>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>>> +             return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>>
>>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>>
>>>>>>
>>>>>> Yes.
>>>>>>
>>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>>> easier reading:
>>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>>> can avoid re-enumerating device."
>>>>>>>
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_exit_phys;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +err_exit_phys:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     return ERR_PTR(err);
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err, ret = 0;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     ret = ret;
>>>>>>>>> +     }
>>>>>>>>
>>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>>> Mediatek SoCs
>>>>>>>
>>>>>>
>>>>>> OK. I suppose that there are 2 cases
>>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>>> Remote wakeup probably required.
>>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>>> during suspend/resume to keep PHY link active.
>>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>>
>>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>>> ACK
>>>>>
>>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>>
>>>>>>>>> +
>>>>>>>>> +     return ret;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +     return err;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>>> +
>>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>
>>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>>> I'm not sure where this problem should be solved:
>>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>>> TI platforms
>>>>>>
>>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>>
>>>>>>> - fix this in the usb_phy_roothub code
>>>>>>
>>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>>> the controller is *NOT* marked as "wakeup-source"?
>>>>> I am also not sure if it would work, since the "wakeup-source"
>>>>> property is defined on the USB controller's parent node in case of the
>>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>>
>>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>>> devices by device_init_wakeup(dev, true),but not dependent on
>>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>>> decide whether call phy_exit()/init() or not.
>>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>>> yet, so I cannot test this
>>> based on this suggestion I threw up two patches which are *compile
>>> tested only* based on Greg's usb-next branch
>>> you can find them here: [0] (as well as attached to this mail)
>>>
>>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>>> @Roger: can you please test this on a TI SoC?
>>>
>>> (apologies in advance if these patches don't work)
>>>
>>> please note that I won't have access to my computer until Saturday.
>>> if these patches need to be rewritten/replaced/etc. then feel free to
>>> send your own version to the list
>>
>> Had to do the following to build
>>
>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>> index 6d4a419..2884607 100644
>> --- a/drivers/usb/core/hcd.c
>> +++ b/drivers/usb/core/hcd.c
>> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>>  		hcd->state = HC_STATE_SUSPENDED;
>>  
>>  		if (!PMSG_IS_AUTO(msg))
>> -			usb_phy_roothub_suspend(hcd->phy_roothub);
>> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> 
> Try to use hcd->self.controller instead of &rhdev->dev;

Actually it should be hcd->self.sysdev.
> 
>>  
>>  		/* Did we race with a root-hub wakeup event? */
>>  		if (rhdev->do_remote_wakeup) {
>> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  	}
>>  
>>  	if (!PMSG_IS_AUTO(msg)) {
>> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
>> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		if (status)
>>  			return status;
>>  	}
>> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  		}
>>  	} else {
>>  		hcd->state = old_state;
>> -		usb_phy_roothub_suspend(hcd->phy_roothub);
>> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>>  				"resume", status);
>>  		if (status != -ESHUTDOWN)
>>
>>
>> And the following to fix runtime issues
>>
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> index 2eca371..8598906 100644
>> --- a/drivers/usb/core/phy.c
>> +++ b/drivers/usb/core/phy.c
>> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>  			return PTR_ERR(phy);
>>  	}
>>  
>> -	roothub_entry = usb_phy_roothub_alloc(dev);
>> -	if (IS_ERR(roothub_entry))
>> -		return PTR_ERR(roothub_entry);
>> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +	if (!roothub_entry)
>> +		return -ENOMEM;
>>  
>>  	roothub_entry->phy = phy;
>>  
>> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>>  	usb_phy_roothub_power_off(phy_roothub);
>>  
>>  	/* keep the PHYs initialized so the device can wake up the system */
>> -	if (device_can_wakeup(dev))
>> +	if (device_may_wakeup(dev))
> It's ok

I had to additionally fix usb_phy_roothub_resume() from
	if (device_can_wakeup(dev))
to
	if (!device_may_wakeup(dev))

>>  		return 0;
>>  
>>  	return usb_phy_roothub_exit(phy_roothub);
>>
>>
>> Here are my obervations
>> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
>> low power states.
>>
>> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
>> and so phy_power_off won't be called either.
>>
>> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> You maybe use a wrong device.

Yes, after using the correct device I don't see the problem.

Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
Once you confirm it works for you I can send the 2 patches officially.

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..04cc453 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..25fe729 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);
@@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
 	int err;
 
 	/* if the device can't wake up the system _exit was called */
-	if (device_can_wakeup(dev)) {
+	if (!device_may_wakeup(dev)) {
 		err = usb_phy_roothub_init(phy_roothub);
 		if (err)
 			return err;
-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-22 12:41                                   ` Roger Quadros
  0 siblings, 0 replies; 115+ messages in thread
From: Roger Quadros @ 2018-03-22 12:41 UTC (permalink / raw)
  To: linus-amlogic

On 22/03/18 10:10, Chunfeng Yun wrote:
> Hi,
> On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
>> Martin,
>>
>> On 21/03/18 00:01, Martin Blumenstingl wrote:
>>> Hi Roger, Hi Chunfeng,
>>>
>>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>>> Hi Martin & Roger:
>>>>
>>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>>> Hi Roger,
>>>>>>>
>>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>>> +some TI folks
>>>>>>>>
>>>>>>>> Hi Martin,
>>>>>>>>
>>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>>
>>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>>> disable/enable them when required:
>>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>>
>>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>>
>>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>>
>>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>>> I'll explain why below.
>>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>>
>>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>>> I missed that, thanks
>>>>>
>>>>>>>>> ---
>>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>>
>>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>>> -usbcore-y += port.o
>>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>>
>>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>>> new file mode 100644
>>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>>> --- /dev/null
>>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>>> +/*
>>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>>> + *
>>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> + */
>>>>>>>>> +
>>>>>>>>> +#include <linux/device.h>
>>>>>>>>> +#include <linux/list.h>
>>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>>> +#include <linux/of.h>
>>>>>>>>> +
>>>>>>>>> +#include "phy.h"
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub {
>>>>>>>>> +     struct phy              *phy;
>>>>>>>>> +     struct list_head        list;
>>>>>>>>> +};
>>>>>>>>> +
>>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>>> +     if (!roothub_entry)
>>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>>> +
>>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>>> +
>>>>>>>>> +     return roothub_entry;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>>> +                                struct list_head *list)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>>> +
>>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>>> +                     return 0;
>>>>>>>>> +             else
>>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>>> +
>>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>>> +
>>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int i, num_phys, err;
>>>>>>>>> +
>>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>>> +                                           "#phy-cells");
>>>>>>>>> +     if (num_phys <= 0)
>>>>>>>>> +             return NULL;
>>>>>>>>> +
>>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>>> +             return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>>
>>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>>
>>>>>>
>>>>>> Yes.
>>>>>>
>>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>>> easier reading:
>>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>>> can avoid re-enumerating device."
>>>>>>>
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_exit_phys;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +err_exit_phys:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     return ERR_PTR(err);
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err, ret = 0;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     ret = ret;
>>>>>>>>> +     }
>>>>>>>>
>>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>>> Mediatek SoCs
>>>>>>>
>>>>>>
>>>>>> OK. I suppose that there are 2 cases
>>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>>> Remote wakeup probably required.
>>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>>> during suspend/resume to keep PHY link active.
>>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>>
>>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>>> ACK
>>>>>
>>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>>
>>>>>>>>> +
>>>>>>>>> +     return ret;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +     return err;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>>> +
>>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>
>>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>>> I'm not sure where this problem should be solved:
>>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>>> TI platforms
>>>>>>
>>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>>
>>>>>>> - fix this in the usb_phy_roothub code
>>>>>>
>>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>>> the controller is *NOT* marked as "wakeup-source"?
>>>>> I am also not sure if it would work, since the "wakeup-source"
>>>>> property is defined on the USB controller's parent node in case of the
>>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>>
>>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>>> devices by device_init_wakeup(dev, true),but not dependent on
>>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>>> decide whether call phy_exit()/init() or not.
>>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>>> yet, so I cannot test this
>>> based on this suggestion I threw up two patches which are *compile
>>> tested only* based on Greg's usb-next branch
>>> you can find them here: [0] (as well as attached to this mail)
>>>
>>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>>> @Roger: can you please test this on a TI SoC?
>>>
>>> (apologies in advance if these patches don't work)
>>>
>>> please note that I won't have access to my computer until Saturday.
>>> if these patches need to be rewritten/replaced/etc. then feel free to
>>> send your own version to the list
>>
>> Had to do the following to build
>>
>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>> index 6d4a419..2884607 100644
>> --- a/drivers/usb/core/hcd.c
>> +++ b/drivers/usb/core/hcd.c
>> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>>  		hcd->state = HC_STATE_SUSPENDED;
>>  
>>  		if (!PMSG_IS_AUTO(msg))
>> -			usb_phy_roothub_suspend(hcd->phy_roothub);
>> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> 
> Try to use hcd->self.controller instead of &rhdev->dev;

Actually it should be hcd->self.sysdev.
> 
>>  
>>  		/* Did we race with a root-hub wakeup event? */
>>  		if (rhdev->do_remote_wakeup) {
>> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  	}
>>  
>>  	if (!PMSG_IS_AUTO(msg)) {
>> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
>> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		if (status)
>>  			return status;
>>  	}
>> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  		}
>>  	} else {
>>  		hcd->state = old_state;
>> -		usb_phy_roothub_suspend(hcd->phy_roothub);
>> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>>  				"resume", status);
>>  		if (status != -ESHUTDOWN)
>>
>>
>> And the following to fix runtime issues
>>
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> index 2eca371..8598906 100644
>> --- a/drivers/usb/core/phy.c
>> +++ b/drivers/usb/core/phy.c
>> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>  			return PTR_ERR(phy);
>>  	}
>>  
>> -	roothub_entry = usb_phy_roothub_alloc(dev);
>> -	if (IS_ERR(roothub_entry))
>> -		return PTR_ERR(roothub_entry);
>> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +	if (!roothub_entry)
>> +		return -ENOMEM;
>>  
>>  	roothub_entry->phy = phy;
>>  
>> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>>  	usb_phy_roothub_power_off(phy_roothub);
>>  
>>  	/* keep the PHYs initialized so the device can wake up the system */
>> -	if (device_can_wakeup(dev))
>> +	if (device_may_wakeup(dev))
> It's ok

I had to additionally fix usb_phy_roothub_resume() from
	if (device_can_wakeup(dev))
to
	if (!device_may_wakeup(dev))

>>  		return 0;
>>  
>>  	return usb_phy_roothub_exit(phy_roothub);
>>
>>
>> Here are my obervations
>> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
>> low power states.
>>
>> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
>> and so phy_power_off won't be called either.
>>
>> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> You maybe use a wrong device.

Yes, after using the correct device I don't see the problem.

Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
Once you confirm it works for you I can send the 2 patches officially.

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..04cc453 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..25fe729 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);
@@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
 	int err;
 
 	/* if the device can't wake up the system _exit was called */
-	if (device_can_wakeup(dev)) {
+	if (!device_may_wakeup(dev)) {
 		err = usb_phy_roothub_init(phy_roothub);
 		if (err)
 			return err;
-- 
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-23  3:19                                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-23  3:19 UTC (permalink / raw)
  To: Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	Martin Blumenstingl, yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ,
	robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

On Thu, 2018-03-22 at 14:41 +0200, Roger Quadros wrote:
> On 22/03/18 10:10, Chunfeng Yun wrote:
> > Hi,
> > On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> >> Martin,
> >>
> >> On 21/03/18 00:01, Martin Blumenstingl wrote:
> >>> Hi Roger, Hi Chunfeng,
> >>>
> >>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
> >>>> Hi Martin & Roger:
> >>>>
> >>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>>>> Hi Roger,
> >>>>>>>
> >>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> >>>>>>>> +some TI folks
> >>>>>>>>
> >>>>>>>> Hi Martin,
> >>>>>>>>
> >>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>>>> disabled again on shutdown/suspend.
> >>>>>>>>>
> >>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>>>> disable/enable them when required:
> >>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>>>
> >>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>>>
> >>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
> >>>>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
> >>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >>>>>>>>
> >>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>>>> I'll explain why below.
> >>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>>>
> >>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>>>> I missed that, thanks
> >>>>>
> >>>>>>>>> ---
> >>>>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>>>
> >>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>>>> -usbcore-y += port.o
> >>>>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>>>
> >>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>>>> new file mode 100644
> >>>>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>>>> --- /dev/null
> >>>>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>>>> @@ -0,0 +1,158 @@
> >>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>>>> +/*
> >>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>>>> + *
> >>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> >>>>>>>>> + */
> >>>>>>>>> +
> >>>>>>>>> +#include <linux/device.h>
> >>>>>>>>> +#include <linux/list.h>
> >>>>>>>>> +#include <linux/phy/phy.h>
> >>>>>>>>> +#include <linux/of.h>
> >>>>>>>>> +
> >>>>>>>>> +#include "phy.h"
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub {
> >>>>>>>>> +     struct phy              *phy;
> >>>>>>>>> +     struct list_head        list;
> >>>>>>>>> +};
> >>>>>>>>> +
> >>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>>>> +     if (!roothub_entry)
> >>>>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>>>> +
> >>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>>>> +
> >>>>>>>>> +     return roothub_entry;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>>>> +                                struct list_head *list)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>>>> +
> >>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>>>> +                     return 0;
> >>>>>>>>> +             else
> >>>>>>>>> +                     return PTR_ERR(phy);
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry->phy = phy;
> >>>>>>>>> +
> >>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int i, num_phys, err;
> >>>>>>>>> +
> >>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>>>> +                                           "#phy-cells");
> >>>>>>>>> +     if (num_phys <= 0)
> >>>>>>>>> +             return NULL;
> >>>>>>>>> +
> >>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>>>> +             return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>>>
> >>>>>>
> >>>>>> Yes.
> >>>>>>
> >>>>>>> an earlier version of my patch did exactly this, but it caused
> >>>>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>>>> easier reading:
> >>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>>>> can avoid re-enumerating device."
> >>>>>>>
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_exit_phys;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +err_exit_phys:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     return ERR_PTR(err);
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err, ret = 0;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     ret = ret;
> >>>>>>>>> +     }
> >>>>>>>>
> >>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>>>> if I understood Chunfeng Yun correctly this will require
> >>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>>>> Mediatek SoCs
> >>>>>>>
> >>>>>>
> >>>>>> OK. I suppose that there are 2 cases
> >>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>>>> Remote wakeup probably required.
> >>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>>>> during suspend/resume to keep PHY link active.
> >>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>>>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>>>
> >>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>>>> ACK
> >>>>>
> >>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>>>
> >>>>>>>>> +
> >>>>>>>>> +     return ret;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +     return err;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>>>> +
> >>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>>>> we're no longer able to reach low power states on system suspend.
> >>>>>>> I'm not sure where this problem should be solved:
> >>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>>>> TI platforms
> >>>>>>
> >>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>>>
> >>>>>>> - fix this in the usb_phy_roothub code
> >>>>>>
> >>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>>>> the controller is *NOT* marked as "wakeup-source"?
> >>>>> I am also not sure if it would work, since the "wakeup-source"
> >>>>> property is defined on the USB controller's parent node in case of the
> >>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>>>
> >>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >>>> devices by device_init_wakeup(dev, true),but not dependent on
> >>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >>>> decide whether call phy_exit()/init() or not.
> >>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> >>> yet, so I cannot test this
> >>> based on this suggestion I threw up two patches which are *compile
> >>> tested only* based on Greg's usb-next branch
> >>> you can find them here: [0] (as well as attached to this mail)
> >>>
> >>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> >>> @Roger: can you please test this on a TI SoC?
> >>>
> >>> (apologies in advance if these patches don't work)
> >>>
> >>> please note that I won't have access to my computer until Saturday.
> >>> if these patches need to be rewritten/replaced/etc. then feel free to
> >>> send your own version to the list
> >>
> >> Had to do the following to build
> >>
> >> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> >> index 6d4a419..2884607 100644
> >> --- a/drivers/usb/core/hcd.c
> >> +++ b/drivers/usb/core/hcd.c
> >> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
> >>  		hcd->state = HC_STATE_SUSPENDED;
> >>  
> >>  		if (!PMSG_IS_AUTO(msg))
> >> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > 
> > Try to use hcd->self.controller instead of &rhdev->dev;
> 
> Actually it should be hcd->self.sysdev.
I find that xhci-plat.c enables wakeup by hcd->self.controller which is
different from hcd->self.sysdev sometimes.

And hcd->self.controller is the same as hcd->self.sysdev on MTK's
platform.

> > 
> >>  
> >>  		/* Did we race with a root-hub wakeup event? */
> >>  		if (rhdev->do_remote_wakeup) {
> >> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  	}
> >>  
> >>  	if (!PMSG_IS_AUTO(msg)) {
> >> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> >> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		if (status)
> >>  			return status;
> >>  	}
> >> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  		}
> >>  	} else {
> >>  		hcd->state = old_state;
> >> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
> >>  				"resume", status);
> >>  		if (status != -ESHUTDOWN)
> >>
> >>
> >> And the following to fix runtime issues
> >>
> >> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >> index 2eca371..8598906 100644
> >> --- a/drivers/usb/core/phy.c
> >> +++ b/drivers/usb/core/phy.c
> >> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>  			return PTR_ERR(phy);
> >>  	}
> >>  
> >> -	roothub_entry = usb_phy_roothub_alloc(dev);
> >> -	if (IS_ERR(roothub_entry))
> >> -		return PTR_ERR(roothub_entry);
> >> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >> +	if (!roothub_entry)
> >> +		return -ENOMEM;
> >>  
> >>  	roothub_entry->phy = phy;
> >>  
> >> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
> >>  	usb_phy_roothub_power_off(phy_roothub);
> >>  
> >>  	/* keep the PHYs initialized so the device can wake up the system */
> >> -	if (device_can_wakeup(dev))
> >> +	if (device_may_wakeup(dev))
> > It's ok
> 
> I had to additionally fix usb_phy_roothub_resume() from
> 	if (device_can_wakeup(dev))
> to
> 	if (!device_may_wakeup(dev))
> 
> >>  		return 0;
> >>  
> >>  	return usb_phy_roothub_exit(phy_roothub);
> >>
> >>
> >> Here are my obervations
> >> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> >> low power states.
> >>
> >> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> >> and so phy_power_off won't be called either.
> >>
> >> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> > You maybe use a wrong device.
> 
> Yes, after using the correct device I don't see the problem.
> 
> Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
> Once you confirm it works for you I can send the 2 patches officially.
It works on MTK's platform, I'll test it again when official patches are
sent out.

Thanks a lot
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..04cc453 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..25fe729 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> @@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
>  	int err;
>  
>  	/* if the device can't wake up the system _exit was called */
> -	if (device_can_wakeup(dev)) {
> +	if (!device_may_wakeup(dev)) {
>  		err = usb_phy_roothub_init(phy_roothub);
>  		if (err)
>  			return err;

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-23  3:19                                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-23  3:19 UTC (permalink / raw)
  To: Roger Quadros
  Cc: Martin Blumenstingl, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

On Thu, 2018-03-22 at 14:41 +0200, Roger Quadros wrote:
> On 22/03/18 10:10, Chunfeng Yun wrote:
> > Hi,
> > On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> >> Martin,
> >>
> >> On 21/03/18 00:01, Martin Blumenstingl wrote:
> >>> Hi Roger, Hi Chunfeng,
> >>>
> >>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >>>> Hi Martin & Roger:
> >>>>
> >>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>>>> Hi Roger,
> >>>>>>>
> >>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>>>> +some TI folks
> >>>>>>>>
> >>>>>>>> Hi Martin,
> >>>>>>>>
> >>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>>>> disabled again on shutdown/suspend.
> >>>>>>>>>
> >>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>>>> disable/enable them when required:
> >>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>>>
> >>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>>>
> >>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>>>
> >>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>>>> I'll explain why below.
> >>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>>>
> >>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>>>> I missed that, thanks
> >>>>>
> >>>>>>>>> ---
> >>>>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>>>
> >>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>>>> -usbcore-y += port.o
> >>>>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>>>
> >>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>>>> new file mode 100644
> >>>>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>>>> --- /dev/null
> >>>>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>>>> @@ -0,0 +1,158 @@
> >>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>>>> +/*
> >>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>>>> + *
> >>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> + */
> >>>>>>>>> +
> >>>>>>>>> +#include <linux/device.h>
> >>>>>>>>> +#include <linux/list.h>
> >>>>>>>>> +#include <linux/phy/phy.h>
> >>>>>>>>> +#include <linux/of.h>
> >>>>>>>>> +
> >>>>>>>>> +#include "phy.h"
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub {
> >>>>>>>>> +     struct phy              *phy;
> >>>>>>>>> +     struct list_head        list;
> >>>>>>>>> +};
> >>>>>>>>> +
> >>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>>>> +     if (!roothub_entry)
> >>>>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>>>> +
> >>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>>>> +
> >>>>>>>>> +     return roothub_entry;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>>>> +                                struct list_head *list)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>>>> +
> >>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>>>> +                     return 0;
> >>>>>>>>> +             else
> >>>>>>>>> +                     return PTR_ERR(phy);
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry->phy = phy;
> >>>>>>>>> +
> >>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int i, num_phys, err;
> >>>>>>>>> +
> >>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>>>> +                                           "#phy-cells");
> >>>>>>>>> +     if (num_phys <= 0)
> >>>>>>>>> +             return NULL;
> >>>>>>>>> +
> >>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>>>> +             return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>>>
> >>>>>>
> >>>>>> Yes.
> >>>>>>
> >>>>>>> an earlier version of my patch did exactly this, but it caused
> >>>>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>>>> easier reading:
> >>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>>>> can avoid re-enumerating device."
> >>>>>>>
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_exit_phys;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +err_exit_phys:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     return ERR_PTR(err);
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err, ret = 0;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     ret = ret;
> >>>>>>>>> +     }
> >>>>>>>>
> >>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>>>> if I understood Chunfeng Yun correctly this will require
> >>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>>>> Mediatek SoCs
> >>>>>>>
> >>>>>>
> >>>>>> OK. I suppose that there are 2 cases
> >>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>>>> Remote wakeup probably required.
> >>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>>>> during suspend/resume to keep PHY link active.
> >>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>>>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>>>
> >>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>>>> ACK
> >>>>>
> >>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>>>
> >>>>>>>>> +
> >>>>>>>>> +     return ret;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +     return err;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>>>> +
> >>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>>>> we're no longer able to reach low power states on system suspend.
> >>>>>>> I'm not sure where this problem should be solved:
> >>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>>>> TI platforms
> >>>>>>
> >>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>>>
> >>>>>>> - fix this in the usb_phy_roothub code
> >>>>>>
> >>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>>>> the controller is *NOT* marked as "wakeup-source"?
> >>>>> I am also not sure if it would work, since the "wakeup-source"
> >>>>> property is defined on the USB controller's parent node in case of the
> >>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>>>
> >>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >>>> devices by device_init_wakeup(dev, true),but not dependent on
> >>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >>>> decide whether call phy_exit()/init() or not.
> >>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> >>> yet, so I cannot test this
> >>> based on this suggestion I threw up two patches which are *compile
> >>> tested only* based on Greg's usb-next branch
> >>> you can find them here: [0] (as well as attached to this mail)
> >>>
> >>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> >>> @Roger: can you please test this on a TI SoC?
> >>>
> >>> (apologies in advance if these patches don't work)
> >>>
> >>> please note that I won't have access to my computer until Saturday.
> >>> if these patches need to be rewritten/replaced/etc. then feel free to
> >>> send your own version to the list
> >>
> >> Had to do the following to build
> >>
> >> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> >> index 6d4a419..2884607 100644
> >> --- a/drivers/usb/core/hcd.c
> >> +++ b/drivers/usb/core/hcd.c
> >> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
> >>  		hcd->state = HC_STATE_SUSPENDED;
> >>  
> >>  		if (!PMSG_IS_AUTO(msg))
> >> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > 
> > Try to use hcd->self.controller instead of &rhdev->dev;
> 
> Actually it should be hcd->self.sysdev.
I find that xhci-plat.c enables wakeup by hcd->self.controller which is
different from hcd->self.sysdev sometimes.

And hcd->self.controller is the same as hcd->self.sysdev on MTK's
platform.

> > 
> >>  
> >>  		/* Did we race with a root-hub wakeup event? */
> >>  		if (rhdev->do_remote_wakeup) {
> >> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  	}
> >>  
> >>  	if (!PMSG_IS_AUTO(msg)) {
> >> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> >> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		if (status)
> >>  			return status;
> >>  	}
> >> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  		}
> >>  	} else {
> >>  		hcd->state = old_state;
> >> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
> >>  				"resume", status);
> >>  		if (status != -ESHUTDOWN)
> >>
> >>
> >> And the following to fix runtime issues
> >>
> >> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >> index 2eca371..8598906 100644
> >> --- a/drivers/usb/core/phy.c
> >> +++ b/drivers/usb/core/phy.c
> >> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>  			return PTR_ERR(phy);
> >>  	}
> >>  
> >> -	roothub_entry = usb_phy_roothub_alloc(dev);
> >> -	if (IS_ERR(roothub_entry))
> >> -		return PTR_ERR(roothub_entry);
> >> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >> +	if (!roothub_entry)
> >> +		return -ENOMEM;
> >>  
> >>  	roothub_entry->phy = phy;
> >>  
> >> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
> >>  	usb_phy_roothub_power_off(phy_roothub);
> >>  
> >>  	/* keep the PHYs initialized so the device can wake up the system */
> >> -	if (device_can_wakeup(dev))
> >> +	if (device_may_wakeup(dev))
> > It's ok
> 
> I had to additionally fix usb_phy_roothub_resume() from
> 	if (device_can_wakeup(dev))
> to
> 	if (!device_may_wakeup(dev))
> 
> >>  		return 0;
> >>  
> >>  	return usb_phy_roothub_exit(phy_roothub);
> >>
> >>
> >> Here are my obervations
> >> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> >> low power states.
> >>
> >> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> >> and so phy_power_off won't be called either.
> >>
> >> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> > You maybe use a wrong device.
> 
> Yes, after using the correct device I don't see the problem.
> 
> Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
> Once you confirm it works for you I can send the 2 patches officially.
It works on MTK's platform, I'll test it again when official patches are
sent out.

Thanks a lot
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..04cc453 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..25fe729 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> @@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
>  	int err;
>  
>  	/* if the device can't wake up the system _exit was called */
> -	if (device_can_wakeup(dev)) {
> +	if (!device_may_wakeup(dev)) {
>  		err = usb_phy_roothub_init(phy_roothub);
>  		if (err)
>  			return err;
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-23  3:19                                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-23  3:19 UTC (permalink / raw)
  To: linux-arm-kernel

On Thu, 2018-03-22 at 14:41 +0200, Roger Quadros wrote:
> On 22/03/18 10:10, Chunfeng Yun wrote:
> > Hi,
> > On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> >> Martin,
> >>
> >> On 21/03/18 00:01, Martin Blumenstingl wrote:
> >>> Hi Roger, Hi Chunfeng,
> >>>
> >>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >>>> Hi Martin & Roger:
> >>>>
> >>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>>>> Hi Roger,
> >>>>>>>
> >>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>>>> +some TI folks
> >>>>>>>>
> >>>>>>>> Hi Martin,
> >>>>>>>>
> >>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>>>> disabled again on shutdown/suspend.
> >>>>>>>>>
> >>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>>>> disable/enable them when required:
> >>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>>>
> >>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>>>
> >>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>>>
> >>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>>>> I'll explain why below.
> >>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>>>
> >>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>>>> I missed that, thanks
> >>>>>
> >>>>>>>>> ---
> >>>>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>>>
> >>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>>>> -usbcore-y += port.o
> >>>>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>>>
> >>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>>>> new file mode 100644
> >>>>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>>>> --- /dev/null
> >>>>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>>>> @@ -0,0 +1,158 @@
> >>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>>>> +/*
> >>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>>>> + *
> >>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> + */
> >>>>>>>>> +
> >>>>>>>>> +#include <linux/device.h>
> >>>>>>>>> +#include <linux/list.h>
> >>>>>>>>> +#include <linux/phy/phy.h>
> >>>>>>>>> +#include <linux/of.h>
> >>>>>>>>> +
> >>>>>>>>> +#include "phy.h"
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub {
> >>>>>>>>> +     struct phy              *phy;
> >>>>>>>>> +     struct list_head        list;
> >>>>>>>>> +};
> >>>>>>>>> +
> >>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>>>> +     if (!roothub_entry)
> >>>>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>>>> +
> >>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>>>> +
> >>>>>>>>> +     return roothub_entry;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>>>> +                                struct list_head *list)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>>>> +
> >>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>>>> +                     return 0;
> >>>>>>>>> +             else
> >>>>>>>>> +                     return PTR_ERR(phy);
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry->phy = phy;
> >>>>>>>>> +
> >>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int i, num_phys, err;
> >>>>>>>>> +
> >>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>>>> +                                           "#phy-cells");
> >>>>>>>>> +     if (num_phys <= 0)
> >>>>>>>>> +             return NULL;
> >>>>>>>>> +
> >>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>>>> +             return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>>>
> >>>>>>
> >>>>>> Yes.
> >>>>>>
> >>>>>>> an earlier version of my patch did exactly this, but it caused
> >>>>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>>>> easier reading:
> >>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>>>> can avoid re-enumerating device."
> >>>>>>>
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_exit_phys;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +err_exit_phys:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     return ERR_PTR(err);
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err, ret = 0;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     ret = ret;
> >>>>>>>>> +     }
> >>>>>>>>
> >>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>>>> if I understood Chunfeng Yun correctly this will require
> >>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>>>> Mediatek SoCs
> >>>>>>>
> >>>>>>
> >>>>>> OK. I suppose that there are 2 cases
> >>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>>>> Remote wakeup probably required.
> >>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>>>> during suspend/resume to keep PHY link active.
> >>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>>>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>>>
> >>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>>>> ACK
> >>>>>
> >>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>>>
> >>>>>>>>> +
> >>>>>>>>> +     return ret;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +     return err;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>>>> +
> >>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>>>> we're no longer able to reach low power states on system suspend.
> >>>>>>> I'm not sure where this problem should be solved:
> >>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>>>> TI platforms
> >>>>>>
> >>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>>>
> >>>>>>> - fix this in the usb_phy_roothub code
> >>>>>>
> >>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>>>> the controller is *NOT* marked as "wakeup-source"?
> >>>>> I am also not sure if it would work, since the "wakeup-source"
> >>>>> property is defined on the USB controller's parent node in case of the
> >>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>>>
> >>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >>>> devices by device_init_wakeup(dev, true),but not dependent on
> >>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >>>> decide whether call phy_exit()/init() or not.
> >>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> >>> yet, so I cannot test this
> >>> based on this suggestion I threw up two patches which are *compile
> >>> tested only* based on Greg's usb-next branch
> >>> you can find them here: [0] (as well as attached to this mail)
> >>>
> >>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> >>> @Roger: can you please test this on a TI SoC?
> >>>
> >>> (apologies in advance if these patches don't work)
> >>>
> >>> please note that I won't have access to my computer until Saturday.
> >>> if these patches need to be rewritten/replaced/etc. then feel free to
> >>> send your own version to the list
> >>
> >> Had to do the following to build
> >>
> >> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> >> index 6d4a419..2884607 100644
> >> --- a/drivers/usb/core/hcd.c
> >> +++ b/drivers/usb/core/hcd.c
> >> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
> >>  		hcd->state = HC_STATE_SUSPENDED;
> >>  
> >>  		if (!PMSG_IS_AUTO(msg))
> >> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > 
> > Try to use hcd->self.controller instead of &rhdev->dev;
> 
> Actually it should be hcd->self.sysdev.
I find that xhci-plat.c enables wakeup by hcd->self.controller which is
different from hcd->self.sysdev sometimes.

And hcd->self.controller is the same as hcd->self.sysdev on MTK's
platform.

> > 
> >>  
> >>  		/* Did we race with a root-hub wakeup event? */
> >>  		if (rhdev->do_remote_wakeup) {
> >> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  	}
> >>  
> >>  	if (!PMSG_IS_AUTO(msg)) {
> >> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> >> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		if (status)
> >>  			return status;
> >>  	}
> >> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  		}
> >>  	} else {
> >>  		hcd->state = old_state;
> >> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
> >>  				"resume", status);
> >>  		if (status != -ESHUTDOWN)
> >>
> >>
> >> And the following to fix runtime issues
> >>
> >> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >> index 2eca371..8598906 100644
> >> --- a/drivers/usb/core/phy.c
> >> +++ b/drivers/usb/core/phy.c
> >> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>  			return PTR_ERR(phy);
> >>  	}
> >>  
> >> -	roothub_entry = usb_phy_roothub_alloc(dev);
> >> -	if (IS_ERR(roothub_entry))
> >> -		return PTR_ERR(roothub_entry);
> >> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >> +	if (!roothub_entry)
> >> +		return -ENOMEM;
> >>  
> >>  	roothub_entry->phy = phy;
> >>  
> >> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
> >>  	usb_phy_roothub_power_off(phy_roothub);
> >>  
> >>  	/* keep the PHYs initialized so the device can wake up the system */
> >> -	if (device_can_wakeup(dev))
> >> +	if (device_may_wakeup(dev))
> > It's ok
> 
> I had to additionally fix usb_phy_roothub_resume() from
> 	if (device_can_wakeup(dev))
> to
> 	if (!device_may_wakeup(dev))
> 
> >>  		return 0;
> >>  
> >>  	return usb_phy_roothub_exit(phy_roothub);
> >>
> >>
> >> Here are my obervations
> >> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> >> low power states.
> >>
> >> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> >> and so phy_power_off won't be called either.
> >>
> >> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> > You maybe use a wrong device.
> 
> Yes, after using the correct device I don't see the problem.
> 
> Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
> Once you confirm it works for you I can send the 2 patches officially.
It works on MTK's platform, I'll test it again when official patches are
sent out.

Thanks a lot
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..04cc453 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..25fe729 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> @@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
>  	int err;
>  
>  	/* if the device can't wake up the system _exit was called */
> -	if (device_can_wakeup(dev)) {
> +	if (!device_may_wakeup(dev)) {
>  		err = usb_phy_roothub_init(phy_roothub);
>  		if (err)
>  			return err;

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-23  3:19                                       ` Chunfeng Yun
  0 siblings, 0 replies; 115+ messages in thread
From: Chunfeng Yun @ 2018-03-23  3:19 UTC (permalink / raw)
  To: linus-amlogic

On Thu, 2018-03-22 at 14:41 +0200, Roger Quadros wrote:
> On 22/03/18 10:10, Chunfeng Yun wrote:
> > Hi,
> > On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> >> Martin,
> >>
> >> On 21/03/18 00:01, Martin Blumenstingl wrote:
> >>> Hi Roger, Hi Chunfeng,
> >>>
> >>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >>>> Hi Martin & Roger:
> >>>>
> >>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>>>> Hi Roger,
> >>>>>>>
> >>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>>>> +some TI folks
> >>>>>>>>
> >>>>>>>> Hi Martin,
> >>>>>>>>
> >>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>>>> disabled again on shutdown/suspend.
> >>>>>>>>>
> >>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>>>> disable/enable them when required:
> >>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>>>
> >>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>>>
> >>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>>>
> >>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>>>> I'll explain why below.
> >>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>>>
> >>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>>>> I missed that, thanks
> >>>>>
> >>>>>>>>> ---
> >>>>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>>>
> >>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>>>> @@ -6,7 +6,7 @@
> >>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
> >>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
> >>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
> >>>>>>>>> -usbcore-y += port.o
> >>>>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>>>
> >>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>>>> new file mode 100644
> >>>>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>>>> --- /dev/null
> >>>>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>>>> @@ -0,0 +1,158 @@
> >>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>>>> +/*
> >>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>>>> + *
> >>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> + */
> >>>>>>>>> +
> >>>>>>>>> +#include <linux/device.h>
> >>>>>>>>> +#include <linux/list.h>
> >>>>>>>>> +#include <linux/phy/phy.h>
> >>>>>>>>> +#include <linux/of.h>
> >>>>>>>>> +
> >>>>>>>>> +#include "phy.h"
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub {
> >>>>>>>>> +     struct phy              *phy;
> >>>>>>>>> +     struct list_head        list;
> >>>>>>>>> +};
> >>>>>>>>> +
> >>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>>>> +     if (!roothub_entry)
> >>>>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>>>> +
> >>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>>>> +
> >>>>>>>>> +     return roothub_entry;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>>>> +                                struct list_head *list)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>>>> +
> >>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>>>> +                     return 0;
> >>>>>>>>> +             else
> >>>>>>>>> +                     return PTR_ERR(phy);
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry->phy = phy;
> >>>>>>>>> +
> >>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int i, num_phys, err;
> >>>>>>>>> +
> >>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>>>> +                                           "#phy-cells");
> >>>>>>>>> +     if (num_phys <= 0)
> >>>>>>>>> +             return NULL;
> >>>>>>>>> +
> >>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>>>> +             return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>>>
> >>>>>>
> >>>>>> Yes.
> >>>>>>
> >>>>>>> an earlier version of my patch did exactly this, but it caused
> >>>>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>>>> easier reading:
> >>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>>>> can avoid re-enumerating device."
> >>>>>>>
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_exit_phys;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +err_exit_phys:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     return ERR_PTR(err);
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err, ret = 0;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     ret = ret;
> >>>>>>>>> +     }
> >>>>>>>>
> >>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>>>> if I understood Chunfeng Yun correctly this will require
> >>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>>>> Mediatek SoCs
> >>>>>>>
> >>>>>>
> >>>>>> OK. I suppose that there are 2 cases
> >>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>>>> Remote wakeup probably required.
> >>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>>>> during suspend/resume to keep PHY link active.
> >>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>>>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>>>
> >>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>>>> ACK
> >>>>>
> >>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>>>
> >>>>>>>>> +
> >>>>>>>>> +     return ret;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +     return err;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>>>> +
> >>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>>>> we're no longer able to reach low power states on system suspend.
> >>>>>>> I'm not sure where this problem should be solved:
> >>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>>>> TI platforms
> >>>>>>
> >>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>>>
> >>>>>>> - fix this in the usb_phy_roothub code
> >>>>>>
> >>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>>>> the controller is *NOT* marked as "wakeup-source"?
> >>>>> I am also not sure if it would work, since the "wakeup-source"
> >>>>> property is defined on the USB controller's parent node in case of the
> >>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>>>
> >>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >>>> devices by device_init_wakeup(dev, true),but not dependent on
> >>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >>>> decide whether call phy_exit()/init() or not.
> >>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> >>> yet, so I cannot test this
> >>> based on this suggestion I threw up two patches which are *compile
> >>> tested only* based on Greg's usb-next branch
> >>> you can find them here: [0] (as well as attached to this mail)
> >>>
> >>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> >>> @Roger: can you please test this on a TI SoC?
> >>>
> >>> (apologies in advance if these patches don't work)
> >>>
> >>> please note that I won't have access to my computer until Saturday.
> >>> if these patches need to be rewritten/replaced/etc. then feel free to
> >>> send your own version to the list
> >>
> >> Had to do the following to build
> >>
> >> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> >> index 6d4a419..2884607 100644
> >> --- a/drivers/usb/core/hcd.c
> >> +++ b/drivers/usb/core/hcd.c
> >> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
> >>  		hcd->state = HC_STATE_SUSPENDED;
> >>  
> >>  		if (!PMSG_IS_AUTO(msg))
> >> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > 
> > Try to use hcd->self.controller instead of &rhdev->dev;
> 
> Actually it should be hcd->self.sysdev.
I find that xhci-plat.c enables wakeup by hcd->self.controller which is
different from hcd->self.sysdev sometimes.

And hcd->self.controller is the same as hcd->self.sysdev on MTK's
platform.

> > 
> >>  
> >>  		/* Did we race with a root-hub wakeup event? */
> >>  		if (rhdev->do_remote_wakeup) {
> >> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  	}
> >>  
> >>  	if (!PMSG_IS_AUTO(msg)) {
> >> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> >> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		if (status)
> >>  			return status;
> >>  	}
> >> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  		}
> >>  	} else {
> >>  		hcd->state = old_state;
> >> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
> >>  				"resume", status);
> >>  		if (status != -ESHUTDOWN)
> >>
> >>
> >> And the following to fix runtime issues
> >>
> >> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >> index 2eca371..8598906 100644
> >> --- a/drivers/usb/core/phy.c
> >> +++ b/drivers/usb/core/phy.c
> >> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>  			return PTR_ERR(phy);
> >>  	}
> >>  
> >> -	roothub_entry = usb_phy_roothub_alloc(dev);
> >> -	if (IS_ERR(roothub_entry))
> >> -		return PTR_ERR(roothub_entry);
> >> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >> +	if (!roothub_entry)
> >> +		return -ENOMEM;
> >>  
> >>  	roothub_entry->phy = phy;
> >>  
> >> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
> >>  	usb_phy_roothub_power_off(phy_roothub);
> >>  
> >>  	/* keep the PHYs initialized so the device can wake up the system */
> >> -	if (device_can_wakeup(dev))
> >> +	if (device_may_wakeup(dev))
> > It's ok
> 
> I had to additionally fix usb_phy_roothub_resume() from
> 	if (device_can_wakeup(dev))
> to
> 	if (!device_may_wakeup(dev))
> 
> >>  		return 0;
> >>  
> >>  	return usb_phy_roothub_exit(phy_roothub);
> >>
> >>
> >> Here are my obervations
> >> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> >> low power states.
> >>
> >> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> >> and so phy_power_off won't be called either.
> >>
> >> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> > You maybe use a wrong device.
> 
> Yes, after using the correct device I don't see the problem.
> 
> Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
> Once you confirm it works for you I can send the 2 patches officially.
It works on MTK's platform, I'll test it again when official patches are
sent out.

Thanks a lot
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..04cc453 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..25fe729 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> @@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
>  	int err;
>  
>  	/* if the device can't wake up the system _exit was called */
> -	if (device_can_wakeup(dev)) {
> +	if (!device_may_wakeup(dev)) {
>  		err = usb_phy_roothub_init(phy_roothub);
>  		if (err)
>  			return err;

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-24 11:23                                 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-24 11:23 UTC (permalink / raw)
  To: Roger Quadros
  Cc: mark.rutland-5wv7dgnIgG8, Peter.Chen-3arQi8VN3Tc, Neil Armstrong,
	matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w,
	thierry.reding-Re5JQEeQqe8AvxtiuMwx3w, KISHON VIJAY ABRAHAM,
	linux-ci5G2KO2hbZ+pU9mqzGVBQ,
	felipe.balbi-VuQAYsv1563Yd54FQh9/CA,
	jonathanh-DDmLM1+adcrQT0dZR+AlfA,
	stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz, Chunfeng Yun,
	devicetree-u79uwXL29TY76Z2rM5mHXA, arnd-r2nGTMty4D4,
	yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ, robh+dt-DgEjT+Ai2ygdnm+yROfE0A,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	linux-amlogic-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-omap-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Gerlach, Dave,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r,
	linux-usb-u79uwXL29TY76Z2rM5mHXA,
	mathias.nyman-ral2JQCrhuEAvxtiuMwx3w, Keerthy

Hi Roger,

On Wed, Mar 21, 2018 at 12:30 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
> Martin,
>
> On 21/03/18 00:01, Martin Blumenstingl wrote:
>> Hi Roger, Hi Chunfeng,
>>
>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
>>> Hi Martin & Roger:
>>>
>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>> Hi,
>>>>>
>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>> Hi Roger,
>>>>>>
>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq-l0cyMroinI0@public.gmane.org> wrote:
>>>>>>> +some TI folks
>>>>>>>
>>>>>>> Hi Martin,
>>>>>>>
>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>
>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>> disable/enable them when required:
>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>
>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>
>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>>> Tested-by: Yixun Lan <yixun.lan-LpR1jeaWuhtBDgjK7y7TUQ@public.gmane.org>
>>>>>>>> Cc: Neil Armstrong <narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
>>>>>>>
>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>> I'll explain why below.
>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>
>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>> I missed that, thanks
>>>>
>>>>>>>> ---
>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>
>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>> -usbcore-y += port.o
>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>
>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>> +/*
>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>> + *
>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
>>>>>>>> + */
>>>>>>>> +
>>>>>>>> +#include <linux/device.h>
>>>>>>>> +#include <linux/list.h>
>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>> +#include <linux/of.h>
>>>>>>>> +
>>>>>>>> +#include "phy.h"
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub {
>>>>>>>> +     struct phy              *phy;
>>>>>>>> +     struct list_head        list;
>>>>>>>> +};
>>>>>>>> +
>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>> +     if (!roothub_entry)
>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>> +
>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>> +
>>>>>>>> +     return roothub_entry;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>> +                                struct list_head *list)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>> +
>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>> +                     return 0;
>>>>>>>> +             else
>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>> +
>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>> +
>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int i, num_phys, err;
>>>>>>>> +
>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>> +                                           "#phy-cells");
>>>>>>>> +     if (num_phys <= 0)
>>>>>>>> +             return NULL;
>>>>>>>> +
>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>> +             return phy_roothub;
>>>>>>>> +
>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>
>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>
>>>>>
>>>>> Yes.
>>>>>
>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>> easier reading:
>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>> can avoid re-enumerating device."
>>>>>>
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_exit_phys;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return phy_roothub;
>>>>>>>> +
>>>>>>>> +err_exit_phys:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     return ERR_PTR(err);
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err, ret = 0;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     ret = ret;
>>>>>>>> +     }
>>>>>>>
>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>> Mediatek SoCs
>>>>>>
>>>>>
>>>>> OK. I suppose that there are 2 cases
>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>> Remote wakeup probably required.
>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>> during suspend/resume to keep PHY link active.
>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>
>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>> ACK
>>>>
>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>
>>>>>>>> +
>>>>>>>> +     return ret;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +     return err;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>> +
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>
>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>> I'm not sure where this problem should be solved:
>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>> TI platforms
>>>>>
>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>
>>>>>> - fix this in the usb_phy_roothub code
>>>>>
>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>> the controller is *NOT* marked as "wakeup-source"?
>>>> I am also not sure if it would work, since the "wakeup-source"
>>>> property is defined on the USB controller's parent node in case of the
>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>
>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>> devices by device_init_wakeup(dev, true),but not dependent on
>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>> decide whether call phy_exit()/init() or not.
>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>> yet, so I cannot test this
>> based on this suggestion I threw up two patches which are *compile
>> tested only* based on Greg's usb-next branch
>> you can find them here: [0] (as well as attached to this mail)
>>
>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>> @Roger: can you please test this on a TI SoC?
>>
>> (apologies in advance if these patches don't work)
>>
>> please note that I won't have access to my computer until Saturday.
>> if these patches need to be rewritten/replaced/etc. then feel free to
>> send your own version to the list
>
> Had to do the following to build
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>                 hcd->state = HC_STATE_SUSPENDED;
>
>                 if (!PMSG_IS_AUTO(msg))
> -                       usb_phy_roothub_suspend(hcd->phy_roothub);
> +                       usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>
>                 /* Did we race with a root-hub wakeup event? */
>                 if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>         }
>
>         if (!PMSG_IS_AUTO(msg)) {
> -               status = usb_phy_roothub_resume(hcd->phy_roothub);
> +               status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
>                 if (status)
>                         return status;
>         }
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>                 }
>         } else {
>                 hcd->state = old_state;
> -               usb_phy_roothub_suspend(hcd->phy_roothub);
> +               usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>                 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>                                 "resume", status);
>                 if (status != -ESHUTDOWN)
>
>
> And the following to fix runtime issues
>
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>                         return PTR_ERR(phy);
>         }
>
> -       roothub_entry = usb_phy_roothub_alloc(dev);
> -       if (IS_ERR(roothub_entry))
> -               return PTR_ERR(roothub_entry);
> +       roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +       if (!roothub_entry)
> +               return -ENOMEM;
>
>         roothub_entry->phy = phy;
>
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>         usb_phy_roothub_power_off(phy_roothub);
>
>         /* keep the PHYs initialized so the device can wake up the system */
> -       if (device_can_wakeup(dev))
> +       if (device_may_wakeup(dev))
>                 return 0;
>
>         return usb_phy_roothub_exit(phy_roothub);
>
>
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
>
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
>
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
>
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
>
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
>
> [1] - https://patchwork.kernel.org/patch/10298641/
>
> What options do we have now to fix the original issue?
>
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
>
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?
I added this code to add support for the USB controller on the Amlogic
Meson GXL and GXM SoCs:
these use a dwc3 controller for host mode

so while this approach would reduce the risk of breaking other
controllers your TI SoCs would still be "affected" since I would like
to enable the new logic for the dwc3 controller (which the TI SoCs use
as well).

I will come up with an RFC patch series based on your and Chunfeng's suggestions
thank you very much for working it out while I was away!

>>
>>>>>> - fix this in the PHY driver
>>>>>
>>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>>> - assuming we define two new callbacks: .suspend and .resume
>>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>>> - the PHY framework could call .power_on by default if .resume is not defined
>>>> - drivers could set .suspend and .resume on their own, allowing more
>>>> fine-grained control by for example *only* stopping the clock (but not
>>>> re-initializing the registers, etc.)
>>>>
>>>> @Roger: what do you think about this?
>>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>>> hear his opinion too
>>>>
>>>>>> - somewhere else
>>>>>>
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>>> @@ -0,0 +1,7 @@
>>>>>>>> +struct usb_phy_roothub;
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>>
>>>>>>>
>>>>>
>
>>
>> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-24 11:23                                 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-24 11:23 UTC (permalink / raw)
  To: Roger Quadros
  Cc: Chunfeng Yun, linux-usb, mathias.nyman, arnd, gregkh,
	felipe.balbi, linux-omap, linux-tegra, linux-mediatek,
	linux-arm-kernel, devicetree, jonathanh, thierry.reding, stern,
	linux, Peter.Chen, matthias.bgg, mark.rutland, robh+dt,
	Neil Armstrong, linux-amlogic, yixun.lan, Keerthy, Gerlach, Dave,
	KISHON VIJAY ABRAHAM

Hi Roger,

On Wed, Mar 21, 2018 at 12:30 PM, Roger Quadros <rogerq@ti.com> wrote:
> Martin,
>
> On 21/03/18 00:01, Martin Blumenstingl wrote:
>> Hi Roger, Hi Chunfeng,
>>
>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>> Hi Martin & Roger:
>>>
>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> Hi,
>>>>>
>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>> Hi Roger,
>>>>>>
>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>> +some TI folks
>>>>>>>
>>>>>>> Hi Martin,
>>>>>>>
>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>
>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>> disable/enable them when required:
>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>
>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>
>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>
>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>> I'll explain why below.
>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>
>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>> I missed that, thanks
>>>>
>>>>>>>> ---
>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>
>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>> -usbcore-y += port.o
>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>
>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>> +/*
>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>> + *
>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> + */
>>>>>>>> +
>>>>>>>> +#include <linux/device.h>
>>>>>>>> +#include <linux/list.h>
>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>> +#include <linux/of.h>
>>>>>>>> +
>>>>>>>> +#include "phy.h"
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub {
>>>>>>>> +     struct phy              *phy;
>>>>>>>> +     struct list_head        list;
>>>>>>>> +};
>>>>>>>> +
>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>> +     if (!roothub_entry)
>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>> +
>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>> +
>>>>>>>> +     return roothub_entry;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>> +                                struct list_head *list)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>> +
>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>> +                     return 0;
>>>>>>>> +             else
>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>> +
>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>> +
>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int i, num_phys, err;
>>>>>>>> +
>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>> +                                           "#phy-cells");
>>>>>>>> +     if (num_phys <= 0)
>>>>>>>> +             return NULL;
>>>>>>>> +
>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>> +             return phy_roothub;
>>>>>>>> +
>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>
>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>
>>>>>
>>>>> Yes.
>>>>>
>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>> easier reading:
>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>> can avoid re-enumerating device."
>>>>>>
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_exit_phys;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return phy_roothub;
>>>>>>>> +
>>>>>>>> +err_exit_phys:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     return ERR_PTR(err);
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err, ret = 0;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     ret = ret;
>>>>>>>> +     }
>>>>>>>
>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>> Mediatek SoCs
>>>>>>
>>>>>
>>>>> OK. I suppose that there are 2 cases
>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>> Remote wakeup probably required.
>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>> during suspend/resume to keep PHY link active.
>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>
>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>> ACK
>>>>
>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>
>>>>>>>> +
>>>>>>>> +     return ret;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +     return err;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>> +
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>
>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>> I'm not sure where this problem should be solved:
>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>> TI platforms
>>>>>
>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>
>>>>>> - fix this in the usb_phy_roothub code
>>>>>
>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>> the controller is *NOT* marked as "wakeup-source"?
>>>> I am also not sure if it would work, since the "wakeup-source"
>>>> property is defined on the USB controller's parent node in case of the
>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>
>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>> devices by device_init_wakeup(dev, true),but not dependent on
>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>> decide whether call phy_exit()/init() or not.
>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>> yet, so I cannot test this
>> based on this suggestion I threw up two patches which are *compile
>> tested only* based on Greg's usb-next branch
>> you can find them here: [0] (as well as attached to this mail)
>>
>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>> @Roger: can you please test this on a TI SoC?
>>
>> (apologies in advance if these patches don't work)
>>
>> please note that I won't have access to my computer until Saturday.
>> if these patches need to be rewritten/replaced/etc. then feel free to
>> send your own version to the list
>
> Had to do the following to build
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>                 hcd->state = HC_STATE_SUSPENDED;
>
>                 if (!PMSG_IS_AUTO(msg))
> -                       usb_phy_roothub_suspend(hcd->phy_roothub);
> +                       usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>
>                 /* Did we race with a root-hub wakeup event? */
>                 if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>         }
>
>         if (!PMSG_IS_AUTO(msg)) {
> -               status = usb_phy_roothub_resume(hcd->phy_roothub);
> +               status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
>                 if (status)
>                         return status;
>         }
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>                 }
>         } else {
>                 hcd->state = old_state;
> -               usb_phy_roothub_suspend(hcd->phy_roothub);
> +               usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>                 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>                                 "resume", status);
>                 if (status != -ESHUTDOWN)
>
>
> And the following to fix runtime issues
>
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>                         return PTR_ERR(phy);
>         }
>
> -       roothub_entry = usb_phy_roothub_alloc(dev);
> -       if (IS_ERR(roothub_entry))
> -               return PTR_ERR(roothub_entry);
> +       roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +       if (!roothub_entry)
> +               return -ENOMEM;
>
>         roothub_entry->phy = phy;
>
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>         usb_phy_roothub_power_off(phy_roothub);
>
>         /* keep the PHYs initialized so the device can wake up the system */
> -       if (device_can_wakeup(dev))
> +       if (device_may_wakeup(dev))
>                 return 0;
>
>         return usb_phy_roothub_exit(phy_roothub);
>
>
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
>
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
>
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
>
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
>
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
>
> [1] - https://patchwork.kernel.org/patch/10298641/
>
> What options do we have now to fix the original issue?
>
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
>
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?
I added this code to add support for the USB controller on the Amlogic
Meson GXL and GXM SoCs:
these use a dwc3 controller for host mode

so while this approach would reduce the risk of breaking other
controllers your TI SoCs would still be "affected" since I would like
to enable the new logic for the dwc3 controller (which the TI SoCs use
as well).

I will come up with an RFC patch series based on your and Chunfeng's suggestions
thank you very much for working it out while I was away!

>>
>>>>>> - fix this in the PHY driver
>>>>>
>>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>>> - assuming we define two new callbacks: .suspend and .resume
>>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>>> - the PHY framework could call .power_on by default if .resume is not defined
>>>> - drivers could set .suspend and .resume on their own, allowing more
>>>> fine-grained control by for example *only* stopping the clock (but not
>>>> re-initializing the registers, etc.)
>>>>
>>>> @Roger: what do you think about this?
>>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>>> hear his opinion too
>>>>
>>>>>> - somewhere else
>>>>>>
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>>> @@ -0,0 +1,7 @@
>>>>>>>> +struct usb_phy_roothub;
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>>
>>>>>>>
>>>>>
>
>>
>> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-24 11:23                                 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-24 11:23 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Roger,

On Wed, Mar 21, 2018 at 12:30 PM, Roger Quadros <rogerq@ti.com> wrote:
> Martin,
>
> On 21/03/18 00:01, Martin Blumenstingl wrote:
>> Hi Roger, Hi Chunfeng,
>>
>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>> Hi Martin & Roger:
>>>
>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> Hi,
>>>>>
>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>> Hi Roger,
>>>>>>
>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>> +some TI folks
>>>>>>>
>>>>>>> Hi Martin,
>>>>>>>
>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>
>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>> disable/enable them when required:
>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>
>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>
>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>
>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>> I'll explain why below.
>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>
>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>> I missed that, thanks
>>>>
>>>>>>>> ---
>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>
>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>> -usbcore-y += port.o
>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>
>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>> +/*
>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>> + *
>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> + */
>>>>>>>> +
>>>>>>>> +#include <linux/device.h>
>>>>>>>> +#include <linux/list.h>
>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>> +#include <linux/of.h>
>>>>>>>> +
>>>>>>>> +#include "phy.h"
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub {
>>>>>>>> +     struct phy              *phy;
>>>>>>>> +     struct list_head        list;
>>>>>>>> +};
>>>>>>>> +
>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>> +     if (!roothub_entry)
>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>> +
>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>> +
>>>>>>>> +     return roothub_entry;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>> +                                struct list_head *list)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>> +
>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>> +                     return 0;
>>>>>>>> +             else
>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>> +
>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>> +
>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int i, num_phys, err;
>>>>>>>> +
>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>> +                                           "#phy-cells");
>>>>>>>> +     if (num_phys <= 0)
>>>>>>>> +             return NULL;
>>>>>>>> +
>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>> +             return phy_roothub;
>>>>>>>> +
>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>
>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>
>>>>>
>>>>> Yes.
>>>>>
>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>> easier reading:
>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>> can avoid re-enumerating device."
>>>>>>
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_exit_phys;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return phy_roothub;
>>>>>>>> +
>>>>>>>> +err_exit_phys:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     return ERR_PTR(err);
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err, ret = 0;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     ret = ret;
>>>>>>>> +     }
>>>>>>>
>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>> Mediatek SoCs
>>>>>>
>>>>>
>>>>> OK. I suppose that there are 2 cases
>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>> Remote wakeup probably required.
>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>> during suspend/resume to keep PHY link active.
>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>
>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>> ACK
>>>>
>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>
>>>>>>>> +
>>>>>>>> +     return ret;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +     return err;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>> +
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>
>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>> I'm not sure where this problem should be solved:
>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>> TI platforms
>>>>>
>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>
>>>>>> - fix this in the usb_phy_roothub code
>>>>>
>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>> the controller is *NOT* marked as "wakeup-source"?
>>>> I am also not sure if it would work, since the "wakeup-source"
>>>> property is defined on the USB controller's parent node in case of the
>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>
>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>> devices by device_init_wakeup(dev, true),but not dependent on
>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>> decide whether call phy_exit()/init() or not.
>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>> yet, so I cannot test this
>> based on this suggestion I threw up two patches which are *compile
>> tested only* based on Greg's usb-next branch
>> you can find them here: [0] (as well as attached to this mail)
>>
>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>> @Roger: can you please test this on a TI SoC?
>>
>> (apologies in advance if these patches don't work)
>>
>> please note that I won't have access to my computer until Saturday.
>> if these patches need to be rewritten/replaced/etc. then feel free to
>> send your own version to the list
>
> Had to do the following to build
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>                 hcd->state = HC_STATE_SUSPENDED;
>
>                 if (!PMSG_IS_AUTO(msg))
> -                       usb_phy_roothub_suspend(hcd->phy_roothub);
> +                       usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>
>                 /* Did we race with a root-hub wakeup event? */
>                 if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>         }
>
>         if (!PMSG_IS_AUTO(msg)) {
> -               status = usb_phy_roothub_resume(hcd->phy_roothub);
> +               status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
>                 if (status)
>                         return status;
>         }
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>                 }
>         } else {
>                 hcd->state = old_state;
> -               usb_phy_roothub_suspend(hcd->phy_roothub);
> +               usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>                 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>                                 "resume", status);
>                 if (status != -ESHUTDOWN)
>
>
> And the following to fix runtime issues
>
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>                         return PTR_ERR(phy);
>         }
>
> -       roothub_entry = usb_phy_roothub_alloc(dev);
> -       if (IS_ERR(roothub_entry))
> -               return PTR_ERR(roothub_entry);
> +       roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +       if (!roothub_entry)
> +               return -ENOMEM;
>
>         roothub_entry->phy = phy;
>
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>         usb_phy_roothub_power_off(phy_roothub);
>
>         /* keep the PHYs initialized so the device can wake up the system */
> -       if (device_can_wakeup(dev))
> +       if (device_may_wakeup(dev))
>                 return 0;
>
>         return usb_phy_roothub_exit(phy_roothub);
>
>
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
>
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
>
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
>
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
>
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
>
> [1] - https://patchwork.kernel.org/patch/10298641/
>
> What options do we have now to fix the original issue?
>
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
>
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?
I added this code to add support for the USB controller on the Amlogic
Meson GXL and GXM SoCs:
these use a dwc3 controller for host mode

so while this approach would reduce the risk of breaking other
controllers your TI SoCs would still be "affected" since I would like
to enable the new logic for the dwc3 controller (which the TI SoCs use
as well).

I will come up with an RFC patch series based on your and Chunfeng's suggestions
thank you very much for working it out while I was away!

>>
>>>>>> - fix this in the PHY driver
>>>>>
>>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>>> - assuming we define two new callbacks: .suspend and .resume
>>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>>> - the PHY framework could call .power_on by default if .resume is not defined
>>>> - drivers could set .suspend and .resume on their own, allowing more
>>>> fine-grained control by for example *only* stopping the clock (but not
>>>> re-initializing the registers, etc.)
>>>>
>>>> @Roger: what do you think about this?
>>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>>> hear his opinion too
>>>>
>>>>>> - somewhere else
>>>>>>
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>>> @@ -0,0 +1,7 @@
>>>>>>>> +struct usb_phy_roothub;
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>>
>>>>>>>
>>>>>
>
>>
>> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD
@ 2018-03-24 11:23                                 ` Martin Blumenstingl
  0 siblings, 0 replies; 115+ messages in thread
From: Martin Blumenstingl @ 2018-03-24 11:23 UTC (permalink / raw)
  To: linus-amlogic

Hi Roger,

On Wed, Mar 21, 2018 at 12:30 PM, Roger Quadros <rogerq@ti.com> wrote:
> Martin,
>
> On 21/03/18 00:01, Martin Blumenstingl wrote:
>> Hi Roger, Hi Chunfeng,
>>
>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>> Hi Martin & Roger:
>>>
>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> Hi,
>>>>>
>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>> Hi Roger,
>>>>>>
>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>> +some TI folks
>>>>>>>
>>>>>>> Hi Martin,
>>>>>>>
>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>
>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>> disable/enable them when required:
>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>
>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>
>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>
>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>> I'll explain why below.
>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>
>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>> I missed that, thanks
>>>>
>>>>>>>> ---
>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>
>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>> @@ -6,7 +6,7 @@
>>>>>>>>  usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
>>>>>>>>  usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
>>>>>>>>  usbcore-y += devio.o notify.o generic.o quirks.o devices.o
>>>>>>>> -usbcore-y += port.o
>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>
>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>> +/*
>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>> + *
>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> + */
>>>>>>>> +
>>>>>>>> +#include <linux/device.h>
>>>>>>>> +#include <linux/list.h>
>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>> +#include <linux/of.h>
>>>>>>>> +
>>>>>>>> +#include "phy.h"
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub {
>>>>>>>> +     struct phy              *phy;
>>>>>>>> +     struct list_head        list;
>>>>>>>> +};
>>>>>>>> +
>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>> +     if (!roothub_entry)
>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>> +
>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>> +
>>>>>>>> +     return roothub_entry;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>> +                                struct list_head *list)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>> +
>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>> +                     return 0;
>>>>>>>> +             else
>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>> +
>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>> +
>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int i, num_phys, err;
>>>>>>>> +
>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>> +                                           "#phy-cells");
>>>>>>>> +     if (num_phys <= 0)
>>>>>>>> +             return NULL;
>>>>>>>> +
>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>> +             return phy_roothub;
>>>>>>>> +
>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>
>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>
>>>>>
>>>>> Yes.
>>>>>
>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>> easier reading:
>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>> can avoid re-enumerating device."
>>>>>>
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_exit_phys;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return phy_roothub;
>>>>>>>> +
>>>>>>>> +err_exit_phys:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     return ERR_PTR(err);
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err, ret = 0;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     ret = ret;
>>>>>>>> +     }
>>>>>>>
>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>> Mediatek SoCs
>>>>>>
>>>>>
>>>>> OK. I suppose that there are 2 cases
>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>> Remote wakeup probably required.
>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>> during suspend/resume to keep PHY link active.
>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>
>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>> ACK
>>>>
>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>
>>>>>>>> +
>>>>>>>> +     return ret;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +     return err;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>> +
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>
>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>> I'm not sure where this problem should be solved:
>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>> TI platforms
>>>>>
>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>
>>>>>> - fix this in the usb_phy_roothub code
>>>>>
>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>> the controller is *NOT* marked as "wakeup-source"?
>>>> I am also not sure if it would work, since the "wakeup-source"
>>>> property is defined on the USB controller's parent node in case of the
>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>
>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>> devices by device_init_wakeup(dev, true),but not dependent on
>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>> decide whether call phy_exit()/init() or not.
>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>> yet, so I cannot test this
>> based on this suggestion I threw up two patches which are *compile
>> tested only* based on Greg's usb-next branch
>> you can find them here: [0] (as well as attached to this mail)
>>
>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>> @Roger: can you please test this on a TI SoC?
>>
>> (apologies in advance if these patches don't work)
>>
>> please note that I won't have access to my computer until Saturday.
>> if these patches need to be rewritten/replaced/etc. then feel free to
>> send your own version to the list
>
> Had to do the following to build
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>                 hcd->state = HC_STATE_SUSPENDED;
>
>                 if (!PMSG_IS_AUTO(msg))
> -                       usb_phy_roothub_suspend(hcd->phy_roothub);
> +                       usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>
>                 /* Did we race with a root-hub wakeup event? */
>                 if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>         }
>
>         if (!PMSG_IS_AUTO(msg)) {
> -               status = usb_phy_roothub_resume(hcd->phy_roothub);
> +               status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
>                 if (status)
>                         return status;
>         }
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>                 }
>         } else {
>                 hcd->state = old_state;
> -               usb_phy_roothub_suspend(hcd->phy_roothub);
> +               usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>                 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>                                 "resume", status);
>                 if (status != -ESHUTDOWN)
>
>
> And the following to fix runtime issues
>
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>                         return PTR_ERR(phy);
>         }
>
> -       roothub_entry = usb_phy_roothub_alloc(dev);
> -       if (IS_ERR(roothub_entry))
> -               return PTR_ERR(roothub_entry);
> +       roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +       if (!roothub_entry)
> +               return -ENOMEM;
>
>         roothub_entry->phy = phy;
>
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>         usb_phy_roothub_power_off(phy_roothub);
>
>         /* keep the PHYs initialized so the device can wake up the system */
> -       if (device_can_wakeup(dev))
> +       if (device_may_wakeup(dev))
>                 return 0;
>
>         return usb_phy_roothub_exit(phy_roothub);
>
>
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
>
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
>
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
>
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
>
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
>
> [1] - https://patchwork.kernel.org/patch/10298641/
>
> What options do we have now to fix the original issue?
>
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
>
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?
I added this code to add support for the USB controller on the Amlogic
Meson GXL and GXM SoCs:
these use a dwc3 controller for host mode

so while this approach would reduce the risk of breaking other
controllers your TI SoCs would still be "affected" since I would like
to enable the new logic for the dwc3 controller (which the TI SoCs use
as well).

I will come up with an RFC patch series based on your and Chunfeng's suggestions
thank you very much for working it out while I was away!

>>
>>>>>> - fix this in the PHY driver
>>>>>
>>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>>> - assuming we define two new callbacks: .suspend and .resume
>>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>>> - the PHY framework could call .power_on by default if .resume is not defined
>>>> - drivers could set .suspend and .resume on their own, allowing more
>>>> fine-grained control by for example *only* stopping the clock (but not
>>>> re-initializing the registers, etc.)
>>>>
>>>> @Roger: what do you think about this?
>>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>>> hear his opinion too
>>>>
>>>>>> - somewhere else
>>>>>>
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>>> @@ -0,0 +1,7 @@
>>>>>>>> +struct usb_phy_roothub;
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>>
>>>>>>>
>>>>>
>
>>
>> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin

^ permalink raw reply	[flat|nested] 115+ messages in thread

end of thread, other threads:[~2018-03-24 11:23 UTC | newest]

Thread overview: 115+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-18 18:44 [PATCH usb-next v10 0/8] initialize (multiple) PHYs for a HCD Martin Blumenstingl
2018-02-18 18:44 ` Martin Blumenstingl
2018-02-18 18:44 ` Martin Blumenstingl
     [not found] ` <20180218184504.3331-1-martin.blumenstingl-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
2018-02-18 18:44   ` [PATCH usb-next v10 1/8] dt-bindings: usb: add the documentation for USB HCDs Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` [usb-next,v10,1/8] " Martin Blumenstingl
2018-02-18 18:44   ` [PATCH usb-next v10 2/8] usb: add a flag to skip PHY initialization to struct usb_hcd Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` [usb-next,v10,2/8] " Martin Blumenstingl
2018-02-26  3:26     ` [PATCH usb-next v10 2/8] " Peter Chen
2018-02-26  3:26       ` Peter Chen
2018-02-26  3:26       ` Peter Chen
2018-02-26  3:26       ` [usb-next,v10,2/8] " Peter Chen
2018-02-18 18:44   ` [PATCH usb-next v10 3/8] usb: core: add a wrapper for the USB PHYs on the HCD Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` Martin Blumenstingl
2018-02-18 18:44     ` [usb-next,v10,3/8] " Martin Blumenstingl
2018-03-16 14:32     ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-16 14:32       ` Roger Quadros
2018-03-16 14:32       ` Roger Quadros
2018-03-16 14:32       ` [usb-next,v10,3/8] " Roger Quadros
     [not found]       ` <7f511a6f-f3a2-2002-f601-5ce1742f4539-l0cyMroinI0@public.gmane.org>
2018-03-18 22:29         ` [PATCH usb-next v10 3/8] " Martin Blumenstingl
2018-03-18 22:29           ` Martin Blumenstingl
2018-03-18 22:29           ` Martin Blumenstingl
2018-03-18 22:29           ` [usb-next,v10,3/8] " Martin Blumenstingl
     [not found]           ` <CAFBinCAOG9Yj5PQo4wsxH2Ev6WOwFge+jBMprRiOgxKuA_z8wA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2018-03-19  6:02             ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-19  6:02               ` Chunfeng Yun
2018-03-19  6:02               ` Chunfeng Yun
2018-03-19  6:02               ` [usb-next,v10,3/8] " Chunfeng Yun
2018-03-19  8:49             ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-19  8:49               ` Roger Quadros
2018-03-19  8:49               ` Roger Quadros
2018-03-19  8:49               ` [usb-next,v10,3/8] " Roger Quadros
     [not found]               ` <e723fe80-1ff3-6f47-fbbd-699dfcd0be4f-l0cyMroinI0@public.gmane.org>
2018-03-19 16:12                 ` [PATCH usb-next v10 3/8] " Martin Blumenstingl
2018-03-19 16:12                   ` Martin Blumenstingl
2018-03-19 16:12                   ` Martin Blumenstingl
2018-03-19 16:12                   ` [usb-next,v10,3/8] " Martin Blumenstingl
2018-03-20 11:27                   ` [PATCH usb-next v10 3/8] " Kishon Vijay Abraham I
2018-03-20 11:27                     ` Kishon Vijay Abraham I
2018-03-20 11:27                     ` Kishon Vijay Abraham I
2018-03-20 11:27                     ` [usb-next,v10,3/8] " Kishon Vijay Abraham I
     [not found]                     ` <e5dcaf32-7001-d647-1a36-359a9728fa9a-l0cyMroinI0@public.gmane.org>
2018-03-20 21:57                       ` [PATCH usb-next v10 3/8] " Martin Blumenstingl
2018-03-20 21:57                         ` Martin Blumenstingl
2018-03-20 21:57                         ` Martin Blumenstingl
2018-03-20 21:57                         ` [usb-next,v10,3/8] " Martin Blumenstingl
2018-03-20 23:47                         ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-20 23:47                           ` Chunfeng Yun
2018-03-20 23:47                           ` Chunfeng Yun
2018-03-20 23:47                           ` [usb-next,v10,3/8] " Chunfeng Yun
     [not found]                   ` <CAFBinCBB2eqHC-k9wN67ZehKb9eun=mPaLuqhrbR-J1C0Ldqtw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2018-03-20  7:54                     ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-20  7:54                       ` Chunfeng Yun
2018-03-20  7:54                       ` Chunfeng Yun
2018-03-20  7:54                       ` [usb-next,v10,3/8] " Chunfeng Yun
2018-03-20 10:55                     ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-20 10:55                       ` Roger Quadros
2018-03-20 10:55                       ` Roger Quadros
2018-03-20 10:55                       ` [usb-next,v10,3/8] " Roger Quadros
     [not found]                       ` <8812bba8-bd2e-85dd-9506-a2e856db4f14-l0cyMroinI0@public.gmane.org>
2018-03-20 11:02                         ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-20 11:02                           ` Roger Quadros
2018-03-20 11:02                           ` Roger Quadros
2018-03-20 11:02                           ` [usb-next,v10,3/8] " Roger Quadros
2018-03-20 12:04                     ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-20 12:04                       ` Chunfeng Yun
2018-03-20 12:04                       ` Chunfeng Yun
2018-03-20 12:04                       ` [usb-next,v10,3/8] " Chunfeng Yun
2018-03-20 22:01                       ` [PATCH usb-next v10 3/8] " Martin Blumenstingl
2018-03-20 22:01                         ` Martin Blumenstingl
2018-03-20 22:01                         ` Martin Blumenstingl
2018-03-20 22:01                         ` [usb-next,v10,3/8] " Martin Blumenstingl
     [not found]                         ` <CAFBinCC7X08oVt223FeZHSqmbCry9aKA8JXz+WmLuDRwwtr+gQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2018-03-21 11:30                           ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-21 11:30                             ` Roger Quadros
2018-03-21 11:30                             ` Roger Quadros
2018-03-21 11:30                             ` [usb-next,v10,3/8] " Roger Quadros
     [not found]                             ` <613ebb74-6167-56bc-6fa0-2b3c9876ccaa-l0cyMroinI0@public.gmane.org>
2018-03-22  8:10                               ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-22  8:10                                 ` Chunfeng Yun
2018-03-22  8:10                                 ` Chunfeng Yun
2018-03-22  8:10                                 ` [usb-next,v10,3/8] " Chunfeng Yun
2018-03-22 12:41                                 ` [PATCH usb-next v10 3/8] " Roger Quadros
2018-03-22 12:41                                   ` Roger Quadros
2018-03-22 12:41                                   ` Roger Quadros
2018-03-22 12:41                                   ` [usb-next,v10,3/8] " Roger Quadros
     [not found]                                   ` <e9ef724f-0a62-cd35-9e0a-82643bde4a16-l0cyMroinI0@public.gmane.org>
2018-03-23  3:19                                     ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-23  3:19                                       ` Chunfeng Yun
2018-03-23  3:19                                       ` Chunfeng Yun
2018-03-23  3:19                                       ` [usb-next,v10,3/8] " Chunfeng Yun
2018-03-24 11:23                               ` [PATCH usb-next v10 3/8] " Martin Blumenstingl
2018-03-24 11:23                                 ` Martin Blumenstingl
2018-03-24 11:23                                 ` Martin Blumenstingl
2018-03-24 11:23                                 ` [usb-next,v10,3/8] " Martin Blumenstingl
2018-03-19  5:43         ` [PATCH usb-next v10 3/8] " Chunfeng Yun
2018-03-19  5:43           ` Chunfeng Yun
2018-03-19  5:43           ` Chunfeng Yun
2018-03-19  5:43           ` [usb-next,v10,3/8] " Chunfeng Yun
2018-02-18 18:45   ` [PATCH usb-next v10 4/8] usb: core: hcd: integrate the PHY wrapper into the HCD core Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` [usb-next,v10,4/8] " Martin Blumenstingl
2018-02-18 18:45   ` [PATCH usb-next v10 5/8] usb: host: xhci-mtk: remove custom USB PHY handling Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` [usb-next,v10,5/8] " Martin Blumenstingl
2018-02-18 18:45   ` [PATCH usb-next v10 6/8] usb: host: ehci-platform: " Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` Martin Blumenstingl
2018-02-18 18:45     ` [usb-next,v10,6/8] " Martin Blumenstingl
2018-02-18 18:45 ` [PATCH usb-next v10 7/8] usb: host: ohci-platform: " Martin Blumenstingl
2018-02-18 18:45   ` Martin Blumenstingl
2018-02-18 18:45   ` Martin Blumenstingl
2018-02-18 18:45   ` [usb-next,v10,7/8] " Martin Blumenstingl
2018-02-18 18:45 ` [PATCH usb-next v10 8/8] usb: core: hcd: remove support for initializing a single PHY Martin Blumenstingl
2018-02-18 18:45   ` Martin Blumenstingl
2018-02-18 18:45   ` Martin Blumenstingl
2018-02-18 18:45   ` [usb-next,v10,8/8] " Martin Blumenstingl

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.