xref: /linux/arch/arm/include/asm/arch_timer.h (revision e9faebc66ec74f1ab7f267d683b45e80faa69763)
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 
81ec944c93SMark Rutland static inline u64 arch_counter_get_cntvct(void)
82ec944c93SMark Rutland {
83ec944c93SMark Rutland 	u64 cval;
84ec944c93SMark Rutland 
8545801042SMark Rutland 	isb();
86ec944c93SMark Rutland 	asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
87ec944c93SMark Rutland 	return cval;
88ec944c93SMark Rutland }
89b2deabe3SMark Rutland 
90*e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void)
91b2deabe3SMark Rutland {
92b2deabe3SMark Rutland 	u32 cntkctl;
93b2deabe3SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl));
94*e9faebc6SSudeep KarkadaNagesha 	return cntkctl;
95*e9faebc6SSudeep KarkadaNagesha }
96*e9faebc6SSudeep KarkadaNagesha 
97*e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl)
98*e9faebc6SSudeep KarkadaNagesha {
99*e9faebc6SSudeep KarkadaNagesha 	asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl));
100*e9faebc6SSudeep KarkadaNagesha }
101*e9faebc6SSudeep KarkadaNagesha 
102*e9faebc6SSudeep KarkadaNagesha static inline void arch_counter_set_user_access(void)
103*e9faebc6SSudeep KarkadaNagesha {
104*e9faebc6SSudeep KarkadaNagesha 	u32 cntkctl = arch_timer_get_cntkctl();
105b2deabe3SMark Rutland 
10628061758SSudeep KarkadaNagesha 	/* Disable user access to both physical/virtual counters/timers */
10728061758SSudeep KarkadaNagesha 	/* Also disable virtual event stream */
10828061758SSudeep KarkadaNagesha 	cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
10928061758SSudeep KarkadaNagesha 			| ARCH_TIMER_USR_VT_ACCESS_EN
11028061758SSudeep KarkadaNagesha 			| ARCH_TIMER_VIRT_EVT_EN
11128061758SSudeep KarkadaNagesha 			| ARCH_TIMER_USR_VCT_ACCESS_EN
11228061758SSudeep KarkadaNagesha 			| ARCH_TIMER_USR_PCT_ACCESS_EN);
113*e9faebc6SSudeep KarkadaNagesha 	arch_timer_set_cntkctl(cntkctl);
114b2deabe3SMark Rutland }
115*e9faebc6SSudeep KarkadaNagesha 
116*e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_evtstrm_enable(int divider)
117*e9faebc6SSudeep KarkadaNagesha {
118*e9faebc6SSudeep KarkadaNagesha 	u32 cntkctl = arch_timer_get_cntkctl();
119*e9faebc6SSudeep KarkadaNagesha 	cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
120*e9faebc6SSudeep KarkadaNagesha 	/* Set the divider and enable virtual event stream */
121*e9faebc6SSudeep KarkadaNagesha 	cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
122*e9faebc6SSudeep KarkadaNagesha 			| ARCH_TIMER_VIRT_EVT_EN;
123*e9faebc6SSudeep KarkadaNagesha 	arch_timer_set_cntkctl(cntkctl);
124*e9faebc6SSudeep KarkadaNagesha 	elf_hwcap |= HWCAP_EVTSTRM;
125*e9faebc6SSudeep KarkadaNagesha }
126*e9faebc6SSudeep KarkadaNagesha 
127022c03a2SMarc Zyngier #endif
128022c03a2SMarc Zyngier 
129022c03a2SMarc Zyngier #endif
130