ehci-hcd.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. msleep(20);
  336. }
  337. /*-------------------------------------------------------------------------*/
  338. /*
  339. * ehci_work is called from some interrupts, timers, and so on.
  340. * it calls driver completion functions, after dropping ehci->lock.
  341. */
  342. static void ehci_work (struct ehci_hcd *ehci)
  343. {
  344. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  345. if (ehci->reclaim_ready)
  346. end_unlink_async (ehci);
  347. /* another CPU may drop ehci->lock during a schedule scan while
  348. * it reports urb completions. this flag guards against bogus
  349. * attempts at re-entrant schedule scanning.
  350. */
  351. if (ehci->scanning)
  352. return;
  353. ehci->scanning = 1;
  354. scan_async (ehci);
  355. if (ehci->next_uframe != -1)
  356. scan_periodic (ehci);
  357. ehci->scanning = 0;
  358. /* the IO watchdog guards against hardware or driver bugs that
  359. * misplace IRQs, and should let us run completely without IRQs.
  360. * such lossage has been observed on both VT6202 and VT8235.
  361. */
  362. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  363. (ehci->async->qh_next.ptr != NULL ||
  364. ehci->periodic_sched != 0))
  365. timer_action (ehci, TIMER_IO_WATCHDOG);
  366. }
  367. static void ehci_stop (struct usb_hcd *hcd)
  368. {
  369. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  370. ehci_dbg (ehci, "stop\n");
  371. /* Turn off port power on all root hub ports. */
  372. ehci_port_power (ehci, 0);
  373. /* no more interrupts ... */
  374. del_timer_sync (&ehci->watchdog);
  375. spin_lock_irq(&ehci->lock);
  376. if (HC_IS_RUNNING (hcd->state))
  377. ehci_quiesce (ehci);
  378. ehci_reset (ehci);
  379. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  380. spin_unlock_irq(&ehci->lock);
  381. #ifdef CONFIG_CPU_FREQ
  382. cpufreq_unregister_notifier(&ehci->cpufreq_transition,
  383. CPUFREQ_TRANSITION_NOTIFIER);
  384. #endif
  385. /* let companion controllers work when we aren't */
  386. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  387. remove_companion_file(ehci);
  388. remove_debug_files (ehci);
  389. /* root hub is shut down separately (first, when possible) */
  390. spin_lock_irq (&ehci->lock);
  391. if (ehci->async)
  392. ehci_work (ehci);
  393. spin_unlock_irq (&ehci->lock);
  394. ehci_mem_cleanup (ehci);
  395. #ifdef EHCI_STATS
  396. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  397. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  398. ehci->stats.lost_iaa);
  399. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  400. ehci->stats.complete, ehci->stats.unlink);
  401. #endif
  402. dbg_status (ehci, "ehci_stop completed",
  403. ehci_readl(ehci, &ehci->regs->status));
  404. }
  405. /* one-time init, only for memory state */
  406. static int ehci_init(struct usb_hcd *hcd)
  407. {
  408. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  409. u32 temp;
  410. int retval;
  411. u32 hcc_params;
  412. spin_lock_init(&ehci->lock);
  413. init_timer(&ehci->watchdog);
  414. ehci->watchdog.function = ehci_watchdog;
  415. ehci->watchdog.data = (unsigned long) ehci;
  416. /*
  417. * hw default: 1K periodic list heads, one per frame.
  418. * periodic_size can shrink by USBCMD update if hcc_params allows.
  419. */
  420. ehci->periodic_size = DEFAULT_I_TDPS;
  421. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  422. return retval;
  423. /* controllers may cache some of the periodic schedule ... */
  424. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  425. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  426. ehci->i_thresh = 8;
  427. else // N microframes cached
  428. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  429. ehci->reclaim = NULL;
  430. ehci->reclaim_ready = 0;
  431. ehci->next_uframe = -1;
  432. /*
  433. * dedicate a qh for the async ring head, since we couldn't unlink
  434. * a 'real' qh without stopping the async schedule [4.8]. use it
  435. * as the 'reclamation list head' too.
  436. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  437. * from automatically advancing to the next td after short reads.
  438. */
  439. ehci->async->qh_next.qh = NULL;
  440. ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
  441. ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
  442. ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  443. ehci->async->hw_qtd_next = EHCI_LIST_END;
  444. ehci->async->qh_state = QH_STATE_LINKED;
  445. ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
  446. /* clear interrupt enables, set irq latency */
  447. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  448. log2_irq_thresh = 0;
  449. temp = 1 << (16 + log2_irq_thresh);
  450. if (HCC_CANPARK(hcc_params)) {
  451. /* HW default park == 3, on hardware that supports it (like
  452. * NVidia and ALI silicon), maximizes throughput on the async
  453. * schedule by avoiding QH fetches between transfers.
  454. *
  455. * With fast usb storage devices and NForce2, "park" seems to
  456. * make problems: throughput reduction (!), data errors...
  457. */
  458. if (park) {
  459. park = min(park, (unsigned) 3);
  460. temp |= CMD_PARK;
  461. temp |= park << 8;
  462. }
  463. ehci_dbg(ehci, "park %d\n", park);
  464. }
  465. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  466. /* periodic schedule size can be smaller than default */
  467. temp &= ~(3 << 2);
  468. temp |= (EHCI_TUNE_FLS << 2);
  469. switch (EHCI_TUNE_FLS) {
  470. case 0: ehci->periodic_size = 1024; break;
  471. case 1: ehci->periodic_size = 512; break;
  472. case 2: ehci->periodic_size = 256; break;
  473. default: BUG();
  474. }
  475. }
  476. ehci->command = temp;
  477. #ifdef CONFIG_CPU_FREQ
  478. INIT_LIST_HEAD(&ehci->split_intr_qhs);
  479. /*
  480. * If the EHCI controller caches enough uframes, this probably
  481. * isn't needed unless there are so many low/full speed devices
  482. * that the controller's can't cache it all.
  483. */
  484. ehci->cpufreq_transition.notifier_call = ehci_cpufreq_notifier;
  485. cpufreq_register_notifier(&ehci->cpufreq_transition,
  486. CPUFREQ_TRANSITION_NOTIFIER);
  487. #endif
  488. return 0;
  489. }
  490. /* start HC running; it's halted, ehci_init() has been run (once) */
  491. static int ehci_run (struct usb_hcd *hcd)
  492. {
  493. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  494. int retval;
  495. u32 temp;
  496. u32 hcc_params;
  497. hcd->uses_new_polling = 1;
  498. hcd->poll_rh = 0;
  499. /* EHCI spec section 4.1 */
  500. if ((retval = ehci_reset(ehci)) != 0) {
  501. ehci_mem_cleanup(ehci);
  502. return retval;
  503. }
  504. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  505. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  506. /*
  507. * hcc_params controls whether ehci->regs->segment must (!!!)
  508. * be used; it constrains QH/ITD/SITD and QTD locations.
  509. * pci_pool consistent memory always uses segment zero.
  510. * streaming mappings for I/O buffers, like pci_map_single(),
  511. * can return segments above 4GB, if the device allows.
  512. *
  513. * NOTE: the dma mask is visible through dma_supported(), so
  514. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  515. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  516. * host side drivers though.
  517. */
  518. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  519. if (HCC_64BIT_ADDR(hcc_params)) {
  520. ehci_writel(ehci, 0, &ehci->regs->segment);
  521. #if 0
  522. // this is deeply broken on almost all architectures
  523. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  524. ehci_info(ehci, "enabled 64bit DMA\n");
  525. #endif
  526. }
  527. // Philips, Intel, and maybe others need CMD_RUN before the
  528. // root hub will detect new devices (why?); NEC doesn't
  529. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  530. ehci->command |= CMD_RUN;
  531. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  532. dbg_cmd (ehci, "init", ehci->command);
  533. /*
  534. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  535. * are explicitly handed to companion controller(s), so no TT is
  536. * involved with the root hub. (Except where one is integrated,
  537. * and there's no companion controller unless maybe for USB OTG.)
  538. */
  539. hcd->state = HC_STATE_RUNNING;
  540. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  541. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  542. temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
  543. ehci_info (ehci,
  544. "USB %x.%x started, EHCI %x.%02x, driver %s%s\n",
  545. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  546. temp >> 8, temp & 0xff, DRIVER_VERSION,
  547. ignore_oc ? ", overcurrent ignored" : "");
  548. ehci_writel(ehci, INTR_MASK,
  549. &ehci->regs->intr_enable); /* Turn On Interrupts */
  550. /* GRR this is run-once init(), being done every time the HC starts.
  551. * So long as they're part of class devices, we can't do it init()
  552. * since the class device isn't created that early.
  553. */
  554. create_debug_files(ehci);
  555. create_companion_file(ehci);
  556. return 0;
  557. }
  558. /*-------------------------------------------------------------------------*/
  559. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  560. {
  561. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  562. u32 status, pcd_status = 0;
  563. int bh;
  564. spin_lock (&ehci->lock);
  565. status = ehci_readl(ehci, &ehci->regs->status);
  566. /* e.g. cardbus physical eject */
  567. if (status == ~(u32) 0) {
  568. ehci_dbg (ehci, "device removed\n");
  569. goto dead;
  570. }
  571. status &= INTR_MASK;
  572. if (!status) { /* irq sharing? */
  573. spin_unlock(&ehci->lock);
  574. return IRQ_NONE;
  575. }
  576. /* clear (just) interrupts */
  577. ehci_writel(ehci, status, &ehci->regs->status);
  578. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  579. bh = 0;
  580. #ifdef EHCI_VERBOSE_DEBUG
  581. /* unrequested/ignored: Frame List Rollover */
  582. dbg_status (ehci, "irq", status);
  583. #endif
  584. /* INT, ERR, and IAA interrupt rates can be throttled */
  585. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  586. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  587. if (likely ((status & STS_ERR) == 0))
  588. COUNT (ehci->stats.normal);
  589. else
  590. COUNT (ehci->stats.error);
  591. bh = 1;
  592. }
  593. /* complete the unlinking of some qh [4.15.2.3] */
  594. if (status & STS_IAA) {
  595. COUNT (ehci->stats.reclaim);
  596. ehci->reclaim_ready = 1;
  597. bh = 1;
  598. }
  599. /* remote wakeup [4.3.1] */
  600. if (status & STS_PCD) {
  601. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  602. pcd_status = status;
  603. /* resume root hub? */
  604. if (!(ehci_readl(ehci, &ehci->regs->command) & CMD_RUN))
  605. usb_hcd_resume_root_hub(hcd);
  606. while (i--) {
  607. int pstatus = ehci_readl(ehci,
  608. &ehci->regs->port_status [i]);
  609. if (pstatus & PORT_OWNER)
  610. continue;
  611. if (!(pstatus & PORT_RESUME)
  612. || ehci->reset_done [i] != 0)
  613. continue;
  614. /* start 20 msec resume signaling from this port,
  615. * and make khubd collect PORT_STAT_C_SUSPEND to
  616. * stop that signaling.
  617. */
  618. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  619. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  620. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  621. }
  622. }
  623. /* PCI errors [4.15.2.4] */
  624. if (unlikely ((status & STS_FATAL) != 0)) {
  625. /* bogus "fatal" IRQs appear on some chips... why? */
  626. status = ehci_readl(ehci, &ehci->regs->status);
  627. dbg_cmd (ehci, "fatal", ehci_readl(ehci,
  628. &ehci->regs->command));
  629. dbg_status (ehci, "fatal", status);
  630. if (status & STS_HALT) {
  631. ehci_err (ehci, "fatal error\n");
  632. dead:
  633. ehci_reset (ehci);
  634. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  635. /* generic layer kills/unlinks all urbs, then
  636. * uses ehci_stop to clean up the rest
  637. */
  638. bh = 1;
  639. }
  640. }
  641. if (bh)
  642. ehci_work (ehci);
  643. spin_unlock (&ehci->lock);
  644. if (pcd_status & STS_PCD)
  645. usb_hcd_poll_rh_status(hcd);
  646. return IRQ_HANDLED;
  647. }
  648. /*-------------------------------------------------------------------------*/
  649. /*
  650. * non-error returns are a promise to giveback() the urb later
  651. * we drop ownership so next owner (or urb unlink) can get it
  652. *
  653. * urb + dev is in hcd.self.controller.urb_list
  654. * we're queueing TDs onto software and hardware lists
  655. *
  656. * hcd-specific init for hcpriv hasn't been done yet
  657. *
  658. * NOTE: control, bulk, and interrupt share the same code to append TDs
  659. * to a (possibly active) QH, and the same QH scanning code.
  660. */
  661. static int ehci_urb_enqueue (
  662. struct usb_hcd *hcd,
  663. struct usb_host_endpoint *ep,
  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, ep, 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, ep, 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. /* if we need to use IAA and it's busy, defer */
  691. if (qh->qh_state == QH_STATE_LINKED
  692. && ehci->reclaim
  693. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) {
  694. struct ehci_qh *last;
  695. for (last = ehci->reclaim;
  696. last->reclaim;
  697. last = last->reclaim)
  698. continue;
  699. qh->qh_state = QH_STATE_UNLINK_WAIT;
  700. last->reclaim = qh;
  701. /* bypass IAA if the hc can't care */
  702. } else if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && ehci->reclaim)
  703. end_unlink_async (ehci);
  704. /* something else might have unlinked the qh by now */
  705. if (qh->qh_state == QH_STATE_LINKED)
  706. start_unlink_async (ehci, qh);
  707. }
  708. /* remove from hardware lists
  709. * completions normally happen asynchronously
  710. */
  711. static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  712. {
  713. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  714. struct ehci_qh *qh;
  715. unsigned long flags;
  716. spin_lock_irqsave (&ehci->lock, flags);
  717. switch (usb_pipetype (urb->pipe)) {
  718. // case PIPE_CONTROL:
  719. // case PIPE_BULK:
  720. default:
  721. qh = (struct ehci_qh *) urb->hcpriv;
  722. if (!qh)
  723. break;
  724. unlink_async (ehci, qh);
  725. break;
  726. case PIPE_INTERRUPT:
  727. qh = (struct ehci_qh *) urb->hcpriv;
  728. if (!qh)
  729. break;
  730. switch (qh->qh_state) {
  731. case QH_STATE_LINKED:
  732. intr_deschedule (ehci, qh);
  733. /* FALL THROUGH */
  734. case QH_STATE_IDLE:
  735. qh_completions (ehci, qh);
  736. break;
  737. default:
  738. ehci_dbg (ehci, "bogus qh %p state %d\n",
  739. qh, qh->qh_state);
  740. goto done;
  741. }
  742. /* reschedule QH iff another request is queued */
  743. if (!list_empty (&qh->qtd_list)
  744. && HC_IS_RUNNING (hcd->state)) {
  745. int status;
  746. status = qh_schedule (ehci, qh);
  747. spin_unlock_irqrestore (&ehci->lock, flags);
  748. if (status != 0) {
  749. // shouldn't happen often, but ...
  750. // FIXME kill those tds' urbs
  751. err ("can't reschedule qh %p, err %d",
  752. qh, status);
  753. }
  754. return status;
  755. }
  756. break;
  757. case PIPE_ISOCHRONOUS:
  758. // itd or sitd ...
  759. // wait till next completion, do it then.
  760. // completion irqs can wait up to 1024 msec,
  761. break;
  762. }
  763. done:
  764. spin_unlock_irqrestore (&ehci->lock, flags);
  765. return 0;
  766. }
  767. /*-------------------------------------------------------------------------*/
  768. // bulk qh holds the data toggle
  769. static void
  770. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  771. {
  772. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  773. unsigned long flags;
  774. struct ehci_qh *qh, *tmp;
  775. /* ASSERT: any requests/urbs are being unlinked */
  776. /* ASSERT: nobody can be submitting urbs for this any more */
  777. rescan:
  778. spin_lock_irqsave (&ehci->lock, flags);
  779. qh = ep->hcpriv;
  780. if (!qh)
  781. goto done;
  782. /* endpoints can be iso streams. for now, we don't
  783. * accelerate iso completions ... so spin a while.
  784. */
  785. if (qh->hw_info1 == 0) {
  786. ehci_vdbg (ehci, "iso delay\n");
  787. goto idle_timeout;
  788. }
  789. if (!HC_IS_RUNNING (hcd->state))
  790. qh->qh_state = QH_STATE_IDLE;
  791. switch (qh->qh_state) {
  792. case QH_STATE_LINKED:
  793. for (tmp = ehci->async->qh_next.qh;
  794. tmp && tmp != qh;
  795. tmp = tmp->qh_next.qh)
  796. continue;
  797. /* periodic qh self-unlinks on empty */
  798. if (!tmp)
  799. goto nogood;
  800. unlink_async (ehci, qh);
  801. /* FALL THROUGH */
  802. case QH_STATE_UNLINK: /* wait for hw to finish? */
  803. idle_timeout:
  804. spin_unlock_irqrestore (&ehci->lock, flags);
  805. schedule_timeout_uninterruptible(1);
  806. goto rescan;
  807. case QH_STATE_IDLE: /* fully unlinked */
  808. if (list_empty (&qh->qtd_list)) {
  809. qh_put (qh);
  810. break;
  811. }
  812. /* else FALL THROUGH */
  813. default:
  814. nogood:
  815. /* caller was supposed to have unlinked any requests;
  816. * that's not our job. just leak this memory.
  817. */
  818. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  819. qh, ep->desc.bEndpointAddress, qh->qh_state,
  820. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  821. break;
  822. }
  823. ep->hcpriv = NULL;
  824. done:
  825. spin_unlock_irqrestore (&ehci->lock, flags);
  826. return;
  827. }
  828. static int ehci_get_frame (struct usb_hcd *hcd)
  829. {
  830. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  831. return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) %
  832. ehci->periodic_size;
  833. }
  834. /*-------------------------------------------------------------------------*/
  835. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  836. MODULE_DESCRIPTION (DRIVER_INFO);
  837. MODULE_AUTHOR (DRIVER_AUTHOR);
  838. MODULE_LICENSE ("GPL");
  839. #ifdef CONFIG_PCI
  840. #include "ehci-pci.c"
  841. #define PCI_DRIVER ehci_pci_driver
  842. #endif
  843. #ifdef CONFIG_MPC834x
  844. #include "ehci-fsl.c"
  845. #define PLATFORM_DRIVER ehci_fsl_driver
  846. #endif
  847. #ifdef CONFIG_SOC_AU1200
  848. #include "ehci-au1xxx.c"
  849. #define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
  850. #endif
  851. #ifdef CONFIG_PPC_PS3
  852. #include "ehci-ps3.c"
  853. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_sb_driver
  854. #endif
  855. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
  856. !defined(PS3_SYSTEM_BUS_DRIVER)
  857. #error "missing bus glue for ehci-hcd"
  858. #endif
  859. static int __init ehci_hcd_init(void)
  860. {
  861. int retval = 0;
  862. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  863. hcd_name,
  864. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  865. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  866. #ifdef PLATFORM_DRIVER
  867. retval = platform_driver_register(&PLATFORM_DRIVER);
  868. if (retval < 0)
  869. return retval;
  870. #endif
  871. #ifdef PCI_DRIVER
  872. retval = pci_register_driver(&PCI_DRIVER);
  873. if (retval < 0) {
  874. #ifdef PLATFORM_DRIVER
  875. platform_driver_unregister(&PLATFORM_DRIVER);
  876. #endif
  877. return retval;
  878. }
  879. #endif
  880. #ifdef PS3_SYSTEM_BUS_DRIVER
  881. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  882. retval = ps3_system_bus_driver_register(
  883. &PS3_SYSTEM_BUS_DRIVER);
  884. if (retval < 0) {
  885. #ifdef PLATFORM_DRIVER
  886. platform_driver_unregister(&PLATFORM_DRIVER);
  887. #endif
  888. #ifdef PCI_DRIVER
  889. pci_unregister_driver(&PCI_DRIVER);
  890. #endif
  891. return retval;
  892. }
  893. }
  894. #endif
  895. return retval;
  896. }
  897. module_init(ehci_hcd_init);
  898. static void __exit ehci_hcd_cleanup(void)
  899. {
  900. #ifdef PLATFORM_DRIVER
  901. platform_driver_unregister(&PLATFORM_DRIVER);
  902. #endif
  903. #ifdef PCI_DRIVER
  904. pci_unregister_driver(&PCI_DRIVER);
  905. #endif
  906. #ifdef PS3_SYSTEM_BUS_DRIVER
  907. if (firmware_has_feature(FW_FEATURE_PS3_LV1))
  908. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  909. #endif
  910. }
  911. module_exit(ehci_hcd_cleanup);