blob: 23db5572de55f2f3a94a8ab557d7f38913643760 [file] [log] [blame]
wdenka21ad7b2005-05-19 22:39:42 +00001/*
2 * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
wdenka21ad7b2005-05-19 22:39:42 +00005 */
6
7#include <common.h>
8#include <asm/arch/platform.h>
9
10/*
Greg Ungerer37da45d2011-09-09 22:19:10 +100011 * Initial timer set constants. Nothing complicated, just set for a 1ms
12 * tick.
13 */
14#define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_1)
15#define TIMER_COUNT (TIMER_INTERVAL / 2)
16#define TIMER_PULSE TIMER_COUNT
17
18/*
wdenka21ad7b2005-05-19 22:39:42 +000019 * Handy KS8695 register access functions.
20 */
21#define ks8695_read(a) *((volatile ulong *) (KS8695_IO_BASE + (a)))
22#define ks8695_write(a,v) *((volatile ulong *) (KS8695_IO_BASE + (a))) = (v)
23
wdenka21ad7b2005-05-19 22:39:42 +000024ulong timer_ticks;
25
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020026int timer_init (void)
wdenka21ad7b2005-05-19 22:39:42 +000027{
Graeme Russ944a7fe2011-07-15 02:21:14 +000028 /* Set the hadware timer for 1ms */
29 ks8695_write(KS8695_TIMER1, TIMER_COUNT);
30 ks8695_write(KS8695_TIMER1_PCOUNT, TIMER_PULSE);
31 ks8695_write(KS8695_TIMER_CTRL, 0x2);
32 timer_ticks = 0;
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020033
34 return 0;
wdenka21ad7b2005-05-19 22:39:42 +000035}
36
wdenka21ad7b2005-05-19 22:39:42 +000037ulong get_timer_masked(void)
38{
39 /* Check for timer wrap */
40 if (ks8695_read(KS8695_INT_STATUS) & KS8695_INTMASK_TIMERINT1) {
41 /* Clear interrupt condition */
42 ks8695_write(KS8695_INT_STATUS, KS8695_INTMASK_TIMERINT1);
43 timer_ticks++;
44 }
45 return timer_ticks;
46}
47
48ulong get_timer(ulong base)
49{
50 return (get_timer_masked() - base);
51}
52
Ingo van Lilf0f778a2009-11-24 14:09:21 +010053void __udelay(ulong usec)
wdenka21ad7b2005-05-19 22:39:42 +000054{
55 ulong start = get_timer_masked();
56 ulong end;
57
wdenka21ad7b2005-05-19 22:39:42 +000058 /* Only 1ms resolution :-( */
59 end = usec / 1000;
60 while (get_timer(start) < end)
61 ;
62}
63
64void reset_cpu (ulong ignored)
65{
66 ulong tc;
67
68 /* Set timer0 to watchdog, and let it timeout */
69 tc = ks8695_read(KS8695_TIMER_CTRL) & 0x2;
70 ks8695_write(KS8695_TIMER_CTRL, tc);
71 ks8695_write(KS8695_TIMER0, ((10 << 8) | 0xff));
72 ks8695_write(KS8695_TIMER_CTRL, (tc | 0x1));
73
74 /* Should only wait here till watchdog resets */
75 for (;;)
76 ;
77}