timer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. static struct globaltimer *global_timer = \
  14. (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
  15. #define CLK2MHZ(clk) (clk / 1000 / 1000)
  16. static u64 get_cpu_global_timer(void)
  17. {
  18. u32 low, high;
  19. u64 timer;
  20. u32 old = readl(&global_timer->cnt_h);
  21. while (1) {
  22. low = readl(&global_timer->cnt_l);
  23. high = readl(&global_timer->cnt_h);
  24. if (old == high)
  25. break;
  26. else
  27. old = high;
  28. }
  29. timer = high;
  30. return (u64)((timer << 32) | low);
  31. }
  32. static u64 get_time_us(void)
  33. {
  34. u64 timer = get_cpu_global_timer();
  35. timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
  36. do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
  37. return timer;
  38. }
  39. static ulong get_time_ms(void)
  40. {
  41. u64 us = get_time_us();
  42. do_div(us, 1000);
  43. return us;
  44. }
  45. int timer_init(void)
  46. {
  47. writel(0x01, &global_timer->ctl);
  48. return 0;
  49. }
  50. void __udelay(unsigned long usec)
  51. {
  52. u64 start, current;
  53. u64 wait;
  54. start = get_cpu_global_timer();
  55. wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
  56. do {
  57. current = get_cpu_global_timer();
  58. } while ((current - start) < wait);
  59. }
  60. ulong get_timer(ulong base)
  61. {
  62. return get_time_ms() - base;
  63. }
  64. unsigned long long get_ticks(void)
  65. {
  66. return get_cpu_global_timer();
  67. }
  68. ulong get_tbclk(void)
  69. {
  70. return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
  71. }