1 /*
2  * Common time prototypes and such for all ppc machines.
3  *
4  * Written by Cort Dougan (cort@fsmlabs.com) to merge
5  * Paul Mackerras' version and mine for PReP and Pmac.
6  */
7 
8 #ifdef __KERNEL__
9 #ifndef __ASM_TIME_H__
10 #define __ASM_TIME_H__
11 
12 #include <linux/config.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/threads.h>
15 #include <linux/compiler.h>
16 
17 #include <asm/processor.h>
18 
19 /* time.c */
20 extern unsigned tb_ticks_per_jiffy;
21 extern unsigned tb_to_us;
22 extern unsigned tb_last_stamp;
23 extern unsigned long disarm_decr[NR_CPUS];
24 
25 extern void to_tm(int tim, struct rtc_time * tm);
26 extern time_t last_rtc_update;
27 
28 extern void set_dec_cpu6(unsigned int val);
29 
30 int via_calibrate_decr(void);
31 
32 /* Accessor functions for the decrementer register.
33  * The 40x doesn't even have a decrementer.  I tried to use the
34  * generic timer interrupt code, which seems OK, with the 40x PIT
35  * in auto-reload mode.  The problem is PIT stops counting when it
36  * hits zero.  If it would wrap, we could use it just like a decrementer.
37  */
get_dec(void)38 static __inline__ unsigned int get_dec(void)
39 {
40 #if defined(CONFIG_40x)
41 	return (mfspr(SPRN_PIT));
42 #else
43 	return (mfspr(SPRN_DEC));
44 #endif
45 }
46 
set_dec(unsigned int val)47 static __inline__ void set_dec(unsigned int val)
48 {
49 #if defined(CONFIG_40x)
50 	return;		/* Have to let it auto-reload */
51 #elif defined(CONFIG_8xx_CPU6)
52 	set_dec_cpu6(val);
53 #else
54 	mtspr(SPRN_DEC, val);
55 #endif
56 }
57 
58 /* Accessor functions for the timebase (RTC on 601) registers. */
59 /* If one day CONFIG_POWER is added just define __USE_RTC as 1 */
60 #ifdef CONFIG_6xx
__USE_RTC(void)61 extern __inline__ int __attribute_const__ __USE_RTC(void) {
62 	return (mfspr(SPRN_PVR)>>16) == 1;
63 }
64 #else
65 #define __USE_RTC() 0
66 #endif
67 
get_tbl(void)68 extern __inline__ unsigned long get_tbl(void) {
69 	unsigned long tbl;
70 	asm volatile("mftb %0" : "=r" (tbl));
71 	return tbl;
72 }
73 
get_tbu(void)74 extern __inline__ unsigned long get_tbu(void) {
75 	unsigned long tbl;
76 	asm volatile("mftbu %0" : "=r" (tbl));
77 	return tbl;
78 }
79 
set_tb(unsigned int upper,unsigned int lower)80 extern __inline__ void set_tb(unsigned int upper, unsigned int lower)
81 {
82 	mtspr(SPRN_TBWL, 0);
83 	mtspr(SPRN_TBWU, upper);
84 	mtspr(SPRN_TBWL, lower);
85 }
86 
get_rtcl(void)87 extern __inline__ unsigned long get_rtcl(void) {
88 	unsigned long rtcl;
89 	asm volatile("mfrtcl %0" : "=r" (rtcl));
90 	return rtcl;
91 }
92 
get_native_tbl(void)93 extern __inline__ unsigned get_native_tbl(void) {
94 	if (__USE_RTC())
95 		return get_rtcl();
96 	else
97 	  	return get_tbl();
98 }
99 
100 /* On machines with RTC, this function can only be used safely
101  * after the timestamp and for 1 second. It is only used by gettimeofday
102  * however so it should not matter.
103  */
tb_ticks_since(unsigned tstamp)104 extern __inline__ unsigned tb_ticks_since(unsigned tstamp) {
105 	if (__USE_RTC()) {
106 		int delta = get_rtcl() - tstamp;
107 		return delta<0 ? delta + 1000000000 : delta;
108 	} else {
109         	return get_tbl() - tstamp;
110 	}
111 }
112 
113 #if 0
114 extern __inline__ unsigned long get_bin_rtcl(void) {
115       unsigned long rtcl, rtcu1, rtcu2;
116       asm volatile("\
117 1:    mfrtcu  %0\n\
118       mfrtcl  %1\n\
119       mfrtcu  %2\n\
120       cmpw    %0,%2\n\
121       bne-    1b\n"
122       : "=r" (rtcu1), "=r" (rtcl), "=r" (rtcu2)
123       : : "cr0");
124       return rtcu2*1000000000+rtcl;
125 }
126 
127 extern __inline__ unsigned binary_tbl(void) {
128       if (__USE_RTC())
129               return get_bin_rtcl();
130       else
131               return get_tbl();
132 }
133 #endif
134 
135 /* Use mulhwu to scale processor timebase to timeval */
136 #define mulhwu(x,y) \
137 ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
138 
139 unsigned mulhwu_scale_factor(unsigned, unsigned);
140 #endif /* __ASM_TIME_H__ */
141 #endif /* __KERNEL__ */
142