time.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * (C) Copyright 2003 Josef Baumgartner <josef.baumgartner@telex.de>
  3. *
  4. * (C) Copyright 2000
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/timer.h>
  27. #include <asm/immap.h>
  28. #include <watchdog.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static volatile ulong timestamp = 0;
  31. #ifndef CONFIG_SYS_WATCHDOG_FREQ
  32. #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
  33. #endif
  34. #if defined(CONFIG_MCFTMR)
  35. #ifndef CONFIG_SYS_UDELAY_BASE
  36. # error "uDelay base not defined!"
  37. #endif
  38. #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
  39. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  40. #endif
  41. extern void dtimer_intr_setup(void);
  42. void udelay(unsigned long usec)
  43. {
  44. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_UDELAY_BASE);
  45. uint start, now, tmp;
  46. while (usec > 0) {
  47. if (usec > 65000)
  48. tmp = 65000;
  49. else
  50. tmp = usec;
  51. usec = usec - tmp;
  52. /* Set up TIMER 3 as timebase clock */
  53. timerp->tmr = DTIM_DTMR_RST_RST;
  54. timerp->tcn = 0;
  55. /* set period to 1 us */
  56. timerp->tmr =
  57. CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
  58. DTIM_DTMR_RST_EN;
  59. start = now = timerp->tcn;
  60. while (now < start + tmp)
  61. now = timerp->tcn;
  62. }
  63. }
  64. void dtimer_interrupt(void *not_used)
  65. {
  66. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
  67. /* check for timer interrupt asserted */
  68. if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
  69. timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
  70. timestamp++;
  71. #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
  72. if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0) {
  73. WATCHDOG_RESET ();
  74. }
  75. #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
  76. return;
  77. }
  78. }
  79. void timer_init(void)
  80. {
  81. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
  82. timestamp = 0;
  83. timerp->tcn = 0;
  84. timerp->trr = 0;
  85. /* Set up TIMER 4 as clock */
  86. timerp->tmr = DTIM_DTMR_RST_RST;
  87. /* initialize and enable timer interrupt */
  88. irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
  89. timerp->tcn = 0;
  90. timerp->trr = 1000; /* Interrupt every ms */
  91. dtimer_intr_setup();
  92. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  93. timerp->tmr = CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
  94. DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
  95. }
  96. void reset_timer(void)
  97. {
  98. timestamp = 0;
  99. }
  100. ulong get_timer(ulong base)
  101. {
  102. return (timestamp - base);
  103. }
  104. void set_timer(ulong t)
  105. {
  106. timestamp = t;
  107. }
  108. #endif /* CONFIG_MCFTMR */
  109. #if defined(CONFIG_MCFPIT)
  110. #if !defined(CONFIG_SYS_PIT_BASE)
  111. # error "CONFIG_SYS_PIT_BASE not defined!"
  112. #endif
  113. static unsigned short lastinc;
  114. void udelay(unsigned long usec)
  115. {
  116. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_UDELAY_BASE);
  117. uint tmp;
  118. while (usec > 0) {
  119. if (usec > 65000)
  120. tmp = 65000;
  121. else
  122. tmp = usec;
  123. usec = usec - tmp;
  124. /* Set up TIMER 3 as timebase clock */
  125. timerp->pcsr = PIT_PCSR_OVW;
  126. timerp->pmr = 0;
  127. /* set period to 1 us */
  128. timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
  129. timerp->pmr = tmp;
  130. while (timerp->pcntr > 0) ;
  131. }
  132. }
  133. void timer_init(void)
  134. {
  135. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
  136. timestamp = 0;
  137. /* Set up TIMER 4 as poll clock */
  138. timerp->pcsr = PIT_PCSR_OVW;
  139. timerp->pmr = lastinc = 0;
  140. timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
  141. }
  142. void set_timer(ulong t)
  143. {
  144. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
  145. timestamp = 0;
  146. timerp->pmr = lastinc = 0;
  147. }
  148. ulong get_timer(ulong base)
  149. {
  150. unsigned short now, diff;
  151. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
  152. now = timerp->pcntr;
  153. diff = -(now - lastinc);
  154. timestamp += diff;
  155. lastinc = now;
  156. return timestamp - base;
  157. }
  158. void wait_ticks(unsigned long ticks)
  159. {
  160. set_timer(0);
  161. while (get_timer(0) < ticks) ;
  162. }
  163. #endif /* CONFIG_MCFPIT */
  164. /*
  165. * This function is derived from PowerPC code (read timebase as long long).
  166. * On M68K it just returns the timer value.
  167. */
  168. unsigned long long get_ticks(void)
  169. {
  170. return get_timer(0);
  171. }
  172. unsigned long usec2ticks(unsigned long usec)
  173. {
  174. return get_timer(usec);
  175. }
  176. /*
  177. * This function is derived from PowerPC code (timebase clock frequency).
  178. * On M68K it returns the number of timer ticks per second.
  179. */
  180. ulong get_tbclk(void)
  181. {
  182. ulong tbclk;
  183. tbclk = CONFIG_SYS_HZ;
  184. return tbclk;
  185. }