timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. ulong get_timer (ulong base)
  66. {
  67. return (get_timer_masked() - base);
  68. }
  69. void __udelay (unsigned long usec)
  70. {
  71. ulong tmo,tmp;
  72. /* normalize */
  73. if (usec >= 1000) {
  74. tmo = usec / 1000;
  75. tmo *= CONFIG_SYS_HZ;
  76. tmo /= 1000;
  77. }
  78. else {
  79. if (usec > 1) {
  80. tmo = usec * CONFIG_SYS_HZ;
  81. tmo /= (1000*1000);
  82. }
  83. else
  84. tmo = 1;
  85. }
  86. /* check for rollover during this delay */
  87. tmp = get_timer (0);
  88. if ((tmp + tmo) < tmp )
  89. reset_timer_masked(); /* timer would roll over */
  90. else
  91. tmo += tmp;
  92. while (get_timer_masked () < tmo);
  93. }
  94. void reset_timer_masked (void)
  95. {
  96. /* reset time */
  97. lastdec = READ_TIMER();
  98. timestamp = 0;
  99. }
  100. ulong get_timer_masked (void)
  101. {
  102. ulong now = READ_TIMER();
  103. if (lastdec >= now) {
  104. /* normal mode */
  105. timestamp += (lastdec - now);
  106. } else {
  107. /* we have an overflow ... */
  108. timestamp += ((lastdec + timer_load_val) - now);
  109. }
  110. lastdec = now;
  111. return timestamp;
  112. }
  113. void udelay_masked (unsigned long usec)
  114. {
  115. ulong tmo;
  116. ulong endtime;
  117. signed long diff;
  118. /* normalize */
  119. if (usec >= 1000) {
  120. tmo = usec / 1000;
  121. tmo *= CONFIG_SYS_HZ;
  122. tmo /= 1000;
  123. } else {
  124. if (usec > 1) {
  125. tmo = usec * CONFIG_SYS_HZ;
  126. tmo /= (1000*1000);
  127. } else {
  128. tmo = 1;
  129. }
  130. }
  131. endtime = get_timer_masked () + tmo;
  132. do {
  133. ulong now = get_timer_masked ();
  134. diff = endtime - now;
  135. } while (diff >= 0);
  136. }
  137. /*
  138. * This function is derived from PowerPC code (read timebase as long long).
  139. * On ARM it just returns the timer value.
  140. */
  141. unsigned long long get_ticks(void)
  142. {
  143. return get_timer(0);
  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. tbclk = timer_load_val * 100;
  153. return tbclk;
  154. }