You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acpi_timer.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2018 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or 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
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <byteswap.h>
  28. #include <ipxe/io.h>
  29. #include <ipxe/acpi.h>
  30. /** @file
  31. *
  32. * ACPI power management timer
  33. *
  34. */
  35. /** ACPI timer frequency (fixed 3.579545MHz) */
  36. #define ACPI_TIMER_HZ 3579545
  37. /** ACPI timer mask
  38. *
  39. * Timers may be implemented as either 24-bit or 32-bit counters. We
  40. * simplify the code by pessimistically assuming that the timer has
  41. * only 24 bits.
  42. */
  43. #define ACPI_TIMER_MASK 0x00ffffffUL
  44. /** Power management timer register address */
  45. static unsigned int pm_tmr;
  46. struct timer acpi_timer __timer ( TIMER_PREFERRED );
  47. /**
  48. * Get current system time in ticks
  49. *
  50. * @ret ticks Current time, in ticks
  51. */
  52. static unsigned long acpi_currticks ( void ) {
  53. static unsigned long offset;
  54. static uint32_t prev;
  55. uint32_t now;
  56. /* Read timer and account for wraparound */
  57. now = ( inl ( pm_tmr ) & ACPI_TIMER_MASK );
  58. if ( now < prev ) {
  59. offset += ( ( ACPI_TIMER_MASK + 1 ) /
  60. ( ACPI_TIMER_HZ / TICKS_PER_SEC ) );
  61. }
  62. prev = now;
  63. /* Convert to timer ticks */
  64. return ( offset + ( now / ( ACPI_TIMER_HZ / TICKS_PER_SEC ) ) );
  65. }
  66. /**
  67. * Delay for a fixed number of microseconds
  68. *
  69. * @v usecs Number of microseconds for which to delay
  70. */
  71. static void acpi_udelay ( unsigned long usecs ) {
  72. uint32_t start;
  73. uint32_t elapsed;
  74. uint32_t threshold;
  75. /* Delay until a suitable number of ticks have elapsed. We do
  76. * not need to allow for multiple wraparound, since the
  77. * wraparound period for a 24-bit timer at 3.579545MHz is
  78. * around 4700000us.
  79. */
  80. start = inl ( pm_tmr );
  81. threshold = ( ( usecs * ACPI_TIMER_HZ ) / 1000000 );
  82. do {
  83. elapsed = ( ( inl ( pm_tmr ) - start ) & ACPI_TIMER_MASK );
  84. } while ( elapsed < threshold );
  85. }
  86. /**
  87. * Probe ACPI power management timer
  88. *
  89. * @ret rc Return status code
  90. */
  91. static int acpi_timer_probe ( void ) {
  92. struct acpi_fadt fadtab;
  93. userptr_t fadt;
  94. unsigned int pm_tmr_blk;
  95. /* Locate FADT */
  96. fadt = acpi_find ( FADT_SIGNATURE, 0 );
  97. if ( ! fadt ) {
  98. DBGC ( &acpi_timer, "ACPI could not find FADT\n" );
  99. return -ENOENT;
  100. }
  101. /* Read FADT */
  102. copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
  103. pm_tmr_blk = le32_to_cpu ( fadtab.pm_tmr_blk );
  104. if ( ! pm_tmr_blk ) {
  105. DBGC ( &acpi_timer, "ACPI has no timer\n" );
  106. return -ENOENT;
  107. }
  108. /* Record power management timer register address */
  109. pm_tmr = ( pm_tmr_blk + ACPI_PM_TMR );
  110. return 0;
  111. }
  112. /** ACPI timer */
  113. struct timer acpi_timer __timer ( TIMER_PREFERRED ) = {
  114. .name = "acpi",
  115. .probe = acpi_timer_probe,
  116. .currticks = acpi_currticks,
  117. .udelay = acpi_udelay,
  118. };