blob: 71493f75ae6bd61e3418234e76caf9f8579e3d12 [file] [log] [blame]
Vineet Guptad8005e62013-01-18 15:12:18 +05301/*
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 Gupta565a9b42015-03-07 17:06:09 +053029 * which however is currently broken
Vineet Guptad8005e62013-01-18 15:12:18 +053030 */
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 Guptad8005e62013-01-18 15:12:18 +053037#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 Gupta03a6d282013-01-18 15:12:26 +053046#include <asm/mach_desc.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053047
Vineet Guptada1677b2013-05-14 13:28:17 +053048/* 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 Guptad8005e62013-01-18 15:12:18 +053059#define ARC_TIMER_MAX 0xFFFFFFFF
60
61/********** Clock Source Device *********/
62
Vineet Guptad8005e62013-01-18 15:12:18 +053063/*
64 * set 32bit TIMER1 to keep counting monotonically and wraparound
65 */
Paul Gortmakerce759952013-06-24 15:30:15 -040066int arc_counter_setup(void)
Vineet Guptad8005e62013-01-18 15:12:18 +053067{
68 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
69 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
70 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
71
Vineet Gupta5b9bd172015-03-07 16:59:38 +053072 /* Not usable in SMP */
73 return !IS_ENABLED(CONFIG_SMP);
Vineet Guptad8005e62013-01-18 15:12:18 +053074}
75
76static cycle_t arc_counter_read(struct clocksource *cs)
77{
78 return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
79}
80
81static struct clocksource arc_counter = {
82 .name = "ARC Timer1",
83 .rating = 300,
84 .read = arc_counter_read,
85 .mask = CLOCKSOURCE_MASK(32),
86 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
87};
88
Vineet Guptad8005e62013-01-18 15:12:18 +053089/********** Clock Event Device *********/
90
91/*
Vineet Guptac9a98e182014-06-25 17:14:03 +053092 * Arm the timer to interrupt after @cycles
Vineet Guptad8005e62013-01-18 15:12:18 +053093 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
94 */
Vineet Guptac9a98e182014-06-25 17:14:03 +053095static void arc_timer_event_setup(unsigned int cycles)
Vineet Guptad8005e62013-01-18 15:12:18 +053096{
Vineet Guptac9a98e182014-06-25 17:14:03 +053097 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
Vineet Guptad8005e62013-01-18 15:12:18 +053098 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
99
100 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
101}
102
Vineet Guptad8005e62013-01-18 15:12:18 +0530103
104static int arc_clkevent_set_next_event(unsigned long delta,
105 struct clock_event_device *dev)
106{
107 arc_timer_event_setup(delta);
108 return 0;
109}
110
111static void arc_clkevent_set_mode(enum clock_event_mode mode,
112 struct clock_event_device *dev)
113{
114 switch (mode) {
115 case CLOCK_EVT_MODE_PERIODIC:
Vineet Guptac9a98e182014-06-25 17:14:03 +0530116 /*
117 * At X Hz, 1 sec = 1000ms -> X cycles;
118 * 10ms -> X / 100 cycles
119 */
Vineet Guptad8005e62013-01-18 15:12:18 +0530120 arc_timer_event_setup(arc_get_core_freq() / HZ);
121 break;
122 case CLOCK_EVT_MODE_ONESHOT:
123 break;
124 default:
125 break;
126 }
127
128 return;
129}
130
131static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
132 .name = "ARC Timer0",
133 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
134 .mode = CLOCK_EVT_MODE_UNUSED,
135 .rating = 300,
136 .irq = TIMER0_IRQ, /* hardwired, no need for resources */
137 .set_next_event = arc_clkevent_set_next_event,
138 .set_mode = arc_clkevent_set_mode,
139};
140
141static irqreturn_t timer_irq_handler(int irq, void *dev_id)
142{
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530143 /*
144 * Note that generic IRQ core could have passed @evt for @dev_id if
145 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
146 */
147 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
148 int irq_reenable = evt->mode == CLOCK_EVT_MODE_PERIODIC;
Vineet Guptad8005e62013-01-18 15:12:18 +0530149
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530150 /*
151 * Any write to CTRL reg ACks the interrupt, we rewrite the
152 * Count when [N]ot [H]alted bit.
153 * And re-arm it if perioid by [I]nterrupt [E]nable bit
154 */
155 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
156
157 evt->event_handler(evt);
158
Vineet Guptad8005e62013-01-18 15:12:18 +0530159 return IRQ_HANDLED;
160}
161
Vineet Guptad8005e62013-01-18 15:12:18 +0530162/*
163 * Setup the local event timer for @cpu
Vineet Guptad8005e62013-01-18 15:12:18 +0530164 */
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530165void arc_local_timer_setup()
Vineet Guptad8005e62013-01-18 15:12:18 +0530166{
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530167 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
168 int cpu = smp_processor_id();
Vineet Guptad8005e62013-01-18 15:12:18 +0530169
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530170 evt->cpumask = cpumask_of(cpu);
171 clockevents_config_and_register(evt, arc_get_core_freq(),
Uwe Kleine-König55c2e262013-09-24 23:05:37 +0200172 0, ARC_TIMER_MAX);
Vineet Guptad8005e62013-01-18 15:12:18 +0530173
Vineet Gupta2b75c0f2014-05-07 15:25:10 +0530174 /* setup the per-cpu timer IRQ handler - for all cpus */
175 arc_request_percpu_irq(TIMER0_IRQ, cpu, timer_irq_handler,
176 "Timer0 (per-cpu-tick)", evt);
Vineet Guptad8005e62013-01-18 15:12:18 +0530177}
178
179/*
180 * Called from start_kernel() - boot CPU only
181 *
182 * -Sets up h/w timers as applicable on boot cpu
183 * -Also sets up any global state needed for timer subsystem:
184 * - for "counting" timer, registers a clocksource, usable across CPUs
185 * (provided that underlying counter h/w is synchronized across cores)
186 * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
187 */
188void __init time_init(void)
189{
190 /*
191 * sets up the timekeeping free-flowing counter which also returns
192 * whether the counter is usable as clocksource
193 */
194 if (arc_counter_setup())
195 /*
196 * CLK upto 4.29 GHz can be safely represented in 32 bits
197 * because Max 32 bit number is 4,294,967,295
198 */
199 clocksource_register_hz(&arc_counter, arc_get_core_freq());
200
201 /* sets up the periodic event timer */
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530202 arc_local_timer_setup();
Vineet Gupta03a6d282013-01-18 15:12:26 +0530203
204 if (machine_desc->init_time)
205 machine_desc->init_time();
Vineet Guptad8005e62013-01-18 15:12:18 +0530206}