time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #if defined(CONFIG_MCFTMR)
  241. #ifndef CFG_UDELAY_BASE
  242. # error "uDelay base not defined!"
  243. #endif
  244. #if !defined(CFG_TMR_BASE) || !defined(CFG_INTR_BASE) || !defined(CFG_TMRINTR_NO) || !defined(CFG_TMRINTR_MASK)
  245. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  246. #endif
  247. #include <asm/immap_5329.h>
  248. extern void dtimer_interrupt(void *not_used);
  249. extern void dtimer_interrupt_setup(void);
  250. extern void dtimer_interrupt_enable(void);
  251. void udelay(unsigned long usec)
  252. {
  253. volatile dtmr_t *timerp = (dtmr_t *) (CFG_UDELAY_BASE);
  254. uint start, now, tmp;
  255. while (usec > 0) {
  256. if (usec > 65000)
  257. tmp = 65000;
  258. else
  259. tmp = usec;
  260. usec = usec - tmp;
  261. /* Set up TIMER 3 as timebase clock */
  262. timerp->tmr = DTIM_DTMR_RST_RST;
  263. timerp->tcn = 0;
  264. /* set period to 1 us */
  265. timerp->tmr =
  266. (((CFG_CLK / 1000000) -
  267. 1) << 8) | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR | DTIM_DTMR_RST_EN;
  268. start = now = timerp->tcn;
  269. while (now < start + tmp)
  270. now = timerp->tcn;
  271. }
  272. }
  273. void dtimer_interrupt(void *not_used)
  274. {
  275. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  276. volatile int0_t *intp = (int0_t *) (CFG_INTR_BASE);
  277. /* check for timer interrupt asserted */
  278. if ((intp->iprh0 & CFG_TMRINTR_MASK) == CFG_TMRINTR_MASK) {
  279. timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
  280. timestamp++;
  281. return;
  282. }
  283. }
  284. void timer_init(void)
  285. {
  286. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  287. volatile int0_t *intp = (int0_t *) (CFG_INTR_BASE);
  288. timestamp = 0;
  289. timerp->tcn = 0;
  290. timerp->trr = 0;
  291. /* Set up TIMER 4 as clock */
  292. timerp->tmr = DTIM_DTMR_RST_RST;
  293. /* initialize and enable timer 4 interrupt */
  294. irq_install_handler(CFG_TMRINTR_NO, dtimer_interrupt, 0);
  295. intp->icr0[CFG_TMRINTR_NO] = CFG_TMRINTR_PRI;
  296. timerp->tcn = 0;
  297. timerp->trr = 1000; /* Interrupt every ms */
  298. intp->imrh0 &= ~CFG_TMRINTR_MASK;
  299. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  300. timerp->tmr = CFG_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
  301. DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
  302. }
  303. void reset_timer(void)
  304. {
  305. timestamp = 0;
  306. }
  307. ulong get_timer(ulong base)
  308. {
  309. return (timestamp - base);
  310. }
  311. void set_timer(ulong t)
  312. {
  313. timestamp = t;
  314. }
  315. #endif /* CONFIG_MCFTMR */
  316. #if defined(CONFIG_MCFPIT)
  317. #if !defined(CFG_PIT_BASE)
  318. # error "CFG_PIT_BASE not defined!"
  319. #endif
  320. static unsigned short lastinc;
  321. void udelay(unsigned long usec)
  322. {
  323. volatile pit_t *timerp = (pit_t *) (CFG_UDELAY_BASE);
  324. uint tmp;
  325. while (usec > 0) {
  326. if (usec > 65000)
  327. tmp = 65000;
  328. else
  329. tmp = usec;
  330. usec = usec - tmp;
  331. /* Set up TIMER 3 as timebase clock */
  332. timerp->pcsr = PIT_PCSR_OVW;
  333. timerp->pmr = 0;
  334. /* set period to 1 us */
  335. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  336. timerp->pmr = tmp;
  337. while (timerp->pcntr > 0) ;
  338. }
  339. }
  340. void timer_init(void)
  341. {
  342. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  343. timestamp = 0;
  344. /* Set up TIMER 4 as poll clock */
  345. timerp->pcsr = PIT_PCSR_OVW;
  346. timerp->pmr = lastinc = 0;
  347. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  348. }
  349. void set_timer(ulong t)
  350. {
  351. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  352. timestamp = 0;
  353. timerp->pmr = lastinc = 0;
  354. }
  355. ulong get_timer(ulong base)
  356. {
  357. unsigned short now, diff;
  358. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  359. now = timerp->pcntr;
  360. diff = -(now - lastinc);
  361. timestamp += diff;
  362. lastinc = now;
  363. return timestamp - base;
  364. }
  365. void wait_ticks(unsigned long ticks)
  366. {
  367. set_timer(0);
  368. while (get_timer(0) < ticks) ;
  369. }
  370. #endif /* CONFIG_MCFPIT */
  371. /*
  372. * This function is derived from PowerPC code (read timebase as long long).
  373. * On M68K it just returns the timer value.
  374. */
  375. unsigned long long get_ticks(void)
  376. {
  377. return get_timer(0);
  378. }
  379. /*
  380. * This function is derived from PowerPC code (timebase clock frequency).
  381. * On M68K it returns the number of timer ticks per second.
  382. */
  383. ulong get_tbclk (void)
  384. {
  385. ulong tbclk;
  386. tbclk = CFG_HZ;
  387. return tbclk;
  388. }