timer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * (C) Copyright 2010
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. * Contributor: Mahavir Jain <mjain@marvell.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. * MA 02110-1301 USA
  24. */
  25. #include <common.h>
  26. #include <asm/arch/armada100.h>
  27. /*
  28. * Timer registers
  29. * Refer Section A.6 in Datasheet
  30. */
  31. struct armd1tmr_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;
  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. /* 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 armd1tmr_registers *armd1timers =
  67. (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
  68. volatile int loop=100;
  69. writel(COUNT_RD_REQ, &armd1timers->cvwr);
  70. while (loop--);
  71. return(readl(&armd1timers->cvwr));
  72. }
  73. void reset_timer_masked(void)
  74. {
  75. /* reset time */
  76. gd->tbl = read_timer();
  77. gd->tbu = 0;
  78. }
  79. ulong get_timer_masked(void)
  80. {
  81. ulong now = read_timer();
  82. if (now >= gd->tbl) {
  83. /* normal mode */
  84. gd->tbu += now - gd->tbl;
  85. } else {
  86. /* we have an overflow ... */
  87. gd->tbu += now + TIMER_LOAD_VAL - gd->tbl;
  88. }
  89. gd->tbl = now;
  90. return gd->tbu;
  91. }
  92. void reset_timer(void)
  93. {
  94. reset_timer_masked();
  95. }
  96. ulong get_timer(ulong base)
  97. {
  98. return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
  99. base);
  100. }
  101. void set_timer(ulong t)
  102. {
  103. gd->tbu = t;
  104. }
  105. void __udelay(unsigned long usec)
  106. {
  107. ulong delayticks;
  108. ulong endtime;
  109. delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
  110. endtime = get_timer_masked() + delayticks;
  111. while (get_timer_masked() < endtime);
  112. }
  113. /*
  114. * init the Timer
  115. */
  116. int timer_init(void)
  117. {
  118. struct armd1apb1_registers *apb1clkres =
  119. (struct armd1apb1_registers *) ARMD1_APBC1_BASE;
  120. struct armd1tmr_registers *armd1timers =
  121. (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
  122. /* Enable Timer clock at 3.25 MHZ */
  123. writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
  124. /* load value into timer */
  125. writel(0x0, &armd1timers->clk_ctrl);
  126. /* Use Timer 0 Match Resiger 0 */
  127. writel(TIMER_LOAD_VAL, &armd1timers->match[MATCH_CMP(0)]);
  128. /* Preload value is 0 */
  129. writel(0x0, &armd1timers->preload[TIMER]);
  130. /* Enable match comparator 0 for Timer 0 */
  131. writel(0x1, &armd1timers->preload_ctrl[TIMER]);
  132. /* Enable timer 0 */
  133. writel(0x1, &armd1timers->cer);
  134. /* init the gd->tbu and gd->tbl value */
  135. reset_timer_masked();
  136. return 0;
  137. }
  138. #define MPMU_APRR_WDTR (1<<4)
  139. #define TMR_WFAR 0xbaba /* WDT Register First key */
  140. #define TMP_WSAR 0xeb10 /* WDT Register Second key */
  141. /*
  142. * This function uses internal Watchdog Timer
  143. * based reset mechanism.
  144. * Steps to write watchdog registers (protected access)
  145. * 1. Write key value to TMR_WFAR reg.
  146. * 2. Write key value to TMP_WSAR reg.
  147. * 3. Perform write operation.
  148. */
  149. void reset_cpu (unsigned long ignored)
  150. {
  151. struct armd1mpmu_registers *mpmu =
  152. (struct armd1mpmu_registers *) ARMD1_MPMU_BASE;
  153. struct armd1tmr_registers *armd1timers =
  154. (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
  155. u32 val;
  156. /* negate hardware reset to the WDT after system reset */
  157. val = readl(&mpmu->aprr);
  158. val = val | MPMU_APRR_WDTR;
  159. writel(val, &mpmu->aprr);
  160. /* reset/enable WDT clock */
  161. writel(APBC_APBCLK | APBC_FNCLK | APBC_RST, &mpmu->wdtpcr);
  162. readl(&mpmu->wdtpcr);
  163. writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
  164. readl(&mpmu->wdtpcr);
  165. /* clear previous WDT status */
  166. writel(TMR_WFAR, &armd1timers->wfar);
  167. writel(TMP_WSAR, &armd1timers->wsar);
  168. writel(0, &armd1timers->wdt_sts);
  169. /* set match counter */
  170. writel(TMR_WFAR, &armd1timers->wfar);
  171. writel(TMP_WSAR, &armd1timers->wsar);
  172. writel(0xf, &armd1timers->wdt_match_r);
  173. /* enable WDT reset */
  174. writel(TMR_WFAR, &armd1timers->wfar);
  175. writel(TMP_WSAR, &armd1timers->wsar);
  176. writel(0x3, &armd1timers->wdt_match_en);
  177. while(1);
  178. }