ehci-hcd.c 31 KB

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