timer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Alex Zuepke <azu@sysgo.de>
  9. *
  10. * (C) Copyright 2002
  11. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #if defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB)
  33. #if defined(CONFIG_S3C2400)
  34. #include <s3c2400.h>
  35. #elif defined(CONFIG_S3C2410)
  36. #include <s3c2410.h>
  37. #endif
  38. int timer_load_val = 0;
  39. /* macro to read the 16 bit timer */
  40. static inline ulong READ_TIMER(void)
  41. {
  42. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  43. return (timers->TCNTO4 & 0xffff);
  44. }
  45. static ulong timestamp;
  46. static ulong lastdec;
  47. int timer_init (void)
  48. {
  49. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  50. /* use PWM Timer 4 because it has no output */
  51. /* prescaler for Timer 4 is 16 */
  52. timers->TCFG0 = 0x0f00;
  53. if (timer_load_val == 0)
  54. {
  55. /*
  56. * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
  57. * (default) and prescaler = 16. Should be 10390
  58. * @33.25MHz and 15625 @ 50 MHz
  59. */
  60. timer_load_val = get_PCLK()/(2 * 16 * 100);
  61. }
  62. /* load value for 10 ms timeout */
  63. lastdec = timers->TCNTB4 = timer_load_val;
  64. /* auto load, manual update of Timer 4 */
  65. timers->TCON = (timers->TCON & ~0x0700000) | 0x600000;
  66. /* auto load, start Timer 4 */
  67. timers->TCON = (timers->TCON & ~0x0700000) | 0x500000;
  68. timestamp = 0;
  69. return (0);
  70. }
  71. /*
  72. * timer without interrupts
  73. */
  74. void reset_timer (void)
  75. {
  76. reset_timer_masked ();
  77. }
  78. ulong get_timer (ulong base)
  79. {
  80. return get_timer_masked () - base;
  81. }
  82. void set_timer (ulong t)
  83. {
  84. timestamp = t;
  85. }
  86. void udelay (unsigned long usec)
  87. {
  88. ulong tmo;
  89. ulong start = get_timer(0);
  90. tmo = usec / 1000;
  91. tmo *= (timer_load_val * 100);
  92. tmo /= 1000;
  93. while ((ulong)(get_timer_masked () - start) < tmo)
  94. /*NOP*/;
  95. }
  96. void reset_timer_masked (void)
  97. {
  98. /* reset time */
  99. lastdec = READ_TIMER();
  100. timestamp = 0;
  101. }
  102. ulong get_timer_masked (void)
  103. {
  104. ulong now = READ_TIMER();
  105. if (lastdec >= now) {
  106. /* normal mode */
  107. timestamp += lastdec - now;
  108. } else {
  109. /* we have an overflow ... */
  110. timestamp += lastdec + timer_load_val - now;
  111. }
  112. lastdec = now;
  113. return timestamp;
  114. }
  115. void udelay_masked (unsigned long usec)
  116. {
  117. ulong tmo;
  118. ulong endtime;
  119. signed long diff;
  120. if (usec >= 1000) {
  121. tmo = usec / 1000;
  122. tmo *= (timer_load_val * 100);
  123. tmo /= 1000;
  124. } else {
  125. tmo = usec * (timer_load_val * 100);
  126. tmo /= (1000*1000);
  127. }
  128. endtime = get_timer_masked () + tmo;
  129. do {
  130. ulong now = get_timer_masked ();
  131. diff = endtime - now;
  132. } while (diff >= 0);
  133. }
  134. /*
  135. * This function is derived from PowerPC code (read timebase as long long).
  136. * On ARM it just returns the timer value.
  137. */
  138. unsigned long long get_ticks(void)
  139. {
  140. return get_timer(0);
  141. }
  142. /*
  143. * This function is derived from PowerPC code (timebase clock frequency).
  144. * On ARM it returns the number of timer ticks per second.
  145. */
  146. ulong get_tbclk (void)
  147. {
  148. ulong tbclk;
  149. #if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB)
  150. tbclk = timer_load_val * 100;
  151. #elif defined(CONFIG_SBC2410X) || \
  152. defined(CONFIG_SMDK2410) || \
  153. defined(CONFIG_VCMA9)
  154. tbclk = CONFIG_SYS_HZ;
  155. #else
  156. # error "tbclk not configured"
  157. #endif
  158. return tbclk;
  159. }
  160. /*
  161. * reset the cpu by setting up the watchdog timer and let him time out
  162. */
  163. void reset_cpu (ulong ignored)
  164. {
  165. volatile S3C24X0_WATCHDOG * watchdog;
  166. #ifdef CONFIG_TRAB
  167. extern void disable_vfd (void);
  168. disable_vfd();
  169. #endif
  170. watchdog = S3C24X0_GetBase_WATCHDOG();
  171. /* Disable watchdog */
  172. watchdog->WTCON = 0x0000;
  173. /* Initialize watchdog timer count register */
  174. watchdog->WTCNT = 0x0001;
  175. /* Enable watchdog timer; assert reset at timer timeout */
  176. watchdog->WTCON = 0x0021;
  177. while(1); /* loop forever and wait for reset to happen */
  178. /*NOTREACHED*/
  179. }
  180. #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */