interrupts.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/mx31-regs.h>
  25. #define TIMER_BASE 0x53f90000 /* General purpose timer 1 */
  26. /* General purpose timers registers */
  27. #define GPTCR __REG(TIMER_BASE) /* Control register */
  28. #define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */
  29. #define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */
  30. #define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */
  31. /* General purpose timers bitfields */
  32. #define GPTCR_SWR (1 << 15) /* Software reset */
  33. #define GPTCR_FRR (1 << 9) /* Freerun / restart */
  34. #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
  35. #define GPTCR_TEN 1 /* Timer enable */
  36. /* "time" is measured in 1 / CFG_HZ seconds, "tick" is internal timer period */
  37. #ifdef CONFIG_MX31_TIMER_HIGH_PRECISION
  38. /* ~0.4% error - measured with stop-watch on 100s boot-delay */
  39. #define TICK_TO_TIME(t) ((t) * CFG_HZ / CONFIG_MX31_CLK32)
  40. #define TIME_TO_TICK(t) ((unsigned long long)(t) * CONFIG_MX31_CLK32 / CFG_HZ)
  41. #define US_TO_TICK(t) (((unsigned long long)(t) * CONFIG_MX31_CLK32 + \
  42. 999999) / 1000000)
  43. #else
  44. /* ~2% error */
  45. #define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CFG_HZ / 2) / CFG_HZ)
  46. #define US_PER_TICK (1000000 / CONFIG_MX31_CLK32)
  47. #define TICK_TO_TIME(t) ((t) / TICK_PER_TIME)
  48. #define TIME_TO_TICK(t) ((unsigned long long)(t) * TICK_PER_TIME)
  49. #define US_TO_TICK(t) (((t) + US_PER_TICK - 1) / US_PER_TICK)
  50. #endif
  51. static ulong timestamp;
  52. static ulong lastinc;
  53. /* nothing really to do with interrupts, just starts up a counter. */
  54. /* The 32768Hz 32-bit timer overruns in 131072 seconds */
  55. int interrupt_init (void)
  56. {
  57. int i;
  58. /* setup GP Timer 1 */
  59. GPTCR = GPTCR_SWR;
  60. for (i = 0; i < 100; i++)
  61. GPTCR = 0; /* We have no udelay by now */
  62. GPTPR = 0; /* 32Khz */
  63. /* Freerun Mode, PERCLK1 input */
  64. GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
  65. return 0;
  66. }
  67. void reset_timer_masked (void)
  68. {
  69. /* reset time */
  70. lastinc = GPTCNT; /* capture current incrementer value time */
  71. timestamp = 0; /* start "advancing" time stamp from 0 */
  72. }
  73. void reset_timer(void)
  74. {
  75. reset_timer_masked();
  76. }
  77. unsigned long long get_ticks (void)
  78. {
  79. ulong now = GPTCNT; /* current tick value */
  80. if (now >= lastinc) /* normal mode (non roll) */
  81. /* move stamp forward with absolut diff ticks */
  82. timestamp += (now - lastinc);
  83. else /* we have rollover of incrementer */
  84. timestamp += (0xFFFFFFFF - lastinc) + now;
  85. lastinc = now;
  86. return timestamp;
  87. }
  88. ulong get_timer_masked (void)
  89. {
  90. /*
  91. * get_ticks() returns a long long (64 bit), it wraps in
  92. * 2^64 / CONFIG_MX31_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  93. * 5 * 10^9 days... and get_ticks() * CFG_HZ wraps in
  94. * 5 * 10^6 days - long enough.
  95. */
  96. return TICK_TO_TIME(get_ticks());
  97. }
  98. ulong get_timer (ulong base)
  99. {
  100. return get_timer_masked () - base;
  101. }
  102. void set_timer (ulong t)
  103. {
  104. timestamp = TIME_TO_TICK(t);
  105. }
  106. /* delay x useconds AND perserve advance timstamp value */
  107. void udelay (unsigned long usec)
  108. {
  109. unsigned long long tmp;
  110. ulong tmo;
  111. tmo = US_TO_TICK(usec);
  112. tmp = get_ticks() + tmo; /* get current timestamp */
  113. while (get_ticks() < tmp) /* loop till event */
  114. /*NOP*/;
  115. }
  116. void reset_cpu (ulong addr)
  117. {
  118. __REG16(WDOG_BASE) = 4;
  119. }