time.c 7.1 KB

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