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. #include <asm/arch/at91_pit.h>
  27. #include <asm/arch/at91_pmc.h>
  28. #include <asm/arch/at91_rstc.h>
  29. #include <asm/arch/io.h>
  30. /*
  31. * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
  32. * setting the 20 bit counter period to its maximum (0xfffff).
  33. */
  34. #define TIMER_LOAD_VAL 0xfffff
  35. #define READ_RESET_TIMER at91_sys_read(AT91_PIT_PIVR)
  36. #define READ_TIMER at91_sys_read(AT91_PIT_PIIR)
  37. #define TIMER_FREQ (AT91C_MASTER_CLOCK << 4)
  38. #define TICKS_TO_USEC(ticks) ((ticks) / 6)
  39. ulong get_timer_masked(void);
  40. ulong resettime;
  41. /* nothing really to do with interrupts, just starts up a counter. */
  42. int timer_init(void)
  43. {
  44. /*
  45. * Enable PITC Clock
  46. * The clock is already enabled for system controller in boot
  47. */
  48. at91_sys_write(AT91_PMC_PCER, 1 << AT91_ID_SYS);
  49. /* Enable PITC */
  50. at91_sys_write(AT91_PIT_MR, TIMER_LOAD_VAL | AT91_PIT_PITEN);
  51. reset_timer_masked();
  52. return 0;
  53. }
  54. /*
  55. * timer without interrupts
  56. */
  57. static inline ulong get_timer_raw(void)
  58. {
  59. ulong now = READ_TIMER;
  60. if (now >= resettime)
  61. return now - resettime;
  62. else
  63. return 0xFFFFFFFFUL - (resettime - now) ;
  64. }
  65. void reset_timer_masked(void)
  66. {
  67. resettime = READ_TIMER;
  68. }
  69. ulong get_timer_masked(void)
  70. {
  71. return TICKS_TO_USEC(get_timer_raw());
  72. }
  73. void udelay_masked(unsigned long usec)
  74. {
  75. ulong tmp;
  76. tmp = get_timer(0);
  77. while (get_timer(tmp) < usec) /* our timer works in usecs */
  78. ; /* NOP */
  79. }
  80. void reset_timer(void)
  81. {
  82. reset_timer_masked();
  83. }
  84. ulong get_timer(ulong base)
  85. {
  86. ulong now = get_timer_masked();
  87. if (now >= base)
  88. return now - base;
  89. else
  90. return TICKS_TO_USEC(0xFFFFFFFFUL) - (base - now) ;
  91. }
  92. void udelay(unsigned long usec)
  93. {
  94. udelay_masked(usec);
  95. }
  96. /*
  97. * This function is derived from PowerPC code (read timebase as long long).
  98. * On ARM it just returns the timer value.
  99. */
  100. unsigned long long get_ticks(void)
  101. {
  102. return get_timer(0);
  103. }
  104. /*
  105. * This function is derived from PowerPC code (timebase clock frequency).
  106. * On ARM it returns the number of timer ticks per second.
  107. */
  108. ulong get_tbclk(void)
  109. {
  110. ulong tbclk;
  111. tbclk = CFG_HZ;
  112. return tbclk;
  113. }
  114. /*
  115. * Reset the cpu by setting up the watchdog timer and let him time out.
  116. */
  117. void reset_cpu(ulong ignored)
  118. {
  119. /* this is the way Linux does it */
  120. at91_sys_write(AT91_RSTC_CR, AT91_RSTC_KEY |
  121. AT91_RSTC_PROCRST |
  122. AT91_RSTC_PERRST);
  123. while (1);
  124. /* Never reached */
  125. }