timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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) || \
  33. defined(CONFIG_S3C2410) || \
  34. defined(CONFIG_TRAB)
  35. #include <asm/io.h>
  36. #if defined(CONFIG_S3C2400)
  37. #include <s3c2400.h>
  38. #elif defined(CONFIG_S3C2410)
  39. #include <s3c2410.h>
  40. #endif
  41. int timer_load_val = 0;
  42. static ulong timer_clk;
  43. /* macro to read the 16 bit timer */
  44. static inline ulong READ_TIMER(void)
  45. {
  46. struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
  47. return readl(&timers->TCNTO4) & 0xffff;
  48. }
  49. static ulong timestamp;
  50. static ulong lastdec;
  51. int timer_init(void)
  52. {
  53. struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
  54. ulong tmr;
  55. /* use PWM Timer 4 because it has no output */
  56. /* prescaler for Timer 4 is 16 */
  57. writel(0x0f00, &timers->TCFG0);
  58. if (timer_load_val == 0) {
  59. /*
  60. * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
  61. * (default) and prescaler = 16. Should be 10390
  62. * @33.25MHz and 15625 @ 50 MHz
  63. */
  64. timer_load_val = get_PCLK() / (2 * 16 * 100);
  65. timer_clk = get_PCLK() / (2 * 16);
  66. }
  67. /* load value for 10 ms timeout */
  68. lastdec = timer_load_val;
  69. writel(timer_load_val, &timers->TCNTB4);
  70. /* auto load, manual update of Timer 4 */
  71. tmr = (readl(&timers->TCON) & ~0x0700000) | 0x0600000;
  72. writel(tmr, &timers->TCON);
  73. /* auto load, start Timer 4 */
  74. tmr = (tmr & ~0x0700000) | 0x0500000;
  75. writel(tmr, &timers->TCON);
  76. timestamp = 0;
  77. return (0);
  78. }
  79. /*
  80. * timer without interrupts
  81. */
  82. void reset_timer(void)
  83. {
  84. reset_timer_masked();
  85. }
  86. ulong get_timer(ulong base)
  87. {
  88. return get_timer_masked() - base;
  89. }
  90. void set_timer(ulong t)
  91. {
  92. timestamp = t;
  93. }
  94. void udelay(unsigned long usec)
  95. {
  96. ulong tmo;
  97. ulong start = get_ticks();
  98. tmo = usec / 1000;
  99. tmo *= (timer_load_val * 100);
  100. tmo /= 1000;
  101. while ((ulong) (get_ticks() - start) < tmo)
  102. /*NOP*/;
  103. }
  104. void reset_timer_masked(void)
  105. {
  106. /* reset time */
  107. lastdec = READ_TIMER();
  108. timestamp = 0;
  109. }
  110. ulong get_timer_masked(void)
  111. {
  112. ulong tmr = get_ticks();
  113. return tmr / (timer_clk / CONFIG_SYS_HZ);
  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_ticks() + tmo;
  129. do {
  130. ulong now = get_ticks();
  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. ulong now = READ_TIMER();
  141. if (lastdec >= now) {
  142. /* normal mode */
  143. timestamp += lastdec - now;
  144. } else {
  145. /* we have an overflow ... */
  146. timestamp += lastdec + timer_load_val - now;
  147. }
  148. lastdec = now;
  149. return timestamp;
  150. }
  151. /*
  152. * This function is derived from PowerPC code (timebase clock frequency).
  153. * On ARM it returns the number of timer ticks per second.
  154. */
  155. ulong get_tbclk(void)
  156. {
  157. ulong tbclk;
  158. #if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB)
  159. tbclk = timer_load_val * 100;
  160. #elif defined(CONFIG_SBC2410X) || \
  161. defined(CONFIG_SMDK2410) || \
  162. defined(CONFIG_VCMA9)
  163. tbclk = CONFIG_SYS_HZ;
  164. #else
  165. # error "tbclk not configured"
  166. #endif
  167. return tbclk;
  168. }
  169. /*
  170. * reset the cpu by setting up the watchdog timer and let him time out
  171. */
  172. void reset_cpu(ulong ignored)
  173. {
  174. struct s3c24x0_watchdog *watchdog;
  175. #ifdef CONFIG_TRAB
  176. extern void disable_vfd(void);
  177. disable_vfd();
  178. #endif
  179. watchdog = s3c24x0_get_base_watchdog();
  180. /* Disable watchdog */
  181. writel(0x0000, &watchdog->WTCON);
  182. /* Initialize watchdog timer count register */
  183. writel(0x0001, &watchdog->WTCNT);
  184. /* Enable watchdog timer; assert reset at timer timeout */
  185. writel(0x0021, &watchdog->WTCON);
  186. while (1)
  187. /* loop forever and wait for reset to happen */;
  188. /*NOTREACHED*/
  189. }
  190. #endif /* defined(CONFIG_S3C2400) ||
  191. defined (CONFIG_S3C2410) ||
  192. defined (CONFIG_TRAB) */