timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. DECLARE_GLOBAL_DATA_PTR;
  32. struct scu_timer {
  33. u32 load; /* Timer Load Register */
  34. u32 counter; /* Timer Counter Register */
  35. u32 control; /* Timer Control Register */
  36. };
  37. static struct scu_timer *timer_base =
  38. (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
  39. #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
  40. #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
  41. #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
  42. #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
  43. #define TIMER_LOAD_VAL 0xFFFFFFFF
  44. #define TIMER_PRESCALE 255
  45. #define TIMER_TICK_HZ (CONFIG_CPU_FREQ_HZ / 2 / TIMER_PRESCALE)
  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. /* Load the timer counter register */
  52. writel(0xFFFFFFFF, &timer_base->load);
  53. /*
  54. * Start the A9Timer device
  55. * Enable Auto reload mode, Clear prescaler control bits
  56. * Set prescaler value, Enable the decrementer
  57. */
  58. clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
  59. emask);
  60. /* Reset time */
  61. gd->arch.lastinc = readl(&timer_base->counter) /
  62. (TIMER_TICK_HZ / CONFIG_SYS_HZ);
  63. gd->arch.tbl = 0;
  64. return 0;
  65. }
  66. /*
  67. * This function is derived from PowerPC code (read timebase as long long).
  68. * On ARM it just returns the timer value.
  69. */
  70. ulong get_timer_masked(void)
  71. {
  72. ulong now;
  73. now = readl(&timer_base->counter) / (TIMER_TICK_HZ / CONFIG_SYS_HZ);
  74. if (gd->arch.lastinc >= now) {
  75. /* Normal mode */
  76. gd->arch.tbl += gd->arch.lastinc - now;
  77. } else {
  78. /* We have an overflow ... */
  79. gd->arch.tbl += gd->arch.lastinc + TIMER_LOAD_VAL - now;
  80. }
  81. gd->arch.lastinc = now;
  82. return gd->arch.tbl;
  83. }
  84. void __udelay(unsigned long usec)
  85. {
  86. u32 countticks;
  87. u32 timeend;
  88. u32 timediff;
  89. u32 timenow;
  90. if (usec == 0)
  91. return;
  92. countticks = (u32) (((unsigned long long) TIMER_TICK_HZ * usec) /
  93. 1000000);
  94. /* decrementing timer */
  95. timeend = readl(&timer_base->counter) - countticks;
  96. #if TIMER_LOAD_VAL != 0xFFFFFFFF
  97. /* do not manage multiple overflow */
  98. if (countticks >= TIMER_LOAD_VAL)
  99. countticks = TIMER_LOAD_VAL - 1;
  100. #endif
  101. do {
  102. timenow = readl(&timer_base->counter);
  103. if (timenow >= timeend) {
  104. /* normal case */
  105. timediff = timenow - timeend;
  106. } else {
  107. if ((TIMER_LOAD_VAL - timeend + timenow) <=
  108. countticks) {
  109. /* overflow */
  110. timediff = TIMER_LOAD_VAL - timeend + timenow;
  111. } else {
  112. /* missed the exact match */
  113. break;
  114. }
  115. }
  116. } while (timediff > 0);
  117. }
  118. /* Timer without interrupts */
  119. ulong get_timer(ulong base)
  120. {
  121. return get_timer_masked() - base;
  122. }
  123. /*
  124. * This function is derived from PowerPC code (read timebase as long long).
  125. * On ARM it just returns the timer value.
  126. */
  127. unsigned long long get_ticks(void)
  128. {
  129. return get_timer(0);
  130. }
  131. /*
  132. * This function is derived from PowerPC code (timebase clock frequency).
  133. * On ARM it returns the number of timer ticks per second.
  134. */
  135. ulong get_tbclk(void)
  136. {
  137. return CONFIG_SYS_HZ;
  138. }