ohci-hub.c 14.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
 * OHCI HCD (Host Controller Driver) for USB.
 * 
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5
 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 
 * This file is licenced under GPL
 */

/*-------------------------------------------------------------------------*/

/*
 * OHCI Root Hub ... the nonsharable stuff
 *
 * Registers don't need cpu_to_le32, that happens transparently
 */

/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
 * The erratum (#4) description is incorrect.  AMD's workaround waits
 * till some bits (mostly reserved) are clear; ok for all revs.
 */
#define read_roothub(hc, register, mask) ({ \
23
	u32 temp = ohci_readl (&hc->regs->roothub.register); \
24 25 26
	if (temp == -1) \
		disable (hc); \
	else if (hc->flags & OHCI_QUIRK_AMD756) \
Linus Torvalds's avatar
Linus Torvalds committed
27
		while (temp & mask) \
28
			temp = ohci_readl (&hc->regs->roothub.register); \
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33
	temp; })

static u32 roothub_a (struct ohci_hcd *hc)
	{ return read_roothub (hc, a, 0xfc0fe000); }
static inline u32 roothub_b (struct ohci_hcd *hc)
34
	{ return ohci_readl (&hc->regs->roothub.b); }
Linus Torvalds's avatar
Linus Torvalds committed
35
static inline u32 roothub_status (struct ohci_hcd *hc)
36
	{ return ohci_readl (&hc->regs->roothub.status); }
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42
static u32 roothub_portstatus (struct ohci_hcd *hc, int i)
	{ return read_roothub (hc, portstatus [i], 0xffe0fce0); }

/*-------------------------------------------------------------------------*/

#define dbg_port(hc,label,num,value) \
43
	ohci_dbg (hc, \
44 45 46
		"%s roothub.portstatus [%d] " \
		"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
		label, num, temp, \
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		(temp & RH_PS_PRSC) ? " PRSC" : "", \
		(temp & RH_PS_OCIC) ? " OCIC" : "", \
		(temp & RH_PS_PSSC) ? " PSSC" : "", \
		(temp & RH_PS_PESC) ? " PESC" : "", \
		(temp & RH_PS_CSC) ? " CSC" : "", \
 		\
		(temp & RH_PS_LSDA) ? " LSDA" : "", \
		(temp & RH_PS_PPS) ? " PPS" : "", \
		(temp & RH_PS_PRS) ? " PRS" : "", \
		(temp & RH_PS_POCI) ? " POCI" : "", \
		(temp & RH_PS_PSS) ? " PSS" : "", \
 		\
		(temp & RH_PS_PES) ? " PES" : "", \
		(temp & RH_PS_CCS) ? " CCS" : "" \
		);

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*-------------------------------------------------------------------------*/

#if	defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)

#define OHCI_SCHED_ENABLES \
	(OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)

static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);

static int ohci_hub_suspend (struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	struct usb_device	*root = hcd_to_bus (&ohci->hcd)->root_hub;
	int			status = 0;

	if (root->dev.power.power_state != 0)
		return 0;
	if (time_before (jiffies, ohci->next_statechange))
		return -EAGAIN;

	spin_lock_irq (&ohci->lock);

86
	ohci->hc_control = ohci_readl (&ohci->regs->control);
87 88 89 90 91 92
	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
	case OHCI_USB_RESUME:
		ohci_dbg (ohci, "resume/suspend?\n");
		ohci->hc_control &= ~OHCI_CTRL_HCFS;
		ohci->hc_control |= OHCI_USB_RESET;
		writel (ohci->hc_control, &ohci->regs->control);
93
		(void) ohci_readl (&ohci->regs->control);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		/* FALL THROUGH */
	case OHCI_USB_RESET:
		status = -EBUSY;
		ohci_dbg (ohci, "needs reinit!\n");
		goto done;
	case OHCI_USB_SUSPEND:
		ohci_dbg (ohci, "already suspended?\n");
		goto succeed;
	}
	ohci_dbg (ohci, "suspend root hub\n");

	/* First stop any processing */
	ohci->hcd.state = USB_STATE_QUIESCING;
	if (ohci->hc_control & OHCI_SCHED_ENABLES) {
		int		limit;

		ohci->hc_control &= ~OHCI_SCHED_ENABLES;
		writel (ohci->hc_control, &ohci->regs->control);
112
		ohci->hc_control = ohci_readl (&ohci->regs->control);
113 114 115 116 117 118 119 120 121 122
		writel (OHCI_INTR_SF, &ohci->regs->intrstatus);

		/* sched disables take effect on the next frame,
		 * then the last WDH could take 6+ msec
		 */
		ohci_dbg (ohci, "stopping schedules ...\n");
		limit = 2000;
		while (limit > 0) {
			udelay (250);
			limit =- 250;
123
			if (ohci_readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
124 125 126 127 128 129 130
				break;
		}
		dl_done_list (ohci, 0);
		mdelay (7);
	}
	dl_done_list (ohci, 0);
	finish_unlinks (ohci, OHCI_FRAME_NO(ohci->hcca), 0);
131
	writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
132 133 134 135 136 137 138 139 140 141 142

	/* maybe resume can wake root hub */
	if (ohci->hcd.remote_wakeup)
		ohci->hc_control |= OHCI_CTRL_RWE;
	else
		ohci->hc_control &= ~OHCI_CTRL_RWE;

	/* Suspend hub */
	ohci->hc_control &= ~OHCI_CTRL_HCFS;
	ohci->hc_control |= OHCI_USB_SUSPEND;
	writel (ohci->hc_control, &ohci->regs->control);
143
	(void) ohci_readl (&ohci->regs->control);
144 145

	/* no resumes until devices finish suspending */
146
	ohci->next_statechange = jiffies + msecs_to_jiffies (5);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

succeed:
	/* it's not USB_STATE_SUSPENDED unless access to this
	 * hub from the non-usb side (PCI, SOC, etc) stopped 
	 */
	root->dev.power.power_state = 3;
done:
	spin_unlock_irq (&ohci->lock);
	return status;
}

