blob: c78fd90f4fb9d3620a9c6ddc411a4653451347c8 [file] [log] [blame]
wdenka21ad7b2005-05-19 22:39:42 +00001/*
2 * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <asm/arch/platform.h>
25
26/*
27 * Handy KS8695 register access functions.
28 */
29#define ks8695_read(a) *((volatile ulong *) (KS8695_IO_BASE + (a)))
30#define ks8695_write(a,v) *((volatile ulong *) (KS8695_IO_BASE + (a))) = (v)
31
wdenka21ad7b2005-05-19 22:39:42 +000032ulong timer_ticks;
33
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020034int timer_init (void)
wdenka21ad7b2005-05-19 22:39:42 +000035{
Graeme Russ139d6cf2011-07-15 02:19:44 +000036 reset_timer_masked();
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020037
38 return 0;
wdenka21ad7b2005-05-19 22:39:42 +000039}
40
41/*
42 * Initial timer set constants. Nothing complicated, just set for a 1ms
43 * tick.
44 */
45#define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_1)
46#define TIMER_COUNT (TIMER_INTERVAL / 2)
47#define TIMER_PULSE TIMER_COUNT
48
49void reset_timer_masked(void)
50{
51 /* Set the hadware timer for 1ms */
52 ks8695_write(KS8695_TIMER1, TIMER_COUNT);
53 ks8695_write(KS8695_TIMER1_PCOUNT, TIMER_PULSE);
54 ks8695_write(KS8695_TIMER_CTRL, 0x2);
55 timer_ticks = 0;
wdenka21ad7b2005-05-19 22:39:42 +000056}
57
wdenka21ad7b2005-05-19 22:39:42 +000058ulong get_timer_masked(void)
59{
60 /* Check for timer wrap */
61 if (ks8695_read(KS8695_INT_STATUS) & KS8695_INTMASK_TIMERINT1) {
62 /* Clear interrupt condition */
63 ks8695_write(KS8695_INT_STATUS, KS8695_INTMASK_TIMERINT1);
64 timer_ticks++;
65 }
66 return timer_ticks;
67}
68
69ulong get_timer(ulong base)
70{
71 return (get_timer_masked() - base);
72}
73
Ingo van Lilf0f778a2009-11-24 14:09:21 +010074void __udelay(ulong usec)
wdenka21ad7b2005-05-19 22:39:42 +000075{
76 ulong start = get_timer_masked();
77 ulong end;
78
wdenka21ad7b2005-05-19 22:39:42 +000079 /* Only 1ms resolution :-( */
80 end = usec / 1000;
81 while (get_timer(start) < end)
82 ;
83}
84
85void reset_cpu (ulong ignored)
86{
87 ulong tc;
88
89 /* Set timer0 to watchdog, and let it timeout */
90 tc = ks8695_read(KS8695_TIMER_CTRL) & 0x2;
91 ks8695_write(KS8695_TIMER_CTRL, tc);
92 ks8695_write(KS8695_TIMER0, ((10 << 8) | 0xff));
93 ks8695_write(KS8695_TIMER_CTRL, (tc | 0x1));
94
95 /* Should only wait here till watchdog resets */
96 for (;;)
97 ;
98}