time.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * (C) Copyright 2009
  3. * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  4. *
  5. * (C) Copyright 2007-2010
  6. * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
  7. *
  8. * (C) Copyright 2003
  9. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <div64.h>
  31. #include <asm/processor.h>
  32. #include <asm/clk.h>
  33. #include <asm/io.h>
  34. #define TMU_MAX_COUNTER (~0UL)
  35. static ulong timer_freq;
  36. static unsigned long last_tcnt;
  37. static unsigned long long overflow_ticks;
  38. static inline unsigned long long tick_to_time(unsigned long long tick)
  39. {
  40. tick *= CONFIG_SYS_HZ;
  41. do_div(tick, timer_freq);
  42. return tick;
  43. }
  44. static inline unsigned long long usec_to_tick(unsigned long long usec)
  45. {
  46. usec *= timer_freq;
  47. do_div(usec, 1000000);
  48. return usec;
  49. }
  50. static void tmu_timer_start (unsigned int timer)
  51. {
  52. if (timer > 2)
  53. return;
  54. writeb(readb(TSTR) | (1 << timer), TSTR);
  55. }
  56. static void tmu_timer_stop (unsigned int timer)
  57. {
  58. if (timer > 2)
  59. return;
  60. writeb(readb(TSTR) & ~(1 << timer), TSTR);
  61. }
  62. int timer_init (void)
  63. {
  64. /* Divide clock by CONFIG_SYS_TMU_CLK_DIV */
  65. u16 bit = 0;
  66. switch (CONFIG_SYS_TMU_CLK_DIV) {
  67. case 1024:
  68. bit = 4;
  69. break;
  70. case 256:
  71. bit = 3;
  72. break;
  73. case 64:
  74. bit = 2;
  75. break;
  76. case 16:
  77. bit = 1;
  78. break;
  79. case 4:
  80. default:
  81. break;
  82. }
  83. writew(readw(TCR0) | bit, TCR0);
  84. /* Calc clock rate */
  85. timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
  86. tmu_timer_stop(0);
  87. tmu_timer_start(0);
  88. last_tcnt = 0;
  89. overflow_ticks = 0;
  90. return 0;
  91. }
  92. unsigned long long get_ticks (void)
  93. {
  94. unsigned long tcnt = 0 - readl(TCNT0);
  95. unsigned long ticks;
  96. if (last_tcnt > tcnt) { /* overflow */
  97. overflow_ticks++;
  98. ticks = (0xffffffff - last_tcnt) + tcnt;
  99. } else {
  100. ticks = tcnt;
  101. }
  102. last_tcnt = tcnt;
  103. return (overflow_ticks << 32) | tcnt;
  104. }
  105. void __udelay (unsigned long usec)
  106. {
  107. unsigned long long tmp;
  108. ulong tmo;
  109. tmo = usec_to_tick(usec);
  110. tmp = get_ticks() + tmo; /* get current timestamp */
  111. while (get_ticks() < tmp) /* loop till event */
  112. /*NOP*/;
  113. }
  114. unsigned long get_timer (unsigned long base)
  115. {
  116. /* return msec */
  117. return tick_to_time(get_ticks()) - base;
  118. }
  119. void set_timer (unsigned long t)
  120. {
  121. writel((0 - t), TCNT0);
  122. }
  123. void reset_timer (void)
  124. {
  125. tmu_timer_stop(0);
  126. set_timer (0);
  127. tmu_timer_start(0);
  128. }
  129. unsigned long get_tbclk (void)
  130. {
  131. return timer_freq;
  132. }