summaryrefslogtreecommitdiff
path: root/drivers/usb/common
diff options
context:
space:
mode:
authorKongyang Liu <[email protected]>2025-01-10 21:55:26 +0800
committerMattijs Korpershoek <[email protected]>2025-06-02 09:57:43 +0200
commitc5d685b8993cf9e2fa4321dc3f9f866d5655e976 (patch)
tree7afe7389f9cd16dd3161181fa8093ad56bada52d /drivers/usb/common
parent24b0e2604e2980dd04078b85a7a8929588dc0988 (diff)
usb: dwc2: Unify flush and reset logic with v4.20a support
This patch merges flush and reset logic for both host and gadget code into a common set of functions, reducing duplication. It also adds support for the updated reset logic to compatible with core version since v4.20a. This patch mainly refers to the patch in the kernel. link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=65dc2e725286106f99c6f6b78e3d9c52c15f3a9c Signed-off-by: Kongyang Liu <[email protected]> Tested-by: Peter Robinson <[email protected]> Signed-off-by: Junhui Liu <[email protected]> Reviewed-by: Mattijs Korpershoek <[email protected]> Reviewed-by: Marek Vasut <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mattijs Korpershoek <[email protected]>
Diffstat (limited to 'drivers/usb/common')
-rw-r--r--drivers/usb/common/Makefile2
-rw-r--r--drivers/usb/common/dwc2_core.c131
-rw-r--r--drivers/usb/common/dwc2_core.h4
3 files changed, 137 insertions, 0 deletions
diff --git a/drivers/usb/common/Makefile b/drivers/usb/common/Makefile
index 4c597c166c6..db8f35c10c4 100644
--- a/drivers/usb/common/Makefile
+++ b/drivers/usb/common/Makefile
@@ -4,6 +4,8 @@
#
obj-$(CONFIG_$(PHASE_)DM_USB) += common.o
+obj-$(CONFIG_USB_DWC2) += dwc2_core.o
+obj-$(CONFIG_USB_GADGET_DWC2_OTG) += dwc2_core.o
obj-$(CONFIG_USB_ISP1760) += usb_urb.o
obj-$(CONFIG_USB_MUSB_HOST) += usb_urb.o
obj-$(CONFIG_USB_MUSB_GADGET) += usb_urb.o
diff --git a/drivers/usb/common/dwc2_core.c b/drivers/usb/common/dwc2_core.c
new file mode 100644
index 00000000000..63062d5cc94
--- /dev/null
+++ b/drivers/usb/common/dwc2_core.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024-2025, Kongyang Liu <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <wait_bit.h>
+
+#include "dwc2_core.h"
+
+int dwc2_core_reset(struct dwc2_core_regs *regs)
+{
+ u32 snpsid;
+ int ret;
+ bool host_mode = false;
+
+ if (!(readl(&regs->global_regs.gotgctl) & GOTGCTL_CONID_B) ||
+ (readl(&regs->global_regs.gusbcfg) & GUSBCFG_FORCEDEVMODE))
+ host_mode = true;
+
+ /* Core Soft Reset */
+ snpsid = readl(&regs->global_regs.gsnpsid);
+ writel(GRSTCTL_CSFTRST, &regs->global_regs.grstctl);
+ if (FIELD_GET(GSNPSID_VER_MASK, snpsid) < 0x420a) {
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST,
+ false, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_CSFTRST timeout\n", __func__);
+ return ret;
+ }
+ } else {
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST_DONE,
+ true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_CSFTRST_DONE timeout\n", __func__);
+ return ret;
+ }
+ clrsetbits_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST, GRSTCTL_CSFTRST_DONE);
+ }
+
+ /* Wait for AHB master IDLE state. */
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE,
+ true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+ return ret;
+ }
+
+ if (host_mode) {
+ ret = wait_for_bit_le32(&regs->global_regs.gintsts, GINTSTS_CURMODE_HOST,
+ host_mode, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GINTSTS_CURMODE_HOST timeout\n", __func__);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+int dwc2_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
+{
+ int ret;
+
+ log_debug("Flush Tx FIFO %d\n", num);
+
+ /* Wait for AHB master IDLE state */
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+ return ret;
+ }
+
+ writel(GRSTCTL_TXFFLSH | FIELD_PREP(GRSTCTL_TXFNUM_MASK, num), &regs->global_regs.grstctl);
+
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_TXFFLSH, false, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_TXFFLSH timeout\n", __func__);
+ return ret;
+ }
+
+ /*
+ * Wait for at least 3 PHY clocks.
+ *
+ * The PHY clock frequency can be configured to 6/30/48/60 MHz
+ * based on the speed mode. A fixed delay of 1us ensures that the
+ * wait time is sufficient even at the lowest PHY clock frequency
+ * (6 MHz), where 1us corresponds to twice the duration of 3 PHY
+ * clocks.
+ */
+ udelay(1);
+
+ return 0;
+}
+
+int dwc2_flush_rx_fifo(struct dwc2_core_regs *regs)
+{
+ int ret;
+
+ log_debug("Flush Rx FIFO\n");
+
+ /* Wait for AHB master IDLE state */
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+ return ret;
+ }
+
+ writel(GRSTCTL_RXFFLSH, &regs->global_regs.grstctl);
+
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_RXFFLSH, false, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_RXFFLSH timeout\n", __func__);
+ return ret;
+ }
+
+ /*
+ * Wait for at least 3 PHY clocks.
+ *
+ * The PHY clock frequency can be configured to 6/30/48/60 MHz
+ * based on the speed mode. A fixed delay of 1us ensures that the
+ * wait time is sufficient even at the lowest PHY clock frequency
+ * (6 MHz), where 1us corresponds to twice the duration of 3 PHY
+ * clocks.
+ */
+ udelay(1);
+
+ return 0;
+}
diff --git a/drivers/usb/common/dwc2_core.h b/drivers/usb/common/dwc2_core.h
index 862d3b3691c..1897ad7cb54 100644
--- a/drivers/usb/common/dwc2_core.h
+++ b/drivers/usb/common/dwc2_core.h
@@ -125,6 +125,10 @@ struct dwc2_core_regs {
u8 ep_fifo[16][0x1000]; /* 0x1000 */
};
+int dwc2_core_reset(struct dwc2_core_regs *regs);
+int dwc2_flush_tx_fifo(struct dwc2_core_regs *regs, const int num);
+int dwc2_flush_rx_fifo(struct dwc2_core_regs *regs);
+
/* Core Global Register */
#define GOTGCTL_CHIRPEN BIT(27)
#define GOTGCTL_MULT_VALID_BC_MASK GENMASK(26, 22)