diff mbox series

[2/2] usb: dwc3: add clock and resets

Message ID 1521113998-25052-2-git-send-email-yamada.masahiro@socionext.com
State New
Headers show
Series None | expand

Commit Message

Masahiro Yamada March 15, 2018, 11:39 a.m. UTC
dwc3-of-simple.c only handles arbitrary number of clocks and resets.
They are both generic enough to be put into the dwc3 core.  For simple
cases, a nested node structure like follows:

  dwc3-glue {
          compatible = "foo,dwc3";
          clocks = ...;
          resets = ...;
          ...

          dwc3 {
                  compatible = "snps,dwc3";
                  ...
          };
  }

would be turned into a single node:

  dwc3 {
          compatible = "foo,dwc3", "snps,dwc3";
          clocks = ...;
          resets = ...;
          ...
  }

I inserted reset_control_deassert() and clk_enable() before the first
register access, i.e. dwc3_cache_hwparams().

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

---

 Documentation/devicetree/bindings/usb/dwc3.txt |   2 +
 drivers/usb/dwc3/core.c                        | 127 ++++++++++++++++++++++++-
 drivers/usb/dwc3/core.h                        |   5 +
 3 files changed, 132 insertions(+), 2 deletions(-)

-- 
2.7.4

Comments

Rob Herring April 9, 2018, 1:32 p.m. UTC | #1
On Wed, Mar 28, 2018 at 11:32 PM, Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:
> 2018-03-19 7:37 GMT+09:00 Masahiro Yamada <yamada.masahiro@socionext.com>:

>> Hi Rob,

>>

>> 2018-03-18 21:52 GMT+09:00 Rob Herring <robh@kernel.org>:

>>> On Thu, Mar 15, 2018 at 08:39:58PM +0900, Masahiro Yamada wrote:

>>>> dwc3-of-simple.c only handles arbitrary number of clocks and resets.

>>>> They are both generic enough to be put into the dwc3 core.  For simple

>>>> cases, a nested node structure like follows:

>>>>

>>>>   dwc3-glue {

>>>>           compatible = "foo,dwc3";

>>>>           clocks = ...;

>>>>           resets = ...;

>>>>           ...

>>>>

>>>>           dwc3 {

>>>>                   compatible = "snps,dwc3";

>>>>                   ...

>>>>           };

>>>

>>> I'm not a fan of how this was done.

>>>

>>>

>>>>   }

>>>>

>>>> would be turned into a single node:

>>>>

>>>>   dwc3 {

>>>>           compatible = "foo,dwc3", "snps,dwc3";

>>>>           clocks = ...;

>>>>           resets = ...;

>>>>           ...

>>>>   }

>>>

>>> And yes, this is what I'd prefer.

>>

>>

>>

>> Not only dwc3-of-simple.c, but all dwc3 nodes are

>> written like this.

>>

>>

>> omap_dwc3_1: omap_dwc3_1@48880000 {

>>         compatible = "ti,dwc3";

>>         reg = <0x48880000 0x10000>;

>>         #address-cells = <1>;

>>         #size-cells = <1>;

>>         ranges;

>>         ...

>>

>>         usb1: usb@48890000 {

>>                 compatible = "snps,dwc3";

>>                 reg = <0x48890000 0x17000>;

>>                 ...

>>         };

>> };

>>

>>

>> The glue layer initializes SoC-specific things,

>> then populates the child "snps,dwc3".

>>

>>

>> I think the following structure should work

>> by handling EPROBE_DEFER properly.

>>

>> omap_dwc3_1: omap_dwc3_1@48880000 {

>>         compatible = "ti,dwc3"; (should be "ti,dwc3-glue" or something)

>>         reg = <0x48880000 0x10000>;

>>         ...

>> };

>>

>> usb1: usb@48890000 {

>>         compatible = "snps,dwc3";

>>         reg = <0x48890000 0x17000>;

>>         ...

>> };

>>

>>

>>

>>>>

>>>> I inserted reset_control_deassert() and clk_enable() before the first

>>>> register access, i.e. dwc3_cache_hwparams().

>>>>

>>>> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

>>>> ---

>>>>

>>>>  Documentation/devicetree/bindings/usb/dwc3.txt |   2 +

>>>>  drivers/usb/dwc3/core.c                        | 127 ++++++++++++++++++++++++-

>>>>  drivers/usb/dwc3/core.h                        |   5 +

>>>>  3 files changed, 132 insertions(+), 2 deletions(-)

>>>>

>>>> diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt

>>>> index 44e8bab..67e9cfb 100644

