time.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. DECLARE_GLOBAL_DATA_PTR;
  29. static ulong timestamp;
  30. #if defined(CONFIG_MCFTMR)
  31. #ifndef CFG_UDELAY_BASE
  32. # error "uDelay base not defined!"
  33. #endif
  34. #if !defined(CFG_TMR_BASE) || !defined(CFG_INTR_BASE) || !defined(CFG_TMRINTR_NO) || !defined(CFG_TMRINTR_MASK)
  35. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  36. #endif
  37. extern void dtimer_intr_setup(void);
  38. void udelay(unsigned long usec)
  39. {
  40. volatile dtmr_t *timerp = (dtmr_t *) (CFG_UDELAY_BASE);
  41. uint start, now, tmp;
  42. while (usec > 0) {
  43. if (usec > 65000)
  44. tmp = 65000;
  45. else
  46. tmp = usec;
  47. usec = usec - tmp;
  48. /* Set up TIMER 3 as timebase clock */
  49. timerp->tmr = DTIM_DTMR_RST_RST;
  50. timerp->tcn = 0;
  51. /* set period to 1 us */
  52. timerp->tmr =
  53. CFG_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
  54. DTIM_DTMR_RST_EN;
  55. start = now = timerp->tcn;
  56. while (now < start + tmp)
  57. now = timerp->tcn;
  58. }
  59. }
  60. void dtimer_interrupt(void *not_used)
  61. {
  62. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  63. /* check for timer interrupt asserted */
  64. if ((CFG_TMRPND_REG & CFG_TMRINTR_MASK) == CFG_TMRINTR_PEND) {
  65. timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
  66. timestamp++;
  67. return;
  68. }
  69. }
  70. void timer_init(void)
  71. {
  72. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  73. timestamp = 0;
  74. timerp->tcn = 0;
  75. timerp->trr = 0;
  76. /* Set up TIMER 4 as clock */
  77. timerp->tmr = DTIM_DTMR_RST_RST;
  78. /* initialize and enable timer interrupt */
  79. irq_install_handler(CFG_TMRINTR_NO, dtimer_interrupt, 0);
  80. timerp->tcn = 0;
  81. timerp->trr = 1000; /* Interrupt every ms */
  82. dtimer_intr_setup();
  83. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  84. timerp->tmr = CFG_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
  85. DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
  86. }
  87. void reset_timer(void)
  88. {
  89. timestamp = 0;
  90. }
  91. ulong get_timer(ulong base)
  92. {
  93. return (timestamp - base);
  94. }
  95. void set_timer(ulong t)
  96. {
  97. timestamp = t;
  98. }
  99. #endif /* CONFIG_MCFTMR */
  100. #if defined(CONFIG_MCFPIT)
  101. #if !defined(CFG_PIT_BASE)
  102. # error "CFG_PIT_BASE not defined!"
  103. #endif
  104. static unsigned short lastinc;
  105. void udelay(unsigned long usec)
  106. {
  107. volatile pit_t *timerp = (pit_t *) (CFG_UDELAY_BASE);
  108. uint tmp;
  109. while (usec > 0) {
  110. if (usec > 65000)
  111. tmp = 65000;
  112. else
  113. tmp = usec;
  114. usec = usec - tmp;
  115. /* Set up TIMER 3 as timebase clock */
  116. timerp->pcsr = PIT_PCSR_OVW;
  117. timerp->pmr = 0;
  118. /* set period to 1 us */
  119. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  120. timerp->pmr = tmp;
  121. while (timerp->pcntr > 0) ;
  122. }
  123. }
  124. void timer_init(void)
  125. {
  126. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  127. timestamp = 0;
  128. /* Set up TIMER 4 as poll clock */
  129. timerp->pcsr = PIT_PCSR_OVW;
  130. timerp->pmr = lastinc = 0;
  131. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  132. }
  133. void set_timer(ulong t)
  134. {
  135. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  136. timestamp = 0;
  137. timerp->pmr = lastinc = 0;
  138. }
  139. ulong get_timer(ulong base)
  140. {
  141. unsigned short now, diff;
  142. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  143. now = timerp->pcntr;
  144. diff = -(now - lastinc);
  145. timestamp += diff;
  146. lastinc = now;
  147. return timestamp - base;
  148. }
  149. void wait_ticks(unsigned long ticks)
  150. {
  151. set_timer(0);
  152. while (get_timer(0) < ticks) ;
  153. }
  154. #endif /* CONFIG_MCFPIT */
  155. /*
  156. * This function is derived from PowerPC code (read timebase as long long).
  157. * On M68K it just returns the timer value.
  158. */
  159. unsigned long long get_ticks(void)
  160. {
  161. return get_timer(0);
  162. }
  163. unsigned long usec2ticks(unsigned long usec)
  164. {
  165. return get_timer(usec);
  166. }
  167. /*
  168. * This function is derived from PowerPC code (timebase clock frequency).
  169. * On M68K it returns the number of timer ticks per second.
  170. */
  171. ulong get_tbclk(void)
  172. {
  173. ulong tbclk;
  174. tbclk = CFG_HZ;
  175. return tbclk;
  176. }