rk_timer.c 1008 B

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