timer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/ftpmu010.h>
  22. #include <asm/arch/fttmr010.h>
  23. static ulong timestamp;
  24. static ulong lastdec;
  25. static struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  26. static struct ftpmu010 *pmu = (struct ftpmu010 *)CONFIG_FTPMU010_BASE;
  27. #define TIMER_CLOCK 32768
  28. #define TIMER_LOAD_VAL 0xffffffff
  29. int timer_init(void)
  30. {
  31. unsigned int oscc;
  32. unsigned int cr;
  33. debug("%s()\n", __func__);
  34. /* disable timers */
  35. writel(0, &tmr->cr);
  36. /*
  37. * use 32768Hz oscillator for RTC, WDT, TIMER
  38. */
  39. /* enable the 32768Hz oscillator */
  40. oscc = readl(&pmu->OSCC);
  41. oscc &= ~(FTPMU010_OSCC_OSCL_OFF | FTPMU010_OSCC_OSCL_TRI);
  42. writel(oscc, &pmu->OSCC);
  43. /* wait until ready */
  44. while (!(readl(&pmu->OSCC) & FTPMU010_OSCC_OSCL_STABLE))
  45. ;
  46. /* select 32768Hz oscillator */
  47. oscc = readl(&pmu->OSCC);
  48. oscc |= FTPMU010_OSCC_OSCL_RTCLSEL;
  49. writel(oscc, &pmu->OSCC);
  50. /* setup timer */
  51. writel(TIMER_LOAD_VAL, &tmr->timer3_load);
  52. writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
  53. writel(0, &tmr->timer3_match1);
  54. writel(0, &tmr->timer3_match2);
  55. /* we don't want timer to issue interrupts */
  56. writel(FTTMR010_TM3_MATCH1 |
  57. FTTMR010_TM3_MATCH2 |
  58. FTTMR010_TM3_OVERFLOW,
  59. &tmr->interrupt_mask);
  60. cr = readl(&tmr->cr);
  61. cr |= FTTMR010_TM3_CLOCK; /* use external clock */
  62. cr |= FTTMR010_TM3_ENABLE;
  63. writel(cr, &tmr->cr);
  64. /* init the timestamp and lastdec value */
  65. reset_timer_masked();
  66. return 0;
  67. }
  68. /*
  69. * timer without interrupts
  70. */
  71. /*
  72. * reset time
  73. */
  74. void reset_timer_masked(void)
  75. {
  76. /* capure current decrementer value time */
  77. lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  78. timestamp = 0; /* start "advancing" time stamp from 0 */
  79. debug("%s(): lastdec = %lx\n", __func__, lastdec);
  80. }
  81. void reset_timer(void)
  82. {
  83. debug("%s()\n", __func__);
  84. reset_timer_masked();
  85. }
  86. /*
  87. * return timer ticks
  88. */
  89. ulong get_timer_masked(void)
  90. {
  91. /* current tick value */
  92. ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  93. debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
  94. if (lastdec >= now) {
  95. /*
  96. * normal mode (non roll)
  97. * move stamp fordward with absoulte diff ticks
  98. */
  99. timestamp += lastdec - now;
  100. } else {
  101. /*
  102. * we have overflow of the count down timer
  103. *
  104. * nts = ts + ld + (TLV - now)
  105. * ts=old stamp, ld=time that passed before passing through -1
  106. * (TLV-now) amount of time after passing though -1
  107. * nts = new "advancing time stamp"...it could also roll and
  108. * cause problems.
  109. */
  110. timestamp += lastdec + TIMER_LOAD_VAL - now;
  111. }
  112. lastdec = now;
  113. debug("%s() returns %lx\n", __func__, timestamp);
  114. return timestamp;
  115. }
  116. /*
  117. * return difference between timer ticks and base
  118. */
  119. ulong get_timer(ulong base)
  120. {
  121. debug("%s(%lx)\n", __func__, base);
  122. return get_timer_masked() - base;
  123. }
  124. void set_timer(ulong t)
  125. {
  126. debug("%s(%lx)\n", __func__, t);
  127. timestamp = t;
  128. }
  129. /* delay x useconds AND preserve advance timestamp value */
  130. void __udelay(unsigned long usec)
  131. {
  132. long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  133. unsigned long now, last = readl(&tmr->timer3_counter);
  134. debug("%s(%lu)\n", __func__, usec);
  135. while (tmo > 0) {
  136. now = readl(&tmr->timer3_counter);
  137. if (now > last) /* count down timer overflow */
  138. tmo -= TIMER_LOAD_VAL + last - now;
  139. else
  140. tmo -= last - now;
  141. last = now;
  142. }
  143. }
  144. /*
  145. * This function is derived from PowerPC code (read timebase as long long).
  146. * On ARM it just returns the timer value.
  147. */
  148. unsigned long long get_ticks(void)
  149. {
  150. debug("%s()\n", __func__);
  151. return get_timer(0);
  152. }
  153. /*
  154. * This function is derived from PowerPC code (timebase clock frequency).
  155. * On ARM it returns the number of timer ticks per second.
  156. */
  157. ulong get_tbclk(void)
  158. {
  159. debug("%s()\n", __func__);
  160. return CONFIG_SYS_HZ;
  161. }