timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * (C) Copyright 2007 Michal Simek
  3. *
  4. * Michal SIMEK <monstr@monstr.eu>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/microblaze_timer.h>
  26. #include <asm/microblaze_intc.h>
  27. volatile int timestamp = 0;
  28. #ifdef CONFIG_SYS_TIMER_0
  29. ulong get_timer (ulong base)
  30. {
  31. return (timestamp - base);
  32. }
  33. #else
  34. ulong get_timer (ulong base)
  35. {
  36. return (timestamp++ - base);
  37. }
  38. #endif
  39. #ifdef CONFIG_SYS_TIMER_0
  40. void __udelay(unsigned long usec)
  41. {
  42. int i;
  43. i = get_timer(0);
  44. while ((get_timer(0) - i) < (usec / 1000))
  45. ;
  46. }
  47. #else
  48. void __udelay(unsigned long usec)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < (usec * CONFIG_XILINX_CLOCK_FREQ / 10000000); i++)
  52. ;
  53. }
  54. #endif
  55. #ifdef CONFIG_SYS_TIMER_0
  56. microblaze_timer_t *tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
  57. void timer_isr (void *arg)
  58. {
  59. timestamp++;
  60. tmr->control = tmr->control | TIMER_INTERRUPT;
  61. }
  62. int timer_init (void)
  63. {
  64. tmr->loadreg = CONFIG_SYS_TIMER_0_PRELOAD;
  65. tmr->control = TIMER_INTERRUPT | TIMER_RESET;
  66. tmr->control =
  67. TIMER_ENABLE | TIMER_ENABLE_INTR | TIMER_RELOAD | TIMER_DOWN_COUNT;
  68. timestamp = 0;
  69. install_interrupt_handler (CONFIG_SYS_TIMER_0_IRQ, timer_isr, (void *)tmr);
  70. return 0;
  71. }
  72. #endif
  73. /*
  74. * This function is derived from PowerPC code (read timebase as long long).
  75. * On Microblaze it just returns the timer value.
  76. */
  77. unsigned long long get_ticks(void)
  78. {
  79. return get_timer(0);
  80. }
  81. /*
  82. * This function is derived from PowerPC code (timebase clock frequency).
  83. * On Microblaze it returns the number of timer ticks per second.
  84. */
  85. ulong get_tbclk(void)
  86. {
  87. return CONFIG_SYS_HZ;
  88. }