timer.c 4.0 KB

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