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> 75a354412SAndrew Murray #include <asm/hwcap.h> 8a1b2dde7SMarc Zyngier #include <linux/clocksource.h> 98a4da6e3SMark Rutland #include <linux/init.h> 10*8b82c4f8SMarc Zyngier #include <linux/io-64-nonatomic-lo-hi.h> 11ec944c93SMark Rutland #include <linux/types.h> 12923df96bSWill Deacon 138a4da6e3SMark Rutland #include <clocksource/arm_arch_timer.h> 148a4da6e3SMark Rutland 15022c03a2SMarc Zyngier #ifdef CONFIG_ARM_ARCH_TIMER 165ef19a16SMarc Zyngier /* 32bit ARM doesn't know anything about timer errata... */ 175ef19a16SMarc Zyngier #define has_erratum_handler(h) (false) 185ef19a16SMarc Zyngier #define erratum_handler(h) (arch_timer_##h) 195ef19a16SMarc Zyngier 200583fe47SRob Herring int arch_timer_arch_init(void); 21ec944c93SMark Rutland 22ec944c93SMark Rutland /* 23ec944c93SMark Rutland * These register accessors are marked inline so the compiler can 24ec944c93SMark Rutland * nicely work out which register we want, and chuck away the rest of 25ec944c93SMark Rutland * the code. At least it does so with a recent GCC (4.6.3). 26ec944c93SMark Rutland */ 27e09f3cc0SStephen Boyd static __always_inline 281e8d9292SMarc Zyngier void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u64 val) 29ec944c93SMark Rutland { 30ec944c93SMark Rutland if (access == ARCH_TIMER_PHYS_ACCESS) { 31ec944c93SMark Rutland switch (reg) { 32ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 331e8d9292SMarc Zyngier asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" ((u32)val)); 34ec944c93SMark Rutland break; 35a38b71b0SMarc Zyngier case ARCH_TIMER_REG_CVAL: 36a38b71b0SMarc Zyngier asm volatile("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val)); 37ec944c93SMark Rutland break; 384775bc63SMarc Zyngier default: 394775bc63SMarc Zyngier BUILD_BUG(); 40ec944c93SMark Rutland } 41e09f3cc0SStephen Boyd } else if (access == ARCH_TIMER_VIRT_ACCESS) { 42ec944c93SMark Rutland switch (reg) { 43ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 441e8d9292SMarc Zyngier asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" ((u32)val)); 45ec944c93SMark Rutland break; 46a38b71b0SMarc Zyngier case ARCH_TIMER_REG_CVAL: 47a38b71b0SMarc Zyngier asm volatile("mcrr p15, 3, %Q0, %R0, c14" : : "r" (val)); 48ec944c93SMark Rutland break; 494775bc63SMarc Zyngier default: 504775bc63SMarc Zyngier BUILD_BUG(); 51ec944c93SMark Rutland } 524775bc63SMarc Zyngier } else { 534775bc63SMarc Zyngier BUILD_BUG(); 54ec944c93SMark Rutland } 5545801042SMark Rutland 5645801042SMark Rutland isb(); 57ec944c93SMark Rutland } 58ec944c93SMark Rutland 59e09f3cc0SStephen Boyd static __always_inline 6060faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg) 61ec944c93SMark Rutland { 62ec944c93SMark Rutland u32 val = 0; 63ec944c93SMark Rutland 64ec944c93SMark Rutland if (access == ARCH_TIMER_PHYS_ACCESS) { 65ec944c93SMark Rutland switch (reg) { 66ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 67ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val)); 68ec944c93SMark Rutland break; 694775bc63SMarc Zyngier default: 704775bc63SMarc Zyngier BUILD_BUG(); 71ec944c93SMark Rutland } 72e09f3cc0SStephen Boyd } else if (access == ARCH_TIMER_VIRT_ACCESS) { 73ec944c93SMark Rutland switch (reg) { 74ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 75ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val)); 76ec944c93SMark Rutland break; 774775bc63SMarc Zyngier default: 784775bc63SMarc Zyngier BUILD_BUG(); 79ec944c93SMark Rutland } 804775bc63SMarc Zyngier } else { 814775bc63SMarc Zyngier BUILD_BUG(); 82ec944c93SMark Rutland } 83ec944c93SMark Rutland 84ec944c93SMark Rutland return val; 85ec944c93SMark Rutland } 86ec944c93SMark Rutland 87ec944c93SMark Rutland static inline u32 arch_timer_get_cntfrq(void) 88ec944c93SMark Rutland { 89ec944c93SMark Rutland u32 val; 90ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val)); 91ec944c93SMark Rutland return val; 92ec944c93SMark Rutland } 93ec944c93SMark Rutland 940ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct(void) 950b46b8a7SSonny Rao { 960b46b8a7SSonny Rao u64 cval; 970b46b8a7SSonny Rao 980b46b8a7SSonny Rao isb(); 990b46b8a7SSonny Rao asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval)); 1000b46b8a7SSonny Rao return cval; 1010b46b8a7SSonny Rao } 1020b46b8a7SSonny Rao 1030ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct_stable(void) 1040ea41539SMarc Zyngier { 1050ea41539SMarc Zyngier return __arch_counter_get_cntpct(); 1060ea41539SMarc Zyngier } 1070ea41539SMarc Zyngier 1080ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct(void) 109ec944c93SMark Rutland { 110ec944c93SMark Rutland u64 cval; 111ec944c93SMark Rutland 11245801042SMark Rutland isb(); 113ec944c93SMark Rutland asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval)); 114ec944c93SMark Rutland return cval; 115ec944c93SMark Rutland } 116b2deabe3SMark Rutland 1170ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct_stable(void) 1180ea41539SMarc Zyngier { 1190ea41539SMarc Zyngier return __arch_counter_get_cntvct(); 1200ea41539SMarc Zyngier } 1210ea41539SMarc Zyngier 122e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void) 123b2deabe3SMark Rutland { 124b2deabe3SMark Rutland u32 cntkctl; 125b2deabe3SMark Rutland asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl)); 126e9faebc6SSudeep KarkadaNagesha return cntkctl; 127e9faebc6SSudeep KarkadaNagesha } 128e9faebc6SSudeep KarkadaNagesha 129e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl) 130e9faebc6SSudeep KarkadaNagesha { 131e9faebc6SSudeep KarkadaNagesha asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl)); 132ec5c8e42SJulien Thierry isb(); 133e9faebc6SSudeep KarkadaNagesha } 134e9faebc6SSudeep KarkadaNagesha 1355a354412SAndrew Murray static inline void arch_timer_set_evtstrm_feature(void) 1365a354412SAndrew Murray { 1375a354412SAndrew Murray elf_hwcap |= HWCAP_EVTSTRM; 1385a354412SAndrew Murray } 1395a354412SAndrew Murray 1405a354412SAndrew Murray static inline bool arch_timer_have_evtstrm_feature(void) 1415a354412SAndrew Murray { 1425a354412SAndrew Murray return elf_hwcap & HWCAP_EVTSTRM; 1435a354412SAndrew Murray } 144022c03a2SMarc Zyngier #endif 145022c03a2SMarc Zyngier 146022c03a2SMarc Zyngier #endif 147