Blame view

arch/arc/kernel/time.c 6.76 KB
d8005e6b9   Vineet Gupta   ARC: Timers/count...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  /*
   * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * vineetg: Jan 1011
   *  -sched_clock( ) no longer jiffies based. Uses the same clocksource
   *   as gtod
   *
   * Rajeshwarr/Vineetg: Mar 2008
   *  -Implemented CONFIG_GENERIC_TIME (rather deleted arch specific code)
   *   for arch independent gettimeofday()
   *  -Implemented CONFIG_GENERIC_CLOCKEVENTS as base for hrtimers
   *
   * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c
   */
  
  /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1
   * Each can programmed to go from @count to @limit and optionally
   * interrupt when that happens.
   * A write to Control Register clears the Interrupt
   *
   * We've designated TIMER0 for events (clockevents)
   * while TIMER1 for free running (clocksource)
   *
   * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1
   */
  
  #include <linux/spinlock.h>
  #include <linux/interrupt.h>
  #include <linux/module.h>
  #include <linux/sched.h>
  #include <linux/kernel.h>
d8005e6b9   Vineet Gupta   ARC: Timers/count...
36
37
38
39
40
41
42
43
44
  #include <linux/time.h>
  #include <linux/init.h>
  #include <linux/timex.h>
  #include <linux/profile.h>
  #include <linux/clocksource.h>
  #include <linux/clockchips.h>
  #include <asm/irq.h>
  #include <asm/arcregs.h>
  #include <asm/clk.h>
03a6d28cd   Vineet Gupta   ARC: [Review] Mul...
45
  #include <asm/mach_desc.h>
d8005e6b9   Vineet Gupta   ARC: Timers/count...
46

da1677b02   Vineet Gupta   ARC: Disintegrate...
47
48
49
50
51
52
53
54
55
56
  /* Timer related Aux registers */
  #define ARC_REG_TIMER0_LIMIT	0x23	/* timer 0 limit */
  #define ARC_REG_TIMER0_CTRL	0x22	/* timer 0 control */
  #define ARC_REG_TIMER0_CNT	0x21	/* timer 0 count */
  #define ARC_REG_TIMER1_LIMIT	0x102	/* timer 1 limit */
  #define ARC_REG_TIMER1_CTRL	0x101	/* timer 1 control */
  #define ARC_REG_TIMER1_CNT	0x100	/* timer 1 count */
  
  #define TIMER_CTRL_IE		(1 << 0) /* Interupt when Count reachs limit */
  #define TIMER_CTRL_NH		(1 << 1) /* Count only when CPU NOT halted */
d8005e6b9   Vineet Gupta   ARC: Timers/count...
57
58
59
60
61
  #define ARC_TIMER_MAX	0xFFFFFFFF
  
  /********** Clock Source Device *********/
  
  #ifdef CONFIG_ARC_HAS_RTSC
ce7599567   Paul Gortmaker   arc: delete __cpu...
62
  int arc_counter_setup(void)
d8005e6b9   Vineet Gupta   ARC: Timers/count...
63
  {
7d0857a54   Vineet Gupta   ARC: [SMP] Disall...
64
65
66
67
  	/*
  	 * For SMP this needs to be 0. However Kconfig glue doesn't
  	 * enable this option for SMP configs
  	 */
d8005e6b9   Vineet Gupta   ARC: Timers/count...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  	return 1;
  }
  
  static cycle_t arc_counter_read(struct clocksource *cs)
  {
  	unsigned long flags;
  	union {
  #ifdef CONFIG_CPU_BIG_ENDIAN
  		struct { u32 high, low; };
  #else
  		struct { u32 low, high; };
  #endif
  		cycle_t  full;
  	} stamp;
  
  	flags = arch_local_irq_save();
  
  	__asm__ __volatile(
  	"	.extCoreRegister tsch, 58,  r, cannot_shortcut	
  "
  	"	rtsc %0, 0	
  "
1e2666299   Vineet Gupta   ARC: 64bit RTSC t...
90
91
  	"	mov  %1, 0	
  "
d8005e6b9   Vineet Gupta   ARC: Timers/count...
92
93
94
95
96
97
98
99
100
101
102
  	: "=r" (stamp.low), "=r" (stamp.high));
  
  	arch_local_irq_restore(flags);
  
  	return stamp.full;
  }
  
  static struct clocksource arc_counter = {
  	.name   = "ARC RTSC",
  	.rating = 300,
  	.read   = arc_counter_read,
1e2666299   Vineet Gupta   ARC: 64bit RTSC t...
103
  	.mask   = CLOCKSOURCE_MASK(32),
d8005e6b9   Vineet Gupta   ARC: Timers/count...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  	.flags  = CLOCK_SOURCE_IS_CONTINUOUS,
  };
  
  #else /* !CONFIG_ARC_HAS_RTSC */
  
  static bool is_usable_as_clocksource(void)
  {
  #ifdef CONFIG_SMP
  	return 0;
  #else
  	return 1;
  #endif
  }
  
  /*
   * set 32bit TIMER1 to keep counting monotonically and wraparound
   */
ce7599567   Paul Gortmaker   arc: delete __cpu...
121
  int arc_counter_setup(void)
d8005e6b9   Vineet Gupta   ARC: Timers/count...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  {
  	write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
  	write_aux_reg(ARC_REG_TIMER1_CNT, 0);
  	write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
  
  	return is_usable_as_clocksource();
  }
  
  static cycle_t arc_counter_read(struct clocksource *cs)
  {
  	return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
  }
  
  static struct clocksource arc_counter = {
  	.name   = "ARC Timer1",
  	.rating = 300,
  	.read   = arc_counter_read,
  	.mask   = CLOCKSOURCE_MASK(32),
  	.flags  = CLOCK_SOURCE_IS_CONTINUOUS,
  };
  
  #endif
  
  /********** Clock Event Device *********/
  
  /*
c9a98e184   Vineet Gupta   ARC: update some ...
148
   * Arm the timer to interrupt after @cycles
d8005e6b9   Vineet Gupta   ARC: Timers/count...
149
150
   * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
   */
