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.

timer.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2008 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 <string.h>
  25. #include <ipxe/process.h>
  26. #include <ipxe/console.h>
  27. #include <ipxe/keys.h>
  28. #include <ipxe/nap.h>
  29. #include <ipxe/init.h>
  30. #include <ipxe/timer.h>
  31. /** Current timer */
  32. static struct timer *timer;
  33. /**
  34. * Get current system time in ticks
  35. *
  36. * @ret ticks Current time, in ticks
  37. */
  38. unsigned long currticks ( void ) {
  39. /* Guard against use during early initialisation */
  40. if ( ! timer ) {
  41. DBGC ( &timer, "TIMER currticks() called before initialisation "
  42. "from %p\n", __builtin_return_address ( 0 ) );
  43. return 0;
  44. }
  45. /* Use selected timer */
  46. return timer->currticks();
  47. }
  48. /**
  49. * Delay for a fixed number of microseconds
  50. *
  51. * @v usecs Number of microseconds for which to delay
  52. */
  53. void udelay ( unsigned long usecs ) {
  54. /* Guard against use during early initialisation */
  55. if ( ! timer ) {
  56. DBGC ( &timer, "TIMER udelay() called before initialisation "
  57. "from %p\n", __builtin_return_address ( 0 ) );
  58. return;
  59. }
  60. /* Use selected timer */
  61. timer->udelay ( usecs );
  62. }
  63. /**
  64. * Delay for a fixed number of milliseconds
  65. *
  66. * @v msecs Number of milliseconds for which to delay
  67. */
  68. void mdelay ( unsigned long msecs ) {
  69. /* Guard against use during early initialisation */
  70. if ( ! timer ) {
  71. DBGC ( &timer, "TIMER mdelay() called before initialisation "
  72. "from %p\n", __builtin_return_address ( 0 ) );
  73. return;
  74. }
  75. /* Delay for specified number of milliseconds */
  76. while ( msecs-- )
  77. udelay ( 1000 );
  78. }
  79. /**
  80. * Sleep (interruptibly) for a fixed number of seconds
  81. *
  82. * @v secs Number of seconds for which to delay
  83. * @ret secs Number of seconds remaining, if interrupted
  84. */
  85. unsigned int sleep ( unsigned int secs ) {
  86. unsigned long start = currticks();
  87. unsigned long now;
  88. for ( ; secs ; secs-- ) {
  89. while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
  90. step();
  91. if ( iskey() && ( getchar() == CTRL_C ) )
  92. return secs;
  93. cpu_nap();
  94. }
  95. start = now;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * Find a working timer
  101. *
  102. */
  103. static void timer_probe ( void ) {
  104. int rc;
  105. /* Use first working timer */
  106. for_each_table_entry ( timer, TIMERS ) {
  107. if ( ( timer->probe == NULL ) ||
  108. ( ( rc = timer->probe() ) == 0 ) ) {
  109. DBGC ( &timer, "TIMER using %s\n", timer->name );
  110. return;
  111. }
  112. DBGC ( &timer, "TIMER could not initialise %s: %s\n",
  113. timer->name, strerror ( rc ) );
  114. }
  115. /* This is a fatal error */
  116. DBGC ( &timer, "TIMER found no working timers!\n" );
  117. while ( 1 ) {}
  118. }
  119. /** Timer initialisation function */
  120. struct init_fn timer_init_fn __init_fn ( INIT_EARLY ) = {
  121. .initialise = timer_probe,
  122. };
  123. /* Drag in timer configuration */
  124. REQUIRING_SYMBOL ( timer_init_fn );
  125. REQUIRE_OBJECT ( config_timer );