ehci-hcd.c 30 KB

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