time.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifdef CONFIG_M5249
  35. #include <asm/m5249.h>
  36. #include <asm/immap_5249.h>
  37. #endif
  38. static ulong timestamp;
  39. #ifdef CONFIG_M5282
  40. static unsigned short lastinc;
  41. #endif
  42. #if defined(CONFIG_M5272)
  43. /*
  44. * We use timer 3 which is running with a period of 1 us
  45. */
  46. void udelay(unsigned long usec)
  47. {
  48. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE3);
  49. uint start, now, tmp;
  50. while (usec > 0) {
  51. if (usec > 65000)
  52. tmp = 65000;
  53. else
  54. tmp = usec;
  55. usec = usec - tmp;
  56. /* Set up TIMER 3 as timebase clock */
  57. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  58. timerp->timer_tcn = 0;
  59. /* set period to 1 us */
  60. timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  61. MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE;
  62. start = now = timerp->timer_tcn;
  63. while (now < start + tmp)
  64. now = timerp->timer_tcn;
  65. }
  66. }
  67. void mcf_timer_interrupt (void * not_used){
  68. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  69. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  70. /* check for timer 4 interrupts */
  71. if ((intp->int_isr & 0x01000000) != 0) {
  72. return;
  73. }
  74. /* reset timer */
  75. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  76. timestamp ++;
  77. }
  78. void timer_init (void) {
  79. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  80. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  81. timestamp = 0;
  82. /* Set up TIMER 4 as clock */
  83. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  84. /* initialize and enable timer 4 interrupt */
  85. irq_install_handler (72, mcf_timer_interrupt, 0);
  86. intp->int_icr1 |= 0x0000000d;
  87. timerp->timer_tcn = 0;
  88. timerp->timer_trr = 1000; /* Interrupt every ms */
  89. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  90. timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  91. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  92. }
  93. void reset_timer (void)
  94. {
  95. timestamp = 0;
  96. }
  97. ulong get_timer (ulong base)
  98. {
  99. return (timestamp - base);
  100. }
  101. void set_timer (ulong t)
  102. {
  103. timestamp = t;
  104. }
  105. #endif
  106. #if defined(CONFIG_M5282)
  107. void udelay(unsigned long usec)
  108. {
  109. }
  110. void timer_init (void)
  111. {
  112. volatile unsigned short *timerp;
  113. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  114. timestamp = 0;
  115. /* Set up TIMER 4 as poll clock */
  116. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  117. timerp[MCFTIMER_PMR] = lastinc = 0;
  118. timerp[MCFTIMER_PCSR] =
  119. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  120. }
  121. void set_timer (ulong t)
  122. {
  123. volatile unsigned short *timerp;
  124. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  125. timestamp = 0;
  126. timerp[MCFTIMER_PMR] = lastinc = 0;
  127. }
  128. ulong get_timer (ulong base)
  129. {
  130. unsigned short now, diff;
  131. volatile unsigned short *timerp;
  132. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  133. now = timerp[MCFTIMER_PCNTR];
  134. diff = -(now - lastinc);
  135. timestamp += diff;
  136. lastinc = now;
  137. return timestamp - base;
  138. }
  139. void wait_ticks (unsigned long ticks)
  140. {
  141. set_timer (0);
  142. while (get_timer (0) < ticks);
  143. }
  144. #endif
  145. #if defined(CONFIG_M5249)
  146. /*
  147. * We use timer 1 which is running with a period of 1 us
  148. */
  149. void udelay(unsigned long usec)
  150. {
  151. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE1);
  152. uint start, now, tmp;
  153. while (usec > 0) {
  154. if (usec > 65000)
  155. tmp = 65000;
  156. else
  157. tmp = usec;
  158. usec = usec - tmp;
  159. /* Set up TIMER 1 as timebase clock */
  160. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  161. timerp->timer_tcn = 0;
  162. /* set period to 1 us */
  163. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  164. timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  165. MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE;
  166. start = now = timerp->timer_tcn;
  167. while (now < start + tmp)
  168. now = timerp->timer_tcn;
  169. }
  170. }
  171. void mcf_timer_interrupt (void * not_used){
  172. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  173. /* check for timer 2 interrupts */
  174. if ((mbar_readLong(MCFSIM_IPR) & 0x00000400) == 0) {
  175. return;
  176. }
  177. /* reset timer */
  178. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  179. timestamp ++;
  180. }
  181. void timer_init (void) {
  182. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  183. timestamp = 0;
  184. /* Set up TIMER 2 as clock */
  185. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  186. /* initialize and enable timer 2 interrupt */
  187. irq_install_handler (31, mcf_timer_interrupt, 0);
  188. mbar_writeLong(MCFSIM_IMR, mbar_readLong(MCFSIM_IMR) & ~0x00000400);
  189. mbar_writeByte(MCFSIM_TIMER2ICR, MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 | MCFSIM_ICR_PRI3);
  190. timerp->timer_tcn = 0;
  191. timerp->timer_trr = 1000; /* Interrupt every ms */
  192. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  193. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  194. timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  195. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  196. }
  197. void reset_timer (void)
  198. {
  199. timestamp = 0;
  200. }
  201. ulong get_timer (ulong base)
  202. {
  203. return (timestamp - base);
  204. }
  205. void set_timer (ulong t)
  206. {
  207. timestamp = t;
  208. }
  209. #endif
  210. /*
  211. * This function is derived from PowerPC code (read timebase as long long).
  212. * On M68K it just returns the timer value.
  213. */
  214. unsigned long long get_ticks(void)
  215. {
  216. return get_timer(0);
  217. }
  218. /*
  219. * This function is derived from PowerPC code (timebase clock frequency).
  220. * On M68K it returns the number of timer ticks per second.
  221. */
  222. ulong get_tbclk (void)
  223. {
  224. ulong tbclk;
  225. tbclk = CFG_HZ;
  226. return tbclk;
  227. }