timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * (C) Copyright 2002
  3. * Lineo, Inc. <www.lineo.com>
  4. * Bernhard Kuhn <bkuhn@lineo.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * (C) Copyright 2002
  11. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  12. * Alex Zuepke <azu@sysgo.de>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <asm/io.h>
  34. #include <asm/hardware.h>
  35. #include <asm/arch/at91_tc.h>
  36. #include <asm/arch/at91_pmc.h>
  37. /* the number of clocks per CONFIG_SYS_HZ */
  38. #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
  39. static u32 timestamp;
  40. static u32 lastinc;
  41. int timer_init(void)
  42. {
  43. at91_tc_t *tc = (at91_tc_t *) AT91_TC_BASE;
  44. at91_pmc_t *pmc = (at91_pmc_t *) AT91_PMC_BASE;
  45. /* enables TC1.0 clock */
  46. writel(1 << AT91_ID_TC0, &pmc->pcer); /* enable clock */
  47. writel(0, &tc->bcr);
  48. writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
  49. AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
  50. writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
  51. /* set to MCLK/2 and restart the timer
  52. when the value in TC_RC is reached */
  53. writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
  54. writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interupts */
  55. writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
  56. writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
  57. lastinc = 0;
  58. timestamp = 0;
  59. return 0;
  60. }
  61. /*
  62. * timer without interrupts
  63. */
  64. void reset_timer(void)
  65. {
  66. reset_timer_masked();
  67. }
  68. ulong get_timer(ulong base)
  69. {
  70. return get_timer_masked() - base;
  71. }
  72. void set_timer(ulong t)
  73. {
  74. timestamp = t;
  75. }
  76. void __udelay(unsigned long usec)
  77. {
  78. udelay_masked(usec);
  79. }
  80. void reset_timer_masked(void)
  81. {
  82. /* reset time */
  83. at91_tc_t *tc = (at91_tc_t *) AT91_TC_BASE;
  84. lastinc = readl(&tc->tc[0].cv) & 0x0000ffff;
  85. timestamp = 0;
  86. }
  87. ulong get_timer_raw(void)
  88. {
  89. at91_tc_t *tc = (at91_tc_t *) AT91_TC_BASE;
  90. u32 now;
  91. now = readl(&tc->tc[0].cv) & 0x0000ffff;
  92. if (now >= lastinc) {
  93. /* normal mode */
  94. timestamp += now - lastinc;
  95. } else {
  96. /* we have an overflow ... */
  97. timestamp += now + TIMER_LOAD_VAL - lastinc;
  98. }
  99. lastinc = now;
  100. return timestamp;
  101. }
  102. ulong get_timer_masked(void)
  103. {
  104. return get_timer_raw()/TIMER_LOAD_VAL;
  105. }
  106. void udelay_masked(unsigned long usec)
  107. {
  108. u32 tmo;
  109. u32 endtime;
  110. signed long diff;
  111. tmo = CONFIG_SYS_HZ_CLOCK / 1000;
  112. tmo *= usec;
  113. tmo /= 1000;
  114. endtime = get_timer_raw() + tmo;
  115. do {
  116. u32 now = get_timer_raw();
  117. diff = endtime - now;
  118. } while (diff >= 0);
  119. }
  120. /*
  121. * This function is derived from PowerPC code (read timebase as long long).
  122. * On ARM it just returns the timer value.
  123. */
  124. unsigned long long get_ticks(void)
  125. {
  126. return get_timer(0);
  127. }
  128. /*
  129. * This function is derived from PowerPC code (timebase clock frequency).
  130. * On ARM it returns the number of timer ticks per second.
  131. */
  132. ulong get_tbclk(void)
  133. {
  134. return CONFIG_SYS_HZ;
  135. }