rk_timer.c 1006 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * (C) Copyright 2015 Rockchip Electronics Co., Ltd
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <asm/arch/timer.h>
  7. #include <asm/io.h>
  8. #include <common.h>
  9. #include <linux/types.h>
  10. struct rk_timer * const timer_ptr = (void *)CONFIG_SYS_TIMER_BASE;
  11. static uint64_t rockchip_get_ticks(void)
  12. {
  13. uint64_t timebase_h, timebase_l;
  14. timebase_l = readl(&timer_ptr->timer_curr_value0);
  15. timebase_h = readl(&timer_ptr->timer_curr_value1);
  16. return timebase_h << 32 | timebase_l;
  17. }
  18. static uint64_t usec_to_tick(unsigned int usec)
  19. {
  20. uint64_t tick = usec;
  21. tick *= CONFIG_SYS_TIMER_RATE / (1000 * 1000);
  22. return tick;
  23. }
  24. void rockchip_udelay(unsigned int usec)
  25. {
  26. uint64_t tmp;
  27. /* get timestamp */
  28. tmp = rockchip_get_ticks() + usec_to_tick(usec);
  29. /* loop till event */
  30. while (rockchip_get_ticks() < tmp+1)
  31. ;
  32. }
  33. void rockchip_timer_init(void)
  34. {
  35. writel(0xffffffff, &timer_ptr->timer_load_count0);
  36. writel(0xffffffff, &timer_ptr->timer_load_count1);
  37. writel(1, &timer_ptr->timer_ctrl_reg);
  38. }