ehci-hcd.c 38 KB

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