timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  4. * (C) Copyright 2012 Renesas Solutions Corp.
  5. */
  6. #include <common.h>
  7. #include <div64.h>
  8. #include <init.h>
  9. #include <time.h>
  10. #include <asm/io.h>
  11. #include <asm/arch-armv7/globaltimer.h>
  12. #include <asm/arch/rmobile.h>
  13. #include <linux/delay.h>
  14. static struct globaltimer *global_timer = \
  15. (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
  16. #define CLK2MHZ(clk) (clk / 1000 / 1000)
  17. static u64 get_cpu_global_timer(void)
  18. {
  19. u32 low, high;
  20. u64 timer;
  21. u32 old = readl(&global_timer->cnt_h);
  22. while (1) {
  23. low = readl(&global_timer->cnt_l);
  24. high = readl(&global_timer->cnt_h);
  25. if (old == high)
  26. break;
  27. else
  28. old = high;
  29. }
  30. timer = high;
  31. return (u64)((timer << 32) | low);
  32. }
  33. static u64 get_time_us(void)
  34. {
  35. u64 timer = get_cpu_global_timer();
  36. timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
  37. do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
  38. return timer;
  39. }
  40. static ulong get_time_ms(void)
  41. {
  42. u64 us = get_time_us();
  43. do_div(us, 1000);
  44. return us;
  45. }
  46. int timer_init(void)
  47. {
  48. writel(0x01, &global_timer->ctl);
  49. return 0;
  50. }
  51. void __udelay(unsigned long usec)
  52. {
  53. u64 start, current;
  54. u64 wait;
  55. start = get_cpu_global_timer();
  56. wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
  57. do {
  58. current = get_cpu_global_timer();
  59. } while ((current - start) < wait);
  60. }
  61. ulong get_timer(ulong base)
  62. {
  63. return get_time_ms() - base;
  64. }
  65. unsigned long long get_ticks(void)
  66. {
  67. return get_cpu_global_timer();
  68. }
  69. ulong get_tbclk(void)
  70. {
  71. return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
  72. }