timer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/arch/imx-regs.h>
  25. #include <div64.h>
  26. #include <watchdog.h>
  27. #include <asm/io.h>
  28. #define TIMER_BASE 0x53f90000 /* General purpose timer 1 */
  29. /* General purpose timers registers */
  30. #define GPTCR __REG(TIMER_BASE) /* Control register */
  31. #define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */
  32. #define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */
  33. #define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */
  34. /* General purpose timers bitfields */
  35. #define GPTCR_SWR (1 << 15) /* Software reset */
  36. #define GPTCR_FRR (1 << 9) /* Freerun / restart */
  37. #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
  38. #define GPTCR_TEN 1 /* Timer enable */
  39. DECLARE_GLOBAL_DATA_PTR;
  40. /* "time" is measured in 1 / CONFIG_SYS_HZ seconds, "tick" is internal timer period */
  41. #ifdef CONFIG_MX31_TIMER_HIGH_PRECISION
  42. /* ~0.4% error - measured with stop-watch on 100s boot-delay */
  43. static inline unsigned long long tick_to_time(unsigned long long tick)
  44. {
  45. tick *= CONFIG_SYS_HZ;
  46. do_div(tick, CONFIG_MX31_CLK32);
  47. return tick;
  48. }
  49. static inline unsigned long long time_to_tick(unsigned long long time)
  50. {
  51. time *= CONFIG_MX31_CLK32;
  52. do_div(time, CONFIG_SYS_HZ);
  53. return time;
  54. }
  55. static inline unsigned long long us_to_tick(unsigned long long us)
  56. {
  57. us = us * CONFIG_MX31_CLK32 + 999999;
  58. do_div(us, 1000000);
  59. return us;
  60. }
  61. #else
  62. /* ~2% error */
  63. #define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
  64. #define US_PER_TICK (1000000 / CONFIG_MX31_CLK32)
  65. static inline unsigned long long tick_to_time(unsigned long long tick)
  66. {
  67. do_div(tick, TICK_PER_TIME);
  68. return tick;
  69. }
  70. static inline unsigned long long time_to_tick(unsigned long long time)
  71. {
  72. return time * TICK_PER_TIME;
  73. }
  74. static inline unsigned long long us_to_tick(unsigned long long us)
  75. {
  76. us += US_PER_TICK - 1;
  77. do_div(us, US_PER_TICK);
  78. return us;
  79. }
  80. #endif
  81. /* The 32768Hz 32-bit timer overruns in 131072 seconds */
  82. int timer_init (void)
  83. {
  84. int i;
  85. /* setup GP Timer 1 */
  86. GPTCR = GPTCR_SWR;
  87. for (i = 0; i < 100; i++)
  88. GPTCR = 0; /* We have no udelay by now */
  89. GPTPR = 0; /* 32Khz */
  90. /* Freerun Mode, PERCLK1 input */
  91. GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
  92. return 0;
  93. }
  94. unsigned long long get_ticks (void)
  95. {
  96. ulong now = GPTCNT; /* current tick value */
  97. if (now >= gd->lastinc) /* normal mode (non roll) */
  98. /* move stamp forward with absolut diff ticks */
  99. gd->tbl += (now - gd->lastinc);
  100. else /* we have rollover of incrementer */
  101. gd->tbl += (0xFFFFFFFF - gd->lastinc) + now;
  102. gd->lastinc = now;
  103. return gd->tbl;
  104. }
  105. ulong get_timer_masked (void)
  106. {
  107. /*
  108. * get_ticks() returns a long long (64 bit), it wraps in
  109. * 2^64 / CONFIG_MX31_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  110. * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
  111. * 5 * 10^6 days - long enough.
  112. */
  113. return tick_to_time(get_ticks());
  114. }
  115. ulong get_timer (ulong base)
  116. {
  117. return get_timer_masked () - base;
  118. }
  119. /* delay x useconds AND preserve advance timestamp value */
  120. void __udelay (unsigned long usec)
  121. {
  122. unsigned long long tmp;
  123. ulong tmo;
  124. tmo = us_to_tick(usec);
  125. tmp = get_ticks() + tmo; /* get current timestamp */
  126. while (get_ticks() < tmp) /* loop till event */
  127. /*NOP*/;
  128. }
  129. void reset_cpu (ulong addr)
  130. {
  131. struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
  132. wdog->wcr = WDOG_ENABLE;
  133. while (1)
  134. ;
  135. }
  136. #ifdef CONFIG_HW_WATCHDOG
  137. void mxc_hw_watchdog_enable(void)
  138. {
  139. struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
  140. u16 secs;
  141. /*
  142. * The timer watchdog can be set between
  143. * 0.5 and 128 Seconds. If not defined
  144. * in configuration file, sets 64 Seconds
  145. */
  146. #ifdef CONFIG_SYS_WD_TIMER_SECS
  147. secs = (CONFIG_SYS_WD_TIMER_SECS << 1) & 0xFF;
  148. if (!secs) secs = 1;
  149. #else
  150. secs = 64;
  151. #endif
  152. setbits_le16(&wdog->wcr, (secs << WDOG_WT_SHIFT) | WDOG_ENABLE
  153. | WDOG_WDZST);
  154. }
  155. void mxc_hw_watchdog_reset(void)
  156. {
  157. struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
  158. writew(0x5555, &wdog->wsr);
  159. writew(0xAAAA, &wdog->wsr);
  160. }
  161. #endif