timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <arm920t.h>
  33. #include <lh7a40x.h>
  34. static ulong timer_load_val = 0;
  35. /* macro to read the 16 bit timer */
  36. static inline ulong READ_TIMER(void)
  37. {
  38. lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
  39. lh7a40x_timer_t* timer = &timers->timer1;
  40. return (timer->value & 0x0000ffff);
  41. }
  42. static ulong timestamp;
  43. static ulong lastdec;
  44. int timer_init (void)
  45. {
  46. lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
  47. lh7a40x_timer_t* timer = &timers->timer1;
  48. /* a periodic timer using the 508kHz source */
  49. timer->control = (TIMER_PER | TIMER_CLK508K);
  50. if (timer_load_val == 0) {
  51. /*
  52. * 10ms period with 508.469kHz clock = 5084
  53. */
  54. timer_load_val = CONFIG_SYS_HZ/100;
  55. }
  56. /* load value for 10 ms timeout */
  57. lastdec = timer->load = timer_load_val;
  58. /* auto load, start timer */
  59. timer->control = timer->control | TIMER_EN;
  60. timestamp = 0;
  61. return (0);
  62. }
  63. /*
  64. * timer without interrupts
  65. */
  66. void reset_timer (void)
  67. {
  68. reset_timer_masked ();
  69. }
  70. ulong get_timer (ulong base)
  71. {
  72. return (get_timer_masked() - base);
  73. }
  74. void set_timer (ulong t)
  75. {
  76. timestamp = t;
  77. }
  78. void udelay (unsigned long usec)
  79. {
  80. ulong tmo,tmp;
  81. /* normalize */
  82. if (usec >= 1000) {
  83. tmo = usec / 1000;
  84. tmo *= CONFIG_SYS_HZ;
  85. tmo /= 1000;
  86. }
  87. else {
  88. if (usec > 1) {
  89. tmo = usec * CONFIG_SYS_HZ;
  90. tmo /= (1000*1000);
  91. }
  92. else
  93. tmo = 1;
  94. }
  95. /* check for rollover during this delay */
  96. tmp = get_timer (0);
  97. if ((tmp + tmo) < tmp )
  98. reset_timer_masked(); /* timer would roll over */
  99. else
  100. tmo += tmp;
  101. while (get_timer_masked () < tmo);
  102. }
  103. void reset_timer_masked (void)
  104. {
  105. /* reset time */
  106. lastdec = READ_TIMER();
  107. timestamp = 0;
  108. }
  109. ulong get_timer_masked (void)
  110. {
  111. ulong now = READ_TIMER();
  112. if (lastdec >= now) {
  113. /* normal mode */
  114. timestamp += (lastdec - now);
  115. } else {
  116. /* we have an overflow ... */
  117. timestamp += ((lastdec + timer_load_val) - now);
  118. }
  119. lastdec = now;
  120. return timestamp;
  121. }
  122. void udelay_masked (unsigned long usec)
  123. {
  124. ulong tmo;
  125. ulong endtime;
  126. signed long diff;
  127. /* normalize */
  128. if (usec >= 1000) {
  129. tmo = usec / 1000;
  130. tmo *= CONFIG_SYS_HZ;
  131. tmo /= 1000;
  132. } else {
  133. if (usec > 1) {
  134. tmo = usec * CONFIG_SYS_HZ;
  135. tmo /= (1000*1000);
  136. } else {
  137. tmo = 1;
  138. }
  139. }
  140. endtime = get_timer_masked () + tmo;
  141. do {
  142. ulong now = get_timer_masked ();
  143. diff = endtime - now;
  144. } while (diff >= 0);
  145. }
  146. /*
  147. * This function is derived from PowerPC code (read timebase as long long).
  148. * On ARM it just returns the timer value.
  149. */
  150. unsigned long long get_ticks(void)
  151. {
  152. return get_timer(0);
  153. }
  154. /*
  155. * This function is derived from PowerPC code (timebase clock frequency).
  156. * On ARM it returns the number of timer ticks per second.
  157. */
  158. ulong get_tbclk (void)
  159. {
  160. ulong tbclk;
  161. tbclk = timer_load_val * 100;
  162. return tbclk;
  163. }