Skip to contentSkip to conversation

Register at91 OHCI into DM and add SAMA7 USB PHY's

PATCH Superseded Sergiu Moga 2 participants 9 messages Jan 3, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Register at91 OHCI into DM and add SAMA7 USB PHY's

This patch series originates from a bigger patch series:
https://lists.denx.de/pipermail/u-boot/2022-December/502865.html

Register ohci-at91 driver into Driver Model. In order for the VBUS to stay enabled, a child_pre_probe method has been added to overcome the DM core disabling it in usb_scan_device: when the generic device_probe method is called, the pinctrl is processed once again, undoing whatever changes have been made in our driver's probe method.
In order to enable USB on SAMA7G5 the addition of the USB 2.0 PHY drivers were required.

v1 -> v2:
- squashed previous 3/4 patch into 2/4 patch
- used *_bulk API's
- re-wrote at91_for_each_port
- use dev_read_u32_default for pdata->ports

Sergiu Moga (3):
phy: at91: Add support for the USB 2.0 PHY's of SAMA7
usb: ohci-at91: Enable OHCI functionality and register into DM
usb: ohci-at91: Add USB PHY functionality

 drivers/phy/Kconfig              |  10 ++
 drivers/phy/Makefile             |   1 +
 drivers/phy/phy-sama7-usb.c      |  90 ++++++++++++++
 drivers/phy/phy-sama7-utmi-clk.c | 202 +++++++++++++++++++++++++++++++
 drivers/usb/host/Kconfig         |   1 +
 drivers/usb/host/ohci-at91.c     | 185 ++++++++++++++++++++++++++++
 6 files changed, 489 insertions(+)
 create mode 100644 drivers/phy/phy-sama7-usb.c
 create mode 100644 drivers/phy/phy-sama7-utmi-clk.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/3 phy: at91: Add support for the USB 2.0 PHY's of SAMA7

In order to have USB functionality, drivers for SAMA7's USB 2.0 PHY's have been added. There is one driver for UTMI clock's SFR and RESET required functionalities and one for its three possible subclocks of the phy's themselves. In order for this layout to properly work in conjunction with CCF and DT, the former driver will also act as a clock provider for the three phy's with the help of a custom hook into the driver's of_xlate method.

---

v1 -> v2:
- Nothing

 drivers/phy/Kconfig              |  10 ++
 drivers/phy/Makefile             |   1 +
 drivers/phy/phy-sama7-usb.c      |  90 ++++++++++++++
 drivers/phy/phy-sama7-utmi-clk.c | 202 +++++++++++++++++++++++++++++++
 4 files changed, 303 insertions(+)
 create mode 100644 drivers/phy/phy-sama7-usb.c
 create mode 100644 drivers/phy/phy-sama7-utmi-clk.c
drivers/phy/Kconfig +10 -0
index cf4d5908d7..9fbb956783 100644
@@ -281,6 +281,16 @@ config PHY_XILINX_ZYNQMP
 	  Enable this to support ZynqMP High Speed Gigabit Transceiver
 	  that is part of ZynqMP SoC.
 
+config PHY_MICROCHIP_SAMA7_USB
+       tristate "Microchip SAMA7 USB 2.0 PHY"
+       depends on PHY && ARCH_AT91
+       help
+	 Enable this to support SAMA7 USB 2.0 PHY.
+
+	 The USB 2.0 PHY integrates high-speed, full-speed and low-speed
+	 termination and signal switching. With a single resistor, it
+	 requires minimal external components.
+
 source "drivers/phy/rockchip/Kconfig"
 source "drivers/phy/cadence/Kconfig"
 source "drivers/phy/ti/Kconfig"
drivers/phy/Makefile +1 -0
index a3b9f3c5b1..9d50affd47 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_PHY_MTK_TPHY) += phy-mtk-tphy.o
 obj-$(CONFIG_PHY_NPCM_USB) += phy-npcm-usb.o
 obj-$(CONFIG_PHY_IMX8MQ_USB) += phy-imx8mq-usb.o
 obj-$(CONFIG_PHY_XILINX_ZYNQMP) += phy-zynqmp.o
+obj-$(CONFIG_PHY_MICROCHIP_SAMA7_USB)  += phy-sama7-utmi-clk.o phy-sama7-usb.o
 obj-y += cadence/
 obj-y += ti/
 obj-y += qcom/
