timer.c 2.4 KB

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