Blame view

drivers/clocksource/timer-atlas7.c 8.68 KB
4898de3d1   Barry Song   ARM: PRIMA2: add ...
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   * System timer for CSR SiRFprimaII
   *
   * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
   *
   * Licensed under GPLv2 or later.
   */
  
  #include <linux/kernel.h>
  #include <linux/interrupt.h>
  #include <linux/clockchips.h>
  #include <linux/clocksource.h>
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
13
  #include <linux/cpu.h>
4898de3d1   Barry Song   ARM: PRIMA2: add ...
14
15
16
17
18
19
20
  #include <linux/bitops.h>
  #include <linux/irq.h>
  #include <linux/clk.h>
  #include <linux/slab.h>
  #include <linux/of.h>
  #include <linux/of_irq.h>
  #include <linux/of_address.h>
38ff87f77   Stephen Boyd   sched_clock: Make...
21
  #include <linux/sched_clock.h>
980c51ab0   Uwe Kleine-König   clocksource: sirf...
22

4898de3d1   Barry Song   ARM: PRIMA2: add ...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #define SIRFSOC_TIMER_32COUNTER_0_CTRL			0x0000
  #define SIRFSOC_TIMER_32COUNTER_1_CTRL			0x0004
  #define SIRFSOC_TIMER_MATCH_0				0x0018
  #define SIRFSOC_TIMER_MATCH_1				0x001c
  #define SIRFSOC_TIMER_COUNTER_0				0x0048
  #define SIRFSOC_TIMER_COUNTER_1				0x004c
  #define SIRFSOC_TIMER_INTR_STATUS			0x0060
  #define SIRFSOC_TIMER_WATCHDOG_EN			0x0064
  #define SIRFSOC_TIMER_64COUNTER_CTRL			0x0068
  #define SIRFSOC_TIMER_64COUNTER_LO			0x006c
  #define SIRFSOC_TIMER_64COUNTER_HI			0x0070
  #define SIRFSOC_TIMER_64COUNTER_LOAD_LO			0x0074
  #define SIRFSOC_TIMER_64COUNTER_LOAD_HI			0x0078
  #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO		0x007c
  #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI		0x0080
  
  #define SIRFSOC_TIMER_REG_CNT 6
5833ac986   Barry Song   clocksource: marc...
40
  static unsigned long atlas7_timer_rate;
ef89af1f4   Yanchang Li   clocksource: sirf...
41

4898de3d1   Barry Song   ARM: PRIMA2: add ...
42
43
44
45
46
47
48
49
50
51
52
53
  static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
  	SIRFSOC_TIMER_WATCHDOG_EN,
  	SIRFSOC_TIMER_32COUNTER_0_CTRL,
  	SIRFSOC_TIMER_32COUNTER_1_CTRL,
  	SIRFSOC_TIMER_64COUNTER_CTRL,
  	SIRFSOC_TIMER_64COUNTER_RLATCHED_LO,
  	SIRFSOC_TIMER_64COUNTER_RLATCHED_HI,
  };
  
  static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
  
  static void __iomem *sirfsoc_timer_base;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
54
55
56
57
58
59
60
61
62
63
64
  
  /* disable count and interrupt */
  static inline void sirfsoc_timer_count_disable(int idx)
  {
  	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7,
  		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
  }
  
  /* enable count and interrupt */
  static inline void sirfsoc_timer_count_enable(int idx)
  {
28cf35675   Hao Liu   clocksource: sirf...
65
  	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3,
4898de3d1   Barry Song   ARM: PRIMA2: add ...
66
67
68
69
70
71
72
73
74
75
76
  		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
  }
  
  /* timer interrupt handler */
  static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
  {
  	struct clock_event_device *ce = dev_id;
  	int cpu = smp_processor_id();
  
  	/* clear timer interrupt */
  	writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
1e729d378   Viresh Kumar   clockevents/drive...
77
  	if (clockevent_state_oneshot(ce))
4898de3d1   Barry Song   ARM: PRIMA2: add ...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  		sirfsoc_timer_count_disable(cpu);
  
  	ce->event_handler(ce);
  
  	return IRQ_HANDLED;
  }
  
  /* read 64-bit timer counter */
  static cycle_t sirfsoc_timer_read(struct clocksource *cs)
  {
  	u64 cycles;
  
  	writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  			BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  
  	cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI);
  	cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO);
  
  	return cycles;
  }
  
  static int sirfsoc_timer_set_next_event(unsigned long delta,
  	struct clock_event_device *ce)
  {
  	int cpu = smp_processor_id();
28cf35675   Hao Liu   clocksource: sirf...
103
104
  	/* disable timer first, then modify the related registers */
  	sirfsoc_timer_count_disable(cpu);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
105
106
107
108
109
110
111
112
113
114
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
  		4 * cpu);
  	writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
  		4 * cpu);
  
  	/* enable the tick */
  	sirfsoc_timer_count_enable(cpu);
  
  	return 0;
  }
