timer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. * Marius Groeger <mgroeger@sysgo.de>
  6. *
  7. * (C) Copyright 2002
  8. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  9. * Alex Zuepke <azu@sysgo.de>
  10. *
  11. * (C) Copyright 2002
  12. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  13. *
  14. * (C) Copyright 2009
  15. * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
  16. */
  17. #include <common.h>
  18. #include <div64.h>
  19. #include <init.h>
  20. #include <time.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/imx-regs.h>
  23. /* General purpose timers bitfields */
  24. #define GPTCR_SWR (1 << 15) /* Software reset */
  25. #define GPTCR_FRR (1 << 8) /* Freerun / restart */
  26. #define GPTCR_CLKSOURCE_32 (4 << 1) /* Clock source */
  27. #define GPTCR_TEN 1 /* Timer enable */
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define timestamp (gd->arch.tbl)
  30. #define lastinc (gd->arch.lastinc)
  31. /*
  32. * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
  33. * "tick" is internal timer period
  34. */
  35. #ifdef CONFIG_MX27_TIMER_HIGH_PRECISION
  36. /* ~0.4% error - measured with stop-watch on 100s boot-delay */
  37. static inline unsigned long long tick_to_time(unsigned long long tick)
  38. {
  39. tick *= CONFIG_SYS_HZ;
  40. do_div(tick, CONFIG_MX27_CLK32);
  41. return tick;
  42. }
  43. static inline unsigned long long time_to_tick(unsigned long long time)
  44. {
  45. time *= CONFIG_MX27_CLK32;
  46. do_div(time, CONFIG_SYS_HZ);
  47. return time;
  48. }
  49. static inline unsigned long long us_to_tick(unsigned long long us)
  50. {
  51. us = us * CONFIG_MX27_CLK32 + 999999;
  52. do_div(us, 1000000);
  53. return us;
  54. }
  55. #else
  56. /* ~2% error */
  57. #define TICK_PER_TIME ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
  58. CONFIG_SYS_HZ)
  59. #define US_PER_TICK (1000000 / CONFIG_MX27_CLK32)
  60. static inline unsigned long long tick_to_time(unsigned long long tick)
  61. {
  62. do_div(tick, TICK_PER_TIME);
  63. return tick;
  64. }
  65. static inline unsigned long long time_to_tick(unsigned long long time)
  66. {
  67. return time * TICK_PER_TIME;
  68. }
  69. static inline unsigned long long us_to_tick(unsigned long long us)
  70. {
  71. us += US_PER_TICK - 1;
  72. do_div(us, US_PER_TICK);
  73. return us;
  74. }
  75. #endif
  76. /* nothing really to do with interrupts, just starts up a counter. */
  77. /* The 32768Hz 32-bit timer overruns in 131072 seconds */
  78. int timer_init(void)
  79. {
  80. int i;
  81. struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
  82. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  83. /* setup GP Timer 1 */
  84. writel(GPTCR_SWR, &regs->gpt_tctl);
  85. writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
  86. writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
  87. for (i = 0; i < 100; i++)
  88. writel(0, &regs->gpt_tctl); /* We have no udelay by now */
  89. writel(0, &regs->gpt_tprer); /* 32Khz */
  90. /* Freerun Mode, PERCLK1 input */
  91. writel(readl(&regs->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
  92. &regs->gpt_tctl);
  93. writel(readl(&regs->gpt_tctl) | GPTCR_TEN, &regs->gpt_tctl);
  94. return 0;
  95. }
  96. unsigned long long get_ticks(void)
  97. {
  98. struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
  99. ulong now = readl(&regs->gpt_tcn); /* current tick value */
  100. if (now >= lastinc) {
  101. /*
  102. * normal mode (non roll)
  103. * move stamp forward with absolut diff ticks
  104. */
  105. timestamp += (now - lastinc);
  106. } else {
  107. /* we have rollover of incrementer */
  108. timestamp += (0xFFFFFFFF - lastinc) + now;
  109. }
  110. lastinc = now;
  111. return timestamp;
  112. }
  113. static ulong get_timer_masked(void)
  114. {
  115. /*
  116. * get_ticks() returns a long long (64 bit), it wraps in
  117. * 2^64 / CONFIG_MX27_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  118. * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
  119. * 5 * 10^6 days - long enough.
  120. */
  121. return tick_to_time(get_ticks());
  122. }
  123. ulong get_timer(ulong base)
  124. {
  125. return get_timer_masked() - base;
  126. }
  127. /* delay x useconds AND preserve advance timstamp value */
  128. void __udelay(unsigned long usec)
  129. {
  130. unsigned long long tmp;
  131. ulong tmo;
  132. tmo = us_to_tick(usec);
  133. tmp = get_ticks() + tmo; /* get current timestamp */
  134. while (get_ticks() < tmp) /* loop till event */
  135. /*NOP*/;
  136. }
  137. ulong get_tbclk(void)
  138. {
  139. return CONFIG_MX27_CLK32;
  140. }