time.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #define TMU_MAX_COUNTER (~0UL)
  26. static void tmu_timer_start (unsigned int timer)
  27. {
  28. if (timer > 2)
  29. return;
  30. *((volatile unsigned char *) TSTR0) |= (1 << timer);
  31. }
  32. int timer_init (void)
  33. {
  34. /* Divide clock by 4 */
  35. *(volatile u16 *)TCR0 = 0;
  36. tmu_timer_start (0);
  37. return 0;
  38. }
  39. /*
  40. In theory we should return a true 64bit value (ie something that doesn't
  41. overflow). However, we don't. Therefore if TMU runs at fastest rate of
  42. 6.75 MHz this value will wrap after u-boot has been running for approx
  43. 10 minutes.
  44. */
  45. unsigned long long get_ticks (void)
  46. {
  47. return (0 - *((volatile unsigned int *) TCNT0));
  48. }
  49. unsigned long get_timer (unsigned long base)
  50. {
  51. unsigned long n =
  52. *((volatile unsigned int *)TCNT0) ;
  53. return ((int)n - base ) < 0 ? ( TMU_MAX_COUNTER - ( base -n )):(n - base );
  54. }
  55. void set_timer (unsigned long t)
  56. {
  57. *((volatile unsigned int *) TCNT0) = (0 - t);
  58. }
  59. void reset_timer (void)
  60. {
  61. set_timer (0);
  62. }
  63. void udelay (unsigned long usec)
  64. {
  65. unsigned int start = get_timer (0);
  66. unsigned int end = 0;
  67. if (usec > 1000000)
  68. end = ((usec/100000) * CFG_HZ) / 10;
  69. else if (usec > 1000)
  70. end = ((usec/100) * CFG_HZ) / 10000;
  71. else
  72. end = (usec * CFG_HZ) / 1000000;
  73. while (get_timer (0) < end)
  74. continue;
  75. }
  76. unsigned long get_tbclk (void)
  77. {
  78. return CFG_HZ;
  79. }