timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * (C) Copyright 2004
  3. * DAVE Srl
  4. * http://www.dave-tech.it
  5. * http://www.wawnet.biz
  6. * mailto:info@wawnet.biz
  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/hardware.h>
  28. /* we always count down the max. */
  29. #define TIMER_LOAD_VAL 0xffff
  30. /* macro to read the 16 bit timer */
  31. #define READ_TIMER (TCNTO1 & 0xffff)
  32. #ifdef CONFIG_USE_IRQ
  33. #error CONFIG_USE_IRQ NOT supported
  34. #endif
  35. static ulong timestamp;
  36. static ulong lastdec;
  37. int timer_init (void)
  38. {
  39. TCFG0 = 0x000000E9;
  40. TCFG1 = 0x00000004;
  41. TCON = 0x00000900;
  42. TCNTB1 = TIMER_LOAD_VAL;
  43. TCMPB1 = 0;
  44. TCON = 0x00000B00;
  45. TCON = 0x00000900;
  46. lastdec = TCNTB1 = TIMER_LOAD_VAL;
  47. timestamp = 0;
  48. return 0;
  49. }
  50. /*
  51. * timer without interrupts
  52. */
  53. void reset_timer (void)
  54. {
  55. reset_timer_masked ();
  56. }
  57. ulong get_timer (ulong base)
  58. {
  59. return get_timer_masked () - base;
  60. }
  61. void set_timer (ulong t)
  62. {
  63. timestamp = t;
  64. }
  65. void udelay (unsigned long usec)
  66. {
  67. ulong tmo;
  68. tmo = usec / 1000;
  69. tmo *= CONFIG_SYS_HZ;
  70. tmo /= 8;
  71. tmo += get_timer (0);
  72. while (get_timer_masked () < tmo)
  73. /*NOP*/;
  74. }
  75. void reset_timer_masked (void)
  76. {
  77. /* reset time */
  78. lastdec = READ_TIMER;
  79. timestamp = 0;
  80. }
  81. ulong get_timer_masked (void)
  82. {
  83. ulong now = READ_TIMER;
  84. if (lastdec >= now) {
  85. /* normal mode */
  86. timestamp += lastdec - now;
  87. } else {
  88. /* we have an overflow ... */
  89. timestamp += lastdec + TIMER_LOAD_VAL - now;
  90. }
  91. lastdec = now;
  92. return timestamp;
  93. }
  94. void udelay_masked (unsigned long usec)
  95. {
  96. ulong tmo;
  97. ulong endtime;
  98. signed long diff;
  99. if (usec >= 1000) {
  100. tmo = usec / 1000;
  101. tmo *= CONFIG_SYS_HZ;
  102. tmo /= 8;
  103. } else {
  104. tmo = usec * CONFIG_SYS_HZ;
  105. tmo /= (1000*8);
  106. }
  107. endtime = get_timer(0) + tmo;
  108. do {
  109. ulong now = get_timer_masked ();
  110. diff = endtime - now;
  111. } while (diff >= 0);
  112. }