ehci-hcd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Copyright (c) 2000-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/dmapool.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/ioport.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/timer.h>
  30. #include <linux/list.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/reboot.h>
  33. #include <linux/usb.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/dma-mapping.h>
  36. #include "../core/hcd.h"
  37. #include <asm/byteorder.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/unaligned.h>
  42. /*-------------------------------------------------------------------------*/
  43. /*
  44. * EHCI hc_driver implementation ... experimental, incomplete.
  45. * Based on the final 1.0 register interface specification.
  46. *
  47. * USB 2.0 shows up in upcoming www.pcmcia.org technology.
  48. * First was PCMCIA, like ISA; then CardBus, which is PCI.
  49. * Next comes "CardBay", using USB 2.0 signals.
  50. *
  51. * Contains additional contributions by Brad Hards, Rory Bolt, and others.
  52. * Special thanks to Intel and VIA for providing host controllers to
  53. * test this driver on, and Cypress (including In-System Design) for
  54. * providing early devices for those host controllers to talk to!
  55. *
  56. * HISTORY:
  57. *
  58. * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
  59. * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
  60. * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
  61. * <sojkam@centrum.cz>, updates by DB).
  62. *
  63. * 2002-11-29 Correct handling for hw async_next register.
  64. * 2002-08-06 Handling for bulk and interrupt transfers is mostly shared;
  65. * only scheduling is different, no arbitrary limitations.
  66. * 2002-07-25 Sanity check PCI reads, mostly for better cardbus support,
  67. * clean up HC run state handshaking.
  68. * 2002-05-24 Preliminary FS/LS interrupts, using scheduling shortcuts
  69. * 2002-05-11 Clear TT errors for FS/LS ctrl/bulk. Fill in some other
  70. * missing pieces: enabling 64bit dma, handoff from BIOS/SMM.
  71. * 2002-05-07 Some error path cleanups to report better errors; wmb();
  72. * use non-CVS version id; better iso bandwidth claim.
  73. * 2002-04-19 Control/bulk/interrupt submit no longer uses giveback() on
  74. * errors in submit path. Bugfixes to interrupt scheduling/processing.
  75. * 2002-03-05 Initial high-speed ISO support; reduce ITD memory; shift
  76. * more checking to generic hcd framework (db). Make it work with
  77. * Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
  78. * 2002-01-14 Minor cleanup; version synch.
  79. * 2002-01-08 Fix roothub handoff of FS/LS to companion controllers.
  80. * 2002-01-04 Control/Bulk queuing behaves.
  81. *
  82. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  83. * 2001-June Works with usb-storage and NEC EHCI on 2.4
  84. */
  85. #define DRIVER_VERSION "10 Dec 2004"
  86. #define DRIVER_AUTHOR "David Brownell"
  87. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  88. static const char hcd_name [] = "ehci_hcd";
  89. #undef EHCI_VERBOSE_DEBUG
  90. #undef EHCI_URB_TRACE
  91. #ifdef DEBUG
  92. #define EHCI_STATS
  93. #endif
  94. /* magic numbers that can affect system performance */
  95. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  96. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  97. #define EHCI_TUNE_RL_TT 0
  98. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  99. #define EHCI_TUNE_MULT_TT 1
  100. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  101. #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
  102. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  103. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  104. #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
  105. /* Initial IRQ latency: faster than hw default */
  106. static int log2_irq_thresh = 0; // 0 to 6
  107. module_param (log2_irq_thresh, int, S_IRUGO);
  108. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  109. /* initial park setting: slower than hw default */
  110. static unsigned park = 0;
  111. module_param (park, uint, S_IRUGO);
  112. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  113. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  114. /*-------------------------------------------------------------------------*/
  115. #include "ehci.h"
  116. #include "ehci-dbg.c"
  117. /*-------------------------------------------------------------------------*/
  118. /*
  119. * handshake - spin reading hc until handshake completes or fails
  120. * @ptr: address of hc register to be read
  121. * @mask: bits to look at in result of read
  122. * @done: value of those bits when handshake succeeds
  123. * @usec: timeout in microseconds
  124. *
  125. * Returns negative errno, or zero on success
  126. *
  127. * Success happens when the "mask" bits have the specified value (hardware
  128. * handshake done). There are two failure modes: "usec" have passed (major
  129. * hardware flakeout), or the register reads as all-ones (hardware removed).
  130. *
  131. * That last failure should_only happen in cases like physical cardbus eject
  132. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  133. * bridge shutdown: shutting down the bridge before the devices using it.
  134. */
  135. static int handshake (void __iomem *ptr, u32 mask, u32 done, int usec)
  136. {
  137. u32 result;
  138. do {
  139. result = readl (ptr);
  140. if (result == ~(u32)0) /* card removed */
  141. return -ENODEV;
  142. result &= mask;
  143. if (result == done)
  144. return 0;
  145. udelay (1);
  146. usec--;
  147. } while (usec > 0);
  148. return -ETIMEDOUT;
  149. }
  150. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  151. static int ehci_halt (struct ehci_hcd *ehci)
  152. {
  153. u32 temp = readl (&ehci->regs->status);
  154. /* disable any irqs left enabled by previous code */
  155. writel (0, &ehci->regs->intr_enable);
  156. if ((temp & STS_HALT) != 0)
  157. return 0;
  158. temp = readl (&ehci->regs->command);
  159. temp &= ~CMD_RUN;
  160. writel (temp, &ehci->regs->command);
  161. return handshake (&ehci->regs->status, STS_HALT, STS_HALT, 16 * 125);
  162. }
  163. /* put TDI/ARC silicon into EHCI mode */
  164. static void tdi_reset (struct ehci_hcd *ehci)
  165. {
  166. u32 __iomem *reg_ptr;
  167. u32 tmp;
  168. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + 0x68);
  169. tmp = readl (reg_ptr);
  170. tmp |= 0x3;
  171. writel (tmp, reg_ptr);
  172. }
  173. /* reset a non-running (STS_HALT == 1) controller */
  174. static int ehci_reset (struct ehci_hcd *ehci)
  175. {
  176. int retval;
  177. u32 command = readl (&ehci->regs->command);
  178. command |= CMD_RESET;
  179. dbg_cmd (ehci, "reset", command);
  180. writel (command, &ehci->regs->command);
  181. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  182. ehci->next_statechange = jiffies;
  183. retval = handshake (&ehci->regs->command, CMD_RESET, 0, 250 * 1000);
  184. if (retval)
  185. return retval;
  186. if (ehci_is_TDI(ehci))
  187. tdi_reset (ehci);
  188. return retval;
  189. }
  190. /* idle the controller (from running) */
  191. static void ehci_quiesce (struct ehci_hcd *ehci)
  192. {
  193. u32 temp;
  194. #ifdef DEBUG
  195. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  196. BUG ();
  197. #endif
  198. /* wait for any schedule enables/disables to take effect */
  199. temp = readl (&ehci->regs->command) << 10;
  200. temp &= STS_ASS | STS_PSS;
  201. if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
  202. temp, 16 * 125) != 0) {
  203. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  204. return;
  205. }
  206. /* then disable anything that's still active */
  207. temp = readl (&ehci->regs->command);
  208. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  209. writel (temp, &ehci->regs->command);
  210. /* hardware can take 16 microframes to turn off ... */
  211. if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
  212. 0, 16 * 125) != 0) {
  213. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  214. return;
  215. }
  216. }
  217. /*-------------------------------------------------------------------------*/
  218. static void ehci_work(struct ehci_hcd *ehci, struct pt_regs *regs);
  219. #include "ehci-hub.c"
  220. #include "ehci-mem.c"
  221. #include "ehci-q.c"
  222. #include "ehci-sched.c"
  223. /*-------------------------------------------------------------------------*/
  224. static void ehci_watchdog (unsigned long param)
  225. {
  226. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  227. unsigned long flags;
  228. spin_lock_irqsave (&ehci->lock, flags);
  229. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  230. if (ehci->reclaim) {
  231. u32 status = readl (&ehci->regs->status);
  232. if (status & STS_IAA) {
  233. ehci_vdbg (ehci, "lost IAA\n");
  234. COUNT (ehci->stats.lost_iaa);
  235. writel (STS_IAA, &ehci->regs->status);
  236. ehci->reclaim_ready = 1;
  237. }
  238. }
  239. /* stop async processing after it's idled a bit */
  240. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  241. start_unlink_async (ehci, ehci->async);
  242. /* ehci could run by timer, without IRQs ... */
  243. ehci_work (ehci, NULL);
  244. spin_unlock_irqrestore (&ehci->lock, flags);
  245. }
  246. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  247. * This forcibly disables dma and IRQs, helping kexec and other cases
  248. * where the next system software may expect clean state.
  249. */
  250. static void
  251. ehci_shutdown (struct usb_hcd *hcd)
  252. {
  253. struct ehci_hcd *ehci;
  254. ehci = hcd_to_ehci (hcd);
  255. (void) ehci_halt (ehci);
  256. /* make BIOS/etc use companion controller during reboot */
  257. writel (0, &ehci->regs->configured_flag);
  258. }
  259. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  260. {
  261. unsigned port;
  262. if (!HCS_PPC (ehci->hcs_params))
  263. return;
  264. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  265. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  266. (void) ehci_hub_control(ehci_to_hcd(ehci),
  267. is_on ? SetPortFeature : ClearPortFeature,
  268. USB_PORT_FEAT_POWER,
  269. port--, NULL, 0);
  270. msleep(20);
  271. }
  272. /*-------------------------------------------------------------------------*/
  273. /*
  274. * ehci_work is called from some interrupts, timers, and so on.
  275. * it calls driver completion functions, after dropping ehci->lock.
  276. */
  277. static void ehci_work (struct ehci_hcd *ehci, struct pt_regs *regs)
  278. {
  279. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  280. if (ehci->reclaim_ready)
  281. end_unlink_async (ehci, regs);
  282. /* another CPU may drop ehci->lock during a schedule scan while
  283. * it reports urb completions. this flag guards against bogus
  284. * attempts at re-entrant schedule scanning.
  285. */
  286. if (ehci->scanning)
  287. return;
  288. ehci->scanning = 1;
  289. scan_async (ehci, regs);
  290. if (ehci->next_uframe != -1)
  291. scan_periodic (ehci, regs);
  292. ehci->scanning = 0;
  293. /* the IO watchdog guards against hardware or driver bugs that
  294. * misplace IRQs, and should let us run completely without IRQs.
  295. * such lossage has been observed on both VT6202 and VT8235.
  296. */
  297. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  298. (ehci->async->qh_next.ptr != NULL ||
  299. ehci->periodic_sched != 0))
  300. timer_action (ehci, TIMER_IO_WATCHDOG);
  301. }
  302. static void ehci_stop (struct usb_hcd *hcd)
  303. {
  304. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  305. ehci_dbg (ehci, "stop\n");
  306. /* Turn off port power on all root hub ports. */
  307. ehci_port_power (ehci, 0);
  308. /* no more interrupts ... */
  309. del_timer_sync (&ehci->watchdog);
  310. spin_lock_irq(&ehci->lock);
  311. if (HC_IS_RUNNING (hcd->state))
  312. ehci_quiesce (ehci);
  313. ehci_reset (ehci);
  314. writel (0, &ehci->regs->intr_enable);
  315. spin_unlock_irq(&ehci->lock);
  316. /* let companion controllers work when we aren't */
  317. writel (0, &ehci->regs->configured_flag);
  318. remove_debug_files (ehci);
  319. /* root hub is shut down separately (first, when possible) */
  320. spin_lock_irq (&ehci->lock);
  321. if (ehci->async)
  322. ehci_work (ehci, NULL);
  323. spin_unlock_irq (&ehci->lock);
  324. ehci_mem_cleanup (ehci);
  325. #ifdef EHCI_STATS
  326. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  327. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  328. ehci->stats.lost_iaa);
  329. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  330. ehci->stats.complete, ehci->stats.unlink);
  331. #endif
  332. dbg_status (ehci, "ehci_stop completed", readl (&ehci->regs->status));
  333. }
  334. /* one-time init, only for memory state */
  335. static int ehci_init(struct usb_hcd *hcd)
  336. {
  337. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  338. u32 temp;
  339. int retval;
  340. u32 hcc_params;
  341. spin_lock_init(&ehci->lock);
  342. init_timer(&ehci->watchdog);
  343. ehci->watchdog.function = ehci_watchdog;
  344. ehci->watchdog.data = (unsigned long) ehci;
  345. /*
  346. * hw default: 1K periodic list heads, one per frame.
  347. * periodic_size can shrink by USBCMD update if hcc_params allows.
  348. */
  349. ehci->periodic_size = DEFAULT_I_TDPS;
  350. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  351. return retval;
  352. /* controllers may cache some of the periodic schedule ... */
  353. hcc_params = readl(&ehci->caps->hcc_params);
  354. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  355. ehci->i_thresh = 8;
  356. else // N microframes cached
  357. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  358. ehci->reclaim = NULL;
  359. ehci->reclaim_ready = 0;
  360. ehci->next_uframe = -1;
  361. /*
  362. * dedicate a qh for the async ring head, since we couldn't unlink
  363. * a 'real' qh without stopping the async schedule [4.8]. use it
  364. * as the 'reclamation list head' too.
  365. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  366. * from automatically advancing to the next td after short reads.
  367. */
  368. ehci->async->qh_next.qh = NULL;
  369. ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
  370. ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
  371. ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  372. ehci->async->hw_qtd_next = EHCI_LIST_END;
  373. ehci->async->qh_state = QH_STATE_LINKED;
  374. ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
  375. /* clear interrupt enables, set irq latency */
  376. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  377. log2_irq_thresh = 0;
  378. temp = 1 << (16 + log2_irq_thresh);
  379. if (HCC_CANPARK(hcc_params)) {
  380. /* HW default park == 3, on hardware that supports it (like
  381. * NVidia and ALI silicon), maximizes throughput on the async
  382. * schedule by avoiding QH fetches between transfers.
  383. *
  384. * With fast usb storage devices and NForce2, "park" seems to
  385. * make problems: throughput reduction (!), data errors...
  386. */
  387. if (park) {
  388. park = min(park, (unsigned) 3);
  389. temp |= CMD_PARK;
  390. temp |= park << 8;
  391. }
  392. ehci_dbg(ehci, "park %d\n", park);
  393. }
  394. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  395. /* periodic schedule size can be smaller than default */
  396. temp &= ~(3 << 2);
  397. temp |= (EHCI_TUNE_FLS << 2);
  398. switch (EHCI_TUNE_FLS) {
  399. case 0: ehci->periodic_size = 1024; break;
  400. case 1: ehci->periodic_size = 512; break;
  401. case 2: ehci->periodic_size = 256; break;
  402. default: BUG();
  403. }
  404. }
  405. ehci->command = temp;
  406. return 0;
  407. }
  408. /* start HC running; it's halted, ehci_init() has been run (once) */
  409. static int ehci_run (struct usb_hcd *hcd)
  410. {
  411. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  412. int retval;
  413. u32 temp;
  414. u32 hcc_params;
  415. /* EHCI spec section 4.1 */
  416. if ((retval = ehci_reset(ehci)) != 0) {
  417. ehci_mem_cleanup(ehci);
  418. return retval;
  419. }
  420. writel(ehci->periodic_dma, &ehci->regs->frame_list);
  421. writel((u32)ehci->async->qh_dma, &ehci->regs->async_next);
  422. /*
  423. * hcc_params controls whether ehci->regs->segment must (!!!)
  424. * be used; it constrains QH/ITD/SITD and QTD locations.
  425. * pci_pool consistent memory always uses segment zero.
  426. * streaming mappings for I/O buffers, like pci_map_single(),
  427. * can return segments above 4GB, if the device allows.
  428. *
  429. * NOTE: the dma mask is visible through dma_supported(), so
  430. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  431. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  432. * host side drivers though.
  433. */
  434. hcc_params = readl(&ehci->caps->hcc_params);
  435. if (HCC_64BIT_ADDR(hcc_params)) {
  436. writel(0, &ehci->regs->segment);
  437. #if 0
  438. // this is deeply broken on almost all architectures
  439. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  440. ehci_info(ehci, "enabled 64bit DMA\n");
  441. #endif
  442. }
  443. // Philips, Intel, and maybe others need CMD_RUN before the
  444. // root hub will detect new devices (why?); NEC doesn't
  445. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  446. ehci->command |= CMD_RUN;
  447. writel (ehci->command, &ehci->regs->command);
  448. dbg_cmd (ehci, "init", ehci->command);
  449. /*
  450. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  451. * are explicitly handed to companion controller(s), so no TT is
  452. * involved with the root hub. (Except where one is integrated,
  453. * and there's no companion controller unless maybe for USB OTG.)
  454. */
  455. hcd->state = HC_STATE_RUNNING;
  456. writel (FLAG_CF, &ehci->regs->configured_flag);
  457. readl (&ehci->regs->command); /* unblock posted writes */
  458. temp = HC_VERSION(readl (&ehci->caps->hc_capbase));
  459. ehci_info (ehci,
  460. "USB %x.%x started, EHCI %x.%02x, driver %s\n",
  461. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  462. temp >> 8, temp & 0xff, DRIVER_VERSION);
  463. writel (INTR_MASK, &ehci->regs->intr_enable); /* Turn On Interrupts */
  464. /* GRR this is run-once init(), being done every time the HC starts.
  465. * So long as they're part of class devices, we can't do it init()
  466. * since the class device isn't created that early.
  467. */
  468. create_debug_files(ehci);
  469. return 0;
  470. }
  471. /*-------------------------------------------------------------------------*/
  472. static irqreturn_t ehci_irq (struct usb_hcd *hcd, struct pt_regs *regs)
  473. {
  474. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  475. u32 status;
  476. int bh;
  477. spin_lock (&ehci->lock);
  478. status = readl (&ehci->regs->status);
  479. /* e.g. cardbus physical eject */
  480. if (status == ~(u32) 0) {
  481. ehci_dbg (ehci, "device removed\n");
  482. goto dead;
  483. }
  484. status &= INTR_MASK;
  485. if (!status) { /* irq sharing? */
  486. spin_unlock(&ehci->lock);
  487. return IRQ_NONE;
  488. }
  489. /* clear (just) interrupts */
  490. writel (status, &ehci->regs->status);
  491. readl (&ehci->regs->command); /* unblock posted write */
  492. bh = 0;
  493. #ifdef EHCI_VERBOSE_DEBUG
  494. /* unrequested/ignored: Frame List Rollover */
  495. dbg_status (ehci, "irq", status);
  496. #endif
  497. /* INT, ERR, and IAA interrupt rates can be throttled */
  498. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  499. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  500. if (likely ((status & STS_ERR) == 0))
  501. COUNT (ehci->stats.normal);
  502. else
  503. COUNT (ehci->stats.error);
  504. bh = 1;
  505. }
  506. /* complete the unlinking of some qh [4.15.2.3] */
  507. if (status & STS_IAA) {
  508. COUNT (ehci->stats.reclaim);
  509. ehci->reclaim_ready = 1;
  510. bh = 1;
  511. }
  512. /* remote wakeup [4.3.1] */
  513. if (status & STS_PCD) {
  514. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  515. /* resume root hub? */
  516. status = readl (&ehci->regs->command);
  517. if (!(status & CMD_RUN))
  518. writel (status | CMD_RUN, &ehci->regs->command);
  519. while (i--) {
  520. int pstatus = readl (&ehci->regs->port_status [i]);
  521. if (pstatus & PORT_OWNER)
  522. continue;
  523. if (!(pstatus & PORT_RESUME)
  524. || ehci->reset_done [i] != 0)
  525. continue;
  526. /* start 20 msec resume signaling from this port,
  527. * and make khubd collect PORT_STAT_C_SUSPEND to
  528. * stop that signaling.
  529. */
  530. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  531. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  532. usb_hcd_resume_root_hub(hcd);
  533. }
  534. }
  535. /* PCI errors [4.15.2.4] */
  536. if (unlikely ((status & STS_FATAL) != 0)) {
  537. /* bogus "fatal" IRQs appear on some chips... why? */
  538. status = readl (&ehci->regs->status);
  539. dbg_cmd (ehci, "fatal", readl (&ehci->regs->command));
  540. dbg_status (ehci, "fatal", status);
  541. if (status & STS_HALT) {
  542. ehci_err (ehci, "fatal error\n");
  543. dead:
  544. ehci_reset (ehci);
  545. writel (0, &ehci->regs->configured_flag);
  546. /* generic layer kills/unlinks all urbs, then
  547. * uses ehci_stop to clean up the rest
  548. */
  549. bh = 1;
  550. }
  551. }
  552. if (bh)
  553. ehci_work (ehci, regs);
  554. spin_unlock (&ehci->lock);
  555. return IRQ_HANDLED;
  556. }
  557. /*-------------------------------------------------------------------------*/
  558. /*
  559. * non-error returns are a promise to giveback() the urb later
  560. * we drop ownership so next owner (or urb unlink) can get it
  561. *
  562. * urb + dev is in hcd.self.controller.urb_list
  563. * we're queueing TDs onto software and hardware lists
  564. *
  565. * hcd-specific init for hcpriv hasn't been done yet
  566. *
  567. * NOTE: control, bulk, and interrupt share the same code to append TDs
  568. * to a (possibly active) QH, and the same QH scanning code.
  569. */
  570. static int ehci_urb_enqueue (
  571. struct usb_hcd *hcd,
  572. struct usb_host_endpoint *ep,
  573. struct urb *urb,
  574. gfp_t mem_flags
  575. ) {
  576. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  577. struct list_head qtd_list;
  578. INIT_LIST_HEAD (&qtd_list);
  579. switch (usb_pipetype (urb->pipe)) {
  580. // case PIPE_CONTROL:
  581. // case PIPE_BULK:
  582. default:
  583. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  584. return -ENOMEM;
  585. return submit_async (ehci, ep, urb, &qtd_list, mem_flags);
  586. case PIPE_INTERRUPT:
  587. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  588. return -ENOMEM;
  589. return intr_submit (ehci, ep, urb, &qtd_list, mem_flags);
  590. case PIPE_ISOCHRONOUS:
  591. if (urb->dev->speed == USB_SPEED_HIGH)
  592. return itd_submit (ehci, urb, mem_flags);
  593. else
  594. return sitd_submit (ehci, urb, mem_flags);
  595. }
  596. }
  597. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  598. {
  599. /* if we need to use IAA and it's busy, defer */
  600. if (qh->qh_state == QH_STATE_LINKED
  601. && ehci->reclaim
  602. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) {
  603. struct ehci_qh *last;
  604. for (last = ehci->reclaim;
  605. last->reclaim;
  606. last = last->reclaim)
  607. continue;
  608. qh->qh_state = QH_STATE_UNLINK_WAIT;
  609. last->reclaim = qh;
  610. /* bypass IAA if the hc can't care */
  611. } else if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && ehci->reclaim)
  612. end_unlink_async (ehci, NULL);
  613. /* something else might have unlinked the qh by now */
  614. if (qh->qh_state == QH_STATE_LINKED)
  615. start_unlink_async (ehci, qh);
  616. }
  617. /* remove from hardware lists
  618. * completions normally happen asynchronously
  619. */
  620. static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  621. {
  622. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  623. struct ehci_qh *qh;
  624. unsigned long flags;
  625. spin_lock_irqsave (&ehci->lock, flags);
  626. switch (usb_pipetype (urb->pipe)) {
  627. // case PIPE_CONTROL:
  628. // case PIPE_BULK:
  629. default:
  630. qh = (struct ehci_qh *) urb->hcpriv;
  631. if (!qh)
  632. break;
  633. unlink_async (ehci, qh);
  634. break;
  635. case PIPE_INTERRUPT:
  636. qh = (struct ehci_qh *) urb->hcpriv;
  637. if (!qh)
  638. break;
  639. switch (qh->qh_state) {
  640. case QH_STATE_LINKED:
  641. intr_deschedule (ehci, qh);
  642. /* FALL THROUGH */
  643. case QH_STATE_IDLE:
  644. qh_completions (ehci, qh, NULL);
  645. break;
  646. default:
  647. ehci_dbg (ehci, "bogus qh %p state %d\n",
  648. qh, qh->qh_state);
  649. goto done;
  650. }
  651. /* reschedule QH iff another request is queued */
  652. if (!list_empty (&qh->qtd_list)
  653. && HC_IS_RUNNING (hcd->state)) {
  654. int status;
  655. status = qh_schedule (ehci, qh);
  656. spin_unlock_irqrestore (&ehci->lock, flags);
  657. if (status != 0) {
  658. // shouldn't happen often, but ...
  659. // FIXME kill those tds' urbs
  660. err ("can't reschedule qh %p, err %d",
  661. qh, status);
  662. }
  663. return status;
  664. }
  665. break;
  666. case PIPE_ISOCHRONOUS:
  667. // itd or sitd ...
  668. // wait till next completion, do it then.
  669. // completion irqs can wait up to 1024 msec,
  670. break;
  671. }
  672. done:
  673. spin_unlock_irqrestore (&ehci->lock, flags);
  674. return 0;
  675. }
  676. /*-------------------------------------------------------------------------*/
  677. // bulk qh holds the data toggle
  678. static void
  679. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  680. {
  681. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  682. unsigned long flags;
  683. struct ehci_qh *qh, *tmp;
  684. /* ASSERT: any requests/urbs are being unlinked */
  685. /* ASSERT: nobody can be submitting urbs for this any more */
  686. rescan:
  687. spin_lock_irqsave (&ehci->lock, flags);
  688. qh = ep->hcpriv;
  689. if (!qh)
  690. goto done;
  691. /* endpoints can be iso streams. for now, we don't
  692. * accelerate iso completions ... so spin a while.
  693. */
  694. if (qh->hw_info1 == 0) {
  695. ehci_vdbg (ehci, "iso delay\n");
  696. goto idle_timeout;
  697. }
  698. if (!HC_IS_RUNNING (hcd->state))
  699. qh->qh_state = QH_STATE_IDLE;
  700. switch (qh->qh_state) {
  701. case QH_STATE_LINKED:
  702. for (tmp = ehci->async->qh_next.qh;
  703. tmp && tmp != qh;
  704. tmp = tmp->qh_next.qh)
  705. continue;
  706. /* periodic qh self-unlinks on empty */
  707. if (!tmp)
  708. goto nogood;
  709. unlink_async (ehci, qh);
  710. /* FALL THROUGH */
  711. case QH_STATE_UNLINK: /* wait for hw to finish? */
  712. idle_timeout:
  713. spin_unlock_irqrestore (&ehci->lock, flags);
  714. schedule_timeout_uninterruptible(1);
  715. goto rescan;
  716. case QH_STATE_IDLE: /* fully unlinked */
  717. if (list_empty (&qh->qtd_list)) {
  718. qh_put (qh);
  719. break;
  720. }
  721. /* else FALL THROUGH */
  722. default:
  723. nogood:
  724. /* caller was supposed to have unlinked any requests;
  725. * that's not our job. just leak this memory.
  726. */
  727. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  728. qh, ep->desc.bEndpointAddress, qh->qh_state,
  729. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  730. break;
  731. }
  732. ep->hcpriv = NULL;
  733. done:
  734. spin_unlock_irqrestore (&ehci->lock, flags);
  735. return;
  736. }
  737. static int ehci_get_frame (struct usb_hcd *hcd)
  738. {
  739. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  740. return (readl (&ehci->regs->frame_index) >> 3) % ehci->periodic_size;
  741. }
  742. /*-------------------------------------------------------------------------*/
  743. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  744. MODULE_DESCRIPTION (DRIVER_INFO);
  745. MODULE_AUTHOR (DRIVER_AUTHOR);
  746. MODULE_LICENSE ("GPL");
  747. #ifdef CONFIG_PCI
  748. #include "ehci-pci.c"
  749. #define PCI_DRIVER ehci_pci_driver
  750. #endif
  751. #ifdef CONFIG_MPC834x
  752. #include "ehci-fsl.c"
  753. #define PLATFORM_DRIVER ehci_fsl_driver
  754. #endif
  755. #ifdef CONFIG_SOC_AU1200
  756. #include "ehci-au1xxx.c"
  757. #define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
  758. #endif
  759. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER)
  760. #error "missing bus glue for ehci-hcd"
  761. #endif
  762. static int __init ehci_hcd_init(void)
  763. {
  764. int retval = 0;
  765. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  766. hcd_name,
  767. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  768. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  769. #ifdef PLATFORM_DRIVER
  770. retval = platform_driver_register(&PLATFORM_DRIVER);
  771. if (retval < 0)
  772. return retval;
  773. #endif
  774. #ifdef PCI_DRIVER
  775. retval = pci_register_driver(&PCI_DRIVER);
  776. if (retval < 0) {
  777. #ifdef PLATFORM_DRIVER
  778. platform_driver_unregister(&PLATFORM_DRIVER);
  779. #endif
  780. }
  781. #endif
  782. return retval;
  783. }
  784. module_init(ehci_hcd_init);
  785. static void __exit ehci_hcd_cleanup(void)
  786. {
  787. #ifdef PLATFORM_DRIVER
  788. platform_driver_unregister(&PLATFORM_DRIVER);
  789. #endif
  790. #ifdef PCI_DRIVER
  791. pci_unregister_driver(&PCI_DRIVER);
  792. #endif
  793. }
  794. module_exit(ehci_hcd_cleanup);