timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * (C) Copyright 2002
  7. * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
  8. *
  9. * (C) Copyright 2003
  10. * Texas Instruments, <www.ti.com>
  11. * Kshitij Gupta <Kshitij@ti.com>
  12. *
  13. * (C) Copyright 2004
  14. * ARM Ltd.
  15. * Philippe Robin, <philippe.robin@arm.com>
  16. *
  17. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of
  23. * the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  33. * MA 02111-1307 USA
  34. */
  35. #include <common.h>
  36. /* The Integrator/AP timer1 is clocked at 24MHz
  37. * can be divided by 16 or 256
  38. * and is a 16-bit counter
  39. */
  40. /* U-Boot expects a 32 bit timer running at CONFIG_SYS_HZ*/
  41. static ulong timestamp; /* U-Boot ticks since startup */
  42. static ulong total_count = 0; /* Total timer count */
  43. static ulong lastdec; /* Timer reading at last call */
  44. static ulong div_clock = 256; /* Divisor applied to the timer clock */
  45. static ulong div_timer = 1; /* Divisor to convert timer reading
  46. * change to U-Boot ticks
  47. */
  48. /* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */
  49. #define TIMER_LOAD_VAL 0x0000FFFFL
  50. #define READ_TIMER ((*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) & 0x0000FFFFL)
  51. /* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec
  52. * - unless otherwise stated
  53. */
  54. /* starts a counter
  55. * - the Integrator/AP timer issues an interrupt
  56. * each time it reaches zero
  57. */
  58. int timer_init (void)
  59. {
  60. /* Load timer with initial value */
  61. *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL;
  62. /* Set timer to be
  63. * enabled 1
  64. * free-running 0
  65. * XX 00
  66. * divider 256 10
  67. * XX 00
  68. */
  69. *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x00000088;
  70. total_count = 0;
  71. /* init the timestamp and lastdec value */
  72. reset_timer_masked();
  73. div_timer = CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ;
  74. div_timer /= div_clock;
  75. return (0);
  76. }
  77. /*
  78. * timer without interrupts
  79. */
  80. void reset_timer (void)
  81. {
  82. reset_timer_masked ();
  83. }
  84. ulong get_timer (ulong base_ticks)
  85. {
  86. return get_timer_masked () - base_ticks;
  87. }
  88. void set_timer (ulong ticks)
  89. {
  90. timestamp = ticks;
  91. total_count = ticks * div_timer;
  92. reset_timer_masked();
  93. }
  94. /* delay x useconds */
  95. void udelay (unsigned long usec)
  96. {
  97. ulong tmo, tmp;
  98. /* Convert to U-Boot ticks */
  99. tmo = usec * CONFIG_SYS_HZ;
  100. tmo /= (1000000L);
  101. tmp = get_timer_masked(); /* get current timestamp */
  102. tmo += tmp; /* wake up timestamp */
  103. while (get_timer_masked () < tmo) { /* loop till event */
  104. /*NOP*/;
  105. }
  106. }
  107. void reset_timer_masked (void)
  108. {
  109. /* reset time */
  110. lastdec = READ_TIMER; /* capture current decrementer value */
  111. timestamp = 0; /* start "advancing" time stamp from 0 */
  112. }
  113. /* converts the timer reading to U-Boot ticks */
  114. /* the timestamp is the number of ticks since reset */
  115. /* This routine does not detect wraps unless called regularly
  116. ASSUMES a call at least every 16 seconds to detect every reload */
  117. ulong get_timer_masked (void)
  118. {
  119. ulong now = READ_TIMER; /* current count */
  120. if (now > lastdec) {
  121. /* Must have wrapped */
  122. total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
  123. } else {
  124. total_count += lastdec - now;
  125. }
  126. lastdec = now;
  127. timestamp = total_count/div_timer;
  128. return timestamp;
  129. }
  130. /* waits specified delay value and resets timestamp */
  131. void udelay_masked (unsigned long usec)
  132. {
  133. udelay(usec);
  134. }
  135. /*
  136. * This function is derived from PowerPC code (read timebase as long long).
  137. * On ARM it just returns the timer value.
  138. */
  139. unsigned long long get_ticks(void)
  140. {
  141. return get_timer(0);
  142. }
  143. /*
  144. * Return the timebase clock frequency
  145. * i.e. how often the timer decrements
  146. */
  147. ulong get_tbclk (void)
  148. {
  149. return CONFIG_SYS_HZ_CLOCK/div_clock;
  150. }