time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/mcftimer.h>
  27. #ifdef CONFIG_M5272
  28. #include <asm/m5272.h>
  29. #include <asm/immap_5272.h>
  30. #endif
  31. #ifdef CONFIG_M5282
  32. #include <asm/m5282.h>
  33. #endif
  34. static ulong timestamp;
  35. #ifdef CONFIG_M5282
  36. static unsigned short lastinc;
  37. #endif
  38. #if defined(CONFIG_M5272)
  39. /*
  40. * We use timer 3 which is running with a period of 1 us
  41. */
  42. void udelay(unsigned long usec)
  43. {
  44. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE3);
  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->timer_tmr = MCFTIMER_TMR_DISABLE;
  54. timerp->timer_tcn = 0;
  55. /* set period to 1 us */
  56. timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  57. MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE;
  58. start = now = timerp->timer_tcn;
  59. while (now < start + tmp)
  60. now = timerp->timer_tcn;
  61. }
  62. }
  63. void mcf_timer_interrupt (void * not_used){
  64. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  65. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  66. /* check for timer 4 interrupts */
  67. if ((intp->int_isr & 0x01000000) != 0) {
  68. return;
  69. }
  70. /* reset timer */
  71. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  72. timestamp ++;
  73. }
  74. void timer_init (void) {
  75. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  76. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  77. timestamp = 0;
  78. /* Set up TIMER 4 as clock */
  79. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  80. /* initialize and enable timer 4 interrupt */
  81. irq_install_handler (72, mcf_timer_interrupt, 0);
  82. intp->int_icr1 |= 0x0000000d;
  83. timerp->timer_tcn = 0;
  84. timerp->timer_trr = 1000; /* Interrupt every ms */
  85. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  86. timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  87. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  88. }
  89. void reset_timer (void)
  90. {
  91. timestamp = 0;
  92. }
  93. ulong get_timer (ulong base)
  94. {
  95. return (timestamp - base);
  96. }
  97. void set_timer (ulong t)
  98. {
  99. timestamp = t;
  100. }
  101. #endif
  102. #if defined(CONFIG_M5282)
  103. void udelay(unsigned long usec)
  104. {
  105. }
  106. void timer_init (void)
  107. {
  108. volatile unsigned short *timerp;
  109. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  110. timestamp = 0;
  111. /* Set up TIMER 4 as poll clock */
  112. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  113. timerp[MCFTIMER_PMR] = lastinc = 0;
  114. timerp[MCFTIMER_PCSR] =
  115. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  116. }
  117. void set_timer (ulong t)
  118. {
  119. volatile unsigned short *timerp;
  120. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  121. timestamp = 0;
  122. timerp[MCFTIMER_PMR] = lastinc = 0;
  123. }
  124. ulong get_timer (ulong base)
  125. {
  126. unsigned short now, diff;
  127. volatile unsigned short *timerp;
  128. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  129. now = timerp[MCFTIMER_PCNTR];
  130. diff = -(now - lastinc);
  131. timestamp += diff;
  132. lastinc = now;
  133. return timestamp - base;
  134. }
  135. void wait_ticks (unsigned long ticks)
  136. {
  137. set_timer (0);
  138. while (get_timer (0) < ticks);
  139. }
  140. #endif