timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <linux/types.h>
  16. #include <asm/arch/ep93xx.h>
  17. #include <asm/io.h>
  18. #include <div64.h>
  19. #define TIMER_CLKSEL (1 << 3)
  20. #define TIMER_ENABLE (1 << 7)
  21. #define TIMER_FREQ 508469 /* ticks / second */
  22. #define TIMER_MAX_VAL 0xFFFFFFFF
  23. static struct ep93xx_timer
  24. {
  25. unsigned long long ticks;
  26. unsigned long last_read;
  27. } timer;
  28. static inline unsigned long long usecs_to_ticks(unsigned long usecs)
  29. {
  30. unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
  31. do_div(ticks, 1000 * 1000);
  32. return ticks;
  33. }
  34. static inline void read_timer(void)
  35. {
  36. struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
  37. const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
  38. if (now >= timer.last_read)
  39. timer.ticks += now - timer.last_read;
  40. else
  41. /* an overflow occurred */
  42. timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
  43. timer.last_read = now;
  44. }
  45. /*
  46. * Get the number of ticks (in CONFIG_SYS_HZ resolution)
  47. */
  48. unsigned long long get_ticks(void)
  49. {
  50. unsigned long long sys_ticks;
  51. read_timer();
  52. sys_ticks = timer.ticks * CONFIG_SYS_HZ;
  53. do_div(sys_ticks, TIMER_FREQ);
  54. return sys_ticks;
  55. }
  56. unsigned long get_timer_masked(void)
  57. {
  58. return get_ticks();
  59. }
  60. unsigned long get_timer(unsigned long base)
  61. {
  62. return get_timer_masked() - base;
  63. }
  64. void __udelay(unsigned long usec)
  65. {
  66. unsigned long long target;
  67. read_timer();
  68. target = timer.ticks + usecs_to_ticks(usec);
  69. while (timer.ticks < target)
  70. read_timer();
  71. }
  72. int timer_init(void)
  73. {
  74. struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
  75. /* use timer 3 with 508KHz and free running, not enabled now */
  76. writel(TIMER_CLKSEL, &timer_regs->timer3.control);
  77. /* set initial timer value */
  78. writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
  79. /* Enable the timer */
  80. writel(TIMER_ENABLE | TIMER_CLKSEL,
  81. &timer_regs->timer3.control);
  82. /* Reset the timer */
  83. read_timer();
  84. timer.ticks = 0;
  85. return 0;
  86. }
  87. /*
  88. * This function is derived from PowerPC code (timebase clock frequency).
  89. * On ARM it returns the number of timer ticks per second.
  90. */
  91. unsigned long get_tbclk(void)
  92. {
  93. return CONFIG_SYS_HZ;
  94. }