timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian.pop <at> leadtechdesign.com>
  4. * Lead Tech Design <www.leadtechdesign.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/arch/hardware.h>
  26. /*
  27. * We're using the AT91CAP9 PITC in 32 bit mode, by
  28. * setting the 20 bit counter period to its maximum (0xfffff).
  29. */
  30. #define TIMER_LOAD_VAL 0xfffff
  31. #define READ_RESET_TIMER (AT91C_BASE_PITC->PITC_PIVR)
  32. #define READ_TIMER (AT91C_BASE_PITC->PITC_PIIR)
  33. #define TIMER_FREQ (AT91C_MASTER_CLOCK << 4)
  34. #define TICKS_TO_USEC(ticks) ((ticks) / 6)
  35. ulong get_timer_masked(void);
  36. ulong resettime;
  37. AT91PS_PITC p_pitc;
  38. /* nothing really to do with interrupts, just starts up a counter. */
  39. int interrupt_init(void)
  40. {
  41. /*
  42. * Enable PITC Clock
  43. * The clock is already enabled for system controller in boot
  44. */
  45. AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_SYS;
  46. /* Enable PITC */
  47. AT91C_BASE_PITC->PITC_PIMR = AT91C_PITC_PITEN;
  48. /* Load PITC_PIMR with the right timer value */
  49. AT91C_BASE_PITC->PITC_PIMR |= TIMER_LOAD_VAL;
  50. reset_timer_masked();
  51. return 0;
  52. }
  53. /*
  54. * timer without interrupts
  55. */
  56. static inline ulong get_timer_raw(void)
  57. {
  58. ulong now = READ_TIMER;
  59. if (now >= resettime)
  60. return now - resettime;
  61. else
  62. return 0xFFFFFFFFUL - (resettime - now) ;
  63. }
  64. void reset_timer_masked(void)
  65. {
  66. resettime = READ_TIMER;
  67. }
  68. ulong get_timer_masked(void)
  69. {
  70. return TICKS_TO_USEC(get_timer_raw());
  71. }
  72. void udelay_masked(unsigned long usec)
  73. {
  74. ulong tmp;
  75. tmp = get_timer(0);
  76. while (get_timer(tmp) < usec) /* our timer works in usecs */
  77. ; /* NOP */
  78. }
  79. void reset_timer(void)
  80. {
  81. reset_timer_masked();
  82. }
  83. ulong get_timer(ulong base)
  84. {
  85. ulong now = get_timer_masked();
  86. if (now >= base)
  87. return now - base;
  88. else
  89. return TICKS_TO_USEC(0xFFFFFFFFUL) - (base - now) ;
  90. }
  91. void udelay(unsigned long usec)
  92. {
  93. udelay_masked(usec);
  94. }
  95. /*
  96. * This function is derived from PowerPC code (read timebase as long long).
  97. * On ARM it just returns the timer value.
  98. */
  99. unsigned long long get_ticks(void)
  100. {
  101. return get_timer(0);
  102. }
  103. /*
  104. * This function is derived from PowerPC code (timebase clock frequency).
  105. * On ARM it returns the number of timer ticks per second.
  106. */
  107. ulong get_tbclk(void)
  108. {
  109. ulong tbclk;
  110. tbclk = CFG_HZ;
  111. return tbclk;
  112. }
  113. /*
  114. * Reset the cpu by setting up the watchdog timer and let him time out
  115. * on the AT91CAP9ADK board
  116. */
  117. void reset_cpu(ulong ignored)
  118. {
  119. /* this is the way Linux does it */
  120. AT91C_BASE_RSTC->RSTC_RCR = (0xA5 << 24) |
  121. AT91C_RSTC_PROCRST |
  122. AT91C_RSTC_PERRST;
  123. while (1);
  124. /* Never reached */
  125. }