timer.c 3.2 KB

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