xref: /openbmc/linux/arch/loongarch/include/asm/time.h (revision 874c8ca1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #ifndef _ASM_TIME_H
6 #define _ASM_TIME_H
7 
8 #include <linux/clockchips.h>
9 #include <linux/clocksource.h>
10 #include <asm/loongarch.h>
11 
12 extern u64 cpu_clock_freq;
13 extern u64 const_clock_freq;
14 
15 extern void sync_counter(void);
16 
17 static inline unsigned int calc_const_freq(void)
18 {
19 	unsigned int res;
20 	unsigned int base_freq;
21 	unsigned int cfm, cfd;
22 
23 	res = read_cpucfg(LOONGARCH_CPUCFG2);
24 	if (!(res & CPUCFG2_LLFTP))
25 		return 0;
26 
27 	base_freq = read_cpucfg(LOONGARCH_CPUCFG4);
28 	res = read_cpucfg(LOONGARCH_CPUCFG5);
29 	cfm = res & 0xffff;
30 	cfd = (res >> 16) & 0xffff;
31 
32 	if (!base_freq || !cfm || !cfd)
33 		return 0;
34 
35 	return (base_freq * cfm / cfd);
36 }
37 
38 /*
39  * Initialize the calling CPU's timer interrupt as clockevent device
40  */
41 extern int constant_clockevent_init(void);
42 extern int constant_clocksource_init(void);
43 
44 static inline void clockevent_set_clock(struct clock_event_device *cd,
45 					unsigned int clock)
46 {
47 	clockevents_calc_mult_shift(cd, clock, 4);
48 }
49 
50 #endif /* _ASM_TIME_H */
51