timer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * (C) Copyright 2009 Faraday Technology
  3. * Po-Yu Chuang <ratbert@faraday-tech.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <common.h>
  20. #include <asm/io.h>
  21. #include <asm/arch/fttmr010.h>
  22. #include <faraday/ftpmu010.h>
  23. static ulong timestamp;
  24. static ulong lastdec;
  25. static struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  26. #define TIMER_CLOCK 32768
  27. #define TIMER_LOAD_VAL 0xffffffff
  28. int timer_init(void)
  29. {
  30. unsigned int cr;
  31. debug("%s()\n", __func__);
  32. /* disable timers */
  33. writel(0, &tmr->cr);
  34. /* use 32768Hz oscillator for RTC, WDT, TIMER */
  35. ftpmu010_32768osc_enable();
  36. /* setup timer */
  37. writel(TIMER_LOAD_VAL, &tmr->timer3_load);
  38. writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
  39. writel(0, &tmr->timer3_match1);
  40. writel(0, &tmr->timer3_match2);
  41. /* we don't want timer to issue interrupts */
  42. writel(FTTMR010_TM3_MATCH1 |
  43. FTTMR010_TM3_MATCH2 |
  44. FTTMR010_TM3_OVERFLOW,
  45. &tmr->interrupt_mask);
  46. cr = readl(&tmr->cr);
  47. cr |= FTTMR010_TM3_CLOCK; /* use external clock */
  48. cr |= FTTMR010_TM3_ENABLE;
  49. writel(cr, &tmr->cr);
  50. /* init the timestamp and lastdec value */
  51. reset_timer_masked();
  52. return 0;
  53. }
  54. /*
  55. * timer without interrupts
  56. */
  57. /*
  58. * reset time
  59. */
  60. void reset_timer_masked(void)
  61. {
  62. /* capure current decrementer value time */
  63. lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  64. timestamp = 0; /* start "advancing" time stamp from 0 */
  65. debug("%s(): lastdec = %lx\n", __func__, lastdec);
  66. }
  67. void reset_timer(void)
  68. {
  69. debug("%s()\n", __func__);
  70. reset_timer_masked();
  71. }
  72. /*
  73. * return timer ticks
  74. */
  75. ulong get_timer_masked(void)
  76. {
  77. /* current tick value */
  78. ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  79. debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
  80. if (lastdec >= now) {
  81. /*
  82. * normal mode (non roll)
  83. * move stamp fordward with absoulte diff ticks
  84. */
  85. timestamp += lastdec - now;
  86. } else {
  87. /*
  88. * we have overflow of the count down timer
  89. *
  90. * nts = ts + ld + (TLV - now)
  91. * ts=old stamp, ld=time that passed before passing through -1
  92. * (TLV-now) amount of time after passing though -1
  93. * nts = new "advancing time stamp"...it could also roll and
  94. * cause problems.
  95. */
  96. timestamp += lastdec + TIMER_LOAD_VAL - now;
  97. }
  98. lastdec = now;
  99. debug("%s() returns %lx\n", __func__, timestamp);
  100. return timestamp;
  101. }
  102. /*
  103. * return difference between timer ticks and base
  104. */
  105. ulong get_timer(ulong base)
  106. {
  107. debug("%s(%lx)\n", __func__, base);
  108. return get_timer_masked() - base;
  109. }
  110. void set_timer(ulong t)
  111. {
  112. debug("%s(%lx)\n", __func__, t);
  113. timestamp = t;
  114. }
  115. /* delay x useconds AND preserve advance timestamp value */
  116. void __udelay(unsigned long usec)
  117. {
  118. long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  119. unsigned long now, last = readl(&tmr->timer3_counter);
  120. debug("%s(%lu)\n", __func__, usec);
  121. while (tmo > 0) {
  122. now = readl(&tmr->timer3_counter);
  123. if (now > last) /* count down timer overflow */
  124. tmo -= TIMER_LOAD_VAL + last - now;
  125. else
  126. tmo -= last - now;
  127. last = now;
  128. }
  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. debug("%s()\n", __func__);
  137. return get_timer(0);
  138. }
  139. /*
  140. * This function is derived from PowerPC code (timebase clock frequency).
  141. * On ARM it returns the number of timer ticks per second.
  142. */
  143. ulong get_tbclk(void)
  144. {
  145. debug("%s()\n", __func__);
  146. return CONFIG_SYS_HZ;
  147. }