>>>> --- a/Documentation/devicetree/bindings/usb/dwc3.txt

>>>> +++ b/Documentation/devicetree/bindings/usb/dwc3.txt

>>>> @@ -9,12 +9,14 @@ Required properties:

>>>>   - interrupts: Interrupts used by the dwc3 controller.

>>>>

>>>>  Optional properties:

>>>> + - clocks: list of phandle and clock specifier pairs

>>>

>>> However, this should be specific as to how many clocks and their

>>> function. This should be readily available to someone with access to

>>> Synopsys datasheet. The number of clocks should generally be the same

>>> across SoCs, it is just some SoCs either tie clocks together or don't

>>> provide s/w control of some of the clocks.

>>

>>

>> Make sense.

>> You also implies this property is mandatory.

>> The number of clocks should be available in the datasheet

>> and no hardware can work with zero clock.

>>

>> However, making it mandatory breaks the binding

>> since the existing DT files do not specify clocks at all

>> in the "snps,dwc3" node.


Because of this it has to be optional. We could list what compatibles
it is optional for and that it is mandatory for new users.

>>

>> Anyway, our current situation:

>>

>> - We have the dwc3-core under the glue layer node

>>   despite they are independent in the CPU address view

>> - We add all sorts of clocks and resets in the glue layer node,

>>   and nothing in the dwc3-core node.

>>

>> If these are design mistake, what should we do?

>>

>> Continue development based on it?

>> If we fix it, how to change the course?

>>

>

> Any insight about this?


We just need to reject new bindings that do things the old way. The
driver will have to support both cases for some time period.


> I think this is rather a general question.

>

> If somebody upstreams a driver without clocks,

> then later it turns out clocks are necessary,

> adding required clocks would break existing platforms

> since clk_get() fails.


We should try to prevent that, but of course that is not always
possible. Unless that platform is deemed unstable and we break
compatibility, we'd have to handle both cases in the driver.

Rob
diff mbox series

Patch

diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt
index 44e8bab..67e9cfb 100644
--- a/Documentation/devicetree/bindings/usb/dwc3.txt
+++ b/Documentation/devicetree/bindings/usb/dwc3.txt
@@ -9,12 +9,14 @@  Required properties:
  - interrupts: Interrupts used by the dwc3 controller.
 
 Optional properties:
+ - clocks: list of phandle and clock specifier pairs
  - usb-phy : array of phandle for the PHY device.  The first element
    in the array is expected to be a handle to the USB2/HS PHY and
    the second element is expected to be a handle to the USB3/SS PHY
  - phys: from the *Generic PHY* bindings
  - phy-names: from the *Generic PHY* bindings; supported names are "usb2-phy"
 	or "usb3-phy".
+ - resets: list of phandle and reset specifier pairs
  - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable
  - snps,disable_scramble_quirk: true when SW should disable data scrambling.
 	Only really useful for FPGA builds.
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index e9083a3..f17e4a9 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -8,6 +8,7 @@ 
  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  */
 
+#include <linux/clk.h>
 #include <linux/version.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
@@ -24,6 +25,7 @@ 
 #include <linux/of.h>
 #include <linux/acpi.h>
 #include <linux/pinctrl/consumer.h>
+#include <linux/reset.h>
 
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
@@ -240,6 +242,74 @@  static int dwc3_core_soft_reset(struct dwc3 *dwc)
 	return -ETIMEDOUT;
 }
 
