ehci-hcd.c 32 KB

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