1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drd.c - DesignWare USB2 DRD Controller Dual-role support 4 * 5 * Copyright (C) 2020 STMicroelectronics 6 * 7 * Author(s): Amelie Delaunay <amelie.delaunay@st.com> 8 */ 9 10 #include <linux/clk.h> 11 #include <linux/iopoll.h> 12 #include <linux/platform_device.h> 13 #include <linux/usb/role.h> 14 #include "core.h" 15 16 static void dwc2_ovr_init(struct dwc2_hsotg *hsotg) 17 { 18 unsigned long flags; 19 u32 gotgctl; 20 21 spin_lock_irqsave(&hsotg->lock, flags); 22 23 gotgctl = dwc2_readl(hsotg, GOTGCTL); 24 gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN; 25 gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS; 26 gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL); 27 dwc2_writel(hsotg, gotgctl, GOTGCTL); 28 29 spin_unlock_irqrestore(&hsotg->lock, flags); 30 31 dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST)); 32 } 33 34 static int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid) 35 { 36 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL); 37 38 /* Check if A-Session is already in the right state */ 39 if ((valid && (gotgctl & GOTGCTL_ASESVLD)) || 40 (!valid && !(gotgctl & GOTGCTL_ASESVLD))) 41 return -EALREADY; 42 43 gotgctl &= ~GOTGCTL_BVALOVAL; 44 if (valid) 45 gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL; 46 else 47 gotgctl &= ~(GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL); 48 dwc2_writel(hsotg, gotgctl, GOTGCTL); 49 50 return 0; 51 } 52 53 static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid) 54 { 55 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL); 56 57 /* Check if B-Session is already in the right state */ 58 if ((valid && (gotgctl & GOTGCTL_BSESVLD)) || 59 (!valid && !(gotgctl & GOTGCTL_BSESVLD))) 60 return -EALREADY; 61 62 gotgctl &= ~GOTGCTL_AVALOVAL; 63 if (valid) 64 gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL; 65 else 66 gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL); 67 dwc2_writel(hsotg, gotgctl, GOTGCTL); 68 69 return 0; 70 } 71 72 static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) 73 { 74 struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw); 75 unsigned long flags; 76 int already = 0; 77 78 /* Skip session not in line with dr_mode */ 79 if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) || 80 (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)) 81 return -EINVAL; 82 83 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \ 84 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) 85 /* Skip session if core is in test mode */ 86 if (role == USB_ROLE_NONE && hsotg->test_mode) { 87 dev_dbg(hsotg->dev, "Core is in test mode\n"); 88 return -EBUSY; 89 } 90 #endif 91 92 /* 93 * In case of USB_DR_MODE_PERIPHERAL, clock is disabled at the end of 94 * the probe and enabled on udc_start. 95 * If role-switch set is called before the udc_start, we need to enable 96 * the clock to read/write GOTGCTL and GUSBCFG registers to override 97 * mode and sessions. It is the case if cable is plugged at boot. 98 */ 99 if (!hsotg->ll_hw_enabled && hsotg->clk) { 100 int ret = clk_prepare_enable(hsotg->clk); 101 102 if (ret) 103 return ret; 104 } 105 106 spin_lock_irqsave(&hsotg->lock, flags); 107 108 if (role == USB_ROLE_HOST) { 109 already = dwc2_ovr_avalid(hsotg, true); 110 } else if (role == USB_ROLE_DEVICE) { 111 already = dwc2_ovr_bvalid(hsotg, true); 112 /* This clear DCTL.SFTDISCON bit */ 113 dwc2_hsotg_core_connect(hsotg); 114 } else { 115 if (dwc2_is_device_mode(hsotg)) { 116 if (!dwc2_ovr_bvalid(hsotg, false)) 117 /* This set DCTL.SFTDISCON bit */ 118 dwc2_hsotg_core_disconnect(hsotg); 119 } else { 120 dwc2_ovr_avalid(hsotg, false); 121 } 122 } 123 124 spin_unlock_irqrestore(&hsotg->lock, flags); 125 126 if (!already && hsotg->dr_mode == USB_DR_MODE_OTG) 127 /* This will raise a Connector ID Status Change Interrupt */ 128 dwc2_force_mode(hsotg, role == USB_ROLE_HOST); 129 130 if (!hsotg->ll_hw_enabled && hsotg->clk) 131 clk_disable_unprepare(hsotg->clk); 132 133 dev_dbg(hsotg->dev, "%s-session valid\n", 134 role == USB_ROLE_NONE ? "No" : 135 role == USB_ROLE_HOST ? "A" : "B"); 136 137 return 0; 138 } 139 140 int dwc2_drd_init(struct dwc2_hsotg *hsotg) 141 { 142 struct usb_role_switch_desc role_sw_desc = {0}; 143 struct usb_role_switch *role_sw; 144 int ret; 145 146 if (!device_property_read_bool(hsotg->dev, "usb-role-switch")) 147 return 0; 148 149 role_sw_desc.driver_data = hsotg; 150 role_sw_desc.fwnode = dev_fwnode(hsotg->dev); 151 role_sw_desc.set = dwc2_drd_role_sw_set; 152 role_sw_desc.allow_userspace_control = true; 153 154 role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc); 155 if (IS_ERR(role_sw)) { 156 ret = PTR_ERR(role_sw); 157 dev_err(hsotg->dev, 158 "failed to register role switch: %d\n", ret); 159 return ret; 160 } 161 162 hsotg->role_sw = role_sw; 163 164 /* Enable override and initialize values */ 165 dwc2_ovr_init(hsotg); 166 167 return 0; 168 } 169 170 void dwc2_drd_suspend(struct dwc2_hsotg *hsotg) 171 { 172 u32 gintsts, gintmsk; 173 174 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) { 175 gintmsk = dwc2_readl(hsotg, GINTMSK); 176 gintmsk &= ~GINTSTS_CONIDSTSCHNG; 177 dwc2_writel(hsotg, gintmsk, GINTMSK); 178 gintsts = dwc2_readl(hsotg, GINTSTS); 179 dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS); 180 } 181 } 182 183 void dwc2_drd_resume(struct dwc2_hsotg *hsotg) 184 { 185 u32 gintsts, gintmsk; 186 187 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) { 188 gintsts = dwc2_readl(hsotg, GINTSTS); 189 dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS); 190 gintmsk = dwc2_readl(hsotg, GINTMSK); 191 gintmsk |= GINTSTS_CONIDSTSCHNG; 192 dwc2_writel(hsotg, gintmsk, GINTMSK); 193 } 194 } 195 196 void dwc2_drd_exit(struct dwc2_hsotg *hsotg) 197 { 198 if (hsotg->role_sw) 199 usb_role_switch_unregister(hsotg->role_sw); 200 } 201