1e729d378   Viresh Kumar   clockevents/drive...
115
116
  /* Oneshot is enabled in set_next_event */
  static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
4898de3d1   Barry Song   ARM: PRIMA2: add ...
117
  {
4898de3d1   Barry Song   ARM: PRIMA2: add ...
118
  	sirfsoc_timer_count_disable(smp_processor_id());
1e729d378   Viresh Kumar   clockevents/drive...
119
  	return 0;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  }
  
  static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  {
  	int i;
  
  	for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  		sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  }
  
  static void sirfsoc_clocksource_resume(struct clocksource *cs)
  {
  	int i;
  
  	for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
  		writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  
  	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
  		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
  		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  
  	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  }
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
145
  static struct clock_event_device __percpu *sirfsoc_clockevent;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  
  static struct clocksource sirfsoc_clocksource = {
  	.name = "sirfsoc_clocksource",
  	.rating = 200,
  	.mask = CLOCKSOURCE_MASK(64),
  	.flags = CLOCK_SOURCE_IS_CONTINUOUS,
  	.read = sirfsoc_timer_read,
  	.suspend = sirfsoc_clocksource_suspend,
  	.resume = sirfsoc_clocksource_resume,
  };
  
  static struct irqaction sirfsoc_timer_irq = {
  	.name = "sirfsoc_timer0",
  	.flags = IRQF_TIMER | IRQF_NOBALANCING,
  	.handler = sirfsoc_timer_interrupt,
4898de3d1   Barry Song   ARM: PRIMA2: add ...
161
  };
4898de3d1   Barry Song   ARM: PRIMA2: add ...
162
163
164
165
166
  static struct irqaction sirfsoc_timer1_irq = {
  	.name = "sirfsoc_timer1",
  	.flags = IRQF_TIMER | IRQF_NOBALANCING,
  	.handler = sirfsoc_timer_interrupt,
  };
8c37bb3ac   Paul Gortmaker   clocksource+irqch...
167
  static int sirfsoc_local_timer_setup(struct clock_event_device *ce)
4898de3d1   Barry Song   ARM: PRIMA2: add ...
168
  {
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
169
170
171
172
173
174
175
  	int cpu = smp_processor_id();
  	struct irqaction *action;
  
  	if (cpu == 0)
  		action = &sirfsoc_timer_irq;
  	else
  		action = &sirfsoc_timer1_irq;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
176

05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
177
  	ce->irq = action->irq;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
178
  	ce->name = "local_timer";
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
179
180
  	ce->features = CLOCK_EVT_FEAT_ONESHOT;
  	ce->rating = 200;
1e729d378   Viresh Kumar   clockevents/drive...
181
182
183
  	ce->set_state_shutdown = sirfsoc_timer_shutdown;
  	ce->set_state_oneshot = sirfsoc_timer_shutdown;
  	ce->tick_resume = sirfsoc_timer_shutdown;
4898de3d1   Barry Song   ARM: PRIMA2: add ...
184
  	ce->set_next_event = sirfsoc_timer_set_next_event;
5833ac986   Barry Song   clocksource: marc...
185
  	clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60);
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
186
187
188
  	ce->max_delta_ns = clockevent_delta2ns(-2, ce);
  	ce->min_delta_ns = clockevent_delta2ns(2, ce);
  	ce->cpumask = cpumask_of(cpu);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
189

05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
190
191
  	action->dev_id = ce;
  	BUG_ON(setup_irq(ce->irq, action));
f214be50f   Zhiwu Song   clocksource: marc...
192
  	irq_force_affinity(action->irq, cpumask_of(cpu));
4898de3d1   Barry Song   ARM: PRIMA2: add ...
193
194
195
196
197
198
199
  
  	clockevents_register_device(ce);
  	return 0;
  }
  
  static void sirfsoc_local_timer_stop(struct clock_event_device *ce)
  {
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
200
  	int cpu = smp_processor_id();
4898de3d1   Barry Song   ARM: PRIMA2: add ...
201
  	sirfsoc_timer_count_disable(1);
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
202
203
204
205
  	if (cpu == 0)
  		remove_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq);
  	else
  		remove_irq(sirfsoc_timer1_irq.irq, &sirfsoc_timer1_irq);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
