xref: /openbmc/linux/drivers/usb/musb/musb_virthub.c (revision 0a73d21e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MUSB OTG driver virtual root hub support
4  *
5  * Copyright 2005 Mentor Graphics Corporation
6  * Copyright (C) 2005-2006 by Texas Instruments
7  * Copyright (C) 2006-2007 Nokia Corporation
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/time.h>
15 #include <linux/timer.h>
16 
17 #include <asm/unaligned.h>
18 
19 #include "musb_core.h"
20 
21 void musb_host_finish_resume(struct work_struct *work)
22 {
23 	struct musb *musb;
24 	unsigned long flags;
25 	u8 power;
26 
27 	musb = container_of(work, struct musb, finish_resume_work.work);
28 
29 	spin_lock_irqsave(&musb->lock, flags);
30 
31 	power = musb_readb(musb->mregs, MUSB_POWER);
32 	power &= ~MUSB_POWER_RESUME;
33 	musb_dbg(musb, "root port resume stopped, power %02x", power);
34 	musb_writeb(musb->mregs, MUSB_POWER, power);
35 
36 	/*
37 	 * ISSUE:  DaVinci (RTL 1.300) disconnects after
38 	 * resume of high speed peripherals (but not full
39 	 * speed ones).
40 	 */
41 	musb->is_active = 1;
42 	musb->port1_status &= ~(USB_PORT_STAT_SUSPEND | MUSB_PORT_STAT_RESUME);
43 	musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
44 	usb_hcd_poll_rh_status(musb->hcd);
45 	/* NOTE: it might really be A_WAIT_BCON ... */
46 	musb->xceiv->otg->state = OTG_STATE_A_HOST;
47 
48 	spin_unlock_irqrestore(&musb->lock, flags);
49 }
50 
51 void musb_port_suspend(struct musb *musb, bool do_suspend)
52 {
53 	struct usb_otg	*otg = musb->xceiv->otg;
54 	u8		power;
55 	void __iomem	*mbase = musb->mregs;
56 
57 	if (!is_host_active(musb))
58 		return;
59 
60 	/* NOTE:  this doesn't necessarily put PHY into low power mode,
61 	 * turning off its clock; that's a function of PHY integration and
62 	 * MUSB_POWER_ENSUSPEND.  PHY may need a clock (sigh) to detect
63 	 * SE0 changing to connect (J) or wakeup (K) states.
64 	 */
65 	power = musb_readb(mbase, MUSB_POWER);
66 	if (do_suspend) {
67 		int retries = 10000;
68 
69 		power &= ~MUSB_POWER_RESUME;
70 		power |= MUSB_POWER_SUSPENDM;
71 		musb_writeb(mbase, MUSB_POWER, power);
72 
73 		/* Needed for OPT A tests */
74 		power = musb_readb(mbase, MUSB_POWER);
75 		while (power & MUSB_POWER_SUSPENDM) {
76 			power = musb_readb(mbase, MUSB_POWER);
77 			if (retries-- < 1)
78 				break;
79 		}
80 
81 		musb_dbg(musb, "Root port suspended, power %02x", power);
82 
83 		musb->port1_status |= USB_PORT_STAT_SUSPEND;
84 		switch (musb->xceiv->otg->state) {
85 		case OTG_STATE_A_HOST:
86 			musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
87 			musb->is_active = otg->host->b_hnp_enable;
88 			if (musb->is_active)
89 				mod_timer(&musb->otg_timer, jiffies
90 					+ msecs_to_jiffies(
91 						OTG_TIME_A_AIDL_BDIS));
92 			musb_platform_try_idle(musb, 0);
93 			break;
94 		case OTG_STATE_B_HOST:
95 			musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
96 			musb->is_active = otg->host->b_hnp_enable;
97 			musb_platform_try_idle(musb, 0);
98 			break;
99 		default:
100 			musb_dbg(musb, "bogus rh suspend? %s",
101 				usb_otg_state_string(musb->xceiv->otg->state));
102 		}
103 	} else if (power & MUSB_POWER_SUSPENDM) {
104 		power &= ~MUSB_POWER_SUSPENDM;
105 		power |= MUSB_POWER_RESUME;
106 		musb_writeb(mbase, MUSB_POWER, power);
107 
108 		musb_dbg(musb, "Root port resuming, power %02x", power);
109 
110 		musb->port1_status |= MUSB_PORT_STAT_RESUME;
111 		schedule_delayed_work(&musb->finish_resume_work,
112 				      msecs_to_jiffies(USB_RESUME_TIMEOUT));
113 	}
114 }
115 
116 void musb_port_reset(struct musb *musb, bool do_reset)
117 {
118 	u8		power;
119 	void __iomem	*mbase = musb->mregs;
120 
121 	if (musb->xceiv->otg->state == OTG_STATE_B_IDLE) {
122 		musb_dbg(musb, "HNP: Returning from HNP; no hub reset from b_idle");
123 		musb->port1_status &= ~USB_PORT_STAT_RESET;
124 		return;
125 	}
126 
127 	if (!is_host_active(musb))
128 		return;
129 
130 	/* NOTE:  caller guarantees it will turn off the reset when
131 	 * the appropriate amount of time has passed
132 	 */
133 	power = musb_readb(mbase, MUSB_POWER);
134 	if (do_reset) {
135 		/*
136 		 * If RESUME is set, we must make sure it stays minimum 20 ms.
137 		 * Then we must clear RESUME and wait a bit to let musb start
138 		 * generating SOFs. If we don't do this, OPT HS A 6.8 tests
139 		 * fail with "Error! Did not receive an SOF before suspend
140 		 * detected".
141 		 */
142 		if (power &  MUSB_POWER_RESUME) {
143 			long remain = (unsigned long) musb->rh_timer - jiffies;
144 
145 			if (musb->rh_timer > 0 && remain > 0) {
146 				/* take into account the minimum delay after resume */
147 				schedule_delayed_work(
148 					&musb->deassert_reset_work, remain);
149 				return;
150 			}
151 
152 			musb_writeb(mbase, MUSB_POWER,
153 				    power & ~MUSB_POWER_RESUME);
154 
155 			/* Give the core 1 ms to clear MUSB_POWER_RESUME */
156 			schedule_delayed_work(&musb->deassert_reset_work,
157 					      msecs_to_jiffies(1));
158 			return;
159 		}
160 
161 		power &= 0xf0;
162 		musb_writeb(mbase, MUSB_POWER,
163 				power | MUSB_POWER_RESET);
164 
165 		musb->port1_status |= USB_PORT_STAT_RESET;
166 		musb->port1_status &= ~USB_PORT_STAT_ENABLE;
167 		schedule_delayed_work(&musb->deassert_reset_work,
168 				      msecs_to_jiffies(50));
169 	} else {
170 		musb_dbg(musb, "root port reset stopped");
171 		musb_platform_pre_root_reset_end(musb);
172 		musb_writeb(mbase, MUSB_POWER,
173 				power & ~MUSB_POWER_RESET);
174 		musb_platform_post_root_reset_end(musb);
175 
176 		power = musb_readb(mbase, MUSB_POWER);
177 		if (power & MUSB_POWER_HSMODE) {
178 			musb_dbg(musb, "high-speed device connected");
179 			musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
180 		}
181 
182 		musb->port1_status &= ~USB_PORT_STAT_RESET;
183 		musb->port1_status |= USB_PORT_STAT_ENABLE
184 					| (USB_PORT_STAT_C_RESET << 16)
185 					| (USB_PORT_STAT_C_ENABLE << 16);
186 		usb_hcd_poll_rh_status(musb->hcd);
187 
188 		musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
189 	}
190 }
191 
192 void musb_root_disconnect(struct musb *musb)
193 {
194 	struct usb_otg	*otg = musb->xceiv->otg;
195 
196 	musb->port1_status = USB_PORT_STAT_POWER
197 			| (USB_PORT_STAT_C_CONNECTION << 16);
198 
199 	usb_hcd_poll_rh_status(musb->hcd);
200 	musb->is_active = 0;
201 
202 	switch (musb->xceiv->otg->state) {
203 	case OTG_STATE_A_SUSPEND:
204 		if (otg->host->b_hnp_enable) {
205 			musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
206 			musb->g.is_a_peripheral = 1;
207 			break;
208 		}
209 		/* FALLTHROUGH */
210 	case OTG_STATE_A_HOST:
211 		musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
212 		musb->is_active = 0;
213 		break;
214 	case OTG_STATE_A_WAIT_VFALL:
215 		musb->xceiv->otg->state = OTG_STATE_B_IDLE;
216 		break;
217 	default:
218 		musb_dbg(musb, "host disconnect (%s)",
219 			usb_otg_state_string(musb->xceiv->otg->state));
220 	}
221 }
222 EXPORT_SYMBOL_GPL(musb_root_disconnect);
223 
224 
225 /*---------------------------------------------------------------------*/
226 
227 /* Caller may or may not hold musb->lock */
228 int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
229 {
230 	struct musb	*musb = hcd_to_musb(hcd);
231 	int		retval = 0;
232 
233 	/* called in_irq() via usb_hcd_poll_rh_status() */
234 	if (musb->port1_status & 0xffff0000) {
235 		*buf = 0x02;
236 		retval = 1;
237 	}
238 	return retval;
239 }
240 
241 static int musb_has_gadget(struct musb *musb)
242 {
243 	/*
244 	 * In host-only mode we start a connection right away. In OTG mode
245 	 * we have to wait until we loaded a gadget. We don't really need a
246 	 * gadget if we operate as a host but we should not start a session
247 	 * as a device without a gadget or else we explode.
248 	 */
249 #ifdef CONFIG_USB_MUSB_HOST
250 	return 1;
251 #else
252 	return musb->port_mode == MUSB_PORT_MODE_HOST;
253 #endif
254 }
255 
256 int musb_hub_control(
257 	struct usb_hcd	*hcd,
258 	u16		typeReq,
259 	u16		wValue,
260 	u16		wIndex,
261 	char		*buf,
262 	u16		wLength)
263 {
264 	struct musb	*musb = hcd_to_musb(hcd);
265 	u32		temp;
266 	int		retval = 0;
267 	unsigned long	flags;
268 	bool		start_musb = false;
269 
270 	spin_lock_irqsave(&musb->lock, flags);
271 
272 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
273 		spin_unlock_irqrestore(&musb->lock, flags);
274 		return -ESHUTDOWN;
275 	}
276 
277 	/* hub features:  always zero, setting is a NOP
278 	 * port features: reported, sometimes updated when host is active
279 	 * no indicators
280 	 */
281 	switch (typeReq) {
282 	case ClearHubFeature:
283 	case SetHubFeature:
284 		switch (wValue) {
285 		case C_HUB_OVER_CURRENT:
286 		case C_HUB_LOCAL_POWER:
287 			break;
288 		default:
289 			goto error;
290 		}
291 		break;
292 	case ClearPortFeature:
293 		if ((wIndex & 0xff) != 1)
294 			goto error;
295 
296 		switch (wValue) {
297 		case USB_PORT_FEAT_ENABLE:
298 			break;
299 		case USB_PORT_FEAT_SUSPEND:
300 			musb_port_suspend(musb, false);
301 			break;
302 		case USB_PORT_FEAT_POWER:
303 			if (!hcd->self.is_b_host)
304 				musb_platform_set_vbus(musb, 0);
305 			break;
306 		case USB_PORT_FEAT_C_CONNECTION:
307 		case USB_PORT_FEAT_C_ENABLE:
308 		case USB_PORT_FEAT_C_OVER_CURRENT:
309 		case USB_PORT_FEAT_C_RESET:
310 		case USB_PORT_FEAT_C_SUSPEND:
311 			break;
312 		default:
313 			goto error;
314 		}
315 		musb_dbg(musb, "clear feature %d", wValue);
316 		musb->port1_status &= ~(1 << wValue);
317 		break;
318 	case GetHubDescriptor:
319 		{
320 		struct usb_hub_descriptor *desc = (void *)buf;
321 
322 		desc->bDescLength = 9;
323 		desc->bDescriptorType = USB_DT_HUB;
324 		desc->bNbrPorts = 1;
325 		desc->wHubCharacteristics = cpu_to_le16(
326 			HUB_CHAR_INDV_PORT_LPSM /* per-port power switching */
327 			| HUB_CHAR_NO_OCPM	/* no overcurrent reporting */
328 			);
329 		desc->bPwrOn2PwrGood = 5;	/* msec/2 */
330 		desc->bHubContrCurrent = 0;
331 
332 		/* workaround bogus struct definition */
333 		desc->u.hs.DeviceRemovable[0] = 0x02;	/* port 1 */
334 		desc->u.hs.DeviceRemovable[1] = 0xff;
335 		}
336 		break;
337 	case GetHubStatus:
338 		temp = 0;
339 		*(__le32 *) buf = cpu_to_le32(temp);
340 		break;
341 	case GetPortStatus:
342 		if (wIndex != 1)
343 			goto error;
344 
345 		put_unaligned(cpu_to_le32(musb->port1_status
346 					& ~MUSB_PORT_STAT_RESUME),
347 				(__le32 *) buf);
348 
349 		/* port change status is more interesting */
350 		musb_dbg(musb, "port status %08x", musb->port1_status);
351 		break;
352 	case SetPortFeature:
353 		if ((wIndex & 0xff) != 1)
354 			goto error;
355 
356 		switch (wValue) {
357 		case USB_PORT_FEAT_POWER:
358 			/* NOTE: this controller has a strange state machine
359 			 * that involves "requesting sessions" according to
360 			 * magic side effects from incompletely-described
361 			 * rules about startup...
362 			 *
363 			 * This call is what really starts the host mode; be
364 			 * very careful about side effects if you reorder any
365 			 * initialization logic, e.g. for OTG, or change any
366 			 * logic relating to VBUS power-up.
367 			 */
368 			if (!hcd->self.is_b_host && musb_has_gadget(musb))
369 				start_musb = true;
370 			break;
371 		case USB_PORT_FEAT_RESET:
372 			musb_port_reset(musb, true);
373 			break;
374 		case USB_PORT_FEAT_SUSPEND:
375 			musb_port_suspend(musb, true);
376 			break;
377 		case USB_PORT_FEAT_TEST:
378 			if (unlikely(is_host_active(musb)))
379 				goto error;
380 
381 			wIndex >>= 8;
382 			switch (wIndex) {
383 			case 1:
384 				pr_debug("TEST_J\n");
385 				temp = MUSB_TEST_J;
386 				break;
387 			case 2:
388 				pr_debug("TEST_K\n");
389 				temp = MUSB_TEST_K;
390 				break;
391 			case 3:
392 				pr_debug("TEST_SE0_NAK\n");
393 				temp = MUSB_TEST_SE0_NAK;
394 				break;
395 			case 4:
396 				pr_debug("TEST_PACKET\n");
397 				temp = MUSB_TEST_PACKET;
398 				musb_load_testpacket(musb);
399 				break;
400 			case 5:
401 				pr_debug("TEST_FORCE_ENABLE\n");
402 				temp = MUSB_TEST_FORCE_HOST
403 					| MUSB_TEST_FORCE_HS;
404 
405 				musb_writeb(musb->mregs, MUSB_DEVCTL,
406 						MUSB_DEVCTL_SESSION);
407 				break;
408 			case 6:
409 				pr_debug("TEST_FIFO_ACCESS\n");
410 				temp = MUSB_TEST_FIFO_ACCESS;
411 				break;
412 			default:
413 				goto error;
414 			}
415 			musb_writeb(musb->mregs, MUSB_TESTMODE, temp);
416 			break;
417 		default:
418 			goto error;
419 		}
420 		musb_dbg(musb, "set feature %d", wValue);
421 		musb->port1_status |= 1 << wValue;
422 		break;
423 
424 	default:
425 error:
426 		/* "protocol stall" on error */
427 		retval = -EPIPE;
428 	}
429 	spin_unlock_irqrestore(&musb->lock, flags);
430 
431 	if (start_musb)
432 		musb_start(musb);
433 
434 	return retval;
435 }
436