timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/cpu.h>
  26. #include <asm/arch/pantheon.h>
  27. /*
  28. * Timer registers
  29. * Refer 6.2.9 in Datasheet
  30. */
  31. struct panthtmr_registers {
  32. u32 clk_ctrl; /* Timer clk control reg */
  33. u32 match[9]; /* Timer match registers */
  34. u32 count[3]; /* Timer count registers */
  35. u32 status[3];
  36. u32 ie[3];
  37. u32 preload[3]; /* Timer preload value */
  38. u32 preload_ctrl[3];
  39. u32 wdt_match_en;
  40. u32 wdt_match_r;
  41. u32 wdt_val;
  42. u32 wdt_sts;
  43. u32 icr[3];
  44. u32 wdt_icr;
  45. u32 cer; /* Timer count enable reg */
  46. u32 cmr;
  47. u32 ilr[3];
  48. u32 wcr;
  49. u32 wfar;
  50. u32 wsar;
  51. u32 cvwr[3];
  52. };
  53. #define TIMER 0 /* Use TIMER 0 */
  54. /* Each timer has 3 match registers */
  55. #define MATCH_CMP(x) ((3 * TIMER) + x)
  56. #define TIMER_LOAD_VAL 0xffffffff
  57. #define COUNT_RD_REQ 0x1
  58. DECLARE_GLOBAL_DATA_PTR;
  59. /* Using gd->tbu from timestamp and gd->tbl for lastdec */
  60. /*
  61. * For preventing risk of instability in reading counter value,
  62. * first set read request to register cvwr and then read same
  63. * register after it captures counter value.
  64. */
  65. ulong read_timer(void)
  66. {
  67. struct panthtmr_registers *panthtimers =
  68. (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
  69. volatile int loop=100;
  70. ulong val;
  71. writel(COUNT_RD_REQ, &panthtimers->cvwr);
  72. while (loop--)
  73. val = readl(&panthtimers->cvwr);
  74. /*
  75. * This stop gcc complain and prevent loop mistake init to 0
  76. */
  77. val = readl(&panthtimers->cvwr);
  78. return val;
  79. }
  80. ulong get_timer_masked(void)
  81. {
  82. ulong now = read_timer();
  83. if (now >= gd->tbl) {
  84. /* normal mode */
  85. gd->tbu += now - gd->tbl;
  86. } else {
  87. /* we have an overflow ... */
  88. gd->tbu += now + TIMER_LOAD_VAL - gd->tbl;
  89. }
  90. gd->tbl = now;
  91. return gd->tbu;
  92. }
  93. ulong get_timer(ulong base)
  94. {
  95. return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
  96. base);
  97. }
  98. void __udelay(unsigned long usec)
  99. {
  100. ulong delayticks;
  101. ulong endtime;
  102. delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
  103. endtime = get_timer_masked() + delayticks;
  104. while (get_timer_masked() < endtime)
  105. ;
  106. }
  107. /*
  108. * init the Timer
  109. */
  110. int timer_init(void)
  111. {
  112. struct panthapb_registers *apb1clkres =
  113. (struct panthapb_registers *) PANTHEON_APBC_BASE;
  114. struct panthtmr_registers *panthtimers =
  115. (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
  116. /* Enable Timer clock at 3.25 MHZ */
  117. writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
  118. /* load value into timer */
  119. writel(0x0, &panthtimers->clk_ctrl);
  120. /* Use Timer 0 Match Resiger 0 */
  121. writel(TIMER_LOAD_VAL, &panthtimers->match[MATCH_CMP(0)]);
  122. /* Preload value is 0 */
  123. writel(0x0, &panthtimers->preload[TIMER]);
  124. /* Enable match comparator 0 for Timer 0 */
  125. writel(0x1, &panthtimers->preload_ctrl[TIMER]);
  126. /* Enable timer 0 */
  127. writel(0x1, &panthtimers->cer);
  128. /* init the gd->tbu and gd->tbl value */
  129. gd->tbl = read_timer();
  130. gd->tbu = 0;
  131. return 0;
  132. }
  133. #define MPMU_APRR_WDTR (1<<4)
  134. #define TMR_WFAR 0xbaba /* WDT Register First key */
  135. #define TMP_WSAR 0xeb10 /* WDT Register Second key */
  136. /*
  137. * This function uses internal Watchdog Timer
  138. * based reset mechanism.
  139. * Steps to write watchdog registers (protected access)
  140. * 1. Write key value to TMR_WFAR reg.
  141. * 2. Write key value to TMP_WSAR reg.
  142. * 3. Perform write operation.
  143. */
  144. void reset_cpu (unsigned long ignored)
  145. {
  146. struct panthmpmu_registers *mpmu =
  147. (struct panthmpmu_registers *) PANTHEON_MPMU_BASE;
  148. struct panthtmr_registers *panthtimers =
  149. (struct panthtmr_registers *) PANTHEON_WD_TIMER_BASE;
  150. u32 val;
  151. /* negate hardware reset to the WDT after system reset */
  152. val = readl(&mpmu->aprr);
  153. val = val | MPMU_APRR_WDTR;
  154. writel(val, &mpmu->aprr);
  155. /* reset/enable WDT clock */
  156. writel(APBC_APBCLK, &mpmu->wdtpcr);
  157. /* clear previous WDT status */
  158. writel(TMR_WFAR, &panthtimers->wfar);
  159. writel(TMP_WSAR, &panthtimers->wsar);
  160. writel(0, &panthtimers->wdt_sts);
  161. /* set match counter */
  162. writel(TMR_WFAR, &panthtimers->wfar);
  163. writel(TMP_WSAR, &panthtimers->wsar);
  164. writel(0xf, &panthtimers->wdt_match_r);
  165. /* enable WDT reset */
  166. writel(TMR_WFAR, &panthtimers->wfar);
  167. writel(TMP_WSAR, &panthtimers->wsar);
  168. writel(0x3, &panthtimers->wdt_match_en);
  169. /*enable functional WDT clock */
  170. writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
  171. }