Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * vineetg: Jan 1011 |
| 9 | * -sched_clock( ) no longer jiffies based. Uses the same clocksource |
| 10 | * as gtod |
| 11 | * |
| 12 | * Rajeshwarr/Vineetg: Mar 2008 |
| 13 | * -Implemented CONFIG_GENERIC_TIME (rather deleted arch specific code) |
| 14 | * for arch independent gettimeofday() |
| 15 | * -Implemented CONFIG_GENERIC_CLOCKEVENTS as base for hrtimers |
| 16 | * |
| 17 | * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c |
| 18 | */ |
| 19 | |
| 20 | /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1 |
| 21 | * Each can programmed to go from @count to @limit and optionally |
| 22 | * interrupt when that happens. |
| 23 | * A write to Control Register clears the Interrupt |
| 24 | * |
| 25 | * We've designated TIMER0 for events (clockevents) |
| 26 | * while TIMER1 for free running (clocksource) |
| 27 | * |
| 28 | * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1 |
| 29 | */ |
| 30 | |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/kernel.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 36 | #include <linux/time.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/timex.h> |
| 39 | #include <linux/profile.h> |
| 40 | #include <linux/clocksource.h> |
| 41 | #include <linux/clockchips.h> |
| 42 | #include <asm/irq.h> |
| 43 | #include <asm/arcregs.h> |
| 44 | #include <asm/clk.h> |
Vineet Gupta | 03a6d28 | 2013-01-18 15:12:26 +0530 | [diff] [blame] | 45 | #include <asm/mach_desc.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 46 | |
Vineet Gupta | da1677b0 | 2013-05-14 13:28:17 +0530 | [diff] [blame] | 47 | /* Timer related Aux registers */ |
| 48 | #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */ |
| 49 | #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */ |
| 50 | #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */ |
| 51 | #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */ |
| 52 | #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */ |
| 53 | #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */ |
| 54 | |
| 55 | #define TIMER_CTRL_IE (1 << 0) /* Interupt when Count reachs limit */ |
| 56 | #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */ |
| 57 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 58 | #define ARC_TIMER_MAX 0xFFFFFFFF |
| 59 | |
| 60 | /********** Clock Source Device *********/ |
| 61 | |
| 62 | #ifdef CONFIG_ARC_HAS_RTSC |
| 63 | |
Paul Gortmaker | ce75995 | 2013-06-24 15:30:15 -0400 | [diff] [blame] | 64 | int arc_counter_setup(void) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 65 | { |
Vineet Gupta | 7d0857a | 2013-09-09 16:04:15 +0530 | [diff] [blame] | 66 | /* |
| 67 | * For SMP this needs to be 0. However Kconfig glue doesn't |
| 68 | * enable this option for SMP configs |
| 69 | */ |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | static cycle_t arc_counter_read(struct clocksource *cs) |
| 74 | { |
| 75 | unsigned long flags; |
| 76 | union { |
| 77 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 78 | struct { u32 high, low; }; |
| 79 | #else |
| 80 | struct { u32 low, high; }; |
| 81 | #endif |
| 82 | cycle_t full; |
| 83 | } stamp; |
| 84 | |
| 85 | flags = arch_local_irq_save(); |
| 86 | |
| 87 | __asm__ __volatile( |
| 88 | " .extCoreRegister tsch, 58, r, cannot_shortcut \n" |
| 89 | " rtsc %0, 0 \n" |
Vineet Gupta | 1e26662 | 2013-02-06 15:09:13 +0530 | [diff] [blame] | 90 | " mov %1, 0 \n" |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 91 | : "=r" (stamp.low), "=r" (stamp.high)); |
| 92 | |
| 93 | arch_local_irq_restore(flags); |
| 94 | |
| 95 | return stamp.full; |
| 96 | } |
| 97 | |
| 98 | static struct clocksource arc_counter = { |
| 99 | .name = "ARC RTSC", |
| 100 | .rating = 300, |
| 101 | .read = arc_counter_read, |
Vineet Gupta | 1e26662 | 2013-02-06 15:09:13 +0530 | [diff] [blame] | 102 | .mask = CLOCKSOURCE_MASK(32), |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 103 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 104 | }; |
| 105 | |
| 106 | #else /* !CONFIG_ARC_HAS_RTSC */ |
| 107 | |
| 108 | static bool is_usable_as_clocksource(void) |
| 109 | { |
| 110 | #ifdef CONFIG_SMP |
| 111 | return 0; |
| 112 | #else |
| 113 | return 1; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * set 32bit TIMER1 to keep counting monotonically and wraparound |
| 119 | */ |
Paul Gortmaker | ce75995 | 2013-06-24 15:30:15 -0400 | [diff] [blame] | 120 | int arc_counter_setup(void) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 121 | { |
| 122 | write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX); |
| 123 | write_aux_reg(ARC_REG_TIMER1_CNT, 0); |
| 124 | write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH); |
| 125 | |
| 126 | return is_usable_as_clocksource(); |
| 127 | } |
| 128 | |
| 129 | static cycle_t arc_counter_read(struct clocksource *cs) |
| 130 | { |
| 131 | return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT); |
| 132 | } |
| 133 | |
| 134 | static struct clocksource arc_counter = { |
| 135 | .name = "ARC Timer1", |
| 136 | .rating = 300, |
| 137 | .read = arc_counter_read, |
| 138 | .mask = CLOCKSOURCE_MASK(32), |
| 139 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 140 | }; |
| 141 | |
| 142 | #endif |
| 143 | |
| 144 | /********** Clock Event Device *********/ |
| 145 | |
| 146 | /* |
| 147 | * Arm the timer to interrupt after @limit cycles |
| 148 | * The distinction for oneshot/periodic is done in arc_event_timer_ack() below |
| 149 | */ |
| 150 | static void arc_timer_event_setup(unsigned int limit) |
| 151 | { |
| 152 | write_aux_reg(ARC_REG_TIMER0_LIMIT, limit); |
| 153 | write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */ |
| 154 | |
| 155 | write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Acknowledge the interrupt (oneshot) and optionally re-arm it (periodic) |
| 160 | * -Any write to CTRL Reg will ack the intr (NH bit: Count when not halted) |
| 161 | * -Rearming is done by setting the IE bit |
| 162 | * |
| 163 | * Small optimisation: Normal code would have been |
| 164 | * if (irq_reenable) |
| 165 | * CTRL_REG = (IE | NH); |
| 166 | * else |
| 167 | * CTRL_REG = NH; |
| 168 | * However since IE is BIT0 we can fold the branch |
| 169 | */ |
| 170 | static void arc_timer_event_ack(unsigned int irq_reenable) |
| 171 | { |
| 172 | write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH); |
| 173 | } |
| 174 | |
| 175 | static int arc_clkevent_set_next_event(unsigned long delta, |
| 176 | struct clock_event_device *dev) |
| 177 | { |
| 178 | arc_timer_event_setup(delta); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void arc_clkevent_set_mode(enum clock_event_mode mode, |
| 183 | struct clock_event_device *dev) |
| 184 | { |
| 185 | switch (mode) { |
| 186 | case CLOCK_EVT_MODE_PERIODIC: |
| 187 | arc_timer_event_setup(arc_get_core_freq() / HZ); |
| 188 | break; |
| 189 | case CLOCK_EVT_MODE_ONESHOT: |
| 190 | break; |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = { |
| 199 | .name = "ARC Timer0", |
| 200 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
| 201 | .mode = CLOCK_EVT_MODE_UNUSED, |
| 202 | .rating = 300, |
| 203 | .irq = TIMER0_IRQ, /* hardwired, no need for resources */ |
| 204 | .set_next_event = arc_clkevent_set_next_event, |
| 205 | .set_mode = arc_clkevent_set_mode, |
| 206 | }; |
| 207 | |
| 208 | static irqreturn_t timer_irq_handler(int irq, void *dev_id) |
| 209 | { |
Christoph Lameter | 6855e95 | 2013-08-28 19:48:15 +0000 | [diff] [blame] | 210 | struct clock_event_device *clk = this_cpu_ptr(&arc_clockevent_device); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 211 | |
| 212 | arc_timer_event_ack(clk->mode == CLOCK_EVT_MODE_PERIODIC); |
| 213 | clk->event_handler(clk); |
| 214 | return IRQ_HANDLED; |
| 215 | } |
| 216 | |
| 217 | static struct irqaction arc_timer_irq = { |
| 218 | .name = "Timer0 (clock-evt-dev)", |
| 219 | .flags = IRQF_TIMER | IRQF_PERCPU, |
| 220 | .handler = timer_irq_handler, |
| 221 | }; |
| 222 | |
| 223 | /* |
| 224 | * Setup the local event timer for @cpu |
| 225 | * N.B. weak so that some exotic ARC SoCs can completely override it |
| 226 | */ |
Vineet Gupta | 064a626 | 2013-10-31 13:53:54 +0530 | [diff] [blame] | 227 | void __weak arc_local_timer_setup(unsigned int cpu) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 228 | { |
| 229 | struct clock_event_device *clk = &per_cpu(arc_clockevent_device, cpu); |
| 230 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 231 | clk->cpumask = cpumask_of(cpu); |
Uwe Kleine-König | 55c2e26 | 2013-09-24 23:05:37 +0200 | [diff] [blame] | 232 | clockevents_config_and_register(clk, arc_get_core_freq(), |
| 233 | 0, ARC_TIMER_MAX); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * setup the per-cpu timer IRQ handler - for all cpus |
| 237 | * For non boot CPU explicitly unmask at intc |
| 238 | * setup_irq() -> .. -> irq_startup() already does this on boot-cpu |
| 239 | */ |
| 240 | if (!cpu) |
| 241 | setup_irq(TIMER0_IRQ, &arc_timer_irq); |
| 242 | else |
| 243 | arch_unmask_irq(TIMER0_IRQ); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Called from start_kernel() - boot CPU only |
| 248 | * |
| 249 | * -Sets up h/w timers as applicable on boot cpu |
| 250 | * -Also sets up any global state needed for timer subsystem: |
| 251 | * - for "counting" timer, registers a clocksource, usable across CPUs |
| 252 | * (provided that underlying counter h/w is synchronized across cores) |
| 253 | * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic) |
| 254 | */ |
| 255 | void __init time_init(void) |
| 256 | { |
| 257 | /* |
| 258 | * sets up the timekeeping free-flowing counter which also returns |
| 259 | * whether the counter is usable as clocksource |
| 260 | */ |
| 261 | if (arc_counter_setup()) |
| 262 | /* |
| 263 | * CLK upto 4.29 GHz can be safely represented in 32 bits |
| 264 | * because Max 32 bit number is 4,294,967,295 |
| 265 | */ |
| 266 | clocksource_register_hz(&arc_counter, arc_get_core_freq()); |
| 267 | |
| 268 | /* sets up the periodic event timer */ |
| 269 | arc_local_timer_setup(smp_processor_id()); |
Vineet Gupta | 03a6d28 | 2013-01-18 15:12:26 +0530 | [diff] [blame] | 270 | |
| 271 | if (machine_desc->init_time) |
| 272 | machine_desc->init_time(); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 273 | } |