Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B68AC072A2 for ; Sun, 19 Nov 2023 03:54:11 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CCF5E86F40; Sun, 19 Nov 2023 04:54:03 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=holesch.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 277F486F45; Sun, 19 Nov 2023 02:05:52 +0100 (CET) Received: from mout.kundenserver.de (mout.kundenserver.de [217.72.192.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C8DC38615A for ; Sun, 19 Nov 2023 02:05:47 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=holesch.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=simon@holesch.de Received: from localhost ([80.209.221.177]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MEF87-1rEAl52kQn-00ACLF; Sun, 19 Nov 2023 02:05:46 +0100 From: Simon Holesch To: Lukasz Majewski , Mattijs Korpershoek , Marek Vasut Cc: Simon Holesch , u-boot@lists.denx.de Subject: [PATCH 1/2] usb: ci: Fix gadget reinit Date: Sun, 19 Nov 2023 02:04:51 +0100 Message-ID: <20231119010453.294168-1-simon@holesch.de> X-Mailer: git-send-email 2.42.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:OmLxpIAZtx3fm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ueh eYhqFrTo0umcvr1l92jdvkPQY3wqikKGLof7uAuOcaJ9CvJpkk8b6NI0Lzb54+Id18qf83I9m 9xUtlm1HCVPN5pw== X-Mailman-Approved-At: Sun, 19 Nov 2023 04:54:02 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The ChipIdea device controller wasn't properly cleaned up when disabled. So enabling it again left it in a broken state. The problem occurred for example when the host unbinds the driver and binds it again. During the first setup, when the out request is queued, the endpoint is primed (`epprime`). If the endpoint is then disabled, it stayed primed with the initial buffer. So after the endpoint is re-enabled, the device controller and device driver were out of sync: the new out request was in the driver queue head, yet not submitted, but the "complete" function was still called, since the endpoint was primed with the old buffer. With the fastboot function this error led to the (rather confusing) error message "buffer overflow". Fixed by clearing the primed buffers with the `epflush` (`ENDPTFLUSH`) register. Signed-off-by: Simon Holesch --- drivers/usb/gadget/ci_udc.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/usb/gadget/ci_udc.c b/drivers/usb/gadget/ci_udc.c index 2bfacfe59f..4ad991c72b 100644 --- a/drivers/usb/gadget/ci_udc.c +++ b/drivers/usb/gadget/ci_udc.c @@ -354,12 +354,45 @@ static int ci_ep_enable(struct usb_ep *ep, return 0; } +static void ep_disable(int num, int in) +{ + struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; + unsigned int ep_bit, n, enable_bit; + + if (in) { + ep_bit = EPT_TX(num); + enable_bit = CTRL_TXE; + } else { + ep_bit = EPT_RX(num); + enable_bit = CTRL_RXE; + } + + /* clear primed buffers */ + do { + writel(ep_bit, &udc->epflush); + while (readl(&udc->epflush) & ep_bit) + ; + } while (readl(&udc->epstat) & ep_bit); + + /* clear enable bit */ + n = readl(&udc->epctrl[num]); + n &= ~enable_bit; + writel(n, &udc->epctrl[num]); +} + static int ci_ep_disable(struct usb_ep *ep) { struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep); + int num, in; + + num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; + in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0; + + ep_disable(num, in); ci_ep->desc = NULL; ep->desc = NULL; + ci_ep->req_primed = false; return 0; } -- 2.42.1