timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * (C) Copyright 2003
  3. * Texas Instruments <www.ti.com>
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * (C) Copyright 2002-2004
  14. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  15. *
  16. * (C) Copyright 2004
  17. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  18. *
  19. * (C) Copyright 2008
  20. * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
  21. *
  22. * See file CREDITS for list of people who contributed to this
  23. * project.
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License as
  27. * published by the Free Software Foundation; either version 2 of
  28. * the License, or (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  38. * MA 02111-1307 USA
  39. */
  40. #include <common.h>
  41. #include <asm/proc-armv/ptrace.h>
  42. #include <s3c6400.h>
  43. #include <div64.h>
  44. static ulong timer_load_val;
  45. #define PRESCALER 167
  46. static s3c64xx_timers *s3c64xx_get_base_timers(void)
  47. {
  48. return (s3c64xx_timers *)ELFIN_TIMER_BASE;
  49. }
  50. /* macro to read the 16 bit timer */
  51. static inline ulong read_timer(void)
  52. {
  53. s3c64xx_timers *const timers = s3c64xx_get_base_timers();
  54. return timers->TCNTO4;
  55. }
  56. /* Internal tick units */
  57. /* Last decremneter snapshot */
  58. static unsigned long lastdec;
  59. /* Monotonic incrementing timer */
  60. static unsigned long long timestamp;
  61. int timer_init(void)
  62. {
  63. s3c64xx_timers *const timers = s3c64xx_get_base_timers();
  64. /* use PWM Timer 4 because it has no output */
  65. /*
  66. * We use the following scheme for the timer:
  67. * Prescaler is hard fixed at 167, divider at 1/4.
  68. * This gives at PCLK frequency 66MHz approx. 10us ticks
  69. * The timer is set to wrap after 100s, at 66MHz this obviously
  70. * happens after 10,000,000 ticks. A long variable can thus
  71. * keep values up to 40,000s, i.e., 11 hours. This should be
  72. * enough for most uses:-) Possible optimizations: select a
  73. * binary-friendly frequency, e.g., 1ms / 128. Also calculate
  74. * the prescaler automatically for other PCLK frequencies.
  75. */
  76. timers->TCFG0 = PRESCALER << 8;
  77. if (timer_load_val == 0) {
  78. timer_load_val = get_PCLK() / PRESCALER * (100 / 4); /* 100s */
  79. timers->TCFG1 = (timers->TCFG1 & ~0xf0000) | 0x20000;
  80. }
  81. /* load value for 10 ms timeout */
  82. lastdec = timers->TCNTB4 = timer_load_val;
  83. /* auto load, manual update of Timer 4 */
  84. timers->TCON = (timers->TCON & ~0x00700000) | TCON_4_AUTO |
  85. TCON_4_UPDATE;
  86. /* auto load, start Timer 4 */
  87. timers->TCON = (timers->TCON & ~0x00700000) | TCON_4_AUTO | COUNT_4_ON;
  88. timestamp = 0;
  89. return 0;
  90. }
  91. /*
  92. * timer without interrupts
  93. */
  94. /*
  95. * This function is derived from PowerPC code (read timebase as long long).
  96. * On ARM it just returns the timer value.
  97. */
  98. unsigned long long get_ticks(void)
  99. {
  100. ulong now = read_timer();
  101. if (lastdec >= now) {
  102. /* normal mode */
  103. timestamp += lastdec - now;
  104. } else {
  105. /* we have an overflow ... */
  106. timestamp += lastdec + timer_load_val - now;
  107. }
  108. lastdec = now;
  109. return timestamp;
  110. }
  111. /*
  112. * This function is derived from PowerPC code (timebase clock frequency).
  113. * On ARM it returns the number of timer ticks per second.
  114. */
  115. ulong get_tbclk(void)
  116. {
  117. /* We overrun in 100s */
  118. return (ulong)(timer_load_val / 100);
  119. }
  120. void reset_timer_masked(void)
  121. {
  122. /* reset time */
  123. lastdec = read_timer();
  124. timestamp = 0;
  125. }
  126. void reset_timer(void)
  127. {
  128. reset_timer_masked();
  129. }
  130. ulong get_timer_masked(void)
  131. {
  132. unsigned long long res = get_ticks();
  133. do_div (res, (timer_load_val / (100 * CONFIG_SYS_HZ)));
  134. return res;
  135. }
  136. ulong get_timer(ulong base)
  137. {
  138. return get_timer_masked() - base;
  139. }
  140. void set_timer(ulong t)
  141. {
  142. timestamp = t * (timer_load_val / (100 * CONFIG_SYS_HZ));
  143. }
  144. void udelay(unsigned long usec)
  145. {
  146. unsigned long long tmp;
  147. ulong tmo;
  148. tmo = (usec + 9) / 10;
  149. tmp = get_ticks() + tmo; /* get current timestamp */
  150. while (get_ticks() < tmp)/* loop till event */
  151. /*NOP*/;
  152. }