time.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * (C) Copyright 2007
  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. #define TMU_MAX_COUNTER (~0UL)
  29. static void tmu_timer_start (unsigned int timer)
  30. {
  31. if (timer > 2)
  32. return;
  33. *((volatile unsigned char *) TSTR) |= (1 << timer);
  34. }
  35. static void tmu_timer_stop (unsigned int timer)
  36. {
  37. u8 val = *((volatile u8 *)TSTR);
  38. if (timer > 2)
  39. return;
  40. *((volatile unsigned char *)TSTR) = val &~(1 << timer);
  41. }
  42. int timer_init (void)
  43. {
  44. /* Divide clock by 4 */
  45. *(volatile u16 *)TCR0 = 0;
  46. tmu_timer_stop(0);
  47. tmu_timer_start(0);
  48. return 0;
  49. }
  50. /*
  51. In theory we should return a true 64bit value (ie something that doesn't
  52. overflow). However, we don't. Therefore if TMU runs at fastest rate of
  53. 6.75 MHz this value will wrap after u-boot has been running for approx
  54. 10 minutes.
  55. */
  56. unsigned long long get_ticks (void)
  57. {
  58. return (0 - *((volatile u32 *) TCNT0));
  59. }
  60. unsigned long get_timer (unsigned long base)
  61. {
  62. return ((0 - *((volatile u32 *) TCNT0)) - base);
  63. }
  64. void set_timer (unsigned long t)
  65. {
  66. *((volatile unsigned int *) TCNT0) = (0 - t);
  67. }
  68. void reset_timer (void)
  69. {
  70. tmu_timer_stop(0);
  71. set_timer (0);
  72. tmu_timer_start(0);
  73. }
  74. void udelay (unsigned long usec)
  75. {
  76. unsigned int start = get_timer (0);
  77. unsigned int end = start + (usec * ((CFG_HZ + 500000) / 1000000));
  78. while (get_timer (0) < end)
  79. continue;
  80. }
  81. unsigned long get_tbclk (void)
  82. {
  83. return CFG_HZ;
  84. }