timer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, <gj@denx.de>
  15. *
  16. * (C) Copyright 2004
  17. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  18. *
  19. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  20. *
  21. * See file CREDITS for list of people who contributed to this
  22. * project.
  23. *
  24. * This program is free software; you can redistribute it and/or
  25. * modify it under the terms of the GNU General Public License as
  26. * published by the Free Software Foundation; either version 2 of
  27. * the License, or (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  37. * MA 02111-1307 USA
  38. */
  39. #include <common.h>
  40. #include <arm926ejs.h>
  41. typedef volatile struct {
  42. u_int32_t pid12;
  43. u_int32_t emumgt_clksped;
  44. u_int32_t gpint_en;
  45. u_int32_t gpdir_dat;
  46. u_int32_t tim12;
  47. u_int32_t tim34;
  48. u_int32_t prd12;
  49. u_int32_t prd34;
  50. u_int32_t tcr;
  51. u_int32_t tgcr;
  52. u_int32_t wdtcr;
  53. u_int32_t tlgc;
  54. u_int32_t tlmr;
  55. } davinci_timer;
  56. davinci_timer *timer = (davinci_timer *)CFG_TIMERBASE;
  57. #define TIMER_LOAD_VAL (CFG_HZ_CLOCK / CFG_HZ)
  58. #define READ_TIMER timer->tim34
  59. /* Timer runs with CFG_HZ_CLOCK, currently 27MHz. To avoid wrap
  60. around of timestamp already after min ~159s, divide it, e.g. by 16.
  61. timestamp will then wrap around all min ~42min */
  62. #define DIV(x) ((x) >> 4)
  63. static ulong timestamp;
  64. static ulong lastinc;
  65. int timer_init(void)
  66. {
  67. /* We are using timer34 in unchained 32-bit mode, full speed */
  68. timer->tcr = 0x0;
  69. timer->tgcr = 0x0;
  70. timer->tgcr = 0x06;
  71. timer->tim34 = 0x0;
  72. timer->prd34 = TIMER_LOAD_VAL;
  73. lastinc = 0;
  74. timer->tcr = 0x80 << 16;
  75. timestamp = 0;
  76. return(0);
  77. }
  78. void reset_timer(void)
  79. {
  80. reset_timer_masked();
  81. }
  82. ulong get_timer(ulong base)
  83. {
  84. return(get_timer_masked() - base);
  85. }
  86. void set_timer(ulong t)
  87. {
  88. timestamp = t;
  89. }
  90. void udelay(unsigned long usec)
  91. {
  92. udelay_masked(usec);
  93. }
  94. void reset_timer_masked(void)
  95. {
  96. lastinc = DIV(READ_TIMER);
  97. timestamp = 0;
  98. }
  99. ulong get_timer_raw(void)
  100. {
  101. ulong now = DIV(READ_TIMER);
  102. if (now >= lastinc) {
  103. /* normal mode */
  104. timestamp += now - lastinc;
  105. } else {
  106. /* overflow ... */
  107. timestamp += now + DIV(TIMER_LOAD_VAL) - lastinc;
  108. }
  109. lastinc = now;
  110. return timestamp;
  111. }
  112. ulong get_timer_masked(void)
  113. {
  114. return(get_timer_raw() / DIV(TIMER_LOAD_VAL));
  115. }
  116. void udelay_masked(unsigned long usec)
  117. {
  118. ulong tmo;
  119. ulong endtime;
  120. signed long diff;
  121. tmo = CFG_HZ_CLOCK / 1000;
  122. tmo *= usec;
  123. tmo /= 1000;
  124. endtime = get_timer_raw() + tmo;
  125. do {
  126. ulong now = get_timer_raw();
  127. diff = endtime - now;
  128. } while (diff >= 0);
  129. }
  130. /*
  131. * This function is derived from PowerPC code (read timebase as long long).
  132. * On ARM it just returns the timer value.
  133. */
  134. unsigned long long get_ticks(void)
  135. {
  136. return(get_timer(0));
  137. }
  138. /*
  139. * This function is derived from PowerPC code (timebase clock frequency).
  140. * On ARM it returns the number of timer ticks per second.
  141. */
  142. ulong get_tbclk(void)
  143. {
  144. ulong tbclk;
  145. tbclk = CFG_HZ;
  146. return(tbclk);
  147. }