timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_masked(void)
  56. {
  57. return get_ticks();
  58. }
  59. unsigned long get_timer(unsigned long base)
  60. {
  61. return get_timer_masked() - base;
  62. }
  63. void __udelay(unsigned long usec)
  64. {
  65. unsigned long long target;
  66. read_timer();
  67. target = timer.ticks + usecs_to_ticks(usec);
  68. while (timer.ticks < target)
  69. read_timer();
  70. }
  71. int timer_init(void)
  72. {
  73. struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
  74. /* use timer 3 with 508KHz and free running, not enabled now */
  75. writel(TIMER_CLKSEL, &timer_regs->timer3.control);
  76. /* set initial timer value */
  77. writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
  78. /* Enable the timer */
  79. writel(TIMER_ENABLE | TIMER_CLKSEL,
  80. &timer_regs->timer3.control);
  81. /* Reset the timer */
  82. read_timer();
  83. timer.ticks = 0;
  84. return 0;
  85. }
  86. /*
  87. * This function is derived from PowerPC code (timebase clock frequency).
  88. * On ARM it returns the number of timer ticks per second.
  89. */
  90. unsigned long get_tbclk(void)
  91. {
  92. return CONFIG_SYS_HZ;
  93. }