ohci-hcd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  6. *
  7. * [ Initialisation is based on Linus' ]
  8. * [ uhci code and gregs ohci fragments ]
  9. * [ (C) Copyright 1999 Linus Torvalds ]
  10. * [ (C) Copyright 1999 Gregory P. Smith]
  11. *
  12. *
  13. * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
  14. * interfaces (though some non-x86 Intel chips use it). It supports
  15. * smarter hardware than UHCI. A download link for the spec available
  16. * through the http://www.usb.org website.
  17. *
  18. * History:
  19. *
  20. * 2004/03/24 LH7A404 support (Durgesh Pattamatta & Marc Singer)
  21. * 2004/02/04 use generic dma_* functions instead of pci_* (dsaxena@plexity.net)
  22. * 2003/02/24 show registers in sysfs (Kevin Brosius)
  23. *
  24. * 2002/09/03 get rid of ed hashtables, rework periodic scheduling and
  25. * bandwidth accounting; if debugging, show schedules in driverfs
  26. * 2002/07/19 fixes to management of ED and schedule state.
  27. * 2002/06/09 SA-1111 support (Christopher Hoover)
  28. * 2002/06/01 remember frame when HC won't see EDs any more; use that info
  29. * to fix urb unlink races caused by interrupt latency assumptions;
  30. * minor ED field and function naming updates
  31. * 2002/01/18 package as a patch for 2.5.3; this should match the
  32. * 2.4.17 kernel modulo some bugs being fixed.
  33. *
  34. * 2001/10/18 merge pmac cleanup (Benjamin Herrenschmidt) and bugfixes
  35. * from post-2.4.5 patches.
  36. * 2001/09/20 URB_ZERO_PACKET support; hcca_dma portability, OPTi warning
  37. * 2001/09/07 match PCI PM changes, errnos from Linus' tree
  38. * 2001/05/05 fork 2.4.5 version into "hcd" framework, cleanup, simplify;
  39. * pbook pci quirks gone (please fix pbook pci sw!) (db)
  40. *
  41. * 2001/04/08 Identify version on module load (gb)
  42. * 2001/03/24 td/ed hashing to remove bus_to_virt (Steve Longerbeam);
  43. pci_map_single (db)
  44. * 2001/03/21 td and dev/ed allocation uses new pci_pool API (db)
  45. * 2001/03/07 hcca allocation uses pci_alloc_consistent (Steve Longerbeam)
  46. *
  47. * 2000/09/26 fixed races in removing the private portion of the urb
  48. * 2000/09/07 disable bulk and control lists when unlinking the last
  49. * endpoint descriptor in order to avoid unrecoverable errors on
  50. * the Lucent chips. (rwc@sgi)
  51. * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
  52. * urb unlink probs, indentation fixes
  53. * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
  54. * device unplugs.
  55. * 2000/06/28 use PCI hotplug framework, for better power management
  56. * and for Cardbus support (David Brownell)
  57. * 2000/earlier: fixes for NEC/Lucent chips; suspend/resume handling
  58. * when the controller loses power; handle UE; cleanup; ...
  59. *
  60. * v5.2 1999/12/07 URB 3rd preview,
  61. * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
  62. * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume
  63. * i386: HUB, Keyboard, Mouse, Printer
  64. *
  65. * v4.3 1999/10/27 multiple HCs, bulk_request
  66. * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
  67. * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
  68. * v4.0 1999/08/18
  69. * v3.0 1999/06/25
  70. * v2.1 1999/05/09 code clean up
  71. * v2.0 1999/05/04
  72. * v1.0 1999/04/27 initial release
  73. *
  74. * This file is licenced under the GPL.
  75. */
  76. #include <linux/config.h>
  77. #ifdef CONFIG_USB_DEBUG
  78. # define DEBUG
  79. #else
  80. # undef DEBUG
  81. #endif
  82. #include <linux/module.h>
  83. #include <linux/moduleparam.h>
  84. #include <linux/pci.h>
  85. #include <linux/kernel.h>
  86. #include <linux/delay.h>
  87. #include <linux/ioport.h>
  88. #include <linux/sched.h>
  89. #include <linux/slab.h>
  90. #include <linux/smp_lock.h>
  91. #include <linux/errno.h>
  92. #include <linux/init.h>
  93. #include <linux/timer.h>
  94. #include <linux/list.h>
  95. #include <linux/usb.h>
  96. #include <linux/usb_otg.h>
  97. #include <linux/dma-mapping.h>
  98. #include <linux/dmapool.h>
  99. #include <linux/reboot.h>
  100. #include <asm/io.h>
  101. #include <asm/irq.h>
  102. #include <asm/system.h>
  103. #include <asm/unaligned.h>
  104. #include <asm/byteorder.h>
  105. #include "../core/hcd.h"
  106. #define DRIVER_VERSION "2005 April 22"
  107. #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
  108. #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
  109. /*-------------------------------------------------------------------------*/
  110. #undef OHCI_VERBOSE_DEBUG /* not always helpful */
  111. /* For initializing controller (mask in an HCFS mode too) */
  112. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  113. #define OHCI_INTR_INIT \
  114. (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_WDH)
  115. #ifdef __hppa__
  116. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  117. #define IR_DISABLE
  118. #endif
  119. #ifdef CONFIG_ARCH_OMAP
  120. /* OMAP doesn't support IR (no SMM; not needed) */
  121. #define IR_DISABLE
  122. #endif
  123. /*-------------------------------------------------------------------------*/
  124. static const char hcd_name [] = "ohci_hcd";
  125. #include "ohci.h"
  126. static void ohci_dump (struct ohci_hcd *ohci, int verbose);
  127. static int ohci_init (struct ohci_hcd *ohci);
  128. static void ohci_stop (struct usb_hcd *hcd);
  129. static int ohci_reboot (struct notifier_block *, unsigned long , void *);
  130. #include "ohci-hub.c"
  131. #include "ohci-dbg.c"
  132. #include "ohci-mem.c"
  133. #include "ohci-q.c"
  134. /*
  135. * On architectures with edge-triggered interrupts we must never return
  136. * IRQ_NONE.
  137. */
  138. #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
  139. #define IRQ_NOTMINE IRQ_HANDLED
  140. #else
  141. #define IRQ_NOTMINE IRQ_NONE
  142. #endif
  143. /* Some boards misreport power switching/overcurrent */
  144. static int distrust_firmware = 1;
  145. module_param (distrust_firmware, bool, 0);
  146. MODULE_PARM_DESC (distrust_firmware,
  147. "true to distrust firmware power/overcurrent setup");
  148. /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
  149. static int no_handshake = 0;
  150. module_param (no_handshake, bool, 0);
  151. MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
  152. /*-------------------------------------------------------------------------*/
  153. /*
  154. * queue up an urb for anything except the root hub
  155. */
  156. static int ohci_urb_enqueue (
  157. struct usb_hcd *hcd,
  158. struct usb_host_endpoint *ep,
  159. struct urb *urb,
  160. gfp_t mem_flags
  161. ) {
  162. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  163. struct ed *ed;
  164. urb_priv_t *urb_priv;
  165. unsigned int pipe = urb->pipe;
  166. int i, size = 0;
  167. unsigned long flags;
  168. int retval = 0;
  169. #ifdef OHCI_VERBOSE_DEBUG
  170. urb_print (urb, "SUB", usb_pipein (pipe));
  171. #endif
  172. /* every endpoint has a ed, locate and maybe (re)initialize it */
  173. if (! (ed = ed_get (ohci, ep, urb->dev, pipe, urb->interval)))
  174. return -ENOMEM;
  175. /* for the private part of the URB we need the number of TDs (size) */
  176. switch (ed->type) {
  177. case PIPE_CONTROL:
  178. /* td_submit_urb() doesn't yet handle these */
  179. if (urb->transfer_buffer_length > 4096)
  180. return -EMSGSIZE;
  181. /* 1 TD for setup, 1 for ACK, plus ... */
  182. size = 2;
  183. /* FALLTHROUGH */
  184. // case PIPE_INTERRUPT:
  185. // case PIPE_BULK:
  186. default:
  187. /* one TD for every 4096 Bytes (can be upto 8K) */
  188. size += urb->transfer_buffer_length / 4096;
  189. /* ... and for any remaining bytes ... */
  190. if ((urb->transfer_buffer_length % 4096) != 0)
  191. size++;
  192. /* ... and maybe a zero length packet to wrap it up */
  193. if (size == 0)
  194. size++;
  195. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  196. && (urb->transfer_buffer_length
  197. % usb_maxpacket (urb->dev, pipe,
  198. usb_pipeout (pipe))) == 0)
  199. size++;
  200. break;
  201. case PIPE_ISOCHRONOUS: /* number of packets from URB */
  202. size = urb->number_of_packets;
  203. break;
  204. }
  205. /* allocate the private part of the URB */
  206. urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
  207. mem_flags);
  208. if (!urb_priv)
  209. return -ENOMEM;
  210. memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (struct td *));
  211. INIT_LIST_HEAD (&urb_priv->pending);
  212. urb_priv->length = size;
  213. urb_priv->ed = ed;
  214. /* allocate the TDs (deferring hash chain updates) */
  215. for (i = 0; i < size; i++) {
  216. urb_priv->td [i] = td_alloc (ohci, mem_flags);
  217. if (!urb_priv->td [i]) {
  218. urb_priv->length = i;
  219. urb_free_priv (ohci, urb_priv);
  220. return -ENOMEM;
  221. }
  222. }
  223. spin_lock_irqsave (&ohci->lock, flags);
  224. /* don't submit to a dead HC */
  225. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  226. retval = -ENODEV;
  227. goto fail;
  228. }
  229. if (!HC_IS_RUNNING(hcd->state)) {
  230. retval = -ENODEV;
  231. goto fail;
  232. }
  233. /* in case of unlink-during-submit */
  234. spin_lock (&urb->lock);
  235. if (urb->status != -EINPROGRESS) {
  236. spin_unlock (&urb->lock);
  237. urb->hcpriv = urb_priv;
  238. finish_urb (ohci, urb, NULL);
  239. retval = 0;
  240. goto fail;
  241. }
  242. /* schedule the ed if needed */
  243. if (ed->state == ED_IDLE) {
  244. retval = ed_schedule (ohci, ed);
  245. if (retval < 0)
  246. goto fail0;
  247. if (ed->type == PIPE_ISOCHRONOUS) {
  248. u16 frame = ohci_frame_no(ohci);
  249. /* delay a few frames before the first TD */
  250. frame += max_t (u16, 8, ed->interval);
  251. frame &= ~(ed->interval - 1);
  252. frame |= ed->branch;
  253. urb->start_frame = frame;
  254. /* yes, only URB_ISO_ASAP is supported, and
  255. * urb->start_frame is never used as input.
  256. */
  257. }
  258. } else if (ed->type == PIPE_ISOCHRONOUS)
  259. urb->start_frame = ed->last_iso + ed->interval;
  260. /* fill the TDs and link them to the ed; and
  261. * enable that part of the schedule, if needed
  262. * and update count of queued periodic urbs
  263. */
  264. urb->hcpriv = urb_priv;
  265. td_submit_urb (ohci, urb);
  266. fail0:
  267. spin_unlock (&urb->lock);
  268. fail:
  269. if (retval)
  270. urb_free_priv (ohci, urb_priv);
  271. spin_unlock_irqrestore (&ohci->lock, flags);
  272. return retval;
  273. }
  274. /*
  275. * decouple the URB from the HC queues (TDs, urb_priv); it's
  276. * already marked using urb->status. reporting is always done
  277. * asynchronously, and we might be dealing with an urb that's
  278. * partially transferred, or an ED with other urbs being unlinked.
  279. */
  280. static int ohci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  281. {
  282. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  283. unsigned long flags;
  284. #ifdef OHCI_VERBOSE_DEBUG
  285. urb_print (urb, "UNLINK", 1);
  286. #endif
  287. spin_lock_irqsave (&ohci->lock, flags);
  288. if (HC_IS_RUNNING(hcd->state)) {
  289. urb_priv_t *urb_priv;
  290. /* Unless an IRQ completed the unlink while it was being
  291. * handed to us, flag it for unlink and giveback, and force
  292. * some upcoming INTR_SF to call finish_unlinks()
  293. */
  294. urb_priv = urb->hcpriv;
  295. if (urb_priv) {
  296. if (urb_priv->ed->state == ED_OPER)
  297. start_ed_unlink (ohci, urb_priv->ed);
  298. }
  299. } else {
  300. /*
  301. * with HC dead, we won't respect hc queue pointers
  302. * any more ... just clean up every urb's memory.
  303. */
  304. if (urb->hcpriv)
  305. finish_urb (ohci, urb, NULL);
  306. }
  307. spin_unlock_irqrestore (&ohci->lock, flags);
  308. return 0;
  309. }
  310. /*-------------------------------------------------------------------------*/
  311. /* frees config/altsetting state for endpoints,
  312. * including ED memory, dummy TD, and bulk/intr data toggle
  313. */
  314. static void
  315. ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  316. {
  317. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  318. unsigned long flags;
  319. struct ed *ed = ep->hcpriv;
  320. unsigned limit = 1000;
  321. /* ASSERT: any requests/urbs are being unlinked */
  322. /* ASSERT: nobody can be submitting urbs for this any more */
  323. if (!ed)
  324. return;
  325. rescan:
  326. spin_lock_irqsave (&ohci->lock, flags);
  327. if (!HC_IS_RUNNING (hcd->state)) {
  328. sanitize:
  329. ed->state = ED_IDLE;
  330. finish_unlinks (ohci, 0, NULL);
  331. }
  332. switch (ed->state) {
  333. case ED_UNLINK: /* wait for hw to finish? */
  334. /* major IRQ delivery trouble loses INTR_SF too... */
  335. if (limit-- == 0) {
  336. ohci_warn (ohci, "IRQ INTR_SF lossage\n");
  337. goto sanitize;
  338. }
  339. spin_unlock_irqrestore (&ohci->lock, flags);
  340. schedule_timeout_uninterruptible(1);
  341. goto rescan;
  342. case ED_IDLE: /* fully unlinked */
  343. if (list_empty (&ed->td_list)) {
  344. td_free (ohci, ed->dummy);
  345. ed_free (ohci, ed);
  346. break;
  347. }
  348. /* else FALL THROUGH */
  349. default:
  350. /* caller was supposed to have unlinked any requests;
  351. * that's not our job. can't recover; must leak ed.
  352. */
  353. ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
  354. ed, ep->desc.bEndpointAddress, ed->state,
  355. list_empty (&ed->td_list) ? "" : " (has tds)");
  356. td_free (ohci, ed->dummy);
  357. break;
  358. }
  359. ep->hcpriv = NULL;
  360. spin_unlock_irqrestore (&ohci->lock, flags);
  361. return;
  362. }
  363. static int ohci_get_frame (struct usb_hcd *hcd)
  364. {
  365. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  366. return ohci_frame_no(ohci);
  367. }
  368. static void ohci_usb_reset (struct ohci_hcd *ohci)
  369. {
  370. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  371. ohci->hc_control &= OHCI_CTRL_RWC;
  372. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  373. }
  374. /* reboot notifier forcibly disables IRQs and DMA, helping kexec and
  375. * other cases where the next software may expect clean state from the
  376. * "firmware". this is bus-neutral, unlike shutdown() methods.
  377. */
  378. static int
  379. ohci_reboot (struct notifier_block *block, unsigned long code, void *null)
  380. {
  381. struct ohci_hcd *ohci;
  382. ohci = container_of (block, struct ohci_hcd, reboot_notifier);
  383. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  384. ohci_usb_reset (ohci);
  385. /* flush the writes */
  386. (void) ohci_readl (ohci, &ohci->regs->control);
  387. return 0;
  388. }
  389. /*-------------------------------------------------------------------------*
  390. * HC functions
  391. *-------------------------------------------------------------------------*/
  392. /* init memory, and kick BIOS/SMM off */
  393. static int ohci_init (struct ohci_hcd *ohci)
  394. {
  395. int ret;
  396. disable (ohci);
  397. ohci->regs = ohci_to_hcd(ohci)->regs;
  398. ohci->next_statechange = jiffies;
  399. #ifndef IR_DISABLE
  400. /* SMM owns the HC? not for long! */
  401. if (!no_handshake && ohci_readl (ohci,
  402. &ohci->regs->control) & OHCI_CTRL_IR) {
  403. u32 temp;
  404. ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
  405. /* this timeout is arbitrary. we make it long, so systems
  406. * depending on usb keyboards may be usable even if the
  407. * BIOS/SMM code seems pretty broken.
  408. */
  409. temp = 500; /* arbitrary: five seconds */
  410. ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
  411. ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
  412. while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
  413. msleep (10);
  414. if (--temp == 0) {
  415. ohci_err (ohci, "USB HC takeover failed!"
  416. " (BIOS/SMM bug)\n");
  417. return -EBUSY;
  418. }
  419. }
  420. ohci_usb_reset (ohci);
  421. }
  422. #endif
  423. /* Disable HC interrupts */
  424. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  425. // flush the writes
  426. (void) ohci_readl (ohci, &ohci->regs->control);
  427. /* Read the number of ports unless overridden */
  428. if (ohci->num_ports == 0)
  429. ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
  430. if (ohci->hcca)
  431. return 0;
  432. ohci->hcca = dma_alloc_coherent (ohci_to_hcd(ohci)->self.controller,
  433. sizeof *ohci->hcca, &ohci->hcca_dma, 0);
  434. if (!ohci->hcca)
  435. return -ENOMEM;
  436. if ((ret = ohci_mem_init (ohci)) < 0)
  437. ohci_stop (ohci_to_hcd(ohci));
  438. return ret;
  439. }
  440. /*-------------------------------------------------------------------------*/
  441. /* Start an OHCI controller, set the BUS operational
  442. * resets USB and controller
  443. * enable interrupts
  444. */
  445. static int ohci_run (struct ohci_hcd *ohci)
  446. {
  447. u32 mask, temp;
  448. int first = ohci->fminterval == 0;
  449. disable (ohci);
  450. /* boot firmware should have set this up (5.1.1.3.1) */
  451. if (first) {
  452. temp = ohci_readl (ohci, &ohci->regs->fminterval);
  453. ohci->fminterval = temp & 0x3fff;
  454. if (ohci->fminterval != FI)
  455. ohci_dbg (ohci, "fminterval delta %d\n",
  456. ohci->fminterval - FI);
  457. ohci->fminterval |= FSMP (ohci->fminterval) << 16;
  458. /* also: power/overcurrent flags in roothub.a */
  459. }
  460. /* Reset USB nearly "by the book". RemoteWakeupConnected
  461. * saved if boot firmware (BIOS/SMM/...) told us it's connected
  462. * (for OHCI integrated on mainboard, it normally is)
  463. */
  464. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  465. ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n",
  466. hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
  467. ohci->hc_control);
  468. if (ohci->hc_control & OHCI_CTRL_RWC
  469. && !(ohci->flags & OHCI_QUIRK_AMD756))
  470. ohci_to_hcd(ohci)->can_wakeup = 1;
  471. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  472. case OHCI_USB_OPER:
  473. temp = 0;
  474. break;
  475. case OHCI_USB_SUSPEND:
  476. case OHCI_USB_RESUME:
  477. ohci->hc_control &= OHCI_CTRL_RWC;
  478. ohci->hc_control |= OHCI_USB_RESUME;
  479. temp = 10 /* msec wait */;
  480. break;
  481. // case OHCI_USB_RESET:
  482. default:
  483. ohci->hc_control &= OHCI_CTRL_RWC;
  484. ohci->hc_control |= OHCI_USB_RESET;
  485. temp = 50 /* msec wait */;
  486. break;
  487. }
  488. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  489. // flush the writes
  490. (void) ohci_readl (ohci, &ohci->regs->control);
  491. msleep(temp);
  492. temp = roothub_a (ohci);
  493. if (!(temp & RH_A_NPS)) {
  494. /* power down each port */
  495. for (temp = 0; temp < ohci->num_ports; temp++)
  496. ohci_writel (ohci, RH_PS_LSDA,
  497. &ohci->regs->roothub.portstatus [temp]);
  498. }
  499. // flush those writes
  500. (void) ohci_readl (ohci, &ohci->regs->control);
  501. memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
  502. /* 2msec timelimit here means no irqs/preempt */
  503. spin_lock_irq (&ohci->lock);
  504. retry:
  505. /* HC Reset requires max 10 us delay */
  506. ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
  507. temp = 30; /* ... allow extra time */
  508. while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
  509. if (--temp == 0) {
  510. spin_unlock_irq (&ohci->lock);
  511. ohci_err (ohci, "USB HC reset timed out!\n");
  512. return -1;
  513. }
  514. udelay (1);
  515. }
  516. /* now we're in the SUSPEND state ... must go OPERATIONAL
  517. * within 2msec else HC enters RESUME
  518. *
  519. * ... but some hardware won't init fmInterval "by the book"
  520. * (SiS, OPTi ...), so reset again instead. SiS doesn't need
  521. * this if we write fmInterval after we're OPERATIONAL.
  522. * Unclear about ALi, ServerWorks, and others ... this could
  523. * easily be a longstanding bug in chip init on Linux.
  524. */
  525. if (ohci->flags & OHCI_QUIRK_INITRESET) {
  526. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  527. // flush those writes
  528. (void) ohci_readl (ohci, &ohci->regs->control);
  529. }
  530. /* Tell the controller where the control and bulk lists are
  531. * The lists are empty now. */
  532. ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
  533. ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
  534. /* a reset clears this */
  535. ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
  536. periodic_reinit (ohci);
  537. /* some OHCI implementations are finicky about how they init.
  538. * bogus values here mean not even enumeration could work.
  539. */
  540. if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
  541. || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
  542. if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
  543. ohci->flags |= OHCI_QUIRK_INITRESET;
  544. ohci_dbg (ohci, "enabling initreset quirk\n");
  545. goto retry;
  546. }
  547. spin_unlock_irq (&ohci->lock);
  548. ohci_err (ohci, "init err (%08x %04x)\n",
  549. ohci_readl (ohci, &ohci->regs->fminterval),
  550. ohci_readl (ohci, &ohci->regs->periodicstart));
  551. return -EOVERFLOW;
  552. }
  553. /* start controller operations */
  554. ohci->hc_control &= OHCI_CTRL_RWC;
  555. ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  556. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  557. ohci_to_hcd(ohci)->state = HC_STATE_RUNNING;
  558. /* wake on ConnectStatusChange, matching external hubs */
  559. ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
  560. /* Choose the interrupts we care about now, others later on demand */
  561. mask = OHCI_INTR_INIT;
  562. ohci_writel (ohci, mask, &ohci->regs->intrstatus);
  563. ohci_writel (ohci, mask, &ohci->regs->intrenable);
  564. /* handle root hub init quirks ... */
  565. temp = roothub_a (ohci);
  566. temp &= ~(RH_A_PSM | RH_A_OCPM);
  567. if (ohci->flags & OHCI_QUIRK_SUPERIO) {
  568. /* NSC 87560 and maybe others */
  569. temp |= RH_A_NOCP;
  570. temp &= ~(RH_A_POTPGT | RH_A_NPS);
  571. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  572. } else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
  573. /* hub power always on; required for AMD-756 and some
  574. * Mac platforms. ganged overcurrent reporting, if any.
  575. */
  576. temp |= RH_A_NPS;
  577. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  578. }
  579. ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
  580. ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
  581. &ohci->regs->roothub.b);
  582. // flush those writes
  583. (void) ohci_readl (ohci, &ohci->regs->control);
  584. spin_unlock_irq (&ohci->lock);
  585. // POTPGT delay is bits 24-31, in 2 ms units.
  586. mdelay ((temp >> 23) & 0x1fe);
  587. ohci_to_hcd(ohci)->state = HC_STATE_RUNNING;
  588. ohci_dump (ohci, 1);
  589. if (ohci_to_hcd(ohci)->self.root_hub == NULL) {
  590. register_reboot_notifier (&ohci->reboot_notifier);
  591. create_debug_files (ohci);
  592. }
  593. return 0;
  594. }
  595. /*-------------------------------------------------------------------------*/
  596. /* an interrupt happens */
  597. static irqreturn_t ohci_irq (struct usb_hcd *hcd, struct pt_regs *ptregs)
  598. {
  599. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  600. struct ohci_regs __iomem *regs = ohci->regs;
  601. int ints;
  602. /* we can eliminate a (slow) ohci_readl()
  603. if _only_ WDH caused this irq */
  604. if ((ohci->hcca->done_head != 0)
  605. && ! (hc32_to_cpup (ohci, &ohci->hcca->done_head)
  606. & 0x01)) {
  607. ints = OHCI_INTR_WDH;
  608. /* cardbus/... hardware gone before remove() */
  609. } else if ((ints = ohci_readl (ohci, &regs->intrstatus)) == ~(u32)0) {
  610. disable (ohci);
  611. ohci_dbg (ohci, "device removed!\n");
  612. return IRQ_HANDLED;
  613. /* interrupt for some other device? */
  614. } else if ((ints &= ohci_readl (ohci, &regs->intrenable)) == 0) {
  615. return IRQ_NOTMINE;
  616. }
  617. if (ints & OHCI_INTR_UE) {
  618. disable (ohci);
  619. ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
  620. // e.g. due to PCI Master/Target Abort
  621. ohci_dump (ohci, 1);
  622. ohci_usb_reset (ohci);
  623. }
  624. if (ints & OHCI_INTR_RD) {
  625. ohci_vdbg (ohci, "resume detect\n");
  626. ohci_writel (ohci, OHCI_INTR_RD, &regs->intrstatus);
  627. if (hcd->state != HC_STATE_QUIESCING)
  628. usb_hcd_resume_root_hub(hcd);
  629. }
  630. if (ints & OHCI_INTR_WDH) {
  631. if (HC_IS_RUNNING(hcd->state))
  632. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
  633. spin_lock (&ohci->lock);
  634. dl_done_list (ohci, ptregs);
  635. spin_unlock (&ohci->lock);
  636. if (HC_IS_RUNNING(hcd->state))
  637. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
  638. }
  639. /* could track INTR_SO to reduce available PCI/... bandwidth */
  640. /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
  641. * when there's still unlinking to be done (next frame).
  642. */
  643. spin_lock (&ohci->lock);
  644. if (ohci->ed_rm_list)
  645. finish_unlinks (ohci, ohci_frame_no(ohci), ptregs);
  646. if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
  647. && HC_IS_RUNNING(hcd->state))
  648. ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
  649. spin_unlock (&ohci->lock);
  650. if (HC_IS_RUNNING(hcd->state)) {
  651. ohci_writel (ohci, ints, &regs->intrstatus);
  652. ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
  653. // flush those writes
  654. (void) ohci_readl (ohci, &ohci->regs->control);
  655. }
  656. return IRQ_HANDLED;
  657. }
  658. /*-------------------------------------------------------------------------*/
  659. static void ohci_stop (struct usb_hcd *hcd)
  660. {
  661. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  662. ohci_dbg (ohci, "stop %s controller (state 0x%02x)\n",
  663. hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
  664. hcd->state);
  665. ohci_dump (ohci, 1);
  666. flush_scheduled_work();
  667. ohci_usb_reset (ohci);
  668. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  669. remove_debug_files (ohci);
  670. unregister_reboot_notifier (&ohci->reboot_notifier);
  671. ohci_mem_cleanup (ohci);
  672. if (ohci->hcca) {
  673. dma_free_coherent (hcd->self.controller,
  674. sizeof *ohci->hcca,
  675. ohci->hcca, ohci->hcca_dma);
  676. ohci->hcca = NULL;
  677. ohci->hcca_dma = 0;
  678. }
  679. }
  680. /*-------------------------------------------------------------------------*/
  681. /* must not be called from interrupt context */
  682. #ifdef CONFIG_PM
  683. static int ohci_restart (struct ohci_hcd *ohci)
  684. {
  685. int temp;
  686. int i;
  687. struct urb_priv *priv;
  688. struct usb_device *root = ohci_to_hcd(ohci)->self.root_hub;
  689. /* mark any devices gone, so they do nothing till khubd disconnects.
  690. * recycle any "live" eds/tds (and urbs) right away.
  691. * later, khubd disconnect processing will recycle the other state,
  692. * (either as disconnect/reconnect, or maybe someday as a reset).
  693. */
  694. spin_lock_irq(&ohci->lock);
  695. disable (ohci);
  696. for (i = 0; i < root->maxchild; i++) {
  697. if (root->children [i])
  698. usb_set_device_state (root->children[i],
  699. USB_STATE_NOTATTACHED);
  700. }
  701. if (!list_empty (&ohci->pending))
  702. ohci_dbg(ohci, "abort schedule...\n");
  703. list_for_each_entry (priv, &ohci->pending, pending) {
  704. struct urb *urb = priv->td[0]->urb;
  705. struct ed *ed = priv->ed;
  706. switch (ed->state) {
  707. case ED_OPER:
  708. ed->state = ED_UNLINK;
  709. ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
  710. ed_deschedule (ohci, ed);
  711. ed->ed_next = ohci->ed_rm_list;
  712. ed->ed_prev = NULL;
  713. ohci->ed_rm_list = ed;
  714. /* FALLTHROUGH */
  715. case ED_UNLINK:
  716. break;
  717. default:
  718. ohci_dbg(ohci, "bogus ed %p state %d\n",
  719. ed, ed->state);
  720. }
  721. spin_lock (&urb->lock);
  722. urb->status = -ESHUTDOWN;
  723. spin_unlock (&urb->lock);
  724. }
  725. finish_unlinks (ohci, 0, NULL);
  726. spin_unlock_irq(&ohci->lock);
  727. /* paranoia, in case that didn't work: */
  728. /* empty the interrupt branches */
  729. for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
  730. for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
  731. /* no EDs to remove */
  732. ohci->ed_rm_list = NULL;
  733. /* empty control and bulk lists */
  734. ohci->ed_controltail = NULL;
  735. ohci->ed_bulktail = NULL;
  736. if ((temp = ohci_run (ohci)) < 0) {
  737. ohci_err (ohci, "can't restart, %d\n", temp);
  738. return temp;
  739. } else {
  740. /* here we "know" root ports should always stay powered,
  741. * and that if we try to turn them back on the root hub
  742. * will respond to CSC processing.
  743. */
  744. i = ohci->num_ports;
  745. while (i--)
  746. ohci_writel (ohci, RH_PS_PSS,
  747. &ohci->regs->roothub.portstatus [temp]);
  748. ohci_dbg (ohci, "restart complete\n");
  749. }
  750. return 0;
  751. }
  752. #endif
  753. /*-------------------------------------------------------------------------*/
  754. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  755. MODULE_AUTHOR (DRIVER_AUTHOR);
  756. MODULE_DESCRIPTION (DRIVER_INFO);
  757. MODULE_LICENSE ("GPL");
  758. #ifdef CONFIG_PCI
  759. #include "ohci-pci.c"
  760. #endif
  761. #ifdef CONFIG_SA1111
  762. #include "ohci-sa1111.c"
  763. #endif
  764. #ifdef CONFIG_ARCH_S3C2410
  765. #include "ohci-s3c2410.c"
  766. #endif
  767. #ifdef CONFIG_ARCH_OMAP
  768. #include "ohci-omap.c"
  769. #endif
  770. #ifdef CONFIG_ARCH_LH7A404
  771. #include "ohci-lh7a404.c"
  772. #endif
  773. #ifdef CONFIG_PXA27x
  774. #include "ohci-pxa27x.c"
  775. #endif
  776. #ifdef CONFIG_SOC_AU1X00
  777. #include "ohci-au1xxx.c"
  778. #endif
  779. #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
  780. #include "ohci-ppc-soc.c"
  781. #endif
  782. #if !(defined(CONFIG_PCI) \
  783. || defined(CONFIG_SA1111) \
  784. || defined(CONFIG_ARCH_S3C2410) \
  785. || defined(CONFIG_ARCH_OMAP) \
  786. || defined (CONFIG_ARCH_LH7A404) \
  787. || defined (CONFIG_PXA27x) \
  788. || defined (CONFIG_SOC_AU1X00) \
  789. || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \
  790. )
  791. #error "missing bus glue for ohci-hcd"
  792. #endif