slicetimer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * (C) Copyright 2007, 2012 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. #include <asm/io.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. static ulong timestamp;
  29. #if defined(CONFIG_SLTTMR)
  30. #ifndef CONFIG_SYS_UDELAY_BASE
  31. # error "uDelay base not defined!"
  32. #endif
  33. #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
  34. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  35. #endif
  36. extern void dtimer_intr_setup(void);
  37. void __udelay(unsigned long usec)
  38. {
  39. slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE);
  40. u32 now, freq;
  41. /* 1 us period */
  42. freq = CONFIG_SYS_TIMER_PRESCALER;
  43. /* Disable */
  44. out_be32(&timerp->cr, 0);
  45. out_be32(&timerp->tcnt, usec * freq);
  46. out_be32(&timerp->cr, SLT_CR_TEN);
  47. now = in_be32(&timerp->cnt);
  48. while (now != 0)
  49. now = in_be32(&timerp->cnt);
  50. setbits_be32(&timerp->sr, SLT_SR_ST);
  51. out_be32(&timerp->cr, 0);
  52. }
  53. void dtimer_interrupt(void *not_used)
  54. {
  55. slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
  56. /* check for timer interrupt asserted */
  57. if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
  58. setbits_be32(&timerp->sr, SLT_SR_ST);
  59. timestamp++;
  60. return;
  61. }
  62. }
  63. int timer_init(void)
  64. {
  65. slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
  66. timestamp = 0;
  67. /* disable timer */
  68. out_be32(&timerp->cr, 0);
  69. out_be32(&timerp->tcnt, 0);
  70. /* clear status */
  71. out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST);
  72. /* initialize and enable timer interrupt */
  73. irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
  74. /* Interrupt every ms */
  75. out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER);
  76. dtimer_intr_setup();
  77. /* set a period of 1us, set timer mode to restart and
  78. enable timer and interrupt */
  79. out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN);
  80. return 0;
  81. }
  82. ulong get_timer(ulong base)
  83. {
  84. return (timestamp - base);
  85. }
  86. #endif /* CONFIG_SLTTMR */