timer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <faraday/ftpmu010.h>
  22. #include <faraday/fttmr010.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. /*
  68. * return timer ticks
  69. */
  70. ulong get_timer_masked(void)
  71. {
  72. /* current tick value */
  73. ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  74. debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
  75. if (lastdec >= now) {
  76. /*
  77. * normal mode (non roll)
  78. * move stamp fordward with absoulte diff ticks
  79. */
  80. timestamp += lastdec - now;
  81. } else {
  82. /*
  83. * we have overflow of the count down timer
  84. *
  85. * nts = ts + ld + (TLV - now)
  86. * ts=old stamp, ld=time that passed before passing through -1
  87. * (TLV-now) amount of time after passing though -1
  88. * nts = new "advancing time stamp"...it could also roll and
  89. * cause problems.
  90. */
  91. timestamp += lastdec + TIMER_LOAD_VAL - now;
  92. }
  93. lastdec = now;
  94. debug("%s() returns %lx\n", __func__, timestamp);
  95. return timestamp;
  96. }
  97. /*
  98. * return difference between timer ticks and base
  99. */
  100. ulong get_timer(ulong base)
  101. {
  102. debug("%s(%lx)\n", __func__, base);
  103. return get_timer_masked() - base;
  104. }
  105. /* delay x useconds AND preserve advance timestamp value */
  106. void __udelay(unsigned long usec)
  107. {
  108. long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  109. unsigned long now, last = readl(&tmr->timer3_counter);
  110. debug("%s(%lu)\n", __func__, usec);
  111. while (tmo > 0) {
  112. now = readl(&tmr->timer3_counter);
  113. if (now > last) /* count down timer overflow */
  114. tmo -= TIMER_LOAD_VAL + last - now;
  115. else
  116. tmo -= last - now;
  117. last = now;
  118. }
  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. debug("%s()\n", __func__);
  127. return get_timer(0);
  128. }
  129. /*
  130. * This function is derived from PowerPC code (timebase clock frequency).
  131. * On ARM it returns the number of timer ticks per second.
  132. */
  133. ulong get_tbclk(void)
  134. {
  135. debug("%s()\n", __func__);
  136. return CONFIG_SYS_HZ;
  137. }