Marc Zyngier | 022c03a | 2012-01-11 17:25:17 +0000 | [diff] [blame] | 1 | #ifndef __ASMARM_ARCH_TIMER_H |
| 2 | #define __ASMARM_ARCH_TIMER_H |
| 3 | |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 4 | #include <asm/barrier.h> |
Will Deacon | 923df96b | 2012-07-06 15:46:45 +0100 | [diff] [blame] | 5 | #include <asm/errno.h> |
Marc Zyngier | a1b2dde | 2012-09-07 18:09:58 +0100 | [diff] [blame] | 6 | #include <linux/clocksource.h> |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 7 | #include <linux/init.h> |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 8 | #include <linux/types.h> |
Will Deacon | 923df96b | 2012-07-06 15:46:45 +0100 | [diff] [blame] | 9 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 10 | #include <clocksource/arm_arch_timer.h> |
| 11 | |
Marc Zyngier | 022c03a | 2012-01-11 17:25:17 +0000 | [diff] [blame] | 12 | #ifdef CONFIG_ARM_ARCH_TIMER |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 13 | int arch_timer_arch_init(void); |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * These register accessors are marked inline so the compiler can |
| 17 | * nicely work out which register we want, and chuck away the rest of |
| 18 | * the code. At least it does so with a recent GCC (4.6.3). |
| 19 | */ |
Stephen Boyd | e09f3cc | 2013-07-18 16:59:28 -0700 | [diff] [blame] | 20 | static __always_inline |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 21 | void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val) |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 22 | { |
| 23 | if (access == ARCH_TIMER_PHYS_ACCESS) { |
| 24 | switch (reg) { |
| 25 | case ARCH_TIMER_REG_CTRL: |
| 26 | asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val)); |
| 27 | break; |
| 28 | case ARCH_TIMER_REG_TVAL: |
| 29 | asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val)); |
| 30 | break; |
| 31 | } |
Stephen Boyd | e09f3cc | 2013-07-18 16:59:28 -0700 | [diff] [blame] | 32 | } else if (access == ARCH_TIMER_VIRT_ACCESS) { |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 33 | switch (reg) { |
| 34 | case ARCH_TIMER_REG_CTRL: |
| 35 | asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val)); |
| 36 | break; |
| 37 | case ARCH_TIMER_REG_TVAL: |
| 38 | asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val)); |
| 39 | break; |
| 40 | } |
| 41 | } |
Mark Rutland | 4580104 | 2013-01-11 14:32:33 +0000 | [diff] [blame] | 42 | |
| 43 | isb(); |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Stephen Boyd | e09f3cc | 2013-07-18 16:59:28 -0700 | [diff] [blame] | 46 | static __always_inline |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 47 | u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg) |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 48 | { |
| 49 | u32 val = 0; |
| 50 | |
| 51 | if (access == ARCH_TIMER_PHYS_ACCESS) { |
| 52 | switch (reg) { |
| 53 | case ARCH_TIMER_REG_CTRL: |
| 54 | asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val)); |
| 55 | break; |
| 56 | case ARCH_TIMER_REG_TVAL: |
| 57 | asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val)); |
| 58 | break; |
| 59 | } |
Stephen Boyd | e09f3cc | 2013-07-18 16:59:28 -0700 | [diff] [blame] | 60 | } else if (access == ARCH_TIMER_VIRT_ACCESS) { |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 61 | switch (reg) { |
| 62 | case ARCH_TIMER_REG_CTRL: |
| 63 | asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val)); |
| 64 | break; |
| 65 | case ARCH_TIMER_REG_TVAL: |
| 66 | asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val)); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return val; |
| 72 | } |
| 73 | |
| 74 | static inline u32 arch_timer_get_cntfrq(void) |
| 75 | { |
| 76 | u32 val; |
| 77 | asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val)); |
| 78 | return val; |
| 79 | } |
| 80 | |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 81 | static inline u64 arch_counter_get_cntvct(void) |
| 82 | { |
| 83 | u64 cval; |
| 84 | |
Mark Rutland | 4580104 | 2013-01-11 14:32:33 +0000 | [diff] [blame] | 85 | isb(); |
Mark Rutland | ec944c9 | 2012-11-12 16:18:00 +0000 | [diff] [blame] | 86 | asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval)); |
| 87 | return cval; |
| 88 | } |
Mark Rutland | b2deabe | 2012-11-14 10:32:24 +0000 | [diff] [blame] | 89 | |
Sudeep KarkadaNagesha | e9faebc | 2013-08-13 14:30:32 +0100 | [diff] [blame] | 90 | static inline u32 arch_timer_get_cntkctl(void) |
Mark Rutland | b2deabe | 2012-11-14 10:32:24 +0000 | [diff] [blame] | 91 | { |
| 92 | u32 cntkctl; |
Mark Rutland | b2deabe | 2012-11-14 10:32:24 +0000 | [diff] [blame] | 93 | asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl)); |
Sudeep KarkadaNagesha | e9faebc | 2013-08-13 14:30:32 +0100 | [diff] [blame] | 94 | return cntkctl; |
| 95 | } |
| 96 | |
| 97 | static inline void arch_timer_set_cntkctl(u32 cntkctl) |
| 98 | { |
| 99 | asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl)); |
| 100 | } |
| 101 | |
| 102 | static inline void arch_counter_set_user_access(void) |
| 103 | { |
| 104 | u32 cntkctl = arch_timer_get_cntkctl(); |
Mark Rutland | b2deabe | 2012-11-14 10:32:24 +0000 | [diff] [blame] | 105 | |
Sudeep KarkadaNagesha | 2806175 | 2013-08-13 13:43:26 +0100 | [diff] [blame] | 106 | /* Disable user access to both physical/virtual counters/timers */ |
| 107 | /* Also disable virtual event stream */ |
| 108 | cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN |
| 109 | | ARCH_TIMER_USR_VT_ACCESS_EN |
| 110 | | ARCH_TIMER_VIRT_EVT_EN |
| 111 | | ARCH_TIMER_USR_VCT_ACCESS_EN |
| 112 | | ARCH_TIMER_USR_PCT_ACCESS_EN); |
Sudeep KarkadaNagesha | e9faebc | 2013-08-13 14:30:32 +0100 | [diff] [blame] | 113 | arch_timer_set_cntkctl(cntkctl); |
Mark Rutland | b2deabe | 2012-11-14 10:32:24 +0000 | [diff] [blame] | 114 | } |
Sudeep KarkadaNagesha | e9faebc | 2013-08-13 14:30:32 +0100 | [diff] [blame] | 115 | |
| 116 | static inline void arch_timer_evtstrm_enable(int divider) |
| 117 | { |
| 118 | u32 cntkctl = arch_timer_get_cntkctl(); |
| 119 | cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK; |
| 120 | /* Set the divider and enable virtual event stream */ |
| 121 | cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT) |
| 122 | | ARCH_TIMER_VIRT_EVT_EN; |
| 123 | arch_timer_set_cntkctl(cntkctl); |
| 124 | elf_hwcap |= HWCAP_EVTSTRM; |
| 125 | } |
| 126 | |
Marc Zyngier | 022c03a | 2012-01-11 17:25:17 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | #endif |