ehci-hcd.c 27 KB

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