timer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * (C) Copyright 2003
  3. * Texas Instruments <www.ti.com>
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * (C) Copyright 2002-2004
  14. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  15. *
  16. * (C) Copyright 2004
  17. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <asm/io.h>
  23. #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
  24. #define TIMER_LOAD_VAL 0xffffffff
  25. /* macro to read the 32 bit timer */
  26. #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+8) \
  27. / (TIMER_CLOCK / CONFIG_SYS_HZ)
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define timestamp gd->arch.tbl
  30. #define lastdec gd->arch.lastinc
  31. int timer_init (void)
  32. {
  33. int32_t val;
  34. /* Start the decrementer ticking down from 0xffffffff */
  35. *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL;
  36. val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT);
  37. *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val;
  38. /* init the timestamp and lastdec value */
  39. reset_timer_masked();
  40. return 0;
  41. }
  42. /*
  43. * timer without interrupts
  44. */
  45. ulong get_timer (ulong base)
  46. {
  47. return get_timer_masked () - base;
  48. }
  49. /* delay x useconds AND preserve advance timestamp value */
  50. void __udelay (unsigned long usec)
  51. {
  52. ulong tmo, tmp;
  53. if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
  54. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  55. tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
  56. tmo /= 1000; /* finish normalize. */
  57. }else{ /* else small number, don't kill it prior to HZ multiply */
  58. tmo = usec * CONFIG_SYS_HZ;
  59. tmo /= (1000*1000);
  60. }
  61. tmp = get_timer (0); /* get current timestamp */
  62. if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */
  63. reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */
  64. else
  65. tmo += tmp; /* else, set advancing stamp wake up time */
  66. while (get_timer_masked () < tmo)/* loop till event */
  67. /*NOP*/;
  68. }
  69. void reset_timer_masked (void)
  70. {
  71. /* reset time */
  72. lastdec = READ_TIMER; /* capure current decrementer value time */
  73. timestamp = 0; /* start "advancing" time stamp from 0 */
  74. }
  75. ulong get_timer_masked (void)
  76. {
  77. ulong now = READ_TIMER; /* current tick value */
  78. if (lastdec >= now) { /* normal mode (non roll) */
  79. /* normal mode */
  80. timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
  81. } else { /* we have overflow of the count down timer */
  82. /* nts = ts + ld + (TLV - now)
  83. * ts=old stamp, ld=time that passed before passing through -1
  84. * (TLV-now) amount of time after passing though -1
  85. * nts = new "advancing time stamp"...it could also roll and cause problems.
  86. */
  87. timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK /
  88. CONFIG_SYS_HZ)) - now;
  89. }
  90. lastdec = now;
  91. return timestamp;
  92. }
  93. /* waits specified delay value and resets timestamp */
  94. void udelay_masked (unsigned long usec)
  95. {
  96. ulong tmo;
  97. ulong endtime;
  98. signed long diff;
  99. if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
  100. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  101. tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
  102. tmo /= 1000; /* finish normalize. */
  103. } else { /* else small number, don't kill it prior to HZ multiply */
  104. tmo = usec * CONFIG_SYS_HZ;
  105. tmo /= (1000*1000);
  106. }
  107. endtime = get_timer_masked () + tmo;
  108. do {
  109. ulong now = get_timer_masked ();
  110. diff = endtime - now;
  111. } while (diff >= 0);
  112. }
  113. /*
  114. * This function is derived from PowerPC code (read timebase as long long).
  115. * On ARM it just returns the timer value.
  116. */
  117. unsigned long long get_ticks(void)
  118. {
  119. return get_timer(0);
  120. }
  121. /*
  122. * This function is derived from PowerPC code (timebase clock frequency).
  123. * On ARM it returns the number of timer ticks per second.
  124. */
  125. ulong get_tbclk (void)
  126. {
  127. return CONFIG_SYS_HZ;
  128. }