timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2010-2011 Calxeda, Inc.
  3. *
  4. * Based on arm926ejs/mx27/timer.c
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <common.h>
  20. #include <div64.h>
  21. #include <linux/types.h> /* for size_t */
  22. #include <linux/stddef.h> /* for NULL */
  23. #include <asm/io.h>
  24. #include <asm/arch-armv7/systimer.h>
  25. #undef SYSTIMER_BASE
  26. #define SYSTIMER_BASE 0xFFF34000 /* Timer 0 and 1 base */
  27. #define SYSTIMER_RATE 150000000
  28. static ulong timestamp;
  29. static ulong lastinc;
  30. static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
  31. /*
  32. * Start the timer
  33. */
  34. int timer_init(void)
  35. {
  36. /*
  37. * Setup timer0
  38. */
  39. writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
  40. writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
  41. writel(SYSTIMER_EN | SYSTIMER_32BIT, &systimer_base->timer0control);
  42. reset_timer_masked();
  43. return 0;
  44. }
  45. #define TICK_PER_TIME ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
  46. #define NS_PER_TICK (1000000000 / SYSTIMER_RATE)
  47. static inline unsigned long long tick_to_time(unsigned long long tick)
  48. {
  49. do_div(tick, TICK_PER_TIME);
  50. return tick;
  51. }
  52. static inline unsigned long long time_to_tick(unsigned long long time)
  53. {
  54. return time * TICK_PER_TIME;
  55. }
  56. static inline unsigned long long us_to_tick(unsigned long long us)
  57. {
  58. unsigned long long tick = us << 16;
  59. tick += NS_PER_TICK - 1;
  60. do_div(tick, NS_PER_TICK);
  61. return tick >> 16;
  62. }
  63. unsigned long long get_ticks(void)
  64. {
  65. ulong now = ~readl(&systimer_base->timer0value);
  66. if (now >= lastinc) /* normal mode (non roll) */
  67. /* move stamp forward with absolut diff ticks */
  68. timestamp += (now - lastinc);
  69. else /* we have rollover of incrementer */
  70. timestamp += (0xFFFFFFFF - lastinc) + now;
  71. lastinc = now;
  72. return timestamp;
  73. }
  74. /*
  75. * Delay x useconds AND preserve advance timstamp value
  76. * assumes timer is ticking at 1 msec
  77. */
  78. void __udelay(ulong usec)
  79. {
  80. unsigned long long tmp;
  81. ulong tmo;
  82. tmo = us_to_tick(usec);
  83. tmp = get_ticks() + tmo; /* get current timestamp */
  84. while (get_ticks() < tmp) /* loop till event */
  85. /*NOP*/;
  86. }
  87. ulong get_timer(ulong base)
  88. {
  89. return get_timer_masked() - base;
  90. }
  91. void reset_timer_masked(void)
  92. {
  93. lastinc = ~readl(&systimer_base->timer0value);
  94. timestamp = 0;
  95. }
  96. void reset_timer(void)
  97. {
  98. reset_timer_masked();
  99. }
  100. ulong get_timer_masked(void)
  101. {
  102. return tick_to_time(get_ticks());
  103. }