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