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.0KB

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