interrupts.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * (C) Copyright 2004
  3. * Texas Instruments
  4. * Richard Woodruff <r-woodruff2@ti.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. * Alex Zuepke <azu@sysgo.de>
  10. *
  11. * (C) Copyright 2002
  12. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <asm/arch/bits.h>
  34. #if !defined(CONFIG_INTEGRATOR) && ! defined(CONFIG_ARCH_CINTEGRATOR)
  35. # include <asm/arch/omap2420.h>
  36. #endif
  37. #define TIMER_LOAD_VAL 0
  38. /* macro to read the 32 bit timer */
  39. #define READ_TIMER (*(volatile ulong *)(CFG_TIMERBASE+TCRR))
  40. #if defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_CINTEGRATOR)
  41. /* Use the IntegratorCP function from board/integratorcp.c */
  42. #else
  43. static ulong timestamp;
  44. static ulong lastinc;
  45. /* nothing really to do with interrupts, just starts up a counter. */
  46. int interrupt_init (void)
  47. {
  48. int32_t val;
  49. /* Start the counter ticking up */
  50. *((int32_t *) (CFG_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
  51. val = (CFG_PVT << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
  52. *((int32_t *) (CFG_TIMERBASE + TCLR)) = val; /* start timer */
  53. reset_timer_masked(); /* init the timestamp and lastinc value */
  54. return(0);
  55. }
  56. /*
  57. * timer without interrupts
  58. */
  59. void reset_timer (void)
  60. {
  61. reset_timer_masked ();
  62. }
  63. ulong get_timer (ulong base)
  64. {
  65. return get_timer_masked () - base;
  66. }
  67. void set_timer (ulong t)
  68. {
  69. timestamp = t;
  70. }
  71. /* delay x useconds AND perserve advance timstamp value */
  72. void udelay (unsigned long usec)
  73. {
  74. ulong tmo, tmp;
  75. if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
  76. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  77. tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */
  78. tmo /= 1000; /* finish normalize. */
  79. } else { /* else small number, don't kill it prior to HZ multiply */
  80. tmo = usec * CFG_HZ;
  81. tmo /= (1000*1000);
  82. }
  83. tmp = get_timer (0); /* get current timestamp */
  84. if ( (tmo + tmp + 1) < tmp )/* if setting this forward will roll time stamp */
  85. reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastinc value */
  86. else
  87. tmo += tmp; /* else, set advancing stamp wake up time */
  88. while (get_timer_masked () < tmo)/* loop till event */
  89. /*NOP*/;
  90. }
  91. void reset_timer_masked (void)
  92. {
  93. /* reset time */
  94. lastinc = READ_TIMER; /* capture current incrementer value time */
  95. timestamp = 0; /* start "advancing" time stamp from 0 */
  96. }
  97. ulong get_timer_masked (void)
  98. {
  99. ulong now = READ_TIMER; /* current tick value */
  100. if (now >= lastinc) /* normal mode (non roll) */
  101. timestamp += (now - lastinc); /* move stamp fordward with absoulte diff ticks */
  102. else /* we have rollover of incrementer */
  103. timestamp += (0xFFFFFFFF - lastinc) + now;
  104. lastinc = now;
  105. return timestamp;
  106. }
  107. /* waits specified delay value and resets timestamp */
  108. void udelay_masked (unsigned long usec)
  109. {
  110. ulong tmo;
  111. ulong endtime;
  112. signed long diff;
  113. if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
  114. tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
  115. tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */
  116. tmo /= 1000; /* finish normalize. */
  117. } else { /* else small number, don't kill it prior to HZ multiply */
  118. tmo = usec * CFG_HZ;
  119. tmo /= (1000*1000);
  120. }
  121. endtime = get_timer_masked () + tmo;
  122. do {
  123. ulong now = get_timer_masked ();
  124. diff = endtime - now;
  125. } while (diff >= 0);
  126. }
  127. /*
  128. * This function is derived from PowerPC code (read timebase as long long).
  129. * On ARM it just returns the timer value.
  130. */
  131. unsigned long long get_ticks(void)
  132. {
  133. return get_timer(0);
  134. }
  135. /*
  136. * This function is derived from PowerPC code (timebase clock frequency).
  137. * On ARM it returns the number of timer ticks per second.
  138. */
  139. ulong get_tbclk (void)
  140. {
  141. ulong tbclk;
  142. tbclk = CFG_HZ;
  143. return tbclk;
  144. }
  145. #endif /* !Integrator/CP */