static inline struct ed *find_head (struct ed *ed)
{
	/* for bulk and control lists */
	while (ed->ed_prev)
		ed = ed->ed_prev;
	return ed;
}

static int hc_restart (struct ohci_hcd *ohci);

/* caller owns root->serialize */
static int ohci_hub_resume (struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	struct usb_device	*root = hcd_to_bus (&ohci->hcd)->root_hub;
	u32			temp, enables;
	int			status = -EINPROGRESS;

	if (!root->dev.power.power_state)
		return 0;
	if (time_before (jiffies, ohci->next_statechange))
		return -EAGAIN;

	spin_lock_irq (&ohci->lock);
182
	ohci->hc_control = ohci_readl (&ohci->regs->control);
183 184 185 186 187
	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
	case OHCI_USB_SUSPEND:
		ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
		ohci->hc_control |= OHCI_USB_RESUME;
		writel (ohci->hc_control, &ohci->regs->control);
188
		(void) ohci_readl (&ohci->regs->control);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		ohci_dbg (ohci, "resume root hub\n");
		break;
	case OHCI_USB_RESUME:
		/* HCFS changes sometime after INTR_RD */
		ohci_info (ohci, "remote wakeup\n");
		break;
	case OHCI_USB_OPER:
		ohci_dbg (ohci, "odd resume\n");
		root->dev.power.power_state = 0;
		status = 0;
		break;
	default:		/* RESET, we lost power */
		ohci_dbg (ohci, "root hub hardware reset\n");
		status = -EBUSY;
	}
	spin_unlock_irq (&ohci->lock);
	if (status == -EBUSY)
		return hc_restart (ohci);
	if (status != -EINPROGRESS)
		return status;

	temp = roothub_a (ohci) & RH_A_NDP;
	enables = 0;
	while (temp--) {
213
		u32 stat = ohci_readl (&ohci->regs->roothub.portstatus [temp]);
214 215 216 217 218 219 220 221 222 223 224

		/* force global, not selective, resume */
		if (!(stat & RH_PS_PSS))
			continue;
		writel (RH_PS_POCI, &ohci->regs->roothub.portstatus [temp]);
	}

	/* Some controllers (lucent) need extra-long delays */
	ohci->hcd.state = USB_STATE_RESUMING;
	mdelay (20 /* usb 11.5.1.10 */ + 15);

225
	temp = ohci_readl (&ohci->regs->control);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	temp &= OHCI_CTRL_HCFS;
	if (temp != OHCI_USB_RESUME) {
		ohci_err (ohci, "controller won't resume\n");
		return -EBUSY;
	}

	/* disable old schedule state, reinit from scratch */
	writel (0, &ohci->regs->ed_controlhead);
	writel (0, &ohci->regs->ed_controlcurrent);
	writel (0, &ohci->regs->ed_bulkhead);
	writel (0, &ohci->regs->ed_bulkcurrent);
	writel (0, &ohci->regs->ed_periodcurrent);
	writel ((u32) ohci->hcca_dma, &ohci->regs->hcca);

	periodic_reinit (ohci);

	/* interrupts might have been disabled */
	writel (OHCI_INTR_INIT, &ohci->regs->intrenable);
	if (ohci->ed_rm_list)
		writel (OHCI_INTR_SF, &ohci->regs->intrenable);
