timer.c 3.8 KB

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