timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian.pop@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/clk.h>
  30. #include <asm/arch/io.h>
  31. #include <div64.h>
  32. /*
  33. * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
  34. * setting the 20 bit counter period to its maximum (0xfffff).
  35. */
  36. #define TIMER_LOAD_VAL 0xfffff
  37. #define READ_RESET_TIMER at91_sys_read(AT91_PIT_PIVR)
  38. #define READ_TIMER at91_sys_read(AT91_PIT_PIIR)
  39. static ulong timestamp;
  40. static ulong lastinc;
  41. static ulong timer_freq;
  42. static inline unsigned long long tick_to_time(unsigned long long tick)
  43. {
  44. tick *= CONFIG_SYS_HZ;
  45. do_div(tick, timer_freq);
  46. return tick;
  47. }
  48. static inline unsigned long long usec_to_tick(unsigned long long usec)
  49. {
  50. usec *= timer_freq;
  51. do_div(usec, 1000000);
  52. return usec;
  53. }
  54. /* nothing really to do with interrupts, just starts up a counter. */
  55. int timer_init(void)
  56. {
  57. /*
  58. * Enable PITC Clock
  59. * The clock is already enabled for system controller in boot
  60. */
  61. at91_sys_write(AT91_PMC_PCER, 1 << AT91_ID_SYS);
  62. /* Enable PITC */
  63. at91_sys_write(AT91_PIT_MR, TIMER_LOAD_VAL | AT91_PIT_PITEN);
  64. reset_timer_masked();
  65. timer_freq = get_mck_clk_rate() >> 4;
  66. return 0;
  67. }
  68. /*
  69. * timer without interrupts
  70. */
  71. unsigned long long get_ticks(void)
  72. {
  73. ulong now = READ_TIMER;
  74. if (now >= lastinc) /* normal mode (non roll) */
  75. /* move stamp forward with absolut diff ticks */
  76. timestamp += (now - lastinc);
  77. else /* we have rollover of incrementer */
  78. timestamp += (0xFFFFFFFF - lastinc) + now;
  79. lastinc = now;
  80. return timestamp;
  81. }
  82. void reset_timer_masked(void)
  83. {
  84. /* reset time */
  85. lastinc = READ_TIMER; /* capture current incrementer value time */
  86. timestamp = 0; /* start "advancing" time stamp from 0 */
  87. }
  88. ulong get_timer_masked(void)
  89. {
  90. return tick_to_time(get_ticks());
  91. }
  92. void udelay(unsigned long usec)
  93. {
  94. unsigned long long tmp;
  95. ulong tmo;
  96. tmo = usec_to_tick(usec);
  97. tmp = get_ticks() + tmo; /* get current timestamp */
  98. while (get_ticks() < tmp) /* loop till event */
  99. /*NOP*/;
  100. }
  101. void reset_timer(void)
  102. {
  103. reset_timer_masked();
  104. }
  105. ulong get_timer(ulong base)
  106. {
  107. return get_timer_masked () - base;
  108. }
  109. /*
  110. * This function is derived from PowerPC code (timebase clock frequency).
  111. * On ARM it returns the number of timer ticks per second.
  112. */
  113. ulong get_tbclk(void)
  114. {
  115. ulong tbclk;
  116. tbclk = CONFIG_SYS_HZ;
  117. return tbclk;
  118. }
  119. /*
  120. * Reset the cpu by setting up the watchdog timer and let him time out.
  121. */
  122. void reset_cpu(ulong ignored)
  123. {
  124. /* this is the way Linux does it */
  125. at91_sys_write(AT91_RSTC_CR, AT91_RSTC_KEY |
  126. AT91_RSTC_PROCRST |
  127. AT91_RSTC_PERRST);
  128. while (1);
  129. /* Never reached */
  130. }