blob: b803952badf2c88511d83dab960149959cc75d1b [file] [log] [blame]
wdenka8c7c702003-12-06 19:49:23 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2003
6 * Gleb Natapov <gnatapov@mrv.com>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <asm/processor.h>
29#include <watchdog.h>
30#ifdef CONFIG_STATUS_LED
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_SHOW_ACTIVITY
35 extern void board_show_activity (ulong);
36#endif /* CONFIG_SHOW_ACTIVITY */
37
38#ifndef CFG_WATCHDOG_FREQ
39#define CFG_WATCHDOG_FREQ (CFG_HZ / 2)
40#endif
41
42extern int interrupt_init_cpu (unsigned *);
43extern void timer_interrupt_cpu (struct pt_regs *);
44
45static unsigned decrementer_count; /* count value for 1e6/HZ microseconds */
46
47static __inline__ unsigned long get_msr (void)
48{
wdenkd4ca31c2004-01-02 14:00:00 +000049 unsigned long msr;
wdenka8c7c702003-12-06 19:49:23 +000050
wdenkd4ca31c2004-01-02 14:00:00 +000051 asm volatile ("mfmsr %0":"=r" (msr):);
wdenka8c7c702003-12-06 19:49:23 +000052
wdenkd4ca31c2004-01-02 14:00:00 +000053 return msr;
wdenka8c7c702003-12-06 19:49:23 +000054}
55
56static __inline__ void set_msr (unsigned long msr)
57{
58 asm volatile ("mtmsr %0"::"r" (msr));
59}
60
61static __inline__ unsigned long get_dec (void)
62{
63 unsigned long val;
64
65 asm volatile ("mfdec %0":"=r" (val):);
66
67 return val;
68}
69
70
71static __inline__ void set_dec (unsigned long val)
72{
73 if (val)
74 asm volatile ("mtdec %0"::"r" (val));
75}
76
77
78void enable_interrupts (void)
79{
80 set_msr (get_msr () | MSR_EE);
81}
82
83/* returns flag if MSR_EE was set before */
84int disable_interrupts (void)
85{
86 ulong msr = get_msr ();
87
88 set_msr (msr & ~MSR_EE);
89 return ((msr & MSR_EE) != 0);
90}
91
92int interrupt_init (void)
93{
94 int ret;
95
wdenkd4ca31c2004-01-02 14:00:00 +000096 /* call cpu specific function from $(CPU)/interrupts.c */
wdenka8c7c702003-12-06 19:49:23 +000097 ret = interrupt_init_cpu (&decrementer_count);
98
99 if (ret)
100 return ret;
101
102 set_dec (decrementer_count);
103
104 set_msr (get_msr () | MSR_EE);
105
106 return (0);
107}
108
109static volatile ulong timestamp = 0;
110
111void timer_interrupt (struct pt_regs *regs)
112{
113 /* call cpu specific function from $(CPU)/interrupts.c */
114 timer_interrupt_cpu (regs);
115
116 /* Restore Decrementer Count */
117 set_dec (decrementer_count);
118
119 timestamp++;
120
121#if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
122 if ((timestamp % (CFG_WATCHDOG_FREQ)) == 0)
123 WATCHDOG_RESET ();
124#endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
125
126#ifdef CONFIG_STATUS_LED
wdenkd4ca31c2004-01-02 14:00:00 +0000127 status_led_tick (timestamp);
wdenka8c7c702003-12-06 19:49:23 +0000128#endif /* CONFIG_STATUS_LED */
129
130#ifdef CONFIG_SHOW_ACTIVITY
131 board_show_activity (timestamp);
132#endif /* CONFIG_SHOW_ACTIVITY */
133}
134
135void reset_timer (void)
136{
137 timestamp = 0;
138}
139
140ulong get_timer (ulong base)
141{
142 return (timestamp - base);
143}
144
145void set_timer (ulong t)
146{
147 timestamp = t;
148}