/drivers/acpi/hardware/hwtimer.c

https://bitbucket.org/evzijst/gittest · C · 203 lines · 67 code · 34 blank · 102 comment · 13 complexity · 3cb33fcec85ea33689cd5789d727a708 MD5 · raw file

  1. /******************************************************************************
  2. *
  3. * Name: hwtimer.c - ACPI Power Management Timer Interface
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <linux/module.h>
  43. #include <acpi/acpi.h>
  44. #define _COMPONENT ACPI_HARDWARE
  45. ACPI_MODULE_NAME ("hwtimer")
  46. /******************************************************************************
  47. *
  48. * FUNCTION: acpi_get_timer_resolution
  49. *
  50. * PARAMETERS: Resolution - Where the resolution is returned
  51. *
  52. * RETURN: Status and timer resolution
  53. *
  54. * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
  55. *
  56. ******************************************************************************/
  57. acpi_status
  58. acpi_get_timer_resolution (
  59. u32 *resolution)
  60. {
  61. ACPI_FUNCTION_TRACE ("acpi_get_timer_resolution");
  62. if (!resolution) {
  63. return_ACPI_STATUS (AE_BAD_PARAMETER);
  64. }
  65. if (0 == acpi_gbl_FADT->tmr_val_ext) {
  66. *resolution = 24;
  67. }
  68. else {
  69. *resolution = 32;
  70. }
  71. return_ACPI_STATUS (AE_OK);
  72. }
  73. /******************************************************************************
  74. *
  75. * FUNCTION: acpi_get_timer
  76. *
  77. * PARAMETERS: Ticks - Where the timer value is returned
  78. *
  79. * RETURN: Status and current ticks
  80. *
  81. * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
  82. *
  83. ******************************************************************************/
  84. acpi_status
  85. acpi_get_timer (
  86. u32 *ticks)
  87. {
  88. acpi_status status;
  89. ACPI_FUNCTION_TRACE ("acpi_get_timer");
  90. if (!ticks) {
  91. return_ACPI_STATUS (AE_BAD_PARAMETER);
  92. }
  93. status = acpi_hw_low_level_read (32, ticks, &acpi_gbl_FADT->xpm_tmr_blk);
  94. return_ACPI_STATUS (status);
  95. }
  96. EXPORT_SYMBOL(acpi_get_timer);
  97. /******************************************************************************
  98. *
  99. * FUNCTION: acpi_get_timer_duration
  100. *
  101. * PARAMETERS: start_ticks - Starting timestamp
  102. * end_ticks - End timestamp
  103. * time_elapsed - Where the elapsed time is returned
  104. *
  105. * RETURN: Status and time_elapsed
  106. *
  107. * DESCRIPTION: Computes the time elapsed (in microseconds) between two
  108. * PM Timer time stamps, taking into account the possibility of
  109. * rollovers, the timer resolution, and timer frequency.
  110. *
  111. * The PM Timer's clock ticks at roughly 3.6 times per
  112. * _microsecond_, and its clock continues through Cx state
  113. * transitions (unlike many CPU timestamp counters) -- making it
  114. * a versatile and accurate timer.
  115. *
  116. * Note that this function accommodates only a single timer
  117. * rollover. Thus for 24-bit timers, this function should only
  118. * be used for calculating durations less than ~4.6 seconds
  119. * (~20 minutes for 32-bit timers) -- calculations below:
  120. *
  121. * 2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
  122. * 2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
  123. *
  124. ******************************************************************************/
  125. acpi_status
  126. acpi_get_timer_duration (
  127. u32 start_ticks,
  128. u32 end_ticks,
  129. u32 *time_elapsed)
  130. {
  131. acpi_status status;
  132. u32 delta_ticks;
  133. acpi_integer quotient;
  134. ACPI_FUNCTION_TRACE ("acpi_get_timer_duration");
  135. if (!time_elapsed) {
  136. return_ACPI_STATUS (AE_BAD_PARAMETER);
  137. }
  138. /*
  139. * Compute Tick Delta:
  140. * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
  141. */
  142. if (start_ticks < end_ticks) {
  143. delta_ticks = end_ticks - start_ticks;
  144. }
  145. else if (start_ticks > end_ticks) {
  146. if (0 == acpi_gbl_FADT->tmr_val_ext) {
  147. /* 24-bit Timer */
  148. delta_ticks = (((0x00FFFFFF - start_ticks) + end_ticks) & 0x00FFFFFF);
  149. }
  150. else {
  151. /* 32-bit Timer */
  152. delta_ticks = (0xFFFFFFFF - start_ticks) + end_ticks;
  153. }
  154. }
  155. else /* start_ticks == end_ticks */ {
  156. *time_elapsed = 0;
  157. return_ACPI_STATUS (AE_OK);
  158. }
  159. /*
  160. * Compute Duration (Requires a 64-bit multiply and divide):
  161. *
  162. * time_elapsed = (delta_ticks * 1000000) / PM_TIMER_FREQUENCY;
  163. */
  164. status = acpi_ut_short_divide (((u64) delta_ticks) * 1000000,
  165. PM_TIMER_FREQUENCY, &quotient, NULL);
  166. *time_elapsed = (u32) quotient;
  167. return_ACPI_STATUS (status);
  168. }
  169. EXPORT_SYMBOL(acpi_get_timer_duration);