c9a98e184   Vineet Gupta   ARC: update some ...
151
  static void arc_timer_event_setup(unsigned int cycles)
d8005e6b9   Vineet Gupta   ARC: Timers/count...
152
  {
c9a98e184   Vineet Gupta   ARC: update some ...
153
  	write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
d8005e6b9   Vineet Gupta   ARC: Timers/count...
154
155
156
157
  	write_aux_reg(ARC_REG_TIMER0_CNT, 0);	/* start from 0 */
  
  	write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
  }
d8005e6b9   Vineet Gupta   ARC: Timers/count...
158
159
160
161
162
163
164
165
166
167
168
169
170
  
  static int arc_clkevent_set_next_event(unsigned long delta,
  				       struct clock_event_device *dev)
  {
  	arc_timer_event_setup(delta);
  	return 0;
  }
  
  static void arc_clkevent_set_mode(enum clock_event_mode mode,
  				  struct clock_event_device *dev)
  {
  	switch (mode) {
  	case CLOCK_EVT_MODE_PERIODIC:
c9a98e184   Vineet Gupta   ARC: update some ...
171
172
173
174
                  /*
                   * At X Hz, 1 sec = 1000ms -> X cycles;
                   *                    10ms -> X / 100 cycles
                   */
d8005e6b9   Vineet Gupta   ARC: Timers/count...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  		arc_timer_event_setup(arc_get_core_freq() / HZ);
  		break;
  	case CLOCK_EVT_MODE_ONESHOT:
  		break;
  	default:
  		break;
  	}
  
  	return;
  }
  
  static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
  	.name		= "ARC Timer0",
  	.features	= CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  	.mode		= CLOCK_EVT_MODE_UNUSED,
  	.rating		= 300,
  	.irq		= TIMER0_IRQ,	/* hardwired, no need for resources */
  	.set_next_event = arc_clkevent_set_next_event,
  	.set_mode	= arc_clkevent_set_mode,
  };
  
  static irqreturn_t timer_irq_handler(int irq, void *dev_id)
  {
f8b34c3fd   Vineet Gupta   ARC: [clockevent]...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  	/*
  	 * Note that generic IRQ core could have passed @evt for @dev_id if
  	 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
  	 */
  	struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  	int irq_reenable = evt->mode == CLOCK_EVT_MODE_PERIODIC;
  
  	/*
  	 * Any write to CTRL reg ACks the interrupt, we rewrite the
  	 * Count when [N]ot [H]alted bit.
  	 * And re-arm it if perioid by [I]nterrupt [E]nable bit
  	 */
  	write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
  
  	evt->event_handler(evt);
d8005e6b9   Vineet Gupta   ARC: Timers/count...
213

d8005e6b9   Vineet Gupta   ARC: Timers/count...
214
215
  	return IRQ_HANDLED;
  }
d8005e6b9   Vineet Gupta   ARC: Timers/count...
216
217
  /*
   * Setup the local event timer for @cpu
d8005e6b9   Vineet Gupta   ARC: Timers/count...
218
   */
2d4899f6b   Vineet Gupta   ARC: arc_local_ti...
219
  void arc_local_timer_setup()
d8005e6b9   Vineet Gupta   ARC: Timers/count...
220
  {
2d4899f6b   Vineet Gupta   ARC: arc_local_ti...
221
222
  	struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  	int cpu = smp_processor_id();
d8005e6b9   Vineet Gupta   ARC: Timers/count...
223

2d4899f6b   Vineet Gupta   ARC: arc_local_ti...
224
225
  	evt->cpumask = cpumask_of(cpu);
  	clockevents_config_and_register(evt, arc_get_core_freq(),
55c2e2620   Uwe Kleine-König   ARC: Use clockeve...
226
  					0, ARC_TIMER_MAX);
d8005e6b9   Vineet Gupta   ARC: Timers/count...
227

2b75c0f93   Vineet Gupta   ARC: [SMP] unify ...
228
229
230
  	/* setup the per-cpu timer IRQ handler - for all cpus */
  	arc_request_percpu_irq(TIMER0_IRQ, cpu, timer_irq_handler,
  			       "Timer0 (per-cpu-tick)", evt);
d8005e6b9   Vineet Gupta   ARC: Timers/count...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  }
  
  /*
   * Called from start_kernel() - boot CPU only
   *
   * -Sets up h/w timers as applicable on boot cpu
   * -Also sets up any global state needed for timer subsystem:
   *    - for "counting" timer, registers a clocksource, usable across CPUs
   *      (provided that underlying counter h/w is synchronized across cores)
   *    - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
   */
  void __init time_init(void)
  {
  	/*
  	 * sets up the timekeeping free-flowing counter which also returns
  	 * whether the counter is usable as clocksource
  	 */
  	if (arc_counter_setup())
  		/*
  		 * CLK upto 4.29 GHz can be safely represented in 32 bits
  		 * because Max 32 bit number is 4,294,967,295
  		 */
  		clocksource_register_hz(&arc_counter, arc_get_core_freq());
  
  	/* sets up the periodic event timer */
2d4899f6b   Vineet Gupta   ARC: arc_local_ti...
256
  	arc_local_timer_setup();
03a6d28cd   Vineet Gupta   ARC: [Review] Mul...
257
258
259
  
  	if (machine_desc->init_time)
  		machine_desc->init_time();
d8005e6b9   Vineet Gupta   ARC: Timers/count...
260
  }