timer.c 4.0 KB

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