ehci-hcd.c 30 KB

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