blob: c71162a8be697abedf379296de0f7ed0bd456edf [file] [log] [blame]
Lei Wen43013032011-02-09 18:06:58 +05301/*
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 */
32struct 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
61DECLARE_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 */
69ulong 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
Lei Wen43013032011-02-09 18:06:58 +053088ulong get_timer_masked(void)
89{
90 ulong now = read_timer();
91
92 if (now >= gd->tbl) {
93 /* normal mode */
94 gd->tbu += now - gd->tbl;
95 } else {
96 /* we have an overflow ... */
97 gd->tbu += now + TIMER_LOAD_VAL - gd->tbl;
98 }
99 gd->tbl = now;
100
101 return gd->tbu;
102}
103
Lei Wen43013032011-02-09 18:06:58 +0530104ulong get_timer(ulong base)
105{
106 return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
107 base);
108}
109
Lei Wen43013032011-02-09 18:06:58 +0530110void __udelay(unsigned long usec)
111{
112 ulong delayticks;
113 ulong endtime;
114
115 delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
116 endtime = get_timer_masked() + delayticks;
117
118 while (get_timer_masked() < endtime)
119 ;
120}
121
122/*
123 * init the Timer
124 */
125int timer_init(void)
126{
127 struct panthapb_registers *apb1clkres =
128 (struct panthapb_registers *) PANTHEON_APBC_BASE;
129 struct panthtmr_registers *panthtimers =
130 (struct panthtmr_registers *) PANTHEON_TIMER_BASE;
131
132 /* Enable Timer clock at 3.25 MHZ */
133 writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
134
135 /* load value into timer */
136 writel(0x0, &panthtimers->clk_ctrl);
137 /* Use Timer 0 Match Resiger 0 */
138 writel(TIMER_LOAD_VAL, &panthtimers->match[MATCH_CMP(0)]);
139 /* Preload value is 0 */
140 writel(0x0, &panthtimers->preload[TIMER]);
141 /* Enable match comparator 0 for Timer 0 */
142 writel(0x1, &panthtimers->preload_ctrl[TIMER]);
143
144 /* Enable timer 0 */
145 writel(0x1, &panthtimers->cer);
146 /* init the gd->tbu and gd->tbl value */
Graeme Russ944a7fe2011-07-15 02:21:14 +0000147 gd->tbl = read_timer();
148 gd->tbu = 0;
Lei Wen43013032011-02-09 18:06:58 +0530149
150 return 0;
151}
152
153#define MPMU_APRR_WDTR (1<<4)
154#define TMR_WFAR 0xbaba /* WDT Register First key */
155#define TMP_WSAR 0xeb10 /* WDT Register Second key */
156
157/*
158 * This function uses internal Watchdog Timer
159 * based reset mechanism.
160 * Steps to write watchdog registers (protected access)
161 * 1. Write key value to TMR_WFAR reg.
162 * 2. Write key value to TMP_WSAR reg.
163 * 3. Perform write operation.
164 */
165void reset_cpu (unsigned long ignored)
166{
167 struct panthmpmu_registers *mpmu =
168 (struct panthmpmu_registers *) PANTHEON_MPMU_BASE;
169 struct panthtmr_registers *panthtimers =
170 (struct panthtmr_registers *) PANTHEON_WD_TIMER_BASE;
171 u32 val;
172
173 /* negate hardware reset to the WDT after system reset */
174 val = readl(&mpmu->aprr);
175 val = val | MPMU_APRR_WDTR;
176 writel(val, &mpmu->aprr);
177
178 /* reset/enable WDT clock */
179 writel(APBC_APBCLK, &mpmu->wdtpcr);
180
181 /* clear previous WDT status */
182 writel(TMR_WFAR, &panthtimers->wfar);
183 writel(TMP_WSAR, &panthtimers->wsar);
184 writel(0, &panthtimers->wdt_sts);
185
186 /* set match counter */
187 writel(TMR_WFAR, &panthtimers->wfar);
188 writel(TMP_WSAR, &panthtimers->wsar);
189 writel(0xf, &panthtimers->wdt_match_r);
190
191 /* enable WDT reset */
192 writel(TMR_WFAR, &panthtimers->wfar);
193 writel(TMP_WSAR, &panthtimers->wsar);
194 writel(0x3, &panthtimers->wdt_match_en);
195
196 /*enable functional WDT clock */
197 writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
198}