timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_INTC_0
  40. #ifdef CONFIG_SYS_TIMER_0
  41. void __udelay(unsigned long usec)
  42. {
  43. int i;
  44. i = get_timer(0);
  45. while ((get_timer(0) - i) < (usec / 1000))
  46. ;
  47. }
  48. #else
  49. void __udelay(unsigned long usec)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < (usec * CONFIG_XILINX_CLOCK_FREQ / 10000000); i++)
  53. ;
  54. }
  55. #endif
  56. #ifdef CONFIG_SYS_TIMER_0
  57. microblaze_timer_t *tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
  58. void timer_isr (void *arg)
  59. {
  60. timestamp++;
  61. tmr->control = tmr->control | TIMER_INTERRUPT;
  62. }
  63. int timer_init (void)
  64. {
  65. tmr->loadreg = CONFIG_SYS_TIMER_0_PRELOAD;
  66. tmr->control = TIMER_INTERRUPT | TIMER_RESET;
  67. tmr->control =
  68. TIMER_ENABLE | TIMER_ENABLE_INTR | TIMER_RELOAD | TIMER_DOWN_COUNT;
  69. timestamp = 0;
  70. install_interrupt_handler (CONFIG_SYS_TIMER_0_IRQ, timer_isr, (void *)tmr);
  71. return 0;
  72. }
  73. #endif
  74. #endif
  75. /*
  76. * This function is derived from PowerPC code (read timebase as long long).
  77. * On Microblaze it just returns the timer value.
  78. */
  79. unsigned long long get_ticks(void)
  80. {
  81. return get_timer(0);
  82. }
  83. /*
  84. * This function is derived from PowerPC code (timebase clock frequency).
  85. * On Microblaze it returns the number of timer ticks per second.
  86. */
  87. ulong get_tbclk(void)
  88. {
  89. return CONFIG_SYS_HZ;
  90. }