timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 __udelay (unsigned long usec)
  62. {
  63. ulong tmo;
  64. tmo = usec / 1000;
  65. tmo *= CONFIG_SYS_HZ;
  66. tmo /= 8;
  67. tmo += get_timer (0);
  68. while (get_timer_masked () < tmo)
  69. /*NOP*/;
  70. }
  71. void reset_timer_masked (void)
  72. {
  73. /* reset time */
  74. lastdec = READ_TIMER;
  75. timestamp = 0;
  76. }
  77. ulong get_timer_masked (void)
  78. {
  79. ulong now = READ_TIMER;
  80. if (lastdec >= now) {
  81. /* normal mode */
  82. timestamp += lastdec - now;
  83. } else {
  84. /* we have an overflow ... */
  85. timestamp += lastdec + TIMER_LOAD_VAL - now;
  86. }
  87. lastdec = now;
  88. return timestamp;
  89. }
  90. void udelay_masked (unsigned long usec)
  91. {
  92. ulong tmo;
  93. ulong endtime;
  94. signed long diff;
  95. if (usec >= 1000) {
  96. tmo = usec / 1000;
  97. tmo *= CONFIG_SYS_HZ;
  98. tmo /= 8;
  99. } else {
  100. tmo = usec * CONFIG_SYS_HZ;
  101. tmo /= (1000*8);
  102. }
  103. endtime = get_timer(0) + tmo;
  104. do {
  105. ulong now = get_timer_masked ();
  106. diff = endtime - now;
  107. } while (diff >= 0);
  108. }