Blame view

drivers/clocksource/dw_apb_timer.c 12 KB
06c3df495   Jamie Iles   clocksource: apb:...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * (C) Copyright 2009 Intel Corporation
   * Author: Jacob Pan (jacob.jun.pan@intel.com)
   *
   * Shared with ARM platforms, Jamie Iles, Picochip 2011
   *
   * 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.
   *
   * Support for the Synopsys DesignWare APB Timers.
   */
  #include <linux/dw_apb_timer.h>
  #include <linux/delay.h>
  #include <linux/kernel.h>
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/io.h>
  #include <linux/slab.h>
  
  #define APBT_MIN_PERIOD			4
  #define APBT_MIN_DELTA_USEC		200
d3d8fee41   John Stultz   Revert "dw_apb_ti...
23
24
25
26
27
  #define APBTMR_N_LOAD_COUNT		0x00
  #define APBTMR_N_CURRENT_VALUE		0x04
  #define APBTMR_N_CONTROL		0x08
  #define APBTMR_N_EOI			0x0c
  #define APBTMR_N_INT_STATUS		0x10
06c3df495   Jamie Iles   clocksource: apb:...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #define APBTMRS_INT_STATUS		0xa0
  #define APBTMRS_EOI			0xa4
  #define APBTMRS_RAW_INT_STATUS		0xa8
  #define APBTMRS_COMP_VERSION		0xac
  
  #define APBTMR_CONTROL_ENABLE		(1 << 0)
  /* 1: periodic, 0:free running. */
  #define APBTMR_CONTROL_MODE_PERIODIC	(1 << 1)
  #define APBTMR_CONTROL_INT		(1 << 2)
  
  static inline struct dw_apb_clock_event_device *
  ced_to_dw_apb_ced(struct clock_event_device *evt)
  {
  	return container_of(evt, struct dw_apb_clock_event_device, ced);
  }
  
  static inline struct dw_apb_clocksource *
  clocksource_to_dw_apb_clocksource(struct clocksource *cs)
  {
  	return container_of(cs, struct dw_apb_clocksource, cs);
  }
520ddad4e   Jisheng Zhang   clocksource/drive...
49
  static inline u32 apbt_readl(struct dw_apb_timer *timer, unsigned long offs)
06c3df495   Jamie Iles   clocksource: apb:...
50
51
52
  {
  	return readl(timer->base + offs);
  }
520ddad4e   Jisheng Zhang   clocksource/drive...
53
54
  static inline void apbt_writel(struct dw_apb_timer *timer, u32 val,
  			unsigned long offs)
06c3df495   Jamie Iles   clocksource: apb:...
55
56
57
  {
  	writel(val, timer->base + offs);
  }
39d3611f2   Jisheng Zhang   clocksource/drive...
58
59
60
61
62
63
64
65
66
67
  static inline u32 apbt_readl_relaxed(struct dw_apb_timer *timer, unsigned long offs)
  {
  	return readl_relaxed(timer->base + offs);
  }
  
  static inline void apbt_writel_relaxed(struct dw_apb_timer *timer, u32 val,
  			unsigned long offs)
  {
  	writel_relaxed(val, timer->base + offs);
  }
