ehci-hcd.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. * Enhanced Host Controller Interface (EHCI) driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * Copyright (c) 2000-2004 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/sched.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/hrtimer.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/hcd.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/slab.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/byteorder.h>
  43. #include <asm/io.h>
  44. #include <asm/irq.h>
  45. #include <asm/unaligned.h>
  46. #if defined(CONFIG_PPC_PS3)
  47. #include <asm/firmware.h>
  48. #endif
  49. /*-------------------------------------------------------------------------*/
  50. /*
  51. * EHCI hc_driver implementation ... experimental, incomplete.
  52. * Based on the final 1.0 register interface specification.
  53. *
  54. * USB 2.0 shows up in upcoming www.pcmcia.org technology.
  55. * First was PCMCIA, like ISA; then CardBus, which is PCI.
  56. * Next comes "CardBay", using USB 2.0 signals.
  57. *
  58. * Contains additional contributions by Brad Hards, Rory Bolt, and others.
  59. * Special thanks to Intel and VIA for providing host controllers to
  60. * test this driver on, and Cypress (including In-System Design) for
  61. * providing early devices for those host controllers to talk to!
  62. */
  63. #define DRIVER_AUTHOR "David Brownell"
  64. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  65. static const char hcd_name [] = "ehci_hcd";
  66. #undef VERBOSE_DEBUG
  67. #undef EHCI_URB_TRACE
  68. #ifdef DEBUG
  69. #define EHCI_STATS
  70. #endif
  71. /* magic numbers that can affect system performance */
  72. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  73. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  74. #define EHCI_TUNE_RL_TT 0
  75. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  76. #define EHCI_TUNE_MULT_TT 1
  77. /*
  78. * Some drivers think it's safe to schedule isochronous transfers more than
  79. * 256 ms into the future (partly as a result of an old bug in the scheduling
  80. * code). In an attempt to avoid trouble, we will use a minimum scheduling
  81. * length of 512 frames instead of 256.
  82. */
  83. #define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
  84. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  85. #define EHCI_SHRINK_JIFFIES (DIV_ROUND_UP(HZ, 200) + 1)
  86. /* 5-ms async qh unlink delay */
  87. /* Initial IRQ latency: faster than hw default */
  88. static int log2_irq_thresh = 0; // 0 to 6
  89. module_param (log2_irq_thresh, int, S_IRUGO);
  90. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  91. /* initial park setting: slower than hw default */
  92. static unsigned park = 0;
  93. module_param (park, uint, S_IRUGO);
  94. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  95. /* for flakey hardware, ignore overcurrent indicators */
  96. static bool ignore_oc = 0;
  97. module_param (ignore_oc, bool, S_IRUGO);
  98. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  99. /* for link power management(LPM) feature */
  100. static unsigned int hird;
  101. module_param(hird, int, S_IRUGO);
  102. MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  103. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  104. /*-------------------------------------------------------------------------*/
  105. #include "ehci.h"
  106. #include "ehci-dbg.c"
  107. #include "pci-quirks.h"
  108. /*-------------------------------------------------------------------------*/
  109. static void
  110. timer_action(struct ehci_hcd *ehci, enum ehci_timer_action action)
  111. {
  112. /* Don't override timeouts which shrink or (later) disable
  113. * the async ring; just the I/O watchdog. Note that if a
  114. * SHRINK were pending, OFF would never be requested.
  115. */
  116. if (timer_pending(&ehci->watchdog)
  117. && (BIT(TIMER_ASYNC_SHRINK)
  118. & ehci->actions))
  119. return;
  120. if (!test_and_set_bit(action, &ehci->actions)) {
  121. unsigned long t;
  122. switch (action) {
  123. case TIMER_IO_WATCHDOG:
  124. if (!ehci->need_io_watchdog)
  125. return;
  126. t = EHCI_IO_JIFFIES;
  127. break;
  128. /* case TIMER_ASYNC_SHRINK: */
  129. default:
  130. t = EHCI_SHRINK_JIFFIES;
  131. break;
  132. }
  133. mod_timer(&ehci->watchdog, t + jiffies);
  134. }
  135. }
  136. /*-------------------------------------------------------------------------*/
  137. /*
  138. * handshake - spin reading hc until handshake completes or fails
  139. * @ptr: address of hc register to be read
  140. * @mask: bits to look at in result of read
  141. * @done: value of those bits when handshake succeeds
  142. * @usec: timeout in microseconds
  143. *
  144. * Returns negative errno, or zero on success
  145. *
  146. * Success happens when the "mask" bits have the specified value (hardware
  147. * handshake done). There are two failure modes: "usec" have passed (major
  148. * hardware flakeout), or the register reads as all-ones (hardware removed).
  149. *
  150. * That last failure should_only happen in cases like physical cardbus eject
  151. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  152. * bridge shutdown: shutting down the bridge before the devices using it.
  153. */
  154. static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
  155. u32 mask, u32 done, int usec)
  156. {
  157. u32 result;
  158. do {
  159. result = ehci_readl(ehci, ptr);
  160. if (result == ~(u32)0) /* card removed */
  161. return -ENODEV;
  162. result &= mask;
  163. if (result == done)
  164. return 0;
  165. udelay (1);
  166. usec--;
  167. } while (usec > 0);
  168. return -ETIMEDOUT;
  169. }
  170. /* check TDI/ARC silicon is in host mode */
  171. static int tdi_in_host_mode (struct ehci_hcd *ehci)
  172. {
  173. u32 tmp;
  174. tmp = ehci_readl(ehci, &ehci->regs->usbmode);
  175. return (tmp & 3) == USBMODE_CM_HC;
  176. }
  177. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  178. static int ehci_halt (struct ehci_hcd *ehci)
  179. {
  180. u32 temp = ehci_readl(ehci, &ehci->regs->status);
  181. /* disable any irqs left enabled by previous code */
  182. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  183. if (ehci_is_TDI(ehci) && tdi_in_host_mode(ehci) == 0) {
  184. return 0;
  185. }
  186. if ((temp & STS_HALT) != 0)
  187. return 0;
  188. /*
  189. * This routine gets called during probe before ehci->command
  190. * has been initialized, so we can't rely on its value.
  191. */
  192. ehci->command &= ~CMD_RUN;
  193. temp = ehci_readl(ehci, &ehci->regs->command);
  194. temp &= ~(CMD_RUN | CMD_IAAD);
  195. ehci_writel(ehci, temp, &ehci->regs->command);
  196. return handshake (ehci, &ehci->regs->status,
  197. STS_HALT, STS_HALT, 16 * 125);
  198. }
  199. /* put TDI/ARC silicon into EHCI mode */
  200. static void tdi_reset (struct ehci_hcd *ehci)
  201. {
  202. u32 tmp;
  203. tmp = ehci_readl(ehci, &ehci->regs->usbmode);
  204. tmp |= USBMODE_CM_HC;
  205. /* The default byte access to MMR space is LE after
  206. * controller reset. Set the required endian mode
  207. * for transfer buffers to match the host microprocessor
  208. */
  209. if (ehci_big_endian_mmio(ehci))
  210. tmp |= USBMODE_BE;
  211. ehci_writel(ehci, tmp, &ehci->regs->usbmode);
  212. }
  213. /* reset a non-running (STS_HALT == 1) controller */
  214. static int ehci_reset (struct ehci_hcd *ehci)
  215. {
  216. int retval;
  217. u32 command = ehci_readl(ehci, &ehci->regs->command);
  218. /* If the EHCI debug controller is active, special care must be
  219. * taken before and after a host controller reset */
  220. if (ehci->debug && !dbgp_reset_prep())
  221. ehci->debug = NULL;
  222. command |= CMD_RESET;
  223. dbg_cmd (ehci, "reset", command);
  224. ehci_writel(ehci, command, &ehci->regs->command);
  225. ehci->rh_state = EHCI_RH_HALTED;
  226. ehci->next_statechange = jiffies;
  227. retval = handshake (ehci, &ehci->regs->command,
  228. CMD_RESET, 0, 250 * 1000);
  229. if (ehci->has_hostpc) {
  230. ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
  231. &ehci->regs->usbmode_ex);
  232. ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
  233. }
  234. if (retval)
  235. return retval;
  236. if (ehci_is_TDI(ehci))
  237. tdi_reset (ehci);
  238. if (ehci->debug)
  239. dbgp_external_startup();
  240. ehci->port_c_suspend = ehci->suspended_ports =
  241. ehci->resuming_ports = 0;
  242. return retval;
  243. }
  244. /* idle the controller (from running) */
  245. static void ehci_quiesce (struct ehci_hcd *ehci)
  246. {
  247. u32 temp;
  248. if (ehci->rh_state != EHCI_RH_RUNNING)
  249. return;
  250. /* wait for any schedule enables/disables to take effect */
  251. temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
  252. handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp, 16 * 125);
  253. /* then disable anything that's still active */
  254. ehci->command &= ~(CMD_ASE | CMD_PSE);
  255. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  256. /* hardware can take 16 microframes to turn off ... */
  257. handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0, 16 * 125);
  258. }
  259. /*-------------------------------------------------------------------------*/
  260. static void end_unlink_async(struct ehci_hcd *ehci);
  261. static void ehci_work(struct ehci_hcd *ehci);
  262. static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
  263. static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
  264. #include "ehci-timer.c"
  265. #include "ehci-hub.c"
  266. #include "ehci-lpm.c"
  267. #include "ehci-mem.c"
  268. #include "ehci-q.c"
  269. #include "ehci-sched.c"
  270. #include "ehci-sysfs.c"
  271. /*-------------------------------------------------------------------------*/
  272. static void ehci_watchdog(unsigned long param)
  273. {
  274. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  275. unsigned long flags;
  276. spin_lock_irqsave(&ehci->lock, flags);
  277. /* ehci could run by timer, without IRQs ... */
  278. ehci_work (ehci);
  279. spin_unlock_irqrestore (&ehci->lock, flags);
  280. }
  281. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  282. * The firmware seems to think that powering off is a wakeup event!
  283. * This routine turns off remote wakeup and everything else, on all ports.
  284. */
  285. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  286. {
  287. int port = HCS_N_PORTS(ehci->hcs_params);
  288. while (port--)
  289. ehci_writel(ehci, PORT_RWC_BITS,
  290. &ehci->regs->port_status[port]);
  291. }
  292. /*
  293. * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  294. * Should be called with ehci->lock held.
  295. */
  296. static void ehci_silence_controller(struct ehci_hcd *ehci)
  297. {
  298. ehci_halt(ehci);
  299. ehci_turn_off_all_ports(ehci);
  300. /* make BIOS/etc use companion controller during reboot */
  301. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  302. /* unblock posted writes */
  303. ehci_readl(ehci, &ehci->regs->configured_flag);
  304. }
  305. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  306. * This forcibly disables dma and IRQs, helping kexec and other cases
  307. * where the next system software may expect clean state.
  308. */
  309. static void ehci_shutdown(struct usb_hcd *hcd)
  310. {
  311. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  312. del_timer_sync(&ehci->watchdog);
  313. spin_lock_irq(&ehci->lock);
  314. ehci->rh_state = EHCI_RH_STOPPING;
  315. ehci_silence_controller(ehci);
  316. ehci->enabled_hrtimer_events = 0;
  317. spin_unlock_irq(&ehci->lock);
  318. hrtimer_cancel(&ehci->hrtimer);
  319. }
  320. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  321. {
  322. unsigned port;
  323. if (!HCS_PPC (ehci->hcs_params))
  324. return;
  325. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  326. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  327. (void) ehci_hub_control(ehci_to_hcd(ehci),
  328. is_on ? SetPortFeature : ClearPortFeature,
  329. USB_PORT_FEAT_POWER,
  330. port--, NULL, 0);
  331. /* Flush those writes */
  332. ehci_readl(ehci, &ehci->regs->command);
  333. msleep(20);
  334. }
  335. /*-------------------------------------------------------------------------*/
  336. /*
  337. * ehci_work is called from some interrupts, timers, and so on.
  338. * it calls driver completion functions, after dropping ehci->lock.
  339. */
  340. static void ehci_work (struct ehci_hcd *ehci)
  341. {
  342. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  343. /* another CPU may drop ehci->lock during a schedule scan while
  344. * it reports urb completions. this flag guards against bogus
  345. * attempts at re-entrant schedule scanning.
  346. */
  347. if (ehci->scanning)
  348. return;
  349. ehci->scanning = 1;
  350. if (ehci->async_count)
  351. scan_async(ehci);
  352. if (ehci->next_uframe != -1)
  353. scan_periodic (ehci);
  354. ehci->scanning = 0;
  355. /* the IO watchdog guards against hardware or driver bugs that
  356. * misplace IRQs, and should let us run completely without IRQs.
  357. * such lossage has been observed on both VT6202 and VT8235.
  358. */
  359. if (ehci->rh_state == EHCI_RH_RUNNING &&
  360. (ehci->async->qh_next.ptr != NULL ||
  361. ehci->periodic_count != 0))
  362. timer_action (ehci, TIMER_IO_WATCHDOG);
  363. }
  364. /*
  365. * Called when the ehci_hcd module is removed.
  366. */
  367. static void ehci_stop (struct usb_hcd *hcd)
  368. {
  369. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  370. ehci_dbg (ehci, "stop\n");
  371. /* no more interrupts ... */
  372. del_timer_sync (&ehci->watchdog);
  373. spin_lock_irq(&ehci->lock);
  374. ehci->enabled_hrtimer_events = 0;
  375. ehci_quiesce(ehci);
  376. ehci_silence_controller(ehci);
  377. ehci_reset (ehci);
  378. spin_unlock_irq(&ehci->lock);
  379. hrtimer_cancel(&ehci->hrtimer);
  380. remove_sysfs_files(ehci);
  381. remove_debug_files (ehci);
  382. /* root hub is shut down separately (first, when possible) */
  383. spin_lock_irq (&ehci->lock);
  384. if (ehci->async)
  385. ehci_work (ehci);
  386. end_free_itds(ehci);
  387. spin_unlock_irq (&ehci->lock);
  388. ehci_mem_cleanup (ehci);
  389. if (ehci->amd_pll_fix == 1)
  390. usb_amd_dev_put();
  391. #ifdef EHCI_STATS
  392. ehci_dbg(ehci, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  393. ehci->stats.normal, ehci->stats.error, ehci->stats.iaa,
  394. ehci->stats.lost_iaa);
  395. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  396. ehci->stats.complete, ehci->stats.unlink);
  397. #endif
  398. dbg_status (ehci, "ehci_stop completed",
  399. ehci_readl(ehci, &ehci->regs->status));
  400. }
  401. /* one-time init, only for memory state */
  402. static int ehci_init(struct usb_hcd *hcd)
  403. {
  404. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  405. u32 temp;
  406. int retval;
  407. u32 hcc_params;
  408. struct ehci_qh_hw *hw;
  409. spin_lock_init(&ehci->lock);
  410. /*
  411. * keep io watchdog by default, those good HCDs could turn off it later
  412. */
  413. ehci->need_io_watchdog = 1;
  414. init_timer(&ehci->watchdog);
  415. ehci->watchdog.function = ehci_watchdog;
  416. ehci->watchdog.data = (unsigned long) ehci;
  417. hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  418. ehci->hrtimer.function = ehci_hrtimer_func;
  419. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  420. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  421. /*
  422. * by default set standard 80% (== 100 usec/uframe) max periodic
  423. * bandwidth as required by USB 2.0
  424. */
  425. ehci->uframe_periodic_max = 100;
  426. /*
  427. * hw default: 1K periodic list heads, one per frame.
  428. * periodic_size can shrink by USBCMD update if hcc_params allows.
  429. */
  430. ehci->periodic_size = DEFAULT_I_TDPS;
  431. INIT_LIST_HEAD(&ehci->cached_itd_list);
  432. INIT_LIST_HEAD(&ehci->cached_sitd_list);
  433. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  434. /* periodic schedule size can be smaller than default */
  435. switch (EHCI_TUNE_FLS) {
  436. case 0: ehci->periodic_size = 1024; break;
  437. case 1: ehci->periodic_size = 512; break;
  438. case 2: ehci->periodic_size = 256; break;
  439. default: BUG();
  440. }
  441. }
  442. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  443. return retval;
  444. /* controllers may cache some of the periodic schedule ... */
  445. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  446. ehci->i_thresh = 2 + 8;
  447. else // N microframes cached
  448. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  449. ehci->next_uframe = -1;
  450. ehci->clock_frame = -1;
  451. /*
  452. * dedicate a qh for the async ring head, since we couldn't unlink
  453. * a 'real' qh without stopping the async schedule [4.8]. use it
  454. * as the 'reclamation list head' too.
  455. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  456. * from automatically advancing to the next td after short reads.
  457. */
  458. ehci->async->qh_next.qh = NULL;
  459. hw = ehci->async->hw;
  460. hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
  461. hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
  462. #if defined(CONFIG_PPC_PS3)
  463. hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
  464. #endif
  465. hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  466. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  467. ehci->async->qh_state = QH_STATE_LINKED;
  468. hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
  469. /* clear interrupt enables, set irq latency */
  470. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  471. log2_irq_thresh = 0;
  472. temp = 1 << (16 + log2_irq_thresh);
  473. if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
  474. ehci->has_ppcd = 1;
  475. ehci_dbg(ehci, "enable per-port change event\n");
  476. temp |= CMD_PPCEE;
  477. }
  478. if (HCC_CANPARK(hcc_params)) {
  479. /* HW default park == 3, on hardware that supports it (like
  480. * NVidia and ALI silicon), maximizes throughput on the async
  481. * schedule by avoiding QH fetches between transfers.
  482. *
  483. * With fast usb storage devices and NForce2, "park" seems to
  484. * make problems: throughput reduction (!), data errors...
  485. */
  486. if (park) {
  487. park = min(park, (unsigned) 3);
  488. temp |= CMD_PARK;
  489. temp |= park << 8;
  490. }
  491. ehci_dbg(ehci, "park %d\n", park);
  492. }
  493. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  494. /* periodic schedule size can be smaller than default */
  495. temp &= ~(3 << 2);
  496. temp |= (EHCI_TUNE_FLS << 2);
  497. }
  498. if (HCC_LPM(hcc_params)) {
  499. /* support link power management EHCI 1.1 addendum */
  500. ehci_dbg(ehci, "support lpm\n");
  501. ehci->has_lpm = 1;
  502. if (hird > 0xf) {
  503. ehci_dbg(ehci, "hird %d invalid, use default 0",
  504. hird);
  505. hird = 0;
  506. }
  507. temp |= hird << 24;
  508. }
  509. ehci->command = temp;
  510. /* Accept arbitrarily long scatter-gather lists */
  511. if (!(hcd->driver->flags & HCD_LOCAL_MEM))
  512. hcd->self.sg_tablesize = ~0;
  513. return 0;
  514. }
  515. /* start HC running; it's halted, ehci_init() has been run (once) */
  516. static int ehci_run (struct usb_hcd *hcd)
  517. {
  518. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  519. u32 temp;
  520. u32 hcc_params;
  521. hcd->uses_new_polling = 1;
  522. /* EHCI spec section 4.1 */
  523. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  524. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  525. /*
  526. * hcc_params controls whether ehci->regs->segment must (!!!)
  527. * be used; it constrains QH/ITD/SITD and QTD locations.
  528. * pci_pool consistent memory always uses segment zero.
  529. * streaming mappings for I/O buffers, like pci_map_single(),
  530. * can return segments above 4GB, if the device allows.
  531. *
  532. * NOTE: the dma mask is visible through dma_supported(), so
  533. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  534. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  535. * host side drivers though.
  536. */
  537. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  538. if (HCC_64BIT_ADDR(hcc_params)) {
  539. ehci_writel(ehci, 0, &ehci->regs->segment);
  540. #if 0
  541. // this is deeply broken on almost all architectures
  542. if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
  543. ehci_info(ehci, "enabled 64bit DMA\n");
  544. #endif
  545. }
  546. // Philips, Intel, and maybe others need CMD_RUN before the
  547. // root hub will detect new devices (why?); NEC doesn't
  548. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  549. ehci->command |= CMD_RUN;
  550. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  551. dbg_cmd (ehci, "init", ehci->command);
  552. /*
  553. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  554. * are explicitly handed to companion controller(s), so no TT is
  555. * involved with the root hub. (Except where one is integrated,
  556. * and there's no companion controller unless maybe for USB OTG.)
  557. *
  558. * Turning on the CF flag will transfer ownership of all ports
  559. * from the companions to the EHCI controller. If any of the
  560. * companions are in the middle of a port reset at the time, it
  561. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  562. * guarantees that no resets are in progress. After we set CF,
  563. * a short delay lets the hardware catch up; new resets shouldn't
  564. * be started before the port switching actions could complete.
  565. */
  566. down_write(&ehci_cf_port_reset_rwsem);
  567. ehci->rh_state = EHCI_RH_RUNNING;
  568. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  569. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  570. msleep(5);
  571. up_write(&ehci_cf_port_reset_rwsem);
  572. ehci->last_periodic_enable = ktime_get_real();
  573. temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  574. ehci_info (ehci,
  575. "USB %x.%x started, EHCI %x.%02x%s\n",
  576. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  577. temp >> 8, temp & 0xff,
  578. ignore_oc ? ", overcurrent ignored" : "");
  579. ehci_writel(ehci, INTR_MASK,
  580. &ehci->regs->intr_enable); /* Turn On Interrupts */
  581. /* GRR this is run-once init(), being done every time the HC starts.
  582. * So long as they're part of class devices, we can't do it init()
  583. * since the class device isn't created that early.
  584. */
  585. create_debug_files(ehci);
  586. create_sysfs_files(ehci);
  587. return 0;
  588. }
  589. static int ehci_setup(struct usb_hcd *hcd)
  590. {
  591. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  592. int retval;
  593. ehci->regs = (void __iomem *)ehci->caps +
  594. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  595. dbg_hcs_params(ehci, "reset");
  596. dbg_hcc_params(ehci, "reset");
  597. /* cache this readonly data; minimize chip reads */
  598. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  599. ehci->sbrn = HCD_USB2;
  600. /* data structure init */
  601. retval = ehci_init(hcd);
  602. if (retval)
  603. return retval;
  604. retval = ehci_halt(ehci);
  605. if (retval)
  606. return retval;
  607. if (ehci_is_TDI(ehci))
  608. tdi_reset(ehci);
  609. ehci_reset(ehci);
  610. return 0;
  611. }
  612. /*-------------------------------------------------------------------------*/
  613. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  614. {
  615. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  616. u32 status, masked_status, pcd_status = 0, cmd;
  617. int bh;
  618. spin_lock (&ehci->lock);
  619. status = ehci_readl(ehci, &ehci->regs->status);
  620. /* e.g. cardbus physical eject */
  621. if (status == ~(u32) 0) {
  622. ehci_dbg (ehci, "device removed\n");
  623. goto dead;
  624. }
  625. /*
  626. * We don't use STS_FLR, but some controllers don't like it to
  627. * remain on, so mask it out along with the other status bits.
  628. */
  629. masked_status = status & (INTR_MASK | STS_FLR);
  630. /* Shared IRQ? */
  631. if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
  632. spin_unlock(&ehci->lock);
  633. return IRQ_NONE;
  634. }
  635. /* clear (just) interrupts */
  636. ehci_writel(ehci, masked_status, &ehci->regs->status);
  637. cmd = ehci_readl(ehci, &ehci->regs->command);
  638. bh = 0;
  639. #ifdef VERBOSE_DEBUG
  640. /* unrequested/ignored: Frame List Rollover */
  641. dbg_status (ehci, "irq", status);
  642. #endif
  643. /* INT, ERR, and IAA interrupt rates can be throttled */
  644. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  645. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  646. if (likely ((status & STS_ERR) == 0))
  647. COUNT (ehci->stats.normal);
  648. else
  649. COUNT (ehci->stats.error);
  650. bh = 1;
  651. }
  652. /* complete the unlinking of some qh [4.15.2.3] */
  653. if (status & STS_IAA) {
  654. /* Turn off the IAA watchdog */
  655. ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_IAA_WATCHDOG);
  656. /*
  657. * Mild optimization: Allow another IAAD to reset the
  658. * hrtimer, if one occurs before the next expiration.
  659. * In theory we could always cancel the hrtimer, but
  660. * tests show that about half the time it will be reset
  661. * for some other event anyway.
  662. */
  663. if (ehci->next_hrtimer_event == EHCI_HRTIMER_IAA_WATCHDOG)
  664. ++ehci->next_hrtimer_event;
  665. /* guard against (alleged) silicon errata */
  666. if (cmd & CMD_IAAD)
  667. ehci_dbg(ehci, "IAA with IAAD still set?\n");
  668. if (ehci->async_iaa) {
  669. COUNT(ehci->stats.iaa);
  670. end_unlink_async(ehci);
  671. } else
  672. ehci_dbg(ehci, "IAA with nothing unlinked?\n");
  673. }
  674. /* remote wakeup [4.3.1] */
  675. if (status & STS_PCD) {
  676. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  677. u32 ppcd = 0;
  678. /* kick root hub later */
  679. pcd_status = status;
  680. /* resume root hub? */
  681. if (ehci->rh_state == EHCI_RH_SUSPENDED)
  682. usb_hcd_resume_root_hub(hcd);
  683. /* get per-port change detect bits */
  684. if (ehci->has_ppcd)
  685. ppcd = status >> 16;
  686. while (i--) {
  687. int pstatus;
  688. /* leverage per-port change bits feature */
  689. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  690. continue;
  691. pstatus = ehci_readl(ehci,
  692. &ehci->regs->port_status[i]);
  693. if (pstatus & PORT_OWNER)
  694. continue;
  695. if (!(test_bit(i, &ehci->suspended_ports) &&
  696. ((pstatus & PORT_RESUME) ||
  697. !(pstatus & PORT_SUSPEND)) &&
  698. (pstatus & PORT_PE) &&
  699. ehci->reset_done[i] == 0))
  700. continue;
  701. /* start 20 msec resume signaling from this port,
  702. * and make khubd collect PORT_STAT_C_SUSPEND to
  703. * stop that signaling. Use 5 ms extra for safety,
  704. * like usb_port_resume() does.
  705. */
  706. ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
  707. set_bit(i, &ehci->resuming_ports);
  708. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  709. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  710. }
  711. }
  712. /* PCI errors [4.15.2.4] */
  713. if (unlikely ((status & STS_FATAL) != 0)) {
  714. ehci_err(ehci, "fatal error\n");
  715. dbg_cmd(ehci, "fatal", cmd);
  716. dbg_status(ehci, "fatal", status);
  717. dead:
  718. usb_hc_died(hcd);
  719. /* Don't let the controller do anything more */
  720. ehci->rh_state = EHCI_RH_STOPPING;
  721. ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  722. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  723. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  724. ehci_handle_controller_death(ehci);
  725. /* Handle completions when the controller stops */
  726. bh = 0;
  727. }
  728. if (bh)
  729. ehci_work (ehci);
  730. spin_unlock (&ehci->lock);
  731. if (pcd_status)
  732. usb_hcd_poll_rh_status(hcd);
  733. return IRQ_HANDLED;
  734. }
  735. /*-------------------------------------------------------------------------*/
  736. /*
  737. * non-error returns are a promise to giveback() the urb later
  738. * we drop ownership so next owner (or urb unlink) can get it
  739. *
  740. * urb + dev is in hcd.self.controller.urb_list
  741. * we're queueing TDs onto software and hardware lists
  742. *
  743. * hcd-specific init for hcpriv hasn't been done yet
  744. *
  745. * NOTE: control, bulk, and interrupt share the same code to append TDs
  746. * to a (possibly active) QH, and the same QH scanning code.
  747. */
  748. static int ehci_urb_enqueue (
  749. struct usb_hcd *hcd,
  750. struct urb *urb,
  751. gfp_t mem_flags
  752. ) {
  753. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  754. struct list_head qtd_list;
  755. INIT_LIST_HEAD (&qtd_list);
  756. switch (usb_pipetype (urb->pipe)) {
  757. case PIPE_CONTROL:
  758. /* qh_completions() code doesn't handle all the fault cases
  759. * in multi-TD control transfers. Even 1KB is rare anyway.
  760. */
  761. if (urb->transfer_buffer_length > (16 * 1024))
  762. return -EMSGSIZE;
  763. /* FALLTHROUGH */
  764. /* case PIPE_BULK: */
  765. default:
  766. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  767. return -ENOMEM;
  768. return submit_async(ehci, urb, &qtd_list, mem_flags);
  769. case PIPE_INTERRUPT:
  770. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  771. return -ENOMEM;
  772. return intr_submit(ehci, urb, &qtd_list, mem_flags);
  773. case PIPE_ISOCHRONOUS:
  774. if (urb->dev->speed == USB_SPEED_HIGH)
  775. return itd_submit (ehci, urb, mem_flags);
  776. else
  777. return sitd_submit (ehci, urb, mem_flags);
  778. }
  779. }
  780. /* remove from hardware lists
  781. * completions normally happen asynchronously
  782. */
  783. static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  784. {
  785. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  786. struct ehci_qh *qh;
  787. unsigned long flags;
  788. int rc;
  789. spin_lock_irqsave (&ehci->lock, flags);
  790. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  791. if (rc)
  792. goto done;
  793. switch (usb_pipetype (urb->pipe)) {
  794. // case PIPE_CONTROL:
  795. // case PIPE_BULK:
  796. default:
  797. qh = (struct ehci_qh *) urb->hcpriv;
  798. if (!qh)
  799. break;
  800. switch (qh->qh_state) {
  801. case QH_STATE_LINKED:
  802. case QH_STATE_COMPLETING:
  803. start_unlink_async(ehci, qh);
  804. break;
  805. case QH_STATE_UNLINK:
  806. case QH_STATE_UNLINK_WAIT:
  807. /* already started */
  808. break;
  809. case QH_STATE_IDLE:
  810. /* QH might be waiting for a Clear-TT-Buffer */
  811. qh_completions(ehci, qh);
  812. break;
  813. }
  814. break;
  815. case PIPE_INTERRUPT:
  816. qh = (struct ehci_qh *) urb->hcpriv;
  817. if (!qh)
  818. break;
  819. switch (qh->qh_state) {
  820. case QH_STATE_LINKED:
  821. case QH_STATE_COMPLETING:
  822. start_unlink_intr(ehci, qh);
  823. break;
  824. case QH_STATE_IDLE:
  825. qh_completions (ehci, qh);
  826. break;
  827. default:
  828. ehci_dbg (ehci, "bogus qh %p state %d\n",
  829. qh, qh->qh_state);
  830. goto done;
  831. }
  832. break;
  833. case PIPE_ISOCHRONOUS:
  834. // itd or sitd ...
  835. // wait till next completion, do it then.
  836. // completion irqs can wait up to 1024 msec,
  837. break;
  838. }
  839. done:
  840. spin_unlock_irqrestore (&ehci->lock, flags);
  841. return rc;
  842. }
  843. /*-------------------------------------------------------------------------*/
  844. // bulk qh holds the data toggle
  845. static void
  846. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  847. {
  848. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  849. unsigned long flags;
  850. struct ehci_qh *qh, *tmp;
  851. /* ASSERT: any requests/urbs are being unlinked */
  852. /* ASSERT: nobody can be submitting urbs for this any more */
  853. rescan:
  854. spin_lock_irqsave (&ehci->lock, flags);
  855. qh = ep->hcpriv;
  856. if (!qh)
  857. goto done;
  858. /* endpoints can be iso streams. for now, we don't
  859. * accelerate iso completions ... so spin a while.
  860. */
  861. if (qh->hw == NULL) {
  862. struct ehci_iso_stream *stream = ep->hcpriv;
  863. if (!list_empty(&stream->td_list))
  864. goto idle_timeout;
  865. /* BUG_ON(!list_empty(&stream->free_list)); */
  866. kfree(stream);
  867. goto done;
  868. }
  869. if (ehci->rh_state < EHCI_RH_RUNNING)
  870. qh->qh_state = QH_STATE_IDLE;
  871. switch (qh->qh_state) {
  872. case QH_STATE_LINKED:
  873. case QH_STATE_COMPLETING:
  874. for (tmp = ehci->async->qh_next.qh;
  875. tmp && tmp != qh;
  876. tmp = tmp->qh_next.qh)
  877. continue;
  878. /* periodic qh self-unlinks on empty, and a COMPLETING qh
  879. * may already be unlinked.
  880. */
  881. if (tmp)
  882. start_unlink_async(ehci, qh);
  883. /* FALL THROUGH */
  884. case QH_STATE_UNLINK: /* wait for hw to finish? */
  885. case QH_STATE_UNLINK_WAIT:
  886. idle_timeout:
  887. spin_unlock_irqrestore (&ehci->lock, flags);
  888. schedule_timeout_uninterruptible(1);
  889. goto rescan;
  890. case QH_STATE_IDLE: /* fully unlinked */
  891. if (qh->clearing_tt)
  892. goto idle_timeout;
  893. if (list_empty (&qh->qtd_list)) {
  894. qh_destroy(ehci, qh);
  895. break;
  896. }
  897. /* else FALL THROUGH */
  898. default:
  899. /* caller was supposed to have unlinked any requests;
  900. * that's not our job. just leak this memory.
  901. */
  902. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  903. qh, ep->desc.bEndpointAddress, qh->qh_state,
  904. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  905. break;
  906. }
  907. done:
  908. ep->hcpriv = NULL;
  909. spin_unlock_irqrestore (&ehci->lock, flags);
  910. }
  911. static void
  912. ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  913. {
  914. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  915. struct ehci_qh *qh;
  916. int eptype = usb_endpoint_type(&ep->desc);
  917. int epnum = usb_endpoint_num(&ep->desc);
  918. int is_out = usb_endpoint_dir_out(&ep->desc);
  919. unsigned long flags;
  920. if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  921. return;
  922. spin_lock_irqsave(&ehci->lock, flags);
  923. qh = ep->hcpriv;
  924. /* For Bulk and Interrupt endpoints we maintain the toggle state
  925. * in the hardware; the toggle bits in udev aren't used at all.
  926. * When an endpoint is reset by usb_clear_halt() we must reset
  927. * the toggle bit in the QH.
  928. */
  929. if (qh) {
  930. usb_settoggle(qh->dev, epnum, is_out, 0);
  931. if (!list_empty(&qh->qtd_list)) {
  932. WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  933. } else if (qh->qh_state == QH_STATE_LINKED ||
  934. qh->qh_state == QH_STATE_COMPLETING) {
  935. /* The toggle value in the QH can't be updated
  936. * while the QH is active. Unlink it now;
  937. * re-linking will call qh_refresh().
  938. */
  939. if (eptype == USB_ENDPOINT_XFER_BULK)
  940. start_unlink_async(ehci, qh);
  941. else
  942. start_unlink_intr(ehci, qh);
  943. }
  944. }
  945. spin_unlock_irqrestore(&ehci->lock, flags);
  946. }
  947. static int ehci_get_frame (struct usb_hcd *hcd)
  948. {
  949. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  950. return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
  951. }
  952. /*-------------------------------------------------------------------------*/
  953. #ifdef CONFIG_PM
  954. /* suspend/resume, section 4.3 */
  955. /* These routines handle the generic parts of controller suspend/resume */
  956. static int __maybe_unused ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  957. {
  958. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  959. if (time_before(jiffies, ehci->next_statechange))
  960. msleep(10);
  961. /*
  962. * Root hub was already suspended. Disable IRQ emission and
  963. * mark HW unaccessible. The PM and USB cores make sure that
  964. * the root hub is either suspended or stopped.
  965. */
  966. ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
  967. spin_lock_irq(&ehci->lock);
  968. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  969. (void) ehci_readl(ehci, &ehci->regs->intr_enable);
  970. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  971. spin_unlock_irq(&ehci->lock);
  972. return 0;
  973. }
  974. /* Returns 0 if power was preserved, 1 if power was lost */
  975. static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
  976. {
  977. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  978. if (time_before(jiffies, ehci->next_statechange))
  979. msleep(100);
  980. /* Mark hardware accessible again as we are back to full power by now */
  981. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  982. /*
  983. * If CF is still set and we aren't resuming from hibernation
  984. * then we maintained suspend power.
  985. * Just undo the effect of ehci_suspend().
  986. */
  987. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
  988. !hibernated) {
  989. int mask = INTR_MASK;
  990. ehci_prepare_ports_for_controller_resume(ehci);
  991. if (!hcd->self.root_hub->do_remote_wakeup)
  992. mask &= ~STS_PCD;
  993. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  994. ehci_readl(ehci, &ehci->regs->intr_enable);
  995. return 0;
  996. }
  997. /*
  998. * Else reset, to cope with power loss or resume from hibernation
  999. * having let the firmware kick in during reboot.
  1000. */
  1001. usb_root_hub_lost_power(hcd->self.root_hub);
  1002. (void) ehci_halt(ehci);
  1003. (void) ehci_reset(ehci);
  1004. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  1005. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  1006. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  1007. /* here we "know" root ports should always stay powered */
  1008. ehci_port_power(ehci, 1);
  1009. ehci->rh_state = EHCI_RH_SUSPENDED;
  1010. return 1;
  1011. }
  1012. #endif
  1013. /*-------------------------------------------------------------------------*/
  1014. /*
  1015. * The EHCI in ChipIdea HDRC cannot be a separate module or device,
  1016. * because its registers (and irq) are shared between host/gadget/otg
  1017. * functions and in order to facilitate role switching we cannot
  1018. * give the ehci driver exclusive access to those.
  1019. */
  1020. #ifndef CHIPIDEA_EHCI
  1021. MODULE_DESCRIPTION(DRIVER_DESC);
  1022. MODULE_AUTHOR (DRIVER_AUTHOR);
  1023. MODULE_LICENSE ("GPL");
  1024. #ifdef CONFIG_PCI
  1025. #include "ehci-pci.c"
  1026. #define PCI_DRIVER ehci_pci_driver
  1027. #endif
  1028. #ifdef CONFIG_USB_EHCI_FSL
  1029. #include "ehci-fsl.c"
  1030. #define PLATFORM_DRIVER ehci_fsl_driver
  1031. #endif
  1032. #ifdef CONFIG_USB_EHCI_MXC
  1033. #include "ehci-mxc.c"
  1034. #define PLATFORM_DRIVER ehci_mxc_driver
  1035. #endif
  1036. #ifdef CONFIG_USB_EHCI_SH
  1037. #include "ehci-sh.c"
  1038. #define PLATFORM_DRIVER ehci_hcd_sh_driver
  1039. #endif
  1040. #ifdef CONFIG_MIPS_ALCHEMY
  1041. #include "ehci-au1xxx.c"
  1042. #define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
  1043. #endif
  1044. #ifdef CONFIG_USB_EHCI_HCD_OMAP
  1045. #include "ehci-omap.c"
  1046. #define PLATFORM_DRIVER ehci_hcd_omap_driver
  1047. #endif
  1048. #ifdef CONFIG_PPC_PS3
  1049. #include "ehci-ps3.c"
  1050. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
  1051. #endif
  1052. #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
  1053. #include "ehci-ppc-of.c"
  1054. #define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
  1055. #endif
  1056. #ifdef CONFIG_XPS_USB_HCD_XILINX
  1057. #include "ehci-xilinx-of.c"
  1058. #define XILINX_OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
  1059. #endif
  1060. #ifdef CONFIG_PLAT_ORION
  1061. #include "ehci-orion.c"
  1062. #define PLATFORM_DRIVER ehci_orion_driver
  1063. #endif
  1064. #ifdef CONFIG_ARCH_IXP4XX
  1065. #include "ehci-ixp4xx.c"
  1066. #define PLATFORM_DRIVER ixp4xx_ehci_driver
  1067. #endif
  1068. #ifdef CONFIG_USB_W90X900_EHCI
  1069. #include "ehci-w90x900.c"
  1070. #define PLATFORM_DRIVER ehci_hcd_w90x900_driver
  1071. #endif
  1072. #ifdef CONFIG_ARCH_AT91
  1073. #include "ehci-atmel.c"
  1074. #define PLATFORM_DRIVER ehci_atmel_driver
  1075. #endif
  1076. #ifdef CONFIG_USB_OCTEON_EHCI
  1077. #include "ehci-octeon.c"
  1078. #define PLATFORM_DRIVER ehci_octeon_driver
  1079. #endif
  1080. #ifdef CONFIG_USB_CNS3XXX_EHCI
  1081. #include "ehci-cns3xxx.c"
  1082. #define PLATFORM_DRIVER cns3xxx_ehci_driver
  1083. #endif
  1084. #ifdef CONFIG_ARCH_VT8500
  1085. #include "ehci-vt8500.c"
  1086. #define PLATFORM_DRIVER vt8500_ehci_driver
  1087. #endif
  1088. #ifdef CONFIG_PLAT_SPEAR
  1089. #include "ehci-spear.c"
  1090. #define PLATFORM_DRIVER spear_ehci_hcd_driver
  1091. #endif
  1092. #ifdef CONFIG_USB_EHCI_MSM
  1093. #include "ehci-msm.c"
  1094. #define PLATFORM_DRIVER ehci_msm_driver
  1095. #endif
  1096. #ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
  1097. #include "ehci-pmcmsp.c"
  1098. #define PLATFORM_DRIVER ehci_hcd_msp_driver
  1099. #endif
  1100. #ifdef CONFIG_USB_EHCI_TEGRA
  1101. #include "ehci-tegra.c"
  1102. #define PLATFORM_DRIVER tegra_ehci_driver
  1103. #endif
  1104. #ifdef CONFIG_USB_EHCI_S5P
  1105. #include "ehci-s5p.c"
  1106. #define PLATFORM_DRIVER s5p_ehci_driver
  1107. #endif
  1108. #ifdef CONFIG_SPARC_LEON
  1109. #include "ehci-grlib.c"
  1110. #define PLATFORM_DRIVER ehci_grlib_driver
  1111. #endif
  1112. #ifdef CONFIG_CPU_XLR
  1113. #include "ehci-xls.c"
  1114. #define PLATFORM_DRIVER ehci_xls_driver
  1115. #endif
  1116. #ifdef CONFIG_USB_EHCI_MV
  1117. #include "ehci-mv.c"
  1118. #define PLATFORM_DRIVER ehci_mv_driver
  1119. #endif
  1120. #ifdef CONFIG_MACH_LOONGSON1
  1121. #include "ehci-ls1x.c"
  1122. #define PLATFORM_DRIVER ehci_ls1x_driver
  1123. #endif
  1124. #ifdef CONFIG_MIPS_SEAD3
  1125. #include "ehci-sead3.c"
  1126. #define PLATFORM_DRIVER ehci_hcd_sead3_driver
  1127. #endif
  1128. #ifdef CONFIG_USB_EHCI_HCD_PLATFORM
  1129. #include "ehci-platform.c"
  1130. #define PLATFORM_DRIVER ehci_platform_driver
  1131. #endif
  1132. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
  1133. !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \
  1134. !defined(XILINX_OF_PLATFORM_DRIVER)
  1135. #error "missing bus glue for ehci-hcd"
  1136. #endif
  1137. static int __init ehci_hcd_init(void)
  1138. {
  1139. int retval = 0;
  1140. if (usb_disabled())
  1141. return -ENODEV;
  1142. printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
  1143. set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1144. if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  1145. test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  1146. printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
  1147. " before uhci_hcd and ohci_hcd, not after\n");
  1148. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  1149. hcd_name,
  1150. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  1151. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  1152. #ifdef DEBUG
  1153. ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
  1154. if (!ehci_debug_root) {
  1155. retval = -ENOENT;
  1156. goto err_debug;
  1157. }
  1158. #endif
  1159. #ifdef PLATFORM_DRIVER
  1160. retval = platform_driver_register(&PLATFORM_DRIVER);
  1161. if (retval < 0)
  1162. goto clean0;
  1163. #endif
  1164. #ifdef PCI_DRIVER
  1165. retval = pci_register_driver(&PCI_DRIVER);
  1166. if (retval < 0)
  1167. goto clean1;
  1168. #endif
  1169. #ifdef PS3_SYSTEM_BUS_DRIVER
  1170. retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  1171. if (retval < 0)
  1172. goto clean2;
  1173. #endif
  1174. #ifdef OF_PLATFORM_DRIVER
  1175. retval = platform_driver_register(&OF_PLATFORM_DRIVER);
  1176. if (retval < 0)
  1177. goto clean3;
  1178. #endif
  1179. #ifdef XILINX_OF_PLATFORM_DRIVER
  1180. retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
  1181. if (retval < 0)
  1182. goto clean4;
  1183. #endif
  1184. return retval;
  1185. #ifdef XILINX_OF_PLATFORM_DRIVER
  1186. /* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
  1187. clean4:
  1188. #endif
  1189. #ifdef OF_PLATFORM_DRIVER
  1190. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1191. clean3:
  1192. #endif
  1193. #ifdef PS3_SYSTEM_BUS_DRIVER
  1194. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1195. clean2:
  1196. #endif
  1197. #ifdef PCI_DRIVER
  1198. pci_unregister_driver(&PCI_DRIVER);
  1199. clean1:
  1200. #endif
  1201. #ifdef PLATFORM_DRIVER
  1202. platform_driver_unregister(&PLATFORM_DRIVER);
  1203. clean0:
  1204. #endif
  1205. #ifdef DEBUG
  1206. debugfs_remove(ehci_debug_root);
  1207. ehci_debug_root = NULL;
  1208. err_debug:
  1209. #endif
  1210. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1211. return retval;
  1212. }
  1213. module_init(ehci_hcd_init);
  1214. static void __exit ehci_hcd_cleanup(void)
  1215. {
  1216. #ifdef XILINX_OF_PLATFORM_DRIVER
  1217. platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
  1218. #endif
  1219. #ifdef OF_PLATFORM_DRIVER
  1220. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1221. #endif
  1222. #ifdef PLATFORM_DRIVER
  1223. platform_driver_unregister(&PLATFORM_DRIVER);
  1224. #endif
  1225. #ifdef PCI_DRIVER
  1226. pci_unregister_driver(&PCI_DRIVER);
  1227. #endif
  1228. #ifdef PS3_SYSTEM_BUS_DRIVER
  1229. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1230. #endif
  1231. #ifdef DEBUG
  1232. debugfs_remove(ehci_debug_root);
  1233. #endif
  1234. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1235. }
  1236. module_exit(ehci_hcd_cleanup);
  1237. #endif /* CHIPIDEA_EHCI */