ehci-hcd.c 28 KB

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