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> 7a1b2dde7SMarc Zyngier #include <linux/clocksource.h> 88a4da6e3SMark Rutland #include <linux/init.h> 9ec944c93SMark Rutland #include <linux/types.h> 10923df96bSWill Deacon 118a4da6e3SMark Rutland #include <clocksource/arm_arch_timer.h> 128a4da6e3SMark Rutland 13022c03a2SMarc Zyngier #ifdef CONFIG_ARM_ARCH_TIMER 145ef19a16SMarc Zyngier /* 32bit ARM doesn't know anything about timer errata... */ 155ef19a16SMarc Zyngier #define has_erratum_handler(h) (false) 165ef19a16SMarc Zyngier #define erratum_handler(h) (arch_timer_##h) 175ef19a16SMarc Zyngier 180583fe47SRob Herring int arch_timer_arch_init(void); 19ec944c93SMark Rutland 20ec944c93SMark Rutland /* 21ec944c93SMark Rutland * These register accessors are marked inline so the compiler can 22ec944c93SMark Rutland * nicely work out which register we want, and chuck away the rest of 23ec944c93SMark Rutland * the code. At least it does so with a recent GCC (4.6.3). 24ec944c93SMark Rutland */ 25e09f3cc0SStephen Boyd static __always_inline 2660faddf6SStephen Boyd void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val) 27ec944c93SMark Rutland { 28ec944c93SMark Rutland if (access == ARCH_TIMER_PHYS_ACCESS) { 29ec944c93SMark Rutland switch (reg) { 30ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 31ec944c93SMark Rutland asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val)); 32ec944c93SMark Rutland break; 33ec944c93SMark Rutland case ARCH_TIMER_REG_TVAL: 34ec944c93SMark Rutland asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val)); 35ec944c93SMark Rutland break; 36ec944c93SMark Rutland } 37e09f3cc0SStephen Boyd } else if (access == ARCH_TIMER_VIRT_ACCESS) { 38ec944c93SMark Rutland switch (reg) { 39ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 40ec944c93SMark Rutland asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val)); 41ec944c93SMark Rutland break; 42ec944c93SMark Rutland case ARCH_TIMER_REG_TVAL: 43ec944c93SMark Rutland asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val)); 44ec944c93SMark Rutland break; 45ec944c93SMark Rutland } 46ec944c93SMark Rutland } 4745801042SMark Rutland 4845801042SMark Rutland isb(); 49ec944c93SMark Rutland } 50ec944c93SMark Rutland 51e09f3cc0SStephen Boyd static __always_inline 5260faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg) 53ec944c93SMark Rutland { 54ec944c93SMark Rutland u32 val = 0; 55ec944c93SMark Rutland 56ec944c93SMark Rutland if (access == ARCH_TIMER_PHYS_ACCESS) { 57ec944c93SMark Rutland switch (reg) { 58ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 59ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val)); 60ec944c93SMark Rutland break; 61ec944c93SMark Rutland case ARCH_TIMER_REG_TVAL: 62ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val)); 63ec944c93SMark Rutland break; 64ec944c93SMark Rutland } 65e09f3cc0SStephen Boyd } else if (access == ARCH_TIMER_VIRT_ACCESS) { 66ec944c93SMark Rutland switch (reg) { 67ec944c93SMark Rutland case ARCH_TIMER_REG_CTRL: 68ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val)); 69ec944c93SMark Rutland break; 70ec944c93SMark Rutland case ARCH_TIMER_REG_TVAL: 71ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val)); 72ec944c93SMark Rutland break; 73ec944c93SMark Rutland } 74ec944c93SMark Rutland } 75ec944c93SMark Rutland 76ec944c93SMark Rutland return val; 77ec944c93SMark Rutland } 78ec944c93SMark Rutland 79ec944c93SMark Rutland static inline u32 arch_timer_get_cntfrq(void) 80ec944c93SMark Rutland { 81ec944c93SMark Rutland u32 val; 82ec944c93SMark Rutland asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val)); 83ec944c93SMark Rutland return val; 84ec944c93SMark Rutland } 85ec944c93SMark Rutland 86*0ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct(void) 870b46b8a7SSonny Rao { 880b46b8a7SSonny Rao u64 cval; 890b46b8a7SSonny Rao 900b46b8a7SSonny Rao isb(); 910b46b8a7SSonny Rao asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval)); 920b46b8a7SSonny Rao return cval; 930b46b8a7SSonny Rao } 940b46b8a7SSonny Rao 95*0ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct_stable(void) 96*0ea41539SMarc Zyngier { 97*0ea41539SMarc Zyngier return __arch_counter_get_cntpct(); 98*0ea41539SMarc Zyngier } 99*0ea41539SMarc Zyngier 100*0ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct(void) 101ec944c93SMark Rutland { 102ec944c93SMark Rutland u64 cval; 103ec944c93SMark Rutland 10445801042SMark Rutland isb(); 105ec944c93SMark Rutland asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval)); 106ec944c93SMark Rutland return cval; 107ec944c93SMark Rutland } 108b2deabe3SMark Rutland 109*0ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct_stable(void) 110*0ea41539SMarc Zyngier { 111*0ea41539SMarc Zyngier return __arch_counter_get_cntvct(); 112*0ea41539SMarc Zyngier } 113*0ea41539SMarc Zyngier 114e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void) 115b2deabe3SMark Rutland { 116b2deabe3SMark Rutland u32 cntkctl; 117b2deabe3SMark Rutland asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl)); 118e9faebc6SSudeep KarkadaNagesha return cntkctl; 119e9faebc6SSudeep KarkadaNagesha } 120e9faebc6SSudeep KarkadaNagesha 121e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl) 122e9faebc6SSudeep KarkadaNagesha { 123e9faebc6SSudeep KarkadaNagesha asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl)); 124ec5c8e42SJulien Thierry isb(); 125e9faebc6SSudeep KarkadaNagesha } 126e9faebc6SSudeep KarkadaNagesha 127022c03a2SMarc Zyngier #endif 128022c03a2SMarc Zyngier 129022c03a2SMarc Zyngier #endif 130