timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. ulong get_timer(ulong base)
  93. {
  94. return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
  95. base);
  96. }
  97. void __udelay(unsigned long usec)
  98. {
  99. ulong delayticks;
  100. ulong endtime;
  101. delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
  102. endtime = get_timer_masked() + delayticks;
  103. while (get_timer_masked() < endtime);
  104. }
  105. /*
  106. * init the Timer
  107. */
  108. int timer_init(void)
  109. {
  110. struct armd1apb1_registers *apb1clkres =
  111. (struct armd1apb1_registers *) ARMD1_APBC1_BASE;
  112. struct armd1tmr_registers *armd1timers =
  113. (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
  114. /* Enable Timer clock at 3.25 MHZ */
  115. writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
  116. /* load value into timer */
  117. writel(0x0, &armd1timers->clk_ctrl);
  118. /* Use Timer 0 Match Resiger 0 */
  119. writel(TIMER_LOAD_VAL, &armd1timers->match[MATCH_CMP(0)]);
  120. /* Preload value is 0 */
  121. writel(0x0, &armd1timers->preload[TIMER]);
  122. /* Enable match comparator 0 for Timer 0 */
  123. writel(0x1, &armd1timers->preload_ctrl[TIMER]);
  124. /* Enable timer 0 */
  125. writel(0x1, &armd1timers->cer);
  126. /* init the gd->tbu and gd->tbl value */
  127. reset_timer_masked();
  128. return 0;
  129. }
  130. #define MPMU_APRR_WDTR (1<<4)
  131. #define TMR_WFAR 0xbaba /* WDT Register First key */
  132. #define TMP_WSAR 0xeb10 /* WDT Register Second key */
  133. /*
  134. * This function uses internal Watchdog Timer
  135. * based reset mechanism.
  136. * Steps to write watchdog registers (protected access)
  137. * 1. Write key value to TMR_WFAR reg.
  138. * 2. Write key value to TMP_WSAR reg.
  139. * 3. Perform write operation.
  140. */
  141. void reset_cpu (unsigned long ignored)
  142. {
  143. struct armd1mpmu_registers *mpmu =
  144. (struct armd1mpmu_registers *) ARMD1_MPMU_BASE;
  145. struct armd1tmr_registers *armd1timers =
  146. (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
  147. u32 val;
  148. /* negate hardware reset to the WDT after system reset */
  149. val = readl(&mpmu->aprr);
  150. val = val | MPMU_APRR_WDTR;
  151. writel(val, &mpmu->aprr);
  152. /* reset/enable WDT clock */
  153. writel(APBC_APBCLK | APBC_FNCLK | APBC_RST, &mpmu->wdtpcr);
  154. readl(&mpmu->wdtpcr);
  155. writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
  156. readl(&mpmu->wdtpcr);
  157. /* clear previous WDT status */
  158. writel(TMR_WFAR, &armd1timers->wfar);
  159. writel(TMP_WSAR, &armd1timers->wsar);
  160. writel(0, &armd1timers->wdt_sts);
  161. /* set match counter */
  162. writel(TMR_WFAR, &armd1timers->wfar);
  163. writel(TMP_WSAR, &armd1timers->wsar);
  164. writel(0xf, &armd1timers->wdt_match_r);
  165. /* enable WDT reset */
  166. writel(TMR_WFAR, &armd1timers->wfar);
  167. writel(TMP_WSAR, &armd1timers->wsar);
  168. writel(0x3, &armd1timers->wdt_match_en);
  169. while(1);
  170. }