Blame view

drivers/clocksource/mxs_timer.c 8.61 KB
4e472096f   Shawn Guo   ARM: mxs: Add tim...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /*
   *  Copyright (C) 2000-2001 Deep Blue Solutions
   *  Copyright (C) 2002 Shane Nay (shane@minirl.com)
   *  Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
   *  Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
   *  Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   * MA 02110-1301, USA.
   */
39d1367e1   Shawn Guo   ARM: mxs: request...
22
  #include <linux/err.h>
4e472096f   Shawn Guo   ARM: mxs: Add tim...
23
24
25
26
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/clockchips.h>
  #include <linux/clk.h>
eeca6e604   Shawn Guo   ARM: mxs: retriev...
27
  #include <linux/of.h>
be35bd2bc   Shawn Guo   ARM: mxs: get tim...
28
  #include <linux/of_address.h>
eeca6e604   Shawn Guo   ARM: mxs: retriev...
29
  #include <linux/of_irq.h>
7a9c39f78   Shawn Guo   ARM: mxs: select ...
30
  #include <linux/stmp_device.h>
38ff87f77   Stephen Boyd   sched_clock: Make...
31
  #include <linux/sched_clock.h>
4e472096f   Shawn Guo   ARM: mxs: Add tim...
32
33
  
  #include <asm/mach/time.h>
4e472096f   Shawn Guo   ARM: mxs: Add tim...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  
  /*
   * There are 2 versions of the timrot on Freescale MXS-based SoCs.
   * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
   * extends the counter to 32 bits.
   *
   * The implementation uses two timers, one for clock_event and
   * another for clocksource. MX28 uses timrot 0 and 1, while MX23
   * uses 0 and 2.
   */
  
  #define MX23_TIMROT_VERSION_OFFSET	0x0a0
  #define MX28_TIMROT_VERSION_OFFSET	0x120
  #define BP_TIMROT_MAJOR_VERSION		24
  #define BV_TIMROT_VERSION_1		0x01
  #define BV_TIMROT_VERSION_2		0x02
  #define timrot_is_v1()	(timrot_major_version == BV_TIMROT_VERSION_1)
  
  /*
   * There are 4 registers for each timrotv2 instance, and 2 registers
   * for each timrotv1. So address step 0x40 in macros below strides
   * one instance of timrotv2 while two instances of timrotv1.
   *
   * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
   * on MX28 while timrot2 on MX23.
   */
  /* common between v1 and v2 */
  #define HW_TIMROT_ROTCTRL		0x00
  #define HW_TIMROT_TIMCTRLn(n)		(0x20 + (n) * 0x40)
  /* v1 only */
  #define HW_TIMROT_TIMCOUNTn(n)		(0x30 + (n) * 0x40)
  /* v2 only */
  #define HW_TIMROT_RUNNING_COUNTn(n)	(0x30 + (n) * 0x40)
  #define HW_TIMROT_FIXED_COUNTn(n)	(0x40 + (n) * 0x40)
  
  #define BM_TIMROT_TIMCTRLn_RELOAD	(1 << 6)
  #define BM_TIMROT_TIMCTRLn_UPDATE	(1 << 7)
  #define BM_TIMROT_TIMCTRLn_IRQ_EN	(1 << 14)
  #define BM_TIMROT_TIMCTRLn_IRQ		(1 << 15)
  #define BP_TIMROT_TIMCTRLn_SELECT	0
2fb318ff7   Torben Hohn   ARM: mxs: use apb...
74
75
76
  #define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL		0x8
  #define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL		0xb
  #define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS	0xf
4e472096f   Shawn Guo   ARM: mxs: Add tim...
77
78
79
  
  static struct clock_event_device mxs_clockevent_device;
  static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED;
be35bd2bc   Shawn Guo   ARM: mxs: get tim...
80
  static void __iomem *mxs_timrot_base;
4e472096f   Shawn Guo   ARM: mxs: Add tim...
81
82
83
84
  static u32 timrot_major_version;
  
  static inline void timrot_irq_disable(void)
  {
7a9c39f78   Shawn Guo   ARM: mxs: select ...
85
86
  	__raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
  		     HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
87
88
89
90
  }
  
  static inline void timrot_irq_enable(void)
  {
7a9c39f78   Shawn Guo   ARM: mxs: select ...
91
92
  	__raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
  		     HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_SET);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
93
94
95
96
  }
  
  static void timrot_irq_acknowledge(void)
  {
7a9c39f78   Shawn Guo   ARM: mxs: select ...
97
98
  	__raw_writel(BM_TIMROT_TIMCTRLn_IRQ, mxs_timrot_base +
  		     HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
99
100
101
102
103
104
105
  }
  
  static cycle_t timrotv1_get_cycles(struct clocksource *cs)
  {
  	return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
  			& 0xffff0000) >> 16);
  }
