timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <common.h>
  18. #include <asm/io.h>
  19. #include <asm/arch/timer.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static const struct socfpga_timer *timer_base = (void *)CONFIG_SYS_TIMERBASE;
  22. /*
  23. * Timer initialization
  24. */
  25. int timer_init(void)
  26. {
  27. writel(TIMER_LOAD_VAL, &timer_base->load_val);
  28. writel(TIMER_LOAD_VAL, &timer_base->curr_val);
  29. writel(readl(&timer_base->ctrl) | 0x3, &timer_base->ctrl);
  30. return 0;
  31. }
  32. static u32 read_timer(void)
  33. {
  34. return readl(&timer_base->curr_val);
  35. }
  36. /*
  37. * Delay x useconds
  38. */
  39. void __udelay(unsigned long usec)
  40. {
  41. unsigned long now, last;
  42. /*
  43. * get the tmo value based on timer clock speed
  44. * tmo = delay required / period of timer clock
  45. */
  46. long tmo = usec * CONFIG_TIMER_CLOCK_KHZ / 1000;
  47. last = read_timer();
  48. while (tmo > 0) {
  49. now = read_timer();
  50. if (last >= now)
  51. /* normal mode (non roll) */
  52. tmo -= last - now;
  53. else
  54. /* we have overflow of the count down timer */
  55. tmo -= TIMER_LOAD_VAL - last + now;
  56. last = now;
  57. }
  58. }
  59. /*
  60. * Get the timer value
  61. */
  62. ulong get_timer(ulong base)
  63. {
  64. return get_timer_masked() - base;
  65. }
  66. /*
  67. * Timer : get the time difference
  68. * Unit of tick is based on the CONFIG_SYS_HZ
  69. */
  70. ulong get_timer_masked(void)
  71. {
  72. /* current tick value */
  73. ulong now = read_timer() / (CONFIG_TIMER_CLOCK_KHZ/CONFIG_SYS_HZ);
  74. if (gd->arch.lastinc >= now) {
  75. /* normal mode (non roll) */
  76. /* move stamp forward with absolute diff ticks */
  77. gd->arch.tbl += gd->arch.lastinc - now;
  78. } else {
  79. /* we have overflow of the count down timer */
  80. gd->arch.tbl += TIMER_LOAD_VAL - gd->arch.lastinc + now;
  81. }
  82. gd->arch.lastinc = now;
  83. return gd->arch.tbl;
  84. }
  85. /*
  86. * Reset the timer
  87. */
  88. void reset_timer(void)
  89. {
  90. /* capture current decrementer value time */
  91. gd->arch.lastinc = read_timer() /
  92. (CONFIG_TIMER_CLOCK_KHZ / CONFIG_SYS_HZ);
  93. /* start "advancing" time stamp from 0 */
  94. gd->arch.tbl = 0;
  95. }