blob: 1b39c4776369b476223c9c2a880dc1521867c9a3 [file] [log] [blame]
Amelie Delaunay17f93402020-09-09 11:35:10 +02001// 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
Amelie Delaunay8d387f62021-10-05 11:53:04 +020010#include <linux/clk.h>
Amelie Delaunay17f93402020-09-09 11:35:10 +020011#include <linux/iopoll.h>
12#include <linux/platform_device.h>
13#include <linux/usb/role.h>
14#include "core.h"
15
Fabrice Gasnier3ad02e02021-12-06 16:56:43 +010016#define dwc2_ovr_gotgctl(gotgctl) \
17 ((gotgctl) |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN | \
18 GOTGCTL_DBNCE_FLTR_BYPASS)
19
Amelie Delaunay17f93402020-09-09 11:35:10 +020020static void dwc2_ovr_init(struct dwc2_hsotg *hsotg)
21{
22 unsigned long flags;
23 u32 gotgctl;
24
25 spin_lock_irqsave(&hsotg->lock, flags);
26
27 gotgctl = dwc2_readl(hsotg, GOTGCTL);
Fabrice Gasnier3ad02e02021-12-06 16:56:43 +010028 dwc2_ovr_gotgctl(gotgctl);
Amelie Delaunay17f93402020-09-09 11:35:10 +020029 gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
Fabrice Gasniere14acb82021-12-06 16:56:42 +010030 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
31 gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
32 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
33 gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
Amelie Delaunay17f93402020-09-09 11:35:10 +020034 dwc2_writel(hsotg, gotgctl, GOTGCTL);
35
Amelie Delaunay17f93402020-09-09 11:35:10 +020036 spin_unlock_irqrestore(&hsotg->lock, flags);
Amelie Delaunayb2cab2a2021-10-05 11:53:03 +020037
38 dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST));
Amelie Delaunay17f93402020-09-09 11:35:10 +020039}
40
41static int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid)
42{
43 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
44
45 /* Check if A-Session is already in the right state */
46 if ((valid && (gotgctl & GOTGCTL_ASESVLD)) ||
47 (!valid && !(gotgctl & GOTGCTL_ASESVLD)))
48 return -EALREADY;
49
Fabrice Gasnier3ad02e02021-12-06 16:56:43 +010050 /* Always enable overrides to handle the resume case */
51 dwc2_ovr_gotgctl(gotgctl);
52
Amelie Delaunay1ad707f2021-10-05 11:53:05 +020053 gotgctl &= ~GOTGCTL_BVALOVAL;
Amelie Delaunay17f93402020-09-09 11:35:10 +020054 if (valid)
55 gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
56 else
57 gotgctl &= ~(GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
58 dwc2_writel(hsotg, gotgctl, GOTGCTL);
59
60 return 0;
61}
62
63static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid)
64{
65 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
66
67 /* Check if B-Session is already in the right state */
68 if ((valid && (gotgctl & GOTGCTL_BSESVLD)) ||
69 (!valid && !(gotgctl & GOTGCTL_BSESVLD)))
70 return -EALREADY;
71
Fabrice Gasnier3ad02e02021-12-06 16:56:43 +010072 /* Always enable overrides to handle the resume case */
73 dwc2_ovr_gotgctl(gotgctl);
74
Amelie Delaunay1ad707f2021-10-05 11:53:05 +020075 gotgctl &= ~GOTGCTL_AVALOVAL;
Amelie Delaunay17f93402020-09-09 11:35:10 +020076 if (valid)
77 gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
78 else
79 gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL);
80 dwc2_writel(hsotg, gotgctl, GOTGCTL);
81
82 return 0;
83}
84
85static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
86{
87 struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw);
88 unsigned long flags;
89 int already = 0;
90
91 /* Skip session not in line with dr_mode */
92 if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) ||
93 (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
94 return -EINVAL;
95
96#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
97 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
98 /* Skip session if core is in test mode */
99 if (role == USB_ROLE_NONE && hsotg->test_mode) {
100 dev_dbg(hsotg->dev, "Core is in test mode\n");
101 return -EBUSY;
102 }
103#endif
104
Amelie Delaunay8d387f62021-10-05 11:53:04 +0200105 /*
106 * In case of USB_DR_MODE_PERIPHERAL, clock is disabled at the end of
107 * the probe and enabled on udc_start.
108 * If role-switch set is called before the udc_start, we need to enable
109 * the clock to read/write GOTGCTL and GUSBCFG registers to override
110 * mode and sessions. It is the case if cable is plugged at boot.
111 */
112 if (!hsotg->ll_hw_enabled && hsotg->clk) {
113 int ret = clk_prepare_enable(hsotg->clk);
114
115 if (ret)
116 return ret;
117 }
118
Amelie Delaunay17f93402020-09-09 11:35:10 +0200119 spin_lock_irqsave(&hsotg->lock, flags);
120
Fabrice Gasniere14acb82021-12-06 16:56:42 +0100121 if (role == USB_ROLE_NONE) {
122 /* default operation mode when usb role is USB_ROLE_NONE */
123 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
124 role = USB_ROLE_HOST;
125 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
126 role = USB_ROLE_DEVICE;
127 }
128
Amelie Delaunay17f93402020-09-09 11:35:10 +0200129 if (role == USB_ROLE_HOST) {
130 already = dwc2_ovr_avalid(hsotg, true);
131 } else if (role == USB_ROLE_DEVICE) {
132 already = dwc2_ovr_bvalid(hsotg, true);
133 /* This clear DCTL.SFTDISCON bit */
134 dwc2_hsotg_core_connect(hsotg);
135 } else {
136 if (dwc2_is_device_mode(hsotg)) {
137 if (!dwc2_ovr_bvalid(hsotg, false))
138 /* This set DCTL.SFTDISCON bit */
139 dwc2_hsotg_core_disconnect(hsotg);
140 } else {
141 dwc2_ovr_avalid(hsotg, false);
142 }
143 }
144
145 spin_unlock_irqrestore(&hsotg->lock, flags);
146
147 if (!already && hsotg->dr_mode == USB_DR_MODE_OTG)
148 /* This will raise a Connector ID Status Change Interrupt */
149 dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
150
Amelie Delaunay8d387f62021-10-05 11:53:04 +0200151 if (!hsotg->ll_hw_enabled && hsotg->clk)
152 clk_disable_unprepare(hsotg->clk);
153
Amelie Delaunay17f93402020-09-09 11:35:10 +0200154 dev_dbg(hsotg->dev, "%s-session valid\n",
155 role == USB_ROLE_NONE ? "No" :
156 role == USB_ROLE_HOST ? "A" : "B");
157
158 return 0;
159}
160
161int dwc2_drd_init(struct dwc2_hsotg *hsotg)
162{
163 struct usb_role_switch_desc role_sw_desc = {0};
164 struct usb_role_switch *role_sw;
165 int ret;
166
167 if (!device_property_read_bool(hsotg->dev, "usb-role-switch"))
168 return 0;
169
Fabrice Gasniere14acb82021-12-06 16:56:42 +0100170 hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev);
Amelie Delaunay17f93402020-09-09 11:35:10 +0200171 role_sw_desc.driver_data = hsotg;
172 role_sw_desc.fwnode = dev_fwnode(hsotg->dev);
173 role_sw_desc.set = dwc2_drd_role_sw_set;
174 role_sw_desc.allow_userspace_control = true;
175
176 role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc);
177 if (IS_ERR(role_sw)) {
178 ret = PTR_ERR(role_sw);
179 dev_err(hsotg->dev,
180 "failed to register role switch: %d\n", ret);
181 return ret;
182 }
183
184 hsotg->role_sw = role_sw;
185
186 /* Enable override and initialize values */
187 dwc2_ovr_init(hsotg);
188
189 return 0;
190}
191
192void dwc2_drd_suspend(struct dwc2_hsotg *hsotg)
193{
194 u32 gintsts, gintmsk;
195
196 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
197 gintmsk = dwc2_readl(hsotg, GINTMSK);
198 gintmsk &= ~GINTSTS_CONIDSTSCHNG;
199 dwc2_writel(hsotg, gintmsk, GINTMSK);
200 gintsts = dwc2_readl(hsotg, GINTSTS);
201 dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
202 }
203}
204
205void dwc2_drd_resume(struct dwc2_hsotg *hsotg)
206{
207 u32 gintsts, gintmsk;
Fabrice Gasnier3ad02e02021-12-06 16:56:43 +0100208 enum usb_role role;
209
210 if (hsotg->role_sw) {
211 /* get last known role (as the get ops isn't implemented by this driver) */
212 role = usb_role_switch_get_role(hsotg->role_sw);
213
214 if (role == USB_ROLE_NONE) {
215 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
216 role = USB_ROLE_HOST;
217 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
218 role = USB_ROLE_DEVICE;
219 }
220
221 /* restore last role that may have been lost */
222 if (role == USB_ROLE_HOST)
223 dwc2_ovr_avalid(hsotg, true);
224 else if (role == USB_ROLE_DEVICE)
225 dwc2_ovr_bvalid(hsotg, true);
226
227 dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
228
229 dev_dbg(hsotg->dev, "resuming %s-session valid\n",
230 role == USB_ROLE_NONE ? "No" :
231 role == USB_ROLE_HOST ? "A" : "B");
232 }
Amelie Delaunay17f93402020-09-09 11:35:10 +0200233
234 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
235 gintsts = dwc2_readl(hsotg, GINTSTS);
236 dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
237 gintmsk = dwc2_readl(hsotg, GINTMSK);
238 gintmsk |= GINTSTS_CONIDSTSCHNG;
239 dwc2_writel(hsotg, gintmsk, GINTMSK);
240 }
241}
242
243void dwc2_drd_exit(struct dwc2_hsotg *hsotg)
244{
245 if (hsotg->role_sw)
246 usb_role_switch_unregister(hsotg->role_sw);
247}