ehci-hcd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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/smp_lock.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/timer.h>
  30. #include <linux/list.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/reboot.h>
  33. #include <linux/usb.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/dma-mapping.h>
  36. #include "../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. * HISTORY:
  57. *
  58. * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
  59. * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
  60. * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
  61. * <sojkam@centrum.cz>, updates by DB).
  62. *
  63. * 2002-11-29 Correct handling for hw async_next register.
  64. * 2002-08-06 Handling for bulk and interrupt transfers is mostly shared;
  65. * only scheduling is different, no arbitrary limitations.
  66. * 2002-07-25 Sanity check PCI reads, mostly for better cardbus support,
  67. * clean up HC run state handshaking.
  68. * 2002-05-24 Preliminary FS/LS interrupts, using scheduling shortcuts
  69. * 2002-05-11 Clear TT errors for FS/LS ctrl/bulk. Fill in some other
  70. * missing pieces: enabling 64bit dma, handoff from BIOS/SMM.
  71. * 2002-05-07 Some error path cleanups to report better errors; wmb();
  72. * use non-CVS version id; better iso bandwidth claim.
  73. * 2002-04-19 Control/bulk/interrupt submit no longer uses giveback() on
  74. * errors in submit path. Bugfixes to interrupt scheduling/processing.
  75. * 2002-03-05 Initial high-speed ISO support; reduce ITD memory; shift
  76. * more checking to generic hcd framework (db). Make it work with
  77. * Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
  78. * 2002-01-14 Minor cleanup; version synch.
  79. * 2002-01-08 Fix roothub handoff of FS/LS to companion controllers.
  80. * 2002-01-04 Control/Bulk queuing behaves.
  81. *
  82. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  83. * 2001-June Works with usb-storage and NEC EHCI on 2.4
  84. */
  85. #define DRIVER_VERSION "10 Dec 2004"
  86. #define DRIVER_AUTHOR "David Brownell"
  87. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  88. static const char hcd_name [] = "ehci_hcd";
  89. #undef EHCI_VERBOSE_DEBUG
  90. #undef EHCI_URB_TRACE
  91. #ifdef DEBUG
  92. #define EHCI_STATS
  93. #endif
  94. /* magic numbers that can affect system performance */
  95. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  96. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  97. #define EHCI_TUNE_RL_TT 0
  98. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  99. #define EHCI_TUNE_MULT_TT 1
  100. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  101. #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
  102. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  103. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  104. #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
  105. /* Initial IRQ latency: faster than hw default */
  106. static int log2_irq_thresh = 0; // 0 to 6
  107. module_param (log2_irq_thresh, int, S_IRUGO);
  108. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  109. /* initial park setting: slower than hw default */
  110. static unsigned park = 0;
  111. module_param (park, uint, S_IRUGO);
  112. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  113. /* for flakey hardware, ignore overcurrent indicators */
  114. static int ignore_oc = 0;
  115. module_param (ignore_oc, bool, S_IRUGO);
  116. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  117. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  118. /*-------------------------------------------------------------------------*/
  119. #include "ehci.h"
  120. #include "ehci-dbg.c"
  121. /*-------------------------------------------------------------------------*/
  122. /*
  123. * handshake - spin reading hc until handshake completes or fails
  124. * @ptr: address of hc register to be read
  125. * @mask: bits to look at in result of read
  126. * @done: value of those bits when handshake succeeds
  127. * @usec: timeout in microseconds
  128. *
  129. * Returns negative errno, or zero on success
  130. *
  131. * Success happens when the "mask" bits have the specified value (hardware
  132. * handshake done). There are two failure modes: "usec" have passed (major
  133. * hardware flakeout), or the register reads as all-ones (hardware removed).
  134. *
  135. * That last failure should_only happen in cases like physical cardbus eject
  136. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  137. * bridge shutdown: shutting down the bridge before the devices using it.
  138. */
  139. static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
  140. u32 mask, u32 done, int usec)
  141. {
  142. u32 result;
  143. do {
  144. result = ehci_readl(ehci, ptr);
  145. if (result == ~(u32)0) /* card removed */
  146. return -ENODEV;
  147. result &= mask;
  148. if (result == done)
  149. return 0;
  150. udelay (1);
  151. usec--;
  152. } while (usec > 0);
  153. return -ETIMEDOUT;
  154. }
  155. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  156. static int ehci_halt (struct ehci_hcd *ehci)
  157. {
  158. u32 temp = ehci_readl(ehci, &ehci->regs->status);
  159. /* disable any irqs left enabled by previous code */
  160. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  161. if ((temp & STS_HALT) != 0)
  162. return 0;
  163. temp = ehci_readl(ehci, &ehci->regs->command);
  164. temp &= ~CMD_RUN;
  165. ehci_writel(ehci, temp, &ehci->regs->command);
  166. return handshake (ehci, &ehci->regs->status,
  167. STS_HALT, STS_HALT, 16 * 125);
  168. }
  169. /* put TDI/ARC silicon into EHCI mode */
  170. static void tdi_reset (struct ehci_hcd *ehci)
  171. {
  172. u32 __iomem *reg_ptr;
  173. u32 tmp;
  174. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + 0x68);
  175. tmp = ehci_readl(ehci, reg_ptr);
  176. tmp |= 0x3;
  177. ehci_writel(ehci, tmp, reg_ptr);
  178. }
  179. /* reset a non-running (STS_HALT == 1) controller */
  180. static int ehci_reset (struct ehci_hcd *ehci)
  181. {
  182. int retval;
  183. u32 command = ehci_readl(ehci, &ehci->regs->command);
  184. command |= CMD_RESET;
  185. dbg_cmd (ehci, "reset", command);
  186. ehci_writel(ehci, command, &ehci->regs->command);
  187. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  188. ehci->next_statechange = jiffies;
  189. retval = handshake (ehci, &ehci->regs->command,
  190. CMD_RESET, 0, 250 * 1000);
  191. if (retval)
  192. return retval;
  193. if (ehci_is_TDI(ehci))
  194. tdi_reset (ehci);
  195. return retval;
  196. }
  197. /* idle the controller (from running) */
  198. static void ehci_quiesce (struct ehci_hcd *ehci)
  199. {
  200. u32 temp;
  201. #ifdef DEBUG
  202. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  203. BUG ();
  204. #endif
  205. /* wait for any schedule enables/disables to take effect */
  206. temp = ehci_readl(ehci, &ehci->regs->command) << 10;
  207. temp &= STS_ASS | STS_PSS;
  208. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  209. temp, 16 * 125) != 0) {
  210. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  211. return;
  212. }
  213. /* then disable anything that's still active */
  214. temp = ehci_readl(ehci, &ehci->regs->command);
  215. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  216. ehci_writel(ehci, temp, &ehci->regs->command);
  217. /* hardware can take 16 microframes to turn off ... */
  218. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  219. 0, 16 * 125) != 0) {
  220. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  221. return;
  222. }
  223. }
  224. /*-------------------------------------------------------------------------*/
  225. static void ehci_work(struct ehci_hcd *ehci);
  226. #include "ehci-hub.c"
  227. #include "ehci-mem.c"
  228. #include "ehci-q.c"
  229. #include "ehci-sched.c"
  230. /*-------------------------------------------------------------------------*/
  231. static void ehci_watchdog (unsigned long param)
  232. {
  233. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  234. unsigned long flags;
  235. spin_lock_irqsave (&ehci->lock, flags);
  236. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  237. if (ehci->reclaim) {
  238. u32 status = ehci_readl(ehci, &ehci->regs->status);
  239. if (status & STS_IAA) {
  240. ehci_vdbg (ehci, "lost IAA\n");
  241. COUNT (ehci->stats.lost_iaa);
  242. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  243. ehci->reclaim_ready = 1;
  244. }
  245. }
  246. /* stop async processing after it's idled a bit */
  247. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  248. start_unlink_async (ehci, ehci->async);
  249. /* ehci could run by timer, without IRQs ... */
  250. ehci_work (ehci);
  251. spin_unlock_irqrestore (&ehci->lock, flags);
  252. }
  253. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  254. * The firmware seems to think that powering off is a wakeup event!
  255. * This routine turns off remote wakeup and everything else, on all ports.
  256. */
  257. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  258. {
  259. int port = HCS_N_PORTS(ehci->hcs_params);
  260. while (port--)
  261. ehci_writel(ehci, PORT_RWC_BITS,
  262. &ehci->regs->port_status[port]);
  263. }
  264. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  265. * This forcibly disables dma and IRQs, helping kexec and other cases
  266. * where the next system software may expect clean state.
  267. */
  268. static void
  269. ehci_shutdown (struct usb_hcd *hcd)
  270. {
  271. struct ehci_hcd *ehci;
  272. ehci = hcd_to_ehci (hcd);
  273. (void) ehci_halt (ehci);
  274. ehci_turn_off_all_ports(ehci);
  275. /* make BIOS/etc use companion controller during reboot */
  276. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  277. /* unblock posted writes */
  278. ehci_readl(ehci, &ehci->regs->configured_flag);
  279. }
  280. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  281. {
  282. unsigned port;
  283. if (!HCS_PPC (ehci->hcs_params))
  284. return;
  285. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  286. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  287. (void) ehci_hub_control(ehci_to_hcd(ehci),
  288. is_on ? SetPortFeature : ClearPortFeature,
  289. USB_PORT_FEAT_POWER,
  290. port--, NULL, 0);
  291. msleep(20);
  292. }
  293. /*-------------------------------------------------------------------------*/
  294. /*
  295. * ehci_work is called from some interrupts, timers, and so on.
  296. * it calls driver completion functions, after dropping ehci->lock.
  297. */
  298. static void ehci_work (struct ehci_hcd *ehci)
  299. {
  300. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  301. if (ehci->reclaim_ready)
  302. end_unlink_async (ehci);
  303. /* another CPU may drop ehci->lock during a schedule scan while
  304. * it reports urb completions. this flag guards against bogus
  305. * attempts at re-entrant schedule scanning.
  306. */
  307. if (ehci->scanning)
  308. return;
  309. ehci->scanning = 1;
  310. scan_async (ehci);
  311. if (ehci->next_uframe != -1)
  312. scan_periodic (ehci);
  313. ehci->scanning = 0;
  314. /* the IO watchdog guards against hardware or driver bugs that
  315. * misplace IRQs, and should let us run completely without IRQs.
  316. * such lossage has been observed on both VT6202 and VT8235.
  317. */
  318. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  319. (ehci->async->qh_next.ptr != NULL ||
  320. ehci->periodic_sched != 0))
  321. timer_action (ehci, TIMER_IO_WATCHDOG);
  322. }
  323. static void ehci_stop (struct usb_hcd *hcd)
  324. {
  325. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  326. ehci_dbg (ehci, "stop\n");
  327. /* Turn off port power on all root hub ports. */
  328. ehci_port_power (ehci, 0);
  329. /* no more interrupts ... */
  330. del_timer_sync (&ehci->watchdog);
  331. spin_lock_irq(&ehci->lock);
  332. if (HC_IS_RUNNING (hcd->state))
  333. ehci_quiesce (ehci);
  334. ehci_reset (ehci);
  335. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  336. spin_unlock_irq(&ehci->lock);
  337. /* let companion controllers work when we aren't */
  338. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  339. remove_companion_file(ehci);
  340. remove_debug_files (ehci);
  341. /* root hub is shut down separately (first, when possible) */
  342. spin_lock_irq (&ehci->lock);
  343. if (ehci->async)
  344. ehci_work (ehci);
  345. spin_unlock_irq (&ehci->lock);
  346. ehci_mem_cleanup (ehci);
  347. #ifdef EHCI_STATS
  348. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  349. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  350. ehci->stats.lost_iaa);
  351. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  352. ehci->stats.complete, ehci->stats.unlink);
  353. #endif
  354. dbg_status (ehci, "ehci_stop completed",
  355. ehci_readl(ehci, &ehci->regs->status));
  356. }
  357. /* one-time init, only for memory state */
  358. static int ehci_init(struct usb_hcd *hcd)
  359. {
  360. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  361. u32 temp;
  362. int retval;
  363. u32 hcc_params;
  364. spin_lock_init(&ehci->lock);
  365. init_timer(&ehci->watchdog);
  366. ehci->watchdog.function = ehci_watchdog;
  367. ehci->watchdog.data = (unsigned long) ehci;
  368. /*
  369. * hw default: 1K periodic list heads, one per frame.
  370. * periodic_size can shrink by USBCMD update if hcc_params allows.
  371. */
  372. ehci->periodic_size = DEFAULT_I_TDPS;
  373. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  374. return retval;
  375. /* controllers may cache some of the periodic schedule ... */
  376. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  377. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  378. ehci->i_thresh = 8;
  379. else // N microframes cached
  380. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  381. ehci->reclaim = NULL;
  382. ehci->reclaim_ready = 0;
  383. ehci->next_uframe = -1;
  384. /*
  385. * dedicate a qh for the async ring head, since we couldn't unlink
  386. * a 'real' qh without stopping the async schedule [4.8]. use it
  387. * as the 'reclamation list head' too.
  388. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  389. * from automatically advancing to the next td after short reads.
  390. */
  391. ehci->async->qh_next.qh = NULL;
  392. ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
  393. ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
  394. ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  395. ehci->async->hw_qtd_next = EHCI_LIST_END;
  396. ehci->async->qh_state = QH_STATE_LINKED;
  397. ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
  398. /* clear interrupt enables, set irq latency */
  399. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  400. log2_irq_thresh = 0;
  401. temp = 1 << (16 + log2_irq_thresh);
  402. if (HCC_CANPARK(hcc_params)) {
  403. /* HW default park == 3, on hardware that supports it (like
  404. * NVidia and ALI silicon), maximizes throughput on the async
  405. * schedule by avoiding QH fetches between transfers.
  406. *
  407. * With fast usb storage devices and NForce2, "park" seems to
  408. * make problems: throughput reduction (!), data errors...
  409. */
  410. if (park) {
  411. park = min(park, (unsigned) 3);
  412. temp |= CMD_PARK;
  413. temp |= park << 8;
  414. }
  415. ehci_dbg(ehci, "park %d\n", park);
  416. }
  417. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  418. /* periodic schedule size can be smaller than default */
  419. temp &= ~(3 << 2);
  420. temp |= (EHCI_TUNE_FLS << 2);
  421. switch (EHCI_TUNE_FLS) {
  422. case 0: ehci->periodic_size = 1024; break;
  423. case 1: ehci->periodic_size = 512; break;
  424. case 2: ehci->periodic_size = 256; break;
  425. default: BUG();
  426. }
  427. }
  428. ehci->command = temp;
  429. return 0;
  430. }
  431. /* start HC running; it's halted, ehci_init() has been run (once) */
  432. static int ehci_run (struct usb_hcd *hcd)
  433. {
  434. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  435. int retval;
  436. u32 temp;
  437. u32 hcc_params;
  438. hcd->uses_new_polling = 1;
  439. hcd->poll_rh = 0;
  440. /* EHCI spec section 4.1 */
  441. if ((retval = ehci_reset(ehci)) != 0) {
  442. ehci_mem_cleanup(ehci);
  443. return retval;
  444. }
  445. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  446. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  447. /*
  448. * hcc_params controls whether ehci->regs->segment must (!!!)
  449. * be used; it constrains QH/ITD/SITD and QTD locations.
  450. * pci_pool consistent memory always uses segment zero.
  451. * streaming mappings for I/O buffers, like pci_map_single(),
  452. * can return segments above 4GB, if the device allows.
  453. *
  454. * NOTE: the dma mask is visible through dma_supported(), so
  455. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  456. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  457. * host side drivers though.
  458. */
  459. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  460. if (HCC_64BIT_ADDR(hcc_params)) {
  461. ehci_writel(ehci, 0, &ehci->regs->segment);
  462. #if 0
  463. // this is deeply broken on almost all architectures
  464. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  465. ehci_info(ehci, "enabled 64bit DMA\n");
  466. #endif
  467. }
  468. // Philips, Intel, and maybe others need CMD_RUN before the
  469. // root hub will detect new devices (why?); NEC doesn't
  470. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  471. ehci->command |= CMD_RUN;
  472. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  473. dbg_cmd (ehci, "init", ehci->command);
  474. /*
  475. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  476. * are explicitly handed to companion controller(s), so no TT is
  477. * involved with the root hub. (Except where one is integrated,
  478. * and there's no companion controller unless maybe for USB OTG.)
  479. */
  480. hcd->state = HC_STATE_RUNNING;
  481. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  482. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  483. temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
  484. ehci_info (ehci,
  485. "USB %x.%x started, EHCI %x.%02x, driver %s%s\n",
  486. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  487. temp >> 8, temp & 0xff, DRIVER_VERSION,
  488. ignore_oc ? ", overcurrent ignored" : "");
  489. ehci_writel(ehci, INTR_MASK,
  490. &ehci->regs->intr_enable); /* Turn On Interrupts */
  491. /* GRR this is run-once init(), being done every time the HC starts.
  492. * So long as they're part of class devices, we can't do it init()
  493. * since the class device isn't created that early.
  494. */
  495. create_debug_files(ehci);
  496. create_companion_file(ehci);
  497. return 0;
  498. }
  499. /*-------------------------------------------------------------------------*/
  500. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  501. {
  502. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  503. u32 status, pcd_status = 0;
  504. int bh;
  505. spin_lock (&ehci->lock);
  506. status = ehci_readl(ehci, &ehci->regs->status);
  507. /* e.g. cardbus physical eject */
  508. if (status == ~(u32) 0) {
  509. ehci_dbg (ehci, "device removed\n");
  510. goto dead;
  511. }
  512. status &= INTR_MASK;
  513. if (!status) { /* irq sharing? */
  514. spin_unlock(&ehci->lock);
  515. return IRQ_NONE;
  516. }
  517. /* clear (just) interrupts */
  518. ehci_writel(ehci, status, &ehci->regs->status);
  519. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  520. bh = 0;
  521. #ifdef EHCI_VERBOSE_DEBUG
  522. /* unrequested/ignored: Frame List Rollover */
  523. dbg_status (ehci, "irq", status);
  524. #endif
  525. /* INT, ERR, and IAA interrupt rates can be throttled */
  526. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  527. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  528. if (likely ((status & STS_ERR) == 0))
  529. COUNT (ehci->stats.normal);
  530. else
  531. COUNT (ehci->stats.error);
  532. bh = 1;
  533. }
  534. /* complete the unlinking of some qh [4.15.2.3] */
  535. if (status & STS_IAA) {
  536. COUNT (ehci->stats.reclaim);
  537. ehci->reclaim_ready = 1;
  538. bh = 1;
  539. }
  540. /* remote wakeup [4.3.1] */
  541. if (status & STS_PCD) {
  542. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  543. pcd_status = status;
  544. /* resume root hub? */
  545. if (!(ehci_readl(ehci, &ehci->regs->command) & CMD_RUN))
  546. usb_hcd_resume_root_hub(hcd);
  547. while (i--) {
  548. int pstatus = ehci_readl(ehci,
  549. &ehci->regs->port_status [i]);
  550. if (pstatus & PORT_OWNER)
  551. continue;
  552. if (!(pstatus & PORT_RESUME)
  553. || ehci->reset_done [i] != 0)
  554. continue;
  555. /* start 20 msec resume signaling from this port,
  556. * and make khubd collect PORT_STAT_C_SUSPEND to
  557. * stop that signaling.
  558. */
  559. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  560. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  561. }
  562. }
  563. /* PCI errors [4.15.2.4] */
  564. if (unlikely ((status & STS_FATAL) != 0)) {
  565. /* bogus "fatal" IRQs appear on some chips... why? */
  566. status = ehci_readl(ehci, &ehci->regs->status);
  567. dbg_cmd (ehci, "fatal", ehci_readl(ehci,
  568. &ehci->regs->command));
  569. dbg_status (ehci, "fatal", status);
  570. if (status & STS_HALT) {
  571. ehci_err (ehci, "fatal error\n");
  572. dead:
  573. ehci_reset (ehci);
  574. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  575. /* generic layer kills/unlinks all urbs, then
  576. * uses ehci_stop to clean up the rest
  577. */
  578. bh = 1;
  579. }
  580. }
  581. if (bh)
  582. ehci_work (ehci);
  583. spin_unlock (&ehci->lock);
  584. if (pcd_status & STS_PCD)
  585. usb_hcd_poll_rh_status(hcd);
  586. return IRQ_HANDLED;
  587. }
  588. /*-------------------------------------------------------------------------*/
  589. /*
  590. * non-error returns are a promise to giveback() the urb later
  591. * we drop ownership so next owner (or urb unlink) can get it
  592. *
  593. * urb + dev is in hcd.self.controller.urb_list
  594. * we're queueing TDs onto software and hardware lists
  595. *
  596. * hcd-specific init for hcpriv hasn't been done yet
  597. *
  598. * NOTE: control, bulk, and interrupt share the same code to append TDs
  599. * to a (possibly active) QH, and the same QH scanning code.
  600. */
  601. static int ehci_urb_enqueue (
  602. struct usb_hcd *hcd,
  603. struct usb_host_endpoint *ep,
  604. struct urb *urb,
  605. gfp_t mem_flags
  606. ) {
  607. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  608. struct list_head qtd_list;
  609. INIT_LIST_HEAD (&qtd_list);
  610. switch (usb_pipetype (urb->pipe)) {
  611. // case PIPE_CONTROL:
  612. // case PIPE_BULK:
  613. default:
  614. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  615. return -ENOMEM;
  616. return submit_async (ehci, ep, urb, &qtd_list, mem_flags);
  617. case PIPE_INTERRUPT:
  618. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  619. return -ENOMEM;
  620. return intr_submit (ehci, ep, urb, &qtd_list, mem_flags);
  621. case PIPE_ISOCHRONOUS:
  622. if (urb->dev->speed == USB_SPEED_HIGH)
  623. return itd_submit (ehci, urb, mem_flags);
  624. else
  625. return sitd_submit (ehci, urb, mem_flags);
  626. }
  627. }
  628. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  629. {
  630. /* if we need to use IAA and it's busy, defer */
  631. if (qh->qh_state == QH_STATE_LINKED
  632. && ehci->reclaim
  633. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) {
  634. struct ehci_qh *last;
  635. for (last = ehci->reclaim;
  636. last->reclaim;
  637. last = last->reclaim)
  638. continue;
  639. qh->qh_state = QH_STATE_UNLINK_WAIT;
  640. last->reclaim = qh;
  641. /* bypass IAA if the hc can't care */
  642. } else if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && ehci->reclaim)
  643. end_unlink_async (ehci);
  644. /* something else might have unlinked the qh by now */
  645. if (qh->qh_state == QH_STATE_LINKED)
  646. start_unlink_async (ehci, qh);
  647. }
  648. /* remove from hardware lists
  649. * completions normally happen asynchronously
  650. */
  651. static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  652. {
  653. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  654. struct ehci_qh *qh;
  655. unsigned long flags;
  656. spin_lock_irqsave (&ehci->lock, flags);
  657. switch (usb_pipetype (urb->pipe)) {
  658. // case PIPE_CONTROL:
  659. // case PIPE_BULK:
  660. default:
  661. qh = (struct ehci_qh *) urb->hcpriv;
  662. if (!qh)
  663. break;
  664. unlink_async (ehci, qh);
  665. break;
  666. case PIPE_INTERRUPT:
  667. qh = (struct ehci_qh *) urb->hcpriv;
  668. if (!qh)
  669. break;
  670. switch (qh->qh_state) {
  671. case QH_STATE_LINKED:
  672. intr_deschedule (ehci, qh);
  673. /* FALL THROUGH */
  674. case QH_STATE_IDLE:
  675. qh_completions (ehci, qh);
  676. break;
  677. default:
  678. ehci_dbg (ehci, "bogus qh %p state %d\n",
  679. qh, qh->qh_state);
  680. goto done;
  681. }
  682. /* reschedule QH iff another request is queued */
  683. if (!list_empty (&qh->qtd_list)
  684. && HC_IS_RUNNING (hcd->state)) {
  685. int status;
  686. status = qh_schedule (ehci, qh);
  687. spin_unlock_irqrestore (&ehci->lock, flags);
  688. if (status != 0) {
  689. // shouldn't happen often, but ...
  690. // FIXME kill those tds' urbs
  691. err ("can't reschedule qh %p, err %d",
  692. qh, status);
  693. }
  694. return status;
  695. }
  696. break;
  697. case PIPE_ISOCHRONOUS:
  698. // itd or sitd ...
  699. // wait till next completion, do it then.
  700. // completion irqs can wait up to 1024 msec,
  701. break;
  702. }
  703. done:
  704. spin_unlock_irqrestore (&ehci->lock, flags);
  705. return 0;
  706. }
  707. /*-------------------------------------------------------------------------*/
  708. // bulk qh holds the data toggle
  709. static void
  710. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  711. {
  712. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  713. unsigned long flags;
  714. struct ehci_qh *qh, *tmp;
  715. /* ASSERT: any requests/urbs are being unlinked */
  716. /* ASSERT: nobody can be submitting urbs for this any more */
  717. rescan:
  718. spin_lock_irqsave (&ehci->lock, flags);
  719. qh = ep->hcpriv;
  720. if (!qh)
  721. goto done;
  722. /* endpoints can be iso streams. for now, we don't
  723. * accelerate iso completions ... so spin a while.
  724. */
  725. if (qh->hw_info1 == 0) {
  726. ehci_vdbg (ehci, "iso delay\n");
  727. goto idle_timeout;
  728. }
  729. if (!HC_IS_RUNNING (hcd->state))
  730. qh->qh_state = QH_STATE_IDLE;
  731. switch (qh->qh_state) {
  732. case QH_STATE_LINKED:
  733. for (tmp = ehci->async->qh_next.qh;
  734. tmp && tmp != qh;
  735. tmp = tmp->qh_next.qh)
  736. continue;
  737. /* periodic qh self-unlinks on empty */
  738. if (!tmp)
  739. goto nogood;
  740. unlink_async (ehci, qh);
  741. /* FALL THROUGH */
  742. case QH_STATE_UNLINK: /* wait for hw to finish? */
  743. idle_timeout:
  744. spin_unlock_irqrestore (&ehci->lock, flags);
  745. schedule_timeout_uninterruptible(1);
  746. goto rescan;
  747. case QH_STATE_IDLE: /* fully unlinked */
  748. if (list_empty (&qh->qtd_list)) {
  749. qh_put (qh);
  750. break;
  751. }
  752. /* else FALL THROUGH */
  753. default:
  754. nogood:
  755. /* caller was supposed to have unlinked any requests;
  756. * that's not our job. just leak this memory.
  757. */
  758. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  759. qh, ep->desc.bEndpointAddress, qh->qh_state,
  760. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  761. break;
  762. }
  763. ep->hcpriv = NULL;
  764. done:
  765. spin_unlock_irqrestore (&ehci->lock, flags);
  766. return;
  767. }
  768. static int ehci_get_frame (struct usb_hcd *hcd)
  769. {
  770. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  771. return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) %
  772. ehci->periodic_size;
  773. }
  774. /*-------------------------------------------------------------------------*/
  775. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  776. MODULE_DESCRIPTION (DRIVER_INFO);
  777. MODULE_AUTHOR (DRIVER_AUTHOR);
  778. MODULE_LICENSE ("GPL");
  779. #ifdef CONFIG_PCI
  780. #include "ehci-pci.c"
  781. #define PCI_DRIVER ehci_pci_driver
  782. #endif
  783. #ifdef CONFIG_MPC834x
  784. #include "ehci-fsl.c"
  785. #define PLATFORM_DRIVER ehci_fsl_driver
  786. #endif
  787. #ifdef CONFIG_SOC_AU1200
  788. #include "ehci-au1xxx.c"
  789. #define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
  790. #endif
  791. #ifdef CONFIG_PPC_PS3
  792. #include "ehci-ps3.c"
  793. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_sb_driver
  794. #endif
  795. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
  796. !defined(PS3_SYSTEM_BUS_DRIVER)
  797. #error "missing bus glue for ehci-hcd"
  798. #endif
  799. static int __init ehci_hcd_init(void)
  800. {
  801. int retval = 0;
  802. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  803. hcd_name,
  804. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  805. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  806. #ifdef PLATFORM_DRIVER
  807. retval = platform_driver_register(&PLATFORM_DRIVER);
  808. if (retval < 0)
  809. return retval;
  810. #endif
  811. #ifdef PCI_DRIVER
  812. retval = pci_register_driver(&PCI_DRIVER);
  813. if (retval < 0) {
  814. #ifdef PLATFORM_DRIVER
  815. platform_driver_unregister(&PLATFORM_DRIVER);
  816. #endif
  817. return retval;
  818. }
  819. #endif
  820. #ifdef PS3_SYSTEM_BUS_DRIVER
  821. retval = ps3_system_bus_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  822. if (retval < 0) {
  823. #ifdef PLATFORM_DRIVER
  824. platform_driver_unregister(&PLATFORM_DRIVER);
  825. #endif
  826. #ifdef PCI_DRIVER
  827. pci_unregister_driver(&PCI_DRIVER);
  828. #endif
  829. return retval;
  830. }
  831. #endif
  832. return retval;
  833. }
  834. module_init(ehci_hcd_init);
  835. static void __exit ehci_hcd_cleanup(void)
  836. {
  837. #ifdef PLATFORM_DRIVER
  838. platform_driver_unregister(&PLATFORM_DRIVER);
  839. #endif
  840. #ifdef PCI_DRIVER
  841. pci_unregister_driver(&PCI_DRIVER);
  842. #endif
  843. #ifdef PS3_SYSTEM_BUS_DRIVER
  844. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  845. #endif
  846. }
  847. module_exit(ehci_hcd_cleanup);