ehci-hcd.c 30 KB

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