timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cirrus Logic EP93xx timer support.
  4. *
  5. * Copyright (C) 2009, 2010 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. #include <common.h>
  14. #include <time.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(unsigned long base)
  57. {
  58. return get_ticks() - base;
  59. }
  60. void __udelay(unsigned long usec)
  61. {
  62. unsigned long long target;
  63. read_timer();
  64. target = timer.ticks + usecs_to_ticks(usec);
  65. while (timer.ticks < target)
  66. read_timer();
  67. }
  68. int timer_init(void)
  69. {
  70. struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
  71. /* use timer 3 with 508KHz and free running, not enabled now */
  72. writel(TIMER_CLKSEL, &timer_regs->timer3.control);
  73. /* set initial timer value */
  74. writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
  75. /* Enable the timer */
  76. writel(TIMER_ENABLE | TIMER_CLKSEL,
  77. &timer_regs->timer3.control);
  78. /* Reset the timer */
  79. read_timer();
  80. timer.ticks = 0;
  81. return 0;
  82. }
  83. /*
  84. * This function is derived from PowerPC code (timebase clock frequency).
  85. * On ARM it returns the number of timer ticks per second.
  86. */
  87. unsigned long get_tbclk(void)
  88. {
  89. return CONFIG_SYS_HZ;
  90. }