ehci-hcd.c 31 KB

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