All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
To: Lukasz Majewski <lukma@denx.de>, Marek Vasut <marex@denx.de>
Cc: Dario Binacchi <dario.binacchi@amarulasolutions.com>,
	 Michael Trimarchi <michael@amarulasolutions.com>,
	u-boot@lists.denx.de,
	 Mattijs Korpershoek <mkorpershoek@baylibre.com>
Subject: [PATCH] usb: gadget: dwc2_udc_otg: implement pullup()
Date: Tue, 10 Jan 2023 10:11:44 +0100	[thread overview]
Message-ID: <20230110-dwc2-pullup-v1-1-2bf9743e59e5@baylibre.com> (raw)

Pullup is used by the usb framework in order to do software-controlled
usb_gadget_connect() and usb_gadget_disconnect().

Implement pullup() for dwc2 using the SOFT_DISCONNECT bit in the dctl
register.

This is especially useful when a gadget disconnection is initiated but
no board_usb_cleanup() is called.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
On some boards using the dwc2 controller, like the Khadas VIM3L, whenever
usb_gadget_release() is called, the D+ and D- lines are in an unknown state.

Because of that, the host can't detect usb disconnection.

It was attempted to be be fixed with [1] but ended up doing the gadget disconnection
too early, creating issues on NXP-based boards which use uuu [2].

By implementing pullup() in the controller driver, we ensure that the disconnection will
only be done when the framework calls usb_gadget_disconnect().

[1] https://lore.kernel.org/all/20220728-reset-usb-controller-v2-1-ef7657ce78b1@baylibre.com/
[2] https://lore.kernel.org/all/20230107164807.3597020-1-dario.binacchi@amarulasolutions.com/
---
 drivers/usb/gadget/dwc2_udc_otg.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/usb/gadget/dwc2_udc_otg.c b/drivers/usb/gadget/dwc2_udc_otg.c
index 77988f78ab30..93ed9712d18a 100644
--- a/drivers/usb/gadget/dwc2_udc_otg.c
+++ b/drivers/usb/gadget/dwc2_udc_otg.c
@@ -236,6 +236,21 @@ static int udc_enable(struct dwc2_udc *dev)
 	return 0;
 }
 
+static int dwc2_gadget_pullup(struct usb_gadget *g, int is_on)
+{
+	unsigned int uTemp;
+
+	is_on = !!is_on;
+	uTemp = readl(&reg->dctl);
+	if (is_on)
+		uTemp = uTemp & ~SOFT_DISCONNECT;
+	else
+		uTemp |= SOFT_DISCONNECT;
+	writel(uTemp, &reg->dctl);
+
+	return 0;
+}
+
 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
 /*
   Register entry point for the peripheral controller driver.
@@ -805,6 +820,7 @@ static void dwc2_fifo_flush(struct usb_ep *_ep)
 }
 
 static const struct usb_gadget_ops dwc2_udc_ops = {
+	.pullup = dwc2_gadget_pullup,
 	/* current versions must always be self-powered */
 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
 	.udc_start		= dwc2_gadget_start,

---
base-commit: 7b84c973b96775576dcff228d865e8570be26c82
change-id: 20230110-dwc2-pullup-5b0f5a073d6b

Best regards,
-- 
Mattijs Korpershoek <mkorpershoek@baylibre.com>


             reply	other threads:[~2023-01-10  9:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-10  9:11 Mattijs Korpershoek [this message]
2023-01-10 13:30 ` [PATCH] usb: gadget: dwc2_udc_otg: implement pullup() Harald Seiler
2023-01-10 13:35   ` Marek Vasut
2023-01-10 13:39   ` Mattijs Korpershoek
2023-01-10 13:45     ` Marek Vasut
2023-01-10 13:55       ` Mattijs Korpershoek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230110-dwc2-pullup-v1-1-2bf9743e59e5@baylibre.com \
    --to=mkorpershoek@baylibre.com \
    --cc=dario.binacchi@amarulasolutions.com \
    --cc=lukma@denx.de \
    --cc=marex@denx.de \
    --cc=michael@amarulasolutions.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.