slicetimer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * (C) Copyright 2007 Freescale Semiconductor, Inc.
  3. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/timer.h>
  25. #include <asm/immap.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static ulong timestamp;
  28. #if defined(CONFIG_SLTTMR)
  29. #ifndef CFG_UDELAY_BASE
  30. # error "uDelay base not defined!"
  31. #endif
  32. #if !defined(CFG_TMR_BASE) || !defined(CFG_INTR_BASE) || !defined(CFG_TMRINTR_NO) || !defined(CFG_TMRINTR_MASK)
  33. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  34. #endif
  35. extern void dtimer_intr_setup(void);
  36. void udelay(unsigned long usec)
  37. {
  38. volatile slt_t *timerp = (slt_t *) (CFG_UDELAY_BASE);
  39. u32 now, freq;
  40. /* 1 us period */
  41. freq = CFG_TIMER_PRESCALER;
  42. timerp->cr = 0; /* Disable */
  43. timerp->tcnt = usec * freq;
  44. timerp->cr = SLT_CR_TEN;
  45. now = timerp->cnt;
  46. while (now != 0)
  47. now = timerp->cnt;
  48. timerp->sr |= SLT_SR_ST;
  49. timerp->cr = 0;
  50. }
  51. void dtimer_interrupt(void *not_used)
  52. {
  53. volatile slt_t *timerp = (slt_t *) (CFG_TMR_BASE);
  54. /* check for timer interrupt asserted */
  55. if ((CFG_TMRPND_REG & CFG_TMRINTR_MASK) == CFG_TMRINTR_PEND) {
  56. timerp->sr |= SLT_SR_ST;
  57. timestamp++;
  58. return;
  59. }
  60. }
  61. void timer_init(void)
  62. {
  63. volatile slt_t *timerp = (slt_t *) (CFG_TMR_BASE);
  64. timestamp = 0;
  65. timerp->cr = 0; /* disable timer */
  66. timerp->tcnt = 0;
  67. timerp->sr = SLT_SR_BE | SLT_SR_ST; /* clear status */
  68. /* initialize and enable timer interrupt */
  69. irq_install_handler(CFG_TMRINTR_NO, dtimer_interrupt, 0);
  70. /* Interrupt every ms */
  71. timerp->tcnt = 1000 * CFG_TIMER_PRESCALER;
  72. dtimer_intr_setup();
  73. /* set a period of 1us, set timer mode to restart and
  74. enable timer and interrupt */
  75. timerp->cr = SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN;
  76. }
  77. void reset_timer(void)
  78. {
  79. timestamp = 0;
  80. }
  81. ulong get_timer(ulong base)
  82. {
  83. return (timestamp - base);
  84. }
  85. void set_timer(ulong t)
  86. {
  87. timestamp = t;
  88. }
  89. #endif /* CONFIG_SLTTMR */