timer.c 2.3 KB

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