timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ulong get_timer (ulong base)
  54. {
  55. return get_timer_masked () - base;
  56. }
  57. void __udelay (unsigned long usec)
  58. {
  59. ulong tmo;
  60. tmo = usec / 1000;
  61. tmo *= CONFIG_SYS_HZ;
  62. tmo /= 8;
  63. tmo += get_timer (0);
  64. while (get_timer_masked () < tmo)
  65. /*NOP*/;
  66. }
  67. ulong get_timer_masked (void)
  68. {
  69. ulong now = READ_TIMER;
  70. if (lastdec >= now) {
  71. /* normal mode */
  72. timestamp += lastdec - now;
  73. } else {
  74. /* we have an overflow ... */
  75. timestamp += lastdec + TIMER_LOAD_VAL - now;
  76. }
  77. lastdec = now;
  78. return timestamp;
  79. }
  80. void udelay_masked (unsigned long usec)
  81. {
  82. ulong tmo;
  83. ulong endtime;
  84. signed long diff;
  85. if (usec >= 1000) {
  86. tmo = usec / 1000;
  87. tmo *= CONFIG_SYS_HZ;
  88. tmo /= 8;
  89. } else {
  90. tmo = usec * CONFIG_SYS_HZ;
  91. tmo /= (1000*8);
  92. }
  93. endtime = get_timer(0) + tmo;
  94. do {
  95. ulong now = get_timer_masked ();
  96. diff = endtime - now;
  97. } while (diff >= 0);
  98. }