drivers/phy/phy-sama7-usb.c new file +90 -0

90 lines not on this page · raw message

drivers/phy/phy-sama7-utmi-clk.c new file +202 -0

202 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/3] phy: at91: Add support for the USB 2.0 PHY's of SAMA7

Quoted 8 lines · Sergiu Moga

In order to have USB functionality, drivers for SAMA7's USB 2.0 PHY's have been added. There is one driver for UTMI clock's SFR and RESET required functionalities and one for its three possible subclocks of the phy's themselves. In order for this layout to properly work in conjunction with CCF and DT, the former driver will also act as a clock provider for the three phy's with the help of a custom hook into the driver's of_xlate method.

[...]

Quoted 18 lines
drivers/phy/phy-sama7-utmi-clk.c new file +18 -0
+static int sama7_utmi_probe(struct udevice *dev)
+{
+	struct clk *utmi_parent_clk, *utmi_clk;
+	struct regmap *regmap_sfr;
+	struct reset_ctl *phy_reset;
+	int i;
+	char name[16];
+
+	utmi_parent_clk = devm_clk_get(dev, "utmi_clk");
+	if (IS_ERR(utmi_parent_clk))
+		return PTR_ERR(utmi_parent_clk);
+
+	regmap_sfr = syscon_regmap_lookup_by_phandle(dev, "sfr-phandle");
+	if (IS_ERR(regmap_sfr))
+		return PTR_ERR(regmap_sfr);
+
+	for (i = 0; i < ARRAY_SIZE(sama7_utmick); i++) {
+		snprintf(name, sizeof(name), "usb%d_reset", i);

drivers/phy/phy-sama7-utmi-clk.c line 161

Quoted 10 lines
drivers/phy/phy-sama7-utmi-clk.c new file +10 -0
+		phy_reset = devm_reset_control_get(dev, name);
+		if (IS_ERR(phy_reset))
+			return PTR_ERR(phy_reset);
+
+		utmi_clk = sama7_utmi_clk_register(regmap_sfr, phy_reset,
+						   sama7_utmick[i].n,
+						   sama7_utmick[i].p,
+						   sama7_utmick[i].id);
+		if (IS_ERR(utmi_clk))
+			return PTR_ERR(utmi_clk);

Isn't this going to leak memory if i>0 and a failure occurs ? I think it will, since sama7_utmi_clk_register() calls kzalloc() .

#3 lore raw

Re: [PATCH v2 1/3] phy: at91: Add support for the USB 2.0 PHY's of SAMA7

Quoted 35 lines · Marek Vasut
Quoted 8 lines · Sergiu Moga

In order to have USB functionality, drivers for SAMA7's USB 2.0 PHY's have been added. There is one driver for UTMI clock's SFR and RESET required functionalities and one for its three possible subclocks of the phy's themselves. In order for this layout to properly work in conjunction with CCF and DT, the former driver will also act as a clock provider for the three phy's with the help of a custom hook into the driver's of_xlate method.

[...]

Quoted 23 lines
drivers/phy/phy-sama7-utmi-clk.c new file +23 -0
+static int sama7_utmi_probe(struct udevice *dev)
+{
+     struct clk *utmi_parent_clk, *utmi_clk;
+     struct regmap *regmap_sfr;
+     struct reset_ctl *phy_reset;
+     int i;
+     char name[16];
+
+     utmi_parent_clk = devm_clk_get(dev, "utmi_clk");
+     if (IS_ERR(utmi_parent_clk))
+             return PTR_ERR(utmi_parent_clk);
+
+     regmap_sfr = syscon_regmap_lookup_by_phandle(dev, "sfr-phandle");
+     if (IS_ERR(regmap_sfr))
+             return PTR_ERR(regmap_sfr);
+
+     for (i = 0; i < ARRAY_SIZE(sama7_utmick); i++) {
+             snprintf(name, sizeof(name), "usb%d_reset", i);
+             phy_reset = devm_reset_control_get(dev, name);
+             if (IS_ERR(phy_reset))
+                     return PTR_ERR(phy_reset);
+
+             utmi_clk = sama7_utmi_clk_register(regmap_sfr, phy_reset,
Quoted 8 lines
Quoted 5 lines

+                                                sama7_utmick[i].n, +                                                sama7_utmick[i].p, +                                                sama7_utmick[i].id); +             if (IS_ERR(utmi_clk))
+                     return PTR_ERR(utmi_clk);

Isn't this going to leak memory if i>0 and a failure occurs ? I think it will, since sama7_utmi_clk_register() calls kzalloc() .

Yes, you are right. Perhaps something like this should work better instead, wouldn't you agree?

static int sama7_utmi_probe(struct udevice *dev) {
- struct clk *utmi_parent_clk, *utmi_clk;
+ struct clk *utmi_parent_clk, *utmi_clk[ARRAY_SIZE(sama7_utmick)];
+ struct sama7_utmi_clk *uclk;
struct regmap *regmap_sfr;
struct reset_ctl *phy_reset;
- int i;
+ int i, j;
char name[16];

utmi_parent_clk = devm_clk_get(dev, "utmi_clk");
@@ -153,12 +154,18 @@ static int sama7_utmi_probe(struct udevice *dev)
if (IS_ERR(phy_reset))
return PTR_ERR(phy_reset);

- utmi_clk = sama7_utmi_clk_register(regmap_sfr, phy_reset,
+ utmi_clk[i] = sama7_utmi_clk_register(regmap_sfr, phy_reset,
sama7_utmick[i].n, sama7_utmick[i].p, sama7_utmick[i].id);
- if (IS_ERR(utmi_clk))
+ if (IS_ERR(utmi_clk)) {
+ for (j = 0; j < i; j++) {
+ uclk = to_sama7_utmi_clk(utmi_clk[j]);
+ kfree(uclk);
+ }
+
return PTR_ERR(utmi_clk);
+ }
}

return 0;

#4 lore raw

Re: [PATCH v2 1/3] phy: at91: Add support for the USB 2.0 PHY's of SAMA7

Quoted 64 lines · On 1/3/23 17:54, [email protected] wrote:
Quoted 35 lines · Marek Vasut
Quoted 8 lines · Sergiu Moga

In order to have USB functionality, drivers for SAMA7's USB 2.0 PHY's have been added. There is one driver for UTMI clock's SFR and RESET required functionalities and one for its three possible subclocks of the phy's themselves. In order for this layout to properly work in conjunction with CCF and DT, the former driver will also act as a clock provider for the three phy's with the help of a custom hook into the driver's of_xlate method.

[...]

Quoted 23 lines
drivers/phy/phy-sama7-utmi-clk.c new file +23 -0
+static int sama7_utmi_probe(struct udevice *dev)
+{
+     struct clk *utmi_parent_clk, *utmi_clk;
+     struct regmap *regmap_sfr;
+     struct reset_ctl *phy_reset;
+     int i;
+     char name[16];
+
+     utmi_parent_clk = devm_clk_get(dev, "utmi_clk");
+     if (IS_ERR(utmi_parent_clk))
+             return PTR_ERR(utmi_parent_clk);
+
+     regmap_sfr = syscon_regmap_lookup_by_phandle(dev, "sfr-phandle");
+     if (IS_ERR(regmap_sfr))
+             return PTR_ERR(regmap_sfr);
+
+     for (i = 0; i < ARRAY_SIZE(sama7_utmick); i++) {
+             snprintf(name, sizeof(name), "usb%d_reset", i);
+             phy_reset = devm_reset_control_get(dev, name);
+             if (IS_ERR(phy_reset))
+                     return PTR_ERR(phy_reset);
+
+             utmi_clk = sama7_utmi_clk_register(regmap_sfr, phy_reset,
Quoted 8 lines
Quoted 5 lines

+                                                sama7_utmick[i].n, +                                                sama7_utmick[i].p, +                                                sama7_utmick[i].id); +             if (IS_ERR(utmi_clk))
+                     return PTR_ERR(utmi_clk);

Isn't this going to leak memory if i>0 and a failure occurs ? I think it will, since sama7_utmi_clk_register() calls kzalloc() .

Yes, you are right. Perhaps something like this should work better instead, wouldn't you agree?

   static int sama7_utmi_probe(struct udevice *dev)
   {
(quoted diff) +3 -2
-	struct clk *utmi_parent_clk, *utmi_clk;
+	struct clk *utmi_parent_clk, *utmi_clk[ARRAY_SIZE(sama7_utmick)];
+	struct sama7_utmi_clk *uclk;
   	struct regmap *regmap_sfr;
   	struct reset_ctl *phy_reset;
-	int i;
+	int i, j;
   	char name[16];
 
   	utmi_parent_clk = devm_clk_get(dev, "utmi_clk");
@@ -153,12 +154,18 @@ static int sama7_utmi_probe(struct udevice *dev)
   		if (IS_ERR(phy_reset))
   			return PTR_ERR(phy_reset);
 

Review static int sama7_utmi_probe(struct udevice *dev) · line 164

Quoted 10 lines
(quoted diff) +5 -2
-		utmi_clk = sama7_utmi_clk_register(regmap_sfr, phy_reset,
+		utmi_clk[i] = sama7_utmi_clk_register(regmap_sfr, phy_reset,
   						   sama7_utmick[i].n,
   						   sama7_utmick[i].p,
   						   sama7_utmick[i].id);
-		if (IS_ERR(utmi_clk))
+		if (IS_ERR(utmi_clk)) {
+			for (j = 0; j < i; j++) {
+				uclk = to_sama7_utmi_clk(utmi_clk[j]);
+				kfree(uclk);

Do the usual error handling path using goto:

{
...
if (IS_ERR(...))
goto error_clk_register;
...

error_clk_register:
kfree();
error_something_else:
clean_up_other_stuff();
return ret;
}

#5 lore raw

PATCH v2 2/3 usb: ohci-at91: Enable OHCI functionality and register into DM

In reply to Sergiu Moga

Register the OHCI driver into DM by properly initializing the required clocks and pins required by the DT node of OHCI. In order for the VBUS to stay enabled, a child_pre_probe method has been added to overcome the DM core disabling it in usb_scan_device: when the generic device_probe method is called, the pinctrl is processed once again, undoing whatever changes have been made in our driver's probe method.

Furthermore, enable CONFIG_DM_GPIO whenever this driver and CONFIG_DM_USB are selected.

---

v1 -> v2:
- squashed 3/4 into this patch
- removed bool clocked
- use *_blk API's
- select DM_GPIO in Kconfig if DM_USB enabled
- use dev_read_u32_default

 drivers/usb/host/Kconfig     |   1 +
 drivers/usb/host/ohci-at91.c | 150 +++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
drivers/usb/host/Kconfig +1 -0
index 6213b3c95f..bb1443a338 100644
@@ -424,6 +424,7 @@ config USB_ATMEL
 	depends on ARCH_AT91
 	select SYS_USB_OHCI_CPU_INIT
 	select USB_OHCI_NEW
+	select DM_GPIO if DM_USB
 
 choice
 	prompt "Clock for OHCI"
drivers/usb/host/ohci-at91.c +150 -0

159 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 2/3] usb: ohci-at91: Enable OHCI functionality and register into DM

On 1/3/23 15:30, Sergiu Moga wrote:

[...]

drivers/usb/host/ohci-at91.c int usb_cpu_init_fail(void) · line 165

Quoted 7 lines
drivers/usb/host/ohci-at91.c +7 -0
+static int ohci_atmel_probe(struct udevice *dev)
+{
+	struct at91_usbh_data *pdata = dev_get_plat(dev);
+	struct ohci_at91_priv *ohci_at91 = dev_get_priv(dev);
+	u32		      i;
+	int		      ret;
+	struct ohci_regs      *regs = (struct ohci_regs *)dev_read_addr(dev);

Use one space after type for indent.

Also, I think the dev_read_addr() can fail, so you want to check the return value.

Quoted 15 lines
drivers/usb/host/ohci-at91.c +15 -0
+	pdata->ports = dev_read_u32_default(dev, "num-ports", 3);
+
+	at91_for_each_port(i, pdata->ports)
+		gpio_request_by_name(dev, "atmel,vbus-gpio", i,
+				     &pdata->vbus_pin[i], GPIOD_IS_OUT |
+				     GPIOD_IS_OUT_ACTIVE);
+
+	ret = clk_get_bulk(dev, &ohci_at91->clks);
+	if (ret)
+		goto fail;
+
+	ret = clk_enable_bulk(&ohci_at91->clks);
+	if (ret)
+		goto fail;
+

drivers/usb/host/ohci-at91.c int usb_cpu_init_fail(void) · line 191

Quoted 10 lines
drivers/usb/host/ohci-at91.c +10 -0
+	ret = at91_start_hc(dev);
+	if (ret)
+		goto fail;
+
+	return ohci_register(dev, regs);
+
+fail:
+	at91_for_each_port(i, pdata->ports)
+		if (dm_gpio_is_valid(&pdata->vbus_pin[i]))
+			gpio_free(pdata->vbus_pin[i].offset);

Add clk_disable in case of failure after at91_start_hc , please check all the fail paths one more time.

[...]

#7 lore raw

PATCH v2 3/3 usb: ohci-at91: Add USB PHY functionality

In reply to Sergiu Moga

Add the ability to enable/disable whatever USB PHY's are passed to the AT91 OHCI driver through DT.

---

v1 -> v2:
- use *_bulk API's

 drivers/usb/host/ohci-at91.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
drivers/usb/host/ohci-at91.c +35 -0
index 9de67df335..fece921d39 100644
@@ -74,6 +74,10 @@ int usb_cpu_init_fail(void)
 #include <usb.h>
 #include "ohci.h"
 
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+#include <generic-phy.h>
+#endif
+
 #define AT91_MAX_USBH_PORTS        3
 
 #define at91_for_each_port(index, ports)					\
@@ -90,15 +94,35 @@ struct at91_usbh_data {
 struct ohci_at91_priv {
 	ohci_t ohci;
 	struct clk_bulk clks;
+
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+	struct phy_bulk phys;
+#endif
 };
 
 static int at91_start_clock(struct ohci_at91_priv *ohci_at91)
 {
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+	int ret;
+
+	ret = generic_phy_power_on_bulk(&ohci_at91->phys);
+	if (ret)
+		return ret;
+#endif
+
 	return clk_enable_bulk(&ohci_at91->clks);
 }
 
 static int at91_stop_clock(struct ohci_at91_priv *ohci_at91)
 {
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+	int ret;
+
+	ret = generic_phy_power_off_bulk(&ohci_at91->phys);
+	if (ret)
+		return ret;
+#endif
+
 	return clk_disable_bulk(&ohci_at91->clks);
 }
 
@@ -179,6 +203,16 @@ static int ohci_atmel_probe(struct udevice *dev)
 	if (ret)
 		goto fail;
 
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+	ret = generic_phy_get_bulk(dev, &ohci_at91->phys);
+	if (ret)
+		goto fail;
+
+	ret = generic_phy_init_bulk(&ohci_at91->phys);
+	if (ret)
+		goto fail;
+#endif
+
 	ret = at91_start_hc(dev);
 	if (ret)
 		goto fail;
@@ -195,6 +229,7 @@ fail:
 
 static const struct udevice_id ohci_usb_ids[] = {
 	{ .compatible = "atmel,at91rm9200-ohci", },
+	{ .compatible = "microchip,sama7g5-ohci", },
 	{ }
 };
 
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v2 3/3] usb: ohci-at91: Add USB PHY functionality

Quoted 33 lines · Sergiu Moga

Add the ability to enable/disable whatever USB PHY's are passed to the AT91 OHCI driver through DT.

---

v1 -> v2:
- use *_bulk API's

  drivers/usb/host/ohci-at91.c | 35 +++++++++++++++++++++++++++++++++++
  1 file changed, 35 insertions(+)
drivers/usb/host/ohci-at91.c +4 -0
index 9de67df335..fece921d39 100644
@@ -74,6 +74,10 @@ int usb_cpu_init_fail(void)
  #include <usb.h>
  #include "ohci.h"
 
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+#include <generic-phy.h>
+#endif
+
  #define AT91_MAX_USBH_PORTS        3
 
  #define at91_for_each_port(index, ports)					\
@@ -90,15 +94,35 @@ struct at91_usbh_data {
  struct ohci_at91_priv {
  	ohci_t ohci;

drivers/usb/host/ohci-at91.c struct at91_usbh_data { · line 105

Quoted 10 lines
drivers/usb/host/ohci-at91.c +5 -0
  	struct clk_bulk clks;
+
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)
+	struct phy_bulk phys;
+#endif
  };
 
  static int at91_start_clock(struct ohci_at91_priv *ohci_at91)
  {
+#if CONFIG_IS_ENABLED(PHY_MICROCHIP_SAMA7_USB)

if (CONFIG_IS_ENABLED(...)) please.

[...]