timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Cirrus Logic EP93xx timer support.
  3. *
  4. * Copyright (C) 2009, 2010
  5. * Matthias Kaehlcke <matthias@kaehlcke.net>
  6. *
  7. * Copyright (C) 2004, 2005
  8. * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
  9. *
  10. * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
  11. * author unknown.
  12. *
  13. * See file CREDITS for list of people who contributed to this project.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  22. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23. * for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <common.h>
  30. #include <linux/types.h>
  31. #include <asm/arch/ep93xx.h>
  32. #include <asm/io.h>
  33. #include <div64.h>
  34. #define TIMER_CLKSEL (1 << 3)
  35. #define TIMER_MODE (1 << 6)
  36. #define TIMER_ENABLE (1 << 7)
  37. #define TIMER_FREQ 508469
  38. #define TIMER_LOAD_VAL (TIMER_FREQ / CONFIG_SYS_HZ)
  39. static ulong timestamp;
  40. static ulong lastdec;
  41. static inline unsigned long clk_to_systicks(unsigned long clk_ticks)
  42. {
  43. unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
  44. do_div(sys_ticks, TIMER_FREQ);
  45. return (unsigned long)sys_ticks;
  46. }
  47. static inline unsigned long usecs_to_ticks(unsigned long usecs)
  48. {
  49. unsigned long ticks;
  50. if (usecs >= 1000) {
  51. ticks = usecs / 1000;
  52. ticks *= (TIMER_LOAD_VAL * CONFIG_SYS_HZ);
  53. ticks /= 1000;
  54. } else {
  55. ticks = usecs * TIMER_LOAD_VAL * CONFIG_SYS_HZ;
  56. ticks /= (1000 * 1000);
  57. }
  58. return ticks;
  59. }
  60. static inline unsigned long read_timer(void)
  61. {
  62. struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
  63. return readl(&timer->timer3.value);
  64. }
  65. /*
  66. * timer without interrupts
  67. */
  68. unsigned long long get_ticks(void)
  69. {
  70. const unsigned long now = read_timer();
  71. if (lastdec >= now) {
  72. /* normal mode */
  73. timestamp += lastdec - now;
  74. } else {
  75. /* we have an overflow ... */
  76. timestamp += lastdec + TIMER_LOAD_VAL - now;
  77. }
  78. lastdec = now;
  79. return timestamp;
  80. }
  81. unsigned long get_timer_masked(void)
  82. {
  83. return clk_to_systicks(get_ticks());
  84. }
  85. unsigned long get_timer(unsigned long base)
  86. {
  87. return get_timer_masked() - base;
  88. }
  89. void reset_timer_masked(void)
  90. {
  91. lastdec = read_timer();
  92. timestamp = 0;
  93. }
  94. void reset_timer(void)
  95. {
  96. reset_timer_masked();
  97. }
  98. void set_timer(unsigned long t)
  99. {
  100. timestamp = t;
  101. }
  102. void __udelay(unsigned long usec)
  103. {
  104. const unsigned long ticks = usecs_to_ticks(usec);
  105. const unsigned long target = clk_to_systicks(ticks) + get_timer(0);
  106. while (get_timer_masked() < target)
  107. /* noop */;
  108. }
  109. void udelay_masked(unsigned long usec)
  110. {
  111. const unsigned long ticks = usecs_to_ticks(usec);
  112. const unsigned long target = clk_to_systicks(ticks) + get_timer(0);
  113. reset_timer_masked();
  114. while (get_timer_masked() < target)
  115. /* noop */;
  116. }
  117. int timer_init(void)
  118. {
  119. struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
  120. /* use timer 3 with 508KHz and free running */
  121. writel(TIMER_CLKSEL, &timer->timer3.control);
  122. /* auto load, manual update of Timer 3 */
  123. lastdec = TIMER_LOAD_VAL;
  124. writel(TIMER_LOAD_VAL, &timer->timer3.load);
  125. /* Enable the timer and periodic mode */
  126. writel(TIMER_ENABLE | TIMER_MODE | TIMER_CLKSEL,
  127. &timer->timer3.control);
  128. reset_timer_masked();
  129. return 0;
  130. }
  131. /*
  132. * This function is derived from PowerPC code (timebase clock frequency).
  133. * On ARM it returns the number of timer ticks per second.
  134. */
  135. unsigned long get_tbclk(void)
  136. {
  137. return CONFIG_SYS_HZ;
  138. }