time.c 7.3 KB

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