ehci-timer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2012 by Alan Stern
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. /* This file is part of ehci-hcd.c */
  15. /*-------------------------------------------------------------------------*/
  16. /* Set a bit in the USBCMD register */
  17. static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit)
  18. {
  19. ehci->command |= bit;
  20. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  21. /* unblock posted write */
  22. ehci_readl(ehci, &ehci->regs->command);
  23. }
  24. /* Clear a bit in the USBCMD register */
  25. static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit)
  26. {
  27. ehci->command &= ~bit;
  28. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  29. /* unblock posted write */
  30. ehci_readl(ehci, &ehci->regs->command);
  31. }
  32. /*-------------------------------------------------------------------------*/
  33. /*
  34. * EHCI timer support... Now using hrtimers.
  35. *
  36. * Lots of different events are triggered from ehci->hrtimer. Whenever
  37. * the timer routine runs, it checks each possible event; events that are
  38. * currently enabled and whose expiration time has passed get handled.
  39. * The set of enabled events is stored as a collection of bitflags in
  40. * ehci->enabled_hrtimer_events, and they are numbered in order of
  41. * increasing delay values (ranging between 1 ms and 100 ms).
  42. *
  43. * Rather than implementing a sorted list or tree of all pending events,
  44. * we keep track only of the lowest-numbered pending event, in
  45. * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its
  46. * expiration time is set to the timeout value for this event.
  47. *
  48. * As a result, events might not get handled right away; the actual delay
  49. * could be anywhere up to twice the requested delay. This doesn't
  50. * matter, because none of the events are especially time-critical. The
  51. * ones that matter most all have a delay of 1 ms, so they will be
  52. * handled after 2 ms at most, which is okay. In addition to this, we
  53. * allow for an expiration range of 1 ms.
  54. */
  55. /*
  56. * Delay lengths for the hrtimer event types.
  57. * Keep this list sorted by delay length, in the same order as
  58. * the event types indexed by enum ehci_hrtimer_event in ehci.h.
  59. */
  60. static unsigned event_delays_ns[] = {
  61. 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */
  62. 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */
  63. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  64. 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
  65. };
  66. /* Enable a pending hrtimer event */
  67. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  68. bool resched)
  69. {
  70. ktime_t *timeout = &ehci->hr_timeouts[event];
  71. if (resched)
  72. *timeout = ktime_add(ktime_get(),
  73. ktime_set(0, event_delays_ns[event]));
  74. ehci->enabled_hrtimer_events |= (1 << event);
  75. /* Track only the lowest-numbered pending event */
  76. if (event < ehci->next_hrtimer_event) {
  77. ehci->next_hrtimer_event = event;
  78. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  79. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  80. }
  81. }
  82. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  83. static void ehci_poll_ASS(struct ehci_hcd *ehci)
  84. {
  85. unsigned actual, want;
  86. /* Don't enable anything if the controller isn't running (e.g., died) */
  87. if (ehci->rh_state != EHCI_RH_RUNNING)
  88. return;
  89. want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
  90. actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
  91. if (want != actual) {
  92. /* Poll again later, but give up after about 20 ms */
  93. if (ehci->ASS_poll_count++ < 20) {
  94. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
  95. return;
  96. }
  97. ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
  98. }
  99. ehci->ASS_poll_count = 0;
  100. /* The status is up-to-date; restart or stop the schedule as needed */
  101. if (want == 0) { /* Stopped */
  102. if (ehci->async_count > 0)
  103. ehci_set_command_bit(ehci, CMD_ASE);
  104. } else { /* Running */
  105. if (ehci->async_count == 0) {
  106. /* Turn off the schedule after a while */
  107. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
  108. true);
  109. }
  110. }
  111. }
  112. /* Turn off the async schedule after a brief delay */
  113. static void ehci_disable_ASE(struct ehci_hcd *ehci)
  114. {
  115. ehci_clear_command_bit(ehci, CMD_ASE);
  116. }
  117. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  118. static void ehci_poll_PSS(struct ehci_hcd *ehci)
  119. {
  120. unsigned actual, want;
  121. /* Don't do anything if the controller isn't running (e.g., died) */
  122. if (ehci->rh_state != EHCI_RH_RUNNING)
  123. return;
  124. want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
  125. actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
  126. if (want != actual) {
  127. /* Poll again later, but give up after about 20 ms */
  128. if (ehci->PSS_poll_count++ < 20) {
  129. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  130. return;
  131. }
  132. ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
  133. }
  134. ehci->PSS_poll_count = 0;
  135. /* The status is up-to-date; restart or stop the schedule as needed */
  136. if (want == 0) { /* Stopped */
  137. free_cached_lists(ehci);
  138. if (ehci->periodic_count > 0) {
  139. /* make sure ehci_work scans these */
  140. ehci->next_uframe = ehci_read_frame_index(ehci)
  141. & ((ehci->periodic_size << 3) - 1);
  142. ehci_set_command_bit(ehci, CMD_PSE);
  143. }
  144. } else { /* Running */
  145. if (ehci->periodic_count == 0) {
  146. /* Turn off the schedule after a while */
  147. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
  148. true);
  149. }
  150. }
  151. }
  152. /* Turn off the periodic schedule after a brief delay */
  153. static void ehci_disable_PSE(struct ehci_hcd *ehci)
  154. {
  155. ehci_clear_command_bit(ehci, CMD_PSE);
  156. /* Poll to see when it actually stops */
  157. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  158. }
  159. /*
  160. * Handler functions for the hrtimer event types.
  161. * Keep this array in the same order as the event types indexed by
  162. * enum ehci_hrtimer_event in ehci.h.
  163. */
  164. static void (*event_handlers[])(struct ehci_hcd *) = {
  165. ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
  166. ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
  167. ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  168. ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
  169. };
  170. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  171. {
  172. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  173. ktime_t now;
  174. unsigned long events;
  175. unsigned long flags;
  176. unsigned e;
  177. spin_lock_irqsave(&ehci->lock, flags);
  178. events = ehci->enabled_hrtimer_events;
  179. ehci->enabled_hrtimer_events = 0;
  180. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  181. /*
  182. * Check each pending event. If its time has expired, handle
  183. * the event; otherwise re-enable it.
  184. */
  185. now = ktime_get();
  186. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  187. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  188. event_handlers[e](ehci);
  189. else
  190. ehci_enable_event(ehci, e, false);
  191. }
  192. spin_unlock_irqrestore(&ehci->lock, flags);
  193. return HRTIMER_NORESTART;
  194. }