timer.c 801 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <linux/io.h>
  8. #include "arm-mpcore.h"
  9. #define PERIPHCLK (50 * 1000 * 1000) /* 50 MHz */
  10. #define PRESCALER ((PERIPHCLK) / (CONFIG_SYS_TIMER_RATE) - 1)
  11. static void *get_global_timer_base(void)
  12. {
  13. void *val;
  14. asm("mrc p15, 4, %0, c15, c0, 0" : "=r" (val) : : "memory");
  15. return val + GLOBAL_TIMER_OFFSET;
  16. }
  17. unsigned long timer_read_counter(void)
  18. {
  19. /*
  20. * ARM 64bit Global Timer is too much for our purpose.
  21. * We use only lower 32 bit of the timer counter.
  22. */
  23. return readl(get_global_timer_base() + GTIMER_CNT_L);
  24. }
  25. int timer_init(void)
  26. {
  27. /* enable timer */
  28. writel(PRESCALER << 8 | 1, get_global_timer_base() + GTIMER_CTRL);
  29. return 0;
  30. }