timer.c 2.5 KB

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