timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * (C) Copyright 2009 Faraday Technology
  3. * Po-Yu Chuang <ratbert@faraday-tech.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <div64.h>
  9. #include <asm/io.h>
  10. #include <faraday/ftpmu010.h>
  11. #include <faraday/fttmr010.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define TIMER_CLOCK 32768
  14. #define TIMER_LOAD_VAL 0xffffffff
  15. static inline unsigned long long tick_to_time(unsigned long long tick)
  16. {
  17. tick *= CONFIG_SYS_HZ;
  18. do_div(tick, gd->arch.timer_rate_hz);
  19. return tick;
  20. }
  21. static inline unsigned long long usec_to_tick(unsigned long long usec)
  22. {
  23. usec *= gd->arch.timer_rate_hz;
  24. do_div(usec, 1000000);
  25. return usec;
  26. }
  27. int timer_init(void)
  28. {
  29. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  30. unsigned int cr;
  31. debug("%s()\n", __func__);
  32. /* disable timers */
  33. writel(0, &tmr->cr);
  34. /* use 32768Hz oscillator for RTC, WDT, TIMER */
  35. ftpmu010_32768osc_enable();
  36. /* setup timer */
  37. writel(TIMER_LOAD_VAL, &tmr->timer3_load);
  38. writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
  39. writel(0, &tmr->timer3_match1);
  40. writel(0, &tmr->timer3_match2);
  41. /* we don't want timer to issue interrupts */
  42. writel(FTTMR010_TM3_MATCH1 |
  43. FTTMR010_TM3_MATCH2 |
  44. FTTMR010_TM3_OVERFLOW,
  45. &tmr->interrupt_mask);
  46. cr = readl(&tmr->cr);
  47. cr |= FTTMR010_TM3_CLOCK; /* use external clock */
  48. cr |= FTTMR010_TM3_ENABLE;
  49. writel(cr, &tmr->cr);
  50. gd->arch.timer_rate_hz = TIMER_CLOCK;
  51. gd->arch.tbu = gd->arch.tbl = 0;
  52. return 0;
  53. }
  54. /*
  55. * Get the current 64 bit timer tick count
  56. */
  57. unsigned long long get_ticks(void)
  58. {
  59. struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
  60. ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
  61. /* increment tbu if tbl has rolled over */
  62. if (now < gd->arch.tbl)
  63. gd->arch.tbu++;
  64. gd->arch.tbl = now;
  65. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  66. }
  67. void __udelay(unsigned long usec)
  68. {
  69. unsigned long long start;
  70. ulong tmo;
  71. start = get_ticks(); /* get current timestamp */
  72. tmo = usec_to_tick(usec); /* convert usecs to ticks */
  73. while ((get_ticks() - start) < tmo)
  74. ; /* loop till time has passed */
  75. }
  76. /*
  77. * get_timer(base) can be used to check for timeouts or
  78. * to measure elasped time relative to an event:
  79. *
  80. * ulong start_time = get_timer(0) sets start_time to the current
  81. * time value.
  82. * get_timer(start_time) returns the time elapsed since then.
  83. *
  84. * The time is used in CONFIG_SYS_HZ units!
  85. */
  86. ulong get_timer(ulong base)
  87. {
  88. return tick_to_time(get_ticks()) - base;
  89. }
  90. /*
  91. * Return the number of timer ticks per second.
  92. */
  93. ulong get_tbclk(void)
  94. {
  95. return gd->arch.timer_rate_hz;
  96. }