timer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. void reset_timer(void)
  77. {
  78. reset_timer_masked();
  79. }
  80. ulong get_timer(ulong base)
  81. {
  82. return get_timer_masked() - base;
  83. }
  84. void set_timer(ulong t)
  85. {
  86. timestamp = t;
  87. }
  88. void __udelay (unsigned long usec)
  89. {
  90. ulong tmo;
  91. ulong start = get_ticks();
  92. tmo = usec / 1000;
  93. tmo *= (timer_load_val * 100);
  94. tmo /= 1000;
  95. while ((ulong) (get_ticks() - start) < tmo)
  96. /*NOP*/;
  97. }
  98. void reset_timer_masked(void)
  99. {
  100. /* reset time */
  101. lastdec = READ_TIMER();
  102. timestamp = 0;
  103. }
  104. ulong get_timer_masked(void)
  105. {
  106. ulong tmr = get_ticks();
  107. return tmr / (timer_clk / CONFIG_SYS_HZ);
  108. }
  109. void udelay_masked(unsigned long usec)
  110. {
  111. ulong tmo;
  112. ulong endtime;
  113. signed long diff;
  114. if (usec >= 1000) {
  115. tmo = usec / 1000;
  116. tmo *= (timer_load_val * 100);
  117. tmo /= 1000;
  118. } else {
  119. tmo = usec * (timer_load_val * 100);
  120. tmo /= (1000 * 1000);
  121. }
  122. endtime = get_ticks() + tmo;
  123. do {
  124. ulong now = get_ticks();
  125. diff = endtime - now;
  126. } while (diff >= 0);
  127. }
  128. /*
  129. * This function is derived from PowerPC code (read timebase as long long).
  130. * On ARM it just returns the timer value.
  131. */
  132. unsigned long long get_ticks(void)
  133. {
  134. ulong now = READ_TIMER();
  135. if (lastdec >= now) {
  136. /* normal mode */
  137. timestamp += lastdec - now;
  138. } else {
  139. /* we have an overflow ... */
  140. timestamp += lastdec + timer_load_val - now;
  141. }
  142. lastdec = now;
  143. return timestamp;
  144. }
  145. /*
  146. * This function is derived from PowerPC code (timebase clock frequency).
  147. * On ARM it returns the number of timer ticks per second.
  148. */
  149. ulong get_tbclk(void)
  150. {
  151. ulong tbclk;
  152. #if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB)
  153. tbclk = timer_load_val * 100;
  154. #elif defined(CONFIG_SBC2410X) || \
  155. defined(CONFIG_SMDK2410) || \
  156. defined(CONFIG_S3C2440) || \
  157. defined(CONFIG_VCMA9)
  158. tbclk = CONFIG_SYS_HZ;
  159. #else
  160. # error "tbclk not configured"
  161. #endif
  162. return tbclk;
  163. }
  164. /*
  165. * reset the cpu by setting up the watchdog timer and let him time out
  166. */
  167. void reset_cpu(ulong ignored)
  168. {
  169. struct s3c24x0_watchdog *watchdog;
  170. #ifdef CONFIG_TRAB
  171. extern void disable_vfd(void);
  172. disable_vfd();
  173. #endif
  174. watchdog = s3c24x0_get_base_watchdog();
  175. /* Disable watchdog */
  176. writel(0x0000, &watchdog->wtcon);
  177. /* Initialize watchdog timer count register */
  178. writel(0x0001, &watchdog->wtcnt);
  179. /* Enable watchdog timer; assert reset at timer timeout */
  180. writel(0x0021, &watchdog->wtcon);
  181. while (1)
  182. /* loop forever and wait for reset to happen */;
  183. /*NOTREACHED*/
  184. }
  185. #endif /* CONFIG_S3C24X0 */