timer.c 3.9 KB

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