timer.c 2.4 KB

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