xref: /linux/arch/arm/include/asm/arch_timer.h (revision ec5c8e429d07737ee94ee1fd2ae5029547484194)
1022c03a2SMarc Zyngier #ifndef __ASMARM_ARCH_TIMER_H
2022c03a2SMarc Zyngier #define __ASMARM_ARCH_TIMER_H
3022c03a2SMarc Zyngier 
4ec944c93SMark Rutland #include <asm/barrier.h>
5923df96bSWill Deacon #include <asm/errno.h>
6a1b2dde7SMarc Zyngier #include <linux/clocksource.h>
78a4da6e3SMark Rutland #include <linux/init.h>
8ec944c93SMark Rutland #include <linux/types.h>
9923df96bSWill Deacon 
108a4da6e3SMark Rutland #include <clocksource/arm_arch_timer.h>
118a4da6e3SMark Rutland 
12022c03a2SMarc Zyngier #ifdef CONFIG_ARM_ARCH_TIMER
130583fe47SRob Herring int arch_timer_arch_init(void);
14ec944c93SMark Rutland 
15ec944c93SMark Rutland /*
16ec944c93SMark Rutland  * These register accessors are marked inline so the compiler can
17ec944c93SMark Rutland  * nicely work out which register we want, and chuck away the rest of
18ec944c93SMark Rutland  * the code. At least it does so with a recent GCC (4.6.3).
19ec944c93SMark Rutland  */
20e09f3cc0SStephen Boyd static __always_inline
2160faddf6SStephen Boyd void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val)
22ec944c93SMark Rutland {
23ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
24ec944c93SMark Rutland 		switch (reg) {
25ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
26ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
27ec944c93SMark Rutland 			break;
28ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
29ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
30ec944c93SMark Rutland 			break;
31ec944c93SMark Rutland 		}
32e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
33ec944c93SMark Rutland 		switch (reg) {
34ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
35ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val));
36ec944c93SMark Rutland 			break;
37ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
38ec944c93SMark Rutland 			asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val));
39ec944c93SMark Rutland 			break;
40ec944c93SMark Rutland 		}
41ec944c93SMark Rutland 	}
4245801042SMark Rutland 
4345801042SMark Rutland 	isb();
44ec944c93SMark Rutland }
45ec944c93SMark Rutland 
46e09f3cc0SStephen Boyd static __always_inline
4760faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
48ec944c93SMark Rutland {
49ec944c93SMark Rutland 	u32 val = 0;
50ec944c93SMark Rutland 
51ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
52ec944c93SMark Rutland 		switch (reg) {
53ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
54ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
55ec944c93SMark Rutland 			break;
56ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
57ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
58ec944c93SMark Rutland 			break;
59ec944c93SMark Rutland 		}
60e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
61ec944c93SMark Rutland 		switch (reg) {
62ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
63ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
64ec944c93SMark Rutland 			break;
65ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
66ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val));
67ec944c93SMark Rutland 			break;
68ec944c93SMark Rutland 		}
69ec944c93SMark Rutland 	}
70ec944c93SMark Rutland 
71ec944c93SMark Rutland 	return val;
72ec944c93SMark Rutland }
73ec944c93SMark Rutland 
74ec944c93SMark Rutland static inline u32 arch_timer_get_cntfrq(void)
75ec944c93SMark Rutland {
76ec944c93SMark Rutland 	u32 val;
77ec944c93SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
78ec944c93SMark Rutland 	return val;
79ec944c93SMark Rutland }
80ec944c93SMark Rutland 
810b46b8a7SSonny Rao static inline u64 arch_counter_get_cntpct(void)
820b46b8a7SSonny Rao {
830b46b8a7SSonny Rao 	u64 cval;
840b46b8a7SSonny Rao 
850b46b8a7SSonny Rao 	isb();
860b46b8a7SSonny Rao 	asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
870b46b8a7SSonny Rao 	return cval;
880b46b8a7SSonny Rao }
890b46b8a7SSonny Rao 
90ec944c93SMark Rutland static inline u64 arch_counter_get_cntvct(void)
91ec944c93SMark Rutland {
92ec944c93SMark Rutland 	u64 cval;
93ec944c93SMark Rutland 
9445801042SMark Rutland 	isb();
95ec944c93SMark Rutland 	asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
96ec944c93SMark Rutland 	return cval;
97ec944c93SMark Rutland }
98b2deabe3SMark Rutland 
99e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void)
100b2deabe3SMark Rutland {
101b2deabe3SMark Rutland 	u32 cntkctl;
102b2deabe3SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl));
103e9faebc6SSudeep KarkadaNagesha 	return cntkctl;
104e9faebc6SSudeep KarkadaNagesha }
105e9faebc6SSudeep KarkadaNagesha 
106e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl)
107e9faebc6SSudeep KarkadaNagesha {
108e9faebc6SSudeep KarkadaNagesha 	asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl));
109*ec5c8e42SJulien Thierry 	isb();
110e9faebc6SSudeep KarkadaNagesha }
111e9faebc6SSudeep KarkadaNagesha 
112022c03a2SMarc Zyngier #endif
113022c03a2SMarc Zyngier 
114022c03a2SMarc Zyngier #endif
115