adm5120-pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 fragments of the OHCI driver.
  7. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  8. * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #define OHCI_SCHED_ENABLES \
  16. (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
  17. #ifdef CONFIG_PM
  18. static int admhc_restart(struct admhcd *ahcd);
  19. static int admhc_rh_suspend(struct admhcd *ahcd, int autostop)
  20. __releases(ahcd->lock)
  21. __acquires(ahcd->lock)
  22. {
  23. int status = 0;
  24. ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
  25. switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
  26. case OHCI_USB_RESUME:
  27. admhc_dbg(ahcd, "resume/suspend?\n");
  28. ahcd->hc_control &= ~OHCI_CTRL_HCFS;
  29. ahcd->hc_control |= OHCI_USB_RESET;
  30. admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
  31. (void) admhc_readl(ahcd, &ahcd->regs->control);
  32. /* FALL THROUGH */
  33. case OHCI_USB_RESET:
  34. status = -EBUSY;
  35. admhc_dbg(ahcd, "needs reinit!\n");
  36. goto done;
  37. case OHCI_USB_SUSPEND:
  38. if (!ahcd->autostop) {
  39. admhc_dbg(ahcd, "already suspended\n");
  40. goto done;
  41. }
  42. }
  43. admhc_dbg(ahcd, "%s root hub\n",
  44. autostop ? "auto-stop" : "suspend");
  45. /* First stop any processing */
  46. if (!autostop && (ahcd->hc_control & OHCI_SCHED_ENABLES)) {
  47. ahcd->hc_control &= ~OHCI_SCHED_ENABLES;
  48. admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
  49. ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
  50. admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->intrstatus);
  51. /* sched disables take effect on the next frame,
  52. * then the last WDH could take 6+ msec
  53. */
  54. admhc_dbg(ahcd, "stopping schedules ...\n");
  55. ahcd->autostop = 0;
  56. spin_unlock_irq (&ahcd->lock);
  57. msleep (8);
  58. spin_lock_irq(&ahcd->lock);
  59. }
  60. dl_done_list (ahcd);
  61. finish_unlinks (ahcd, admhc_frame_no(ahcd));
  62. /* maybe resume can wake root hub */
  63. if (device_may_wakeup(&admhcd_to_hcd(ahcd)->self.root_hub->dev) ||
  64. autostop)
  65. ahcd->hc_control |= OHCI_CTRL_RWE;
  66. else {
  67. admhc_writel(ahcd, OHCI_INTR_RHSC, &ahcd->regs->intrdisable);
  68. ahcd->hc_control &= ~OHCI_CTRL_RWE;
  69. }
  70. /* Suspend hub ... this is the "global (to this bus) suspend" mode,
  71. * which doesn't imply ports will first be individually suspended.
  72. */
  73. ahcd->hc_control &= ~OHCI_CTRL_HCFS;
  74. ahcd->hc_control |= OHCI_USB_SUSPEND;
  75. admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
  76. (void) admhc_readl(ahcd, &ahcd->regs->control);
  77. /* no resumes until devices finish suspending */
  78. if (!autostop) {
  79. ahcd->next_statechange = jiffies + msecs_to_jiffies (5);
  80. ahcd->autostop = 0;
  81. }
  82. done:
  83. return status;
  84. }
  85. static inline struct ed *find_head(struct ed *ed)
  86. {
  87. /* for bulk and control lists */
  88. while (ed->ed_prev)
  89. ed = ed->ed_prev;
  90. return ed;
  91. }
  92. /* caller has locked the root hub */
  93. static int admhc_rh_resume(struct admhcd *ahcd)
  94. __releases(ahcd->lock)
  95. __acquires(ahcd->lock)
  96. {
  97. struct usb_hcd *hcd = admhcd_to_hcd (ahcd);
  98. u32 temp, enables;
  99. int status = -EINPROGRESS;
  100. int autostopped = ahcd->autostop;
  101. ahcd->autostop = 0;
  102. ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
  103. if (ahcd->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
  104. /* this can happen after resuming a swsusp snapshot */
  105. if (hcd->state == HC_STATE_RESUMING) {
  106. admhc_dbg(ahcd, "BIOS/SMM active, control %03x\n",
  107. ahcd->hc_control);
  108. status = -EBUSY;
  109. /* this happens when pmcore resumes HC then root */
  110. } else {
  111. admhc_dbg(ahcd, "duplicate resume\n");
  112. status = 0;
  113. }
  114. } else switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
  115. case OHCI_USB_SUSPEND:
  116. ahcd->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
  117. ahcd->hc_control |= OHCI_USB_RESUME;
  118. admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
  119. (void) admhc_readl(ahcd, &ahcd->regs->control);
  120. admhc_dbg(ahcd, "%s root hub\n",
  121. autostopped ? "auto-start" : "resume");
  122. break;
  123. case OHCI_USB_RESUME:
  124. /* HCFS changes sometime after INTR_RD */
  125. admhc_dbg(ahcd, "%swakeup root hub\n",
  126. autostopped ? "auto-" : "");
  127. break;
  128. case OHCI_USB_OPER:
  129. /* this can happen after resuming a swsusp snapshot */
  130. admhc_dbg(ahcd, "snapshot resume? reinit\n");
  131. status = -EBUSY;
  132. break;
  133. default: /* RESET, we lost power */
  134. admhc_dbg(ahcd, "lost power\n");
  135. status = -EBUSY;
  136. }
  137. if (status == -EBUSY) {
  138. if (!autostopped) {
  139. spin_unlock_irq (&ahcd->lock);
  140. (void) ahcd_init (ahcd);
  141. status = admhc_restart (ahcd);
  142. spin_lock_irq(&ahcd->lock);
  143. }
  144. return status;
  145. }
  146. if (status != -EINPROGRESS)
  147. return status;
  148. if (autostopped)
  149. goto skip_resume;
  150. spin_unlock_irq (&ahcd->lock);
  151. /* Some controllers (lucent erratum) need extra-long delays */
  152. msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
  153. temp = admhc_readl(ahcd, &ahcd->regs->control);
  154. temp &= OHCI_CTRL_HCFS;
  155. if (temp != OHCI_USB_RESUME) {
  156. admhc_err (ahcd, "controller won't resume\n");
  157. spin_lock_irq(&ahcd->lock);
  158. return -EBUSY;
  159. }
  160. /* disable old schedule state, reinit from scratch */
  161. admhc_writel(ahcd, 0, &ahcd->regs->ed_controlhead);
  162. admhc_writel(ahcd, 0, &ahcd->regs->ed_controlcurrent);
  163. admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkhead);
  164. admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkcurrent);
  165. admhc_writel(ahcd, 0, &ahcd->regs->ed_periodcurrent);
  166. admhc_writel(ahcd, (u32) ahcd->hcca_dma, &ahcd->ahcd->regs->hcca);
  167. /* Sometimes PCI D3 suspend trashes frame timings ... */
  168. periodic_reinit(ahcd);
  169. /* the following code is executed with ahcd->lock held and
  170. * irqs disabled if and only if autostopped is true
  171. */
  172. skip_resume:
  173. /* interrupts might have been disabled */
  174. admhc_writel(ahcd, OHCI_INTR_INIT, &ahcd->regs->int_enable);
  175. if (ahcd->ed_rm_list)
  176. admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->int_enable);
  177. /* Then re-enable operations */
  178. admhc_writel(ahcd, OHCI_USB_OPER, &ahcd->regs->control);
  179. (void) admhc_readl(ahcd, &ahcd->regs->control);
  180. if (!autostopped)
  181. msleep (3);
  182. temp = ahcd->hc_control;
  183. temp &= OHCI_CTRL_RWC;
  184. temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  185. ahcd->hc_control = temp;
  186. admhc_writel(ahcd, temp, &ahcd->regs->control);
  187. (void) admhc_readl(ahcd, &ahcd->regs->control);
  188. /* TRSMRCY */
  189. if (!autostopped) {
  190. msleep (10);
  191. spin_lock_irq(&ahcd->lock);
  192. }
  193. /* now ahcd->lock is always held and irqs are always disabled */
  194. /* keep it alive for more than ~5x suspend + resume costs */
  195. ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
  196. /* maybe turn schedules back on */
  197. enables = 0;
  198. temp = 0;
  199. if (!ahcd->ed_rm_list) {
  200. if (ahcd->ed_controltail) {
  201. admhc_writel(ahcd,
  202. find_head (ahcd->ed_controltail)->dma,
  203. &ahcd->regs->ed_controlhead);
  204. enables |= OHCI_CTRL_CLE;
  205. temp |= OHCI_CLF;
  206. }
  207. if (ahcd->ed_bulktail) {
  208. admhc_writel(ahcd, find_head (ahcd->ed_bulktail)->dma,
  209. &ahcd->regs->ed_bulkhead);
  210. enables |= OHCI_CTRL_BLE;
  211. temp |= OHCI_BLF;
  212. }
  213. }
  214. if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
  215. enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
  216. if (enables) {
  217. admhc_dbg(ahcd, "restarting schedules ... %08x\n", enables);
  218. ahcd->hc_control |= enables;
  219. admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
  220. if (temp)
  221. admhc_writel(ahcd, temp, &ahcd->regs->cmdstatus);
  222. (void) admhc_readl(ahcd, &ahcd->regs->control);
  223. }
  224. return 0;
  225. }
  226. static int admhc_bus_suspend(struct usb_hcd *hcd)
  227. {
  228. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  229. int rc;
  230. spin_lock_irq(&ahcd->lock);
  231. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  232. rc = -ESHUTDOWN;
  233. else
  234. rc = admhc_rh_suspend(ahcd, 0);
  235. spin_unlock_irq(&ahcd->lock);
  236. return rc;
  237. }
  238. static int admhc_bus_resume(struct usb_hcd *hcd)
  239. {
  240. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  241. int rc;
  242. if (time_before(jiffies, ahcd->next_statechange))
  243. msleep(5);
  244. spin_lock_irq(&ahcd->lock);
  245. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  246. rc = -ESHUTDOWN;
  247. else
  248. rc = admhc_rh_resume(ahcd);
  249. spin_unlock_irq(&ahcd->lock);
  250. /* poll until we know a device is connected or we autostop */
  251. if (rc == 0)
  252. usb_hcd_poll_rh_status(hcd);
  253. return rc;
  254. }
  255. /* Carry out polling-, autostop-, and autoresume-related state changes */
  256. static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
  257. int any_connected)
  258. {
  259. int poll_rh = 1;
  260. switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
  261. case OHCI_USB_OPER:
  262. /* keep on polling until we know a device is connected
  263. * and RHSC is enabled */
  264. if (!ahcd->autostop) {
  265. if (any_connected ||
  266. !device_may_wakeup(&admhcd_to_hcd(ahcd)
  267. ->self.root_hub->dev)) {
  268. if (admhc_readl(ahcd, &ahcd->regs->int_enable) &
  269. OHCI_INTR_RHSC)
  270. poll_rh = 0;
  271. } else {
  272. ahcd->autostop = 1;
  273. ahcd->next_statechange = jiffies + HZ;
  274. }
  275. /* if no devices have been attached for one second, autostop */
  276. } else {
  277. if (changed || any_connected) {
  278. ahcd->autostop = 0;
  279. ahcd->next_statechange = jiffies +
  280. STATECHANGE_DELAY;
  281. } else if (time_after_eq(jiffies,
  282. ahcd->next_statechange)
  283. && !ahcd->ed_rm_list
  284. && !(ahcd->hc_control &
  285. OHCI_SCHED_ENABLES)) {
  286. ahcd_rh_suspend(ahcd, 1);
  287. }
  288. }
  289. break;
  290. /* if there is a port change, autostart or ask to be resumed */
  291. case OHCI_USB_SUSPEND:
  292. case OHCI_USB_RESUME:
  293. if (changed) {
  294. if (ahcd->autostop)
  295. admhc_rh_resume(ahcd);
  296. else
  297. usb_hcd_resume_root_hub(admhcd_to_hcd(ahcd));
  298. } else {
  299. /* everything is idle, no need for polling */
  300. poll_rh = 0;
  301. }
  302. break;
  303. }
  304. return poll_rh;
  305. }
  306. /*-------------------------------------------------------------------------*/
  307. /* must not be called from interrupt context */
  308. static int admhc_restart(struct admhcd *ahcd)
  309. {
  310. int temp;
  311. int i;
  312. struct urb_priv *priv;
  313. /* mark any devices gone, so they do nothing till khubd disconnects.
  314. * recycle any "live" eds/tds (and urbs) right away.
  315. * later, khubd disconnect processing will recycle the other state,
  316. * (either as disconnect/reconnect, or maybe someday as a reset).
  317. */
  318. spin_lock_irq(&ahcd->lock);
  319. admhc_disable(ahcd);
  320. usb_root_hub_lost_power(admhcd_to_hcd(ahcd)->self.root_hub);
  321. if (!list_empty(&ahcd->pending))
  322. admhc_dbg(ahcd, "abort schedule...\n");
  323. list_for_each_entry(priv, &ahcd->pending, pending) {
  324. struct urb *urb = priv->td[0]->urb;
  325. struct ed *ed = priv->ed;
  326. switch (ed->state) {
  327. case ED_OPER:
  328. ed->state = ED_UNLINK;
  329. ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
  330. ed_deschedule (ahcd, ed);
  331. ed->ed_next = ahcd->ed_rm_list;
  332. ed->ed_prev = NULL;
  333. ahcd->ed_rm_list = ed;
  334. /* FALLTHROUGH */
  335. case ED_UNLINK:
  336. break;
  337. default:
  338. admhc_dbg(ahcd, "bogus ed %p state %d\n",
  339. ed, ed->state);
  340. }
  341. if (!urb->unlinked)
  342. urb->unlinked = -ESHUTDOWN;
  343. }
  344. finish_unlinks(ahcd, 0);
  345. spin_unlock_irq(&ahcd->lock);
  346. /* paranoia, in case that didn't work: */
  347. /* empty the interrupt branches */
  348. for (i = 0; i < NUM_INTS; i++) ahcd->load[i] = 0;
  349. for (i = 0; i < NUM_INTS; i++) ahcd->hcca->int_table[i] = 0;
  350. /* no EDs to remove */
  351. ahcd->ed_rm_list = NULL;
  352. /* empty control and bulk lists */
  353. ahcd->ed_controltail = NULL;
  354. ahcd->ed_bulktail = NULL;
  355. if ((temp = admhc_run(ahcd)) < 0) {
  356. admhc_err(ahcd, "can't restart, %d\n", temp);
  357. return temp;
  358. } else {
  359. /* here we "know" root ports should always stay powered,
  360. * and that if we try to turn them back on the root hub
  361. * will respond to CSC processing.
  362. */
  363. i = ahcd->num_ports;
  364. while (i--)
  365. admhc_writel(ahcd, RH_PS_PSS,
  366. &ahcd->regs->portstatus[i]);
  367. admhc_dbg(ahcd, "restart complete\n");
  368. }
  369. return 0;
  370. }
  371. #else /* CONFIG_PM */
  372. static inline int admhc_rh_resume(struct admhcd *ahcd)
  373. {
  374. return 0;
  375. }
  376. /* Carry out polling-related state changes.
  377. * autostop isn't used when CONFIG_PM is turned off.
  378. */
  379. static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
  380. int any_connected)
  381. {
  382. /* If INSM is enabled, don't poll */
  383. if (admhc_readl(ahcd, &ahcd->regs->int_enable) & ADMHC_INTR_INSM)
  384. return 0;
  385. /* If no status changes are pending, enable status-change interrupts */
  386. if (!changed) {
  387. admhc_intr_enable(ahcd, ADMHC_INTR_INSM);
  388. return 0;
  389. }
  390. return 1;
  391. }
  392. #endif /* CONFIG_PM */