timer.c 3.7 KB

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