time.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * (C) Copyright 2009
  3. * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  4. *
  5. * (C) Copyright 2007-2008
  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 inline unsigned long long tick_to_time(unsigned long long tick)
  37. {
  38. tick *= CONFIG_SYS_HZ;
  39. do_div(tick, timer_freq);
  40. return tick;
  41. }
  42. static inline unsigned long long usec_to_tick(unsigned long long usec)
  43. {
  44. usec *= timer_freq;
  45. do_div(usec, 1000000);
  46. return usec;
  47. }
  48. static void tmu_timer_start (unsigned int timer)
  49. {
  50. if (timer > 2)
  51. return;
  52. writeb(readb(TSTR) | (1 << timer), TSTR);
  53. }
  54. static void tmu_timer_stop (unsigned int timer)
  55. {
  56. if (timer > 2)
  57. return;
  58. writeb(readb(TSTR) & ~(1 << timer), TSTR);
  59. }
  60. int timer_init (void)
  61. {
  62. /* Divide clock by TMU_CLK_DIVIDER */
  63. u16 bit = 0;
  64. switch (TMU_CLK_DIVIDER) {
  65. case 1024:
  66. bit = 4;
  67. break;
  68. case 256:
  69. bit = 3;
  70. break;
  71. case 64:
  72. bit = 2;
  73. break;
  74. case 16:
  75. bit = 1;
  76. break;
  77. case 4:
  78. default:
  79. break;
  80. }
  81. writew(readw(TCR0) | bit, TCR0);
  82. /* Calc clock rate */
  83. timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
  84. tmu_timer_stop(0);
  85. tmu_timer_start(0);
  86. return 0;
  87. }
  88. unsigned long long get_ticks (void)
  89. {
  90. return 0 - readl(TCNT0);
  91. }
  92. void udelay (unsigned long usec)
  93. {
  94. unsigned long long tmp;
  95. ulong tmo;
  96. tmo = usec_to_tick(usec);
  97. tmp = get_ticks() + tmo; /* get current timestamp */
  98. while (get_ticks() < tmp) /* loop till event */
  99. /*NOP*/;
  100. }
  101. unsigned long get_timer (unsigned long base)
  102. {
  103. /* return msec */
  104. return tick_to_time(get_ticks()) - base;
  105. }
  106. void set_timer (unsigned long t)
  107. {
  108. writel((0 - t), TCNT0);
  109. }
  110. void reset_timer (void)
  111. {
  112. tmu_timer_stop(0);
  113. set_timer (0);
  114. tmu_timer_start(0);
  115. }
  116. unsigned long get_tbclk (void)
  117. {
  118. return timer_freq;
  119. }