timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007-2011
  4. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  5. * Tom Cubie <tangliang@allwinnertech.com>
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/timer.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. #define TIMER_MODE (0x0 << 7) /* continuous mode */
  12. #define TIMER_DIV (0x0 << 4) /* pre scale 1 */
  13. #define TIMER_SRC (0x1 << 2) /* osc24m */
  14. #define TIMER_RELOAD (0x1 << 1) /* reload internal value */
  15. #define TIMER_EN (0x1 << 0) /* enable timer */
  16. #define TIMER_CLOCK (24 * 1000 * 1000)
  17. #define COUNT_TO_USEC(x) ((x) / 24)
  18. #define USEC_TO_COUNT(x) ((x) * 24)
  19. #define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ)
  20. #define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ)
  21. #define TIMER_LOAD_VAL 0xffffffff
  22. #define TIMER_NUM 0 /* we use timer 0 */
  23. /* read the 32-bit timer */
  24. static ulong read_timer(void)
  25. {
  26. struct sunxi_timer_reg *timers =
  27. (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
  28. struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
  29. /*
  30. * The hardware timer counts down, therefore we invert to
  31. * produce an incrementing timer.
  32. */
  33. return ~readl(&timer->val);
  34. }
  35. /* init timer register */
  36. int timer_init(void)
  37. {
  38. struct sunxi_timer_reg *timers =
  39. (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
  40. struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
  41. writel(TIMER_LOAD_VAL, &timer->inter);
  42. writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN,
  43. &timer->ctl);
  44. return 0;
  45. }
  46. /* timer without interrupts */
  47. ulong get_timer(ulong base)
  48. {
  49. return get_timer_masked() - base;
  50. }
  51. ulong get_timer_masked(void)
  52. {
  53. /* current tick value */
  54. ulong now = TICKS_TO_HZ(read_timer());
  55. if (now >= gd->arch.lastinc) /* normal (non rollover) */
  56. gd->arch.tbl += (now - gd->arch.lastinc);
  57. else {
  58. /* rollover */
  59. gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL)
  60. - gd->arch.lastinc) + now;
  61. }
  62. gd->arch.lastinc = now;
  63. return gd->arch.tbl;
  64. }
  65. /* delay x useconds */
  66. void __udelay(unsigned long usec)
  67. {
  68. long tmo = USEC_TO_COUNT(usec);
  69. ulong now, last = read_timer();
  70. while (tmo > 0) {
  71. now = read_timer();
  72. if (now > last) /* normal (non rollover) */
  73. tmo -= now - last;
  74. else /* rollover */
  75. tmo -= TIMER_LOAD_VAL - last + now;
  76. last = now;
  77. }
  78. }
  79. /*
  80. * This function is derived from PowerPC code (read timebase as long long).
  81. * On ARM it just returns the timer value.
  82. */
  83. unsigned long long get_ticks(void)
  84. {
  85. return get_timer(0);
  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. return CONFIG_SYS_HZ;
  94. }