adm5120-hcd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * ADM5120 HCD (Host Controller Driver) for USB
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was derived from: drivers/usb/host/ohci-hcd.c
  7. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  8. * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  9. *
  10. * [ Initialisation is based on Linus' ]
  11. * [ uhci code and gregs ahcd fragments ]
  12. * [ (C) Copyright 1999 Linus Torvalds ]
  13. * [ (C) Copyright 1999 Gregory P. Smith]
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published
  17. * by the Free Software Foundation.
  18. *
  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/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/timer.h>
  31. #include <linux/list.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/usb/hcd.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/dmapool.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/unaligned.h>
  41. #include <asm/byteorder.h>
  42. #define DRIVER_VERSION "0.27.0"
  43. #define DRIVER_AUTHOR "Gabor Juhos <juhosg@openwrt.org>"
  44. #define DRIVER_DESC "ADMtek USB 1.1 Host Controller Driver"
  45. /*-------------------------------------------------------------------------*/
  46. #undef ADMHC_VERBOSE_DEBUG /* not always helpful */
  47. /* For initializing controller (mask in an HCFS mode too) */
  48. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  49. #define ADMHC_INTR_INIT \
  50. (ADMHC_INTR_MIE | ADMHC_INTR_INSM | ADMHC_INTR_FATI \
  51. | ADMHC_INTR_RESI | ADMHC_INTR_TDC | ADMHC_INTR_BABI)
  52. /*-------------------------------------------------------------------------*/
  53. static const char hcd_name[] = "admhc-hcd";
  54. #define STATECHANGE_DELAY msecs_to_jiffies(300)
  55. #include "adm5120.h"
  56. static void admhc_dump(struct admhcd *ahcd, int verbose);
  57. static int admhc_init(struct admhcd *ahcd);
  58. static void admhc_stop(struct usb_hcd *hcd);
  59. #include "adm5120-dbg.c"
  60. #include "adm5120-mem.c"
  61. #include "adm5120-pm.c"
  62. #include "adm5120-hub.c"
  63. #include "adm5120-q.c"
  64. /*-------------------------------------------------------------------------*/
  65. /*
  66. * queue up an urb for anything except the root hub
  67. */
  68. static int admhc_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  69. gfp_t mem_flags)
  70. {
  71. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  72. struct ed *ed;
  73. struct urb_priv *urb_priv;
  74. unsigned int pipe = urb->pipe;
  75. int td_cnt = 0;
  76. unsigned long flags;
  77. int ret = 0;
  78. #ifdef ADMHC_VERBOSE_DEBUG
  79. spin_lock_irqsave(&ahcd->lock, flags);
  80. urb_print(ahcd, urb, "ENQEUE", usb_pipein(pipe), -EINPROGRESS);
  81. spin_unlock_irqrestore(&ahcd->lock, flags);
  82. #endif
  83. /* every endpoint has an ed, locate and maybe (re)initialize it */
  84. ed = ed_get(ahcd, urb->ep, urb->dev, pipe, urb->interval);
  85. if (!ed)
  86. return -ENOMEM;
  87. /* for the private part of the URB we need the number of TDs */
  88. switch (ed->type) {
  89. case PIPE_CONTROL:
  90. if (urb->transfer_buffer_length > TD_DATALEN_MAX)
  91. /* td_submit_urb() doesn't yet handle these */
  92. return -EMSGSIZE;
  93. /* 1 TD for setup, 1 for ACK, plus ... */
  94. td_cnt = 2;
  95. /* FALLTHROUGH */
  96. case PIPE_BULK:
  97. /* one TD for every 4096 Bytes (can be up to 8K) */
  98. td_cnt += urb->transfer_buffer_length / TD_DATALEN_MAX;
  99. /* ... and for any remaining bytes ... */
  100. if ((urb->transfer_buffer_length % TD_DATALEN_MAX) != 0)
  101. td_cnt++;
  102. /* ... and maybe a zero length packet to wrap it up */
  103. if (td_cnt == 0)
  104. td_cnt++;
  105. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  106. && (urb->transfer_buffer_length
  107. % usb_maxpacket(urb->dev, pipe,
  108. usb_pipeout(pipe))) == 0)
  109. td_cnt++;
  110. break;
  111. case PIPE_INTERRUPT:
  112. /*
  113. * for Interrupt IN/OUT transactions, each ED contains
  114. * only 1 TD.
  115. * TODO: check transfer_buffer_length?
  116. */
  117. td_cnt = 1;
  118. break;
  119. case PIPE_ISOCHRONOUS:
  120. /* number of packets from URB */
  121. td_cnt = urb->number_of_packets;
  122. break;
  123. }
  124. urb_priv = urb_priv_alloc(ahcd, td_cnt, mem_flags);
  125. if (!urb_priv)
  126. return -ENOMEM;
  127. urb_priv->ed = ed;
  128. spin_lock_irqsave(&ahcd->lock, flags);
  129. /* don't submit to a dead HC */
  130. if (!HCD_HW_ACCESSIBLE(hcd)) {
  131. ret = -ENODEV;
  132. goto fail;
  133. }
  134. if (!HC_IS_RUNNING(hcd->state)) {
  135. ret = -ENODEV;
  136. goto fail;
  137. }
  138. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  139. if (ret)
  140. goto fail;
  141. /* schedule the ed if needed */
  142. if (ed->state == ED_IDLE) {
  143. ret = ed_schedule(ahcd, ed);
  144. if (ret < 0) {
  145. usb_hcd_unlink_urb_from_ep(hcd, urb);
  146. goto fail;
  147. }
  148. if (ed->type == PIPE_ISOCHRONOUS) {
  149. u16 frame = admhc_frame_no(ahcd);
  150. /* delay a few frames before the first TD */
  151. frame += max_t (u16, 8, ed->interval);
  152. frame &= ~(ed->interval - 1);
  153. frame |= ed->branch;
  154. urb->start_frame = frame;
  155. /* yes, only URB_ISO_ASAP is supported, and
  156. * urb->start_frame is never used as input.
  157. */
  158. }
  159. } else if (ed->type == PIPE_ISOCHRONOUS)
  160. urb->start_frame = ed->last_iso + ed->interval;
  161. /* fill the TDs and link them to the ed; and
  162. * enable that part of the schedule, if needed
  163. * and update count of queued periodic urbs
  164. */
  165. urb->hcpriv = urb_priv;
  166. td_submit_urb(ahcd, urb);
  167. #ifdef ADMHC_VERBOSE_DEBUG
  168. admhc_dump_ed(ahcd, "admhc_urb_enqueue", urb_priv->ed, 1);
  169. #endif
  170. fail:
  171. if (ret)
  172. urb_priv_free(ahcd, urb_priv);
  173. spin_unlock_irqrestore(&ahcd->lock, flags);
  174. return ret;
  175. }
  176. /*
  177. * decouple the URB from the HC queues (TDs, urb_priv);
  178. * reporting is always done
  179. * asynchronously, and we might be dealing with an urb that's
  180. * partially transferred, or an ED with other urbs being unlinked.
  181. */
  182. static int admhc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  183. int status)
  184. {
  185. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  186. unsigned long flags;
  187. int ret;
  188. spin_lock_irqsave(&ahcd->lock, flags);
  189. #ifdef ADMHC_VERBOSE_DEBUG
  190. urb_print(ahcd, urb, "DEQUEUE", 1, status);
  191. #endif
  192. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  193. if (ret) {
  194. /* Do nothing */
  195. ;
  196. } else if (HC_IS_RUNNING(hcd->state)) {
  197. struct urb_priv *urb_priv;
  198. /* Unless an IRQ completed the unlink while it was being
  199. * handed to us, flag it for unlink and giveback, and force
  200. * some upcoming INTR_SF to call finish_unlinks()
  201. */
  202. urb_priv = urb->hcpriv;
  203. if (urb_priv) {
  204. if (urb_priv->ed->state == ED_OPER)
  205. start_ed_unlink(ahcd, urb_priv->ed);
  206. }
  207. } else {
  208. /*
  209. * with HC dead, we won't respect hc queue pointers
  210. * any more ... just clean up every urb's memory.
  211. */
  212. if (urb->hcpriv)
  213. finish_urb(ahcd, urb, status);
  214. }
  215. spin_unlock_irqrestore(&ahcd->lock, flags);
  216. return ret;
  217. }
  218. /*-------------------------------------------------------------------------*/
  219. /* frees config/altsetting state for endpoints,
  220. * including ED memory, dummy TD, and bulk/intr data toggle
  221. */
  222. static void admhc_endpoint_disable(struct usb_hcd *hcd,
  223. struct usb_host_endpoint *ep)
  224. {
  225. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  226. unsigned long flags;
  227. struct ed *ed = ep->hcpriv;
  228. unsigned limit = 1000;
  229. /* ASSERT: any requests/urbs are being unlinked */
  230. /* ASSERT: nobody can be submitting urbs for this any more */
  231. if (!ed)
  232. return;
  233. #ifdef ADMHC_VERBOSE_DEBUG
  234. spin_lock_irqsave(&ahcd->lock, flags);
  235. admhc_dump_ed(ahcd, "EP-DISABLE", ed, 1);
  236. spin_unlock_irqrestore(&ahcd->lock, flags);
  237. #endif
  238. rescan:
  239. spin_lock_irqsave(&ahcd->lock, flags);
  240. if (!HC_IS_RUNNING(hcd->state)) {
  241. sanitize:
  242. ed->state = ED_IDLE;
  243. finish_unlinks(ahcd, 0);
  244. }
  245. switch (ed->state) {
  246. case ED_UNLINK: /* wait for hw to finish? */
  247. /* major IRQ delivery trouble loses INTR_SOFI too... */
  248. if (limit-- == 0) {
  249. admhc_warn(ahcd, "IRQ INTR_SOFI lossage\n");
  250. goto sanitize;
  251. }
  252. spin_unlock_irqrestore(&ahcd->lock, flags);
  253. schedule_timeout_uninterruptible(1);
  254. goto rescan;
  255. case ED_IDLE: /* fully unlinked */
  256. if (list_empty(&ed->td_list)) {
  257. td_free(ahcd, ed->dummy);
  258. ed_free(ahcd, ed);
  259. break;
  260. }
  261. /* else FALL THROUGH */
  262. default:
  263. /* caller was supposed to have unlinked any requests;
  264. * that's not our job. can't recover; must leak ed.
  265. */
  266. admhc_err(ahcd, "leak ed %p (#%02x) state %d%s\n",
  267. ed, ep->desc.bEndpointAddress, ed->state,
  268. list_empty(&ed->td_list) ? "" : " (has tds)");
  269. td_free(ahcd, ed->dummy);
  270. break;
  271. }
  272. ep->hcpriv = NULL;
  273. spin_unlock_irqrestore(&ahcd->lock, flags);
  274. }
  275. static int admhc_get_frame_number(struct usb_hcd *hcd)
  276. {
  277. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  278. return admhc_frame_no(ahcd);
  279. }
  280. static void admhc_usb_reset(struct admhcd *ahcd)
  281. {
  282. #if 0
  283. ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
  284. ahcd->hc_control &= OHCI_CTRL_RWC;
  285. admhc_writel(ahcd, ahcd->hc_control, &ahcd->regs->control);
  286. #else
  287. /* FIXME */
  288. ahcd->host_control = ADMHC_BUSS_RESET;
  289. admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
  290. #endif
  291. }
  292. /* admhc_shutdown forcibly disables IRQs and DMA, helping kexec and
  293. * other cases where the next software may expect clean state from the
  294. * "firmware". this is bus-neutral, unlike shutdown() methods.
  295. */
  296. static void
  297. admhc_shutdown(struct usb_hcd *hcd)
  298. {
  299. struct admhcd *ahcd;
  300. ahcd = hcd_to_admhcd(hcd);
  301. admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
  302. admhc_dma_disable(ahcd);
  303. admhc_usb_reset(ahcd);
  304. /* flush the writes */
  305. admhc_writel_flush(ahcd);
  306. }
  307. /*-------------------------------------------------------------------------*
  308. * HC functions
  309. *-------------------------------------------------------------------------*/
  310. static void admhc_eds_cleanup(struct admhcd *ahcd)
  311. {
  312. if (ahcd->ed_tails[PIPE_INTERRUPT]) {
  313. ed_free(ahcd, ahcd->ed_tails[PIPE_INTERRUPT]);
  314. ahcd->ed_tails[PIPE_INTERRUPT] = NULL;
  315. }
  316. if (ahcd->ed_tails[PIPE_ISOCHRONOUS]) {
  317. ed_free(ahcd, ahcd->ed_tails[PIPE_ISOCHRONOUS]);
  318. ahcd->ed_tails[PIPE_ISOCHRONOUS] = NULL;
  319. }
  320. if (ahcd->ed_tails[PIPE_CONTROL]) {
  321. ed_free(ahcd, ahcd->ed_tails[PIPE_CONTROL]);
  322. ahcd->ed_tails[PIPE_CONTROL] = NULL;
  323. }
  324. if (ahcd->ed_tails[PIPE_BULK]) {
  325. ed_free(ahcd, ahcd->ed_tails[PIPE_BULK]);
  326. ahcd->ed_tails[PIPE_BULK] = NULL;
  327. }
  328. ahcd->ed_head = NULL;
  329. }
  330. #define ED_DUMMY_INFO (ED_SPEED_FULL | ED_SKIP)
  331. static int admhc_eds_init(struct admhcd *ahcd)
  332. {
  333. struct ed *ed;
  334. ed = ed_create(ahcd, PIPE_INTERRUPT, ED_DUMMY_INFO);
  335. if (!ed)
  336. goto err;
  337. ahcd->ed_tails[PIPE_INTERRUPT] = ed;
  338. ed = ed_create(ahcd, PIPE_ISOCHRONOUS, ED_DUMMY_INFO);
  339. if (!ed)
  340. goto err;
  341. ahcd->ed_tails[PIPE_ISOCHRONOUS] = ed;
  342. ed->ed_prev = ahcd->ed_tails[PIPE_INTERRUPT];
  343. ahcd->ed_tails[PIPE_INTERRUPT]->ed_next = ed;
  344. ahcd->ed_tails[PIPE_INTERRUPT]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
  345. ed = ed_create(ahcd, PIPE_CONTROL, ED_DUMMY_INFO);
  346. if (!ed)
  347. goto err;
  348. ahcd->ed_tails[PIPE_CONTROL] = ed;
  349. ed->ed_prev = ahcd->ed_tails[PIPE_ISOCHRONOUS];
  350. ahcd->ed_tails[PIPE_ISOCHRONOUS]->ed_next = ed;
  351. ahcd->ed_tails[PIPE_ISOCHRONOUS]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
  352. ed = ed_create(ahcd, PIPE_BULK, ED_DUMMY_INFO);
  353. if (!ed)
  354. goto err;
  355. ahcd->ed_tails[PIPE_BULK] = ed;
  356. ed->ed_prev = ahcd->ed_tails[PIPE_CONTROL];
  357. ahcd->ed_tails[PIPE_CONTROL]->ed_next = ed;
  358. ahcd->ed_tails[PIPE_CONTROL]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
  359. ahcd->ed_head = ahcd->ed_tails[PIPE_INTERRUPT];
  360. #ifdef ADMHC_VERBOSE_DEBUG
  361. admhc_dump_ed(ahcd, "ed intr", ahcd->ed_tails[PIPE_INTERRUPT], 1);
  362. admhc_dump_ed(ahcd, "ed isoc", ahcd->ed_tails[PIPE_ISOCHRONOUS], 1);
  363. admhc_dump_ed(ahcd, "ed ctrl", ahcd->ed_tails[PIPE_CONTROL], 1);
  364. admhc_dump_ed(ahcd, "ed bulk", ahcd->ed_tails[PIPE_BULK], 1);
  365. #endif
  366. return 0;
  367. err:
  368. admhc_eds_cleanup(ahcd);
  369. return -ENOMEM;
  370. }
  371. /* init memory, and kick BIOS/SMM off */
  372. static int admhc_init(struct admhcd *ahcd)
  373. {
  374. struct usb_hcd *hcd = admhcd_to_hcd(ahcd);
  375. int ret;
  376. admhc_disable(ahcd);
  377. ahcd->regs = hcd->regs;
  378. /* Disable HC interrupts */
  379. admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
  380. /* Read the number of ports unless overridden */
  381. if (ahcd->num_ports == 0)
  382. ahcd->num_ports = admhc_read_rhdesc(ahcd) & ADMHC_RH_NUMP;
  383. ret = admhc_mem_init(ahcd);
  384. if (ret)
  385. goto err;
  386. /* init dummy endpoints */
  387. ret = admhc_eds_init(ahcd);
  388. if (ret)
  389. goto err;
  390. create_debug_files(ahcd);
  391. return 0;
  392. err:
  393. admhc_stop(hcd);
  394. return ret;
  395. }
  396. /*-------------------------------------------------------------------------*/
  397. /* Start an OHCI controller, set the BUS operational
  398. * resets USB and controller
  399. * enable interrupts
  400. */
  401. static int admhc_run(struct admhcd *ahcd)
  402. {
  403. u32 val;
  404. int first = ahcd->fminterval == 0;
  405. struct usb_hcd *hcd = admhcd_to_hcd(ahcd);
  406. admhc_disable(ahcd);
  407. /* boot firmware should have set this up (5.1.1.3.1) */
  408. if (first) {
  409. val = admhc_readl(ahcd, &ahcd->regs->fminterval);
  410. ahcd->fminterval = val & ADMHC_SFI_FI_MASK;
  411. if (ahcd->fminterval != FI)
  412. admhc_dbg(ahcd, "fminterval delta %d\n",
  413. ahcd->fminterval - FI);
  414. ahcd->fminterval |=
  415. (FSLDP(ahcd->fminterval) << ADMHC_SFI_FSLDP_SHIFT);
  416. /* also: power/overcurrent flags in rhdesc */
  417. }
  418. #if 0 /* TODO: not applicable */
  419. /* Reset USB nearly "by the book". RemoteWakeupConnected has
  420. * to be checked in case boot firmware (BIOS/SMM/...) has set up
  421. * wakeup in a way the bus isn't aware of (e.g., legacy PCI PM).
  422. * If the bus glue detected wakeup capability then it should
  423. * already be enabled; if so we'll just enable it again.
  424. */
  425. if ((ahcd->hc_control & OHCI_CTRL_RWC) != 0)
  426. device_set_wakeup_capable(hcd->self.controller, 1);
  427. #endif
  428. switch (ahcd->host_control & ADMHC_HC_BUSS) {
  429. case ADMHC_BUSS_OPER:
  430. val = 0;
  431. break;
  432. case ADMHC_BUSS_SUSPEND:
  433. /* FALLTHROUGH ? */
  434. case ADMHC_BUSS_RESUME:
  435. ahcd->host_control = ADMHC_BUSS_RESUME;
  436. val = 10 /* msec wait */;
  437. break;
  438. /* case ADMHC_BUSS_RESET: */
  439. default:
  440. ahcd->host_control = ADMHC_BUSS_RESET;
  441. val = 50 /* msec wait */;
  442. break;
  443. }
  444. admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
  445. /* flush the writes */
  446. admhc_writel_flush(ahcd);
  447. msleep(val);
  448. val = admhc_read_rhdesc(ahcd);
  449. if (!(val & ADMHC_RH_NPS)) {
  450. /* power down each port */
  451. for (val = 0; val < ahcd->num_ports; val++)
  452. admhc_write_portstatus(ahcd, val, ADMHC_PS_CPP);
  453. }
  454. /* flush those writes */
  455. admhc_writel_flush(ahcd);
  456. /* 2msec timelimit here means no irqs/preempt */
  457. spin_lock_irq(&ahcd->lock);
  458. admhc_writel(ahcd, ADMHC_CTRL_SR, &ahcd->regs->gencontrol);
  459. val = 30; /* ... allow extra time */
  460. while ((admhc_readl(ahcd, &ahcd->regs->gencontrol) & ADMHC_CTRL_SR) != 0) {
  461. if (--val == 0) {
  462. spin_unlock_irq(&ahcd->lock);
  463. admhc_err(ahcd, "USB HC reset timed out!\n");
  464. return -1;
  465. }
  466. udelay(1);
  467. }
  468. /* enable HOST mode, before access any host specific register */
  469. admhc_writel(ahcd, ADMHC_CTRL_UHFE, &ahcd->regs->gencontrol);
  470. /* Tell the controller where the descriptor list is */
  471. admhc_writel(ahcd, (u32)ahcd->ed_head->dma, &ahcd->regs->hosthead);
  472. periodic_reinit(ahcd);
  473. /* use rhsc irqs after khubd is fully initialized */
  474. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  475. hcd->uses_new_polling = 1;
  476. #if 0
  477. /* wake on ConnectStatusChange, matching external hubs */
  478. admhc_writel(ahcd, RH_HS_DRWE, &ahcd->regs->roothub.status);
  479. #else
  480. /* FIXME roothub_write_status (ahcd, ADMHC_RH_DRWE); */
  481. #endif
  482. /* Choose the interrupts we care about now, others later on demand */
  483. admhc_intr_ack(ahcd, ~0);
  484. admhc_intr_enable(ahcd, ADMHC_INTR_INIT);
  485. admhc_writel(ahcd, ADMHC_RH_NPS | ADMHC_RH_LPSC, &ahcd->regs->rhdesc);
  486. /* flush those writes */
  487. admhc_writel_flush(ahcd);
  488. /* start controller operations */
  489. ahcd->host_control = ADMHC_BUSS_OPER;
  490. admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
  491. val = 20;
  492. while ((admhc_readl(ahcd, &ahcd->regs->host_control)
  493. & ADMHC_HC_BUSS) != ADMHC_BUSS_OPER) {
  494. if (--val == 0) {
  495. spin_unlock_irq(&ahcd->lock);
  496. admhc_err(ahcd, "unable to setup operational mode!\n");
  497. return -1;
  498. }
  499. mdelay(1);
  500. }
  501. hcd->state = HC_STATE_RUNNING;
  502. ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
  503. #if 0
  504. /* FIXME: enabling DMA is always failed here for an unknown reason */
  505. admhc_dma_enable(ahcd);
  506. val = 200;
  507. while ((admhc_readl(ahcd, &ahcd->regs->host_control)
  508. & ADMHC_HC_DMAE) != ADMHC_HC_DMAE) {
  509. if (--val == 0) {
  510. spin_unlock_irq(&ahcd->lock);
  511. admhc_err(ahcd, "unable to enable DMA!\n");
  512. admhc_dump(ahcd, 1);
  513. return -1;
  514. }
  515. mdelay(1);
  516. }
  517. #endif
  518. spin_unlock_irq(&ahcd->lock);
  519. mdelay(ADMHC_POTPGT);
  520. return 0;
  521. }
  522. /*-------------------------------------------------------------------------*/
  523. /* an interrupt happens */
  524. static irqreturn_t admhc_irq(struct usb_hcd *hcd)
  525. {
  526. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  527. struct admhcd_regs __iomem *regs = ahcd->regs;
  528. u32 ints;
  529. ints = admhc_readl(ahcd, &regs->int_status);
  530. if ((ints & ADMHC_INTR_INTA) == 0) {
  531. /* no unmasked interrupt status is set */
  532. return IRQ_NONE;
  533. }
  534. ints &= admhc_readl(ahcd, &regs->int_enable);
  535. if (ints & ADMHC_INTR_FATI) {
  536. /* e.g. due to PCI Master/Target Abort */
  537. admhc_disable(ahcd);
  538. admhc_err(ahcd, "Fatal Error, controller disabled\n");
  539. admhc_dump(ahcd, 1);
  540. admhc_usb_reset(ahcd);
  541. }
  542. if (ints & ADMHC_INTR_BABI) {
  543. admhc_intr_disable(ahcd, ADMHC_INTR_BABI);
  544. admhc_intr_ack(ahcd, ADMHC_INTR_BABI);
  545. admhc_err(ahcd, "Babble Detected\n");
  546. }
  547. if (ints & ADMHC_INTR_INSM) {
  548. admhc_vdbg(ahcd, "Root Hub Status Change\n");
  549. ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
  550. admhc_intr_ack(ahcd, ADMHC_INTR_RESI | ADMHC_INTR_INSM);
  551. /* NOTE: Vendors didn't always make the same implementation
  552. * choices for RHSC. Many followed the spec; RHSC triggers
  553. * on an edge, like setting and maybe clearing a port status
  554. * change bit. With others it's level-triggered, active
  555. * until khubd clears all the port status change bits. We'll
  556. * always disable it here and rely on polling until khubd
  557. * re-enables it.
  558. */
  559. admhc_intr_disable(ahcd, ADMHC_INTR_INSM);
  560. usb_hcd_poll_rh_status(hcd);
  561. } else if (ints & ADMHC_INTR_RESI) {
  562. /* For connect and disconnect events, we expect the controller
  563. * to turn on RHSC along with RD. But for remote wakeup events
  564. * this might not happen.
  565. */
  566. admhc_vdbg(ahcd, "Resume Detect\n");
  567. admhc_intr_ack(ahcd, ADMHC_INTR_RESI);
  568. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  569. if (ahcd->autostop) {
  570. spin_lock(&ahcd->lock);
  571. admhc_rh_resume(ahcd);
  572. spin_unlock(&ahcd->lock);
  573. } else
  574. usb_hcd_resume_root_hub(hcd);
  575. }
  576. if (ints & ADMHC_INTR_TDC) {
  577. admhc_vdbg(ahcd, "Transfer Descriptor Complete\n");
  578. admhc_intr_ack(ahcd, ADMHC_INTR_TDC);
  579. if (HC_IS_RUNNING(hcd->state))
  580. admhc_intr_disable(ahcd, ADMHC_INTR_TDC);
  581. spin_lock(&ahcd->lock);
  582. admhc_td_complete(ahcd);
  583. spin_unlock(&ahcd->lock);
  584. if (HC_IS_RUNNING(hcd->state))
  585. admhc_intr_enable(ahcd, ADMHC_INTR_TDC);
  586. }
  587. if (ints & ADMHC_INTR_SO) {
  588. /* could track INTR_SO to reduce available PCI/... bandwidth */
  589. admhc_vdbg(ahcd, "Schedule Overrun\n");
  590. }
  591. #if 1
  592. spin_lock(&ahcd->lock);
  593. if (ahcd->ed_rm_list)
  594. finish_unlinks(ahcd, admhc_frame_no(ahcd));
  595. if ((ints & ADMHC_INTR_SOFI) != 0 && !ahcd->ed_rm_list
  596. && HC_IS_RUNNING(hcd->state))
  597. admhc_intr_disable(ahcd, ADMHC_INTR_SOFI);
  598. spin_unlock(&ahcd->lock);
  599. #else
  600. if (ints & ADMHC_INTR_SOFI) {
  601. admhc_vdbg(ahcd, "Start Of Frame\n");
  602. spin_lock(&ahcd->lock);
  603. /* handle any pending ED removes */
  604. finish_unlinks(ahcd, admhc_frameno(ahcd));
  605. /* leaving INTR_SOFI enabled when there's still unlinking
  606. * to be done in the (next frame).
  607. */
  608. if ((ahcd->ed_rm_list == NULL) ||
  609. HC_IS_RUNNING(hcd->state) == 0)
  610. /*
  611. * disable INTR_SOFI if there are no unlinking to be
  612. * done (in the next frame)
  613. */
  614. admhc_intr_disable(ahcd, ADMHC_INTR_SOFI);
  615. spin_unlock(&ahcd->lock);
  616. }
  617. #endif
  618. if (HC_IS_RUNNING(hcd->state)) {
  619. admhc_intr_ack(ahcd, ints);
  620. admhc_intr_enable(ahcd, ADMHC_INTR_MIE);
  621. admhc_writel_flush(ahcd);
  622. }
  623. return IRQ_HANDLED;
  624. }
  625. /*-------------------------------------------------------------------------*/
  626. static void admhc_stop(struct usb_hcd *hcd)
  627. {
  628. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  629. admhc_dump(ahcd, 1);
  630. flush_scheduled_work();
  631. admhc_usb_reset(ahcd);
  632. admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
  633. free_irq(hcd->irq, hcd);
  634. hcd->irq = -1;
  635. remove_debug_files(ahcd);
  636. admhc_eds_cleanup(ahcd);
  637. admhc_mem_cleanup(ahcd);
  638. }
  639. /*-------------------------------------------------------------------------*/
  640. #ifdef CONFIG_ADM5120
  641. #include "adm5120-drv.c"
  642. #define PLATFORM_DRIVER usb_hcd_adm5120_driver
  643. #endif
  644. #if !defined(PLATFORM_DRIVER)
  645. #error "missing bus glue for admhc-hcd"
  646. #endif
  647. #define DRIVER_INFO DRIVER_DESC " version " DRIVER_VERSION
  648. static int __init admhc_hcd_mod_init(void)
  649. {
  650. int ret = 0;
  651. if (usb_disabled())
  652. return -ENODEV;
  653. pr_info("%s: " DRIVER_INFO "\n", hcd_name);
  654. pr_info("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
  655. sizeof(struct ed), sizeof(struct td));
  656. set_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
  657. #ifdef DEBUG
  658. admhc_debug_root = debugfs_create_dir("admhc", usb_debug_root);
  659. if (!admhc_debug_root) {
  660. ret = -ENOENT;
  661. goto error_debug;
  662. }
  663. #endif
  664. #ifdef PLATFORM_DRIVER
  665. ret = platform_driver_register(&PLATFORM_DRIVER);
  666. if (ret < 0)
  667. goto error_platform;
  668. #endif
  669. return ret;
  670. #ifdef PLATFORM_DRIVER
  671. platform_driver_unregister(&PLATFORM_DRIVER);
  672. error_platform:
  673. #endif
  674. #ifdef DEBUG
  675. debugfs_remove(admhc_debug_root);
  676. admhc_debug_root = NULL;
  677. error_debug:
  678. #endif
  679. clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
  680. return ret;
  681. }
  682. module_init(admhc_hcd_mod_init);
  683. static void __exit admhc_hcd_mod_exit(void)
  684. {
  685. platform_driver_unregister(&PLATFORM_DRIVER);
  686. #ifdef DEBUG
  687. debugfs_remove(admhc_debug_root);
  688. #endif
  689. clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
  690. }
  691. module_exit(admhc_hcd_mod_exit);
  692. MODULE_AUTHOR(DRIVER_AUTHOR);
  693. MODULE_DESCRIPTION(DRIVER_INFO);
  694. MODULE_VERSION(DRIVER_VERSION);
  695. MODULE_LICENSE("GPL v2");