xref: /rk3399_rockchip-uboot/arch/powerpc/lib/interrupts.c (revision f3603b438283aa3e47b7bc564ded4b75c6ccf051)
1 /*
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  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #include <common.h>
12 #include <asm/processor.h>
13 #include <watchdog.h>
14 #ifdef CONFIG_LED_STATUS
15 #include <status_led.h>
16 #endif
17 
18 #ifdef CONFIG_SHOW_ACTIVITY
19 void board_show_activity (ulong) __attribute__((weak, alias("__board_show_activity")));
20 
21 void __board_show_activity (ulong dummy)
22 {
23 	return;
24 }
25 #endif /* CONFIG_SHOW_ACTIVITY */
26 
27 #ifndef CONFIG_SYS_WATCHDOG_FREQ
28 #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
29 #endif
30 
31 extern int interrupt_init_cpu (unsigned *);
32 extern void timer_interrupt_cpu (struct pt_regs *);
33 
34 static unsigned decrementer_count; /* count value for 1e6/HZ microseconds */
35 
36 static __inline__ unsigned long get_dec (void)
37 {
38 	unsigned long val;
39 
40 	asm volatile ("mfdec %0":"=r" (val):);
41 
42 	return val;
43 }
44 
45 
46 static __inline__ void set_dec (unsigned long val)
47 {
48 	if (val)
49 		asm volatile ("mtdec %0"::"r" (val));
50 }
51 
52 
53 void enable_interrupts (void)
54 {
55 	set_msr (get_msr () | MSR_EE);
56 }
57 
58 /* returns flag if MSR_EE was set before */
59 int disable_interrupts (void)
60 {
61 	ulong msr = get_msr ();
62 
63 	set_msr (msr & ~MSR_EE);
64 	return ((msr & MSR_EE) != 0);
65 }
66 
67 int interrupt_init (void)
68 {
69 	int ret;
70 
71 	/* call cpu specific function from $(CPU)/interrupts.c */
72 	ret = interrupt_init_cpu (&decrementer_count);
73 
74 	if (ret)
75 		return ret;
76 
77 	set_dec (decrementer_count);
78 
79 	set_msr (get_msr () | MSR_EE);
80 
81 	return (0);
82 }
83 
84 static volatile ulong timestamp = 0;
85 
86 void timer_interrupt (struct pt_regs *regs)
87 {
88 	/* call cpu specific function from $(CPU)/interrupts.c */
89 	timer_interrupt_cpu (regs);
90 
91 	/* Restore Decrementer Count */
92 	set_dec (decrementer_count);
93 
94 	timestamp++;
95 
96 #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
97 	if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0)
98 		WATCHDOG_RESET ();
99 #endif    /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
100 
101 #ifdef CONFIG_LED_STATUS
102 	status_led_tick (timestamp);
103 #endif /* CONFIG_LED_STATUS */
104 
105 #ifdef CONFIG_SHOW_ACTIVITY
106 	board_show_activity (timestamp);
107 #endif /* CONFIG_SHOW_ACTIVITY */
108 }
109 
110 ulong get_timer (ulong base)
111 {
112 	return (timestamp - base);
113 }
114