timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * (C) Copyright 2011
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Lei Wen <leiwen@marvell.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #include <common.h>
  25. #include <asm/arch/pantheon.h>
  26. /*
  27. * Timer registers
  28. * Refer 6.2.9 in Datasheet
  29. */
  30. struct panthtmr_registers {
  31. u32 clk_ctrl; /* Timer clk control reg */
  32. u32 match[9]; /* Timer match registers */
  33. u32 count[3]; /* Timer count registers */
  34. u32 status[3];
  35. u32 ie[3];
  36. u32 preload[3]; /* Timer preload value */
  37. u32 preload_ctrl[3];
  38. u32 wdt_match_en;
  39. u32 wdt_match_r;
  40. u32 wdt_val;
  41. u32 wdt_sts;
  42. u32 icr[3];
  43. u32 wdt_icr;
  44. u32 cer; /* Timer count enable reg */
  45. u32 cmr;
  46. u32 ilr[3];
  47. u32 wcr;
  48. u32 wfar;
  49. u32 wsar;
  50. u32 cvwr[3];
  51. };
  52. #define TIMER 0 /* Use TIMER 0 */
  53. /* Each timer has 3 match registers */
  54. #define MATCH_CMP(x) ((3 * TIMER) + x)
  55. #define TIMER_LOAD_VAL 0xffffffff
  56. #define COUNT_RD_REQ 0x1
  57. DECLARE_GLOBAL_DATA_PTR;
  58. /* Using gd->tbu from timestamp and gd->tbl for lastdec */
  59. /*
  60. * For preventing risk of instability in reading counter value,
  61. * first set read request to register cvwr and then read same
  62. * register after it captures counter value.
  63. */
  64. ulong read_timer(void)
  65. {
  66. struct panthtmr_registers *panthtimers =
  67. (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
  68. volatile int loop=100;
  69. ulong val;
  70. writel(COUNT_RD_REQ, &panthtimers->cvwr);
  71. while (loop--)
  72. val = readl(&panthtimers->cvwr);
  73. /*
  74. * This stop gcc complain and prevent loop mistake init to 0
  75. */
  76. val = readl(&panthtimers->cvwr);
  77. return val;
  78. }
  79. void reset_timer_masked(void)
  80. {
  81. /* reset time */
  82. gd->tbl = read_timer();
  83. gd->tbu = 0;
  84. }
  85. ulong get_timer_masked(void)
  86. {
  87. ulong now = read_timer();
  88. if (now >= gd->tbl) {
  89. /* normal mode */
  90. gd->tbu += now - gd->tbl;
  91. } else {
  92. /* we have an overflow ... */
  93. gd->tbu += now + TIMER_LOAD_VAL - gd->tbl;
  94. }
  95. gd->tbl = now;
  96. return gd->tbu;
  97. }
  98. ulong get_timer(ulong base)
  99. {
  100. return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
  101. base);
  102. }
  103. void __udelay(unsigned long usec)
  104. {
  105. ulong delayticks;
  106. ulong endtime;
  107. delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
  108. endtime = get_timer_masked() + delayticks;
  109. while (get_timer_masked() < endtime)
  110. ;
  111. }
  112. /*
  113. * init the Timer
  114. */
  115. int timer_init(void)
  116. {
  117. struct panthapb_registers *apb1clkres =
  118. (struct panthapb_registers *) PANTHEON_APBC_BASE;
  119. struct panthtmr_registers *panthtimers =
  120. (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
  121. /* Enable Timer clock at 3.25 MHZ */
  122. writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
  123. /* load value into timer */
  124. writel(0x0, &panthtimers->clk_ctrl);
  125. /* Use Timer 0 Match Resiger 0 */
  126. writel(TIMER_LOAD_VAL, &panthtimers->match[MATCH_CMP(0)]);
  127. /* Preload value is 0 */
  128. writel(0x0, &panthtimers->preload[TIMER]);
  129. /* Enable match comparator 0 for Timer 0 */
  130. writel(0x1, &panthtimers->preload_ctrl[TIMER]);
  131. /* Enable timer 0 */
  132. writel(0x1, &panthtimers->cer);
  133. /* init the gd->tbu and gd->tbl value */
  134. reset_timer_masked();
  135. return 0;
  136. }
  137. #define MPMU_APRR_WDTR (1<<4)
  138. #define TMR_WFAR 0xbaba /* WDT Register First key */
  139. #define TMP_WSAR 0xeb10 /* WDT Register Second key */
  140. /*
  141. * This function uses internal Watchdog Timer
  142. * based reset mechanism.
  143. * Steps to write watchdog registers (protected access)
  144. * 1. Write key value to TMR_WFAR reg.
  145. * 2. Write key value to TMP_WSAR reg.
  146. * 3. Perform write operation.
  147. */
  148. void reset_cpu (unsigned long ignored)
  149. {
  150. struct panthmpmu_registers *mpmu =
  151. (struct panthmpmu_registers *) PANTHEON_MPMU_BASE;
  152. struct panthtmr_registers *panthtimers =
  153. (struct panthtmr_registers *) PANTHEON_WD_TIMER_BASE;
  154. u32 val;
  155. /* negate hardware reset to the WDT after system reset */
  156. val = readl(&mpmu->aprr);
  157. val = val | MPMU_APRR_WDTR;
  158. writel(val, &mpmu->aprr);
  159. /* reset/enable WDT clock */
  160. writel(APBC_APBCLK, &mpmu->wdtpcr);
  161. /* clear previous WDT status */
  162. writel(TMR_WFAR, &panthtimers->wfar);
  163. writel(TMP_WSAR, &panthtimers->wsar);
  164. writel(0, &panthtimers->wdt_sts);
  165. /* set match counter */
  166. writel(TMR_WFAR, &panthtimers->wfar);
  167. writel(TMP_WSAR, &panthtimers->wsar);
  168. writel(0xf, &panthtimers->wdt_match_r);
  169. /* enable WDT reset */
  170. writel(TMR_WFAR, &panthtimers->wfar);
  171. writel(TMP_WSAR, &panthtimers->wsar);
  172. writel(0x3, &panthtimers->wdt_match_en);
  173. /*enable functional WDT clock */
  174. writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
  175. }