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/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/timer.h>
  29. #include <linux/list.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/reboot.h>
  32. #include <linux/usb.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/dma-mapping.h>
  35. #include "../core/hcd.h"
  36. #include <asm/byteorder.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #include <asm/system.h>
  40. #include <asm/unaligned.h>
  41. #ifdef CONFIG_PPC_PS3
  42. #include <asm/firmware.h>
  43. #endif
  44. /*-------------------------------------------------------------------------*/
  45. /*
  46. * EHCI hc_driver implementation ... experimental, incomplete.
  47. * Based on the final 1.0 register interface specification.
  48. *
  49. * USB 2.0 shows up in upcoming www.pcmcia.org technology.
  50. * First was PCMCIA, like ISA; then CardBus, which is PCI.
  51. * Next comes "CardBay", using USB 2.0 signals.
  52. *
  53. * Contains additional contributions by Brad Hards, Rory Bolt, and others.
  54. * Special thanks to Intel and VIA for providing host controllers to
  55. * test this driver on, and Cypress (including In-System Design) for
  56. * providing early devices for those host controllers to talk to!
  57. *
  58. * HISTORY:
  59. *
  60. * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
  61. * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
  62. * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
  63. * <sojkam@centrum.cz>, updates by DB).
  64. *
  65. * 2002-11-29 Correct handling for hw async_next register.
  66. * 2002-08-06 Handling for bulk and interrupt transfers is mostly shared;
  67. * only scheduling is different, no arbitrary limitations.
  68. * 2002-07-25 Sanity check PCI reads, mostly for better cardbus support,
  69. * clean up HC run state handshaking.
  70. * 2002-05-24 Preliminary FS/LS interrupts, using scheduling shortcuts
  71. * 2002-05-11 Clear TT errors for FS/LS ctrl/bulk. Fill in some other
  72. * missing pieces: enabling 64bit dma, handoff from BIOS/SMM.
  73. * 2002-05-07 Some error path cleanups to report better errors; wmb();
  74. * use non-CVS version id; better iso bandwidth claim.
  75. * 2002-04-19 Control/bulk/interrupt submit no longer uses giveback() on
  76. * errors in submit path. Bugfixes to interrupt scheduling/processing.
  77. * 2002-03-05 Initial high-speed ISO support; reduce ITD memory; shift
  78. * more checking to generic hcd framework (db). Make it work with
  79. * Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
  80. * 2002-01-14 Minor cleanup; version synch.
  81. * 2002-01-08 Fix roothub handoff of FS/LS to companion controllers.
  82. * 2002-01-04 Control/Bulk queuing behaves.
  83. *
  84. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  85. * 2001-June Works with usb-storage and NEC EHCI on 2.4
  86. */
  87. #define DRIVER_VERSION "10 Dec 2004"
  88. #define DRIVER_AUTHOR "David Brownell"
  89. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  90. static const char hcd_name [] = "ehci_hcd";
  91. #undef EHCI_VERBOSE_DEBUG
  92. #undef EHCI_URB_TRACE
  93. #ifdef DEBUG
  94. #define EHCI_STATS
  95. #endif
  96. /* magic numbers that can affect system performance */
  97. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  98. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  99. #define EHCI_TUNE_RL_TT 0
  100. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  101. #define EHCI_TUNE_MULT_TT 1
  102. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  103. #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
  104. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  105. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  106. #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
  107. /* Initial IRQ latency: faster than hw default */
  108. static int log2_irq_thresh = 0; // 0 to 6
  109. module_param (log2_irq_thresh, int, S_IRUGO);
  110. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  111. /* initial park setting: slower than hw default */
  112. static unsigned park = 0;
  113. module_param (park, uint, S_IRUGO);
  114. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  115. /* for flakey hardware, ignore overcurrent indicators */
  116. static int ignore_oc = 0;
  117. module_param (ignore_oc, bool, S_IRUGO);
  118. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  119. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  120. /*-------------------------------------------------------------------------*/
  121. #include "ehci.h"
  122. #include "ehci-dbg.c"
  123. /*-------------------------------------------------------------------------*/
  124. /*
  125. * handshake - spin reading hc until handshake completes or fails
  126. * @ptr: address of hc register to be read
  127. * @mask: bits to look at in result of read
  128. * @done: value of those bits when handshake succeeds
  129. * @usec: timeout in microseconds
  130. *
  131. * Returns negative errno, or zero on success
  132. *
  133. * Success happens when the "mask" bits have the specified value (hardware
  134. * handshake done). There are two failure modes: "usec" have passed (major
  135. * hardware flakeout), or the register reads as all-ones (hardware removed).
  136. *
  137. * That last failure should_only happen in cases like physical cardbus eject
  138. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  139. * bridge shutdown: shutting down the bridge before the devices using it.
  140. */
  141. static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
  142. u32 mask, u32 done, int usec)
  143. {
  144. u32 result;
  145. do {
  146. result = ehci_readl(ehci, ptr);
  147. if (result == ~(u32)0) /* card removed */
  148. return -ENODEV;
  149. result &= mask;
  150. if (result == done)
  151. return 0;
  152. udelay (1);
  153. usec--;
  154. } while (usec > 0);
  155. return -ETIMEDOUT;
  156. }
  157. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  158. static int ehci_halt (struct ehci_hcd *ehci)
  159. {
  160. u32 temp = ehci_readl(ehci, &ehci->regs->status);
  161. /* disable any irqs left enabled by previous code */
  162. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  163. if ((temp & STS_HALT) != 0)
  164. return 0;
  165. temp = ehci_readl(ehci, &ehci->regs->command);
  166. temp &= ~CMD_RUN;
  167. ehci_writel(ehci, temp, &ehci->regs->command);
  168. return handshake (ehci, &ehci->regs->status,
  169. STS_HALT, STS_HALT, 16 * 125);
  170. }
  171. /* put TDI/ARC silicon into EHCI mode */
  172. static void tdi_reset (struct ehci_hcd *ehci)
  173. {
  174. u32 __iomem *reg_ptr;
  175. u32 tmp;
  176. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + 0x68);
  177. tmp = ehci_readl(ehci, reg_ptr);
  178. tmp |= 0x3;
  179. ehci_writel(ehci, tmp, reg_ptr);
  180. }
  181. /* reset a non-running (STS_HALT == 1) controller */
  182. static int ehci_reset (struct ehci_hcd *ehci)
  183. {
  184. int retval;
  185. u32 command = ehci_readl(ehci, &ehci->regs->command);
  186. command |= CMD_RESET;
  187. dbg_cmd (ehci, "reset", command);
  188. ehci_writel(ehci, command, &ehci->regs->command);
  189. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  190. ehci->next_statechange = jiffies;
  191. retval = handshake (ehci, &ehci->regs->command,
  192. CMD_RESET, 0, 250 * 1000);
  193. if (retval)
  194. return retval;
  195. if (ehci_is_TDI(ehci))
  196. tdi_reset (ehci);
  197. return retval;
  198. }
  199. /* idle the controller (from running) */
  200. static void ehci_quiesce (struct ehci_hcd *ehci)
  201. {
  202. u32 temp;
  203. #ifdef DEBUG
  204. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  205. BUG ();
  206. #endif
  207. /* wait for any schedule enables/disables to take effect */
  208. temp = ehci_readl(ehci, &ehci->regs->command) << 10;
  209. temp &= STS_ASS | STS_PSS;
  210. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  211. temp, 16 * 125) != 0) {
  212. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  213. return;
  214. }
  215. /* then disable anything that's still active */
  216. temp = ehci_readl(ehci, &ehci->regs->command);
  217. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  218. ehci_writel(ehci, temp, &ehci->regs->command);
  219. /* hardware can take 16 microframes to turn off ... */
  220. if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
  221. 0, 16 * 125) != 0) {
  222. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  223. return;
  224. }
  225. }
  226. /*-------------------------------------------------------------------------*/
  227. static void ehci_work(struct ehci_hcd *ehci);
  228. #include "ehci-hub.c"
  229. #include "ehci-mem.c"
  230. #include "ehci-q.c"
  231. #include "ehci-sched.c"
  232. /*-------------------------------------------------------------------------*/
  233. static void ehci_watchdog (unsigned long param)
  234. {
  235. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  236. unsigned long flags;
  237. spin_lock_irqsave (&ehci->lock, flags);
  238. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  239. if (ehci->reclaim) {
  240. u32 status = ehci_readl(ehci, &ehci->regs->status);
  241. if (status & STS_IAA) {
  242. ehci_vdbg (ehci, "lost IAA\n");
  243. COUNT (ehci->stats.lost_iaa);
  244. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  245. ehci->reclaim_ready = 1;
  246. }
  247. }
  248. /* stop async processing after it's idled a bit */
  249. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  250. start_unlink_async (ehci, ehci->async);
  251. /* ehci could run by timer, without IRQs ... */
  252. ehci_work (ehci);
  253. spin_unlock_irqrestore (&ehci->lock, flags);
  254. }
  255. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  256. * The firmware seems to think that powering off is a wakeup event!
  257. * This routine turns off remote wakeup and everything else, on all ports.
  258. */
  259. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  260. {
  261. int port = HCS_N_PORTS(ehci->hcs_params);
  262. while (port--)
  263. ehci_writel(ehci, PORT_RWC_BITS,
  264. &ehci->regs->port_status[port]);
  265. }
  266. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  267. * This forcibly disables dma and IRQs, helping kexec and other cases
  268. * where the next system software may expect clean state.
  269. */
  270. static void
  271. ehci_shutdown (struct usb_hcd *hcd)
  272. {
  273. struct ehci_hcd *ehci;
  274. ehci = hcd_to_ehci (hcd);
  275. (void) ehci_halt (ehci);
  276. ehci_turn_off_all_ports(ehci);
  277. /* make BIOS/etc use companion controller during reboot */
  278. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  279. /* unblock posted writes */
  280. ehci_readl(ehci, &ehci->regs->configured_flag);
  281. }
  282. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  283. {
  284. unsigned port;
  285. if (!HCS_PPC (ehci->hcs_params))
  286. return;
  287. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  288. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  289. (void) ehci_hub_control(ehci_to_hcd(ehci),
  290. is_on ? SetPortFeature : ClearPortFeature,
  291. USB_PORT_FEAT_POWER,
  292. port--, NULL, 0);
  293. msleep(20);
  294. }
  295. /*-------------------------------------------------------------------------*/
  296. /*
  297. * ehci_work is called from some interrupts, timers, and so on.
  298. * it calls driver completion functions, after dropping ehci->lock.
  299. */
  300. static void ehci_work (struct ehci_hcd *ehci)
  301. {
  302. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  303. if (ehci->reclaim_ready)
  304. end_unlink_async (ehci);
  305. /* another CPU may drop ehci->lock during a schedule scan while
  306. * it reports urb completions. this flag guards against bogus
  307. * attempts at re-entrant schedule scanning.
  308. */
  309. if (ehci->scanning)
  310. return;
  311. ehci->scanning = 1;
  312. scan_async (ehci);
  313. if (ehci->next_uframe != -1)
  314. scan_periodic (ehci);
  315. ehci->scanning = 0;
  316. /* the IO watchdog guards against hardware or driver bugs that
  317. * misplace IRQs, and should let us run completely without IRQs.
  318. * such lossage has been observed on both VT6202 and VT8235.
  319. */
  320. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  321. (ehci->async->qh_next.ptr != NULL ||
  322. ehci->periodic_sched != 0))
  323. timer_action (ehci, TIMER_IO_WATCHDOG);
  324. }
  325. static void ehci_stop (struct usb_hcd *hcd)
  326. {
  327. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  328. ehci_dbg (ehci, "stop\n");
  329. /* Turn off port power on all root hub ports. */
  330. ehci_port_power (ehci, 0);
  331. /* no more interrupts ... */
  332. del_timer_sync (&ehci->watchdog);
  333. spin_lock_irq(&ehci->lock);
  334. if (HC_IS_RUNNING (hcd->state))
  335. ehci_quiesce (ehci);
  336. ehci_reset (ehci);
  337. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  338. spin_unlock_irq(&ehci->lock);
  339. /* let companion controllers work when we aren't */
  340. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  341. remove_companion_file(ehci);
  342. remove_debug_files (ehci);
  343. /* root hub is shut down separately (first, when possible) */
  344. spin_lock_irq (&ehci->lock);
  345. if (ehci->async)
  346. ehci_work (ehci);
  347. spin_unlock_irq (&ehci->lock);
  348. ehci_mem_cleanup (ehci);
  349. #ifdef EHCI_STATS
  350. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  351. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  352. ehci->stats.lost_iaa);
  353. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  354. ehci->stats.complete, ehci->stats.unlink);
  355. #endif
  356. dbg_status (ehci, "ehci_stop completed",
  357. ehci_readl(ehci, &ehci->regs->status));
  358. }
  359. /* one-time init, only for memory state */
  360. static int ehci_init(struct usb_hcd *hcd)
  361. {
  362. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  363. u32 temp;
  364. int retval;
  365. u32 hcc_params;
  366. spin_lock_init(&ehci->lock);
  367. init_timer(&ehci->watchdog);
  368. ehci->watchdog.function = ehci_watchdog;
  369. ehci->watchdog.data = (unsigned long) ehci;
  370. /*
  371. * hw default: 1K periodic list heads, one per frame.
  372. * periodic_size can shrink by USBCMD update if hcc_params allows.
  373. */
  374. ehci->periodic_size = DEFAULT_I_TDPS;
  375. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  376. return retval;
  377. /* controllers may cache some of the periodic schedule ... */
  378. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  379. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  380. ehci->i_thresh = 8;
  381. else // N microframes cached
  382. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  383. ehci->reclaim = NULL;
  384. ehci->reclaim_ready = 0;
  385. ehci->next_uframe = -1;
  386. /*
  387. * dedicate a qh for the async ring head, since we couldn't unlink
  388. * a 'real' qh without stopping the async schedule [4.8]. use it
  389. * as the 'reclamation list head' too.
  390. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  391. * from automatically advancing to the next td after short reads.
  392. */
  393. ehci->async->qh_next.qh = NULL;
  394. ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
  395. ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
  396. ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  397. ehci->async->hw_qtd_next = EHCI_LIST_END;
  398. ehci->async->qh_state = QH_STATE_LINKED;
  399. ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
  400. /* clear interrupt enables, set irq latency */
  401. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  402. log2_irq_thresh = 0;
  403. temp = 1 << (16 + log2_irq_thresh);
  404. if (HCC_CANPARK(hcc_params)) {
  405. /* HW default park == 3, on hardware that supports it (like
  406. * NVidia and ALI silicon), maximizes throughput on the async
  407. * schedule by avoiding QH fetches between transfers.
  408. *
  409. * With fast usb storage devices and NForce2, "park" seems to
  410. * make problems: throughput reduction (!), data errors...
  411. */
  412. if (park) {
  413. park = min(park, (unsigned) 3);
  414. temp |= CMD_PARK;
  415. temp |= park << 8;
  416. }
  417. ehci_dbg(ehci, "park %d\n", park);
  418. }
  419. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  420. /* periodic schedule size can be smaller than default */
  421. temp &= ~(3 << 2);
  422. temp |= (EHCI_TUNE_FLS << 2);
  423. switch (EHCI_TUNE_FLS) {
  424. case 0: ehci->periodic_size = 1024; break;
  425. case 1: ehci->periodic_size = 512; break;
  426. case 2: ehci->periodic_size = 256; break;
  427. default: BUG();
  428. }
  429. }
  430. ehci->command = temp;
  431. return 0;
  432. }
  433. /* start HC running; it's halted, ehci_init() has been run (once) */
  434. static int ehci_run (struct usb_hcd *hcd)
  435. {
  436. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  437. int retval;
  438. u32 temp;
  439. u32 hcc_params;
  440. hcd->uses_new_polling = 1;
  441. hcd->poll_rh = 0;
  442. /* EHCI spec section 4.1 */
  443. if ((retval = ehci_reset(ehci)) != 0) {
  444. ehci_mem_cleanup(ehci);
  445. return retval;
  446. }
  447. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  448. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  449. /*
  450. * hcc_params controls whether ehci->regs->segment must (!!!)
  451. * be used; it constrains QH/ITD/SITD and QTD locations.
  452. * pci_pool consistent memory always uses segment zero.
  453. * streaming mappings for I/O buffers, like pci_map_single(),
  454. * can return segments above 4GB, if the device allows.
  455. *
  456. * NOTE: the dma mask is visible through dma_supported(), so
  457. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  458. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  459. * host side drivers though.
  460. */
  461. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  462. if (HCC_64BIT_ADDR(hcc_params)) {
  463. ehci_writel(ehci, 0, &ehci->regs->segment);
  464. #if 0
  465. // this is deeply broken on almost all architectures
  466. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  467. ehci_info(ehci, "enabled 64bit DMA\n");
  468. #endif
  469. }
  470. // Philips, Intel, and maybe others need CMD_RUN before the
  471. // root hub will detect new devices (why?); NEC doesn't
  472. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  473. ehci->command |= CMD_RUN;
  474. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  475. dbg_cmd (ehci, "init", ehci->command);
  476. /*
  477. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  478. * are explicitly handed to companion controller(s), so no TT is
  479. * involved with the root hub. (Except where one is integrated,
  480. * and there's no companion controller unless maybe for USB OTG.)
  481. */
  482. hcd->state = HC_STATE_RUNNING;
  483. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  484. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  485. temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
  486. ehci_info (ehci,
  487. "USB %x.%x started, EHCI %x.%02x, driver %s%s\n",
  488. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  489. temp >> 8, temp & 0xff, DRIVER_VERSION,
  490. ignore_oc ? ", overcurrent ignored" : "");
  491. ehci_writel(ehci, INTR_MASK,
  492. &ehci->regs->intr_enable); /* Turn On Interrupts */
  493. /* GRR this is run-once init(), being done every time the HC starts.
  494. * So long as they're part of class devices, we can't do it init()
  495. * since the class device isn't created that early.
  496. */
  497. create_debug_files(ehci);
  498. create_companion_file(ehci);
  499. return 0;
  500. }
  501. /*-------------------------------------------------------------------------*/
  502. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  503. {
  504. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  505. u32 status, pcd_status = 0;
  506. int bh;
  507. spin_lock (&ehci->lock);
  508. status = ehci_readl(ehci, &ehci->regs->status);
  509. /* e.g. cardbus physical eject */
  510. if (status == ~(u32) 0) {
  511. ehci_dbg (ehci, "device removed\n");
  512. goto dead;
  513. }
  514. status &= INTR_MASK;
  515. if (!status) { /* irq sharing? */
  516. spin_unlock(&ehci->lock);
  517. return IRQ_NONE;
  518. }
  519. /* clear (just) interrupts */
  520. ehci_writel(ehci, status, &ehci->regs->status);
  521. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  522. bh = 0;
  523. #ifdef EHCI_VERBOSE_DEBUG
  524. /* unrequested/ignored: Frame List Rollover */
  525. dbg_status (ehci, "irq", status);
  526. #endif
  527. /* INT, ERR, and IAA interrupt rates can be throttled */
  528. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  529. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  530. if (likely ((status & STS_ERR) == 0))
  531. COUNT (ehci->stats.normal);
  532. else
  533. COUNT (ehci->stats.error);
  534. bh = 1;
  535. }
  536. /* complete the unlinking of some qh [4.15.2.3] */
  537. if (status & STS_IAA) {
  538. COUNT (ehci->stats.reclaim);
  539. ehci->reclaim_ready = 1;
  540. bh = 1;
  541. }
  542. /* remote wakeup [4.3.1] */
  543. if (status & STS_PCD) {
  544. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  545. pcd_status = status;
  546. /* resume root hub? */
  547. if (!(ehci_readl(ehci, &ehci->regs->command) & CMD_RUN))
  548. usb_hcd_resume_root_hub(hcd);
  549. while (i--) {
  550. int pstatus = ehci_readl(ehci,
  551. &ehci->regs->port_status [i]);
  552. if (pstatus & PORT_OWNER)
  553. continue;
  554. if (!(pstatus & PORT_RESUME)
  555. || ehci->reset_done [i] != 0)
  556. continue;
  557. /* start 20 msec resume signaling from this port,
  558. * and make khubd collect PORT_STAT_C_SUSPEND to
  559. * stop that signaling.
  560. */
  561. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  562. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  563. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  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);