time.c 11 KB

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