timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. microblaze_timer_t *tmr;
  29. ulong get_timer (ulong base)
  30. {
  31. if (tmr)
  32. return timestamp - base;
  33. return timestamp++ - base;
  34. }
  35. void __udelay(unsigned long usec)
  36. {
  37. u32 i;
  38. if (tmr) {
  39. i = get_timer(0);
  40. while ((get_timer(0) - i) < (usec / 1000))
  41. ;
  42. } else {
  43. for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++)
  44. ;
  45. }
  46. }
  47. static void timer_isr(void *arg)
  48. {
  49. timestamp++;
  50. tmr->control = tmr->control | TIMER_INTERRUPT;
  51. }
  52. int timer_init (void)
  53. {
  54. int irq = -1;
  55. u32 preload = 0;
  56. u32 ret = 0;
  57. #if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM)
  58. preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ;
  59. irq = CONFIG_SYS_TIMER_0_IRQ;
  60. tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
  61. #endif
  62. if (tmr && preload && irq >= 0) {
  63. tmr->loadreg = preload;
  64. tmr->control = TIMER_INTERRUPT | TIMER_RESET;
  65. tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
  66. TIMER_RELOAD | TIMER_DOWN_COUNT;
  67. timestamp = 0;
  68. ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
  69. if (ret)
  70. tmr = NULL;
  71. }
  72. /* No problem if timer is not found/initialized */
  73. return 0;
  74. }
  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. }