timer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
  4. *
  5. * (C) Copyright 2008
  6. * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
  7. *
  8. * (C) Copyright 2004
  9. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  10. *
  11. * (C) Copyright 2002-2004
  12. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  13. *
  14. * (C) Copyright 2003
  15. * Texas Instruments <www.ti.com>
  16. *
  17. * (C) Copyright 2002
  18. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  19. * Marius Groeger <mgroeger@sysgo.de>
  20. *
  21. * (C) Copyright 2002
  22. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  23. * Alex Zuepke <azu@sysgo.de>
  24. *
  25. * SPDX-License-Identifier: GPL-2.0+
  26. */
  27. #include <common.h>
  28. #include <div64.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/hardware.h>
  31. #include <asm/arch/clk.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. struct scu_timer {
  34. u32 load; /* Timer Load Register */
  35. u32 counter; /* Timer Counter Register */
  36. u32 control; /* Timer Control Register */
  37. };
  38. static struct scu_timer *timer_base =
  39. (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
  40. #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
  41. #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
  42. #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
  43. #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
  44. #define TIMER_LOAD_VAL 0xFFFFFFFF
  45. #define TIMER_PRESCALE 255
  46. int timer_init(void)
  47. {
  48. const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
  49. (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
  50. SCUTIMER_CONTROL_ENABLE_MASK;
  51. gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
  52. /* Load the timer counter register */
  53. writel(0xFFFFFFFF, &timer_base->load);
  54. /*
  55. * Start the A9Timer device
  56. * Enable Auto reload mode, Clear prescaler control bits
  57. * Set prescaler value, Enable the decrementer
  58. */
  59. clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
  60. emask);
  61. /* Reset time */
  62. gd->arch.lastinc = readl(&timer_base->counter) /
  63. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
  64. gd->arch.tbl = 0;
  65. return 0;
  66. }
  67. /*
  68. * This function is derived from PowerPC code (read timebase as long long).
  69. * On ARM it just returns the timer value.
  70. */
  71. ulong get_timer_masked(void)
  72. {
  73. ulong now;
  74. now = readl(&timer_base->counter) /
  75. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
  76. if (gd->arch.lastinc >= now) {
  77. /* Normal mode */
  78. gd->arch.tbl += gd->arch.lastinc - now;
  79. } else {
  80. /* We have an overflow ... */
  81. gd->arch.tbl += gd->arch.lastinc + (TIMER_LOAD_VAL /
  82. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) -
  83. now + 1;
  84. }
  85. gd->arch.lastinc = now;
  86. return gd->arch.tbl;
  87. }
  88. void __udelay(unsigned long usec)
  89. {
  90. u32 countticks;
  91. u32 timeend;
  92. u32 timediff;
  93. u32 timenow;
  94. if (usec == 0)
  95. return;
  96. countticks = lldiv(((unsigned long long)gd->arch.timer_rate_hz * usec),
  97. 1000000);
  98. /* decrementing timer */
  99. timeend = readl(&timer_base->counter) - countticks;
  100. #if TIMER_LOAD_VAL != 0xFFFFFFFF
  101. /* do not manage multiple overflow */
  102. if (countticks >= TIMER_LOAD_VAL)
  103. countticks = TIMER_LOAD_VAL - 1;
  104. #endif
  105. do {
  106. timenow = readl(&timer_base->counter);
  107. if (timenow >= timeend) {
  108. /* normal case */
  109. timediff = timenow - timeend;
  110. } else {
  111. if ((TIMER_LOAD_VAL - timeend + timenow) <=
  112. countticks) {
  113. /* overflow */
  114. timediff = TIMER_LOAD_VAL - timeend + timenow;
  115. } else {
  116. /* missed the exact match */
  117. break;
  118. }
  119. }
  120. } while (timediff > 0);
  121. }
  122. /* Timer without interrupts */
  123. ulong get_timer(ulong base)
  124. {
  125. return get_timer_masked() - base;
  126. }
  127. /*
  128. * This function is derived from PowerPC code (read timebase as long long).
  129. * On ARM it just returns the timer value.
  130. */
  131. unsigned long long get_ticks(void)
  132. {
  133. return get_timer(0);
  134. }
  135. /*
  136. * This function is derived from PowerPC code (timebase clock frequency).
  137. * On ARM it returns the number of timer ticks per second.
  138. */
  139. ulong get_tbclk(void)
  140. {
  141. return CONFIG_SYS_HZ;
  142. }