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 |
Vineet Gupta | 565a9b4 | 2015-03-07 17:06:09 +0530 | [diff] [blame] | 29 | * which however is currently broken |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/sched.h> |
| 36 | #include <linux/kernel.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 37 | #include <linux/time.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/timex.h> |
| 40 | #include <linux/profile.h> |
| 41 | #include <linux/clocksource.h> |
| 42 | #include <linux/clockchips.h> |
| 43 | #include <asm/irq.h> |
| 44 | #include <asm/arcregs.h> |
| 45 | #include <asm/clk.h> |
Vineet Gupta | 03a6d28 | 2013-01-18 15:12:26 +0530 | [diff] [blame] | 46 | #include <asm/mach_desc.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 47 | |
Vineet Gupta | da1677b | 2013-05-14 13:28:17 +0530 | [diff] [blame] | 48 | /* Timer related Aux registers */ |
| 49 | #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */ |
| 50 | #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */ |
| 51 | #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */ |
| 52 | #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */ |
| 53 | #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */ |
| 54 | #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */ |
| 55 | |
| 56 | #define TIMER_CTRL_IE (1 << 0) /* Interupt when Count reachs limit */ |
| 57 | #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */ |
| 58 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 59 | #define ARC_TIMER_MAX 0xFFFFFFFF |
| 60 | |
| 61 | /********** Clock Source Device *********/ |
| 62 | |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame^] | 63 | #ifdef CONFIG_ARC_HAS_RTC |
| 64 | |
| 65 | #define AUX_RTC_CTRL 0x103 |
| 66 | #define AUX_RTC_LOW 0x104 |
| 67 | #define AUX_RTC_HIGH 0x105 |
| 68 | |
| 69 | int arc_counter_setup(void) |
| 70 | { |
| 71 | write_aux_reg(AUX_RTC_CTRL, 1); |
| 72 | |
| 73 | /* Not usable in SMP */ |
| 74 | return !IS_ENABLED(CONFIG_SMP); |
| 75 | } |
| 76 | |
| 77 | static cycle_t arc_counter_read(struct clocksource *cs) |
| 78 | { |
| 79 | unsigned long status; |
| 80 | union { |
| 81 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 82 | struct { u32 high, low; }; |
| 83 | #else |
| 84 | struct { u32 low, high; }; |
| 85 | #endif |
| 86 | cycle_t full; |
| 87 | } stamp; |
| 88 | |
| 89 | |
| 90 | __asm__ __volatile( |
| 91 | "1: \n" |
| 92 | " lr %0, [AUX_RTC_LOW] \n" |
| 93 | " lr %1, [AUX_RTC_HIGH] \n" |
| 94 | " lr %2, [AUX_RTC_CTRL] \n" |
| 95 | " bbit0.nt %2, 31, 1b \n" |
| 96 | : "=r" (stamp.low), "=r" (stamp.high), "=r" (status)); |
| 97 | |
| 98 | return stamp.full; |
| 99 | } |
| 100 | |
| 101 | static struct clocksource arc_counter = { |
| 102 | .name = "ARCv2 RTC", |
| 103 | .rating = 350, |
| 104 | .read = arc_counter_read, |
| 105 | .mask = CLOCKSOURCE_MASK(64), |
| 106 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 107 | }; |
| 108 | |
| 109 | #else /* !CONFIG_ARC_HAS_RTC */ |
| 110 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 111 | /* |
| 112 | * set 32bit TIMER1 to keep counting monotonically and wraparound |
| 113 | */ |
Paul Gortmaker | ce75995 | 2013-06-24 15:30:15 -0400 | [diff] [blame] | 114 | int arc_counter_setup(void) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 115 | { |
| 116 | write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX); |
| 117 | write_aux_reg(ARC_REG_TIMER1_CNT, 0); |
| 118 | write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH); |
| 119 | |
Vineet Gupta | 5b9bd17 | 2015-03-07 16:59:38 +0530 | [diff] [blame] | 120 | /* Not usable in SMP */ |
| 121 | return !IS_ENABLED(CONFIG_SMP); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static cycle_t arc_counter_read(struct clocksource *cs) |
| 125 | { |
| 126 | return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT); |
| 127 | } |
| 128 | |
| 129 | static struct clocksource arc_counter = { |
| 130 | .name = "ARC Timer1", |
| 131 | .rating = 300, |
| 132 | .read = arc_counter_read, |
| 133 | .mask = CLOCKSOURCE_MASK(32), |
| 134 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 135 | }; |
| 136 | |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame^] | 137 | #endif |
| 138 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 139 | /********** Clock Event Device *********/ |
| 140 | |
| 141 | /* |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 142 | * Arm the timer to interrupt after @cycles |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 143 | * The distinction for oneshot/periodic is done in arc_event_timer_ack() below |
| 144 | */ |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 145 | static void arc_timer_event_setup(unsigned int cycles) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 146 | { |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 147 | write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 148 | write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */ |
| 149 | |
| 150 | write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH); |
| 151 | } |
| 152 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 153 | |
| 154 | static int arc_clkevent_set_next_event(unsigned long delta, |
| 155 | struct clock_event_device *dev) |
| 156 | { |
| 157 | arc_timer_event_setup(delta); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static void arc_clkevent_set_mode(enum clock_event_mode mode, |
| 162 | struct clock_event_device *dev) |
| 163 | { |
| 164 | switch (mode) { |
| 165 | case CLOCK_EVT_MODE_PERIODIC: |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 166 | /* |
| 167 | * At X Hz, 1 sec = 1000ms -> X cycles; |
| 168 | * 10ms -> X / 100 cycles |
| 169 | */ |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 170 | arc_timer_event_setup(arc_get_core_freq() / HZ); |
| 171 | break; |
| 172 | case CLOCK_EVT_MODE_ONESHOT: |
| 173 | break; |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = { |
| 182 | .name = "ARC Timer0", |
| 183 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
| 184 | .mode = CLOCK_EVT_MODE_UNUSED, |
| 185 | .rating = 300, |
| 186 | .irq = TIMER0_IRQ, /* hardwired, no need for resources */ |
| 187 | .set_next_event = arc_clkevent_set_next_event, |
| 188 | .set_mode = arc_clkevent_set_mode, |
| 189 | }; |
| 190 | |
| 191 | static irqreturn_t timer_irq_handler(int irq, void *dev_id) |
| 192 | { |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 193 | /* |
| 194 | * Note that generic IRQ core could have passed @evt for @dev_id if |
| 195 | * irq_set_chip_and_handler() asked for handle_percpu_devid_irq() |
| 196 | */ |
| 197 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
| 198 | int irq_reenable = evt->mode == CLOCK_EVT_MODE_PERIODIC; |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 199 | |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 200 | /* |
| 201 | * Any write to CTRL reg ACks the interrupt, we rewrite the |
| 202 | * Count when [N]ot [H]alted bit. |
| 203 | * And re-arm it if perioid by [I]nterrupt [E]nable bit |
| 204 | */ |
| 205 | write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH); |
| 206 | |
| 207 | evt->event_handler(evt); |
| 208 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 209 | return IRQ_HANDLED; |
| 210 | } |
| 211 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 212 | /* |
| 213 | * Setup the local event timer for @cpu |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 214 | */ |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 215 | void arc_local_timer_setup() |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 216 | { |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 217 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
| 218 | int cpu = smp_processor_id(); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 219 | |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 220 | evt->cpumask = cpumask_of(cpu); |
| 221 | clockevents_config_and_register(evt, arc_get_core_freq(), |
Uwe Kleine-König | 55c2e26 | 2013-09-24 23:05:37 +0200 | [diff] [blame] | 222 | 0, ARC_TIMER_MAX); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 223 | |
Vineet Gupta | 2b75c0f | 2014-05-07 15:25:10 +0530 | [diff] [blame] | 224 | /* setup the per-cpu timer IRQ handler - for all cpus */ |
| 225 | arc_request_percpu_irq(TIMER0_IRQ, cpu, timer_irq_handler, |
| 226 | "Timer0 (per-cpu-tick)", evt); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Called from start_kernel() - boot CPU only |
| 231 | * |
| 232 | * -Sets up h/w timers as applicable on boot cpu |
| 233 | * -Also sets up any global state needed for timer subsystem: |
| 234 | * - for "counting" timer, registers a clocksource, usable across CPUs |
| 235 | * (provided that underlying counter h/w is synchronized across cores) |
| 236 | * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic) |
| 237 | */ |
| 238 | void __init time_init(void) |
| 239 | { |
| 240 | /* |
| 241 | * sets up the timekeeping free-flowing counter which also returns |
| 242 | * whether the counter is usable as clocksource |
| 243 | */ |
| 244 | if (arc_counter_setup()) |
| 245 | /* |
| 246 | * CLK upto 4.29 GHz can be safely represented in 32 bits |
| 247 | * because Max 32 bit number is 4,294,967,295 |
| 248 | */ |
| 249 | clocksource_register_hz(&arc_counter, arc_get_core_freq()); |
| 250 | |
| 251 | /* sets up the periodic event timer */ |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 252 | arc_local_timer_setup(); |
Vineet Gupta | 03a6d28 | 2013-01-18 15:12:26 +0530 | [diff] [blame] | 253 | |
| 254 | if (machine_desc->init_time) |
| 255 | machine_desc->init_time(); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 256 | } |