timer.c 798 B

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