timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2010 Albert ARIBAUD <albert.aribaud@free.fr>
  3. *
  4. * Based on original Kirkwood support which is
  5. * Copyright (C) Marvell International Ltd. and its affiliates
  6. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24. * MA 02110-1301 USA
  25. */
  26. #include <common.h>
  27. #include <asm/arch/orion5x.h>
  28. #define UBOOT_CNTR 0 /* counter to use for uboot timer */
  29. /* Timer reload and current value registers */
  30. struct orion5x_tmr_val {
  31. u32 reload; /* Timer reload reg */
  32. u32 val; /* Timer value reg */
  33. };
  34. /* Timer registers */
  35. struct orion5x_tmr_registers {
  36. u32 ctrl; /* Timer control reg */
  37. u32 pad[3];
  38. struct orion5x_tmr_val tmr[2];
  39. u32 wdt_reload;
  40. u32 wdt_val;
  41. };
  42. struct orion5x_tmr_registers *orion5x_tmr_regs =
  43. (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
  44. /*
  45. * ARM Timers Registers Map
  46. */
  47. #define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl)
  48. #define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload)
  49. #define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val)
  50. /*
  51. * ARM Timers Control Register
  52. * CPU_TIMERS_CTRL_REG (CTCR)
  53. */
  54. #define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
  55. #define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
  56. #define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
  57. #define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
  58. #define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
  59. #define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
  60. #define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
  61. #define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
  62. /*
  63. * ARM Timer\Watchdog Reload Register
  64. * CNTMR_RELOAD_REG (TRR)
  65. */
  66. #define TRG_ARM_TIMER_REL_OFFS 0
  67. #define TRG_ARM_TIMER_REL_MASK 0xffffffff
  68. /*
  69. * ARM Timer\Watchdog Register
  70. * CNTMR_VAL_REG (TVRG)
  71. */
  72. #define TVR_ARM_TIMER_OFFS 0
  73. #define TVR_ARM_TIMER_MASK 0xffffffff
  74. #define TVR_ARM_TIMER_MAX 0xffffffff
  75. #define TIMER_LOAD_VAL 0xffffffff
  76. static inline ulong read_timer(void)
  77. {
  78. return readl(CNTMR_VAL_REG(UBOOT_CNTR))
  79. / (CONFIG_SYS_TCLK / 1000);
  80. }
  81. DECLARE_GLOBAL_DATA_PTR;
  82. #define timestamp gd->tbl
  83. #define lastdec gd->lastinc
  84. void reset_timer_masked(void)
  85. {
  86. /* reset time */
  87. lastdec = read_timer();
  88. timestamp = 0;
  89. }
  90. ulong get_timer_masked(void)
  91. {
  92. ulong now = read_timer();
  93. if (lastdec >= now) {
  94. /* normal mode */
  95. timestamp += lastdec - now;
  96. } else {
  97. /* we have an overflow ... */
  98. timestamp += lastdec +
  99. (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
  100. }
  101. lastdec = now;
  102. return timestamp;
  103. }
  104. void reset_timer(void)
  105. {
  106. reset_timer_masked();
  107. }
  108. ulong get_timer(ulong base)
  109. {
  110. return get_timer_masked() - base;
  111. }
  112. void set_timer(ulong t)
  113. {
  114. timestamp = t;
  115. }
  116. static inline ulong uboot_cntr_val(void)
  117. {
  118. return readl(CNTMR_VAL_REG(UBOOT_CNTR));
  119. }
  120. void __udelay(unsigned long usec)
  121. {
  122. uint current;
  123. ulong delayticks;
  124. current = uboot_cntr_val();
  125. delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
  126. if (current < delayticks) {
  127. delayticks -= current;
  128. while (uboot_cntr_val() < current)
  129. ;
  130. while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
  131. ;
  132. } else {
  133. while (uboot_cntr_val() > (current - delayticks))
  134. ;
  135. }
  136. }
  137. /*
  138. * init the counter
  139. */
  140. int timer_init(void)
  141. {
  142. unsigned int cntmrctrl;
  143. /* load value into timer */
  144. writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
  145. writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
  146. /* enable timer in auto reload mode */
  147. cntmrctrl = readl(CNTMR_CTRL_REG);
  148. cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
  149. cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
  150. writel(cntmrctrl, CNTMR_CTRL_REG);
  151. return 0;
  152. }
  153. void timer_init_r(void)
  154. {
  155. /* init the timestamp and lastdec value */
  156. reset_timer_masked();
  157. }