USB: EHCI: use hrtimer for unlinking empty async QHs
[deliverable/linux.git] / drivers / usb / host / ehci-hcd.c
CommitLineData
1da177e4 1/*
578333ab
AS
2 * Enhanced Host Controller Interface (EHCI) driver for USB.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
1da177e4 6 * Copyright (c) 2000-2004 by David Brownell
53bd6a60 7 *
1da177e4
LT
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
1da177e4
LT
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <linux/dmapool.h>
26#include <linux/kernel.h>
27#include <linux/delay.h>
28#include <linux/ioport.h>
29#include <linux/sched.h>
3c04e20e 30#include <linux/vmalloc.h>
1da177e4
LT
31#include <linux/errno.h>
32#include <linux/init.h>
d58b4bcc 33#include <linux/hrtimer.h>
1da177e4
LT
34#include <linux/list.h>
35#include <linux/interrupt.h>
1da177e4 36#include <linux/usb.h>
27729aad 37#include <linux/usb/hcd.h>
1da177e4
LT
38#include <linux/moduleparam.h>
39#include <linux/dma-mapping.h>
694cc208 40#include <linux/debugfs.h>
5a0e3ad6 41#include <linux/slab.h>
aa4d8342 42#include <linux/uaccess.h>
1da177e4 43
1da177e4
LT
44#include <asm/byteorder.h>
45#include <asm/io.h>
46#include <asm/irq.h>
1da177e4 47#include <asm/unaligned.h>
1da177e4 48
df7c1ca2
GL
49#if defined(CONFIG_PPC_PS3)
50#include <asm/firmware.h>
51#endif
52
1da177e4
LT
53/*-------------------------------------------------------------------------*/
54
55/*
56 * EHCI hc_driver implementation ... experimental, incomplete.
57 * Based on the final 1.0 register interface specification.
58 *
59 * USB 2.0 shows up in upcoming www.pcmcia.org technology.
60 * First was PCMCIA, like ISA; then CardBus, which is PCI.
61 * Next comes "CardBay", using USB 2.0 signals.
62 *
63 * Contains additional contributions by Brad Hards, Rory Bolt, and others.
64 * Special thanks to Intel and VIA for providing host controllers to
65 * test this driver on, and Cypress (including In-System Design) for
66 * providing early devices for those host controllers to talk to!
1da177e4
LT
67 */
68
1da177e4
LT
69#define DRIVER_AUTHOR "David Brownell"
70#define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
71
72static const char hcd_name [] = "ehci_hcd";
73
74
9776afc8 75#undef VERBOSE_DEBUG
1da177e4
LT
76#undef EHCI_URB_TRACE
77
78#ifdef DEBUG
79#define EHCI_STATS
80#endif
81
82/* magic numbers that can affect system performance */
83#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
84#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
85#define EHCI_TUNE_RL_TT 0
86#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
87#define EHCI_TUNE_MULT_TT 1
ffda0803
AS
88/*
89 * Some drivers think it's safe to schedule isochronous transfers more than
90 * 256 ms into the future (partly as a result of an old bug in the scheduling
91 * code). In an attempt to avoid trouble, we will use a minimum scheduling
92 * length of 512 frames instead of 256.
93 */
94#define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
1da177e4 95
1da177e4 96#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
1da177e4
LT
97
98/* Initial IRQ latency: faster than hw default */
99static int log2_irq_thresh = 0; // 0 to 6
100module_param (log2_irq_thresh, int, S_IRUGO);
101MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
102
103/* initial park setting: slower than hw default */
104static unsigned park = 0;
105module_param (park, uint, S_IRUGO);
106MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
107
93f1a47c 108/* for flakey hardware, ignore overcurrent indicators */
90ab5ee9 109static bool ignore_oc = 0;
93f1a47c
DB
110module_param (ignore_oc, bool, S_IRUGO);
111MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
112
48f24970
AD
113/* for link power management(LPM) feature */
114static unsigned int hird;
115module_param(hird, int, S_IRUGO);
cc556871 116MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
48f24970 117
1da177e4
LT
118#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
119
120/*-------------------------------------------------------------------------*/
121
122#include "ehci.h"
123#include "ehci-dbg.c"
ad93562b 124#include "pci-quirks.h"
1da177e4
LT
125
126/*-------------------------------------------------------------------------*/
127
bc29847e
AS
128static void
129timer_action(struct ehci_hcd *ehci, enum ehci_timer_action action)
130{
bc29847e
AS
131 if (!test_and_set_bit(action, &ehci->actions)) {
132 unsigned long t;
133
134 switch (action) {
135 case TIMER_IO_WATCHDOG:
403dbd36
AD
136 if (!ehci->need_io_watchdog)
137 return;
bc29847e
AS
138 t = EHCI_IO_JIFFIES;
139 break;
bc29847e
AS
140 }
141 mod_timer(&ehci->watchdog, t + jiffies);
142 }
143}
144
145/*-------------------------------------------------------------------------*/
146
1da177e4
LT
147/*
148 * handshake - spin reading hc until handshake completes or fails
149 * @ptr: address of hc register to be read
150 * @mask: bits to look at in result of read
151 * @done: value of those bits when handshake succeeds
152 * @usec: timeout in microseconds
153 *
154 * Returns negative errno, or zero on success
155 *
156 * Success happens when the "mask" bits have the specified value (hardware
157 * handshake done). There are two failure modes: "usec" have passed (major
158 * hardware flakeout), or the register reads as all-ones (hardware removed).
159 *
160 * That last failure should_only happen in cases like physical cardbus eject
161 * before driver shutdown. But it also seems to be caused by bugs in cardbus
162 * bridge shutdown: shutting down the bridge before the devices using it.
163 */
083522d7
BH
164static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
165 u32 mask, u32 done, int usec)
1da177e4
LT
166{
167 u32 result;
168
169 do {
083522d7 170 result = ehci_readl(ehci, ptr);
1da177e4
LT
171 if (result == ~(u32)0) /* card removed */
172 return -ENODEV;
173 result &= mask;
174 if (result == done)
175 return 0;
176 udelay (1);
177 usec--;
178 } while (usec > 0);
179 return -ETIMEDOUT;
180}
181
65fd4272
MC
182/* check TDI/ARC silicon is in host mode */
183static int tdi_in_host_mode (struct ehci_hcd *ehci)
184{
65fd4272
MC
185 u32 tmp;
186
a46af4eb 187 tmp = ehci_readl(ehci, &ehci->regs->usbmode);
65fd4272
MC
188 return (tmp & 3) == USBMODE_CM_HC;
189}
190
1da177e4
LT
191/* force HC to halt state from unknown (EHCI spec section 2.3) */
192static int ehci_halt (struct ehci_hcd *ehci)
193{
083522d7 194 u32 temp = ehci_readl(ehci, &ehci->regs->status);
1da177e4 195
72f30b6f 196 /* disable any irqs left enabled by previous code */
083522d7 197 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
72f30b6f 198
65fd4272
MC
199 if (ehci_is_TDI(ehci) && tdi_in_host_mode(ehci) == 0) {
200 return 0;
201 }
202
1da177e4
LT
203 if ((temp & STS_HALT) != 0)
204 return 0;
205
3d9545cc
AS
206 /*
207 * This routine gets called during probe before ehci->command
208 * has been initialized, so we can't rely on its value.
209 */
210 ehci->command &= ~CMD_RUN;
083522d7 211 temp = ehci_readl(ehci, &ehci->regs->command);
3d9545cc 212 temp &= ~(CMD_RUN | CMD_IAAD);
083522d7
BH
213 ehci_writel(ehci, temp, &ehci->regs->command);
214 return handshake (ehci, &ehci->regs->status,
215 STS_HALT, STS_HALT, 16 * 125);
1da177e4
LT
216}
217
218/* put TDI/ARC silicon into EHCI mode */
219static void tdi_reset (struct ehci_hcd *ehci)
220{
1da177e4
LT
221 u32 tmp;
222
a46af4eb 223 tmp = ehci_readl(ehci, &ehci->regs->usbmode);
d23a1377
VB
224 tmp |= USBMODE_CM_HC;
225 /* The default byte access to MMR space is LE after
226 * controller reset. Set the required endian mode
227 * for transfer buffers to match the host microprocessor
228 */
229 if (ehci_big_endian_mmio(ehci))
230 tmp |= USBMODE_BE;
a46af4eb 231 ehci_writel(ehci, tmp, &ehci->regs->usbmode);
1da177e4
LT
232}
233
234/* reset a non-running (STS_HALT == 1) controller */
235static int ehci_reset (struct ehci_hcd *ehci)
236{
237 int retval;
083522d7 238 u32 command = ehci_readl(ehci, &ehci->regs->command);
1da177e4 239
8d053c79
JW
240 /* If the EHCI debug controller is active, special care must be
241 * taken before and after a host controller reset */
242 if (ehci->debug && !dbgp_reset_prep())
243 ehci->debug = NULL;
244
1da177e4
LT
245 command |= CMD_RESET;
246 dbg_cmd (ehci, "reset", command);
083522d7 247 ehci_writel(ehci, command, &ehci->regs->command);
e8799906 248 ehci->rh_state = EHCI_RH_HALTED;
1da177e4 249 ehci->next_statechange = jiffies;
083522d7
BH
250 retval = handshake (ehci, &ehci->regs->command,
251 CMD_RESET, 0, 250 * 1000);
1da177e4 252
331ac6b2
AD
253 if (ehci->has_hostpc) {
254 ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
a46af4eb
AS
255 &ehci->regs->usbmode_ex);
256 ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
331ac6b2 257 }
1da177e4
LT
258 if (retval)
259 return retval;
260
261 if (ehci_is_TDI(ehci))
262 tdi_reset (ehci);
263
8d053c79
JW
264 if (ehci->debug)
265 dbgp_external_startup();
266
a448e4dc
AS
267 ehci->port_c_suspend = ehci->suspended_ports =
268 ehci->resuming_ports = 0;
1da177e4
LT
269 return retval;
270}
271
272/* idle the controller (from running) */
273static void ehci_quiesce (struct ehci_hcd *ehci)
274{
275 u32 temp;
276
e8799906 277 if (ehci->rh_state != EHCI_RH_RUNNING)
c0c53dbc 278 return;
1da177e4
LT
279
280 /* wait for any schedule enables/disables to take effect */
3d9545cc 281 temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
9671cd7a 282 handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp, 16 * 125);
1da177e4
LT
283
284 /* then disable anything that's still active */
3d9545cc
AS
285 ehci->command &= ~(CMD_ASE | CMD_PSE);
286 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1da177e4
LT
287
288 /* hardware can take 16 microframes to turn off ... */
9671cd7a 289 handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0, 16 * 125);
1da177e4
LT
290}
291
292/*-------------------------------------------------------------------------*/
293
07d29b63 294static void end_unlink_async(struct ehci_hcd *ehci);
32830f20 295static void unlink_empty_async(struct ehci_hcd *ehci);
7d12e780 296static void ehci_work(struct ehci_hcd *ehci);
df202255
AS
297static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
298static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
1da177e4 299
d58b4bcc 300#include "ehci-timer.c"
1da177e4 301#include "ehci-hub.c"
48f24970 302#include "ehci-lpm.c"
1da177e4
LT
303#include "ehci-mem.c"
304#include "ehci-q.c"
305#include "ehci-sched.c"
4c67045b 306#include "ehci-sysfs.c"
1da177e4
LT
307
308/*-------------------------------------------------------------------------*/
309
07d29b63
AS
310static void ehci_watchdog(unsigned long param)
311{
312 struct ehci_hcd *ehci = (struct ehci_hcd *) param;
313 unsigned long flags;
314
315 spin_lock_irqsave(&ehci->lock, flags);
316
1da177e4 317 /* ehci could run by timer, without IRQs ... */
7d12e780 318 ehci_work (ehci);
1da177e4
LT
319
320 spin_unlock_irqrestore (&ehci->lock, flags);
321}
322
8903795a
AS
323/* On some systems, leaving remote wakeup enabled prevents system shutdown.
324 * The firmware seems to think that powering off is a wakeup event!
325 * This routine turns off remote wakeup and everything else, on all ports.
326 */
327static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
328{
329 int port = HCS_N_PORTS(ehci->hcs_params);
330
331 while (port--)
332 ehci_writel(ehci, PORT_RWC_BITS,
333 &ehci->regs->port_status[port]);
334}
335
21da84a8
SS
336/*
337 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
338 * Should be called with ehci->lock held.
72f30b6f 339 */
21da84a8 340static void ehci_silence_controller(struct ehci_hcd *ehci)
1da177e4 341{
21da84a8 342 ehci_halt(ehci);
8903795a 343 ehci_turn_off_all_ports(ehci);
1da177e4
LT
344
345 /* make BIOS/etc use companion controller during reboot */
083522d7 346 ehci_writel(ehci, 0, &ehci->regs->configured_flag);
8903795a
AS
347
348 /* unblock posted writes */
349 ehci_readl(ehci, &ehci->regs->configured_flag);
1da177e4
LT
350}
351
21da84a8
SS
352/* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
353 * This forcibly disables dma and IRQs, helping kexec and other cases
354 * where the next system software may expect clean state.
355 */
356static void ehci_shutdown(struct usb_hcd *hcd)
357{
358 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
359
360 del_timer_sync(&ehci->watchdog);
21da84a8
SS
361
362 spin_lock_irq(&ehci->lock);
c0c53dbc 363 ehci->rh_state = EHCI_RH_STOPPING;
21da84a8 364 ehci_silence_controller(ehci);
d58b4bcc 365 ehci->enabled_hrtimer_events = 0;
21da84a8 366 spin_unlock_irq(&ehci->lock);
d58b4bcc
AS
367
368 hrtimer_cancel(&ehci->hrtimer);
21da84a8
SS
369}
370
56c1e26d
DB
371static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
372{
373 unsigned port;
374
375 if (!HCS_PPC (ehci->hcs_params))
376 return;
377
378 ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
379 for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
380 (void) ehci_hub_control(ehci_to_hcd(ehci),
381 is_on ? SetPortFeature : ClearPortFeature,
382 USB_PORT_FEAT_POWER,
383 port--, NULL, 0);
383975d7
AS
384 /* Flush those writes */
385 ehci_readl(ehci, &ehci->regs->command);
56c1e26d
DB
386 msleep(20);
387}
388
7ff71d6a 389/*-------------------------------------------------------------------------*/
1da177e4 390
7ff71d6a
MP
391/*
392 * ehci_work is called from some interrupts, timers, and so on.
393 * it calls driver completion functions, after dropping ehci->lock.
394 */
7d12e780 395static void ehci_work (struct ehci_hcd *ehci)
7ff71d6a
MP
396{
397 timer_action_done (ehci, TIMER_IO_WATCHDOG);
7ff71d6a
MP
398
399 /* another CPU may drop ehci->lock during a schedule scan while
400 * it reports urb completions. this flag guards against bogus
401 * attempts at re-entrant schedule scanning.
402 */
403 if (ehci->scanning)
404 return;
405 ehci->scanning = 1;
31446610
AS
406 if (ehci->async_count)
407 scan_async(ehci);
7ff71d6a 408 if (ehci->next_uframe != -1)
7d12e780 409 scan_periodic (ehci);
7ff71d6a
MP
410 ehci->scanning = 0;
411
412 /* the IO watchdog guards against hardware or driver bugs that
413 * misplace IRQs, and should let us run completely without IRQs.
414 * such lossage has been observed on both VT6202 and VT8235.
415 */
e8799906 416 if (ehci->rh_state == EHCI_RH_RUNNING &&
7ff71d6a 417 (ehci->async->qh_next.ptr != NULL ||
3ca9aeba 418 ehci->periodic_count != 0))
7ff71d6a
MP
419 timer_action (ehci, TIMER_IO_WATCHDOG);
420}
1da177e4 421
21da84a8
SS
422/*
423 * Called when the ehci_hcd module is removed.
424 */
7ff71d6a 425static void ehci_stop (struct usb_hcd *hcd)
1da177e4
LT
426{
427 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
1da177e4 428
7ff71d6a 429 ehci_dbg (ehci, "stop\n");
1da177e4 430
7ff71d6a
MP
431 /* no more interrupts ... */
432 del_timer_sync (&ehci->watchdog);
56c1e26d 433
7ff71d6a 434 spin_lock_irq(&ehci->lock);
d58b4bcc 435 ehci->enabled_hrtimer_events = 0;
c0c53dbc 436 ehci_quiesce(ehci);
1da177e4 437
21da84a8 438 ehci_silence_controller(ehci);
7ff71d6a 439 ehci_reset (ehci);
7ff71d6a 440 spin_unlock_irq(&ehci->lock);
1da177e4 441
d58b4bcc 442 hrtimer_cancel(&ehci->hrtimer);
4c67045b 443 remove_sysfs_files(ehci);
7ff71d6a 444 remove_debug_files (ehci);
1da177e4 445
7ff71d6a
MP
446 /* root hub is shut down separately (first, when possible) */
447 spin_lock_irq (&ehci->lock);
448 if (ehci->async)
7d12e780 449 ehci_work (ehci);
55934eb3 450 end_free_itds(ehci);
7ff71d6a
MP
451 spin_unlock_irq (&ehci->lock);
452 ehci_mem_cleanup (ehci);
1da177e4 453
ad93562b
AX
454 if (ehci->amd_pll_fix == 1)
455 usb_amd_dev_put();
05570297 456
7ff71d6a 457#ifdef EHCI_STATS
99ac5b1e
AS
458 ehci_dbg(ehci, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
459 ehci->stats.normal, ehci->stats.error, ehci->stats.iaa,
7ff71d6a
MP
460 ehci->stats.lost_iaa);
461 ehci_dbg (ehci, "complete %ld unlink %ld\n",
462 ehci->stats.complete, ehci->stats.unlink);
1da177e4 463#endif
1da177e4 464
083522d7
BH
465 dbg_status (ehci, "ehci_stop completed",
466 ehci_readl(ehci, &ehci->regs->status));
1da177e4
LT
467}
468
18807521
DB
469/* one-time init, only for memory state */
470static int ehci_init(struct usb_hcd *hcd)
1da177e4 471{
18807521 472 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1da177e4 473 u32 temp;
1da177e4
LT
474 int retval;
475 u32 hcc_params;
3807e26d 476 struct ehci_qh_hw *hw;
18807521
DB
477
478 spin_lock_init(&ehci->lock);
479
403dbd36
AD
480 /*
481 * keep io watchdog by default, those good HCDs could turn off it later
482 */
483 ehci->need_io_watchdog = 1;
18807521
DB
484 init_timer(&ehci->watchdog);
485 ehci->watchdog.function = ehci_watchdog;
486 ehci->watchdog.data = (unsigned long) ehci;
1da177e4 487
d58b4bcc
AS
488 hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
489 ehci->hrtimer.function = ehci_hrtimer_func;
490 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
491
f75593ce
AS
492 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
493
cc62a7eb
KS
494 /*
495 * by default set standard 80% (== 100 usec/uframe) max periodic
496 * bandwidth as required by USB 2.0
497 */
498 ehci->uframe_periodic_max = 100;
499
1da177e4
LT
500 /*
501 * hw default: 1K periodic list heads, one per frame.
502 * periodic_size can shrink by USBCMD update if hcc_params allows.
503 */
504 ehci->periodic_size = DEFAULT_I_TDPS;
9aa09d2f 505 INIT_LIST_HEAD(&ehci->cached_itd_list);
0e5f231b 506 INIT_LIST_HEAD(&ehci->cached_sitd_list);
f75593ce 507
8e192910 508 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
f75593ce
AS
509 /* periodic schedule size can be smaller than default */
510 switch (EHCI_TUNE_FLS) {
511 case 0: ehci->periodic_size = 1024; break;
512 case 1: ehci->periodic_size = 512; break;
513 case 2: ehci->periodic_size = 256; break;
514 default: BUG();
515 }
516 }
18807521 517 if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
1da177e4
LT
518 return retval;
519
520 /* controllers may cache some of the periodic schedule ... */
53bd6a60 521 if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
dccd574c 522 ehci->i_thresh = 2 + 8;
1da177e4 523 else // N microframes cached
18807521 524 ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
1da177e4 525
1da177e4 526 ehci->next_uframe = -1;
9aa09d2f 527 ehci->clock_frame = -1;
1da177e4 528
1da177e4
LT
529 /*
530 * dedicate a qh for the async ring head, since we couldn't unlink
531 * a 'real' qh without stopping the async schedule [4.8]. use it
532 * as the 'reclamation list head' too.
533 * its dummy is used in hw_alt_next of many tds, to prevent the qh
534 * from automatically advancing to the next td after short reads.
535 */
18807521 536 ehci->async->qh_next.qh = NULL;
3807e26d
AD
537 hw = ehci->async->hw;
538 hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
539 hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
4f7a67e2 540#if defined(CONFIG_PPC_PS3)
4c53de72 541 hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
4f7a67e2 542#endif
3807e26d
AD
543 hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
544 hw->hw_qtd_next = EHCI_LIST_END(ehci);
18807521 545 ehci->async->qh_state = QH_STATE_LINKED;
3807e26d 546 hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
1da177e4
LT
547
548 /* clear interrupt enables, set irq latency */
549 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
550 log2_irq_thresh = 0;
551 temp = 1 << (16 + log2_irq_thresh);
5a9cdf33
AD
552 if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
553 ehci->has_ppcd = 1;
554 ehci_dbg(ehci, "enable per-port change event\n");
555 temp |= CMD_PPCEE;
556 }
1da177e4
LT
557 if (HCC_CANPARK(hcc_params)) {
558 /* HW default park == 3, on hardware that supports it (like
559 * NVidia and ALI silicon), maximizes throughput on the async
560 * schedule by avoiding QH fetches between transfers.
561 *
562 * With fast usb storage devices and NForce2, "park" seems to
563 * make problems: throughput reduction (!), data errors...
564 */
565 if (park) {
18807521 566 park = min(park, (unsigned) 3);
1da177e4
LT
567 temp |= CMD_PARK;
568 temp |= park << 8;
569 }
18807521 570 ehci_dbg(ehci, "park %d\n", park);
1da177e4 571 }
18807521 572 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
1da177e4
LT
573 /* periodic schedule size can be smaller than default */
574 temp &= ~(3 << 2);
575 temp |= (EHCI_TUNE_FLS << 2);
1da177e4 576 }
48f24970
AD
577 if (HCC_LPM(hcc_params)) {
578 /* support link power management EHCI 1.1 addendum */
579 ehci_dbg(ehci, "support lpm\n");
580 ehci->has_lpm = 1;
581 if (hird > 0xf) {
582 ehci_dbg(ehci, "hird %d invalid, use default 0",
583 hird);
584 hird = 0;
585 }
586 temp |= hird << 24;
587 }
18807521
DB
588 ehci->command = temp;
589
40f8db8f 590 /* Accept arbitrarily long scatter-gather lists */
4307a28e
AR
591 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
592 hcd->self.sg_tablesize = ~0;
18807521
DB
593 return 0;
594}
595
596/* start HC running; it's halted, ehci_init() has been run (once) */
597static int ehci_run (struct usb_hcd *hcd)
598{
599 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
18807521
DB
600 u32 temp;
601 u32 hcc_params;
602
1d619f12 603 hcd->uses_new_polling = 1;
1d619f12 604
18807521 605 /* EHCI spec section 4.1 */
876e0df9 606
083522d7
BH
607 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
608 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
18807521
DB
609
610 /*
611 * hcc_params controls whether ehci->regs->segment must (!!!)
612 * be used; it constrains QH/ITD/SITD and QTD locations.
613 * pci_pool consistent memory always uses segment zero.
614 * streaming mappings for I/O buffers, like pci_map_single(),
615 * can return segments above 4GB, if the device allows.
616 *
617 * NOTE: the dma mask is visible through dma_supported(), so
618 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
619 * Scsi_Host.highmem_io, and so forth. It's readonly to all
620 * host side drivers though.
621 */
083522d7 622 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
18807521 623 if (HCC_64BIT_ADDR(hcc_params)) {
083522d7 624 ehci_writel(ehci, 0, &ehci->regs->segment);
18807521
DB
625#if 0
626// this is deeply broken on almost all architectures
6a35528a 627 if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
18807521
DB
628 ehci_info(ehci, "enabled 64bit DMA\n");
629#endif
630 }
631
632
1da177e4
LT
633 // Philips, Intel, and maybe others need CMD_RUN before the
634 // root hub will detect new devices (why?); NEC doesn't
18807521
DB
635 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
636 ehci->command |= CMD_RUN;
083522d7 637 ehci_writel(ehci, ehci->command, &ehci->regs->command);
18807521 638 dbg_cmd (ehci, "init", ehci->command);
1da177e4 639
1da177e4
LT
640 /*
641 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
642 * are explicitly handed to companion controller(s), so no TT is
643 * involved with the root hub. (Except where one is integrated,
644 * and there's no companion controller unless maybe for USB OTG.)
32fe0198
AS
645 *
646 * Turning on the CF flag will transfer ownership of all ports
647 * from the companions to the EHCI controller. If any of the
648 * companions are in the middle of a port reset at the time, it
649 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
1cb52658
DB
650 * guarantees that no resets are in progress. After we set CF,
651 * a short delay lets the hardware catch up; new resets shouldn't
652 * be started before the port switching actions could complete.
1da177e4 653 */
32fe0198 654 down_write(&ehci_cf_port_reset_rwsem);
e8799906 655 ehci->rh_state = EHCI_RH_RUNNING;
083522d7
BH
656 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
657 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1cb52658 658 msleep(5);
32fe0198 659 up_write(&ehci_cf_port_reset_rwsem);
ee4ecb8a 660 ehci->last_periodic_enable = ktime_get_real();
1da177e4 661
c430131a 662 temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
1da177e4 663 ehci_info (ehci,
2b70f073 664 "USB %x.%x started, EHCI %x.%02x%s\n",
7ff71d6a 665 ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
2b70f073 666 temp >> 8, temp & 0xff,
93f1a47c 667 ignore_oc ? ", overcurrent ignored" : "");
1da177e4 668
083522d7
BH
669 ehci_writel(ehci, INTR_MASK,
670 &ehci->regs->intr_enable); /* Turn On Interrupts */
1da177e4 671
18807521
DB
672 /* GRR this is run-once init(), being done every time the HC starts.
673 * So long as they're part of class devices, we can't do it init()
674 * since the class device isn't created that early.
675 */
676 create_debug_files(ehci);
4c67045b 677 create_sysfs_files(ehci);
1da177e4
LT
678
679 return 0;
680}
681
1a49e2ac 682static int ehci_setup(struct usb_hcd *hcd)
2093c6b4
MC
683{
684 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
685 int retval;
686
687 ehci->regs = (void __iomem *)ehci->caps +
688 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
689 dbg_hcs_params(ehci, "reset");
690 dbg_hcc_params(ehci, "reset");
691
692 /* cache this readonly data; minimize chip reads */
693 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
694
695 ehci->sbrn = HCD_USB2;
696
631fe9d9
AS
697 /* data structure init */
698 retval = ehci_init(hcd);
2093c6b4
MC
699 if (retval)
700 return retval;
701
631fe9d9 702 retval = ehci_halt(ehci);
2093c6b4
MC
703 if (retval)
704 return retval;
705
1a49e2ac
AS
706 if (ehci_is_TDI(ehci))
707 tdi_reset(ehci);
708
2093c6b4
MC
709 ehci_reset(ehci);
710
711 return 0;
712}
713
1da177e4
LT
714/*-------------------------------------------------------------------------*/
715
7d12e780 716static irqreturn_t ehci_irq (struct usb_hcd *hcd)
1da177e4
LT
717{
718 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
67b2e029 719 u32 status, masked_status, pcd_status = 0, cmd;
1da177e4
LT
720 int bh;
721
722 spin_lock (&ehci->lock);
723
083522d7 724 status = ehci_readl(ehci, &ehci->regs->status);
1da177e4
LT
725
726 /* e.g. cardbus physical eject */
727 if (status == ~(u32) 0) {
728 ehci_dbg (ehci, "device removed\n");
729 goto dead;
730 }
731
2fbe2bf1
AS
732 /*
733 * We don't use STS_FLR, but some controllers don't like it to
734 * remain on, so mask it out along with the other status bits.
735 */
736 masked_status = status & (INTR_MASK | STS_FLR);
737
69fff59d 738 /* Shared IRQ? */
e8799906 739 if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
1da177e4
LT
740 spin_unlock(&ehci->lock);
741 return IRQ_NONE;
742 }
743
744 /* clear (just) interrupts */
67b2e029 745 ehci_writel(ehci, masked_status, &ehci->regs->status);
e82cc128 746 cmd = ehci_readl(ehci, &ehci->regs->command);
1da177e4
LT
747 bh = 0;
748
9776afc8 749#ifdef VERBOSE_DEBUG
1da177e4
LT
750 /* unrequested/ignored: Frame List Rollover */
751 dbg_status (ehci, "irq", status);
752#endif
753
754 /* INT, ERR, and IAA interrupt rates can be throttled */
755
756 /* normal [4.15.1.2] or error [4.15.1.1] completion */
757 if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
758 if (likely ((status & STS_ERR) == 0))
759 COUNT (ehci->stats.normal);
760 else
761 COUNT (ehci->stats.error);
762 bh = 1;
763 }
764
765 /* complete the unlinking of some qh [4.15.2.3] */
766 if (status & STS_IAA) {
9d938747
AS
767
768 /* Turn off the IAA watchdog */
769 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_IAA_WATCHDOG);
770
771 /*
772 * Mild optimization: Allow another IAAD to reset the
773 * hrtimer, if one occurs before the next expiration.
774 * In theory we could always cancel the hrtimer, but
775 * tests show that about half the time it will be reset
776 * for some other event anyway.
777 */
778 if (ehci->next_hrtimer_event == EHCI_HRTIMER_IAA_WATCHDOG)
779 ++ehci->next_hrtimer_event;
780
e82cc128 781 /* guard against (alleged) silicon errata */
6feff1b9 782 if (cmd & CMD_IAAD)
e82cc128 783 ehci_dbg(ehci, "IAA with IAAD still set?\n");
3c273a05 784 if (ehci->async_iaa) {
99ac5b1e 785 COUNT(ehci->stats.iaa);
e82cc128
DB
786 end_unlink_async(ehci);
787 } else
99ac5b1e 788 ehci_dbg(ehci, "IAA with nothing unlinked?\n");
1da177e4
LT
789 }
790
791 /* remote wakeup [4.3.1] */
d97cc2f2 792 if (status & STS_PCD) {
1da177e4 793 unsigned i = HCS_N_PORTS (ehci->hcs_params);
5a9cdf33 794 u32 ppcd = 0;
d1b1842c
DB
795
796 /* kick root hub later */
1d619f12 797 pcd_status = status;
1da177e4
LT
798
799 /* resume root hub? */
dc75ce9d 800 if (ehci->rh_state == EHCI_RH_SUSPENDED)
8c03356a 801 usb_hcd_resume_root_hub(hcd);
1da177e4 802
5a9cdf33
AD
803 /* get per-port change detect bits */
804 if (ehci->has_ppcd)
805 ppcd = status >> 16;
806
1da177e4 807 while (i--) {
5a9cdf33
AD
808 int pstatus;
809
810 /* leverage per-port change bits feature */
811 if (ehci->has_ppcd && !(ppcd & (1 << i)))
812 continue;
813 pstatus = ehci_readl(ehci,
814 &ehci->regs->port_status[i]);
b972b68c
DB
815
816 if (pstatus & PORT_OWNER)
1da177e4 817 continue;
eafe5b99
AS
818 if (!(test_bit(i, &ehci->suspended_ports) &&
819 ((pstatus & PORT_RESUME) ||
820 !(pstatus & PORT_SUSPEND)) &&
821 (pstatus & PORT_PE) &&
822 ehci->reset_done[i] == 0))
1da177e4
LT
823 continue;
824
825 /* start 20 msec resume signaling from this port,
826 * and make khubd collect PORT_STAT_C_SUSPEND to
49d0f078
AS
827 * stop that signaling. Use 5 ms extra for safety,
828 * like usb_port_resume() does.
1da177e4 829 */
49d0f078 830 ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
a448e4dc 831 set_bit(i, &ehci->resuming_ports);
1da177e4 832 ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
61e8b858 833 mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
1da177e4
LT
834 }
835 }
836
837 /* PCI errors [4.15.2.4] */
838 if (unlikely ((status & STS_FATAL) != 0)) {
67b2e029 839 ehci_err(ehci, "fatal error\n");
eafe5b99
AS
840 dbg_cmd(ehci, "fatal", cmd);
841 dbg_status(ehci, "fatal", status);
1da177e4 842dead:
69fff59d 843 usb_hc_died(hcd);
bf6387bc
AS
844
845 /* Don't let the controller do anything more */
846 ehci->rh_state = EHCI_RH_STOPPING;
847 ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
848 ehci_writel(ehci, ehci->command, &ehci->regs->command);
849 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
850 ehci_handle_controller_death(ehci);
851
852 /* Handle completions when the controller stops */
853 bh = 0;
1da177e4
LT
854 }
855
856 if (bh)
7d12e780 857 ehci_work (ehci);
1da177e4 858 spin_unlock (&ehci->lock);
d1b1842c 859 if (pcd_status)
1d619f12 860 usb_hcd_poll_rh_status(hcd);
1da177e4
LT
861 return IRQ_HANDLED;
862}
863
864/*-------------------------------------------------------------------------*/
865
866/*
867 * non-error returns are a promise to giveback() the urb later
868 * we drop ownership so next owner (or urb unlink) can get it
869 *
870 * urb + dev is in hcd.self.controller.urb_list
871 * we're queueing TDs onto software and hardware lists
872 *
873 * hcd-specific init for hcpriv hasn't been done yet
874 *
875 * NOTE: control, bulk, and interrupt share the same code to append TDs
876 * to a (possibly active) QH, and the same QH scanning code.
877 */
878static int ehci_urb_enqueue (
879 struct usb_hcd *hcd,
1da177e4 880 struct urb *urb,
55016f10 881 gfp_t mem_flags
1da177e4
LT
882) {
883 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
884 struct list_head qtd_list;
885
886 INIT_LIST_HEAD (&qtd_list);
887
888 switch (usb_pipetype (urb->pipe)) {
25b70a86
DB
889 case PIPE_CONTROL:
890 /* qh_completions() code doesn't handle all the fault cases
891 * in multi-TD control transfers. Even 1KB is rare anyway.
892 */
893 if (urb->transfer_buffer_length > (16 * 1024))
894 return -EMSGSIZE;
895 /* FALLTHROUGH */
896 /* case PIPE_BULK: */
1da177e4
LT
897 default:
898 if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
899 return -ENOMEM;
e9df41c5 900 return submit_async(ehci, urb, &qtd_list, mem_flags);
1da177e4
LT
901
902 case PIPE_INTERRUPT:
903 if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
904 return -ENOMEM;
e9df41c5 905 return intr_submit(ehci, urb, &qtd_list, mem_flags);
1da177e4
LT
906
907 case PIPE_ISOCHRONOUS:
908 if (urb->dev->speed == USB_SPEED_HIGH)
909 return itd_submit (ehci, urb, mem_flags);
910 else
911 return sitd_submit (ehci, urb, mem_flags);
912 }
913}
914
1da177e4
LT
915/* remove from hardware lists
916 * completions normally happen asynchronously
917 */
918
e9df41c5 919static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1da177e4
LT
920{
921 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
922 struct ehci_qh *qh;
923 unsigned long flags;
e9df41c5 924 int rc;
1da177e4
LT
925
926 spin_lock_irqsave (&ehci->lock, flags);
e9df41c5
AS
927 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
928 if (rc)
929 goto done;
930
1da177e4
LT
931 switch (usb_pipetype (urb->pipe)) {
932 // case PIPE_CONTROL:
933 // case PIPE_BULK:
934 default:
935 qh = (struct ehci_qh *) urb->hcpriv;
936 if (!qh)
937 break;
07d29b63
AS
938 switch (qh->qh_state) {
939 case QH_STATE_LINKED:
940 case QH_STATE_COMPLETING:
3c273a05 941 start_unlink_async(ehci, qh);
07d29b63
AS
942 break;
943 case QH_STATE_UNLINK:
944 case QH_STATE_UNLINK_WAIT:
945 /* already started */
946 break;
947 case QH_STATE_IDLE:
7a0f0d95
AS
948 /* QH might be waiting for a Clear-TT-Buffer */
949 qh_completions(ehci, qh);
07d29b63
AS
950 break;
951 }
1da177e4
LT
952 break;
953
954 case PIPE_INTERRUPT:
955 qh = (struct ehci_qh *) urb->hcpriv;
956 if (!qh)
957 break;
958 switch (qh->qh_state) {
959 case QH_STATE_LINKED:
a448c9d8 960 case QH_STATE_COMPLETING:
df202255 961 start_unlink_intr(ehci, qh);
a448c9d8 962 break;
1da177e4 963 case QH_STATE_IDLE:
7d12e780 964 qh_completions (ehci, qh);
1da177e4
LT
965 break;
966 default:
967 ehci_dbg (ehci, "bogus qh %p state %d\n",
968 qh, qh->qh_state);
969 goto done;
970 }
1da177e4
LT
971 break;
972
973 case PIPE_ISOCHRONOUS:
974 // itd or sitd ...
975
976 // wait till next completion, do it then.
977 // completion irqs can wait up to 1024 msec,
978 break;
979 }
980done:
981 spin_unlock_irqrestore (&ehci->lock, flags);
e9df41c5 982 return rc;
1da177e4
LT
983}
984
985/*-------------------------------------------------------------------------*/
986
987// bulk qh holds the data toggle
988
989static void
990ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
991{
992 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
993 unsigned long flags;
994 struct ehci_qh *qh, *tmp;
995
996 /* ASSERT: any requests/urbs are being unlinked */
997 /* ASSERT: nobody can be submitting urbs for this any more */
998
999rescan:
1000 spin_lock_irqsave (&ehci->lock, flags);
1001 qh = ep->hcpriv;
1002 if (!qh)
1003 goto done;
1004
1005 /* endpoints can be iso streams. for now, we don't
1006 * accelerate iso completions ... so spin a while.
1007 */
1082f57a 1008 if (qh->hw == NULL) {
8c5bf7be
AS
1009 struct ehci_iso_stream *stream = ep->hcpriv;
1010
1011 if (!list_empty(&stream->td_list))
1012 goto idle_timeout;
1013
1014 /* BUG_ON(!list_empty(&stream->free_list)); */
1015 kfree(stream);
1016 goto done;
1da177e4
LT
1017 }
1018
c0c53dbc 1019 if (ehci->rh_state < EHCI_RH_RUNNING)
1da177e4
LT
1020 qh->qh_state = QH_STATE_IDLE;
1021 switch (qh->qh_state) {
1022 case QH_STATE_LINKED:
3a44494e 1023 case QH_STATE_COMPLETING:
1da177e4
LT
1024 for (tmp = ehci->async->qh_next.qh;
1025 tmp && tmp != qh;
1026 tmp = tmp->qh_next.qh)
1027 continue;
02e2c51b
AS
1028 /* periodic qh self-unlinks on empty, and a COMPLETING qh
1029 * may already be unlinked.
1030 */
1031 if (tmp)
3c273a05 1032 start_unlink_async(ehci, qh);
1da177e4
LT
1033 /* FALL THROUGH */
1034 case QH_STATE_UNLINK: /* wait for hw to finish? */
07d29b63 1035 case QH_STATE_UNLINK_WAIT:
1da177e4
LT
1036idle_timeout:
1037 spin_unlock_irqrestore (&ehci->lock, flags);
22c43863 1038 schedule_timeout_uninterruptible(1);
1da177e4
LT
1039 goto rescan;
1040 case QH_STATE_IDLE: /* fully unlinked */
914b7012
AS
1041 if (qh->clearing_tt)
1042 goto idle_timeout;
1da177e4 1043 if (list_empty (&qh->qtd_list)) {
c83e1a9f 1044 qh_destroy(ehci, qh);
1da177e4
LT
1045 break;
1046 }
1047 /* else FALL THROUGH */
1048 default:
1da177e4
LT
1049 /* caller was supposed to have unlinked any requests;
1050 * that's not our job. just leak this memory.
1051 */
1052 ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
1053 qh, ep->desc.bEndpointAddress, qh->qh_state,
1054 list_empty (&qh->qtd_list) ? "" : "(has tds)");
1055 break;
1056 }
8c5bf7be 1057 done:
1da177e4 1058 ep->hcpriv = NULL;
1da177e4 1059 spin_unlock_irqrestore (&ehci->lock, flags);
1da177e4
LT
1060}
1061
b18ffd49
AS
1062static void
1063ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1064{
1065 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1066 struct ehci_qh *qh;
1067 int eptype = usb_endpoint_type(&ep->desc);
a455212d
AS
1068 int epnum = usb_endpoint_num(&ep->desc);
1069 int is_out = usb_endpoint_dir_out(&ep->desc);
1070 unsigned long flags;
b18ffd49
AS
1071
1072 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1073 return;
1074
a455212d 1075 spin_lock_irqsave(&ehci->lock, flags);
b18ffd49
AS
1076 qh = ep->hcpriv;
1077
1078 /* For Bulk and Interrupt endpoints we maintain the toggle state
1079 * in the hardware; the toggle bits in udev aren't used at all.
1080 * When an endpoint is reset by usb_clear_halt() we must reset
1081 * the toggle bit in the QH.
1082 */
1083 if (qh) {
a455212d 1084 usb_settoggle(qh->dev, epnum, is_out, 0);
b18ffd49
AS
1085 if (!list_empty(&qh->qtd_list)) {
1086 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
3a44494e
AS
1087 } else if (qh->qh_state == QH_STATE_LINKED ||
1088 qh->qh_state == QH_STATE_COMPLETING) {
a455212d
AS
1089
1090 /* The toggle value in the QH can't be updated
1091 * while the QH is active. Unlink it now;
1092 * re-linking will call qh_refresh().
b18ffd49 1093 */
a448c9d8 1094 if (eptype == USB_ENDPOINT_XFER_BULK)
3c273a05 1095 start_unlink_async(ehci, qh);
a448c9d8 1096 else
df202255 1097 start_unlink_intr(ehci, qh);
b18ffd49
AS
1098 }
1099 }
a455212d 1100 spin_unlock_irqrestore(&ehci->lock, flags);
b18ffd49
AS
1101}
1102
7ff71d6a
MP
1103static int ehci_get_frame (struct usb_hcd *hcd)
1104{
1105 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
68aa95d5 1106 return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
7ff71d6a 1107}
1da177e4
LT
1108
1109/*-------------------------------------------------------------------------*/
c5cf9212
AS
1110
1111#ifdef CONFIG_PM
1112
1113/* suspend/resume, section 4.3 */
1114
1115/* These routines handle the generic parts of controller suspend/resume */
1116
1117static int __maybe_unused ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
1118{
1119 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1120
1121 if (time_before(jiffies, ehci->next_statechange))
1122 msleep(10);
1123
1124 /*
1125 * Root hub was already suspended. Disable IRQ emission and
1126 * mark HW unaccessible. The PM and USB cores make sure that
1127 * the root hub is either suspended or stopped.
1128 */
1129 ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
1130
1131 spin_lock_irq(&ehci->lock);
1132 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1133 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
1134
1135 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1136 spin_unlock_irq(&ehci->lock);
1137
1138 return 0;
1139}
1140
1141/* Returns 0 if power was preserved, 1 if power was lost */
1142static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
1143{
1144 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1145
1146 if (time_before(jiffies, ehci->next_statechange))
1147 msleep(100);
1148
1149 /* Mark hardware accessible again as we are back to full power by now */
1150 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1151
1152 /*
1153 * If CF is still set and we aren't resuming from hibernation
1154 * then we maintained suspend power.
1155 * Just undo the effect of ehci_suspend().
1156 */
1157 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
1158 !hibernated) {
1159 int mask = INTR_MASK;
1160
1161 ehci_prepare_ports_for_controller_resume(ehci);
1162 if (!hcd->self.root_hub->do_remote_wakeup)
1163 mask &= ~STS_PCD;
1164 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
1165 ehci_readl(ehci, &ehci->regs->intr_enable);
1166 return 0;
1167 }
1168
1169 /*
1170 * Else reset, to cope with power loss or resume from hibernation
1171 * having let the firmware kick in during reboot.
1172 */
1173 usb_root_hub_lost_power(hcd->self.root_hub);
1174 (void) ehci_halt(ehci);
1175 (void) ehci_reset(ehci);
1176
c5cf9212
AS
1177 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1178 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
1179 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1180
1181 /* here we "know" root ports should always stay powered */
1182 ehci_port_power(ehci, 1);
1183
1184 ehci->rh_state = EHCI_RH_SUSPENDED;
1185 return 1;
1186}
1187
1188#endif
1189
1190/*-------------------------------------------------------------------------*/
1191
eb70e5ab
AS
1192/*
1193 * The EHCI in ChipIdea HDRC cannot be a separate module or device,
1194 * because its registers (and irq) are shared between host/gadget/otg
1195 * functions and in order to facilitate role switching we cannot
1196 * give the ehci driver exclusive access to those.
1197 */
1198#ifndef CHIPIDEA_EHCI
1da177e4 1199
2b70f073 1200MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4
LT
1201MODULE_AUTHOR (DRIVER_AUTHOR);
1202MODULE_LICENSE ("GPL");
1203
7ff71d6a
MP
1204#ifdef CONFIG_PCI
1205#include "ehci-pci.c"
01cced25 1206#define PCI_DRIVER ehci_pci_driver
7ff71d6a 1207#endif
1da177e4 1208
ba02978a 1209#ifdef CONFIG_USB_EHCI_FSL
80cb9aee 1210#include "ehci-fsl.c"
01cced25 1211#define PLATFORM_DRIVER ehci_fsl_driver
80cb9aee
RV
1212#endif
1213
7e8d5cd9
DM
1214#ifdef CONFIG_USB_EHCI_MXC
1215#include "ehci-mxc.c"
1216#define PLATFORM_DRIVER ehci_mxc_driver
1217#endif
1218
60b0bf0f 1219#ifdef CONFIG_USB_EHCI_SH
63c84552
PM
1220#include "ehci-sh.c"
1221#define PLATFORM_DRIVER ehci_hcd_sh_driver
1222#endif
1223
37663860 1224#ifdef CONFIG_MIPS_ALCHEMY
76fa9a24 1225#include "ehci-au1xxx.c"
01cced25 1226#define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
76fa9a24
JC
1227#endif
1228
7f124f4b 1229#ifdef CONFIG_USB_EHCI_HCD_OMAP
54ab2b02
FB
1230#include "ehci-omap.c"
1231#define PLATFORM_DRIVER ehci_hcd_omap_driver
1232#endif
1233
ad75a410
GL
1234#ifdef CONFIG_PPC_PS3
1235#include "ehci-ps3.c"
7a4eb7fd 1236#define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
ad75a410
GL
1237#endif
1238
da0e8fb0
VB
1239#ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1240#include "ehci-ppc-of.c"
1241#define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
1242#endif
1243
08d3c18e
JZ
1244#ifdef CONFIG_XPS_USB_HCD_XILINX
1245#include "ehci-xilinx-of.c"
1f23b2d9 1246#define XILINX_OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
08d3c18e
JZ
1247#endif
1248
705a7521 1249#ifdef CONFIG_PLAT_ORION
e96ffe2f
TP
1250#include "ehci-orion.c"
1251#define PLATFORM_DRIVER ehci_orion_driver
1252#endif
1253
91bc4d31
VB
1254#ifdef CONFIG_ARCH_IXP4XX
1255#include "ehci-ixp4xx.c"
1256#define PLATFORM_DRIVER ixp4xx_ehci_driver
1257#endif
1258
586dfc8c
WZ
1259#ifdef CONFIG_USB_W90X900_EHCI
1260#include "ehci-w90x900.c"
1261#define PLATFORM_DRIVER ehci_hcd_w90x900_driver
1262#endif
1263
501c9c08
NF
1264#ifdef CONFIG_ARCH_AT91
1265#include "ehci-atmel.c"
1266#define PLATFORM_DRIVER ehci_atmel_driver
1267#endif
1268
1643accd
DD
1269#ifdef CONFIG_USB_OCTEON_EHCI
1270#include "ehci-octeon.c"
1271#define PLATFORM_DRIVER ehci_octeon_driver
1272#endif
1273
760efe69
ML
1274#ifdef CONFIG_USB_CNS3XXX_EHCI
1275#include "ehci-cns3xxx.c"
1276#define PLATFORM_DRIVER cns3xxx_ehci_driver
1277#endif
1278
ad78acaf
AC
1279#ifdef CONFIG_ARCH_VT8500
1280#include "ehci-vt8500.c"
1281#define PLATFORM_DRIVER vt8500_ehci_driver
1282#endif
1283
c8c38de9
DS
1284#ifdef CONFIG_PLAT_SPEAR
1285#include "ehci-spear.c"
1286#define PLATFORM_DRIVER spear_ehci_hcd_driver
1287#endif
1288
b0848aea
PK
1289#ifdef CONFIG_USB_EHCI_MSM
1290#include "ehci-msm.c"
1291#define PLATFORM_DRIVER ehci_msm_driver
1292#endif
1293
22ced687
A
1294#ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
1295#include "ehci-pmcmsp.c"
1296#define PLATFORM_DRIVER ehci_hcd_msp_driver
1297#endif
1298
79ad3b5a
BG
1299#ifdef CONFIG_USB_EHCI_TEGRA
1300#include "ehci-tegra.c"
1301#define PLATFORM_DRIVER tegra_ehci_driver
1302#endif
1303
1bcc5aa8
JS
1304#ifdef CONFIG_USB_EHCI_S5P
1305#include "ehci-s5p.c"
1306#define PLATFORM_DRIVER s5p_ehci_driver
1307#endif
1308
9be03929
JA
1309#ifdef CONFIG_SPARC_LEON
1310#include "ehci-grlib.c"
1311#define PLATFORM_DRIVER ehci_grlib_driver
1312#endif
1313
3af5154a 1314#ifdef CONFIG_CPU_XLR
23106343
J
1315#include "ehci-xls.c"
1316#define PLATFORM_DRIVER ehci_xls_driver
1317#endif
1318
3a082ec9
NZ
1319#ifdef CONFIG_USB_EHCI_MV
1320#include "ehci-mv.c"
1321#define PLATFORM_DRIVER ehci_mv_driver
1322#endif
1323
f30cdbcb
KC
1324#ifdef CONFIG_MACH_LOONGSON1
1325#include "ehci-ls1x.c"
1326#define PLATFORM_DRIVER ehci_ls1x_driver
1327#endif
1328
c256667f
SH
1329#ifdef CONFIG_MIPS_SEAD3
1330#include "ehci-sead3.c"
1331#define PLATFORM_DRIVER ehci_hcd_sead3_driver
1332#endif
1333
7a7a4a59
HM
1334#ifdef CONFIG_USB_EHCI_HCD_PLATFORM
1335#include "ehci-platform.c"
1336#define PLATFORM_DRIVER ehci_platform_driver
1337#endif
1338
ad75a410 1339#if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
1f23b2d9
GL
1340 !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \
1341 !defined(XILINX_OF_PLATFORM_DRIVER)
7ff71d6a
MP
1342#error "missing bus glue for ehci-hcd"
1343#endif
01cced25
KG
1344
1345static int __init ehci_hcd_init(void)
1346{
1347 int retval = 0;
1348
2b70f073
AS
1349 if (usb_disabled())
1350 return -ENODEV;
1351
1352 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
9beeee65
AS
1353 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
1354 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
1355 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
1356 printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
1357 " before uhci_hcd and ohci_hcd, not after\n");
1358
01cced25
KG
1359 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
1360 hcd_name,
1361 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
1362 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
1363
694cc208 1364#ifdef DEBUG
08f4e586 1365 ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
9beeee65
AS
1366 if (!ehci_debug_root) {
1367 retval = -ENOENT;
1368 goto err_debug;
1369 }
694cc208
TJ
1370#endif
1371
01cced25
KG
1372#ifdef PLATFORM_DRIVER
1373 retval = platform_driver_register(&PLATFORM_DRIVER);
da0e8fb0
VB
1374 if (retval < 0)
1375 goto clean0;
01cced25
KG
1376#endif
1377
1378#ifdef PCI_DRIVER
1379 retval = pci_register_driver(&PCI_DRIVER);
da0e8fb0
VB
1380 if (retval < 0)
1381 goto clean1;
ad75a410
GL
1382#endif
1383
1384#ifdef PS3_SYSTEM_BUS_DRIVER
7a4eb7fd 1385 retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
da0e8fb0
VB
1386 if (retval < 0)
1387 goto clean2;
694cc208 1388#endif
da0e8fb0
VB
1389
1390#ifdef OF_PLATFORM_DRIVER
d35fb641 1391 retval = platform_driver_register(&OF_PLATFORM_DRIVER);
da0e8fb0
VB
1392 if (retval < 0)
1393 goto clean3;
1394#endif
1f23b2d9
GL
1395
1396#ifdef XILINX_OF_PLATFORM_DRIVER
d35fb641 1397 retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
1f23b2d9
GL
1398 if (retval < 0)
1399 goto clean4;
1400#endif
da0e8fb0
VB
1401 return retval;
1402
1f23b2d9 1403#ifdef XILINX_OF_PLATFORM_DRIVER
d35fb641 1404 /* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
1f23b2d9
GL
1405clean4:
1406#endif
da0e8fb0 1407#ifdef OF_PLATFORM_DRIVER
d35fb641 1408 platform_driver_unregister(&OF_PLATFORM_DRIVER);
da0e8fb0
VB
1409clean3:
1410#endif
1411#ifdef PS3_SYSTEM_BUS_DRIVER
1412 ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1413clean2:
ad75a410
GL
1414#endif
1415#ifdef PCI_DRIVER
da0e8fb0
VB
1416 pci_unregister_driver(&PCI_DRIVER);
1417clean1:
ad75a410 1418#endif
da0e8fb0
VB
1419#ifdef PLATFORM_DRIVER
1420 platform_driver_unregister(&PLATFORM_DRIVER);
1421clean0:
1422#endif
1423#ifdef DEBUG
1424 debugfs_remove(ehci_debug_root);
1425 ehci_debug_root = NULL;
9beeee65 1426err_debug:
a9b6148d 1427#endif
9beeee65 1428 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
01cced25
KG
1429 return retval;
1430}
1431module_init(ehci_hcd_init);
1432
1433static void __exit ehci_hcd_cleanup(void)
1434{
1f23b2d9 1435#ifdef XILINX_OF_PLATFORM_DRIVER
d35fb641 1436 platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
1f23b2d9 1437#endif
da0e8fb0 1438#ifdef OF_PLATFORM_DRIVER
d35fb641 1439 platform_driver_unregister(&OF_PLATFORM_DRIVER);
da0e8fb0 1440#endif
01cced25
KG
1441#ifdef PLATFORM_DRIVER
1442 platform_driver_unregister(&PLATFORM_DRIVER);
1443#endif
1444#ifdef PCI_DRIVER
1445 pci_unregister_driver(&PCI_DRIVER);
1446#endif
ad75a410 1447#ifdef PS3_SYSTEM_BUS_DRIVER
7a4eb7fd 1448 ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
ad75a410 1449#endif
694cc208
TJ
1450#ifdef DEBUG
1451 debugfs_remove(ehci_debug_root);
1452#endif
9beeee65 1453 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
01cced25
KG
1454}
1455module_exit(ehci_hcd_cleanup);
1456
eb70e5ab 1457#endif /* CHIPIDEA_EHCI */
This page took 0.915508 seconds and 5 git commands to generate.