time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #include <asm/timer.h>
  28. #include <asm/immap.h>
  29. #ifdef CONFIG_M5271
  30. #include <asm/m5271.h>
  31. #include <asm/immap_5271.h>
  32. #endif
  33. #ifdef CONFIG_M5272
  34. #include <asm/m5272.h>
  35. #include <asm/immap_5272.h>
  36. #endif
  37. #ifdef CONFIG_M5282
  38. #include <asm/m5282.h>
  39. #endif
  40. #ifdef CONFIG_M5249
  41. #include <asm/m5249.h>
  42. #include <asm/immap_5249.h>
  43. #endif
  44. static ulong timestamp;
  45. #if defined(CONFIG_M5282) || defined(CONFIG_M5271)
  46. static unsigned short lastinc;
  47. #endif
  48. #if defined(CONFIG_M5272)
  49. /*
  50. * We use timer 3 which is running with a period of 1 us
  51. */
  52. void udelay(unsigned long usec)
  53. {
  54. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE3);
  55. uint start, now, tmp;
  56. while (usec > 0) {
  57. if (usec > 65000)
  58. tmp = 65000;
  59. else
  60. tmp = usec;
  61. usec = usec - tmp;
  62. /* Set up TIMER 3 as timebase clock */
  63. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  64. timerp->timer_tcn = 0;
  65. /* set period to 1 us */
  66. timerp->timer_tmr =
  67. (((CFG_CLK / 1000000) -
  68. 1) << 8) | MCFTIMER_TMR_CLK1 | MCFTIMER_TMR_FREERUN |
  69. MCFTIMER_TMR_ENABLE;
  70. start = now = timerp->timer_tcn;
  71. while (now < start + tmp)
  72. now = timerp->timer_tcn;
  73. }
  74. }
  75. void mcf_timer_interrupt(void *not_used)
  76. {
  77. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  78. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  79. /* check for timer 4 interrupts */
  80. if ((intp->int_isr & 0x01000000) != 0) {
  81. return;
  82. }
  83. /* reset timer */
  84. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  85. timestamp++;
  86. }
  87. void timer_init(void)
  88. {
  89. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4);
  90. volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1);
  91. timestamp = 0;
  92. /* Set up TIMER 4 as clock */
  93. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  94. /* initialize and enable timer 4 interrupt */
  95. irq_install_handler(72, mcf_timer_interrupt, 0);
  96. intp->int_icr1 |= 0x0000000d;
  97. timerp->timer_tcn = 0;
  98. timerp->timer_trr = 1000; /* Interrupt every ms */
  99. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  100. timerp->timer_tmr =
  101. (((CFG_CLK / 1000000) -
  102. 1) << 8) | MCFTIMER_TMR_CLK1 | MCFTIMER_TMR_RESTART |
  103. MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  104. }
  105. void reset_timer(void)
  106. {
  107. timestamp = 0;
  108. }
  109. ulong get_timer(ulong base)
  110. {
  111. return (timestamp - base);
  112. }
  113. void set_timer(ulong t)
  114. {
  115. timestamp = t;
  116. }
  117. #endif
  118. #if defined(CONFIG_M5282) || defined(CONFIG_M5271)
  119. void udelay(unsigned long usec)
  120. {
  121. volatile unsigned short *timerp;
  122. uint tmp;
  123. timerp = (volatile unsigned short *)(CFG_MBAR + MCFTIMER_BASE3);
  124. while (usec > 0) {
  125. if (usec > 65000)
  126. tmp = 65000;
  127. else
  128. tmp = usec;
  129. usec = usec - tmp;
  130. /* Set up TIMER 3 as timebase clock */
  131. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  132. timerp[MCFTIMER_PMR] = 0;
  133. /* set period to 1 us */
  134. timerp[MCFTIMER_PCSR] =
  135. #ifdef CONFIG_M5271
  136. (6 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  137. #else /* !CONFIG_M5271 */
  138. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  139. #endif /* CONFIG_M5271 */
  140. timerp[MCFTIMER_PMR] = tmp;
  141. while (timerp[MCFTIMER_PCNTR] > 0) ;
  142. }
  143. }
  144. void timer_init(void)
  145. {
  146. volatile unsigned short *timerp;
  147. timerp = (volatile unsigned short *)(CFG_MBAR + MCFTIMER_BASE4);
  148. timestamp = 0;
  149. /* Set up TIMER 4 as poll clock */
  150. timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW;
  151. timerp[MCFTIMER_PMR] = lastinc = 0;
  152. timerp[MCFTIMER_PCSR] =
  153. #ifdef CONFIG_M5271
  154. (6 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  155. #else /* !CONFIG_M5271 */
  156. (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW;
  157. #endif /* CONFIG_M5271 */
  158. }
  159. void set_timer(ulong t)
  160. {
  161. volatile unsigned short *timerp;
  162. timerp = (volatile unsigned short *)(CFG_MBAR + MCFTIMER_BASE4);
  163. timestamp = 0;
  164. timerp[MCFTIMER_PMR] = lastinc = 0;
  165. }
  166. ulong get_timer(ulong base)
  167. {
  168. unsigned short now, diff;
  169. volatile unsigned short *timerp;
  170. timerp = (volatile unsigned short *)(CFG_MBAR + MCFTIMER_BASE4);
  171. now = timerp[MCFTIMER_PCNTR];
  172. diff = -(now - lastinc);
  173. timestamp += diff;
  174. lastinc = now;
  175. return timestamp - base;
  176. }
  177. void wait_ticks(unsigned long ticks)
  178. {
  179. set_timer(0);
  180. while (get_timer(0) < ticks) ;
  181. }
  182. #endif
  183. #if defined(CONFIG_M5249)
  184. /*
  185. * We use timer 1 which is running with a period of 1 us
  186. */
  187. void udelay(unsigned long usec)
  188. {
  189. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE1);
  190. uint start, now, tmp;
  191. while (usec > 0) {
  192. if (usec > 65000)
  193. tmp = 65000;
  194. else
  195. tmp = usec;
  196. usec = usec - tmp;
  197. /* Set up TIMER 1 as timebase clock */
  198. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  199. timerp->timer_tcn = 0;
  200. /* set period to 1 us */
  201. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  202. timerp->timer_tmr =
  203. (((CFG_CLK / 2000000) -
  204. 1) << 8) | MCFTIMER_TMR_CLK1 | MCFTIMER_TMR_FREERUN |
  205. MCFTIMER_TMR_ENABLE;
  206. start = now = timerp->timer_tcn;
  207. while (now < start + tmp)
  208. now = timerp->timer_tcn;
  209. }
  210. }
  211. void mcf_timer_interrupt(void *not_used)
  212. {
  213. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  214. /* check for timer 2 interrupts */
  215. if ((mbar_readLong(MCFSIM_IPR) & 0x00000400) == 0) {
  216. return;
  217. }
  218. /* reset timer */
  219. timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF;
  220. timestamp++;
  221. }
  222. void timer_init(void)
  223. {
  224. volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2);
  225. timestamp = 0;
  226. /* Set up TIMER 2 as clock */
  227. timerp->timer_tmr = MCFTIMER_TMR_DISABLE;
  228. /* initialize and enable timer 2 interrupt */
  229. irq_install_handler(31, mcf_timer_interrupt, 0);
  230. mbar_writeLong(MCFSIM_IMR, mbar_readLong(MCFSIM_IMR) & ~0x00000400);
  231. mbar_writeByte(MCFSIM_TIMER2ICR,
  232. MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 |
  233. MCFSIM_ICR_PRI3);
  234. timerp->timer_tcn = 0;
  235. timerp->timer_trr = 1000; /* Interrupt every ms */
  236. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  237. /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */
  238. timerp->timer_tmr =
  239. (((CFG_CLK / 2000000) -
  240. 1) << 8) | MCFTIMER_TMR_CLK1 | MCFTIMER_TMR_RESTART |
  241. MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE;
  242. }
  243. void reset_timer(void)
  244. {
  245. timestamp = 0;
  246. }
  247. ulong get_timer(ulong base)
  248. {
  249. return (timestamp - base);
  250. }
  251. void set_timer(ulong t)
  252. {
  253. timestamp = t;
  254. }
  255. #endif
  256. #if defined(CONFIG_MCFTMR)
  257. #ifndef CFG_UDELAY_BASE
  258. # error "uDelay base not defined!"
  259. #endif
  260. #if !defined(CFG_TMR_BASE) || !defined(CFG_INTR_BASE) || !defined(CFG_TMRINTR_NO) || !defined(CFG_TMRINTR_MASK)
  261. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  262. #endif
  263. extern void dtimer_intr_setup(void);
  264. void udelay(unsigned long usec)
  265. {
  266. volatile dtmr_t *timerp = (dtmr_t *) (CFG_UDELAY_BASE);
  267. uint start, now, tmp;
  268. while (usec > 0) {
  269. if (usec > 65000)
  270. tmp = 65000;
  271. else
  272. tmp = usec;
  273. usec = usec - tmp;
  274. /* Set up TIMER 3 as timebase clock */
  275. timerp->tmr = DTIM_DTMR_RST_RST;
  276. timerp->tcn = 0;
  277. /* set period to 1 us */
  278. timerp->tmr =
  279. CFG_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
  280. DTIM_DTMR_RST_EN;
  281. start = now = timerp->tcn;
  282. while (now < start + tmp)
  283. now = timerp->tcn;
  284. }
  285. }
  286. void dtimer_interrupt(void *not_used)
  287. {
  288. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  289. volatile int0_t *intp = (int0_t *) (CFG_INTR_BASE);
  290. /* check for timer interrupt asserted */
  291. if ((intp->iprh0 & CFG_TMRINTR_MASK) == CFG_TMRINTR_MASK) {
  292. timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
  293. timestamp++;
  294. return;
  295. }
  296. }
  297. void timer_init(void)
  298. {
  299. volatile dtmr_t *timerp = (dtmr_t *) (CFG_TMR_BASE);
  300. timestamp = 0;
  301. timerp->tcn = 0;
  302. timerp->trr = 0;
  303. /* Set up TIMER 4 as clock */
  304. timerp->tmr = DTIM_DTMR_RST_RST;
  305. /* initialize and enable timer interrupt */
  306. irq_install_handler(CFG_TMRINTR_NO, dtimer_interrupt, 0);
  307. timerp->tcn = 0;
  308. timerp->trr = 1000; /* Interrupt every ms */
  309. dtimer_intr_setup();
  310. /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
  311. timerp->tmr = CFG_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
  312. DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
  313. }
  314. void reset_timer(void)
  315. {
  316. timestamp = 0;
  317. }
  318. ulong get_timer(ulong base)
  319. {
  320. return (timestamp - base);
  321. }
  322. void set_timer(ulong t)
  323. {
  324. timestamp = t;
  325. }
  326. #endif /* CONFIG_MCFTMR */
  327. #if defined(CONFIG_MCFPIT)
  328. #if !defined(CFG_PIT_BASE)
  329. # error "CFG_PIT_BASE not defined!"
  330. #endif
  331. static unsigned short lastinc;
  332. void udelay(unsigned long usec)
  333. {
  334. volatile pit_t *timerp = (pit_t *) (CFG_UDELAY_BASE);
  335. uint tmp;
  336. while (usec > 0) {
  337. if (usec > 65000)
  338. tmp = 65000;
  339. else
  340. tmp = usec;
  341. usec = usec - tmp;
  342. /* Set up TIMER 3 as timebase clock */
  343. timerp->pcsr = PIT_PCSR_OVW;
  344. timerp->pmr = 0;
  345. /* set period to 1 us */
  346. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  347. timerp->pmr = tmp;
  348. while (timerp->pcntr > 0) ;
  349. }
  350. }
  351. void timer_init(void)
  352. {
  353. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  354. timestamp = 0;
  355. /* Set up TIMER 4 as poll clock */
  356. timerp->pcsr = PIT_PCSR_OVW;
  357. timerp->pmr = lastinc = 0;
  358. timerp->pcsr |= PIT_PCSR_PRE(CFG_PIT_PRESCALE) | PIT_PCSR_EN;
  359. }
  360. void set_timer(ulong t)
  361. {
  362. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  363. timestamp = 0;
  364. timerp->pmr = lastinc = 0;
  365. }
  366. ulong get_timer(ulong base)
  367. {
  368. unsigned short now, diff;
  369. volatile pit_t *timerp = (pit_t *) (CFG_PIT_BASE);
  370. now = timerp->pcntr;
  371. diff = -(now - lastinc);
  372. timestamp += diff;
  373. lastinc = now;
  374. return timestamp - base;
  375. }
  376. void wait_ticks(unsigned long ticks)
  377. {
  378. set_timer(0);
  379. while (get_timer(0) < ticks) ;
  380. }
  381. #endif /* CONFIG_MCFPIT */
  382. /*
  383. * This function is derived from PowerPC code (read timebase as long long).
  384. * On M68K it just returns the timer value.
  385. */
  386. unsigned long long get_ticks(void)
  387. {
  388. return get_timer(0);
  389. }
  390. /*
  391. * This function is derived from PowerPC code (timebase clock frequency).
  392. * On M68K it returns the number of timer ticks per second.
  393. */
  394. ulong get_tbclk(void)
  395. {
  396. ulong tbclk;
  397. tbclk = CFG_HZ;
  398. return tbclk;
  399. }