usb: dwc3: Add dual-role support using OTG core
Subsystems (3)
PATCH 1/4 usb: dwc3: prevent setting PRTCAP to OTG from debugfs
In reply to Roger QuadrosWe don't support PRTCAP == OTG so prevent user from setting it via debugfs.
--- drivers/usb/dwc3/core.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/usb/dwc3/core.c +3 -0
@@ -119,6 +119,9 @@ static void __dwc3_set_mode(struct work_struct *work)if (dwc->dr_mode !=USB_DR_MODE_OTG )return ; +if (dwc->desired_dr_role ==DWC3_GCTL_PRTCAP_OTG ) +return ; +switch (dwc->current_dr_role) {case DWC3_GCTL_PRTCAP_HOST :dwc3_host_exit (dwc);
PATCH 2/4 usb: dwc3: core.h: add some register definitions
In reply to Roger QuadrosAdd OTG and GHWPARAMS6 register definitions
--- drivers/usb/dwc3/core.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
drivers/usb/dwc3/core.h +82 -0
@@ -199,6 +199,15 @@#define DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS BIT(28)#define DWC3_GUCTL1_DEV_L1_EXIT_BY_HW BIT(24) +/* Global Status Register */ +#define DWC3_GSTS_OTG_IP BIT(10) +#define DWC3_GSTS_BC_IP BIT(9) +#define DWC3_GSTS_ADP_IP BIT(8) +#define DWC3_GSTS_HOST_IP BIT(7) +#define DWC3_GSTS_DEVICE_IP BIT(6) +#define DWC3_GSTS_CSR_TIMEOUT BIT(5) +#define DWC3_GSTS_BUS_ERR_ADDR_VLD BIT(4) +/* Global USB2 PHY Configuration Register */ #define DWC3_GUSB2PHYCFG_PHYSOFTRST BIT(31)#define DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS BIT(30)
@@ -284,6 +293,11 @@#define DWC3_MAX_HIBER_SCRATCHBUFS 15/* Global HWPARAMS6 Register */ +#define DWC3_GHWPARAMS6_BCSUPPORT BIT(14) +#define DWC3_GHWPARAMS6_OTG3SUPPORT BIT(13) +#define DWC3_GHWPARAMS6_ADPSUPPORT BIT(12) +#define DWC3_GHWPARAMS6_HNPSUPPORT BIT(11) +#define DWC3_GHWPARAMS6_SRPSUPPORT BIT(10)#define DWC3_GHWPARAMS6_EN_FPGA BIT(7)/* Global HWPARAMS7 Register */
@@ -465,6 +479,74 @@#define DWC3_DEV_IMOD_INTERVAL_SHIFT 0#define DWC3_DEV_IMOD_INTERVAL_MASK (0xffff << 0) +/* OTG Configuration Register */ +#define DWC3_OCFG_DISPWRCUTTOFF BIT(5) +#define DWC3_OCFG_HIBDISMASK BIT(4) +#define DWC3_OCFG_SFTRSTMASK BIT(3) +#define DWC3_OCFG_OTGVERSION BIT(2) +#define DWC3_OCFG_HNPCAP BIT(1) +#define DWC3_OCFG_SRPCAP BIT(0) + +/* OTG CTL Register */ +#define DWC3_OCTL_OTG3GOERR BIT(7) +#define DWC3_OCTL_PERIMODE BIT(6) +#define DWC3_OCTL_PRTPWRCTL BIT(5) +#define DWC3_OCTL_HNPREQ BIT(4) +#define DWC3_OCTL_SESREQ BIT(3) +#define DWC3_OCTL_TERMSELIDPULSE BIT(2) +#define DWC3_OCTL_DEVSETHNPEN BIT(1) +#define DWC3_OCTL_HSTSETHNPEN BIT(0) + +/* OTG Event Register */ +#define DWC3_OEVT_DEVICEMODE BIT(31) +#define DWC3_OEVT_XHCIRUNSTPSET BIT(27) +#define DWC3_OEVT_DEVRUNSTPSET BIT(26) +#define DWC3_OEVT_HIBENTRY BIT(25) +#define DWC3_OEVT_CONIDSTSCHNG BIT(24) +#define DWC3_OEVT_HRRCONFNOTIF BIT(23) +#define DWC3_OEVT_HRRINITNOTIF BIT(22) +#define DWC3_OEVT_ADEVIDLE BIT(21) +#define DWC3_OEVT_ADEVBHOSTEND BIT(20) +#define DWC3_OEVT_ADEVHOST BIT(19) +#define DWC3_OEVT_ADEVHNPCHNG BIT(18) +#define DWC3_OEVT_ADEVSRPDET BIT(17) +#define DWC3_OEVT_ADEVSESSENDDET BIT(16) +#define DWC3_OEVT_BDEVBHOSTEND BIT(11) +#define DWC3_OEVT_BDEVHNPCHNG BIT(10) +#define DWC3_OEVT_BDEVSESSVLDDET BIT(9) +#define DWC3_OEVT_BDEVVBUSCHNG BIT(8) +#define DWC3_OEVT_BSESSVLD BIT(3) +#define DWC3_OEVT_HSTNEGSTS BIT(2) +#define DWC3_OEVT_SESREQSTS BIT(1) +#define DWC3_OEVT_ERROR BIT(0) + +/* OTG Event Enable Register */ +#define DWC3_OEVTEN_XHCIRUNSTPSETEN BIT(27) +#define DWC3_OEVTEN_DEVRUNSTPSETEN BIT(26) +#define DWC3_OEVTEN_HIBENTRYEN BIT(25) +#define DWC3_OEVTEN_CONIDSTSCHNGEN BIT(24) +#define DWC3_OEVTEN_HRRCONFNOTIFEN BIT(23) +#define DWC3_OEVTEN_HRRINITNOTIFEN BIT(22) +#define DWC3_OEVTEN_ADEVIDLEEN BIT(21) +#define DWC3_OEVTEN_ADEVBHOSTENDEN BIT(20) +#define DWC3_OEVTEN_ADEVHOSTEN BIT(19) +#define DWC3_OEVTEN_ADEVHNPCHNGEN BIT(18) +#define DWC3_OEVTEN_ADEVSRPDETEN BIT(17) +#define DWC3_OEVTEN_ADEVSESSENDDETEN BIT(16) +#define DWC3_OEVTEN_BDEVBHOSTENDEN BIT(11) +#define DWC3_OEVTEN_BDEVHNPCHNGEN BIT(10) +#define DWC3_OEVTEN_BDEVSESSVLDDETEN BIT(9) +#define DWC3_OEVTEN_BDEVVBUSCHNGEN BIT(8) + +/* OTG Status Register */ +#define DWC3_OSTS_DEVRUNSTP BIT(13) +#define DWC3_OSTS_XHCIRUNSTP BIT(12) +#define DWC3_OSTS_PERIPHERALSTATE BIT(4) +#define DWC3_OSTS_XHCIPRTPOWER BIT(3) +#define DWC3_OSTS_BSESVLD BIT(2) +#define DWC3_OSTS_VBUSVLD BIT(1) +#define DWC3_OSTS_CONIDSTS BIT(0) +/* Structures */ struct dwc3_trb ;
PATCH 3/4 usb: dwc3: add dual role support using OTG block
In reply to Roger QuadrosThis is useful on platforms (e.g. TI AM437x) that don't have ID available on a GPIO but do have the OTG block.
We can obtain the ID state via the OTG block and use it for dual-role switching.
--- drivers/usb/dwc3/core.c | 73 +++++-- drivers/usb/dwc3/core.h | 29 +++ drivers/usb/dwc3/drd.c | 492 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 565 insertions(+), 29 deletions(-)
drivers/usb/dwc3/core.c +62 -11
@@ -89,10 +89,7 @@ static int dwc3_get_dr_mode(struct dwc3 *dwc)return 0 ; } -static void dwc3_event_buffers_cleanup (struct dwc3 *dwc); -static int dwc3_event_buffers_setup (struct dwc3 *dwc); - -static void dwc3_set_prtcap (struct dwc3 *dwc,u32 mode) +void dwc3_set_prtcap (struct dwc3 *dwc,u32 mode) {u32 reg;
@@ -110,16 +107,19 @@ static void __dwc3_set_mode(struct work_struct *work)unsigned long flags;int ret; -if (!dwc->desired_dr_role) +if (dwc->dr_mode !=USB_DR_MODE_OTG )return ; -if (dwc->desired_dr_role == dwc->current_dr_role) +if (dwc->current_dr_role ==DWC3_GCTL_PRTCAP_OTG ) +dwc3_otg_update (dwc,0 ); + +if (!dwc->desired_dr_role)return ; -if (dwc->dr_mode !=USB_DR_MODE_OTG ) +if (dwc->desired_dr_role == dwc->current_dr_role)return ; -if (dwc->desired_dr_role ==DWC3_GCTL_PRTCAP_OTG ) +if (dwc->desired_dr_role ==DWC3_GCTL_PRTCAP_OTG && dwc->edev)return ;switch (dwc->current_dr_role) {
@@ -130,6 +130,13 @@ static void __dwc3_set_mode(struct work_struct *work)dwc3_gadget_exit (dwc);dwc3_event_buffers_cleanup (dwc);break ; +case DWC3_GCTL_PRTCAP_OTG : +dwc3_otg_exit (dwc); +spin_lock_irqsave (&dwc->lock, flags); + dwc->desired_otg_role =DWC3_OTG_ROLE_IDLE ; +spin_unlock_irqrestore (&dwc->lock, flags); +dwc3_otg_update (dwc,1 ); +break ;default :break ; }
@@ -164,9 +171,14 @@ static void __dwc3_set_mode(struct work_struct *work)if (ret)dev_err (dwc->dev,"failed to initialize peripheral\n" );break ; +case DWC3_GCTL_PRTCAP_OTG : +dwc3_otg_init (dwc); +dwc3_otg_update (dwc,0 ); +break ;default :break ; } + }void dwc3_set_mode (struct dwc3 *dwc,u32 mode)
@@ -347,7 +359,7 @@ static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)* * Returns 0 on success otherwise negative errno. */ -static int dwc3_event_buffers_setup (struct dwc3 *dwc) +int dwc3_event_buffers_setup (struct dwc3 *dwc) {struct dwc3_event_buffer *evt;
@@ -364,7 +376,7 @@ static int dwc3_event_buffers_setup(struct dwc3 *dwc)return 0 ; } -static void dwc3_event_buffers_cleanup (struct dwc3 *dwc) +void dwc3_event_buffers_cleanup (struct dwc3 *dwc) {struct dwc3_event_buffer *evt;
@@ -1296,6 +1308,20 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)if (!PMSG_IS_AUTO (msg))dwc3_core_exit (dwc);break ; +case DWC3_GCTL_PRTCAP_OTG : +/* do nothing during runtime_suspend */ +if (PMSG_IS_AUTO (msg)) +break ; + +if (dwc->current_otg_role ==DWC3_OTG_ROLE_DEVICE ) { +spin_lock_irqsave (&dwc->lock, flags); +dwc3_gadget_suspend (dwc); +spin_unlock_irqrestore (&dwc->lock, flags); + } + +dwc3_otg_exit (dwc); +dwc3_core_exit (dwc); +break ;default :/* do nothing */ break ;
@@ -1327,6 +1353,27 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)return ret; }break ; +case DWC3_GCTL_PRTCAP_OTG : +/* nothing to do on runtime_resume */ +if (PMSG_IS_AUTO (msg)) +break ; + + ret =dwc3_core_init (dwc); +if (ret) +return ret; + +dwc3_set_prtcap (dwc, dwc->current_dr_role); + +dwc3_otg_init (dwc); +if (dwc->current_otg_role ==DWC3_OTG_ROLE_HOST ) { +dwc3_otg_host_init (dwc); + }else if (dwc->current_otg_role ==DWC3_OTG_ROLE_DEVICE ) { +spin_lock_irqsave (&dwc->lock, flags); +dwc3_gadget_resume (dwc); +spin_unlock_irqrestore (&dwc->lock, flags); + } + +break ;default :/* do nothing */ break ;
@@ -1440,7 +1487,11 @@ static void dwc3_complete(struct device *dev)spin_lock_irqsave (&dwc->lock, flags); dwc->dr_keep_role = false;spin_unlock_irqrestore (&dwc->lock, flags); -dwc3_drd_update (dwc); + +if (dwc->edev) +dwc3_drd_update (dwc); +else if (dwc->current_dr_role ==DWC3_GCTL_PRTCAP_OTG ) +dwc3_set_mode (dwc,DWC3_GCTL_PRTCAP_OTG ); } }
drivers/usb/dwc3/core.h +29 -0
@@ -58,6 +58,11 @@#define DWC3_DEVICE_EVENT_CMD_CMPL 10#define DWC3_DEVICE_EVENT_OVERFLOW 11 +/* Controller's role while using the OTG block */ +#define DWC3_OTG_ROLE_IDLE 0 +#define DWC3_OTG_ROLE_HOST 1 +#define DWC3_OTG_ROLE_DEVICE 2 +#define DWC3_GEVNTCOUNT_MASK 0xfffc#define DWC3_GEVNTCOUNT_EHB BIT(31)#define DWC3_GSNPSID_MASK 0xffff0000
@@ -861,6 +866,10 @@ struct dwc3_scratchpad_array {* @regs_size: address space size * @fladj: frame length adjustment * @irq_gadget: peripheral controller's IRQ number +* @otg_irq: IRQ number for OTG IRQs +* @current_otg_role: current role of operation while using the OTG block +* @desired_otg_role: desired role of operation while using the OTG block +* @otg_restart_host: flag that OTG controller needs to restart host * @nr_scratch: number of scratch buffers * @u1u2: only used on revisions <1.83a for workaround * @maximum_speed: maximum speed requested (mainly for testing purposes)
@@ -991,6 +1000,10 @@ struct dwc3 {u32 fladj;u32 irq_gadget; +u32 otg_irq; +u32 current_otg_role; +u32 desired_otg_role; +bool otg_restart_host;u32 nr_scratch;u32 u1u2;u32 maximum_speed;
@@ -1251,6 +1264,7 @@ struct dwc3_gadget_ep_cmd_params {#define DWC3_HAS_OTG BIT(3)/* prototypes */ +void dwc3_set_prtcap (struct dwc3 *dwc,u32 mode);void dwc3_set_mode (struct dwc3 *dwc,u32 mode);u32 dwc3_core_fifo_space (struct dwc3_ep *dep,u8 type);
@@ -1268,6 +1282,9 @@ static inline bool dwc3_is_usb31(struct dwc3 *dwc)bool dwc3_has_imod (struct dwc3 *dwc); +int dwc3_event_buffers_setup (struct dwc3 *dwc); +void dwc3_event_buffers_cleanup (struct dwc3 *dwc); +#if IS_ENABLED (CONFIG_USB_DWC3_HOST ) ||IS_ENABLED (CONFIG_USB_DWC3_DUAL_ROLE )int dwc3_host_init (struct dwc3 *dwc);void dwc3_host_exit (struct dwc3 *dwc);
@@ -1312,6 +1329,10 @@ static inline int dwc3_send_gadget_generic_command(struct dwc3 *dwc,int dwc3_drd_init (struct dwc3 *dwc);void dwc3_drd_exit (struct dwc3 *dwc);void dwc3_drd_update (struct dwc3 *dwc); +void dwc3_otg_init (struct dwc3 *dwc); +void dwc3_otg_exit (struct dwc3 *dwc); +void dwc3_otg_update (struct dwc3 *dwc,bool ignore_idstatus); +void dwc3_otg_host_init (struct dwc3 *dwc);#else static inline int dwc3_drd_init (struct dwc3 *dwc) {return 0 ; }
@@ -1319,6 +1340,14 @@ static inline void dwc3_drd_exit(struct dwc3 *dwc) { }static inline void dwc3_drd_update (struct dwc3 *dwc); { } +static inline void dwc3_otg_init (struct dwc3 *dwc) +{ } +static inline void dwc3_otg_exit (struct dwc3 *dwc) +{ } +static void dwc3_otg_update (struct dwc3 *dwc,bool ignore_idstatus) +{ } +static void dwc3_otg_host_init (struct dwc3 *dwc) +{ }#endif /* power management interface */
drivers/usb/dwc3/drd.c +474 -18
526 lines not on this page · raw message
PATCH 4/4 ARM: dts: am43xx: Enable dual-role mode for USB1
In reply to Roger QuadrosUSB1 port is micro-AB type and can function as peripheral as well as host. Enable dual-role mode for USB1.
--- arch/arm/boot/dts/am437x-gp-evm.dts | 2 +- arch/arm/boot/dts/am437x-sk-evm.dts | 2 +- arch/arm/boot/dts/am43x-epos-evm.dts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
arch/arm/boot/dts/am437x-gp-evm.dts +1 -1
@@ -803,7 +803,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; };
arch/arm/boot/dts/am437x-sk-evm.dts +1 -1
@@ -598,7 +598,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&usb1_pins>;
arch/arm/boot/dts/am43x-epos-evm.dts +1 -1
@@ -854,7 +854,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; };
cover usb: dwc3: Add dual-role support using OTG core
Hi Felipe,
Some platforms (e.g. TI's AM437x) don't have USB ID pin state available over GPIO/extcon but need to rely on the DWC3 core's OTG block to get the ID pin state instead.
This series implements simple dual-role functionality using DWC3's OTG block. Debugfs 'mode' override is also functional so user can switch between "otg", "host" or "device" modes for debug.
Although system suspend/resume isn't working yet in mainline for AM437x, I've tested this series for system suspend/resume using a local tree.
This series depends on [1] and [2]
[1] https://www.mail-archive.com/[email protected]/msg98771.html
[2] https://www.mail-archive.com/[email protected]/msg98848.html
Roger Quadros (4):
usb: dwc3: prevent setting PRTCAP to OTG from debugfs
usb: dwc3: core.h: add some register definitions
usb: dwc3: add dual role support using OTG block
ARM: dts: am43xx: Enable dual-role mode for USB1
arch/arm/boot/dts/am437x-gp-evm.dts | 2 +- arch/arm/boot/dts/am437x-sk-evm.dts | 2 +- arch/arm/boot/dts/am43x-epos-evm.dts | 2 +- drivers/usb/dwc3/core.c | 70 ++++- drivers/usb/dwc3/core.h | 111 ++++++++ drivers/usb/dwc3/drd.c | 492 +++++++++++++++++++++++++++++++++-- 6 files changed, 650 insertions(+), 29 deletions(-)
Signature · 4 lines
cheers, -roger Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
Re: [PATCH 4/4] ARM: dts: am43xx: Enable dual-role mode for USB1
In reply to Roger Quadros+Tony
Quoted 50 lines · Roger Quadros
USB1 port is micro-AB type and can function as peripheral as well as host. Enable dual-role mode for USB1.
Signed-off-by Roger Quadros <[email protected]>--- arch/arm/boot/dts/am437x-gp-evm.dts | 2 +- arch/arm/boot/dts/am437x-sk-evm.dts | 2 +- arch/arm/boot/dts/am43x-epos-evm.dts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)arch/arm/boot/dts/am437x-gp-evm.dts +1
-1@@ -803,7 +803,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; };arch/arm/boot/dts/am437x-sk-evm.dts +1
-1@@ -598,7 +598,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&usb1_pins>;arch/arm/boot/dts/am43x-epos-evm.dts +1
-1@@ -854,7 +854,7 @@ }; &usb1 { - dr_mode = "peripheral"; + dr_mode = "otg"; status = "okay"; };
Signature · 8 lines
cheers, -roger Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 4/4] ARM: dts: am43xx: Enable dual-role mode for USB1
Quoted 5 lines · Roger Quadros
+Tony
Quoted 2 lines · Roger Quadros
USB1 port is micro-AB type and can function as peripheral as well as host. Enable dual-role mode for USB1.
Looks like I don't have this one, please resend it separately when it's safe to apply.
Regards,
Tony
Hi Felipe,
Quoted 6 lines · Roger Quadros
Hi Felipe,
Some platforms (e.g. TI's AM437x) don't have USB ID pin state available over GPIO/extcon but need to rely on the DWC3 core's OTG block to get the ID pin state instead.
Quoted 6 lines
This series implements simple dual-role functionality using DWC3's OTG block. Debugfs 'mode' override is also functional so user can switch between "otg", "host" or "device" modes for debug.
Although system suspend/resume isn't working yet in mainline for AM437x, I've tested this series for system suspend/resume using a local tree.
Any comments on this series?
Quoted 21 lines
This series depends on [1] and [2]
[1] https://www.mail-archive.com/[email protected]/msg98771.html
[2] https://www.mail-archive.com/[email protected]/msg98848.html
Roger Quadros (4):
usb: dwc3: prevent setting PRTCAP to OTG from debugfs
usb: dwc3:core.h: add some register definitions
usb: dwc3: add dual role support using OTG block
ARM: dts: am43xx: Enable dual-role mode for USB1arch/arm/boot/dts/am437x-gp-evm.dts | 2 +- arch/arm/boot/dts/am437x-sk-evm.dts | 2 +- arch/arm/boot/dts/am43x-epos-evm.dts | 2 +- drivers/usb/dwc3/core.c | 70 ++++- drivers/usb/dwc3/core.h | 111 ++++++++ drivers/usb/dwc3/drd.c | 492 +++++++++++++++++++++++++++++++++-- 6 files changed, 650 insertions(+), 29 deletions(-)
Signature · 4 lines
cheers, -roger Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki