timer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * 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 <asm/io.h>
  41. struct davinci_timer {
  42. u_int32_t pid12;
  43. u_int32_t emumgt;
  44. u_int32_t na1;
  45. u_int32_t na2;
  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. };
  54. static struct davinci_timer * const timer =
  55. (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
  56. #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
  57. #define TIM_CLK_DIV 16
  58. static ulong timestamp;
  59. static ulong lastinc;
  60. int timer_init(void)
  61. {
  62. /* We are using timer34 in unchained 32-bit mode, full speed */
  63. writel(0x0, &timer->tcr);
  64. writel(0x0, &timer->tgcr);
  65. writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
  66. writel(0x0, &timer->tim34);
  67. writel(TIMER_LOAD_VAL, &timer->prd34);
  68. lastinc = 0;
  69. timestamp = 0;
  70. writel(2 << 22, &timer->tcr);
  71. return(0);
  72. }
  73. void reset_timer(void)
  74. {
  75. writel(0x0, &timer->tcr);
  76. writel(0x0, &timer->tim34);
  77. lastinc = 0;
  78. timestamp = 0;
  79. writel(2 << 22, &timer->tcr);
  80. }
  81. static ulong get_timer_raw(void)
  82. {
  83. ulong now = readl(&timer->tim34);
  84. if (now >= lastinc) {
  85. /* normal mode */
  86. timestamp += now - lastinc;
  87. } else {
  88. /* overflow ... */
  89. timestamp += now + TIMER_LOAD_VAL - lastinc;
  90. }
  91. lastinc = now;
  92. return timestamp;
  93. }
  94. ulong get_timer(ulong base)
  95. {
  96. return((get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base);
  97. }
  98. void set_timer(ulong t)
  99. {
  100. timestamp = t;
  101. }
  102. void __udelay(unsigned long usec)
  103. {
  104. ulong tmo;
  105. ulong endtime;
  106. signed long diff;
  107. tmo = CONFIG_SYS_HZ_CLOCK / 1000;
  108. tmo *= usec;
  109. tmo /= (1000 * TIM_CLK_DIV);
  110. endtime = get_timer_raw() + tmo;
  111. do {
  112. ulong now = get_timer_raw();
  113. diff = endtime - now;
  114. } while (diff >= 0);
  115. }
  116. /*
  117. * This function is derived from PowerPC code (read timebase as long long).
  118. * On ARM it just returns the timer value.
  119. */
  120. unsigned long long get_ticks(void)
  121. {
  122. return(get_timer(0));
  123. }
  124. /*
  125. * This function is derived from PowerPC code (timebase clock frequency).
  126. * On ARM it returns the number of timer ticks per second.
  127. */
  128. ulong get_tbclk(void)
  129. {
  130. return CONFIG_SYS_HZ;
  131. }