timer.c 1.5 KB

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