timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian.pop@leadtechdesign.com>
  4. * Lead Tech Design <www.leadtechdesign.com>
  5. *
  6. * (C) Copyright 2010
  7. * Matthias Weisser, Graf-Syteco <weisserm@arcor.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <div64.h>
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/hardware.h>
  31. #define TIMER_LOAD_VAL 0xffffffff
  32. #define TIMER_FREQ (CONFIG_MB86R0x_IOCLK / 256)
  33. static unsigned long long timestamp;
  34. static ulong lastdec;
  35. static inline unsigned long long tick_to_time(unsigned long long tick)
  36. {
  37. tick *= CONFIG_SYS_HZ;
  38. do_div(tick, TIMER_FREQ);
  39. return tick;
  40. }
  41. static inline unsigned long long usec_to_tick(unsigned long long usec)
  42. {
  43. usec *= TIMER_FREQ;
  44. do_div(usec, 1000000);
  45. return usec;
  46. }
  47. /* nothing really to do with interrupts, just starts up a counter. */
  48. int timer_init(void)
  49. {
  50. struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
  51. MB86R0x_TIMER_BASE;
  52. ulong ctrl = readl(&timer->control);
  53. writel(TIMER_LOAD_VAL, &timer->load);
  54. ctrl |= MB86R0x_TIMER_ENABLE | MB86R0x_TIMER_PRS_8S |
  55. MB86R0x_TIMER_SIZE_32;
  56. writel(ctrl, &timer->control);
  57. reset_timer_masked();
  58. return 0;
  59. }
  60. /*
  61. * timer without interrupts
  62. */
  63. unsigned long long get_ticks(void)
  64. {
  65. struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
  66. MB86R0x_TIMER_BASE;
  67. ulong now = readl(&timer->value);
  68. if (now <= lastdec) {
  69. /* normal mode (non roll) */
  70. /* move stamp forward with absolut diff ticks */
  71. timestamp += lastdec - now;
  72. } else {
  73. /* we have rollover of incrementer */
  74. timestamp += lastdec + TIMER_LOAD_VAL - now;
  75. }
  76. lastdec = now;
  77. return timestamp;
  78. }
  79. void reset_timer_masked(void)
  80. {
  81. struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
  82. MB86R0x_TIMER_BASE;
  83. /* capture current value time */
  84. lastdec = readl(&timer->value);
  85. timestamp = 0; /* start "advancing" time stamp from 0 */
  86. }
  87. ulong get_timer_masked(void)
  88. {
  89. return tick_to_time(get_ticks());
  90. }
  91. void __udelay(unsigned long usec)
  92. {
  93. unsigned long long tmp;
  94. ulong tmo;
  95. tmo = usec_to_tick(usec);
  96. tmp = get_ticks(); /* get current timestamp */
  97. while ((get_ticks() - tmp) < tmo) /* loop till event */
  98. /*NOP*/;
  99. }
  100. void reset_timer(void)
  101. {
  102. reset_timer_masked();
  103. }
  104. ulong get_timer(ulong base)
  105. {
  106. return get_timer_masked() - base;
  107. }
  108. /*
  109. * This function is derived from PowerPC code (timebase clock frequency).
  110. * On ARM it returns the number of timer ticks per second.
  111. */
  112. ulong get_tbclk(void)
  113. {
  114. ulong tbclk;
  115. tbclk = TIMER_FREQ;
  116. return tbclk;
  117. }