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