syscounter.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Freescale Semiconductor, Inc.
  4. *
  5. * The file use ls102xa/timer.c as a reference.
  6. */
  7. #include <common.h>
  8. #include <init.h>
  9. #include <time.h>
  10. #include <asm/global_data.h>
  11. #include <asm/io.h>
  12. #include <div64.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <asm/mach-imx/syscounter.h>
  16. #include <linux/delay.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /*
  19. * This function is intended for SHORT delays only.
  20. * It will overflow at around 10 seconds @ 400MHz,
  21. * or 20 seconds @ 200MHz.
  22. */
  23. unsigned long usec2ticks(unsigned long usec)
  24. {
  25. ulong ticks;
  26. if (usec < 1000)
  27. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  28. else
  29. ticks = ((usec / 10) * (get_tbclk() / 100000));
  30. return ticks;
  31. }
  32. static inline unsigned long long tick_to_time(unsigned long long tick)
  33. {
  34. unsigned long freq;
  35. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  36. tick *= CONFIG_SYS_HZ;
  37. do_div(tick, freq);
  38. return tick;
  39. }
  40. static inline unsigned long long us_to_tick(unsigned long long usec)
  41. {
  42. unsigned long freq;
  43. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  44. usec = usec * freq + 999999;
  45. do_div(usec, 1000000);
  46. return usec;
  47. }
  48. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  49. int timer_init(void)
  50. {
  51. struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
  52. unsigned long val, freq;
  53. freq = CONFIG_SC_TIMER_CLK;
  54. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  55. writel(freq, &sctr->cntfid0);
  56. /* Enable system counter */
  57. val = readl(&sctr->cntcr);
  58. val &= ~(SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1);
  59. val |= SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG;
  60. writel(val, &sctr->cntcr);
  61. gd->arch.tbl = 0;
  62. gd->arch.tbu = 0;
  63. return 0;
  64. }
  65. #endif
  66. unsigned long long get_ticks(void)
  67. {
  68. unsigned long long now;
  69. asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
  70. gd->arch.tbl = (unsigned long)(now & 0xffffffff);
  71. gd->arch.tbu = (unsigned long)(now >> 32);
  72. return now;
  73. }
  74. ulong get_timer(ulong base)
  75. {
  76. return tick_to_time(get_ticks()) - base;
  77. }
  78. void __udelay(unsigned long usec)
  79. {
  80. unsigned long long tmp;
  81. ulong tmo;
  82. tmo = us_to_tick(usec);
  83. tmp = get_ticks() + tmo; /* get current timestamp */
  84. while (get_ticks() < tmp) /* loop till event */
  85. /*NOP*/;
  86. }
  87. /*
  88. * This function is derived from PowerPC code (timebase clock frequency).
  89. * On ARM it returns the number of timer ticks per second.
  90. */
  91. ulong get_tbclk(void)
  92. {
  93. unsigned long freq;
  94. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  95. return freq;
  96. }