timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
  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/io.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. ulong get_timer_masked(void)
  85. {
  86. ulong now = read_timer();
  87. if (lastdec >= now) {
  88. /* normal mode */
  89. timestamp += lastdec - now;
  90. } else {
  91. /* we have an overflow ... */
  92. timestamp += lastdec +
  93. (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
  94. }
  95. lastdec = now;
  96. return timestamp;
  97. }
  98. ulong get_timer(ulong base)
  99. {
  100. return get_timer_masked() - base;
  101. }
  102. static inline ulong uboot_cntr_val(void)
  103. {
  104. return readl(CNTMR_VAL_REG(UBOOT_CNTR));
  105. }
  106. void __udelay(unsigned long usec)
  107. {
  108. uint current;
  109. ulong delayticks;
  110. current = uboot_cntr_val();
  111. delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
  112. if (current < delayticks) {
  113. delayticks -= current;
  114. while (uboot_cntr_val() < current)
  115. ;
  116. while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
  117. ;
  118. } else {
  119. while (uboot_cntr_val() > (current - delayticks))
  120. ;
  121. }
  122. }
  123. /*
  124. * init the counter
  125. */
  126. int timer_init(void)
  127. {
  128. unsigned int cntmrctrl;
  129. /* load value into timer */
  130. writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
  131. writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
  132. /* enable timer in auto reload mode */
  133. cntmrctrl = readl(CNTMR_CTRL_REG);
  134. cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
  135. cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
  136. writel(cntmrctrl, CNTMR_CTRL_REG);
  137. return 0;
  138. }
  139. void timer_init_r(void)
  140. {
  141. /* init the timestamp and lastdec value */
  142. lastdec = read_timer();
  143. timestamp = 0;
  144. }