timer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * (C) Copyright 2007 Michal Simek
  3. *
  4. * Michal SIMEK <monstr@monstr.eu>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/microblaze_timer.h>
  10. #include <asm/microblaze_intc.h>
  11. volatile int timestamp = 0;
  12. microblaze_timer_t *tmr;
  13. ulong get_timer (ulong base)
  14. {
  15. if (tmr)
  16. return timestamp - base;
  17. return timestamp++ - base;
  18. }
  19. void __udelay(unsigned long usec)
  20. {
  21. u32 i;
  22. if (tmr) {
  23. i = get_timer(0);
  24. while ((get_timer(0) - i) < (usec / 1000))
  25. ;
  26. } else {
  27. for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++)
  28. ;
  29. }
  30. }
  31. #ifndef CONFIG_SPL_BUILD
  32. static void timer_isr(void *arg)
  33. {
  34. timestamp++;
  35. tmr->control = tmr->control | TIMER_INTERRUPT;
  36. }
  37. int timer_init (void)
  38. {
  39. int irq = -1;
  40. u32 preload = 0;
  41. u32 ret = 0;
  42. #if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM)
  43. preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ;
  44. irq = CONFIG_SYS_TIMER_0_IRQ;
  45. tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
  46. #endif
  47. if (tmr && preload && irq >= 0) {
  48. tmr->loadreg = preload;
  49. tmr->control = TIMER_INTERRUPT | TIMER_RESET;
  50. tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
  51. TIMER_RELOAD | TIMER_DOWN_COUNT;
  52. timestamp = 0;
  53. ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
  54. if (ret)
  55. tmr = NULL;
  56. }
  57. /* No problem if timer is not found/initialized */
  58. return 0;
  59. }
  60. #else
  61. int timer_init(void)
  62. {
  63. return 0;
  64. }
  65. #endif
  66. /*
  67. * This function is derived from PowerPC code (read timebase as long long).
  68. * On Microblaze it just returns the timer value.
  69. */
  70. unsigned long long get_ticks(void)
  71. {
  72. return get_timer(0);
  73. }
  74. /*
  75. * This function is derived from PowerPC code (timebase clock frequency).
  76. * On Microblaze it returns the number of timer ticks per second.
  77. */
  78. ulong get_tbclk(void)
  79. {
  80. return CONFIG_SYS_HZ;
  81. }