time.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
  4. *
  5. * (C) Copyright 2003
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/processor.h>
  28. #include <asm/io.h>
  29. #define TMU_MAX_COUNTER (~0UL)
  30. static int clk_adj = 1;
  31. static void tmu_timer_start (unsigned int timer)
  32. {
  33. if (timer > 2)
  34. return;
  35. writeb(readb(TSTR) | (1 << timer), TSTR);
  36. }
  37. static void tmu_timer_stop (unsigned int timer)
  38. {
  39. if (timer > 2)
  40. return;
  41. writeb(readb(TSTR) & ~(1 << timer), TSTR);
  42. }
  43. int timer_init (void)
  44. {
  45. /* Divide clock by TMU_CLK_DIVIDER */
  46. u16 bit = 0;
  47. switch( TMU_CLK_DIVIDER ){
  48. case 4:
  49. bit = 0;
  50. break;
  51. case 16:
  52. bit = 1;
  53. break;
  54. case 64: bit = 2;
  55. break;
  56. case 256:
  57. bit = 3;
  58. break;
  59. case 1024:
  60. bit = 4;
  61. break;
  62. default:
  63. bit = 0;
  64. break;
  65. }
  66. writew(readw(TCR0) | bit, TCR0);
  67. /* Clock adjustment calc */
  68. clk_adj = (int)(1.0/((1.0/CONFIG_SYS_HZ)*1000000));
  69. if (clk_adj < 1)
  70. clk_adj = 1;
  71. tmu_timer_stop(0);
  72. tmu_timer_start(0);
  73. return 0;
  74. }
  75. unsigned long long get_ticks (void)
  76. {
  77. return 0 - readl(TCNT0);
  78. }
  79. static unsigned long get_usec (void)
  80. {
  81. return (0 - readl(TCNT0));
  82. }
  83. void udelay (unsigned long usec)
  84. {
  85. unsigned int start = get_usec();
  86. unsigned int end = start + (usec * clk_adj);
  87. while (get_usec() < end)
  88. continue;
  89. }
  90. unsigned long get_timer (unsigned long base)
  91. {
  92. /* return msec */
  93. return ((get_usec()/clk_adj)/1000) - base;
  94. }
  95. void set_timer (unsigned long t)
  96. {
  97. writel((0 - t), TCNT0);
  98. }
  99. void reset_timer (void)
  100. {
  101. tmu_timer_stop(0);
  102. set_timer (0);
  103. tmu_timer_start(0);
  104. }
  105. unsigned long get_tbclk (void)
  106. {
  107. return CONFIG_SYS_HZ;
  108. }