]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/arm926ejs/pantheon/timer.c
Timer: Remove set_timer completely
[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 void reset_timer(void)
112 {
113         reset_timer_masked();
114 }
115
116 ulong get_timer(ulong base)
117 {
118         return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
119                 base);
120 }
121
122 void __udelay(unsigned long usec)
123 {
124         ulong delayticks;
125         ulong endtime;
126
127         delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
128         endtime = get_timer_masked() + delayticks;
129
130         while (get_timer_masked() < endtime)
131                 ;
132 }
133
134 /*
135  * init the Timer
136  */
137 int timer_init(void)
138 {
139         struct panthapb_registers *apb1clkres =
140                 (struct panthapb_registers *) PANTHEON_APBC_BASE;
141         struct panthtmr_registers *panthtimers =
142                 (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
143
144         /* Enable Timer clock at 3.25 MHZ */
145         writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
146
147         /* load value into timer */
148         writel(0x0, &panthtimers->clk_ctrl);
149         /* Use Timer 0 Match Resiger 0 */
150         writel(TIMER_LOAD_VAL, &panthtimers->match[MATCH_CMP(0)]);
151         /* Preload value is 0 */
152         writel(0x0, &panthtimers->preload[TIMER]);
153         /* Enable match comparator 0 for Timer 0 */
154         writel(0x1, &panthtimers->preload_ctrl[TIMER]);
155
156         /* Enable timer 0 */
157         writel(0x1, &panthtimers->cer);
158         /* init the gd->tbu and gd->tbl value */
159         reset_timer_masked();
160
161         return 0;
162 }
163
164 #define MPMU_APRR_WDTR  (1<<4)
165 #define TMR_WFAR        0xbaba  /* WDT Register First key */
166 #define TMP_WSAR        0xeb10  /* WDT Register Second key */
167
168 /*
169  * This function uses internal Watchdog Timer
170  * based reset mechanism.
171  * Steps to write watchdog registers (protected access)
172  * 1. Write key value to TMR_WFAR reg.
173  * 2. Write key value to TMP_WSAR reg.
174  * 3. Perform write operation.
175  */
176 void reset_cpu (unsigned long ignored)
177 {
178         struct panthmpmu_registers *mpmu =
179                 (struct panthmpmu_registers *) PANTHEON_MPMU_BASE;
180         struct panthtmr_registers *panthtimers =
181                 (struct panthtmr_registers *) PANTHEON_WD_TIMER_BASE;
182         u32 val;
183
184         /* negate hardware reset to the WDT after system reset */
185         val = readl(&mpmu->aprr);
186         val = val | MPMU_APRR_WDTR;
187         writel(val, &mpmu->aprr);
188
189         /* reset/enable WDT clock */
190         writel(APBC_APBCLK, &mpmu->wdtpcr);
191
192         /* clear previous WDT status */
193         writel(TMR_WFAR, &panthtimers->wfar);
194         writel(TMP_WSAR, &panthtimers->wsar);
195         writel(0, &panthtimers->wdt_sts);
196
197         /* set match counter */
198         writel(TMR_WFAR, &panthtimers->wfar);
199         writel(TMP_WSAR, &panthtimers->wsar);
200         writel(0xf, &panthtimers->wdt_match_r);
201
202         /* enable WDT reset */
203         writel(TMR_WFAR, &panthtimers->wfar);
204         writel(TMP_WSAR, &panthtimers->wsar);
205         writel(0x3, &panthtimers->wdt_match_en);
206
207         /*enable functional WDT clock */
208         writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
209 }