usb: dwc2: Refactor and update USB DWC2 driver
cover usb: dwc2: Refactor and update USB DWC2 driver
This series improves the USB DWC2 driver by extracting register definitions into a common file for better readability and updating the reset method to reflect changes in version 4.20a, including the new GRSTCTL_CSFTRST_DONE bit for reset completion indication.
Kongyang Liu (2):
usb: dwc2: Extract USB DWC2 register definitions
usb: dwc2: Update reset method for host and device mode
drivers/usb/common/Makefile | 2 + drivers/usb/common/dwc2_core.c | 103 +++ drivers/usb/common/dwc2_core.h | 557 ++++++++++++++++ drivers/usb/gadget/dwc2_udc_otg.c | 124 ++-- drivers/usb/gadget/dwc2_udc_otg_regs.h | 247 +------ drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 306 ++++----- drivers/usb/host/dwc2.c | 392 +++++------ drivers/usb/host/dwc2.h | 736 --------------------- 8 files changed, 1039 insertions(+), 1428 deletions(-) create mode 100644 drivers/usb/common/dwc2_core.c create mode 100644 drivers/usb/common/dwc2_core.h
Signature · 1 line
2.41.0
PATCH 1/2 usb: dwc2: Extract USB DWC2 register definitions
Extract the register definitions and their bit definitions from the USB DWC2 driver host and device into a common file.
---
drivers/usb/common/Makefile | 2 + drivers/usb/common/dwc2_core.c | 53 ++ drivers/usb/common/dwc2_core.h | 556 ++++++++++++++++ drivers/usb/gadget/dwc2_udc_otg.c | 124 ++-- drivers/usb/gadget/dwc2_udc_otg_regs.h | 247 +------ drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 306 ++++----- drivers/usb/host/dwc2.c | 362 +++++----- drivers/usb/host/dwc2.h | 736 --------------------- 8 files changed, 988 insertions(+), 1398 deletions(-) create mode 100644 drivers/usb/common/dwc2_core.c create mode 100644 drivers/usb/common/dwc2_core.h
drivers/usb/common/Makefile +2 -0
@@ -4,6 +4,8 @@ # obj-$(CONFIG_$(SPL_)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
drivers/usb/common/dwc2_core.c new file +53 -0
53 lines not on this page · raw message
drivers/usb/common/dwc2_core.h new file +556 -0
556 lines not on this page · raw message
drivers/usb/gadget/dwc2_udc_otg.c +62 -62
272 lines not on this page · raw message
drivers/usb/gadget/dwc2_udc_otg_regs.h +8 -239
268 lines not on this page · raw message
drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +154 -152
630 lines not on this page · raw message
drivers/usb/host/dwc2.c +153 -209
656 lines not on this page · raw message
drivers/usb/host/dwc2.h +0 -736
557 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.41.0
Part of the diff is not shown here. View the raw message.
Re: [PATCH 1/2] usb: dwc2: Extract USB DWC2 register definitions
On 5/22/24 4:22 PM, Kongyang Liu wrote:
Hi,
sorry for the late reply.
Quoted 16 lines
drivers/usb/common/dwc2_core.c new file +10
-0@@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2024, Kongyang Liu <[email protected]> +*/ + +#include <linux/bitfield.h> +#include <linux/errno.h> +#include <linux/io.h> +#include <wait_bit.h> +
drivers/usb/common/dwc2_core.c line 20
Quoted 10 lines
drivers/usb/common/dwc2_core.c new file +10
-0+#include "dwc2_core.h" + +void 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 (®s->global_regs.grstctl,GRSTCTL_AHBIDLE , true,1000 , false);
Just a quick design point, would it be possible to split this patch into two, one which adds this .global_regs and changes the code accordingly, and another which does the code refactoring/move ? That would make it easier to review.
Re: [PATCH 1/2] usb: dwc2: Extract USB DWC2 register definitions
Quoted 30 lines · Marek Vasut <[email protected]> 于2024年6月23日周日 07:49写道:
On 5/22/24 4:22 PM, Kongyang Liu wrote:
Hi,
sorry for the late reply.
Quoted 23 lines
drivers/usb/common/dwc2_core.c new file +17
-0@@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2024, Kongyang Liu <[email protected]> +*/ + +#include <linux/bitfield.h> +#include <linux/errno.h> +#include <linux/io.h> +#include <wait_bit.h> + +#include "dwc2_core.h" + +void dwc2_flush_tx_fifo (struct dwc2_core_regs *regs,const int num) +{ +int ret; + +log_debug ("Flush Tx FIFO %d\n" , num);
Quoted 8 lines
drivers/usb/common/dwc2_core.c line 20
Quoted 3 lines
drivers/usb/common/dwc2_core.c new file +3
-0+ +/* Wait for AHB master IDLE state */ + ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_AHBIDLE , true,1000 , false);Just a quick design point, would it be possible to split this patch into two, one which adds this .global_regs and changes the code accordingly, and another which does the code refactoring/move ? That would make it easier to review.
This patch only extracts the common parts of the host mode and gadget mode, without any code refactoring. Could you please describe more clearly how the patch should be split?
Best regards
Kongyang Liu
Re: [PATCH 1/2] usb: dwc2: Extract USB DWC2 register definitions
Quoted 40 lines · Kongyang Liu
Quoted 30 lines · Marek Vasut <[email protected]> 于2024年6月23日周日 07:49写道:
On 5/22/24 4:22 PM, Kongyang Liu wrote:
Hi,
sorry for the late reply.
Quoted 23 lines
drivers/usb/common/dwc2_core.c new file +17
-0@@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2024, Kongyang Liu <[email protected]> +*/ + +#include <linux/bitfield.h> +#include <linux/errno.h> +#include <linux/io.h> +#include <wait_bit.h> + +#include "dwc2_core.h" + +void dwc2_flush_tx_fifo (struct dwc2_core_regs *regs,const int num) +{ +int ret; + +log_debug ("Flush Tx FIFO %d\n" , num);Quoted 8 lines
drivers/usb/common/dwc2_core.c line 20
Quoted 3 lines
drivers/usb/common/dwc2_core.c new file +3
-0+ +/* Wait for AHB master IDLE state */ + ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_AHBIDLE , true,1000 , false);Just a quick design point, would it be possible to split this patch into two, one which adds this .global_regs and changes the code accordingly, and another which does the code refactoring/move ? That would make it easier to review.
Quoted 3 lines
This patch only extracts the common parts of the host mode and gadget mode, without any code refactoring. Could you please describe more clearly how the patch should be split?
Sure, sorry for being unclear. There seems to be multiple types of changes that are combined in this patch:
1) Structure reorganization:
- writel(DIEPMSK_INIT, ®->diepmsk);
+ writel(DIEPMSK_INIT, ®->device_regs.diepmsk);
2) Structure rename:
- reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
+ reg = (struct dwc2_core_regs *)pdata->regs_otg;
3) Conversion from (1 << n) to BIT(n)
4) Macro rename:
5) Moving code around between files:
#define DWC2_HWCFG4_IDDIG_FILT_EN (1 << 20)
#define GHWCFG4_IDDIG_FILT_EN BIT(20)
It would be good to have those as separate patches (one type of clean up change per patch), otherwise it is really hard to review such a combined patch.
Thanks !
PATCH 2/2 usb: dwc2: Update reset method for host and device mode
In reply to Kongyang LiuStarting from version 4.20a, there has been a change in the reset method. A new bit, GRSTCTL_CSFTRST_DONE, has been introduced in the GRSTCTL register to indicate whether the reset has been completed.
This patch mainly refers to the patch in the kernel.
---
drivers/usb/common/dwc2_core.c | 50 +++++++++++++++++++++++++++++++ drivers/usb/common/dwc2_core.h | 1 + drivers/usb/gadget/dwc2_udc_otg.c | 2 +- drivers/usb/host/dwc2.c | 36 ++-------------------- 4 files changed, 55 insertions(+), 34 deletions(-)
drivers/usb/common/dwc2_core.c +50 -0
@@ -10,6 +10,56 @@#include "dwc2_core.h" +int dwc2_core_reset (struct dwc2_core_regs *regs) +{ +u32 snpsid; +int ret; +bool host_mode = false; + +if (!(readl (®s->global_regs.gotgctl) &GOTGCTL_CONID_B ) || + (readl (®s->global_regs.gusbcfg) &GUSBCFG_FORCEDEVMODE )) + host_mode = true; + +/* Core Soft Reset */ + snpsid =readl (®s->global_regs.gsnpsid); +writel (GRSTCTL_CSFTRST , ®s->global_regs.grstctl); +if (FIELD_GET (GSNPSID_VER_MASK , snpsid) <0x420a ) { + ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_CSFTRST , + false,1000 , false); +if (ret) { +log_warning ("%s: Waiting for GRSTCTL_CSFTRST timeout\n" , __func__); +return -EBUSY ; + } + }else { + ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_CSFTRST_DONE , + true,1000 , false); +if (ret) { +log_warning ("%s: Waiting for GRSTCTL_CSFTRST_DONE timeout\n" , __func__); +return -EBUSY ; + } +clrsetbits_le32 (®s->global_regs.grstctl,GRSTCTL_CSFTRST ,GRSTCTL_CSFTRST_DONE ); + } + +/* Wait for AHB master IDLE state. */ + ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_AHBIDLE , + true,1000 , false); +if (ret) { +log_warning ("%s: Waiting for GRSTCTL_AHBIDLE timeout\n" , __func__); +return -EBUSY ; + } + +if (host_mode) { + ret =wait_for_bit_le32 (®s->global_regs.gintsts,GINTSTS_CURMODE_HOST , + host_mode,1000 , false); +if (ret) { +log_warning ("%s: Waiting for GINTSTS_CURMODE_HOST timeout\n" , __func__); +return -EBUSY ; + } + } + +return 0 ; +} +void dwc2_flush_tx_fifo (struct dwc2_core_regs *regs,const int num) {int ret;
drivers/usb/common/dwc2_core.h +1 -0
@@ -123,6 +123,7 @@ struct dwc2_core_regs {u8 ep_fifo[16 ][0x1000 ]; }; +int dwc2_core_reset (struct dwc2_core_regs *regs);void dwc2_flush_tx_fifo (struct dwc2_core_regs *regs,const int num);void dwc2_flush_rx_fifo (struct dwc2_core_regs *regs);
drivers/usb/gadget/dwc2_udc_otg.c +1 -1
@@ -469,7 +469,7 @@ static void reconfig_usbd(struct dwc2_udc *dev)u32 max_hw_ep;int pdata_hw_ep; -writel (GRSTCTL_CSFTRST , ®->global_regs.grstctl); +dwc2_core_reset (reg);debug ("Resetting OTG controller\n" );
drivers/usb/host/dwc2.c +3 -33
@@ -109,36 +109,6 @@ static void init_fslspclksel(struct dwc2_core_regs *regs) FIELD_PREP(HCFG_FSLSPCLKSEL_MASK, phyclk)); } -/* -* Do core a soft reset of the core. Be careful with this because it -* resets all the internal state machines of the core. -*/ -static void dwc_otg_core_reset (struct udevice *dev, -struct dwc2_core_regs *regs) -{ -int ret; - -/* Wait for AHB master IDLE state. */ - ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_AHBIDLE , - true,1000 , false); -if (ret) -dev_info (dev,"%s: Timeout!\n" , __func__); - -/* Core Soft Reset */ -writel (GRSTCTL_CSFTRST , ®s->global_regs.grstctl); - ret =wait_for_bit_le32 (®s->global_regs.grstctl,GRSTCTL_CSFTRST , - false,1000 , false); -if (ret) -dev_info (dev,"%s: Timeout!\n" , __func__); - -/* -* Wait for core to come out of reset. -* NOTE: This long sleep is _very_ important, otherwise the core will -* not stay in host mode after a connector ID change! -*/ -mdelay (100 ); -} -#if CONFIG_IS_ENABLED (DM_USB ) && defined(CONFIG_DM_REGULATOR )static int dwc_vbus_supply_init (struct udevice *dev) {
@@ -311,7 +281,7 @@ static void dwc_otg_core_init(struct udevice *dev)writel (usbcfg, ®s->global_regs.gusbcfg);/* Reset the Controller */ -dwc_otg_core_reset (dev, regs); +dwc2_core_reset (regs);/* * This programming sequence needs to happen in FS mode before
@@ -323,7 +293,7 @@ static void dwc_otg_core_init(struct udevice *dev)setbits_le32 (®s->global_regs.gusbcfg,GUSBCFG_PHYSEL );/* Reset after a PHY select */ -dwc_otg_core_reset (dev, regs); +dwc2_core_reset (regs);/* * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
@@ -369,7 +339,7 @@ static void dwc_otg_core_init(struct udevice *dev)writel (usbcfg, ®s->global_regs.gusbcfg);/* Reset after setting the PHY parameters */ -dwc_otg_core_reset (dev, regs); +dwc2_core_reset (regs);#endif usbcfg =readl (®s->global_regs.gusbcfg);
Signature · 1 line
2.41.0