timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/io.h>
  25. #include <asm/arch/kirkwood.h>
  26. #define UBOOT_CNTR 0 /* counter to use for uboot timer */
  27. /* Timer reload and current value registers */
  28. struct kwtmr_val {
  29. u32 reload; /* Timer reload reg */
  30. u32 val; /* Timer value reg */
  31. };
  32. /* Timer registers */
  33. struct kwtmr_registers {
  34. u32 ctrl; /* Timer control reg */
  35. u32 pad[3];
  36. struct kwtmr_val tmr[2];
  37. u32 wdt_reload;
  38. u32 wdt_val;
  39. };
  40. struct kwtmr_registers *kwtmr_regs = (struct kwtmr_registers *)KW_TIMER_BASE;
  41. /*
  42. * ARM Timers Registers Map
  43. */
  44. #define CNTMR_CTRL_REG &kwtmr_regs->ctrl
  45. #define CNTMR_RELOAD_REG(tmrnum) &kwtmr_regs->tmr[tmrnum].reload
  46. #define CNTMR_VAL_REG(tmrnum) &kwtmr_regs->tmr[tmrnum].val
  47. /*
  48. * ARM Timers Control Register
  49. * CPU_TIMERS_CTRL_REG (CTCR)
  50. */
  51. #define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
  52. #define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
  53. #define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
  54. #define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
  55. #define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
  56. #define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
  57. #define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
  58. #define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
  59. /*
  60. * ARM Timer\Watchdog Reload Register
  61. * CNTMR_RELOAD_REG (TRR)
  62. */
  63. #define TRG_ARM_TIMER_REL_OFFS 0
  64. #define TRG_ARM_TIMER_REL_MASK 0xffffffff
  65. /*
  66. * ARM Timer\Watchdog Register
  67. * CNTMR_VAL_REG (TVRG)
  68. */
  69. #define TVR_ARM_TIMER_OFFS 0
  70. #define TVR_ARM_TIMER_MASK 0xffffffff
  71. #define TVR_ARM_TIMER_MAX 0xffffffff
  72. #define TIMER_LOAD_VAL 0xffffffff
  73. #define READ_TIMER (readl(CNTMR_VAL_REG(UBOOT_CNTR)) / \
  74. (CONFIG_SYS_TCLK / 1000))
  75. DECLARE_GLOBAL_DATA_PTR;
  76. #define timestamp gd->arch.tbl
  77. #define lastdec gd->lastinc
  78. ulong get_timer_masked(void)
  79. {
  80. ulong now = READ_TIMER;
  81. if (lastdec >= now) {
  82. /* normal mode */
  83. timestamp += lastdec - now;
  84. } else {
  85. /* we have an overflow ... */
  86. timestamp += lastdec +
  87. (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
  88. }
  89. lastdec = now;
  90. return timestamp;
  91. }
  92. ulong get_timer(ulong base)
  93. {
  94. return get_timer_masked() - base;
  95. }
  96. void __udelay(unsigned long usec)
  97. {
  98. uint current;
  99. ulong delayticks;
  100. current = readl(CNTMR_VAL_REG(UBOOT_CNTR));
  101. delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
  102. if (current < delayticks) {
  103. delayticks -= current;
  104. while (readl(CNTMR_VAL_REG(UBOOT_CNTR)) < current) ;
  105. while ((TIMER_LOAD_VAL - delayticks) <
  106. readl(CNTMR_VAL_REG(UBOOT_CNTR))) ;
  107. } else {
  108. while (readl(CNTMR_VAL_REG(UBOOT_CNTR)) >
  109. (current - delayticks)) ;
  110. }
  111. }
  112. /*
  113. * init the counter
  114. */
  115. int timer_init(void)
  116. {
  117. unsigned int cntmrctrl;
  118. /* load value into timer */
  119. writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
  120. writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
  121. /* enable timer in auto reload mode */
  122. cntmrctrl = readl(CNTMR_CTRL_REG);
  123. cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
  124. cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
  125. writel(cntmrctrl, CNTMR_CTRL_REG);
  126. /* init the timestamp and lastdec value */
  127. lastdec = READ_TIMER;
  128. timestamp = 0;
  129. return 0;
  130. }
  131. /*
  132. * This function is derived from PowerPC code (read timebase as long long).
  133. * On ARM it just returns the timer value.
  134. */
  135. unsigned long long get_ticks(void)
  136. {
  137. return get_timer(0);
  138. }
  139. /*
  140. * This function is derived from PowerPC code (timebase clock frequency).
  141. * On ARM it returns the number of timer ticks per second.
  142. */
  143. ulong get_tbclk (void)
  144. {
  145. return (ulong)CONFIG_SYS_HZ;
  146. }