ohci-hcd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. * This file is licenced under the GPL.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/pci.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/ioport.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/errno.h>
  30. #include <linux/init.h>
  31. #include <linux/timer.h>
  32. #include <linux/list.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/dmapool.h>
  37. #include <linux/reboot.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/unaligned.h>
  42. #include <asm/byteorder.h>
  43. #ifdef CONFIG_PPC_PS3
  44. #include <asm/firmware.h>
  45. #endif
  46. #include "../core/hcd.h"
  47. #define DRIVER_VERSION "2006 August 04"
  48. #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
  49. #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
  50. /*-------------------------------------------------------------------------*/
  51. #undef OHCI_VERBOSE_DEBUG /* not always helpful */
  52. /* For initializing controller (mask in an HCFS mode too) */
  53. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  54. #define OHCI_INTR_INIT \
  55. (OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
  56. | OHCI_INTR_RD | OHCI_INTR_WDH)
  57. #ifdef __hppa__
  58. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  59. #define IR_DISABLE
  60. #endif
  61. #ifdef CONFIG_ARCH_OMAP
  62. /* OMAP doesn't support IR (no SMM; not needed) */
  63. #define IR_DISABLE
  64. #endif
  65. /*-------------------------------------------------------------------------*/
  66. static const char hcd_name [] = "ohci_hcd";
  67. #define STATECHANGE_DELAY msecs_to_jiffies(300)
  68. #include "ohci.h"
  69. static void ohci_dump (struct ohci_hcd *ohci, int verbose);
  70. static int ohci_init (struct ohci_hcd *ohci);
  71. static void ohci_stop (struct usb_hcd *hcd);
  72. #include "ohci-hub.c"
  73. #include "ohci-dbg.c"
  74. #include "ohci-mem.c"
  75. #include "ohci-q.c"
  76. /*
  77. * On architectures with edge-triggered interrupts we must never return
  78. * IRQ_NONE.
  79. */
  80. #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
  81. #define IRQ_NOTMINE IRQ_HANDLED
  82. #else
  83. #define IRQ_NOTMINE IRQ_NONE
  84. #endif
  85. /* Some boards misreport power switching/overcurrent */
  86. static int distrust_firmware = 1;
  87. module_param (distrust_firmware, bool, 0);
  88. MODULE_PARM_DESC (distrust_firmware,
  89. "true to distrust firmware power/overcurrent setup");
  90. /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
  91. static int no_handshake = 0;
  92. module_param (no_handshake, bool, 0);
  93. MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
  94. /*-------------------------------------------------------------------------*/
  95. /*
  96. * queue up an urb for anything except the root hub
  97. */
  98. static int ohci_urb_enqueue (
  99. struct usb_hcd *hcd,
  100. struct usb_host_endpoint *ep,
  101. struct urb *urb,
  102. gfp_t mem_flags
  103. ) {
  104. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  105. struct ed *ed;
  106. urb_priv_t *urb_priv;
  107. unsigned int pipe = urb->pipe;
  108. int i, size = 0;
  109. unsigned long flags;
  110. int retval = 0;
  111. #ifdef OHCI_VERBOSE_DEBUG
  112. urb_print (urb, "SUB", usb_pipein (pipe));
  113. #endif
  114. /* every endpoint has a ed, locate and maybe (re)initialize it */
  115. if (! (ed = ed_get (ohci, ep, urb->dev, pipe, urb->interval)))
  116. return -ENOMEM;
  117. /* for the private part of the URB we need the number of TDs (size) */
  118. switch (ed->type) {
  119. case PIPE_CONTROL:
  120. /* td_submit_urb() doesn't yet handle these */
  121. if (urb->transfer_buffer_length > 4096)
  122. return -EMSGSIZE;
  123. /* 1 TD for setup, 1 for ACK, plus ... */
  124. size = 2;
  125. /* FALLTHROUGH */
  126. // case PIPE_INTERRUPT:
  127. // case PIPE_BULK:
  128. default:
  129. /* one TD for every 4096 Bytes (can be upto 8K) */
  130. size += urb->transfer_buffer_length / 4096;
  131. /* ... and for any remaining bytes ... */
  132. if ((urb->transfer_buffer_length % 4096) != 0)
  133. size++;
  134. /* ... and maybe a zero length packet to wrap it up */
  135. if (size == 0)
  136. size++;
  137. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  138. && (urb->transfer_buffer_length
  139. % usb_maxpacket (urb->dev, pipe,
  140. usb_pipeout (pipe))) == 0)
  141. size++;
  142. break;
  143. case PIPE_ISOCHRONOUS: /* number of packets from URB */
  144. size = urb->number_of_packets;
  145. break;
  146. }
  147. /* allocate the private part of the URB */
  148. urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
  149. mem_flags);
  150. if (!urb_priv)
  151. return -ENOMEM;
  152. memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (struct td *));
  153. INIT_LIST_HEAD (&urb_priv->pending);
  154. urb_priv->length = size;
  155. urb_priv->ed = ed;
  156. /* allocate the TDs (deferring hash chain updates) */
  157. for (i = 0; i < size; i++) {
  158. urb_priv->td [i] = td_alloc (ohci, mem_flags);
  159. if (!urb_priv->td [i]) {
  160. urb_priv->length = i;
  161. urb_free_priv (ohci, urb_priv);
  162. return -ENOMEM;
  163. }
  164. }
  165. spin_lock_irqsave (&ohci->lock, flags);
  166. /* don't submit to a dead HC */
  167. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  168. retval = -ENODEV;
  169. goto fail;
  170. }
  171. if (!HC_IS_RUNNING(hcd->state)) {
  172. retval = -ENODEV;
  173. goto fail;
  174. }
  175. /* in case of unlink-during-submit */
  176. spin_lock (&urb->lock);
  177. if (urb->status != -EINPROGRESS) {
  178. spin_unlock (&urb->lock);
  179. urb->hcpriv = urb_priv;
  180. finish_urb (ohci, urb);
  181. retval = 0;
  182. goto fail;
  183. }
  184. /* schedule the ed if needed */
  185. if (ed->state == ED_IDLE) {
  186. retval = ed_schedule (ohci, ed);
  187. if (retval < 0)
  188. goto fail0;
  189. if (ed->type == PIPE_ISOCHRONOUS) {
  190. u16 frame = ohci_frame_no(ohci);
  191. /* delay a few frames before the first TD */
  192. frame += max_t (u16, 8, ed->interval);
  193. frame &= ~(ed->interval - 1);
  194. frame |= ed->branch;
  195. urb->start_frame = frame;
  196. /* yes, only URB_ISO_ASAP is supported, and
  197. * urb->start_frame is never used as input.
  198. */
  199. }
  200. } else if (ed->type == PIPE_ISOCHRONOUS)
  201. urb->start_frame = ed->last_iso + ed->interval;
  202. /* fill the TDs and link them to the ed; and
  203. * enable that part of the schedule, if needed
  204. * and update count of queued periodic urbs
  205. */
  206. urb->hcpriv = urb_priv;
  207. td_submit_urb (ohci, urb);
  208. fail0:
  209. spin_unlock (&urb->lock);
  210. fail:
  211. if (retval)
  212. urb_free_priv (ohci, urb_priv);
  213. spin_unlock_irqrestore (&ohci->lock, flags);
  214. return retval;
  215. }
  216. /*
  217. * decouple the URB from the HC queues (TDs, urb_priv); it's
  218. * already marked using urb->status. reporting is always done
  219. * asynchronously, and we might be dealing with an urb that's
  220. * partially transferred, or an ED with other urbs being unlinked.
  221. */
  222. static int ohci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  223. {
  224. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  225. unsigned long flags;
  226. #ifdef OHCI_VERBOSE_DEBUG
  227. urb_print (urb, "UNLINK", 1);
  228. #endif
  229. spin_lock_irqsave (&ohci->lock, flags);
  230. if (HC_IS_RUNNING(hcd->state)) {
  231. urb_priv_t *urb_priv;
  232. /* Unless an IRQ completed the unlink while it was being
  233. * handed to us, flag it for unlink and giveback, and force
  234. * some upcoming INTR_SF to call finish_unlinks()
  235. */
  236. urb_priv = urb->hcpriv;
  237. if (urb_priv) {
  238. if (urb_priv->ed->state == ED_OPER)
  239. start_ed_unlink (ohci, urb_priv->ed);
  240. }
  241. } else {
  242. /*
  243. * with HC dead, we won't respect hc queue pointers
  244. * any more ... just clean up every urb's memory.
  245. */
  246. if (urb->hcpriv)
  247. finish_urb (ohci, urb);
  248. }
  249. spin_unlock_irqrestore (&ohci->lock, flags);
  250. return 0;
  251. }
  252. /*-------------------------------------------------------------------------*/
  253. /* frees config/altsetting state for endpoints,
  254. * including ED memory, dummy TD, and bulk/intr data toggle
  255. */
  256. static void
  257. ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  258. {
  259. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  260. unsigned long flags;
  261. struct ed *ed = ep->hcpriv;
  262. unsigned limit = 1000;
  263. /* ASSERT: any requests/urbs are being unlinked */
  264. /* ASSERT: nobody can be submitting urbs for this any more */
  265. if (!ed)
  266. return;
  267. rescan:
  268. spin_lock_irqsave (&ohci->lock, flags);
  269. if (!HC_IS_RUNNING (hcd->state)) {
  270. sanitize:
  271. ed->state = ED_IDLE;
  272. finish_unlinks (ohci, 0);
  273. }
  274. switch (ed->state) {
  275. case ED_UNLINK: /* wait for hw to finish? */
  276. /* major IRQ delivery trouble loses INTR_SF too... */
  277. if (limit-- == 0) {
  278. ohci_warn (ohci, "IRQ INTR_SF lossage\n");
  279. goto sanitize;
  280. }
  281. spin_unlock_irqrestore (&ohci->lock, flags);
  282. schedule_timeout_uninterruptible(1);
  283. goto rescan;
  284. case ED_IDLE: /* fully unlinked */
  285. if (list_empty (&ed->td_list)) {
  286. td_free (ohci, ed->dummy);
  287. ed_free (ohci, ed);
  288. break;
  289. }
  290. /* else FALL THROUGH */
  291. default:
  292. /* caller was supposed to have unlinked any requests;
  293. * that's not our job. can't recover; must leak ed.
  294. */
  295. ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
  296. ed, ep->desc.bEndpointAddress, ed->state,
  297. list_empty (&ed->td_list) ? "" : " (has tds)");
  298. td_free (ohci, ed->dummy);
  299. break;
  300. }
  301. ep->hcpriv = NULL;
  302. spin_unlock_irqrestore (&ohci->lock, flags);
  303. return;
  304. }
  305. static int ohci_get_frame (struct usb_hcd *hcd)
  306. {
  307. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  308. return ohci_frame_no(ohci);
  309. }
  310. static void ohci_usb_reset (struct ohci_hcd *ohci)
  311. {
  312. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  313. ohci->hc_control &= OHCI_CTRL_RWC;
  314. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  315. }
  316. /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
  317. * other cases where the next software may expect clean state from the
  318. * "firmware". this is bus-neutral, unlike shutdown() methods.
  319. */
  320. static void
  321. ohci_shutdown (struct usb_hcd *hcd)
  322. {
  323. struct ohci_hcd *ohci;
  324. ohci = hcd_to_ohci (hcd);
  325. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  326. ohci_usb_reset (ohci);
  327. /* flush the writes */
  328. (void) ohci_readl (ohci, &ohci->regs->control);
  329. }
  330. /*-------------------------------------------------------------------------*
  331. * HC functions
  332. *-------------------------------------------------------------------------*/
  333. /* init memory, and kick BIOS/SMM off */
  334. static int ohci_init (struct ohci_hcd *ohci)
  335. {
  336. int ret;
  337. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  338. disable (ohci);
  339. ohci->regs = hcd->regs;
  340. /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
  341. * was never needed for most non-PCI systems ... remove the code?
  342. */
  343. #ifndef IR_DISABLE
  344. /* SMM owns the HC? not for long! */
  345. if (!no_handshake && ohci_readl (ohci,
  346. &ohci->regs->control) & OHCI_CTRL_IR) {
  347. u32 temp;
  348. ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
  349. /* this timeout is arbitrary. we make it long, so systems
  350. * depending on usb keyboards may be usable even if the
  351. * BIOS/SMM code seems pretty broken.
  352. */
  353. temp = 500; /* arbitrary: five seconds */
  354. ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
  355. ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
  356. while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
  357. msleep (10);
  358. if (--temp == 0) {
  359. ohci_err (ohci, "USB HC takeover failed!"
  360. " (BIOS/SMM bug)\n");
  361. return -EBUSY;
  362. }
  363. }
  364. ohci_usb_reset (ohci);
  365. }
  366. #endif
  367. /* Disable HC interrupts */
  368. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  369. /* flush the writes, and save key bits like RWC */
  370. if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
  371. ohci->hc_control |= OHCI_CTRL_RWC;
  372. /* Read the number of ports unless overridden */
  373. if (ohci->num_ports == 0)
  374. ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
  375. if (ohci->hcca)
  376. return 0;
  377. ohci->hcca = dma_alloc_coherent (hcd->self.controller,
  378. sizeof *ohci->hcca, &ohci->hcca_dma, 0);
  379. if (!ohci->hcca)
  380. return -ENOMEM;
  381. if ((ret = ohci_mem_init (ohci)) < 0)
  382. ohci_stop (hcd);
  383. else {
  384. create_debug_files (ohci);
  385. }
  386. return ret;
  387. }
  388. /*-------------------------------------------------------------------------*/
  389. /* Start an OHCI controller, set the BUS operational
  390. * resets USB and controller
  391. * enable interrupts
  392. */
  393. static int ohci_run (struct ohci_hcd *ohci)
  394. {
  395. u32 mask, temp;
  396. int first = ohci->fminterval == 0;
  397. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  398. disable (ohci);
  399. /* boot firmware should have set this up (5.1.1.3.1) */
  400. if (first) {
  401. temp = ohci_readl (ohci, &ohci->regs->fminterval);
  402. ohci->fminterval = temp & 0x3fff;
  403. if (ohci->fminterval != FI)
  404. ohci_dbg (ohci, "fminterval delta %d\n",
  405. ohci->fminterval - FI);
  406. ohci->fminterval |= FSMP (ohci->fminterval) << 16;
  407. /* also: power/overcurrent flags in roothub.a */
  408. }
  409. /* Reset USB nearly "by the book". RemoteWakeupConnected was
  410. * saved if boot firmware (BIOS/SMM/...) told us it's connected,
  411. * or if bus glue did the same (e.g. for PCI add-in cards with
  412. * PCI PM support).
  413. */
  414. if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
  415. && !device_may_wakeup(hcd->self.controller))
  416. device_init_wakeup(hcd->self.controller, 1);
  417. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  418. case OHCI_USB_OPER:
  419. temp = 0;
  420. break;
  421. case OHCI_USB_SUSPEND:
  422. case OHCI_USB_RESUME:
  423. ohci->hc_control &= OHCI_CTRL_RWC;
  424. ohci->hc_control |= OHCI_USB_RESUME;
  425. temp = 10 /* msec wait */;
  426. break;
  427. // case OHCI_USB_RESET:
  428. default:
  429. ohci->hc_control &= OHCI_CTRL_RWC;
  430. ohci->hc_control |= OHCI_USB_RESET;
  431. temp = 50 /* msec wait */;
  432. break;
  433. }
  434. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  435. // flush the writes
  436. (void) ohci_readl (ohci, &ohci->regs->control);
  437. msleep(temp);
  438. temp = roothub_a (ohci);
  439. if (!(temp & RH_A_NPS)) {
  440. /* power down each port */
  441. for (temp = 0; temp < ohci->num_ports; temp++)
  442. ohci_writel (ohci, RH_PS_LSDA,
  443. &ohci->regs->roothub.portstatus [temp]);
  444. }
  445. // flush those writes
  446. (void) ohci_readl (ohci, &ohci->regs->control);
  447. memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
  448. /* 2msec timelimit here means no irqs/preempt */
  449. spin_lock_irq (&ohci->lock);
  450. retry:
  451. /* HC Reset requires max 10 us delay */
  452. ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
  453. temp = 30; /* ... allow extra time */
  454. while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
  455. if (--temp == 0) {
  456. spin_unlock_irq (&ohci->lock);
  457. ohci_err (ohci, "USB HC reset timed out!\n");
  458. return -1;
  459. }
  460. udelay (1);
  461. }
  462. /* now we're in the SUSPEND state ... must go OPERATIONAL
  463. * within 2msec else HC enters RESUME
  464. *
  465. * ... but some hardware won't init fmInterval "by the book"
  466. * (SiS, OPTi ...), so reset again instead. SiS doesn't need
  467. * this if we write fmInterval after we're OPERATIONAL.
  468. * Unclear about ALi, ServerWorks, and others ... this could
  469. * easily be a longstanding bug in chip init on Linux.
  470. */
  471. if (ohci->flags & OHCI_QUIRK_INITRESET) {
  472. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  473. // flush those writes
  474. (void) ohci_readl (ohci, &ohci->regs->control);
  475. }
  476. /* Tell the controller where the control and bulk lists are
  477. * The lists are empty now. */
  478. ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
  479. ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
  480. /* a reset clears this */
  481. ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
  482. periodic_reinit (ohci);
  483. /* some OHCI implementations are finicky about how they init.
  484. * bogus values here mean not even enumeration could work.
  485. */
  486. if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
  487. || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
  488. if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
  489. ohci->flags |= OHCI_QUIRK_INITRESET;
  490. ohci_dbg (ohci, "enabling initreset quirk\n");
  491. goto retry;
  492. }
  493. spin_unlock_irq (&ohci->lock);
  494. ohci_err (ohci, "init err (%08x %04x)\n",
  495. ohci_readl (ohci, &ohci->regs->fminterval),
  496. ohci_readl (ohci, &ohci->regs->periodicstart));
  497. return -EOVERFLOW;
  498. }
  499. /* use rhsc irqs after khubd is fully initialized */
  500. hcd->poll_rh = 1;
  501. hcd->uses_new_polling = 1;
  502. /* start controller operations */
  503. ohci->hc_control &= OHCI_CTRL_RWC;
  504. ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  505. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  506. hcd->state = HC_STATE_RUNNING;
  507. /* wake on ConnectStatusChange, matching external hubs */
  508. ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
  509. /* Choose the interrupts we care about now, others later on demand */
  510. mask = OHCI_INTR_INIT;
  511. ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
  512. ohci_writel (ohci, mask, &ohci->regs->intrenable);
  513. /* handle root hub init quirks ... */
  514. temp = roothub_a (ohci);
  515. temp &= ~(RH_A_PSM | RH_A_OCPM);
  516. if (ohci->flags & OHCI_QUIRK_SUPERIO) {
  517. /* NSC 87560 and maybe others */
  518. temp |= RH_A_NOCP;
  519. temp &= ~(RH_A_POTPGT | RH_A_NPS);
  520. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  521. } else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
  522. /* hub power always on; required for AMD-756 and some
  523. * Mac platforms. ganged overcurrent reporting, if any.
  524. */
  525. temp |= RH_A_NPS;
  526. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  527. }
  528. ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
  529. ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
  530. &ohci->regs->roothub.b);
  531. // flush those writes
  532. (void) ohci_readl (ohci, &ohci->regs->control);
  533. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  534. spin_unlock_irq (&ohci->lock);
  535. // POTPGT delay is bits 24-31, in 2 ms units.
  536. mdelay ((temp >> 23) & 0x1fe);
  537. hcd->state = HC_STATE_RUNNING;
  538. ohci_dump (ohci, 1);
  539. return 0;
  540. }
  541. /*-------------------------------------------------------------------------*/
  542. /* an interrupt happens */
  543. static irqreturn_t ohci_irq (struct usb_hcd *hcd)
  544. {
  545. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  546. struct ohci_regs __iomem *regs = ohci->regs;
  547. int ints;
  548. /* we can eliminate a (slow) ohci_readl()
  549. if _only_ WDH caused this irq */
  550. if ((ohci->hcca->done_head != 0)
  551. && ! (hc32_to_cpup (ohci, &ohci->hcca->done_head)
  552. & 0x01)) {
  553. ints = OHCI_INTR_WDH;
  554. /* cardbus/... hardware gone before remove() */
  555. } else if ((ints = ohci_readl (ohci, &regs->intrstatus)) == ~(u32)0) {
  556. disable (ohci);
  557. ohci_dbg (ohci, "device removed!\n");
  558. return IRQ_HANDLED;
  559. /* interrupt for some other device? */
  560. } else if ((ints &= ohci_readl (ohci, &regs->intrenable)) == 0) {
  561. return IRQ_NOTMINE;
  562. }
  563. if (ints & OHCI_INTR_UE) {
  564. disable (ohci);
  565. ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
  566. // e.g. due to PCI Master/Target Abort
  567. ohci_dump (ohci, 1);
  568. ohci_usb_reset (ohci);
  569. }
  570. if (ints & OHCI_INTR_RHSC) {
  571. ohci_vdbg(ohci, "rhsc\n");
  572. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  573. ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
  574. &regs->intrstatus);
  575. /* NOTE: Vendors didn't always make the same implementation
  576. * choices for RHSC. Many followed the spec; RHSC triggers
  577. * on an edge, like setting and maybe clearing a port status
  578. * change bit. With others it's level-triggered, active
  579. * until khubd clears all the port status change bits. We'll
  580. * always disable it here and rely on polling until khubd
  581. * re-enables it.
  582. */
  583. ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
  584. usb_hcd_poll_rh_status(hcd);
  585. }
  586. /* For connect and disconnect events, we expect the controller
  587. * to turn on RHSC along with RD. But for remote wakeup events
  588. * this might not happen.
  589. */
  590. else if (ints & OHCI_INTR_RD) {
  591. ohci_vdbg(ohci, "resume detect\n");
  592. ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
  593. hcd->poll_rh = 1;
  594. if (ohci->autostop) {
  595. spin_lock (&ohci->lock);
  596. ohci_rh_resume (ohci);
  597. spin_unlock (&ohci->lock);
  598. } else
  599. usb_hcd_resume_root_hub(hcd);
  600. }
  601. if (ints & OHCI_INTR_WDH) {
  602. if (HC_IS_RUNNING(hcd->state))
  603. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
  604. spin_lock (&ohci->lock);
  605. dl_done_list (ohci);
  606. spin_unlock (&ohci->lock);
  607. if (HC_IS_RUNNING(hcd->state))
  608. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
  609. }
  610. /* could track INTR_SO to reduce available PCI/... bandwidth */
  611. /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
  612. * when there's still unlinking to be done (next frame).
  613. */
  614. spin_lock (&ohci->lock);
  615. if (ohci->ed_rm_list)
  616. finish_unlinks (ohci, ohci_frame_no(ohci));
  617. if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
  618. && HC_IS_RUNNING(hcd->state))
  619. ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
  620. spin_unlock (&ohci->lock);
  621. if (HC_IS_RUNNING(hcd->state)) {
  622. ohci_writel (ohci, ints, &regs->intrstatus);
  623. ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
  624. // flush those writes
  625. (void) ohci_readl (ohci, &ohci->regs->control);
  626. }
  627. return IRQ_HANDLED;
  628. }
  629. /*-------------------------------------------------------------------------*/
  630. static void ohci_stop (struct usb_hcd *hcd)
  631. {
  632. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  633. ohci_dump (ohci, 1);
  634. flush_scheduled_work();
  635. ohci_usb_reset (ohci);
  636. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  637. free_irq(hcd->irq, hcd);
  638. hcd->irq = -1;
  639. remove_debug_files (ohci);
  640. ohci_mem_cleanup (ohci);
  641. if (ohci->hcca) {
  642. dma_free_coherent (hcd->self.controller,
  643. sizeof *ohci->hcca,
  644. ohci->hcca, ohci->hcca_dma);
  645. ohci->hcca = NULL;
  646. ohci->hcca_dma = 0;
  647. }
  648. }
  649. /*-------------------------------------------------------------------------*/
  650. /* must not be called from interrupt context */
  651. #ifdef CONFIG_PM
  652. static int ohci_restart (struct ohci_hcd *ohci)
  653. {
  654. int temp;
  655. int i;
  656. struct urb_priv *priv;
  657. /* mark any devices gone, so they do nothing till khubd disconnects.
  658. * recycle any "live" eds/tds (and urbs) right away.
  659. * later, khubd disconnect processing will recycle the other state,
  660. * (either as disconnect/reconnect, or maybe someday as a reset).
  661. */
  662. spin_lock_irq(&ohci->lock);
  663. disable (ohci);
  664. usb_root_hub_lost_power(ohci_to_hcd(ohci)->self.root_hub);
  665. if (!list_empty (&ohci->pending))
  666. ohci_dbg(ohci, "abort schedule...\n");
  667. list_for_each_entry (priv, &ohci->pending, pending) {
  668. struct urb *urb = priv->td[0]->urb;
  669. struct ed *ed = priv->ed;
  670. switch (ed->state) {
  671. case ED_OPER:
  672. ed->state = ED_UNLINK;
  673. ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
  674. ed_deschedule (ohci, ed);
  675. ed->ed_next = ohci->ed_rm_list;
  676. ed->ed_prev = NULL;
  677. ohci->ed_rm_list = ed;
  678. /* FALLTHROUGH */
  679. case ED_UNLINK:
  680. break;
  681. default:
  682. ohci_dbg(ohci, "bogus ed %p state %d\n",
  683. ed, ed->state);
  684. }
  685. spin_lock (&urb->lock);
  686. urb->status = -ESHUTDOWN;
  687. spin_unlock (&urb->lock);
  688. }
  689. finish_unlinks (ohci, 0);
  690. spin_unlock_irq(&ohci->lock);
  691. /* paranoia, in case that didn't work: */
  692. /* empty the interrupt branches */
  693. for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
  694. for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
  695. /* no EDs to remove */
  696. ohci->ed_rm_list = NULL;
  697. /* empty control and bulk lists */
  698. ohci->ed_controltail = NULL;
  699. ohci->ed_bulktail = NULL;
  700. if ((temp = ohci_run (ohci)) < 0) {
  701. ohci_err (ohci, "can't restart, %d\n", temp);
  702. return temp;
  703. } else {
  704. /* here we "know" root ports should always stay powered,
  705. * and that if we try to turn them back on the root hub
  706. * will respond to CSC processing.
  707. */
  708. i = ohci->num_ports;
  709. while (i--)
  710. ohci_writel (ohci, RH_PS_PSS,
  711. &ohci->regs->roothub.portstatus [i]);
  712. ohci_dbg (ohci, "restart complete\n");
  713. }
  714. return 0;
  715. }
  716. #endif
  717. /*-------------------------------------------------------------------------*/
  718. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  719. MODULE_AUTHOR (DRIVER_AUTHOR);
  720. MODULE_DESCRIPTION (DRIVER_INFO);
  721. MODULE_LICENSE ("GPL");
  722. #ifdef CONFIG_PCI
  723. #include "ohci-pci.c"
  724. #define PCI_DRIVER ohci_pci_driver
  725. #endif
  726. #ifdef CONFIG_SA1111
  727. #include "ohci-sa1111.c"
  728. #define SA1111_DRIVER ohci_hcd_sa1111_driver
  729. #endif
  730. #ifdef CONFIG_ARCH_S3C2410
  731. #include "ohci-s3c2410.c"
  732. #define PLATFORM_DRIVER ohci_hcd_s3c2410_driver
  733. #endif
  734. #ifdef CONFIG_ARCH_OMAP
  735. #include "ohci-omap.c"
  736. #define PLATFORM_DRIVER ohci_hcd_omap_driver
  737. #endif
  738. #ifdef CONFIG_ARCH_LH7A404
  739. #include "ohci-lh7a404.c"
  740. #define PLATFORM_DRIVER ohci_hcd_lh7a404_driver
  741. #endif
  742. #ifdef CONFIG_PXA27x
  743. #include "ohci-pxa27x.c"
  744. #define PLATFORM_DRIVER ohci_hcd_pxa27x_driver
  745. #endif
  746. #ifdef CONFIG_ARCH_EP93XX
  747. #include "ohci-ep93xx.c"
  748. #define PLATFORM_DRIVER ohci_hcd_ep93xx_driver
  749. #endif
  750. #ifdef CONFIG_SOC_AU1X00
  751. #include "ohci-au1xxx.c"
  752. #define PLATFORM_DRIVER ohci_hcd_au1xxx_driver
  753. #endif
  754. #ifdef CONFIG_PNX8550
  755. #include "ohci-pnx8550.c"
  756. #define PLATFORM_DRIVER ohci_hcd_pnx8550_driver
  757. #endif
  758. #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
  759. #include "ohci-ppc-soc.c"
  760. #define PLATFORM_DRIVER ohci_hcd_ppc_soc_driver
  761. #endif
  762. #ifdef CONFIG_ARCH_AT91
  763. #include "ohci-at91.c"
  764. #define PLATFORM_DRIVER ohci_hcd_at91_driver
  765. #endif
  766. #ifdef CONFIG_ARCH_PNX4008
  767. #include "ohci-pnx4008.c"
  768. #define PLATFORM_DRIVER usb_hcd_pnx4008_driver
  769. #endif
  770. #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
  771. #include "ohci-ppc-of.c"
  772. #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
  773. #endif
  774. #ifdef CONFIG_PPC_PS3
  775. #include "ohci-ps3.c"
  776. #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_sb_driver
  777. #endif
  778. #if !defined(PCI_DRIVER) && \
  779. !defined(PLATFORM_DRIVER) && \
  780. !defined(OF_PLATFORM_DRIVER) && \
  781. !defined(SA1111_DRIVER) && \
  782. !defined(PS3_SYSTEM_BUS_DRIVER)
  783. #error "missing bus glue for ohci-hcd"
  784. #endif
  785. static int __init ohci_hcd_mod_init(void)
  786. {
  787. int retval = 0;
  788. if (usb_disabled())
  789. return -ENODEV;
  790. printk (KERN_DEBUG "%s: " DRIVER_INFO "\n", hcd_name);
  791. pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
  792. sizeof (struct ed), sizeof (struct td));
  793. #ifdef PS3_SYSTEM_BUS_DRIVER
  794. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  795. retval = ps3_system_bus_driver_register(
  796. &PS3_SYSTEM_BUS_DRIVER);
  797. if (retval < 0)
  798. goto error_ps3;
  799. }
  800. #endif
  801. #ifdef PLATFORM_DRIVER
  802. retval = platform_driver_register(&PLATFORM_DRIVER);
  803. if (retval < 0)
  804. goto error_platform;
  805. #endif
  806. #ifdef OF_PLATFORM_DRIVER
  807. retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
  808. if (retval < 0)
  809. goto error_of_platform;
  810. #endif
  811. #ifdef SA1111_DRIVER
  812. retval = sa1111_driver_register(&SA1111_DRIVER);
  813. if (retval < 0)
  814. goto error_sa1111;
  815. #endif
  816. #ifdef PCI_DRIVER
  817. retval = pci_register_driver(&PCI_DRIVER);
  818. if (retval < 0)
  819. goto error_pci;
  820. #endif
  821. return retval;
  822. /* Error path */
  823. #ifdef PCI_DRIVER
  824. error_pci:
  825. #endif
  826. #ifdef SA1111_DRIVER
  827. sa1111_driver_unregister(&SA1111_DRIVER);
  828. error_sa1111:
  829. #endif
  830. #ifdef OF_PLATFORM_DRIVER
  831. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  832. error_of_platform:
  833. #endif
  834. #ifdef PLATFORM_DRIVER
  835. platform_driver_unregister(&PLATFORM_DRIVER);
  836. error_platform:
  837. #endif
  838. #ifdef PS3_SYSTEM_BUS_DRIVER
  839. if (firmware_has_feature(FW_FEATURE_PS3_LV1))
  840. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  841. error_ps3:
  842. #endif
  843. return retval;
  844. }
  845. module_init(ohci_hcd_mod_init);
  846. static void __exit ohci_hcd_mod_exit(void)
  847. {
  848. #ifdef PCI_DRIVER
  849. pci_unregister_driver(&PCI_DRIVER);
  850. #endif
  851. #ifdef SA1111_DRIVER
  852. sa1111_driver_unregister(&SA1111_DRIVER);
  853. #endif
  854. #ifdef OF_PLATFORM_DRIVER
  855. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  856. #endif
  857. #ifdef PLATFORM_DRIVER
  858. platform_driver_unregister(&PLATFORM_DRIVER);
  859. #endif
  860. #ifdef PS3_SYSTEM_BUS_DRIVER
  861. if (firmware_has_feature(FW_FEATURE_PS3_LV1))
  862. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  863. #endif
  864. }
  865. module_exit(ohci_hcd_mod_exit);