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