blob: 856f4c7960618dad52fac637bf17ad21792b6acd [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>
29#include <linux/of_irq.h>
Shawn Guo4e472092010-12-18 21:39:30 +080030
31#include <asm/mach/time.h>
Stanislav Meduna67948ad2012-11-08 23:39:14 +010032#include <asm/sched_clock.h>
Shawn Guo4e472092010-12-18 21:39:30 +080033#include <mach/mxs.h>
34#include <mach/common.h>
35
36/*
37 * There are 2 versions of the timrot on Freescale MXS-based SoCs.
38 * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
39 * extends the counter to 32 bits.
40 *
41 * The implementation uses two timers, one for clock_event and
42 * another for clocksource. MX28 uses timrot 0 and 1, while MX23
43 * uses 0 and 2.
44 */
45
46#define MX23_TIMROT_VERSION_OFFSET 0x0a0
47#define MX28_TIMROT_VERSION_OFFSET 0x120
48#define BP_TIMROT_MAJOR_VERSION 24
49#define BV_TIMROT_VERSION_1 0x01
50#define BV_TIMROT_VERSION_2 0x02
51#define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
52
53/*
54 * There are 4 registers for each timrotv2 instance, and 2 registers
55 * for each timrotv1. So address step 0x40 in macros below strides
56 * one instance of timrotv2 while two instances of timrotv1.
57 *
58 * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
59 * on MX28 while timrot2 on MX23.
60 */
61/* common between v1 and v2 */
62#define HW_TIMROT_ROTCTRL 0x00
63#define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
64/* v1 only */
65#define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
66/* v2 only */
67#define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
68#define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
69
70#define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
71#define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
72#define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
73#define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
74#define BP_TIMROT_TIMCTRLn_SELECT 0
75#define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
76#define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
77
78static struct clock_event_device mxs_clockevent_device;
79static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED;
80
81static void __iomem *mxs_timrot_base = MXS_IO_ADDRESS(MXS_TIMROT_BASE_ADDR);
82static u32 timrot_major_version;
83
84static inline void timrot_irq_disable(void)
85{
86 __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ_EN,
87 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
88}
89
90static inline void timrot_irq_enable(void)
91{
92 __mxs_setl(BM_TIMROT_TIMCTRLn_IRQ_EN,
93 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
94}
95
96static void timrot_irq_acknowledge(void)
97{
98 __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ,
99 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
100}
101
102static cycle_t timrotv1_get_cycles(struct clocksource *cs)
103{
104 return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
105 & 0xffff0000) >> 16);
106}
107
Shawn Guo4e472092010-12-18 21:39:30 +0800108static int timrotv1_set_next_event(unsigned long evt,
109 struct clock_event_device *dev)
110{
111 /* timrot decrements the count */
112 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
113
114 return 0;
115}
116
117static int timrotv2_set_next_event(unsigned long evt,
118 struct clock_event_device *dev)
119{
120 /* timrot decrements the count */
121 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
122
123 return 0;
124}
125
126static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
127{
128 struct clock_event_device *evt = dev_id;
129
130 timrot_irq_acknowledge();
131 evt->event_handler(evt);
132
133 return IRQ_HANDLED;
134}
135
136static struct irqaction mxs_timer_irq = {
137 .name = "MXS Timer Tick",
138 .dev_id = &mxs_clockevent_device,
139 .flags = IRQF_TIMER | IRQF_IRQPOLL,
140 .handler = mxs_timer_interrupt,
141};
142
143#ifdef DEBUG
144static const char *clock_event_mode_label[] const = {
145 [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
146 [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
147 [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
148 [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
149};
150#endif /* DEBUG */
151
152static void mxs_set_mode(enum clock_event_mode mode,
153 struct clock_event_device *evt)
154{
155 /* Disable interrupt in timer module */
156 timrot_irq_disable();
157
158 if (mode != mxs_clockevent_mode) {
159 /* Set event time into the furthest future */
160 if (timrot_is_v1())
161 __raw_writel(0xffff,
162 mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
163 else
164 __raw_writel(0xffffffff,
165 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
166
167 /* Clear pending interrupt */
168 timrot_irq_acknowledge();
169 }
170
171#ifdef DEBUG
172 pr_info("%s: changing mode from %s to %s\n", __func__,
173 clock_event_mode_label[mxs_clockevent_mode],
174 clock_event_mode_label[mode]);
175#endif /* DEBUG */
176
177 /* Remember timer mode */
178 mxs_clockevent_mode = mode;
179
180 switch (mode) {
181 case CLOCK_EVT_MODE_PERIODIC:
182 pr_err("%s: Periodic mode is not implemented\n", __func__);
183 break;
184 case CLOCK_EVT_MODE_ONESHOT:
185 timrot_irq_enable();
186 break;
187 case CLOCK_EVT_MODE_SHUTDOWN:
188 case CLOCK_EVT_MODE_UNUSED:
189 case CLOCK_EVT_MODE_RESUME:
190 /* Left event sources disabled, no more interrupts appear */
191 break;
192 }
193}
194
195static struct clock_event_device mxs_clockevent_device = {
196 .name = "mxs_timrot",
197 .features = CLOCK_EVT_FEAT_ONESHOT,
198 .shift = 32,
199 .set_mode = mxs_set_mode,
200 .set_next_event = timrotv2_set_next_event,
201 .rating = 200,
202};
203
204static int __init mxs_clockevent_init(struct clk *timer_clk)
205{
206 unsigned int c = clk_get_rate(timer_clk);
207
208 mxs_clockevent_device.mult =
209 div_sc(c, NSEC_PER_SEC, mxs_clockevent_device.shift);
210 mxs_clockevent_device.cpumask = cpumask_of(0);
211 if (timrot_is_v1()) {
212 mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
213 mxs_clockevent_device.max_delta_ns =
214 clockevent_delta2ns(0xfffe, &mxs_clockevent_device);
215 mxs_clockevent_device.min_delta_ns =
216 clockevent_delta2ns(0xf, &mxs_clockevent_device);
217 } else {
218 mxs_clockevent_device.max_delta_ns =
219 clockevent_delta2ns(0xfffffffe, &mxs_clockevent_device);
220 mxs_clockevent_device.min_delta_ns =
221 clockevent_delta2ns(0xf, &mxs_clockevent_device);
222 }
223
224 clockevents_register_device(&mxs_clockevent_device);
225
226 return 0;
227}
228
229static struct clocksource clocksource_mxs = {
230 .name = "mxs_timer",
231 .rating = 200,
Russell King5c61ddc2011-05-08 17:21:49 +0100232 .read = timrotv1_get_cycles,
233 .mask = CLOCKSOURCE_MASK(16),
Shawn Guo4e472092010-12-18 21:39:30 +0800234 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
235};
236
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100237static u32 notrace mxs_read_sched_clock_v2(void)
238{
239 return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
240}
241
Shawn Guo4e472092010-12-18 21:39:30 +0800242static int __init mxs_clocksource_init(struct clk *timer_clk)
243{
244 unsigned int c = clk_get_rate(timer_clk);
245
Russell King5c61ddc2011-05-08 17:21:49 +0100246 if (timrot_is_v1())
247 clocksource_register_hz(&clocksource_mxs, c);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100248 else {
Russell King5c61ddc2011-05-08 17:21:49 +0100249 clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
250 "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100251 setup_sched_clock(mxs_read_sched_clock_v2, 32, c);
252 }
Shawn Guo4e472092010-12-18 21:39:30 +0800253
254 return 0;
255}
256
Shawn Guoeeca6e62012-08-20 08:51:45 +0800257void __init mxs_timer_init(void)
Shawn Guo4e472092010-12-18 21:39:30 +0800258{
Shawn Guoeeca6e62012-08-20 08:51:45 +0800259 struct device_node *np;
Shawn Guo50260922012-04-29 00:02:41 +0800260 struct clk *timer_clk;
Shawn Guoeeca6e62012-08-20 08:51:45 +0800261 int irq;
262
263 np = of_find_compatible_node(NULL, NULL, "fsl,timrot");
264 if (!np) {
265 pr_err("%s: failed find timrot node\n", __func__);
266 return;
267 }
Shawn Guo50260922012-04-29 00:02:41 +0800268
269 timer_clk = clk_get_sys("timrot", NULL);
270 if (IS_ERR(timer_clk)) {
271 pr_err("%s: failed to get clk\n", __func__);
272 return;
Shawn Guo39d13672012-04-29 00:02:37 +0800273 }
274
Shawn Guoae68f7a2011-12-20 13:50:11 +0800275 clk_prepare_enable(timer_clk);
Shawn Guo4e472092010-12-18 21:39:30 +0800276
277 /*
278 * Initialize timers to a known state
279 */
280 mxs_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
281
282 /* get timrot version */
283 timrot_major_version = __raw_readl(mxs_timrot_base +
284 (cpu_is_mx23() ? MX23_TIMROT_VERSION_OFFSET :
285 MX28_TIMROT_VERSION_OFFSET));
286 timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
287
288 /* one for clock_event */
289 __raw_writel((timrot_is_v1() ?
290 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
291 BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL) |
292 BM_TIMROT_TIMCTRLn_UPDATE |
293 BM_TIMROT_TIMCTRLn_IRQ_EN,
294 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
295
296 /* another for clocksource */
297 __raw_writel((timrot_is_v1() ?
298 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
299 BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL) |
300 BM_TIMROT_TIMCTRLn_RELOAD,
301 mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
302
303 /* set clocksource timer fixed count to the maximum */
304 if (timrot_is_v1())
305 __raw_writel(0xffff,
306 mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
307 else
308 __raw_writel(0xffffffff,
309 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
310
311 /* init and register the timer to the framework */
312 mxs_clocksource_init(timer_clk);
313 mxs_clockevent_init(timer_clk);
314
315 /* Make irqs happen */
Shawn Guoeeca6e62012-08-20 08:51:45 +0800316 irq = irq_of_parse_and_map(np, 0);
Shawn Guo4e472092010-12-18 21:39:30 +0800317 setup_irq(irq, &mxs_timer_irq);
318}