Marc Zyngier | f5b3b2b | 2011-11-07 14:28:33 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * linux/arch/arm/kernel/arch_timer.c |
| 3 | * |
| 4 | * Copyright (C) 2011 ARM Ltd. |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/jiffies.h> |
| 18 | #include <linux/clockchips.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | |
| 22 | #include <asm/cputype.h> |
| 23 | #include <asm/hardware/gic.h> |
| 24 | |
| 25 | static unsigned long arch_timer_rate; |
| 26 | static int arch_timer_ppi; |
| 27 | static int arch_timer_ppi2; |
| 28 | |
| 29 | static struct clock_event_device __percpu *arch_timer_evt; |
| 30 | |
| 31 | /* |
| 32 | * Architected system timer support. |
| 33 | */ |
| 34 | |
| 35 | #define ARCH_TIMER_CTRL_ENABLE (1 << 0) |
| 36 | #define ARCH_TIMER_CTRL_IT_MASK (1 << 1) |
| 37 | |
| 38 | #define ARCH_TIMER_REG_CTRL 0 |
| 39 | #define ARCH_TIMER_REG_FREQ 1 |
| 40 | #define ARCH_TIMER_REG_TVAL 2 |
| 41 | |
| 42 | static void arch_timer_reg_write(int reg, u32 val) |
| 43 | { |
| 44 | switch (reg) { |
| 45 | case ARCH_TIMER_REG_CTRL: |
| 46 | asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val)); |
| 47 | break; |
| 48 | case ARCH_TIMER_REG_TVAL: |
| 49 | asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val)); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | isb(); |
| 54 | } |
| 55 | |
| 56 | static u32 arch_timer_reg_read(int reg) |
| 57 | { |
| 58 | u32 val; |
| 59 | |
| 60 | switch (reg) { |
| 61 | case ARCH_TIMER_REG_CTRL: |
| 62 | asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val)); |
| 63 | break; |
| 64 | case ARCH_TIMER_REG_FREQ: |
| 65 | asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val)); |
| 66 | break; |
| 67 | case ARCH_TIMER_REG_TVAL: |
| 68 | asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val)); |
| 69 | break; |
| 70 | default: |
| 71 | BUG(); |
| 72 | } |
| 73 | |
| 74 | return val; |
| 75 | } |
| 76 | |
| 77 | static irqreturn_t arch_timer_handler(int irq, void *dev_id) |
| 78 | { |
| 79 | struct clock_event_device *evt = dev_id; |
| 80 | unsigned long ctrl; |
| 81 | |
| 82 | ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL); |
| 83 | if (ctrl & 0x4) { |
| 84 | ctrl |= ARCH_TIMER_CTRL_IT_MASK; |
| 85 | arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl); |
| 86 | evt->event_handler(evt); |
| 87 | return IRQ_HANDLED; |
| 88 | } |
| 89 | |
| 90 | return IRQ_NONE; |
| 91 | } |
| 92 | |
| 93 | static void arch_timer_stop(void) |
| 94 | { |
| 95 | unsigned long ctrl; |
| 96 | |
| 97 | ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL); |
| 98 | ctrl &= ~ARCH_TIMER_CTRL_ENABLE; |
| 99 | arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl); |
| 100 | } |
| 101 | |
| 102 | static void arch_timer_set_mode(enum clock_event_mode mode, |
| 103 | struct clock_event_device *clk) |
| 104 | { |
| 105 | switch (mode) { |
| 106 | case CLOCK_EVT_MODE_UNUSED: |
| 107 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 108 | arch_timer_stop(); |
| 109 | break; |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int arch_timer_set_next_event(unsigned long evt, |
| 116 | struct clock_event_device *unused) |
| 117 | { |
| 118 | unsigned long ctrl; |
| 119 | |
| 120 | ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL); |
| 121 | ctrl |= ARCH_TIMER_CTRL_ENABLE; |
| 122 | ctrl &= ~ARCH_TIMER_CTRL_IT_MASK; |
| 123 | |
| 124 | arch_timer_reg_write(ARCH_TIMER_REG_TVAL, evt); |
| 125 | arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static void __cpuinit arch_timer_setup(void *data) |
| 131 | { |
| 132 | struct clock_event_device *clk = data; |
| 133 | int err; |
| 134 | |
| 135 | /* Be safe... */ |
| 136 | arch_timer_stop(); |
| 137 | |
| 138 | clk->features = CLOCK_EVT_FEAT_ONESHOT; |
| 139 | clk->name = "arch_sys_timer"; |
| 140 | clk->rating = 450; |
| 141 | clk->set_mode = arch_timer_set_mode; |
| 142 | clk->set_next_event = arch_timer_set_next_event; |
| 143 | clk->irq = arch_timer_ppi; |
| 144 | clk->cpumask = cpumask_of(smp_processor_id()); |
| 145 | |
| 146 | clockevents_config_and_register(clk, arch_timer_rate, |
| 147 | 0xf, 0x7fffffff); |
| 148 | |
| 149 | err = gic_request_ppi(clk->irq, arch_timer_handler, clk); |
| 150 | if (err) { |
| 151 | pr_err("%s: can't register interrupt %d on cpu %d (%d)\n", |
| 152 | clk->name, clk->irq, smp_processor_id(), err); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (arch_timer_ppi2 >= 0) { |
| 157 | err = gic_request_ppi(arch_timer_ppi2, arch_timer_handler, clk); |
| 158 | if (err) { |
| 159 | pr_warn("%s: can't register interrupt %d on cpu %d (%d)\n", |
| 160 | clk->name, arch_timer_ppi2, smp_processor_id(), err); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* Is the optional system timer available? */ |
| 166 | static int local_timer_is_architected(void) |
| 167 | { |
| 168 | return (cpu_architecture() >= CPU_ARCH_ARMv7) && |
| 169 | ((read_cpuid_ext(CPUID_EXT_PFR1) >> 16) & 0xf) == 1; |
| 170 | } |
| 171 | |
| 172 | static int arch_timer_available(void) |
| 173 | { |
| 174 | unsigned long freq; |
| 175 | |
| 176 | if (!local_timer_is_architected()) |
| 177 | return -ENXIO; |
| 178 | |
| 179 | if (arch_timer_rate == 0) { |
| 180 | arch_timer_reg_write(ARCH_TIMER_REG_CTRL, 0); |
| 181 | freq = arch_timer_reg_read(ARCH_TIMER_REG_FREQ); |
| 182 | |
| 183 | /* Check the timer frequency. */ |
| 184 | if (freq == 0) { |
| 185 | pr_warn("Architected timer frequency not available\n"); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | arch_timer_rate = freq; |
| 190 | pr_info("Architected local timer running at %lu.%02luMHz.\n", |
| 191 | arch_timer_rate / 1000000, (arch_timer_rate % 100000) / 100); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static inline cycle_t arch_counter_get_cntpct(void) |
| 198 | { |
| 199 | u32 cvall, cvalh; |
| 200 | |
| 201 | asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (cvall), "=r" (cvalh)); |
| 202 | |
| 203 | return ((u64) cvalh << 32) | cvall; |
| 204 | } |
| 205 | |
| 206 | static inline cycle_t arch_counter_get_cntvct(void) |
| 207 | { |
| 208 | u32 cvall, cvalh; |
| 209 | |
| 210 | asm volatile("mrrc p15, 1, %0, %1, c14" : "=r" (cvall), "=r" (cvalh)); |
| 211 | |
| 212 | return ((u64) cvalh << 32) | cvall; |
| 213 | } |
| 214 | |
| 215 | static cycle_t arch_counter_read(struct clocksource *cs) |
| 216 | { |
| 217 | return arch_counter_get_cntpct(); |
| 218 | } |
| 219 | |
| 220 | static struct clocksource clocksource_counter = { |
| 221 | .name = "arch_sys_counter", |
| 222 | .rating = 400, |
| 223 | .read = arch_counter_read, |
| 224 | .mask = CLOCKSOURCE_MASK(56), |
| 225 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 226 | }; |
| 227 | |
| 228 | static void __cpuinit arch_timer_teardown(void *data) |
| 229 | { |
| 230 | struct clock_event_device *clk = data; |
| 231 | pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n", |
| 232 | clk->irq, smp_processor_id()); |
| 233 | gic_free_ppi(clk->irq, clk); |
| 234 | if (arch_timer_ppi2 >= 0) |
| 235 | gic_free_ppi(arch_timer_ppi2, clk); |
| 236 | arch_timer_set_mode(CLOCK_EVT_MODE_UNUSED, clk); |
| 237 | } |
| 238 | |
| 239 | static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self, |
| 240 | unsigned long action, void *data) |
| 241 | { |
| 242 | int cpu = (int)data; |
| 243 | struct clock_event_device *clk = per_cpu_ptr(arch_timer_evt, cpu); |
| 244 | |
| 245 | switch(action) { |
| 246 | case CPU_ONLINE: |
| 247 | case CPU_ONLINE_FROZEN: |
| 248 | smp_call_function_single(cpu, arch_timer_setup, clk, 1); |
| 249 | break; |
| 250 | |
| 251 | case CPU_DOWN_PREPARE: |
| 252 | case CPU_DOWN_PREPARE_FROZEN: |
| 253 | smp_call_function_single(cpu, arch_timer_teardown, clk, 1); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | return NOTIFY_OK; |
| 258 | } |
| 259 | |
| 260 | static struct notifier_block __cpuinitdata arch_timer_cpu_nb = { |
| 261 | .notifier_call = arch_timer_cpu_notify, |
| 262 | }; |
| 263 | |
| 264 | int arch_timer_register(struct resource *res, int res_nr) |
| 265 | { |
| 266 | int err; |
| 267 | |
| 268 | if (!res_nr || res[0].start < 0 || !(res[0].flags & IORESOURCE_IRQ)) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | err = arch_timer_available(); |
| 272 | if (err) |
| 273 | return err; |
| 274 | |
| 275 | arch_timer_evt = alloc_percpu(struct clock_event_device); |
| 276 | if (!arch_timer_evt) |
| 277 | return -ENOMEM; |
| 278 | |
| 279 | arch_timer_ppi = res[0].start; |
| 280 | if (res_nr > 1 && (res[1].flags & IORESOURCE_IRQ)) |
| 281 | arch_timer_ppi2 = res[1].start; |
| 282 | |
| 283 | clocksource_register_hz(&clocksource_counter, arch_timer_rate); |
| 284 | |
| 285 | /* Immediately configure the timer on the boot CPU */ |
| 286 | arch_timer_setup(per_cpu_ptr(arch_timer_evt, smp_processor_id())); |
| 287 | |
| 288 | register_cpu_notifier(&arch_timer_cpu_nb); |
| 289 | |
| 290 | return 0; |
| 291 | } |