timer.c 3.8 KB

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