246
	writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
247 248 249

	/* Then re-enable operations */
	writel (OHCI_USB_OPER, &ohci->regs->control);
250
	(void) ohci_readl (&ohci->regs->control);
251
	msleep (3);
252 253 254 255 256 257

	temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
	if (ohci->hcd.can_wakeup)
		temp |= OHCI_CTRL_RWC;
	ohci->hc_control = temp;
	writel (temp, &ohci->regs->control);
258
	(void) ohci_readl (&ohci->regs->control);
259 260

	/* TRSMRCY */
261
	msleep (10);
262 263 264
	root->dev.power.power_state = 0;

	/* keep it alive for ~5x suspend + resume costs */
265
	ohci->next_statechange = jiffies + msecs_to_jiffies (250);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	/* maybe turn schedules back on */
	enables = 0;
	temp = 0;
	if (!ohci->ed_rm_list) {
		if (ohci->ed_controltail) {
			writel (find_head (ohci->ed_controltail)->dma,
				&ohci->regs->ed_controlhead);
			enables |= OHCI_CTRL_CLE;
			temp |= OHCI_CLF;
		}
		if (ohci->ed_bulktail) {
			writel (find_head (ohci->ed_bulktail)->dma,
				&ohci->regs->ed_bulkhead);
			enables |= OHCI_CTRL_BLE;
			temp |= OHCI_BLF;
		}
	}
	if (hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs
			|| hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs)
		enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
	if (enables) {
		ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
		ohci->hc_control |= enables;
		writel (ohci->hc_control, &ohci->regs->control);
		if (temp)
			writel (status, &ohci->regs->cmdstatus);
293
		(void) ohci_readl (&ohci->regs->control);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	}

	ohci->hcd.state = USB_STATE_RUNNING;
	return 0;
}

static void ohci_rh_resume (void *_hcd)
{
	struct usb_hcd	*hcd = _hcd;

	down (&hcd->self.root_hub->serialize);
	(void) ohci_hub_resume (hcd);
	up (&hcd->self.root_hub->serialize);
}

#else

static void ohci_rh_resume (void *_hcd)
{
313
	struct ohci_hcd	*ohci = hcd_to_ohci (_hcd);
314 315 316 317
	ohci_dbg(ohci, "rh_resume ??\n");
}

#endif	/* CONFIG_USB_SUSPEND || CONFIG_PM */
Linus Torvalds's avatar
Linus Torvalds committed
318 319 320 321 322 323 324 325 326 327

/*-------------------------------------------------------------------------*/

/* build "status change" packet (one or two bytes) from HC registers */

static int
ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
{
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
	int		ports, i, changed = 0, length = 1;
328
	int		can_suspend = 1;
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331

	ports = roothub_a (ohci) & RH_A_NDP; 
	if (ports > MAX_ROOT_PORTS) {
332
		if (!HCD_IS_RUNNING(ohci->hcd.state))
333
			return -ESHUTDOWN;
334
		ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n",
335
			ports, ohci_readl (&ohci->regs->roothub.a) & RH_A_NDP);
Linus Torvalds's avatar
Linus Torvalds committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		/* retry later; "should not happen" */
		return 0;
	}

	/* init status */
	if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
		buf [0] = changed = 1;
	else
		buf [0] = 0;
	if (ports > 7) {
		buf [1] = 0;
		length++;
	}

	/* look at each port */
	for (i = 0; i < ports; i++) {
		u32	status = roothub_portstatus (ohci, i);

354 355
		if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
				| RH_PS_OCIC | RH_PS_PRSC)) {
Linus Torvalds's avatar
Linus Torvalds committed
356
			changed = 1;
357 358 359 360
			if (i < 7)
			    buf [0] |= 1 << (i + 1);
			else
			    buf [1] |= 1 << (i - 7);
361
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
362
		}
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

		/* can suspend if no ports are enabled; or if all all
		 * enabled ports are suspended AND remote wakeup is on.
		 */
		if (!(status & RH_PS_CCS))
			continue;
		if ((status & RH_PS_PSS) && ohci->hcd.remote_wakeup)
			continue;
		can_suspend = 0;
	}

#ifdef CONFIG_PM
	/* save power by suspending idle root hubs;
	 * INTR_RD wakes us when there's work
	 */
	if (can_suspend
			&& !changed
			&& !ohci->ed_rm_list
			&& ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
					& ohci->hc_control)
				== OHCI_USB_OPER
			&& down_trylock (&hcd->self.root_hub->serialize) == 0
			) {
		ohci_vdbg (ohci, "autosuspend\n");
		(void) ohci_hub_suspend (&ohci->hcd);
David Brownell's avatar
David Brownell committed
388
		ohci->hcd.state = USB_STATE_RUNNING;
389
		up (&hcd->self.root_hub->serialize);
Linus Torvalds's avatar
Linus Torvalds committed
390
	}
