time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <asm/timer.h>
  11. #include <asm/immap.h>
  12. #include <watchdog.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static volatile ulong timestamp = 0;
  15. #ifndef CONFIG_SYS_WATCHDOG_FREQ
  16. #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
  17. #endif
  18. #if defined(CONFIG_MCFTMR)
  19. #ifndef CONFIG_SYS_UDELAY_BASE
  20. # error "uDelay base not defined!"
  21. #endif
  22. #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
  23. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  24. #endif
  25. extern void dtimer_intr_setup(void);
  26. void __udelay(unsigned long usec)
  27. {
  28. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_UDELAY_BASE);
  29. uint start, now, tmp;
  30. while (usec > 0) {
  31. if (usec > 65000)
  32. tmp = 65000;
  33. else
  34. tmp = usec;
  35. usec = usec - tmp;
  36. /* Set up TIMER 3 as timebase clock */
  37. timerp->tmr = DTIM_DTMR_RST_RST;
  38. timerp->tcn = 0;
  39. /* set period to 1 us */
  40. timerp->tmr =
  41. CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
  42. DTIM_DTMR_RST_EN;
  43. start = now = timerp->tcn;
  44. while (now < start + tmp)
  45. now = timerp->tcn;
  46. }
  47. }
  48. void dtimer_interrupt(void *not_used)
  49. {
  50. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
  51. /* check for timer interrupt asserted */
  52. if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
  53. timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
  54. timestamp++;
  55. #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
  56. if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0) {
  57. WATCHDOG_RESET ();
  58. }
  59. #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
  60. return;
  61. }
  62. }
  63. int timer_init(void)
  64. {
  65. volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
  66. timestamp = 0;
  67. timerp->tcn = 0;
  68. timerp->trr = 0;
  69. /* Set up TIMER 4 as clock */
  70. timerp->tmr = DTIM_DTMR_RST_RST;
  71. /* initialize and enable timer interrupt */
  72. irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
  73. timerp->tcn = 0;
  74. timerp->trr = 1000; /* Interrupt every ms */
  75. dtimer_intr_setup();
  76. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  77. timerp->tmr = CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
  78. DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
  79. return 0;
  80. }
  81. ulong get_timer(ulong base)
  82. {
  83. return (timestamp - base);
  84. }
  85. #endif /* CONFIG_MCFTMR */
  86. #if defined(CONFIG_MCFPIT)
  87. #if !defined(CONFIG_SYS_PIT_BASE)
  88. # error "CONFIG_SYS_PIT_BASE not defined!"
  89. #endif
  90. static unsigned short lastinc;
  91. void __udelay(unsigned long usec)
  92. {
  93. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_UDELAY_BASE);
  94. uint tmp;
  95. while (usec > 0) {
  96. if (usec > 65000)
  97. tmp = 65000;
  98. else
  99. tmp = usec;
  100. usec = usec - tmp;
  101. /* Set up TIMER 3 as timebase clock */
  102. timerp->pcsr = PIT_PCSR_OVW;
  103. timerp->pmr = 0;
  104. /* set period to 1 us */
  105. timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
  106. timerp->pmr = tmp;
  107. while (timerp->pcntr > 0) ;
  108. }
  109. }
  110. void timer_init(void)
  111. {
  112. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
  113. timestamp = 0;
  114. /* Set up TIMER 4 as poll clock */
  115. timerp->pcsr = PIT_PCSR_OVW;
  116. timerp->pmr = lastinc = 0;
  117. timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
  118. return 0;
  119. }
  120. ulong get_timer(ulong base)
  121. {
  122. unsigned short now, diff;
  123. volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
  124. now = timerp->pcntr;
  125. diff = -(now - lastinc);
  126. timestamp += diff;
  127. lastinc = now;
  128. return timestamp - base;
  129. }
  130. void wait_ticks(unsigned long ticks)
  131. {
  132. u32 start = get_timer(0);
  133. while (get_timer(start) < ticks) ;
  134. }
  135. #endif /* CONFIG_MCFPIT */
  136. /*
  137. * This function is derived from PowerPC code (read timebase as long long).
  138. * On M68K it just returns the timer value.
  139. */
  140. unsigned long long get_ticks(void)
  141. {
  142. return get_timer(0);
  143. }
  144. unsigned long usec2ticks(unsigned long usec)
  145. {
  146. return get_timer(usec);
  147. }
  148. /*
  149. * This function is derived from PowerPC code (timebase clock frequency).
  150. * On M68K it returns the number of timer ticks per second.
  151. */
  152. ulong get_tbclk(void)
  153. {
  154. return CONFIG_SYS_HZ;
  155. }