blob: f4dd96ff081a6145432468eb8eede37b891fb377 [file] [log] [blame]
Shawn Guo4e472092010-12-18 21:39:30 +08001/*
2 * Copyright (C) 2000-2001 Deep Blue Solutions
3 * Copyright (C) 2002 Shane Nay (shane@minirl.com)
4 * Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
5 * Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
6 * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 */
22
Shawn Guo39d13672012-04-29 00:02:37 +080023#include <linux/err.h>
Shawn Guo4e472092010-12-18 21:39:30 +080024#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/clockchips.h>
27#include <linux/clk.h>
Shawn Guoeeca6e62012-08-20 08:51:45 +080028#include <linux/of.h>
Shawn Guobe35bd22013-03-25 14:57:41 +080029#include <linux/of_address.h>
Shawn Guoeeca6e62012-08-20 08:51:45 +080030#include <linux/of_irq.h>
Shawn Guo4e472092010-12-18 21:39:30 +080031
32#include <asm/mach/time.h>
Stanislav Meduna67948ad2012-11-08 23:39:14 +010033#include <asm/sched_clock.h>
Shawn Guo4e472092010-12-18 21:39:30 +080034#include <mach/mxs.h>
35#include <mach/common.h>
36
37/*
38 * There are 2 versions of the timrot on Freescale MXS-based SoCs.
39 * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
40 * extends the counter to 32 bits.
41 *
42 * The implementation uses two timers, one for clock_event and
43 * another for clocksource. MX28 uses timrot 0 and 1, while MX23
44 * uses 0 and 2.
45 */
46
47#define MX23_TIMROT_VERSION_OFFSET 0x0a0
48#define MX28_TIMROT_VERSION_OFFSET 0x120
49#define BP_TIMROT_MAJOR_VERSION 24
50#define BV_TIMROT_VERSION_1 0x01
51#define BV_TIMROT_VERSION_2 0x02
52#define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
53
54/*
55 * There are 4 registers for each timrotv2 instance, and 2 registers
56 * for each timrotv1. So address step 0x40 in macros below strides
57 * one instance of timrotv2 while two instances of timrotv1.
58 *
59 * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
60 * on MX28 while timrot2 on MX23.
61 */
62/* common between v1 and v2 */
63#define HW_TIMROT_ROTCTRL 0x00
64#define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
65/* v1 only */
66#define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
67/* v2 only */
68#define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
69#define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
70
71#define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
72#define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
73#define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
74#define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
75#define BP_TIMROT_TIMCTRLn_SELECT 0
Torben Hohn2fb318f2012-12-21 15:06:15 +010076#define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
77#define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
78#define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf
Shawn Guo4e472092010-12-18 21:39:30 +080079
80static struct clock_event_device mxs_clockevent_device;
81static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED;
82
Shawn Guobe35bd22013-03-25 14:57:41 +080083static void __iomem *mxs_timrot_base;
Shawn Guo4e472092010-12-18 21:39:30 +080084static u32 timrot_major_version;
85
86static inline void timrot_irq_disable(void)
87{
88 __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ_EN,
89 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
90}
91
92static inline void timrot_irq_enable(void)
93{
94 __mxs_setl(BM_TIMROT_TIMCTRLn_IRQ_EN,
95 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
96}
97
98static void timrot_irq_acknowledge(void)
99{
100 __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ,
101 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
102}
103
104static cycle_t timrotv1_get_cycles(struct clocksource *cs)
105{
106 return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
107 & 0xffff0000) >> 16);
108}
109
Shawn Guo4e472092010-12-18 21:39:30 +0800110static int timrotv1_set_next_event(unsigned long evt,
111 struct clock_event_device *dev)
112{
113 /* timrot decrements the count */
114 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
115
116 return 0;
117}
118
119static int timrotv2_set_next_event(unsigned long evt,
120 struct clock_event_device *dev)
121{
122 /* timrot decrements the count */
123 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
124
125 return 0;
126}
127
128static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
129{
130 struct clock_event_device *evt = dev_id;
131
132 timrot_irq_acknowledge();
133 evt->event_handler(evt);
134
135 return IRQ_HANDLED;
136}
137
138static struct irqaction mxs_timer_irq = {
139 .name = "MXS Timer Tick",
140 .dev_id = &mxs_clockevent_device,
141 .flags = IRQF_TIMER | IRQF_IRQPOLL,
142 .handler = mxs_timer_interrupt,
143};
144
145#ifdef DEBUG
146static const char *clock_event_mode_label[] const = {
147 [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
148 [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
149 [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
150 [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
151};
152#endif /* DEBUG */
153
154static void mxs_set_mode(enum clock_event_mode mode,
155 struct clock_event_device *evt)
156{
157 /* Disable interrupt in timer module */
158 timrot_irq_disable();
159
160 if (mode != mxs_clockevent_mode) {
161 /* Set event time into the furthest future */
162 if (timrot_is_v1())
163 __raw_writel(0xffff,
164 mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
165 else
166 __raw_writel(0xffffffff,
167 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
168
169 /* Clear pending interrupt */
170 timrot_irq_acknowledge();
171 }
172
173#ifdef DEBUG
174 pr_info("%s: changing mode from %s to %s\n", __func__,
175 clock_event_mode_label[mxs_clockevent_mode],
176 clock_event_mode_label[mode]);
177#endif /* DEBUG */
178
179 /* Remember timer mode */
180 mxs_clockevent_mode = mode;
181
182 switch (mode) {
183 case CLOCK_EVT_MODE_PERIODIC:
184 pr_err("%s: Periodic mode is not implemented\n", __func__);
185 break;
186 case CLOCK_EVT_MODE_ONESHOT:
187 timrot_irq_enable();
188 break;
189 case CLOCK_EVT_MODE_SHUTDOWN:
190 case CLOCK_EVT_MODE_UNUSED:
191 case CLOCK_EVT_MODE_RESUME:
192 /* Left event sources disabled, no more interrupts appear */
193 break;
194 }
195}
196
197static struct clock_event_device mxs_clockevent_device = {
198 .name = "mxs_timrot",
199 .features = CLOCK_EVT_FEAT_ONESHOT,
Shawn Guo4e472092010-12-18 21:39:30 +0800200 .set_mode = mxs_set_mode,
201 .set_next_event = timrotv2_set_next_event,
202 .rating = 200,
203};
204
205static int __init mxs_clockevent_init(struct clk *timer_clk)
206{
Shawn Guo838a2ae2013-01-12 11:50:05 +0000207 if (timrot_is_v1())
Shawn Guo4e472092010-12-18 21:39:30 +0800208 mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
Shawn Guo838a2ae2013-01-12 11:50:05 +0000209 mxs_clockevent_device.cpumask = cpumask_of(0);
210 clockevents_config_and_register(&mxs_clockevent_device,
Torben Hohn93044fa2012-12-21 15:06:16 +0100211 clk_get_rate(timer_clk),
212 timrot_is_v1() ? 0xf : 0x2,
Shawn Guo838a2ae2013-01-12 11:50:05 +0000213 timrot_is_v1() ? 0xfffe : 0xfffffffe);
Shawn Guo4e472092010-12-18 21:39:30 +0800214
215 return 0;
216}
217
218static struct clocksource clocksource_mxs = {
219 .name = "mxs_timer",
220 .rating = 200,
Russell King5c61ddc2011-05-08 17:21:49 +0100221 .read = timrotv1_get_cycles,
222 .mask = CLOCKSOURCE_MASK(16),
Shawn Guo4e472092010-12-18 21:39:30 +0800223 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
224};
225
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100226static u32 notrace mxs_read_sched_clock_v2(void)
227{
228 return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
229}
230
Shawn Guo4e472092010-12-18 21:39:30 +0800231static int __init mxs_clocksource_init(struct clk *timer_clk)
232{
233 unsigned int c = clk_get_rate(timer_clk);
234
Russell King5c61ddc2011-05-08 17:21:49 +0100235 if (timrot_is_v1())
236 clocksource_register_hz(&clocksource_mxs, c);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100237 else {
Russell King5c61ddc2011-05-08 17:21:49 +0100238 clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
239 "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100240 setup_sched_clock(mxs_read_sched_clock_v2, 32, c);
241 }
Shawn Guo4e472092010-12-18 21:39:30 +0800242
243 return 0;
244}
245
Shawn Guo633ef4c2013-03-25 14:53:08 +0800246static void __init mxs_timer_init(struct device_node *np)
Shawn Guo4e472092010-12-18 21:39:30 +0800247{
Shawn Guo50260922012-04-29 00:02:41 +0800248 struct clk *timer_clk;
Shawn Guoeeca6e62012-08-20 08:51:45 +0800249 int irq;
250
Shawn Guobe35bd22013-03-25 14:57:41 +0800251 mxs_timrot_base = of_iomap(np, 0);
252 WARN_ON(!mxs_timrot_base);
253
Shawn Guo2efb9502013-03-25 22:57:14 +0800254 timer_clk = of_clk_get(np, 0);
Shawn Guo50260922012-04-29 00:02:41 +0800255 if (IS_ERR(timer_clk)) {
256 pr_err("%s: failed to get clk\n", __func__);
257 return;
Shawn Guo39d13672012-04-29 00:02:37 +0800258 }
259
Shawn Guoae68f7a2011-12-20 13:50:11 +0800260 clk_prepare_enable(timer_clk);
Shawn Guo4e472092010-12-18 21:39:30 +0800261
262 /*
263 * Initialize timers to a known state
264 */
265 mxs_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
266
267 /* get timrot version */
268 timrot_major_version = __raw_readl(mxs_timrot_base +
Shawn Guo220d2f22013-03-25 19:41:39 +0800269 (of_device_is_compatible(np, "fsl,imx23-timrot") ?
270 MX23_TIMROT_VERSION_OFFSET :
Shawn Guo4e472092010-12-18 21:39:30 +0800271 MX28_TIMROT_VERSION_OFFSET));
272 timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
273
274 /* one for clock_event */
275 __raw_writel((timrot_is_v1() ?
276 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
Torben Hohn2fb318f2012-12-21 15:06:15 +0100277 BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
Shawn Guo4e472092010-12-18 21:39:30 +0800278 BM_TIMROT_TIMCTRLn_UPDATE |
279 BM_TIMROT_TIMCTRLn_IRQ_EN,
280 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
281
282 /* another for clocksource */
283 __raw_writel((timrot_is_v1() ?
284 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
Torben Hohn2fb318f2012-12-21 15:06:15 +0100285 BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
Shawn Guo4e472092010-12-18 21:39:30 +0800286 BM_TIMROT_TIMCTRLn_RELOAD,
287 mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
288
289 /* set clocksource timer fixed count to the maximum */
290 if (timrot_is_v1())
291 __raw_writel(0xffff,
292 mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
293 else
294 __raw_writel(0xffffffff,
295 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
296
297 /* init and register the timer to the framework */
298 mxs_clocksource_init(timer_clk);
299 mxs_clockevent_init(timer_clk);
300
301 /* Make irqs happen */
Shawn Guoeeca6e62012-08-20 08:51:45 +0800302 irq = irq_of_parse_and_map(np, 0);
Shawn Guo4e472092010-12-18 21:39:30 +0800303 setup_irq(irq, &mxs_timer_irq);
304}
Shawn Guo633ef4c2013-03-25 14:53:08 +0800305CLOCKSOURCE_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init)