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