選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

timer.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * core/timer.c
  3. *
  4. * Copyright (C) 2007 Alexey Zaytsev <alexey.zaytsev@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stddef.h>
  21. #include <assert.h>
  22. #include <gpxe/timer.h>
  23. static struct timer ts_table[0]
  24. __table_start ( struct timer, timers );
  25. static struct timer ts_table_end[0]
  26. __table_end ( struct timer, timers );
  27. /*
  28. * This function may be used in custom timer driver.
  29. *
  30. * This udelay implementation works well if you've got a
  31. * fast currticks().
  32. */
  33. void generic_currticks_udelay ( unsigned int usecs ) {
  34. tick_t start;
  35. tick_t elapsed;
  36. start = currticks();
  37. do {
  38. /* xxx: Relax the cpu some way. */
  39. elapsed = ( currticks() - start );
  40. } while ( elapsed < usecs );
  41. }
  42. /**
  43. * Identify timer source
  44. *
  45. * @ret timer Timer source
  46. */
  47. static struct timer * timer ( void ) {
  48. static struct timer *ts = NULL;
  49. /* If we have a timer, use it */
  50. if ( ts )
  51. return ts;
  52. /* Scan for a usable timer */
  53. for ( ts = ts_table ; ts < ts_table_end ; ts++ ) {
  54. if ( ts->init() == 0 )
  55. return ts;
  56. }
  57. /* No timer found; we cannot continue */
  58. assert ( 0 );
  59. while ( 1 ) {};
  60. }
  61. /**
  62. * Read current time
  63. *
  64. * @ret ticks Current time, in ticks
  65. */
  66. tick_t currticks ( void ) {
  67. tick_t ct;
  68. ct = timer()->currticks();
  69. DBG ( "currticks: %ld.%06ld seconds\n",
  70. ct / USECS_IN_SEC, ct % USECS_IN_SEC );
  71. return ct;
  72. }
  73. /**
  74. * Delay
  75. *
  76. * @v usecs Time to delay, in microseconds
  77. */
  78. void udelay ( unsigned int usecs ) {
  79. timer()->udelay ( usecs );
  80. }
  81. /**
  82. * Delay
  83. *
  84. * @v msecs Time to delay, in milliseconds
  85. */
  86. void mdelay ( unsigned int msecs ) {
  87. while ( msecs-- )
  88. udelay ( USECS_IN_MSEC );
  89. }
  90. /**
  91. * Delay
  92. *
  93. * @v secs Time to delay, in seconds
  94. */
  95. unsigned int sleep ( unsigned int secs ) {
  96. while ( secs-- )
  97. mdelay ( MSECS_IN_SEC );
  98. return 0;
  99. }