time.c 2.2 KB

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