ehci-hcd.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/timer.h>
  29. #include <linux/list.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/reboot.h>
  32. #include <linux/usb.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/debugfs.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_MSECS 10 /* arbitrary */
  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. /* for flakey hardware, ignore overcurrent indicators */
  114. static int ignore_oc = 0;
  115. module_param (ignore_oc, bool, S_IRUGO);
  116. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  117. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  118. /*-------------------------------------------------------------------------*/
  119. #include "ehci.h"
  120. #include "ehci-dbg.c"
  121. /*-------------------------------------------------------------------------*/
  122. /*
  123. * handshake - spin reading hc until handshake completes or fails
  124. * @ptr: address of hc register to be read
  125. * @mask: bits to look at in result of read
  126. * @done: value of those bits when handshake succeeds
  127. * @usec: timeout in microseconds
  128. *
  129. * Returns negative errno, or zero on success
  130. *
  131. * Success happens when the "mask" bits have the specified value (hardware
  132. * handshake done). There are two failure modes: "usec" have passed (major
  133. * hardware flakeout), or the register reads as all-ones (hardware removed).
  134. *
  135. * That last failure should_only happen in cases like physical cardbus eject
  136. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  137. * bridge shutdown: shutting down the bridge before the devices using it.
  138. */
  139. static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
  140. u32 mask, u32 done, int usec)
  141. {
  142. u32 result;
  143. do {
  144. result = ehci_readl(ehci, ptr);
  145. if (result == ~(u32)0) /* card removed */
  146. return -ENODEV;
  147. result &= mask;
  148. if (result == done)
  149. return 0;
  150. udelay (1);
  151. usec--;
  152. } while (usec > 0);
  153. return -ETIMEDOUT;
  154. }
  155. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  156. static int ehci_halt (struct ehci_hcd *ehci)
  157. {
  158. u32 temp = ehci_readl(ehci, &ehci->regs->status);
  159. /* disable any irqs left enabled by previous code */
  160. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  161. if ((temp & STS_HALT) != 0)
  162. return 0;
  163. temp = ehci_readl(ehci, &ehci->regs->command);
  164. temp &= ~CMD_RUN;
  165. ehci_writel(ehci, temp, &ehci->regs->command);
  166. return handshake (ehci, &ehci->regs->status,
  167. STS_HALT, STS_HALT, 16 * 125);
  168. }
  169. /* put TDI/ARC silicon into EHCI mode */
  170. static void tdi_reset (struct ehci_hcd *ehci)
  171. {
  172. u32 __iomem *reg_ptr;
  173. u32 tmp;
  174. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + USBMODE);
  175. tmp = ehci_readl(ehci, reg_ptr);
  176. tmp |= USBMODE_CM_HC;
  177. /* The default byte access to MMR space is LE after
  178. * controller reset. Set the required endian mode
  179. * for transfer buffers to match the host microprocessor
  180. */
  181. if (ehci_big_endian_mmio(ehci))
  182. tmp |= USBMODE_BE;
  183. ehci_writel(ehci, tmp, reg_ptr);
  184. }
  185. /* reset a non-running (STS_HALT == 1) controller */
  186. static int ehci_reset (struct ehci_hcd *ehci)
  187. {
  188. int retval;
  189. u32 command = ehci_readl(ehci, &ehci->regs->command);
  190. command |= CMD_RESET;
  191. dbg_cmd (ehci, "reset", command);
  192. ehci_writel(ehci, command, &ehci->regs->command);
  193. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  194. ehci->next_statechange = jiffies;
  195. retval = handshake (ehci, &ehci->regs->command,
  196. CMD_RESET, 0, 250 * 1000);
  197. if (retval)
  198. return retval;
  199. if (ehci_is_TDI(ehci))
  200. tdi_reset (ehci);
  201. return retval;
  202. }
  203. /* idle the controller (from running) */
  204. static void ehci_quiesce (struct ehci_hcd *ehci)
  205. {
  206. u32 temp;
  207. #ifdef DEBUG
  208. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  209. BUG ();
  210. #endif
  211. /* wait for any schedule enables/disables to take effect */
  212. temp = ehci_readl(ehci, &ehci->regs->command) << 10;
  213. temp &= STS_ASS | STS_PSS;
  214. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  215. temp, 16 * 125) != 0) {
  216. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  217. return;
  218. }
  219. /* then disable anything that's still active */
  220. temp = ehci_readl(ehci, &ehci->regs->command);
  221. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  222. ehci_writel(ehci, temp, &ehci->regs->command);
  223. /* hardware can take 16 microframes to turn off ... */
  224. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  225. 0, 16 * 125) != 0) {
  226. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  227. return;
  228. }
  229. }
  230. /*-------------------------------------------------------------------------*/
  231. static void end_unlink_async(struct ehci_hcd *ehci);
  232. static void ehci_work(struct ehci_hcd *ehci);
  233. #include "ehci-hub.c"
  234. #include "ehci-mem.c"
  235. #include "ehci-q.c"
  236. #include "ehci-sched.c"
  237. /*-------------------------------------------------------------------------*/
  238. static void ehci_iaa_watchdog(unsigned long param)
  239. {
  240. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  241. unsigned long flags;
  242. spin_lock_irqsave (&ehci->lock, flags);
  243. /* Lost IAA irqs wedge things badly; seen first with a vt8235.
  244. * So we need this watchdog, but must protect it against both
  245. * (a) SMP races against real IAA firing and retriggering, and
  246. * (b) clean HC shutdown, when IAA watchdog was pending.
  247. */
  248. if (ehci->reclaim
  249. && !timer_pending(&ehci->iaa_watchdog)
  250. && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
  251. u32 cmd, status;
  252. /* If we get here, IAA is *REALLY* late. It's barely
  253. * conceivable that the system is so busy that CMD_IAAD
  254. * is still legitimately set, so let's be sure it's
  255. * clear before we read STS_IAA. (The HC should clear
  256. * CMD_IAAD when it sets STS_IAA.)
  257. */
  258. cmd = ehci_readl(ehci, &ehci->regs->command);
  259. if (cmd & CMD_IAAD)
  260. ehci_writel(ehci, cmd & ~CMD_IAAD,
  261. &ehci->regs->command);
  262. /* If IAA is set here it either legitimately triggered
  263. * before we cleared IAAD above (but _way_ late, so we'll
  264. * still count it as lost) ... or a silicon erratum:
  265. * - VIA seems to set IAA without triggering the IRQ;
  266. * - IAAD potentially cleared without setting IAA.
  267. */
  268. status = ehci_readl(ehci, &ehci->regs->status);
  269. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  270. COUNT (ehci->stats.lost_iaa);
  271. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  272. }
  273. ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
  274. status, cmd);
  275. end_unlink_async(ehci);
  276. }
  277. spin_unlock_irqrestore(&ehci->lock, flags);
  278. }
  279. static void ehci_watchdog(unsigned long param)
  280. {
  281. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  282. unsigned long flags;
  283. spin_lock_irqsave(&ehci->lock, flags);
  284. /* stop async processing after it's idled a bit */
  285. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  286. start_unlink_async (ehci, ehci->async);
  287. /* ehci could run by timer, without IRQs ... */
  288. ehci_work (ehci);
  289. spin_unlock_irqrestore (&ehci->lock, flags);
  290. }
  291. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  292. * The firmware seems to think that powering off is a wakeup event!
  293. * This routine turns off remote wakeup and everything else, on all ports.
  294. */
  295. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  296. {
  297. int port = HCS_N_PORTS(ehci->hcs_params);
  298. while (port--)
  299. ehci_writel(ehci, PORT_RWC_BITS,
  300. &ehci->regs->port_status[port]);
  301. }
  302. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  303. * This forcibly disables dma and IRQs, helping kexec and other cases
  304. * where the next system software may expect clean state.
  305. */
  306. static void
  307. ehci_shutdown (struct usb_hcd *hcd)
  308. {
  309. struct ehci_hcd *ehci;
  310. ehci = hcd_to_ehci (hcd);
  311. (void) ehci_halt (ehci);
  312. ehci_turn_off_all_ports(ehci);
  313. /* make BIOS/etc use companion controller during reboot */
  314. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  315. /* unblock posted writes */
  316. ehci_readl(ehci, &ehci->regs->configured_flag);
  317. }
  318. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  319. {
  320. unsigned port;
  321. if (!HCS_PPC (ehci->hcs_params))
  322. return;
  323. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  324. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  325. (void) ehci_hub_control(ehci_to_hcd(ehci),
  326. is_on ? SetPortFeature : ClearPortFeature,
  327. USB_PORT_FEAT_POWER,
  328. port--, NULL, 0);
  329. /* Flush those writes */
  330. ehci_readl(ehci, &ehci->regs->command);
  331. msleep(20);
  332. }
  333. /*-------------------------------------------------------------------------*/
  334. /*
  335. * ehci_work is called from some interrupts, timers, and so on.
  336. * it calls driver completion functions, after dropping ehci->lock.
  337. */
  338. static void ehci_work (struct ehci_hcd *ehci)
  339. {
  340. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  341. /* another CPU may drop ehci->lock during a schedule scan while
  342. * it reports urb completions. this flag guards against bogus
  343. * attempts at re-entrant schedule scanning.
  344. */
  345. if (ehci->scanning)
  346. return;
  347. ehci->scanning = 1;
  348. scan_async (ehci);
  349. if (ehci->next_uframe != -1)
  350. scan_periodic (ehci);
  351. ehci->scanning = 0;
  352. /* the IO watchdog guards against hardware or driver bugs that
  353. * misplace IRQs, and should let us run completely without IRQs.
  354. * such lossage has been observed on both VT6202 and VT8235.
  355. */
  356. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  357. (ehci->async->qh_next.ptr != NULL ||
  358. ehci->periodic_sched != 0))
  359. timer_action (ehci, TIMER_IO_WATCHDOG);
  360. }
  361. static void ehci_stop (struct usb_hcd *hcd)
  362. {
  363. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  364. ehci_dbg (ehci, "stop\n");
  365. /* Turn off port power on all root hub ports. */
  366. ehci_port_power (ehci, 0);
  367. /* no more interrupts ... */
  368. del_timer_sync (&ehci->watchdog);
  369. del_timer_sync(&ehci->iaa_watchdog);
  370. spin_lock_irq(&ehci->lock);
  371. if (HC_IS_RUNNING (hcd->state))
  372. ehci_quiesce (ehci);
  373. ehci_reset (ehci);
  374. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  375. spin_unlock_irq(&ehci->lock);
  376. /* let companion controllers work when we aren't */
  377. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  378. remove_companion_file(ehci);
  379. remove_debug_files (ehci);
  380. /* root hub is shut down separately (first, when possible) */
  381. spin_lock_irq (&ehci->lock);
  382. if (ehci->async)
  383. ehci_work (ehci);
  384. spin_unlock_irq (&ehci->lock);
  385. ehci_mem_cleanup (ehci);
  386. #ifdef EHCI_STATS
  387. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  388. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  389. ehci->stats.lost_iaa);
  390. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  391. ehci->stats.complete, ehci->stats.unlink);
  392. #endif
  393. dbg_status (ehci, "ehci_stop completed",
  394. ehci_readl(ehci, &ehci->regs->status));
  395. }
  396. /* one-time init, only for memory state */
  397. static int ehci_init(struct usb_hcd *hcd)
  398. {
  399. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  400. u32 temp;
  401. int retval;
  402. u32 hcc_params;
  403. spin_lock_init(&ehci->lock);
  404. init_timer(&ehci->watchdog);
  405. ehci->watchdog.function = ehci_watchdog;
  406. ehci->watchdog.data = (unsigned long) ehci;
  407. init_timer(&ehci->iaa_watchdog);
  408. ehci->iaa_watchdog.function = ehci_iaa_watchdog;
  409. ehci->iaa_watchdog.data = (unsigned long) ehci;
  410. /*
  411. * hw default: 1K periodic list heads, one per frame.
  412. * periodic_size can shrink by USBCMD update if hcc_params allows.
  413. */
  414. ehci->periodic_size = DEFAULT_I_TDPS;
  415. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  416. return retval;
  417. /* controllers may cache some of the periodic schedule ... */
  418. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  419. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  420. ehci->i_thresh = 8;
  421. else // N microframes cached
  422. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  423. ehci->reclaim = NULL;
  424. ehci->next_uframe = -1;
  425. /*
  426. * dedicate a qh for the async ring head, since we couldn't unlink
  427. * a 'real' qh without stopping the async schedule [4.8]. use it
  428. * as the 'reclamation list head' too.
  429. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  430. * from automatically advancing to the next td after short reads.
  431. */
  432. ehci->async->qh_next.qh = NULL;
  433. ehci->async->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
  434. ehci->async->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
  435. ehci->async->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  436. ehci->async->hw_qtd_next = EHCI_LIST_END(ehci);
  437. ehci->async->qh_state = QH_STATE_LINKED;
  438. ehci->async->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
  439. /* clear interrupt enables, set irq latency */
  440. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  441. log2_irq_thresh = 0;
  442. temp = 1 << (16 + log2_irq_thresh);
  443. if (HCC_CANPARK(hcc_params)) {
  444. /* HW default park == 3, on hardware that supports it (like
  445. * NVidia and ALI silicon), maximizes throughput on the async
  446. * schedule by avoiding QH fetches between transfers.
  447. *
  448. * With fast usb storage devices and NForce2, "park" seems to
  449. * make problems: throughput reduction (!), data errors...
  450. */
  451. if (park) {
  452. park = min(park, (unsigned) 3);
  453. temp |= CMD_PARK;
  454. temp |= park << 8;
  455. }
  456. ehci_dbg(ehci, "park %d\n", park);
  457. }
  458. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  459. /* periodic schedule size can be smaller than default */
  460. temp &= ~(3 << 2);
  461. temp |= (EHCI_TUNE_FLS << 2);
  462. switch (EHCI_TUNE_FLS) {
  463. case 0: ehci->periodic_size = 1024; break;
  464. case 1: ehci->periodic_size = 512; break;
  465. case 2: ehci->periodic_size = 256; break;
  466. default: BUG();
  467. }
  468. }
  469. ehci->command = temp;
  470. return 0;
  471. }
  472. /* start HC running; it's halted, ehci_init() has been run (once) */
  473. static int ehci_run (struct usb_hcd *hcd)
  474. {
  475. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  476. int retval;
  477. u32 temp;
  478. u32 hcc_params;
  479. hcd->uses_new_polling = 1;
  480. hcd->poll_rh = 0;
  481. /* EHCI spec section 4.1 */
  482. if ((retval = ehci_reset(ehci)) != 0) {
  483. ehci_mem_cleanup(ehci);
  484. return retval;
  485. }
  486. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  487. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  488. /*
  489. * hcc_params controls whether ehci->regs->segment must (!!!)
  490. * be used; it constrains QH/ITD/SITD and QTD locations.
  491. * pci_pool consistent memory always uses segment zero.
  492. * streaming mappings for I/O buffers, like pci_map_single(),
  493. * can return segments above 4GB, if the device allows.
  494. *
  495. * NOTE: the dma mask is visible through dma_supported(), so
  496. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  497. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  498. * host side drivers though.
  499. */
  500. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  501. if (HCC_64BIT_ADDR(hcc_params)) {
  502. ehci_writel(ehci, 0, &ehci->regs->segment);
  503. #if 0
  504. // this is deeply broken on almost all architectures
  505. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  506. ehci_info(ehci, "enabled 64bit DMA\n");
  507. #endif
  508. }
  509. // Philips, Intel, and maybe others need CMD_RUN before the
  510. // root hub will detect new devices (why?); NEC doesn't
  511. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  512. ehci->command |= CMD_RUN;
  513. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  514. dbg_cmd (ehci, "init", ehci->command);
  515. /*
  516. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  517. * are explicitly handed to companion controller(s), so no TT is
  518. * involved with the root hub. (Except where one is integrated,
  519. * and there's no companion controller unless maybe for USB OTG.)
  520. *
  521. * Turning on the CF flag will transfer ownership of all ports
  522. * from the companions to the EHCI controller. If any of the
  523. * companions are in the middle of a port reset at the time, it
  524. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  525. * guarantees that no resets are in progress. After we set CF,
  526. * a short delay lets the hardware catch up; new resets shouldn't
  527. * be started before the port switching actions could complete.
  528. */
  529. down_write(&ehci_cf_port_reset_rwsem);
  530. hcd->state = HC_STATE_RUNNING;
  531. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  532. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  533. msleep(5);
  534. up_write(&ehci_cf_port_reset_rwsem);
  535. temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
  536. ehci_info (ehci,
  537. "USB %x.%x started, EHCI %x.%02x, driver %s%s\n",
  538. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  539. temp >> 8, temp & 0xff, DRIVER_VERSION,
  540. ignore_oc ? ", overcurrent ignored" : "");
  541. ehci_writel(ehci, INTR_MASK,
  542. &ehci->regs->intr_enable); /* Turn On Interrupts */
  543. /* GRR this is run-once init(), being done every time the HC starts.
  544. * So long as they're part of class devices, we can't do it init()
  545. * since the class device isn't created that early.
  546. */
  547. create_debug_files(ehci);
  548. create_companion_file(ehci);
  549. return 0;
  550. }
  551. /*-------------------------------------------------------------------------*/
  552. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  553. {
  554. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  555. u32 status, pcd_status = 0, cmd;
  556. int bh;
  557. spin_lock (&ehci->lock);
  558. status = ehci_readl(ehci, &ehci->regs->status);
  559. /* e.g. cardbus physical eject */
  560. if (status == ~(u32) 0) {
  561. ehci_dbg (ehci, "device removed\n");
  562. goto dead;
  563. }
  564. status &= INTR_MASK;
  565. if (!status) { /* irq sharing? */
  566. spin_unlock(&ehci->lock);
  567. return IRQ_NONE;
  568. }
  569. /* clear (just) interrupts */
  570. ehci_writel(ehci, status, &ehci->regs->status);
  571. cmd = ehci_readl(ehci, &ehci->regs->command);
  572. bh = 0;
  573. #ifdef EHCI_VERBOSE_DEBUG
  574. /* unrequested/ignored: Frame List Rollover */
  575. dbg_status (ehci, "irq", status);
  576. #endif
  577. /* INT, ERR, and IAA interrupt rates can be throttled */
  578. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  579. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  580. if (likely ((status & STS_ERR) == 0))
  581. COUNT (ehci->stats.normal);
  582. else
  583. COUNT (ehci->stats.error);
  584. bh = 1;
  585. }
  586. /* complete the unlinking of some qh [4.15.2.3] */
  587. if (status & STS_IAA) {
  588. /* guard against (alleged) silicon errata */
  589. if (cmd & CMD_IAAD) {
  590. ehci_writel(ehci, cmd & ~CMD_IAAD,
  591. &ehci->regs->command);
  592. ehci_dbg(ehci, "IAA with IAAD still set?\n");
  593. }
  594. if (ehci->reclaim) {
  595. COUNT(ehci->stats.reclaim);
  596. end_unlink_async(ehci);
  597. } else
  598. ehci_dbg(ehci, "IAA with nothing to reclaim?\n");
  599. }
  600. /* remote wakeup [4.3.1] */
  601. if (status & STS_PCD) {
  602. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  603. pcd_status = status;
  604. /* resume root hub? */
  605. if (!(ehci_readl(ehci, &ehci->regs->command) & CMD_RUN))
  606. usb_hcd_resume_root_hub(hcd);
  607. while (i--) {
  608. int pstatus = ehci_readl(ehci,
  609. &ehci->regs->port_status [i]);
  610. if (pstatus & PORT_OWNER)
  611. continue;
  612. if (!(pstatus & PORT_RESUME)
  613. || ehci->reset_done [i] != 0)
  614. continue;
  615. /* start 20 msec resume signaling from this port,
  616. * and make khubd collect PORT_STAT_C_SUSPEND to
  617. * stop that signaling.
  618. */
  619. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  620. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  621. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  622. }
  623. }
  624. /* PCI errors [4.15.2.4] */
  625. if (unlikely ((status & STS_FATAL) != 0)) {
  626. /* bogus "fatal" IRQs appear on some chips... why? */
  627. status = ehci_readl(ehci, &ehci->regs->status);
  628. dbg_cmd (ehci, "fatal", ehci_readl(ehci,
  629. &ehci->regs->command));
  630. dbg_status (ehci, "fatal", status);
  631. if (status & STS_HALT) {
  632. ehci_err (ehci, "fatal error\n");
  633. dead:
  634. ehci_reset (ehci);
  635. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  636. /* generic layer kills/unlinks all urbs, then
  637. * uses ehci_stop to clean up the rest
  638. */
  639. bh = 1;
  640. }
  641. }
  642. if (bh)
  643. ehci_work (ehci);
  644. spin_unlock (&ehci->lock);
  645. if (pcd_status & STS_PCD)
  646. usb_hcd_poll_rh_status(hcd);
  647. return IRQ_HANDLED;
  648. }
  649. /*-------------------------------------------------------------------------*/
  650. /*
  651. * non-error returns are a promise to giveback() the urb later
  652. * we drop ownership so next owner (or urb unlink) can get it
  653. *
  654. * urb + dev is in hcd.self.controller.urb_list
  655. * we're queueing TDs onto software and hardware lists
  656. *
  657. * hcd-specific init for hcpriv hasn't been done yet
  658. *
  659. * NOTE: control, bulk, and interrupt share the same code to append TDs
  660. * to a (possibly active) QH, and the same QH scanning code.
  661. */
  662. static int ehci_urb_enqueue (
  663. struct usb_hcd *hcd,
  664. struct urb *urb,
  665. gfp_t mem_flags
  666. ) {
  667. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  668. struct list_head qtd_list;
  669. INIT_LIST_HEAD (&qtd_list);
  670. switch (usb_pipetype (urb->pipe)) {
  671. // case PIPE_CONTROL:
  672. // case PIPE_BULK:
  673. default:
  674. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  675. return -ENOMEM;
  676. return submit_async(ehci, urb, &qtd_list, mem_flags);
  677. case PIPE_INTERRUPT:
  678. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  679. return -ENOMEM;
  680. return intr_submit(ehci, urb, &qtd_list, mem_flags);
  681. case PIPE_ISOCHRONOUS:
  682. if (urb->dev->speed == USB_SPEED_HIGH)
  683. return itd_submit (ehci, urb, mem_flags);
  684. else
  685. return sitd_submit (ehci, urb, mem_flags);
  686. }
  687. }
  688. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  689. {
  690. /* failfast */
  691. if (!HC_IS_RUNNING(ehci_to_hcd(ehci)->state) && ehci->reclaim)
  692. end_unlink_async(ehci);
  693. /* if it's not linked then there's nothing to do */
  694. if (qh->qh_state != QH_STATE_LINKED)
  695. ;
  696. /* defer till later if busy */
  697. else if (ehci->reclaim) {
  698. struct ehci_qh *last;
  699. for (last = ehci->reclaim;
  700. last->reclaim;
  701. last = last->reclaim)
  702. continue;
  703. qh->qh_state = QH_STATE_UNLINK_WAIT;
  704. last->reclaim = qh;
  705. /* start IAA cycle */
  706. } else
  707. start_unlink_async (ehci, qh);
  708. }
  709. /* remove from hardware lists
  710. * completions normally happen asynchronously
  711. */
  712. static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  713. {
  714. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  715. struct ehci_qh *qh;
  716. unsigned long flags;
  717. int rc;
  718. spin_lock_irqsave (&ehci->lock, flags);
  719. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  720. if (rc)
  721. goto done;
  722. switch (usb_pipetype (urb->pipe)) {
  723. // case PIPE_CONTROL:
  724. // case PIPE_BULK:
  725. default:
  726. qh = (struct ehci_qh *) urb->hcpriv;
  727. if (!qh)
  728. break;
  729. switch (qh->qh_state) {
  730. case QH_STATE_LINKED:
  731. case QH_STATE_COMPLETING:
  732. unlink_async(ehci, qh);
  733. break;
  734. case QH_STATE_UNLINK:
  735. case QH_STATE_UNLINK_WAIT:
  736. /* already started */
  737. break;
  738. case QH_STATE_IDLE:
  739. WARN_ON(1);
  740. break;
  741. }
  742. break;
  743. case PIPE_INTERRUPT:
  744. qh = (struct ehci_qh *) urb->hcpriv;
  745. if (!qh)
  746. break;
  747. switch (qh->qh_state) {
  748. case QH_STATE_LINKED:
  749. intr_deschedule (ehci, qh);
  750. /* FALL THROUGH */
  751. case QH_STATE_IDLE:
  752. qh_completions (ehci, qh);
  753. break;
  754. default:
  755. ehci_dbg (ehci, "bogus qh %p state %d\n",
  756. qh, qh->qh_state);
  757. goto done;
  758. }
  759. /* reschedule QH iff another request is queued */
  760. if (!list_empty (&qh->qtd_list)
  761. && HC_IS_RUNNING (hcd->state)) {
  762. rc = qh_schedule(ehci, qh);
  763. /* An error here likely indicates handshake failure
  764. * or no space left in the schedule. Neither fault
  765. * should happen often ...
  766. *
  767. * FIXME kill the now-dysfunctional queued urbs
  768. */
  769. if (rc != 0)
  770. ehci_err(ehci,
  771. "can't reschedule qh %p, err %d",
  772. qh, rc);
  773. }
  774. break;
  775. case PIPE_ISOCHRONOUS:
  776. // itd or sitd ...
  777. // wait till next completion, do it then.
  778. // completion irqs can wait up to 1024 msec,
  779. break;
  780. }
  781. done:
  782. spin_unlock_irqrestore (&ehci->lock, flags);
  783. return rc;
  784. }
  785. /*-------------------------------------------------------------------------*/
  786. // bulk qh holds the data toggle
  787. static void
  788. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  789. {
  790. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  791. unsigned long flags;
  792. struct ehci_qh *qh, *tmp;
  793. /* ASSERT: any requests/urbs are being unlinked */
  794. /* ASSERT: nobody can be submitting urbs for this any more */
  795. rescan:
  796. spin_lock_irqsave (&ehci->lock, flags);
  797. qh = ep->hcpriv;
  798. if (!qh)
  799. goto done;
  800. /* endpoints can be iso streams. for now, we don't
  801. * accelerate iso completions ... so spin a while.
  802. */
  803. if (qh->hw_info1 == 0) {
  804. ehci_vdbg (ehci, "iso delay\n");
  805. goto idle_timeout;
  806. }
  807. if (!HC_IS_RUNNING (hcd->state))
  808. qh->qh_state = QH_STATE_IDLE;
  809. switch (qh->qh_state) {
  810. case QH_STATE_LINKED:
  811. for (tmp = ehci->async->qh_next.qh;
  812. tmp && tmp != qh;
  813. tmp = tmp->qh_next.qh)
  814. continue;
  815. /* periodic qh self-unlinks on empty */
  816. if (!tmp)
  817. goto nogood;
  818. unlink_async (ehci, qh);
  819. /* FALL THROUGH */
  820. case QH_STATE_UNLINK: /* wait for hw to finish? */
  821. case QH_STATE_UNLINK_WAIT:
  822. idle_timeout:
  823. spin_unlock_irqrestore (&ehci->lock, flags);
  824. schedule_timeout_uninterruptible(1);
  825. goto rescan;
  826. case QH_STATE_IDLE: /* fully unlinked */
  827. if (list_empty (&qh->qtd_list)) {
  828. qh_put (qh);
  829. break;
  830. }
  831. /* else FALL THROUGH */
  832. default:
  833. nogood:
  834. /* caller was supposed to have unlinked any requests;
  835. * that's not our job. just leak this memory.
  836. */
  837. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  838. qh, ep->desc.bEndpointAddress, qh->qh_state,
  839. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  840. break;
  841. }
  842. ep->hcpriv = NULL;
  843. done:
  844. spin_unlock_irqrestore (&ehci->lock, flags);
  845. return;
  846. }
  847. static int ehci_get_frame (struct usb_hcd *hcd)
  848. {
  849. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  850. return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) %
  851. ehci->periodic_size;
  852. }
  853. /*-------------------------------------------------------------------------*/
  854. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  855. MODULE_DESCRIPTION (DRIVER_INFO);
  856. MODULE_AUTHOR (DRIVER_AUTHOR);
  857. MODULE_LICENSE ("GPL");
  858. #ifdef CONFIG_PCI
  859. #include "ehci-pci.c"
  860. #define PCI_DRIVER ehci_pci_driver
  861. #endif
  862. #ifdef CONFIG_USB_EHCI_FSL
  863. #include "ehci-fsl.c"
  864. #define PLATFORM_DRIVER ehci_fsl_driver
  865. #endif
  866. #ifdef CONFIG_SOC_AU1200
  867. #include "ehci-au1xxx.c"
  868. #define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
  869. #endif
  870. #ifdef CONFIG_PPC_PS3
  871. #include "ehci-ps3.c"
  872. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
  873. #endif
  874. #if defined(CONFIG_440EPX) && !defined(CONFIG_PPC_MERGE)
  875. #include "ehci-ppc-soc.c"
  876. #define PLATFORM_DRIVER ehci_ppc_soc_driver
  877. #endif
  878. #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
  879. #include "ehci-ppc-of.c"
  880. #define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
  881. #endif
  882. #ifdef CONFIG_PLAT_ORION
  883. #include "ehci-orion.c"
  884. #define PLATFORM_DRIVER ehci_orion_driver
  885. #endif
  886. #ifdef CONFIG_ARCH_IXP4XX
  887. #include "ehci-ixp4xx.c"
  888. #define PLATFORM_DRIVER ixp4xx_ehci_driver
  889. #endif
  890. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
  891. !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER)
  892. #error "missing bus glue for ehci-hcd"
  893. #endif
  894. static int __init ehci_hcd_init(void)
  895. {
  896. int retval = 0;
  897. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  898. hcd_name,
  899. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  900. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  901. #ifdef DEBUG
  902. ehci_debug_root = debugfs_create_dir("ehci", NULL);
  903. if (!ehci_debug_root)
  904. return -ENOENT;
  905. #endif
  906. #ifdef PLATFORM_DRIVER
  907. retval = platform_driver_register(&PLATFORM_DRIVER);
  908. if (retval < 0)
  909. goto clean0;
  910. #endif
  911. #ifdef PCI_DRIVER
  912. retval = pci_register_driver(&PCI_DRIVER);
  913. if (retval < 0)
  914. goto clean1;
  915. #endif
  916. #ifdef PS3_SYSTEM_BUS_DRIVER
  917. retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  918. if (retval < 0)
  919. goto clean2;
  920. #endif
  921. #ifdef OF_PLATFORM_DRIVER
  922. retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
  923. if (retval < 0)
  924. goto clean3;
  925. #endif
  926. return retval;
  927. #ifdef OF_PLATFORM_DRIVER
  928. /* of_unregister_platform_driver(&OF_PLATFORM_DRIVER); */
  929. clean3:
  930. #endif
  931. #ifdef PS3_SYSTEM_BUS_DRIVER
  932. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  933. clean2:
  934. #endif
  935. #ifdef PCI_DRIVER
  936. pci_unregister_driver(&PCI_DRIVER);
  937. clean1:
  938. #endif
  939. #ifdef PLATFORM_DRIVER
  940. platform_driver_unregister(&PLATFORM_DRIVER);
  941. clean0:
  942. #endif
  943. #ifdef DEBUG
  944. debugfs_remove(ehci_debug_root);
  945. ehci_debug_root = NULL;
  946. #endif
  947. return retval;
  948. }
  949. module_init(ehci_hcd_init);
  950. static void __exit ehci_hcd_cleanup(void)
  951. {
  952. #ifdef OF_PLATFORM_DRIVER
  953. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  954. #endif
  955. #ifdef PLATFORM_DRIVER
  956. platform_driver_unregister(&PLATFORM_DRIVER);
  957. #endif
  958. #ifdef PCI_DRIVER
  959. pci_unregister_driver(&PCI_DRIVER);
  960. #endif
  961. #ifdef PS3_SYSTEM_BUS_DRIVER
  962. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  963. #endif
  964. #ifdef DEBUG
  965. debugfs_remove(ehci_debug_root);
  966. #endif
  967. }
  968. module_exit(ehci_hcd_cleanup);