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 1024:
  49. bit = 4;
  50. break;
  51. case 256:
  52. bit = 3;
  53. break;
  54. case 64:
  55. bit = 2;
  56. break;
  57. case 16:
  58. bit = 1;
  59. break;
  60. case 4:
  61. default:
  62. bit = 0;
  63. break;
  64. }
  65. writew(readw(TCR0) | bit, TCR0);
  66. /* Clock adjustment calc */
  67. clk_adj = (int)(1.0 / ((1.0 / CONFIG_SYS_HZ) * 1000000));
  68. if (clk_adj < 1)
  69. clk_adj = 1;
  70. tmu_timer_stop(0);
  71. tmu_timer_start(0);
  72. return 0;
  73. }
  74. unsigned long long get_ticks (void)
  75. {
  76. return 0 - readl(TCNT0);
  77. }
  78. static unsigned long get_usec (void)
  79. {
  80. return (0 - readl(TCNT0));
  81. }
  82. void udelay (unsigned long usec)
  83. {
  84. unsigned int start = get_usec();
  85. unsigned int end = start + (usec * clk_adj);
  86. while (get_usec() < end)
  87. continue;
  88. }
  89. unsigned long get_timer (unsigned long base)
  90. {
  91. /* return msec */
  92. return ((get_usec() / clk_adj) / 1000) - base;
  93. }
  94. void set_timer (unsigned long t)
  95. {
  96. writel((0 - t), TCNT0);
  97. }
  98. void reset_timer (void)
  99. {
  100. tmu_timer_stop(0);
  101. set_timer (0);
  102. tmu_timer_start(0);
  103. }
  104. unsigned long get_tbclk (void)
  105. {
  106. return CONFIG_SYS_HZ;
  107. }