06c3df495   Jamie Iles   clocksource: apb:...
68
69
  static void apbt_disable_int(struct dw_apb_timer *timer)
  {
9f4165dc4   Jisheng Zhang   clocksource/drive...
70
  	u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  
  	ctrl |= APBTMR_CONTROL_INT;
  	apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  }
  
  /**
   * dw_apb_clockevent_pause() - stop the clock_event_device from running
   *
   * @dw_ced:	The APB clock to stop generating events.
   */
  void dw_apb_clockevent_pause(struct dw_apb_clock_event_device *dw_ced)
  {
  	disable_irq(dw_ced->timer.irq);
  	apbt_disable_int(&dw_ced->timer);
  }
  
  static void apbt_eoi(struct dw_apb_timer *timer)
  {
39d3611f2   Jisheng Zhang   clocksource/drive...
89
  	apbt_readl_relaxed(timer, APBTMR_N_EOI);
06c3df495   Jamie Iles   clocksource: apb:...
90
91
92
93
94
95
96
97
  }
  
  static irqreturn_t dw_apb_clockevent_irq(int irq, void *data)
  {
  	struct clock_event_device *evt = data;
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  
  	if (!evt->event_handler) {
ac9ce6d1a   Rafał Miłecki   clocksource: Add ...
98
99
  		pr_info("Spurious APBT timer interrupt %d
  ", irq);
06c3df495   Jamie Iles   clocksource: apb:...
100
101
102
103
104
105
106
107
108
109
110
111
  		return IRQ_NONE;
  	}
  
  	if (dw_ced->eoi)
  		dw_ced->eoi(&dw_ced->timer);
  
  	evt->event_handler(evt);
  	return IRQ_HANDLED;
  }
  
  static void apbt_enable_int(struct dw_apb_timer *timer)
  {
9f4165dc4   Jisheng Zhang   clocksource/drive...
112
  	u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
113
114
115
116
117
  	/* clear pending intr */
  	apbt_readl(timer, APBTMR_N_EOI);
  	ctrl &= ~APBTMR_CONTROL_INT;
  	apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  }
226be92b3   Viresh Kumar   clockevents/drive...
118
  static int apbt_shutdown(struct clock_event_device *evt)
06c3df495   Jamie Iles   clocksource: apb:...
119
  {
226be92b3   Viresh Kumar   clockevents/drive...
120
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
9f4165dc4   Jisheng Zhang   clocksource/drive...
121
  	u32 ctrl;
226be92b3   Viresh Kumar   clockevents/drive...
122
123
124
125
126
127
128
129
130
131
132
133
134
  
  	pr_debug("%s CPU %d state=shutdown
  ", __func__,
  		 cpumask_first(evt->cpumask));
  
  	ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  	ctrl &= ~APBTMR_CONTROL_ENABLE;
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	return 0;
  }
  
  static int apbt_set_oneshot(struct clock_event_device *evt)
  {
06c3df495   Jamie Iles   clocksource: apb:...
135
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
9f4165dc4   Jisheng Zhang   clocksource/drive...
136
  	u32 ctrl;
06c3df495   Jamie Iles   clocksource: apb:...
137

226be92b3   Viresh Kumar   clockevents/drive...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  	pr_debug("%s CPU %d state=oneshot
  ", __func__,
  		 cpumask_first(evt->cpumask));
  
  	ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  	/*
  	 * set free running mode, this mode will let timer reload max
  	 * timeout which will give time (3min on 25MHz clock) to rearm
  	 * the next event, therefore emulate the one-shot mode.
  	 */
  	ctrl &= ~APBTMR_CONTROL_ENABLE;
  	ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	/* write again to set free running mode */
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  
  	/*
  	 * DW APB p. 46, load counter with all 1s before starting free
  	 * running mode.
  	 */
  	apbt_writel(&dw_ced->timer, ~0, APBTMR_N_LOAD_COUNT);
  	ctrl &= ~APBTMR_CONTROL_INT;
  	ctrl |= APBTMR_CONTROL_ENABLE;
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	return 0;
  }
  
  static int apbt_set_periodic(struct clock_event_device *evt)
  {
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  	unsigned long period = DIV_ROUND_UP(dw_ced->timer.freq, HZ);
9f4165dc4   Jisheng Zhang   clocksource/drive...
170
  	u32 ctrl;
226be92b3   Viresh Kumar   clockevents/drive...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  	pr_debug("%s CPU %d state=periodic
  ", __func__,
  		 cpumask_first(evt->cpumask));
  
  	ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  	ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	/*
  	 * DW APB p. 46, have to disable timer before load counter,
  	 * may cause sync problem.
  	 */
  	ctrl &= ~APBTMR_CONTROL_ENABLE;
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	udelay(1);
  	pr_debug("Setting clock period %lu for HZ %d
  ", period, HZ);
  	apbt_writel(&dw_ced->timer, period, APBTMR_N_LOAD_COUNT);
  	ctrl |= APBTMR_CONTROL_ENABLE;
  	apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  	return 0;
  }
  
  static int apbt_resume(struct clock_event_device *evt)
  {
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  
  	pr_debug("%s CPU %d state=resume
  ", __func__,
  		 cpumask_first(evt->cpumask));
  
  	apbt_enable_int(&dw_ced->timer);
  	return 0;
06c3df495   Jamie Iles   clocksource: apb:...
204
205
206
207
208
  }
  
  static int apbt_next_event(unsigned long delta,
  			   struct clock_event_device *evt)
  {
9f4165dc4   Jisheng Zhang   clocksource/drive...
209
  	u32 ctrl;
06c3df495   Jamie Iles   clocksource: apb:...
210
211
212
  	struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  
  	/* Disable timer */
39d3611f2   Jisheng Zhang   clocksource/drive...
213
  	ctrl = apbt_readl_relaxed(&dw_ced->timer, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
214
  	ctrl &= ~APBTMR_CONTROL_ENABLE;
39d3611f2   Jisheng Zhang   clocksource/drive...
215
  	apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
216
  	/* write new count */
39d3611f2   Jisheng Zhang   clocksource/drive...
217
  	apbt_writel_relaxed(&dw_ced->timer, delta, APBTMR_N_LOAD_COUNT);
06c3df495   Jamie Iles   clocksource: apb:...
218
  	ctrl |= APBTMR_CONTROL_ENABLE;
39d3611f2   Jisheng Zhang   clocksource/drive...
219
  	apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
220
221
222
223
224
225
226
227
228
229
230
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
256
257
258
  
  	return 0;
  }
  
  /**
   * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
   *
   * @cpu:	The CPU the events will be targeted at.
   * @name:	The name used for the timer and the IRQ for it.
   * @rating:	The rating to give the timer.
   * @base:	I/O base for the timer registers.
   * @irq:	The interrupt number to use for the timer.
   * @freq:	The frequency that the timer counts at.
   *
   * This creates a clock_event_device for using with the generic clock layer
   * but does not start and register it.  This should be done with
   * dw_apb_clockevent_register() as the next step.  If this is the first time
   * it has been called for a timer then the IRQ will be requested, if not it
   * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
   * releasing the IRQ.
   */
  struct dw_apb_clock_event_device *
  dw_apb_clockevent_init(int cpu, const char *name, unsigned rating,
  		       void __iomem *base, int irq, unsigned long freq)
  {
  	struct dw_apb_clock_event_device *dw_ced =
  		kzalloc(sizeof(*dw_ced), GFP_KERNEL);
  	int err;
  
  	if (!dw_ced)
  		return NULL;
  
  	dw_ced->timer.base = base;
  	dw_ced->timer.irq = irq;
  	dw_ced->timer.freq = freq;
  
  	clockevents_calc_mult_shift(&dw_ced->ced, freq, APBT_MIN_PERIOD);
  	dw_ced->ced.max_delta_ns = clockevent_delta2ns(0x7fffffff,
  						       &dw_ced->ced);
8317b53fb   Nicolai Stange   clockevents/drive...
259
  	dw_ced->ced.max_delta_ticks = 0x7fffffff;
06c3df495   Jamie Iles   clocksource: apb:...
260
  	dw_ced->ced.min_delta_ns = clockevent_delta2ns(5000, &dw_ced->ced);
8317b53fb   Nicolai Stange   clockevents/drive...
261
  	dw_ced->ced.min_delta_ticks = 5000;
06c3df495   Jamie Iles   clocksource: apb:...
262
  	dw_ced->ced.cpumask = cpumask_of(cpu);
8b5f0010f   Jisheng Zhang   clockevents/drive...
263
264
  	dw_ced->ced.features = CLOCK_EVT_FEAT_PERIODIC |
  				CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ;
226be92b3   Viresh Kumar   clockevents/drive...
265
266
267
  	dw_ced->ced.set_state_shutdown = apbt_shutdown;
  	dw_ced->ced.set_state_periodic = apbt_set_periodic;
  	dw_ced->ced.set_state_oneshot = apbt_set_oneshot;
457353260   Jisheng Zhang   clockevents/drive...
268
  	dw_ced->ced.set_state_oneshot_stopped = apbt_shutdown;
226be92b3   Viresh Kumar   clockevents/drive...
269
  	dw_ced->ced.tick_resume = apbt_resume;
06c3df495   Jamie Iles   clocksource: apb:...
270
271
272
273
274
275
276
277
278
279
  	dw_ced->ced.set_next_event = apbt_next_event;
  	dw_ced->ced.irq = dw_ced->timer.irq;
  	dw_ced->ced.rating = rating;
  	dw_ced->ced.name = name;
  
  	dw_ced->irqaction.name		= dw_ced->ced.name;
  	dw_ced->irqaction.handler	= dw_apb_clockevent_irq;
  	dw_ced->irqaction.dev_id	= &dw_ced->ced;
  	dw_ced->irqaction.irq		= irq;
  	dw_ced->irqaction.flags		= IRQF_TIMER | IRQF_IRQPOLL |
38c30a842   Michael Opdenacker   clocksource: misc...
280
  					  IRQF_NOBALANCING;
06c3df495   Jamie Iles   clocksource: apb:...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  
  	dw_ced->eoi = apbt_eoi;
  	err = setup_irq(irq, &dw_ced->irqaction);
  	if (err) {
  		pr_err("failed to request timer irq
  ");
  		kfree(dw_ced);
  		dw_ced = NULL;
  	}
  
  	return dw_ced;
  }
  
  /**
   * dw_apb_clockevent_resume() - resume a clock that has been paused.
   *
   * @dw_ced:	The APB clock to resume.
   */
  void dw_apb_clockevent_resume(struct dw_apb_clock_event_device *dw_ced)
  {
  	enable_irq(dw_ced->timer.irq);
  }
  
  /**
   * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
   *
   * @dw_ced:	The APB clock to stop generating the events.
   */
  void dw_apb_clockevent_stop(struct dw_apb_clock_event_device *dw_ced)
  {
  	free_irq(dw_ced->timer.irq, &dw_ced->ced);
  }
  
  /**
   * dw_apb_clockevent_register() - register the clock with the generic layer
   *
   * @dw_ced:	The APB clock to register as a clock_event_device.
   */
  void dw_apb_clockevent_register(struct dw_apb_clock_event_device *dw_ced)
  {
  	apbt_writel(&dw_ced->timer, 0, APBTMR_N_CONTROL);
  	clockevents_register_device(&dw_ced->ced);
  	apbt_enable_int(&dw_ced->timer);
  }
  
  /**
   * dw_apb_clocksource_start() - start the clocksource counting.
   *
   * @dw_cs:	The clocksource to start.
   *
   * This is used to start the clocksource before registration and can be used
   * to enable calibration of timers.
   */
  void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs)
  {
  	/*
  	 * start count down from 0xffff_ffff. this is done by toggling the
  	 * enable bit then load initial load count to ~0.
  	 */
9f4165dc4   Jisheng Zhang   clocksource/drive...
340
  	u32 ctrl = apbt_readl(&dw_cs->timer, APBTMR_N_CONTROL);
06c3df495   Jamie Iles   clocksource: apb:...
341
342
343
344
345
346
347
348
349
350
351
  
  	ctrl &= ~APBTMR_CONTROL_ENABLE;
  	apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  	apbt_writel(&dw_cs->timer, ~0, APBTMR_N_LOAD_COUNT);
  	/* enable, mask interrupt */
  	ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  	ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  	apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  	/* read it once to get cached counter value initialized */
  	dw_apb_clocksource_read(dw_cs);
  }
a5a1d1c29   Thomas Gleixner   clocksource: Use ...
352
  static u64 __apbt_read_clocksource(struct clocksource *cs)
06c3df495   Jamie Iles   clocksource: apb:...
353
  {
9f4165dc4   Jisheng Zhang   clocksource/drive...
354
  	u32 current_count;
06c3df495   Jamie Iles   clocksource: apb:...
355
356
  	struct dw_apb_clocksource *dw_cs =
  		clocksource_to_dw_apb_clocksource(cs);
39d3611f2   Jisheng Zhang   clocksource/drive...
357
358
  	current_count = apbt_readl_relaxed(&dw_cs->timer,
  					APBTMR_N_CURRENT_VALUE);
06c3df495   Jamie Iles   clocksource: apb:...
359

a5a1d1c29   Thomas Gleixner   clocksource: Use ...
360
  	return (u64)~current_count;
06c3df495   Jamie Iles   clocksource: apb:...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  }
  
  static void apbt_restart_clocksource(struct clocksource *cs)
  {
  	struct dw_apb_clocksource *dw_cs =
  		clocksource_to_dw_apb_clocksource(cs);
  
  	dw_apb_clocksource_start(dw_cs);
  }
  
  /**
   * dw_apb_clocksource_init() - use an APB timer as a clocksource.
   *
   * @rating:	The rating to give the clocksource.
   * @name:	The name for the clocksource.
   * @base:	The I/O base for the timer registers.
   * @freq:	The frequency that the timer counts at.
   *
   * This creates a clocksource using an APB timer but does not yet register it
   * with the clocksource system.  This should be done with
   * dw_apb_clocksource_register() as the next step.
   */
  struct dw_apb_clocksource *
a1330228f   Jamie Iles   dw_apb_timer: con...
384
  dw_apb_clocksource_init(unsigned rating, const char *name, void __iomem *base,
06c3df495   Jamie Iles   clocksource: apb:...
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  			unsigned long freq)
  {
  	struct dw_apb_clocksource *dw_cs = kzalloc(sizeof(*dw_cs), GFP_KERNEL);
  
  	if (!dw_cs)
  		return NULL;
  
  	dw_cs->timer.base = base;
  	dw_cs->timer.freq = freq;
  	dw_cs->cs.name = name;
  	dw_cs->cs.rating = rating;
  	dw_cs->cs.read = __apbt_read_clocksource;
  	dw_cs->cs.mask = CLOCKSOURCE_MASK(32);
  	dw_cs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  	dw_cs->cs.resume = apbt_restart_clocksource;
  
  	return dw_cs;
  }
  
  /**
   * dw_apb_clocksource_register() - register the APB clocksource.
   *
   * @dw_cs:	The clocksource to register.
   */
  void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs)
  {
  	clocksource_register_hz(&dw_cs->cs, dw_cs->timer.freq);
  }
  
  /**
   * dw_apb_clocksource_read() - read the current value of a clocksource.
   *
   * @dw_cs:	The clocksource to read.
   */
a5a1d1c29   Thomas Gleixner   clocksource: Use ...
419
  u64 dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs)
06c3df495   Jamie Iles   clocksource: apb:...
420
  {
a5a1d1c29   Thomas Gleixner   clocksource: Use ...
421
  	return (u64)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);
06c3df495   Jamie Iles   clocksource: apb:...
422
  }