timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009 Faraday Technology
  4. * Po-Yu Chuang <ratbert@faraday-tech.com>
  5. *
  6. * Copyright (C) 2011 Andes Technology Corporation
  7. * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
  8. * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
  9. */
  10. #ifndef CONFIG_TIMER
  11. #include <common.h>
  12. #include <time.h>
  13. #include <asm/io.h>
  14. #include <faraday/fttmr010.h>
  15. static ulong timestamp;
  16. static ulong lastdec;
  17. int timer_init(void)
  18. {
  19. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  20. unsigned int cr;
  21. debug("%s()\n", __func__);
  22. /* disable timers */
  23. writel(0, &tmr->cr);
  24. #ifdef CONFIG_FTTMR010_EXT_CLK
  25. /* use 32768Hz oscillator for RTC, WDT, TIMER */
  26. ftpmu010_32768osc_enable();
  27. #endif
  28. /* setup timer */
  29. writel(TIMER_LOAD_VAL, &tmr->timer3_load);
  30. writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
  31. writel(0, &tmr->timer3_match1);
  32. writel(0, &tmr->timer3_match2);
  33. /* we don't want timer to issue interrupts */
  34. writel(FTTMR010_TM3_MATCH1 |
  35. FTTMR010_TM3_MATCH2 |
  36. FTTMR010_TM3_OVERFLOW,
  37. &tmr->interrupt_mask);
  38. cr = readl(&tmr->cr);
  39. #ifdef CONFIG_FTTMR010_EXT_CLK
  40. cr |= FTTMR010_TM3_CLOCK; /* use external clock */
  41. #endif
  42. cr |= FTTMR010_TM3_ENABLE;
  43. writel(cr, &tmr->cr);
  44. /* init the timestamp and lastdec value */
  45. reset_timer_masked();
  46. return 0;
  47. }
  48. /*
  49. * timer without interrupts
  50. */
  51. /*
  52. * reset time
  53. */
  54. void reset_timer_masked(void)
  55. {
  56. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  57. /* capure current decrementer value time */
  58. #ifdef CONFIG_FTTMR010_EXT_CLK
  59. lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  60. #else
  61. lastdec = readl(&tmr->timer3_counter) /
  62. (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
  63. #endif
  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. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  78. /* current tick value */
  79. #ifdef CONFIG_FTTMR010_EXT_CLK
  80. ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
  81. #else
  82. ulong now = readl(&tmr->timer3_counter) /
  83. (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
  84. #endif
  85. debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
  86. if (lastdec >= now) {
  87. /*
  88. * normal mode (non roll)
  89. * move stamp fordward with absoulte diff ticks
  90. */
  91. timestamp += lastdec - now;
  92. } else {
  93. /*
  94. * we have overflow of the count down timer
  95. *
  96. * nts = ts + ld + (TLV - now)
  97. * ts=old stamp, ld=time that passed before passing through -1
  98. * (TLV-now) amount of time after passing though -1
  99. * nts = new "advancing time stamp"...it could also roll and
  100. * cause problems.
  101. */
  102. timestamp += lastdec + TIMER_LOAD_VAL - now;
  103. }
  104. lastdec = now;
  105. debug("%s() returns %lx\n", __func__, timestamp);
  106. return timestamp;
  107. }
  108. /*
  109. * return difference between timer ticks and base
  110. */
  111. ulong get_timer(ulong base)
  112. {
  113. debug("%s(%lx)\n", __func__, base);
  114. return get_timer_masked() - base;
  115. }
  116. void set_timer(ulong t)
  117. {
  118. debug("%s(%lx)\n", __func__, t);
  119. timestamp = t;
  120. }
  121. /* delay x useconds AND preserve advance timestamp value */
  122. void __udelay(unsigned long usec)
  123. {
  124. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  125. #ifdef CONFIG_FTTMR010_EXT_CLK
  126. long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  127. #else
  128. long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
  129. #endif
  130. unsigned long now, last = readl(&tmr->timer3_counter);
  131. debug("%s(%lu)\n", __func__, usec);
  132. while (tmo > 0) {
  133. now = readl(&tmr->timer3_counter);
  134. if (now > last) /* count down timer overflow */
  135. tmo -= TIMER_LOAD_VAL + last - now;
  136. else
  137. tmo -= last - now;
  138. last = now;
  139. }
  140. }
  141. /*
  142. * This function is derived from PowerPC code (read timebase as long long).
  143. * On ARM it just returns the timer value.
  144. */
  145. unsigned long long get_ticks(void)
  146. {
  147. debug("%s()\n", __func__);
  148. return get_timer(0);
  149. }
  150. /*
  151. * This function is derived from PowerPC code (timebase clock frequency).
  152. * On ARM it returns the number of timer ticks per second.
  153. */
  154. ulong get_tbclk(void)
  155. {
  156. debug("%s()\n", __func__);
  157. #ifdef CONFIG_FTTMR010_EXT_CLK
  158. return CONFIG_SYS_HZ;
  159. #else
  160. return CONFIG_SYS_CLK_FREQ;
  161. #endif
  162. }
  163. #endif /* CONFIG_TIMER */