timer.c 4.2 KB

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