timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #ifdef CONFIG_S3C24X0
  33. #include <asm/io.h>
  34. #include <asm/arch/s3c24x0_cpu.h>
  35. int timer_load_val = 0;
  36. static ulong timer_clk;
  37. /* macro to read the 16 bit timer */
  38. static inline ulong READ_TIMER(void)
  39. {
  40. struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
  41. return readl(&timers->tcnto4) & 0xffff;
  42. }
  43. static ulong timestamp;
  44. static ulong lastdec;
  45. int timer_init(void)
  46. {
  47. struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
  48. ulong tmr;
  49. /* use PWM Timer 4 because it has no output */
  50. /* prescaler for Timer 4 is 16 */
  51. writel(0x0f00, &timers->tcfg0);
  52. if (timer_load_val == 0) {
  53. /*
  54. * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
  55. * (default) and prescaler = 16. Should be 10390
  56. * @33.25MHz and 15625 @ 50 MHz
  57. */
  58. timer_load_val = get_PCLK() / (2 * 16 * 100);
  59. timer_clk = get_PCLK() / (2 * 16);
  60. }
  61. /* load value for 10 ms timeout */
  62. lastdec = timer_load_val;
  63. writel(timer_load_val, &timers->tcntb4);
  64. /* auto load, manual update of timer 4 */
  65. tmr = (readl(&timers->tcon) & ~0x0700000) | 0x0600000;
  66. writel(tmr, &timers->tcon);
  67. /* auto load, start timer 4 */
  68. tmr = (tmr & ~0x0700000) | 0x0500000;
  69. writel(tmr, &timers->tcon);
  70. timestamp = 0;
  71. return (0);
  72. }
  73. /*
  74. * timer without interrupts
  75. */
  76. ulong get_timer(ulong base)
  77. {
  78. return get_timer_masked() - base;
  79. }
  80. void __udelay (unsigned long usec)
  81. {
  82. ulong tmo;
  83. ulong start = get_ticks();
  84. tmo = usec / 1000;
  85. tmo *= (timer_load_val * 100);
  86. tmo /= 1000;
  87. while ((ulong) (get_ticks() - start) < tmo)
  88. /*NOP*/;
  89. }
  90. ulong get_timer_masked(void)
  91. {
  92. ulong tmr = get_ticks();
  93. return tmr / (timer_clk / CONFIG_SYS_HZ);
  94. }
  95. void udelay_masked(unsigned long usec)
  96. {
  97. ulong tmo;
  98. ulong endtime;
  99. signed long diff;
  100. if (usec >= 1000) {
  101. tmo = usec / 1000;
  102. tmo *= (timer_load_val * 100);
  103. tmo /= 1000;
  104. } else {
  105. tmo = usec * (timer_load_val * 100);
  106. tmo /= (1000 * 1000);
  107. }
  108. endtime = get_ticks() + tmo;
  109. do {
  110. ulong now = get_ticks();
  111. diff = endtime - now;
  112. } while (diff >= 0);
  113. }
  114. /*
  115. * This function is derived from PowerPC code (read timebase as long long).
  116. * On ARM it just returns the timer value.
  117. */
  118. unsigned long long get_ticks(void)
  119. {
  120. ulong now = READ_TIMER();
  121. if (lastdec >= now) {
  122. /* normal mode */
  123. timestamp += lastdec - now;
  124. } else {
  125. /* we have an overflow ... */
  126. timestamp += lastdec + timer_load_val - now;
  127. }
  128. lastdec = now;
  129. return timestamp;
  130. }
  131. /*
  132. * This function is derived from PowerPC code (timebase clock frequency).
  133. * On ARM it returns the number of timer ticks per second.
  134. */
  135. ulong get_tbclk(void)
  136. {
  137. ulong tbclk;
  138. #if defined(CONFIG_SMDK2400)
  139. tbclk = timer_load_val * 100;
  140. #elif defined(CONFIG_SBC2410X) || \
  141. defined(CONFIG_SMDK2410) || \
  142. defined(CONFIG_S3C2440) || \
  143. defined(CONFIG_VCMA9)
  144. tbclk = CONFIG_SYS_HZ;
  145. #else
  146. # error "tbclk not configured"
  147. #endif
  148. return tbclk;
  149. }
  150. /*
  151. * reset the cpu by setting up the watchdog timer and let him time out
  152. */
  153. void reset_cpu(ulong ignored)
  154. {
  155. struct s3c24x0_watchdog *watchdog;
  156. watchdog = s3c24x0_get_base_watchdog();
  157. /* Disable watchdog */
  158. writel(0x0000, &watchdog->wtcon);
  159. /* Initialize watchdog timer count register */
  160. writel(0x0001, &watchdog->wtcnt);
  161. /* Enable watchdog timer; assert reset at timer timeout */
  162. writel(0x0021, &watchdog->wtcon);
  163. while (1)
  164. /* loop forever and wait for reset to happen */;
  165. /*NOTREACHED*/
  166. }
  167. #endif /* CONFIG_S3C24X0 */