ehci-hcd.c 31 KB

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