xref: /linux/arch/arm/include/asm/arch_timer.h (revision 5a354412567d7de81d69b6ac61c3b7fcebbe497e)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2022c03a2SMarc Zyngier #ifndef __ASMARM_ARCH_TIMER_H
3022c03a2SMarc Zyngier #define __ASMARM_ARCH_TIMER_H
4022c03a2SMarc Zyngier 
5ec944c93SMark Rutland #include <asm/barrier.h>
6923df96bSWill Deacon #include <asm/errno.h>
7*5a354412SAndrew Murray #include <asm/hwcap.h>
8a1b2dde7SMarc Zyngier #include <linux/clocksource.h>
98a4da6e3SMark Rutland #include <linux/init.h>
10ec944c93SMark Rutland #include <linux/types.h>
11923df96bSWill Deacon 
128a4da6e3SMark Rutland #include <clocksource/arm_arch_timer.h>
138a4da6e3SMark Rutland 
14022c03a2SMarc Zyngier #ifdef CONFIG_ARM_ARCH_TIMER
155ef19a16SMarc Zyngier /* 32bit ARM doesn't know anything about timer errata... */
165ef19a16SMarc Zyngier #define has_erratum_handler(h)		(false)
175ef19a16SMarc Zyngier #define erratum_handler(h)		(arch_timer_##h)
185ef19a16SMarc Zyngier 
190583fe47SRob Herring int arch_timer_arch_init(void);
20ec944c93SMark Rutland 
21ec944c93SMark Rutland /*
22ec944c93SMark Rutland  * These register accessors are marked inline so the compiler can
23ec944c93SMark Rutland  * nicely work out which register we want, and chuck away the rest of
24ec944c93SMark Rutland  * the code. At least it does so with a recent GCC (4.6.3).
25ec944c93SMark Rutland  */
26e09f3cc0SStephen Boyd static __always_inline
2760faddf6SStephen Boyd void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val)
28ec944c93SMark Rutland {
29ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
30ec944c93SMark Rutland 		switch (reg) {
31ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
32ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
33ec944c93SMark Rutland 			break;
34ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
35ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
36ec944c93SMark Rutland 			break;
37ec944c93SMark Rutland 		}
38e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
39ec944c93SMark Rutland 		switch (reg) {
40ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
41ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val));
42ec944c93SMark Rutland 			break;
43ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
44ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val));
45ec944c93SMark Rutland 			break;
46ec944c93SMark Rutland 		}
47ec944c93SMark Rutland 	}
4845801042SMark Rutland 
4945801042SMark Rutland 	isb();
50ec944c93SMark Rutland }
51ec944c93SMark Rutland 
52e09f3cc0SStephen Boyd static __always_inline
5360faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
54ec944c93SMark Rutland {
55ec944c93SMark Rutland 	u32 val = 0;
56ec944c93SMark Rutland 
57ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
58ec944c93SMark Rutland 		switch (reg) {
59ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
60ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
61ec944c93SMark Rutland 			break;
62ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
63ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
64ec944c93SMark Rutland 			break;
65ec944c93SMark Rutland 		}
66e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
67ec944c93SMark Rutland 		switch (reg) {
68ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
69ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
70ec944c93SMark Rutland 			break;
71ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
72ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val));
73ec944c93SMark Rutland 			break;
74ec944c93SMark Rutland 		}
75ec944c93SMark Rutland 	}
76ec944c93SMark Rutland 
77ec944c93SMark Rutland 	return val;
78ec944c93SMark Rutland }
79ec944c93SMark Rutland 
80ec944c93SMark Rutland static inline u32 arch_timer_get_cntfrq(void)
81ec944c93SMark Rutland {
82ec944c93SMark Rutland 	u32 val;
83ec944c93SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
84ec944c93SMark Rutland 	return val;
85ec944c93SMark Rutland }
86ec944c93SMark Rutland 
870ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct(void)
880b46b8a7SSonny Rao {
890b46b8a7SSonny Rao 	u64 cval;
900b46b8a7SSonny Rao 
910b46b8a7SSonny Rao 	isb();
920b46b8a7SSonny Rao 	asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
930b46b8a7SSonny Rao 	return cval;
940b46b8a7SSonny Rao }
950b46b8a7SSonny Rao 
960ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct_stable(void)
970ea41539SMarc Zyngier {
980ea41539SMarc Zyngier 	return __arch_counter_get_cntpct();
990ea41539SMarc Zyngier }
1000ea41539SMarc Zyngier 
1010ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct(void)
102ec944c93SMark Rutland {
103ec944c93SMark Rutland 	u64 cval;
104ec944c93SMark Rutland 
10545801042SMark Rutland 	isb();
106ec944c93SMark Rutland 	asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
107ec944c93SMark Rutland 	return cval;
108ec944c93SMark Rutland }
109b2deabe3SMark Rutland 
1100ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct_stable(void)
1110ea41539SMarc Zyngier {
1120ea41539SMarc Zyngier 	return __arch_counter_get_cntvct();
1130ea41539SMarc Zyngier }
1140ea41539SMarc Zyngier 
115e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void)
116b2deabe3SMark Rutland {
117b2deabe3SMark Rutland 	u32 cntkctl;
118b2deabe3SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl));
119e9faebc6SSudeep KarkadaNagesha 	return cntkctl;
120e9faebc6SSudeep KarkadaNagesha }
121e9faebc6SSudeep KarkadaNagesha 
122e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl)
123e9faebc6SSudeep KarkadaNagesha {
124e9faebc6SSudeep KarkadaNagesha 	asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl));
125ec5c8e42SJulien Thierry 	isb();
126e9faebc6SSudeep KarkadaNagesha }
127e9faebc6SSudeep KarkadaNagesha 
128*5a354412SAndrew Murray static inline void arch_timer_set_evtstrm_feature(void)
129*5a354412SAndrew Murray {
130*5a354412SAndrew Murray 	elf_hwcap |= HWCAP_EVTSTRM;
131*5a354412SAndrew Murray }
132*5a354412SAndrew Murray 
133*5a354412SAndrew Murray static inline bool arch_timer_have_evtstrm_feature(void)
134*5a354412SAndrew Murray {
135*5a354412SAndrew Murray 	return elf_hwcap & HWCAP_EVTSTRM;
136*5a354412SAndrew Murray }
137022c03a2SMarc Zyngier #endif
138022c03a2SMarc Zyngier 
139022c03a2SMarc Zyngier #endif
140