timer.c 4.1 KB

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