time.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. volatile unsigned short *timerp;
  110. uint tmp;
  111. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE3);
  112. while (usec > 0) {
  113. if (usec > 65000)
  114. tmp = 65000;
  115. else
  116. tmp = usec;
  117. usec = usec - tmp;
  118. /* Set up TIMER 3 as timebase clock */
  119. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  120. timerp[MCFTIMER_PMR] = 0;
  121. /* set period to 1 us */
  122. timerp[MCFTIMER_PCSR] =
  123. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  124. timerp[MCFTIMER_PMR] = tmp;
  125. while (timerp[MCFTIMER_PCNTR] > 0);
  126. }
  127. }
  128. void timer_init (void)
  129. {
  130. volatile unsigned short *timerp;
  131. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  132. timestamp = 0;
  133. /* Set up TIMER 4 as poll clock */
  134. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  135. timerp[MCFTIMER_PMR] = lastinc = 0;
  136. timerp[MCFTIMER_PCSR] =
  137. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  138. }
  139. void set_timer (ulong t)
  140. {
  141. volatile unsigned short *timerp;
  142. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  143. timestamp = 0;
  144. timerp[MCFTIMER_PMR] = lastinc = 0;
  145. }
  146. ulong get_timer (ulong base)
  147. {
  148. unsigned short now, diff;
  149. volatile unsigned short *timerp;
  150. timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4);
  151. now = timerp[MCFTIMER_PCNTR];
  152. diff = -(now - lastinc);
  153. timestamp += diff;
  154. lastinc = now;
  155. return timestamp - base;
  156. }
  157. void wait_ticks (unsigned long ticks)
  158. {
  159. set_timer (0);
  160. while (get_timer (0) < ticks);
  161. }
  162. #endif
  163. #if defined(CONFIG_M5249)
  164. /*
  165. * We use timer 1 which is running with a period of 1 us
  166. */
  167. void udelay(unsigned long usec)
  168. {
  169. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE1);
  170. uint start, now, tmp;
  171. while (usec > 0) {
  172. if (usec > 65000)
  173. tmp = 65000;
  174. else
  175. tmp = usec;
  176. usec = usec - tmp;
  177. /* Set up TIMER 1 as timebase clock */
  178. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  179. timerp->timer_tcn = 0;
  180. /* set period to 1 us */
  181. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  182. timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  183. MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE;
  184. start = now = timerp->timer_tcn;
  185. while (now < start + tmp)
  186. now = timerp->timer_tcn;
  187. }
  188. }
  189. void mcf_timer_interrupt (void * not_used){
  190. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  191. /* check for timer 2 interrupts */
  192. if ((mbar_readLong(MCFSIM_IPR) & 0x00000400) == 0) {
  193. return;
  194. }
  195. /* reset timer */
  196. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  197. timestamp ++;
  198. }
  199. void timer_init (void) {
  200. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  201. timestamp = 0;
  202. /* Set up TIMER 2 as clock */
  203. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  204. /* initialize and enable timer 2 interrupt */
  205. irq_install_handler (31, mcf_timer_interrupt, 0);
  206. mbar_writeLong(MCFSIM_IMR, mbar_readLong(MCFSIM_IMR) & ~0x00000400);
  207. mbar_writeByte(MCFSIM_TIMER2ICR, MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 | MCFSIM_ICR_PRI3);
  208. timerp->timer_tcn = 0;
  209. timerp->timer_trr = 1000; /* Interrupt every ms */
  210. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  211. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  212. timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 |
  213. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  214. }
  215. void reset_timer (void)
  216. {
  217. timestamp = 0;
  218. }
  219. ulong get_timer (ulong base)
  220. {
  221. return (timestamp - base);
  222. }
  223. void set_timer (ulong t)
  224. {
  225. timestamp = t;
  226. }
  227. #endif
  228. /*
  229. * This function is derived from PowerPC code (read timebase as long long).
  230. * On M68K it just returns the timer value.
  231. */
  232. unsigned long long get_ticks(void)
  233. {
  234. return get_timer(0);
  235. }
  236. /*
  237. * This function is derived from PowerPC code (timebase clock frequency).
  238. * On M68K it returns the number of timer ticks per second.
  239. */
  240. ulong get_tbclk (void)
  241. {
  242. ulong tbclk;
  243. tbclk = CFG_HZ;
  244. return tbclk;
  245. }