timer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007 Michal Simek
  4. *
  5. * Michal SIMEK <monstr@monstr.eu>
  6. */
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <asm/microblaze_timer.h>
  10. #include <asm/microblaze_intc.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. volatile int timestamp = 0;
  13. microblaze_timer_t *tmr;
  14. ulong get_timer (ulong base)
  15. {
  16. if (tmr)
  17. return timestamp - base;
  18. return timestamp++ - base;
  19. }
  20. void __udelay(unsigned long usec)
  21. {
  22. u32 i;
  23. if (tmr) {
  24. i = get_timer(0);
  25. while ((get_timer(0) - i) < (usec / 1000))
  26. ;
  27. }
  28. }
  29. #ifndef CONFIG_SPL_BUILD
  30. static void timer_isr(void *arg)
  31. {
  32. timestamp++;
  33. tmr->control = tmr->control | TIMER_INTERRUPT;
  34. }
  35. int timer_init (void)
  36. {
  37. int irq = -1;
  38. u32 preload = 0;
  39. u32 ret = 0;
  40. const void *blob = gd->fdt_blob;
  41. int node = 0;
  42. u32 cell[2];
  43. debug("TIMER: Initialization\n");
  44. node = fdt_node_offset_by_compatible(blob, node,
  45. "xlnx,xps-timer-1.00.a");
  46. if (node != -1) {
  47. fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
  48. if (base == FDT_ADDR_T_NONE)
  49. return -1;
  50. debug("TIMER: Base addr %lx\n", base);
  51. tmr = (microblaze_timer_t *)base;
  52. ret = fdtdec_get_int_array(blob, node, "interrupts",
  53. cell, ARRAY_SIZE(cell));
  54. if (ret)
  55. return ret;
  56. irq = cell[0];
  57. debug("TIMER: IRQ %x\n", irq);
  58. preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
  59. preload /= CONFIG_SYS_HZ;
  60. } else {
  61. return node;
  62. }
  63. if (tmr && preload && irq >= 0) {
  64. tmr->loadreg = preload;
  65. tmr->control = TIMER_INTERRUPT | TIMER_RESET;
  66. tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
  67. TIMER_RELOAD | TIMER_DOWN_COUNT;
  68. timestamp = 0;
  69. ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
  70. if (ret)
  71. tmr = NULL;
  72. }
  73. /* No problem if timer is not found/initialized */
  74. return 0;
  75. }
  76. #else
  77. int timer_init(void)
  78. {
  79. return 0;
  80. }
  81. #endif
  82. /*
  83. * This function is derived from PowerPC code (read timebase as long long).
  84. * On Microblaze it just returns the timer value.
  85. */
  86. unsigned long long get_ticks(void)
  87. {
  88. return get_timer(0);
  89. }
  90. /*
  91. * This function is derived from PowerPC code (timebase clock frequency).
  92. * On Microblaze it returns the number of timer ticks per second.
  93. */
  94. ulong get_tbclk(void)
  95. {
  96. return CONFIG_SYS_HZ;
  97. }