timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * (C) Copyright 2004
  3. * Texas Instruments
  4. * Richard Woodruff <r-woodruff2@ti.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. * Alex Zuepke <azu@sysgo.de>
  10. *
  11. * (C) Copyright 2002
  12. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <asm/io.h>
  34. #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (1 << (CONFIG_TIMER_PRESCALE * 4)))
  35. #define TIMER_LOAD_VAL 0xFFFFFF
  36. /* macro to read the 32 bit timer */
  37. #define READ_TIMER (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR)) \
  38. / (TIMER_CLOCK / CONFIG_SYS_HZ)
  39. #define READ_TIMER_HW (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR))
  40. DECLARE_GLOBAL_DATA_PTR;
  41. int timer_init (void)
  42. {
  43. int32_t val;
  44. /* Start the counter ticking up */
  45. writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + TIMER_LOAD); /* reload value on overflow*/
  46. val = (CONFIG_TIMER_PRESCALE << TIMER_PRESCALE_SHIFT) |
  47. (TIMER_MODE_PERIODIC << TIMER_MODE_SHIFT) |
  48. (TIMER_ENABLE << TIMER_ENABLE_SHIFT); /* mask to enable timer*/
  49. writel(val, CONFIG_SYS_TIMERBASE + TIMER_CTRL); /* start timer */
  50. /* reset time */
  51. gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
  52. gd->arch.tbl = 0; /* start "advancing" time stamp */
  53. return(0);
  54. }
  55. /*
  56. * timer without interrupts
  57. */
  58. ulong get_timer (ulong base)
  59. {
  60. return get_timer_masked () - base;
  61. }
  62. /* delay x useconds AND preserve advance timestamp value */
  63. void __udelay (unsigned long usec)
  64. {
  65. ulong tmo, tmp;
  66. if (usec > 100000) { /* if "big" number, spread normalization to seconds */
  67. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  68. tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
  69. tmo /= 1000; /* finish normalize. */
  70. tmp = get_timer (0); /* get current timestamp */
  71. while (get_timer (tmp) < tmo)/* loop till event */
  72. /*NOP*/;
  73. } else { /* else small number, convert to hw ticks */
  74. tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  75. /* timeout is no more than 0.1s, and the hw timer will roll over at most once */
  76. tmp = READ_TIMER_HW;
  77. while (((READ_TIMER_HW -tmp) & TIMER_LOAD_VAL) < tmo)/* loop till event */
  78. /*NOP*/;
  79. }
  80. }
  81. ulong get_timer_masked (void)
  82. {
  83. ulong now = READ_TIMER; /* current tick value */
  84. if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
  85. /* move stamp fordward with absoulte diff ticks */
  86. gd->arch.tbl += (now - gd->arch.lastinc);
  87. } else {
  88. /* we have rollover of incrementer */
  89. gd->arch.tbl += ((TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ))
  90. - gd->arch.lastinc) + now;
  91. }
  92. gd->arch.lastinc = now;
  93. return gd->arch.tbl;
  94. }
  95. /*
  96. * This function is derived from PowerPC code (read timebase as long long).
  97. * On ARM it just returns the timer value.
  98. */
  99. unsigned long long get_ticks(void)
  100. {
  101. return get_timer(0);
  102. }
  103. /*
  104. * This function is derived from PowerPC code (timebase clock frequency).
  105. * On ARM it returns the number of timer ticks per second.
  106. */
  107. ulong get_tbclk (void)
  108. {
  109. ulong tbclk;
  110. tbclk = CONFIG_SYS_HZ;
  111. return tbclk;
  112. }