+static int dwc3_core_get_clks(struct dwc3 *dwc)
+{
+	struct device *dev = dwc->dev;
+	struct device_node *node = dev->of_node;
+	struct clk *clk;
+	int num_clks, i;
+
+	num_clks = of_count_phandle_with_args(node, "clocks", "#clock-cells");
+	if (num_clks <= 0)
+		return 0;
+
+	dwc->num_clks = num_clks;
+
+	dwc->clks = devm_kcalloc(dev, num_clks, sizeof(*dwc->clks), GFP_KERNEL);
+	if (!dwc->clks)
+		return -ENOMEM;
+
+	for (i = 0; i < num_clks; i++) {
+		clk = of_clk_get(node, i);
+		if (IS_ERR(clk))
+			goto put_clks;
+		dwc->clks[i] = clk;
+	}
+
+	return 0;
+
+put_clks:
+	while (--i >= 0)
+		clk_put(dwc->clks[i]);
+
+	return PTR_ERR(clk);
+}
+
+static void dwc3_core_put_clks(struct dwc3 *dwc)
+{
+	int i;
+
+	for (i = dwc->num_clks - 1; i >= 0; i--)
+		clk_put(dwc->clks[i]);
+}
+
+static int dwc3_core_enable_clks(struct dwc3 *dwc)
+{
+	int ret, i;
+
+	for (i = 0; i < dwc->num_clks; i++) {
+		ret = clk_prepare_enable(dwc->clks[i]);
+		if (ret)
+			goto disable_clks;
+	}
+
+	return 0;
+
+disable_clks:
+	while (--i >= 0)
+		clk_disable_unprepare(dwc->clks[i]);
+
+	return ret;
+}
+
+static void dwc3_core_disable_clks(struct dwc3 *dwc)
+{
+	int i;
+
+	for (i = dwc->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(dwc->clks[i]);
+}
+
 /*
  * dwc3_frame_length_adjustment - Adjusts frame length if required
  * @dwc3: Pointer to our controller context structure
@@ -641,6 +711,8 @@  static void dwc3_core_exit(struct dwc3 *dwc)
 	usb_phy_set_suspend(dwc->usb3_phy, 1);
 	phy_power_off(dwc->usb2_generic_phy);
 	phy_power_off(dwc->usb3_generic_phy);
+	dwc3_core_disable_clks(dwc);
+	reset_control_assert(dwc->resets);
 }
 
 static bool dwc3_core_is_valid(struct dwc3 *dwc)
@@ -1205,6 +1277,22 @@  static int dwc3_probe(struct platform_device *pdev)
 
 	dwc3_get_properties(dwc);
 
+	dwc->resets = devm_reset_control_array_get_optional_shared(dev);
+	if (IS_ERR(dwc->resets))
+		return PTR_ERR(dwc->resets);
+
+	ret = dwc3_core_get_clks(dwc);
+	if (ret)
+		return ret;
+
+	ret = reset_control_deassert(dwc->resets);
+	if (ret)
+		goto put_clks;
+
+	ret = dwc3_core_enable_clks(dwc);
+	if (ret)
+		goto assert_resets;
+
 	platform_set_drvdata(pdev, dwc);
 	dwc3_cache_hwparams(dwc);
 
@@ -1268,6 +1356,14 @@  static int dwc3_probe(struct platform_device *pdev)
 	pm_runtime_put_sync(&pdev->dev);
 	pm_runtime_disable(&pdev->dev);
 
+	dwc3_core_disable_clks(dwc);
+
+assert_resets:
+	reset_control_assert(dwc->resets);
+
+put_clks:
+	dwc3_core_put_clks(dwc);
+
 	return ret;
 }
 
@@ -1289,11 +1385,38 @@  static int dwc3_remove(struct platform_device *pdev)
 
 	dwc3_free_event_buffers(dwc);
 	dwc3_free_scratch_buffers(dwc);
+	dwc3_core_put_clks(dwc);
 
 	return 0;
 }
 
 #ifdef CONFIG_PM
+static int dwc3_core_init_for_resume(struct dwc3 *dwc)
+{
+	int ret;
+
+	ret = reset_control_deassert(dwc->resets);
+	if (ret)
+		return ret;
+
+	ret = dwc3_core_enable_clks(dwc);
+	if (ret)
+		goto assert_resets;
+
+	ret = dwc3_core_init(dwc);
+	if (ret)
+		goto disable_clks;
+
+	return 0;
+
+disable_clks:
+	dwc3_core_disable_clks(dwc);
+assert_resets:
+	reset_control_assert(dwc->resets);
+
+	return ret;
+}
+
 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
 {
 	unsigned long	flags;
@@ -1325,7 +1448,7 @@  static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
 
 	switch (dwc->current_dr_role) {
 	case DWC3_GCTL_PRTCAP_DEVICE:
-		ret = dwc3_core_init(dwc);
+		ret = dwc3_core_init_for_resume(dwc);
 		if (ret)
 			return ret;
 
@@ -1336,7 +1459,7 @@  static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
 	case DWC3_GCTL_PRTCAP_HOST:
 		/* nothing to do on host runtime_resume */
 		if (!PMSG_IS_AUTO(msg)) {
-			ret = dwc3_core_init(dwc);
+			ret = dwc3_core_init_for_resume(dwc);
 			if (ret)
 				return ret;
 		}
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index 860d2bc..14cd335 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -891,6 +891,11 @@  struct dwc3 {
 	struct usb_gadget	gadget;
 	struct usb_gadget_driver *gadget_driver;
 
+	struct clk		**clks;
+	int			num_clks;
+
+	struct reset_control	*resets;
+
 	struct usb_phy		*usb2_phy;
 	struct usb_phy		*usb3_phy;