timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * (C) Copyright 2011, Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
  3. * (C) Copyright 2011, Julius Baxter <julius@opencores.org>
  4. * (C) Copyright 2003
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/system.h>
  27. #include <asm/openrisc_exc.h>
  28. static ulong timestamp;
  29. /* how many counter cycles in a jiffy */
  30. #define TIMER_COUNTER_CYCLES (CONFIG_SYS_CLK_FREQ/CONFIG_SYS_OPENRISC_TMR_HZ)
  31. /* how many ms elapses between each timer interrupt */
  32. #define TIMER_TIMESTAMP_INC (1000/CONFIG_SYS_OPENRISC_TMR_HZ)
  33. /* how many cycles per ms */
  34. #define TIMER_CYCLES_MS (CONFIG_SYS_CLK_FREQ/1000)
  35. /* how many cycles per us */
  36. #define TIMER_CYCLES_US (CONFIG_SYS_CLK_FREQ/1000000uL)
  37. void timer_isr(void)
  38. {
  39. timestamp += TIMER_TIMESTAMP_INC;
  40. mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
  41. (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
  42. }
  43. int timer_init(void)
  44. {
  45. /* Install timer exception handler */
  46. exception_install_handler(EXC_TIMER, timer_isr);
  47. /* Set up the timer for the first expiration. */
  48. timestamp = 0;
  49. mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
  50. (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
  51. /* Enable tick timer exception in supervisor register */
  52. mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_TEE);
  53. return 0;
  54. }
  55. void reset_timer(void)
  56. {
  57. timestamp = 0;
  58. mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
  59. (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
  60. }
  61. /*
  62. * The timer value in ms is calculated by taking the
  63. * value accumulated by full timer revolutions plus the value
  64. * accumulated in this period
  65. */
  66. ulong get_timer(ulong base)
  67. {
  68. return timestamp + mfspr(SPR_TTCR)/TIMER_CYCLES_MS - base;
  69. }
  70. void set_timer(ulong t)
  71. {
  72. reset_timer();
  73. timestamp = t;
  74. }
  75. unsigned long long get_ticks(void)
  76. {
  77. return get_timer(0);
  78. }
  79. ulong get_tbclk(void)
  80. {
  81. return CONFIG_SYS_HZ;
  82. }
  83. void __udelay(ulong usec)
  84. {
  85. ulong elapsed = 0;
  86. ulong tick;
  87. ulong last_tick;
  88. last_tick = mfspr(SPR_TTCR);
  89. while ((elapsed / TIMER_CYCLES_US) < usec) {
  90. tick = mfspr(SPR_TTCR);
  91. if (tick >= last_tick)
  92. elapsed += (tick - last_tick);
  93. else
  94. elapsed += TIMER_COUNTER_CYCLES - (last_tick - tick);
  95. last_tick = tick;
  96. }
  97. }