timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <div64.h>
  8. #include <asm/arch/immap_ls102xa.h>
  9. #include <asm/arch/clock.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. /*
  12. * This function is intended for SHORT delays only.
  13. * It will overflow at around 10 seconds @ 400MHz,
  14. * or 20 seconds @ 200MHz.
  15. */
  16. unsigned long usec2ticks(unsigned long usec)
  17. {
  18. ulong ticks;
  19. if (usec < 1000)
  20. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  21. else
  22. ticks = ((usec / 10) * (get_tbclk() / 100000));
  23. return ticks;
  24. }
  25. static inline unsigned long long tick_to_time(unsigned long long tick)
  26. {
  27. unsigned long freq;
  28. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  29. tick *= CONFIG_SYS_HZ;
  30. do_div(tick, freq);
  31. return tick;
  32. }
  33. static inline unsigned long long us_to_tick(unsigned long long usec)
  34. {
  35. unsigned long freq;
  36. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  37. usec = usec * freq + 999999;
  38. do_div(usec, 1000000);
  39. return usec;
  40. }
  41. int timer_init(void)
  42. {
  43. struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
  44. unsigned long ctrl, freq;
  45. unsigned long long val;
  46. /* Enable System Counter */
  47. writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr);
  48. freq = COUNTER_FREQUENCY;
  49. asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  50. /* Set PL1 Physical Timer Ctrl */
  51. ctrl = ARCH_TIMER_CTRL_ENABLE;
  52. asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
  53. /* Set PL1 Physical Comp Value */
  54. val = TIMER_COMP_VAL;
  55. asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val));
  56. gd->arch.tbl = 0;
  57. gd->arch.tbu = 0;
  58. return 0;
  59. }
  60. unsigned long long get_ticks(void)
  61. {
  62. unsigned long long now;
  63. asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
  64. gd->arch.tbl = (unsigned long)(now & 0xffffffff);
  65. gd->arch.tbu = (unsigned long)(now >> 32);
  66. return now;
  67. }
  68. unsigned long get_timer_masked(void)
  69. {
  70. return tick_to_time(get_ticks());
  71. }
  72. unsigned long get_timer(ulong base)
  73. {
  74. return get_timer_masked() - base;
  75. }
  76. /* delay x useconds and preserve advance timstamp value */
  77. void __udelay(unsigned long usec)
  78. {
  79. unsigned long long start;
  80. unsigned long tmo;
  81. start = get_ticks(); /* get current timestamp */
  82. tmo = us_to_tick(usec); /* convert usecs to ticks */
  83. while ((get_ticks() - start) < tmo)
  84. ; /* loop till time has passed */
  85. }
  86. /*
  87. * This function is derived from PowerPC code (timebase clock frequency).
  88. * On ARM it returns the number of timer ticks per second.
  89. */
  90. unsigned long get_tbclk(void)
  91. {
  92. unsigned long freq;
  93. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  94. return freq;
  95. }