timer.c 3.9 KB

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