4e472096f   Shawn Guo   ARM: mxs: Add tim...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
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
170
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
  static int timrotv1_set_next_event(unsigned long evt,
  					struct clock_event_device *dev)
  {
  	/* timrot decrements the count */
  	__raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
  
  	return 0;
  }
  
  static int timrotv2_set_next_event(unsigned long evt,
  					struct clock_event_device *dev)
  {
  	/* timrot decrements the count */
  	__raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
  
  	return 0;
  }
  
  static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
  {
  	struct clock_event_device *evt = dev_id;
  
  	timrot_irq_acknowledge();
  	evt->event_handler(evt);
  
  	return IRQ_HANDLED;
  }
  
  static struct irqaction mxs_timer_irq = {
  	.name		= "MXS Timer Tick",
  	.dev_id		= &mxs_clockevent_device,
  	.flags		= IRQF_TIMER | IRQF_IRQPOLL,
  	.handler	= mxs_timer_interrupt,
  };
  
  #ifdef DEBUG
  static const char *clock_event_mode_label[] const = {
  	[CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
  	[CLOCK_EVT_MODE_ONESHOT]  = "CLOCK_EVT_MODE_ONESHOT",
  	[CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
  	[CLOCK_EVT_MODE_UNUSED]   = "CLOCK_EVT_MODE_UNUSED"
  };
  #endif /* DEBUG */
  
  static void mxs_set_mode(enum clock_event_mode mode,
  				struct clock_event_device *evt)
  {
  	/* Disable interrupt in timer module */
  	timrot_irq_disable();
  
  	if (mode != mxs_clockevent_mode) {
  		/* Set event time into the furthest future */
  		if (timrot_is_v1())
  			__raw_writel(0xffff,
  				mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  		else
  			__raw_writel(0xffffffff,
  				mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  
  		/* Clear pending interrupt */
  		timrot_irq_acknowledge();
  	}
  
  #ifdef DEBUG
  	pr_info("%s: changing mode from %s to %s
  ", __func__,
  		clock_event_mode_label[mxs_clockevent_mode],
  		clock_event_mode_label[mode]);
  #endif /* DEBUG */
  
  	/* Remember timer mode */
  	mxs_clockevent_mode = mode;
  
  	switch (mode) {
  	case CLOCK_EVT_MODE_PERIODIC:
  		pr_err("%s: Periodic mode is not implemented
  ", __func__);
  		break;
  	case CLOCK_EVT_MODE_ONESHOT:
  		timrot_irq_enable();
  		break;
  	case CLOCK_EVT_MODE_SHUTDOWN:
  	case CLOCK_EVT_MODE_UNUSED:
  	case CLOCK_EVT_MODE_RESUME:
  		/* Left event sources disabled, no more interrupts appear */
  		break;
  	}
  }
  
  static struct clock_event_device mxs_clockevent_device = {
  	.name		= "mxs_timrot",
  	.features	= CLOCK_EVT_FEAT_ONESHOT,
4e472096f   Shawn Guo   ARM: mxs: Add tim...
198
199
200
201
202
203
204
  	.set_mode	= mxs_set_mode,
  	.set_next_event	= timrotv2_set_next_event,
  	.rating		= 200,
  };
  
  static int __init mxs_clockevent_init(struct clk *timer_clk)
  {
838a2ae80   Shawn Guo   ARM: use clockeve...
205
  	if (timrot_is_v1())
4e472096f   Shawn Guo   ARM: mxs: Add tim...
206
  		mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
838a2ae80   Shawn Guo   ARM: use clockeve...
207
208
  	mxs_clockevent_device.cpumask = cpumask_of(0);
  	clockevents_config_and_register(&mxs_clockevent_device,
93044fa15   Torben Hohn   ARM: mxs: decreas...
209
210
  					clk_get_rate(timer_clk),
  					timrot_is_v1() ? 0xf : 0x2,
838a2ae80   Shawn Guo   ARM: use clockeve...
211
  					timrot_is_v1() ? 0xfffe : 0xfffffffe);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
212
213
214
215
216
217
218
  
  	return 0;
  }
  
  static struct clocksource clocksource_mxs = {
  	.name		= "mxs_timer",
  	.rating		= 200,
5c61ddcfa   Russell King   clocksource: conv...
219
220
  	.read		= timrotv1_get_cycles,
  	.mask		= CLOCKSOURCE_MASK(16),
4e472096f   Shawn Guo   ARM: mxs: Add tim...
221
222
  	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
  };
fcfca6ef6   Stephen Boyd   clocksource: mxs_...
223
  static u64 notrace mxs_read_sched_clock_v2(void)
67948adad   Stanislav Meduna   ARM: mxs: Setup s...
224
225
226
  {
  	return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
  }
4e472096f   Shawn Guo   ARM: mxs: Add tim...
227
228
229
  static int __init mxs_clocksource_init(struct clk *timer_clk)
  {
  	unsigned int c = clk_get_rate(timer_clk);
5c61ddcfa   Russell King   clocksource: conv...
230
231
  	if (timrot_is_v1())
  		clocksource_register_hz(&clocksource_mxs, c);
67948adad   Stanislav Meduna   ARM: mxs: Setup s...
232
  	else {
5c61ddcfa   Russell King   clocksource: conv...
233
234
  		clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
  			"mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
fcfca6ef6   Stephen Boyd   clocksource: mxs_...
235
  		sched_clock_register(mxs_read_sched_clock_v2, 32, c);
67948adad   Stanislav Meduna   ARM: mxs: Setup s...
236
  	}
4e472096f   Shawn Guo   ARM: mxs: Add tim...
237
238
239
  
  	return 0;
  }
633ef4c7d   Shawn Guo   ARM: mxs: use CLK...
240
  static void __init mxs_timer_init(struct device_node *np)
4e472096f   Shawn Guo   ARM: mxs: Add tim...
241
  {
50260924a   Shawn Guo   ARM: mxs: remove ...
242
  	struct clk *timer_clk;
eeca6e604   Shawn Guo   ARM: mxs: retriev...
243
  	int irq;
be35bd2bc   Shawn Guo   ARM: mxs: get tim...
244
245
  	mxs_timrot_base = of_iomap(np, 0);
  	WARN_ON(!mxs_timrot_base);
2efb95046   Shawn Guo   ARM: mxs: look up...
246
  	timer_clk = of_clk_get(np, 0);
50260924a   Shawn Guo   ARM: mxs: remove ...
247
248
249
250
  	if (IS_ERR(timer_clk)) {
  		pr_err("%s: failed to get clk
  ", __func__);
  		return;
39d1367e1   Shawn Guo   ARM: mxs: request...
251
  	}
ae68f7af0   Shawn Guo   ARM: mxs: convert...
252
  	clk_prepare_enable(timer_clk);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
253
254
255
256
  
  	/*
  	 * Initialize timers to a known state
  	 */
7a9c39f78   Shawn Guo   ARM: mxs: select ...
257
  	stmp_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
258
259
260
  
  	/* get timrot version */
  	timrot_major_version = __raw_readl(mxs_timrot_base +
220d2f269   Shawn Guo   ARM: mxs: remove ...
261
262
  			(of_device_is_compatible(np, "fsl,imx23-timrot") ?
  						MX23_TIMROT_VERSION_OFFSET :
4e472096f   Shawn Guo   ARM: mxs: Add tim...
263
264
265
266
267
268
  						MX28_TIMROT_VERSION_OFFSET));
  	timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
  
  	/* one for clock_event */
  	__raw_writel((timrot_is_v1() ?
  			BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
2fb318ff7   Torben Hohn   ARM: mxs: use apb...
269
  			BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
4e472096f   Shawn Guo   ARM: mxs: Add tim...
270
271
272
273
274
275
276
  			BM_TIMROT_TIMCTRLn_UPDATE |
  			BM_TIMROT_TIMCTRLn_IRQ_EN,
  			mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  
  	/* another for clocksource */
  	__raw_writel((timrot_is_v1() ?
  			BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
2fb318ff7   Torben Hohn   ARM: mxs: use apb...
277
  			BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
4e472096f   Shawn Guo   ARM: mxs: Add tim...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  			BM_TIMROT_TIMCTRLn_RELOAD,
  			mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
  
  	/* set clocksource timer fixed count to the maximum */
  	if (timrot_is_v1())
  		__raw_writel(0xffff,
  			mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  	else
  		__raw_writel(0xffffffff,
  			mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  
  	/* init and register the timer to the framework */
  	mxs_clocksource_init(timer_clk);
  	mxs_clockevent_init(timer_clk);
  
  	/* Make irqs happen */
eeca6e604   Shawn Guo   ARM: mxs: retriev...
294
  	irq = irq_of_parse_and_map(np, 0);
4e472096f   Shawn Guo   ARM: mxs: Add tim...
295
296
  	setup_irq(irq, &mxs_timer_irq);
  }
e933a1a12   Fabio Estevam   clocksource: mxs_...
297
  CLOCKSOURCE_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init);