206
  }
47dcd3563   Olof Johansson   Merge tag 'remove...
207
208
  static int sirfsoc_cpu_notify(struct notifier_block *self,
  			      unsigned long action, void *hcpu)
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  {
  	/*
  	 * Grab cpu pointer in each case to avoid spurious
  	 * preemptible warnings
  	 */
  	switch (action & ~CPU_TASKS_FROZEN) {
  	case CPU_STARTING:
  		sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent));
  		break;
  	case CPU_DYING:
  		sirfsoc_local_timer_stop(this_cpu_ptr(sirfsoc_clockevent));
  		break;
  	}
  
  	return NOTIFY_OK;
  }
47dcd3563   Olof Johansson   Merge tag 'remove...
225
  static struct notifier_block sirfsoc_cpu_nb = {
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
226
  	.notifier_call = sirfsoc_cpu_notify,
4898de3d1   Barry Song   ARM: PRIMA2: add ...
227
  };
4898de3d1   Barry Song   ARM: PRIMA2: add ...
228
229
230
  
  static void __init sirfsoc_clockevent_init(void)
  {
05a654855   Stephen Boyd   ARM: PRIMA2: Divo...
231
232
233
234
235
236
237
  	sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
  	BUG_ON(!sirfsoc_clockevent);
  
  	BUG_ON(register_cpu_notifier(&sirfsoc_cpu_nb));
  
  	/* Immediately configure the timer on the boot CPU */
  	sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent));
4898de3d1   Barry Song   ARM: PRIMA2: add ...
238
239
240
  }
  
  /* initialize the kernel jiffy timer source */
5833ac986   Barry Song   clocksource: marc...
241
  static void __init sirfsoc_atlas7_timer_init(struct device_node *np)
4898de3d1   Barry Song   ARM: PRIMA2: add ...
242
  {
4898de3d1   Barry Song   ARM: PRIMA2: add ...
243
  	struct clk *clk;
c7cff54d5   Zhiwu Song   clocksource:sirf:...
244
  	clk = of_clk_get(np, 0);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
245
  	BUG_ON(IS_ERR(clk));
38941522e   Zhiwu Song   clocksource: sirf...
246
247
  
  	BUG_ON(clk_prepare_enable(clk));
5833ac986   Barry Song   clocksource: marc...
248
  	atlas7_timer_rate = clk_get_rate(clk);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
249

ef89af1f4   Yanchang Li   clocksource: sirf...
250
251
252
253
  	/* timer dividers: 0, not divided */
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
254
255
256
257
258
259
260
261
262
263
264
  
  	/* Initialize timer counters to 0 */
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
  	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
  
  	/* Clear all interrupts */
  	writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
5833ac986   Barry Song   clocksource: marc...
265
  	BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
4898de3d1   Barry Song   ARM: PRIMA2: add ...
266

4898de3d1   Barry Song   ARM: PRIMA2: add ...
267
268
  	sirfsoc_clockevent_init();
  }
275786b71   Arnd Bergmann   ARM: sirf: use cl...
269
  static void __init sirfsoc_of_timer_init(struct device_node *np)
4898de3d1   Barry Song   ARM: PRIMA2: add ...
270
  {
4898de3d1   Barry Song   ARM: PRIMA2: add ...
271
272
273
274
275
276
277
278
279
  	sirfsoc_timer_base = of_iomap(np, 0);
  	if (!sirfsoc_timer_base)
  		panic("unable to map timer cpu registers
  ");
  
  	sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  	if (!sirfsoc_timer_irq.irq)
  		panic("No irq passed for timer0 via DT
  ");
4898de3d1   Barry Song   ARM: PRIMA2: add ...
280
281
282
283
  	sirfsoc_timer1_irq.irq = irq_of_parse_and_map(np, 1);
  	if (!sirfsoc_timer1_irq.irq)
  		panic("No irq passed for timer1 via DT
  ");
4898de3d1   Barry Song   ARM: PRIMA2: add ...
284

5833ac986   Barry Song   clocksource: marc...
285
  	sirfsoc_atlas7_timer_init(np);
4898de3d1   Barry Song   ARM: PRIMA2: add ...
286
  }
5833ac986   Barry Song   clocksource: marc...
287
  CLOCKSOURCE_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);