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 21*60faddf6SStephen 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 47*60faddf6SStephen 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 90b2deabe3SMark Rutland static inline void __cpuinit arch_counter_set_user_access(void) 91b2deabe3SMark Rutland { 92b2deabe3SMark Rutland u32 cntkctl; 93b2deabe3SMark Rutland 94b2deabe3SMark Rutland asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl)); 95b2deabe3SMark Rutland 96b2deabe3SMark Rutland /* disable user access to everything */ 97b2deabe3SMark Rutland cntkctl &= ~((3 << 8) | (7 << 0)); 98b2deabe3SMark Rutland 99b2deabe3SMark Rutland asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl)); 100b2deabe3SMark Rutland } 101022c03a2SMarc Zyngier #endif 102022c03a2SMarc Zyngier 103022c03a2SMarc Zyngier #endif 104