timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <init.h>
  7. #include <time.h>
  8. #include <asm/global_data.h>
  9. #include <asm/io.h>
  10. #include <div64.h>
  11. #include <asm/arch/immap_ls102xa.h>
  12. #include <asm/arch/clock.h>
  13. #include <linux/delay.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /*
  16. * This function is intended for SHORT delays only.
  17. * It will overflow at around 10 seconds @ 400MHz,
  18. * or 20 seconds @ 200MHz.
  19. */
  20. unsigned long usec2ticks(unsigned long usec)
  21. {
  22. ulong ticks;
  23. if (usec < 1000)
  24. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  25. else
  26. ticks = ((usec / 10) * (get_tbclk() / 100000));
  27. return ticks;
  28. }
  29. static inline unsigned long long tick_to_time(unsigned long long tick)
  30. {
  31. unsigned long freq;
  32. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  33. tick *= CONFIG_SYS_HZ;
  34. do_div(tick, freq);
  35. return tick;
  36. }
  37. static inline unsigned long long us_to_tick(unsigned long long usec)
  38. {
  39. unsigned long freq;
  40. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
  41. usec = usec * freq + 999999;
  42. do_div(usec, 1000000);
  43. return usec;
  44. }
  45. int timer_init(void)
  46. {
  47. struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
  48. unsigned long ctrl, freq;
  49. unsigned long long val;
  50. /* Enable System Counter */
  51. writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr);
  52. freq = COUNTER_FREQUENCY;
  53. asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  54. /* Set PL1 Physical Timer Ctrl */
  55. ctrl = ARCH_TIMER_CTRL_ENABLE;
  56. asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
  57. /* Set PL1 Physical Comp Value */
  58. val = TIMER_COMP_VAL;
  59. asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val));
  60. gd->arch.tbl = 0;
  61. gd->arch.tbu = 0;
  62. return 0;
  63. }
  64. unsigned long long get_ticks(void)
  65. {
  66. unsigned long long now;
  67. asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
  68. gd->arch.tbl = (unsigned long)(now & 0xffffffff);
  69. gd->arch.tbu = (unsigned long)(now >> 32);
  70. return now;
  71. }
  72. unsigned long get_timer(ulong base)
  73. {
  74. return tick_to_time(get_ticks()) - 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. }