timer.c 4.9 KB

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