timer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Alex Zuepke <azu@sysgo.de>
  9. *
  10. * (C) Copyright 2002
  11. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  12. *
  13. * (C) Copyright 2009
  14. * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
  15. *
  16. * (C) Copyright 2009 DENX Software Engineering
  17. * Author: John Rigby <jrigby@gmail.com>
  18. * Add support for MX25
  19. *
  20. * See file CREDITS for list of people who contributed to this
  21. * project.
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License as
  25. * published by the Free Software Foundation; either version 2 of
  26. * the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  36. * MA 02111-1307 USA
  37. */
  38. #include <common.h>
  39. #include <div64.h>
  40. #include <asm/io.h>
  41. #include <asm/arch/imx-regs.h>
  42. static ulong timestamp;
  43. static ulong lastinc;
  44. /*
  45. * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
  46. * "tick" is internal timer period
  47. */
  48. #ifdef CONFIG_MX25_TIMER_HIGH_PRECISION
  49. /* ~0.4% error - measured with stop-watch on 100s boot-delay */
  50. static inline unsigned long long tick_to_time(unsigned long long tick)
  51. {
  52. tick *= CONFIG_SYS_HZ;
  53. do_div(tick, CONFIG_MX25_CLK32);
  54. return tick;
  55. }
  56. static inline unsigned long long time_to_tick(unsigned long long time)
  57. {
  58. time *= CONFIG_MX25_CLK32;
  59. do_div(time, CONFIG_SYS_HZ);
  60. return time;
  61. }
  62. static inline unsigned long long us_to_tick(unsigned long long us)
  63. {
  64. us = us * CONFIG_MX25_CLK32 + 999999;
  65. do_div(us, 1000000);
  66. return us;
  67. }
  68. #else
  69. /* ~2% error */
  70. #define TICK_PER_TIME ((CONFIG_MX25_CLK32 + CONFIG_SYS_HZ / 2) / \
  71. CONFIG_SYS_HZ)
  72. #define US_PER_TICK (1000000 / CONFIG_MX25_CLK32)
  73. static inline unsigned long long tick_to_time(unsigned long long tick)
  74. {
  75. do_div(tick, TICK_PER_TIME);
  76. return tick;
  77. }
  78. static inline unsigned long long time_to_tick(unsigned long long time)
  79. {
  80. return time * TICK_PER_TIME;
  81. }
  82. static inline unsigned long long us_to_tick(unsigned long long us)
  83. {
  84. us += US_PER_TICK - 1;
  85. do_div(us, US_PER_TICK);
  86. return us;
  87. }
  88. #endif
  89. /* nothing really to do with interrupts, just starts up a counter. */
  90. /* The 32KHz 32-bit timer overruns in 134217 seconds */
  91. int timer_init(void)
  92. {
  93. int i;
  94. struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
  95. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  96. /* setup GP Timer 1 */
  97. writel(GPT_CTRL_SWR, &gpt->ctrl);
  98. writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1);
  99. for (i = 0; i < 100; i++)
  100. writel(0, &gpt->ctrl); /* We have no udelay by now */
  101. writel(0, &gpt->pre); /* prescaler = 1 */
  102. /* Freerun Mode, 32KHz input */
  103. writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR,
  104. &gpt->ctrl);
  105. writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl);
  106. return 0;
  107. }
  108. void reset_timer_masked(void)
  109. {
  110. struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
  111. /* reset time */
  112. /* capture current incrementer value time */
  113. lastinc = readl(&gpt->counter);
  114. timestamp = 0; /* start "advancing" time stamp from 0 */
  115. }
  116. void reset_timer(void)
  117. {
  118. reset_timer_masked();
  119. }
  120. unsigned long long get_ticks (void)
  121. {
  122. struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
  123. ulong now = readl(&gpt->counter); /* current tick value */
  124. if (now >= lastinc) {
  125. /*
  126. * normal mode (non roll)
  127. * move stamp forward with absolut diff ticks
  128. */
  129. timestamp += (now - lastinc);
  130. } else {
  131. /* we have rollover of incrementer */
  132. timestamp += (0xFFFFFFFF - lastinc) + now;
  133. }
  134. lastinc = now;
  135. return timestamp;
  136. }
  137. ulong get_timer_masked (void)
  138. {
  139. /*
  140. * get_ticks() returns a long long (64 bit), it wraps in
  141. * 2^64 / CONFIG_MX25_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  142. * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
  143. * 5 * 10^6 days - long enough.
  144. */
  145. return tick_to_time(get_ticks());
  146. }
  147. ulong get_timer (ulong base)
  148. {
  149. return get_timer_masked () - base;
  150. }
  151. void set_timer (ulong t)
  152. {
  153. timestamp = time_to_tick(t);
  154. }
  155. /* delay x useconds AND preserve advance timstamp value */
  156. void __udelay (unsigned long usec)
  157. {
  158. unsigned long long tmp;
  159. ulong tmo;
  160. tmo = us_to_tick(usec);
  161. tmp = get_ticks() + tmo; /* get current timestamp */
  162. while (get_ticks() < tmp) /* loop till event */
  163. /*NOP*/;
  164. }