timer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #define TIMER_LOAD_VAL 0xffffffff
  23. /* macro to read the 32 bit timer */
  24. #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4))
  25. DECLARE_GLOBAL_DATA_PTR;
  26. #define timestamp gd->arch.tbl
  27. #define lastdec gd->arch.lastinc
  28. #define TIMER_ENABLE (1 << 7)
  29. #define TIMER_MODE_MSK (1 << 6)
  30. #define TIMER_MODE_FR (0 << 6)
  31. #define TIMER_MODE_PD (1 << 6)
  32. #define TIMER_INT_EN (1 << 5)
  33. #define TIMER_PRS_MSK (3 << 2)
  34. #define TIMER_PRS_8S (1 << 3)
  35. #define TIMER_SIZE_MSK (1 << 2)
  36. #define TIMER_ONE_SHT (1 << 0)
  37. int timer_init (void)
  38. {
  39. ulong tmr_ctrl_val;
  40. /* 1st disable the Timer */
  41. tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
  42. tmr_ctrl_val &= ~TIMER_ENABLE;
  43. *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
  44. /*
  45. * The Timer Control Register has one Undefined/Shouldn't Use Bit
  46. * So we should do read/modify/write Operation
  47. */
  48. /*
  49. * Timer Mode : Free Running
  50. * Interrupt : Disabled
  51. * Prescale : 8 Stage, Clk/256
  52. * Tmr Siz : 16 Bit Counter
  53. * Tmr in Wrapping Mode
  54. */
  55. tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
  56. tmr_ctrl_val &= ~(TIMER_MODE_MSK | TIMER_INT_EN | TIMER_PRS_MSK | TIMER_SIZE_MSK | TIMER_ONE_SHT );
  57. tmr_ctrl_val |= (TIMER_ENABLE | TIMER_PRS_8S);
  58. *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
  59. /* init the timestamp and lastdec value */
  60. reset_timer_masked();
  61. return 0;
  62. }
  63. /*
  64. * timer without interrupts
  65. */
  66. ulong get_timer (ulong base)
  67. {
  68. return get_timer_masked () - base;
  69. }
  70. /* delay x useconds AND preserve advance timestamp value */
  71. void __udelay (unsigned long usec)
  72. {
  73. ulong tmo, tmp;
  74. if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
  75. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  76. tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
  77. tmo /= 1000; /* finish normalize. */
  78. }else{ /* else small number, don't kill it prior to HZ multiply */
  79. tmo = usec * CONFIG_SYS_HZ;
  80. tmo /= (1000*1000);
  81. }
  82. tmp = get_timer (0); /* get current timestamp */
  83. if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */
  84. reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */
  85. else
  86. tmo += tmp; /* else, set advancing stamp wake up time */
  87. while (get_timer_masked () < tmo)/* loop till event */
  88. /*NOP*/;
  89. }
  90. void reset_timer_masked (void)
  91. {
  92. /* reset time */
  93. lastdec = READ_TIMER; /* capure current decrementer value time */
  94. timestamp = 0; /* start "advancing" time stamp from 0 */
  95. }
  96. ulong get_timer_masked (void)
  97. {
  98. ulong now = READ_TIMER; /* current tick value */
  99. if (lastdec >= now) { /* normal mode (non roll) */
  100. /* normal mode */
  101. timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
  102. } else { /* we have overflow of the count down timer */
  103. /* nts = ts + ld + (TLV - now)
  104. * ts=old stamp, ld=time that passed before passing through -1
  105. * (TLV-now) amount of time after passing though -1
  106. * nts = new "advancing time stamp"...it could also roll and cause problems.
  107. */
  108. timestamp += lastdec + TIMER_LOAD_VAL - now;
  109. }
  110. lastdec = now;
  111. return timestamp;
  112. }
  113. /* waits specified delay value and resets timestamp */
  114. void udelay_masked (unsigned long usec)
  115. {
  116. ulong tmo;
  117. ulong endtime;
  118. signed long diff;
  119. if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
  120. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  121. tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
  122. tmo /= 1000; /* finish normalize. */
  123. } else { /* else small number, don't kill it prior to HZ multiply */
  124. tmo = usec * CONFIG_SYS_HZ;
  125. tmo /= (1000*1000);
  126. }
  127. endtime = get_timer_masked () + tmo;
  128. do {
  129. ulong now = get_timer_masked ();
  130. diff = endtime - now;
  131. } while (diff >= 0);
  132. }
  133. /*
  134. * This function is derived from PowerPC code (read timebase as long long).
  135. * On ARM it just returns the timer value.
  136. */
  137. unsigned long long get_ticks(void)
  138. {
  139. return get_timer(0);
  140. }
  141. /*
  142. * This function is derived from PowerPC code (timebase clock frequency).
  143. * On ARM it returns the number of timer ticks per second.
  144. */
  145. ulong get_tbclk (void)
  146. {
  147. ulong tbclk;
  148. tbclk = CONFIG_SYS_HZ;
  149. return tbclk;
  150. }