391 392
#endif

Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	return changed ? length : 0;
}

/*-------------------------------------------------------------------------*/

static void
ohci_hub_descriptor (
	struct ohci_hcd			*ohci,
	struct usb_hub_descriptor	*desc
) {
	u32		rh = roothub_a (ohci);
	int		ports = rh & RH_A_NDP; 
	u16		temp;

	desc->bDescriptorType = 0x29;
	desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
	desc->bHubContrCurrent = 0;

	desc->bNbrPorts = ports;
	temp = 1 + (ports / 8);
	desc->bDescLength = 7 + 2 * temp;

	temp = 0;
416 417
	if (rh & RH_A_NPS)		/* no power switching? */
	    temp |= 0x0002;
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	if (rh & RH_A_PSM) 		/* per-port power switching? */
	    temp |= 0x0001;
	if (rh & RH_A_NOCP)		/* no overcurrent reporting? */
	    temp |= 0x0010;
	else if (rh & RH_A_OCPM)	/* per-port overcurrent reporting? */
	    temp |= 0x0008;
	desc->wHubCharacteristics = cpu_to_le16 (temp);

	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
	rh = roothub_b (ohci);
	desc->bitmap [0] = rh & RH_B_DR;
	if (ports > 7) {
		desc->bitmap [1] = (rh & RH_B_DR) >> 8;
		desc->bitmap [2] = desc->bitmap [3] = 0xff;
	} else
		desc->bitmap [1] = 0xff;
}

/*-------------------------------------------------------------------------*/

static int ohci_hub_control (
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
) {
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
447
	int		ports = hcd_to_bus (hcd)->root_hub->maxchild;
Linus Torvalds's avatar
Linus Torvalds committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	u32		temp;
	int		retval = 0;

	switch (typeReq) {
	case ClearHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
			writel (RH_HS_OCIC, &ohci->regs->roothub.status);
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			temp = RH_PS_CCS;
			break;
		case USB_PORT_FEAT_C_ENABLE:
			temp = RH_PS_PESC;
			break;
		case USB_PORT_FEAT_SUSPEND:
			temp = RH_PS_POCI;
476 477 478
			if ((ohci->hc_control & OHCI_CTRL_HCFS)
					!= OHCI_USB_OPER)
				schedule_work (&ohci->rh_resume);
Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
			break;
		case USB_PORT_FEAT_C_SUSPEND:
			temp = RH_PS_PSSC;
			break;
		case USB_PORT_FEAT_POWER:
			temp = RH_PS_LSDA;
			break;
		case USB_PORT_FEAT_C_CONNECTION:
			temp = RH_PS_CSC;
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			temp = RH_PS_OCIC;
			break;
		case USB_PORT_FEAT_C_RESET:
			temp = RH_PS_PRSC;
			break;
		default:
			goto error;
		}
		writel (temp, &ohci->regs->roothub.portstatus [wIndex]);
499
		// ohci_readl (&ohci->regs->roothub.portstatus [wIndex]);
Linus Torvalds's avatar
Linus Torvalds committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		break;
	case GetHubDescriptor:
		ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
		break;
	case GetHubStatus:
		temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
		*(u32 *) buf = cpu_to_le32 (temp);
		break;
	case GetPortStatus:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		temp = roothub_portstatus (ohci, wIndex);
		*(u32 *) buf = cpu_to_le32 (temp);

#ifndef	OHCI_VERBOSE_DEBUG
	if (*(u16*)(buf+2))	/* only if wPortChange is interesting */
#endif
		dbg_port (ohci, "GetStatus", wIndex + 1, temp);
		break;
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
			// FIXME:  this can be cleared, yes?
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case SetPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
			writel (RH_PS_PSS,
				&ohci->regs->roothub.portstatus [wIndex]);
			break;
		case USB_PORT_FEAT_POWER:
			writel (RH_PS_PPS,
				&ohci->regs->roothub.portstatus [wIndex]);
			break;
		case USB_PORT_FEAT_RESET:
544
			temp = ohci_readl (&ohci->regs->roothub.portstatus [wIndex]);
Linus Torvalds's avatar
Linus Torvalds committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
			if (temp & RH_PS_CCS)
				writel (RH_PS_PRS,
				    &ohci->regs->roothub.portstatus [wIndex]);
			break;
		default:
			goto error;
		}
		break;

	default:
error:
		/* "protocol stall" on error */
		retval = -EPIPE;
	}
	return retval;
}