blob: febd6bf7a37de1e7fd07ef9ce416592453af68ac [file] [log] [blame]
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001/*
2 * SuperH Timer Support - CMT
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000021#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/io.h>
26#include <linux/clk.h>
27#include <linux/irq.h>
28#include <linux/err.h>
Magnus Damm3f7e5e22011-07-13 07:59:48 +000029#include <linux/delay.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000030#include <linux/clocksource.h>
31#include <linux/clockchips.h>
Paul Mundt46a12f72009-05-03 17:57:17 +090032#include <linux/sh_timer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker7deeab52011-07-03 13:36:22 -040034#include <linux/module.h>
Rafael J. Wysocki615a4452012-03-13 22:40:06 +010035#include <linux/pm_domain.h>
Rafael J. Wysockibad81382012-08-06 01:48:57 +020036#include <linux/pm_runtime.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000037
Laurent Pinchart2653caf2014-01-27 22:04:17 +010038struct sh_cmt_device;
Laurent Pinchart7269f932014-01-27 15:29:19 +010039
40struct sh_cmt_channel {
Laurent Pinchart2653caf2014-01-27 22:04:17 +010041 struct sh_cmt_device *cmt;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000042
43 unsigned long flags;
44 unsigned long match_value;
45 unsigned long next_match_value;
46 unsigned long max_match_value;
47 unsigned long rate;
Paul Mundt7d0c3992012-05-25 13:36:43 +090048 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000049 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +000050 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000051 unsigned long total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +020052 bool cs_enabled;
Laurent Pinchart7269f932014-01-27 15:29:19 +010053};
54
Laurent Pinchart2653caf2014-01-27 22:04:17 +010055struct sh_cmt_device {
Laurent Pinchart7269f932014-01-27 15:29:19 +010056 struct platform_device *pdev;
57
58 void __iomem *mapbase;
59 void __iomem *mapbase_str;
60 struct clk *clk;
61
62 struct sh_cmt_channel channel;
63
64 unsigned long width; /* 16 or 32 bit version of hardware block */
65 unsigned long overflow_bit;
66 unsigned long clear_bits;
Magnus Damma6a912c2012-12-14 14:54:19 +090067
Magnus Dammcccd7042012-12-14 14:54:28 +090068 /* callbacks for CMSTR and CMCSR access */
69 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
70 void (*write_control)(void __iomem *base, unsigned long offs,
71 unsigned long value);
72
Magnus Damma6a912c2012-12-14 14:54:19 +090073 /* callbacks for CMCNT and CMCOR access */
74 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
75 void (*write_count)(void __iomem *base, unsigned long offs,
76 unsigned long value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000077};
78
Magnus Damm118aee42012-12-14 14:54:37 +090079/* Examples of supported CMT timer register layouts and I/O access widths:
80 *
81 * "16-bit counter and 16-bit control" as found on sh7263:
82 * CMSTR 0xfffec000 16-bit
83 * CMCSR 0xfffec002 16-bit
84 * CMCNT 0xfffec004 16-bit
85 * CMCOR 0xfffec006 16-bit
86 *
87 * "32-bit counter and 16-bit control" as found on sh7372, sh73a0, r8a7740:
88 * CMSTR 0xffca0000 16-bit
89 * CMCSR 0xffca0060 16-bit
90 * CMCNT 0xffca0064 32-bit
91 * CMCOR 0xffca0068 32-bit
Magnus Damm8874c5e2013-06-17 15:40:52 +090092 *
93 * "32-bit counter and 32-bit control" as found on r8a73a4 and r8a7790:
94 * CMSTR 0xffca0500 32-bit
95 * CMCSR 0xffca0510 32-bit
96 * CMCNT 0xffca0514 32-bit
97 * CMCOR 0xffca0518 32-bit
Magnus Damm118aee42012-12-14 14:54:37 +090098 */
99
Magnus Damma6a912c2012-12-14 14:54:19 +0900100static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +0900101{
102 return ioread16(base + (offs << 1));
103}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000104
Magnus Damma6a912c2012-12-14 14:54:19 +0900105static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
106{
107 return ioread32(base + (offs << 2));
108}
109
110static void sh_cmt_write16(void __iomem *base, unsigned long offs,
111 unsigned long value)
Magnus Damm587acb32012-12-14 14:54:10 +0900112{
113 iowrite16(value, base + (offs << 1));
114}
115
Magnus Damma6a912c2012-12-14 14:54:19 +0900116static void sh_cmt_write32(void __iomem *base, unsigned long offs,
117 unsigned long value)
118{
119 iowrite32(value, base + (offs << 2));
120}
121
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000122#define CMCSR 0 /* channel register */
123#define CMCNT 1 /* channel register */
124#define CMCOR 2 /* channel register */
125
Laurent Pinchart7269f932014-01-27 15:29:19 +0100126static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900127{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100128 return ch->cmt->read_control(ch->cmt->mapbase_str, 0);
Magnus Damm1b56b962012-12-14 14:54:00 +0900129}
130
Laurent Pinchart7269f932014-01-27 15:29:19 +0100131static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900132{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100133 return ch->cmt->read_control(ch->cmt->mapbase, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900134}
135
Laurent Pinchart7269f932014-01-27 15:29:19 +0100136static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900137{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100138 return ch->cmt->read_count(ch->cmt->mapbase, CMCNT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000139}
140
Laurent Pinchart7269f932014-01-27 15:29:19 +0100141static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900142 unsigned long value)
143{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100144 ch->cmt->write_control(ch->cmt->mapbase_str, 0, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900145}
146
Laurent Pinchart7269f932014-01-27 15:29:19 +0100147static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900148 unsigned long value)
149{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100150 ch->cmt->write_control(ch->cmt->mapbase, CMCSR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900151}
152
Laurent Pinchart7269f932014-01-27 15:29:19 +0100153static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900154 unsigned long value)
155{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100156 ch->cmt->write_count(ch->cmt->mapbase, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900157}
158
Laurent Pinchart7269f932014-01-27 15:29:19 +0100159static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900160 unsigned long value)
161{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100162 ch->cmt->write_count(ch->cmt->mapbase, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900163}
164
Laurent Pinchart7269f932014-01-27 15:29:19 +0100165static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000166 int *has_wrapped)
167{
168 unsigned long v1, v2, v3;
Magnus Damm5b644c72009-04-28 08:17:54 +0000169 int o1, o2;
170
Laurent Pinchart7269f932014-01-27 15:29:19 +0100171 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000172
173 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
174 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000175 o2 = o1;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100176 v1 = sh_cmt_read_cmcnt(ch);
177 v2 = sh_cmt_read_cmcnt(ch);
178 v3 = sh_cmt_read_cmcnt(ch);
179 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000180 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
181 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000182
Magnus Damm5b644c72009-04-28 08:17:54 +0000183 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000184 return v2;
185}
186
Magnus Damm587acb32012-12-14 14:54:10 +0900187static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000188
Laurent Pinchart7269f932014-01-27 15:29:19 +0100189static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000190{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100191 struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000192 unsigned long flags, value;
193
194 /* start stop register shared by multiple timer channels */
Paul Mundt7d0c3992012-05-25 13:36:43 +0900195 raw_spin_lock_irqsave(&sh_cmt_lock, flags);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100196 value = sh_cmt_read_cmstr(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000197
198 if (start)
199 value |= 1 << cfg->timer_bit;
200 else
201 value &= ~(1 << cfg->timer_bit);
202
Laurent Pinchart7269f932014-01-27 15:29:19 +0100203 sh_cmt_write_cmstr(ch, value);
Paul Mundt7d0c3992012-05-25 13:36:43 +0900204 raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000205}
206
Laurent Pinchart7269f932014-01-27 15:29:19 +0100207static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000208{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000209 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000210
Laurent Pinchart7269f932014-01-27 15:29:19 +0100211 pm_runtime_get_sync(&ch->cmt->pdev->dev);
212 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200213
Paul Mundt9436b4a2011-05-31 15:26:42 +0900214 /* enable clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100215 ret = clk_enable(ch->cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000216 if (ret) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100217 dev_err(&ch->cmt->pdev->dev, "cannot enable clock\n");
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000218 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000219 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000220
221 /* make sure channel is disabled */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100222 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000223
224 /* configure channel, periodic mode and maximum timeout */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100225 if (ch->cmt->width == 16) {
226 *rate = clk_get_rate(ch->cmt->clk) / 512;
227 sh_cmt_write_cmcsr(ch, 0x43);
Magnus Damm3014f472009-04-29 14:50:37 +0000228 } else {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100229 *rate = clk_get_rate(ch->cmt->clk) / 8;
230 sh_cmt_write_cmcsr(ch, 0x01a4);
Magnus Damm3014f472009-04-29 14:50:37 +0000231 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000232
Laurent Pinchart7269f932014-01-27 15:29:19 +0100233 sh_cmt_write_cmcor(ch, 0xffffffff);
234 sh_cmt_write_cmcnt(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000235
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000236 /*
237 * According to the sh73a0 user's manual, as CMCNT can be operated
238 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
239 * modifying CMCNT register; two RCLK cycles are necessary before
240 * this register is either read or any modification of the value
241 * it holds is reflected in the LSI's actual operation.
242 *
243 * While at it, we're supposed to clear out the CMCNT as of this
244 * moment, so make sure it's processed properly here. This will
245 * take RCLKx2 at maximum.
246 */
247 for (k = 0; k < 100; k++) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100248 if (!sh_cmt_read_cmcnt(ch))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000249 break;
250 udelay(1);
251 }
252
Laurent Pinchart7269f932014-01-27 15:29:19 +0100253 if (sh_cmt_read_cmcnt(ch)) {
254 dev_err(&ch->cmt->pdev->dev, "cannot clear CMCNT\n");
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000255 ret = -ETIMEDOUT;
256 goto err1;
257 }
258
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000259 /* enable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100260 sh_cmt_start_stop_ch(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000261 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000262 err1:
263 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100264 clk_disable(ch->cmt->clk);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000265
266 err0:
267 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000268}
269
Laurent Pinchart7269f932014-01-27 15:29:19 +0100270static void sh_cmt_disable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000271{
272 /* disable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100273 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000274
Magnus Dammbe890a12009-06-17 05:04:04 +0000275 /* disable interrupts in CMT block */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100276 sh_cmt_write_cmcsr(ch, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000277
Paul Mundt9436b4a2011-05-31 15:26:42 +0900278 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100279 clk_disable(ch->cmt->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200280
Laurent Pinchart7269f932014-01-27 15:29:19 +0100281 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
282 pm_runtime_put(&ch->cmt->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000283}
284
285/* private flags */
286#define FLAG_CLOCKEVENT (1 << 0)
287#define FLAG_CLOCKSOURCE (1 << 1)
288#define FLAG_REPROGRAM (1 << 2)
289#define FLAG_SKIPEVENT (1 << 3)
290#define FLAG_IRQCONTEXT (1 << 4)
291
Laurent Pinchart7269f932014-01-27 15:29:19 +0100292static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000293 int absolute)
294{
295 unsigned long new_match;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100296 unsigned long value = ch->next_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000297 unsigned long delay = 0;
298 unsigned long now = 0;
299 int has_wrapped;
300
Laurent Pinchart7269f932014-01-27 15:29:19 +0100301 now = sh_cmt_get_counter(ch, &has_wrapped);
302 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000303
304 if (has_wrapped) {
305 /* we're competing with the interrupt handler.
306 * -> let the interrupt handler reprogram the timer.
307 * -> interrupt number two handles the event.
308 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100309 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000310 return;
311 }
312
313 if (absolute)
314 now = 0;
315
316 do {
317 /* reprogram the timer hardware,
318 * but don't save the new match value yet.
319 */
320 new_match = now + value + delay;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100321 if (new_match > ch->max_match_value)
322 new_match = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000323
Laurent Pinchart7269f932014-01-27 15:29:19 +0100324 sh_cmt_write_cmcor(ch, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000325
Laurent Pinchart7269f932014-01-27 15:29:19 +0100326 now = sh_cmt_get_counter(ch, &has_wrapped);
327 if (has_wrapped && (new_match > ch->match_value)) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000328 /* we are changing to a greater match value,
329 * so this wrap must be caused by the counter
330 * matching the old value.
331 * -> first interrupt reprograms the timer.
332 * -> interrupt number two handles the event.
333 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100334 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000335 break;
336 }
337
338 if (has_wrapped) {
339 /* we are changing to a smaller match value,
340 * so the wrap must be caused by the counter
341 * matching the new value.
342 * -> save programmed match value.
343 * -> let isr handle the event.
344 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100345 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000346 break;
347 }
348
349 /* be safe: verify hardware settings */
350 if (now < new_match) {
351 /* timer value is below match value, all good.
352 * this makes sure we won't miss any match events.
353 * -> save programmed match value.
354 * -> let isr handle the event.
355 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100356 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000357 break;
358 }
359
360 /* the counter has reached a value greater
361 * than our new match value. and since the
362 * has_wrapped flag isn't set we must have
363 * programmed a too close event.
364 * -> increase delay and retry.
365 */
366 if (delay)
367 delay <<= 1;
368 else
369 delay = 1;
370
371 if (!delay)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100372 dev_warn(&ch->cmt->pdev->dev, "too long delay\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000373
374 } while (delay);
375}
376
Laurent Pinchart7269f932014-01-27 15:29:19 +0100377static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Takashi YOSHII65ada542010-12-17 07:25:09 +0000378{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100379 if (delta > ch->max_match_value)
380 dev_warn(&ch->cmt->pdev->dev, "delta out of range\n");
Takashi YOSHII65ada542010-12-17 07:25:09 +0000381
Laurent Pinchart7269f932014-01-27 15:29:19 +0100382 ch->next_match_value = delta;
383 sh_cmt_clock_event_program_verify(ch, 0);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000384}
385
Laurent Pinchart7269f932014-01-27 15:29:19 +0100386static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000387{
388 unsigned long flags;
389
Laurent Pinchart7269f932014-01-27 15:29:19 +0100390 raw_spin_lock_irqsave(&ch->lock, flags);
391 __sh_cmt_set_next(ch, delta);
392 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000393}
394
395static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
396{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100397 struct sh_cmt_channel *ch = dev_id;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000398
399 /* clear flags */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100400 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) & ch->cmt->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000401
402 /* update clock source counter to begin with if enabled
403 * the wrap flag should be cleared by the timer specific
404 * isr before we end up here.
405 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100406 if (ch->flags & FLAG_CLOCKSOURCE)
407 ch->total_cycles += ch->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000408
Laurent Pinchart7269f932014-01-27 15:29:19 +0100409 if (!(ch->flags & FLAG_REPROGRAM))
410 ch->next_match_value = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000411
Laurent Pinchart7269f932014-01-27 15:29:19 +0100412 ch->flags |= FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000413
Laurent Pinchart7269f932014-01-27 15:29:19 +0100414 if (ch->flags & FLAG_CLOCKEVENT) {
415 if (!(ch->flags & FLAG_SKIPEVENT)) {
416 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
417 ch->next_match_value = ch->max_match_value;
418 ch->flags |= FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000419 }
420
Laurent Pinchart7269f932014-01-27 15:29:19 +0100421 ch->ced.event_handler(&ch->ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000422 }
423 }
424
Laurent Pinchart7269f932014-01-27 15:29:19 +0100425 ch->flags &= ~FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000426
Laurent Pinchart7269f932014-01-27 15:29:19 +0100427 if (ch->flags & FLAG_REPROGRAM) {
428 ch->flags &= ~FLAG_REPROGRAM;
429 sh_cmt_clock_event_program_verify(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000430
Laurent Pinchart7269f932014-01-27 15:29:19 +0100431 if (ch->flags & FLAG_CLOCKEVENT)
432 if ((ch->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
433 || (ch->match_value == ch->next_match_value))
434 ch->flags &= ~FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000435 }
436
Laurent Pinchart7269f932014-01-27 15:29:19 +0100437 ch->flags &= ~FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000438
439 return IRQ_HANDLED;
440}
441
Laurent Pinchart7269f932014-01-27 15:29:19 +0100442static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000443{
444 int ret = 0;
445 unsigned long flags;
446
Laurent Pinchart7269f932014-01-27 15:29:19 +0100447 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000448
Laurent Pinchart7269f932014-01-27 15:29:19 +0100449 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
450 ret = sh_cmt_enable(ch, &ch->rate);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000451
452 if (ret)
453 goto out;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100454 ch->flags |= flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000455
456 /* setup timeout if no clockevent */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100457 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
458 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000459 out:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100460 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000461
462 return ret;
463}
464
Laurent Pinchart7269f932014-01-27 15:29:19 +0100465static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000466{
467 unsigned long flags;
468 unsigned long f;
469
Laurent Pinchart7269f932014-01-27 15:29:19 +0100470 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000471
Laurent Pinchart7269f932014-01-27 15:29:19 +0100472 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
473 ch->flags &= ~flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000474
Laurent Pinchart7269f932014-01-27 15:29:19 +0100475 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
476 sh_cmt_disable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000477
478 /* adjust the timeout to maximum if only clocksource left */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100479 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
480 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000481
Laurent Pinchart7269f932014-01-27 15:29:19 +0100482 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000483}
484
Laurent Pinchart7269f932014-01-27 15:29:19 +0100485static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000486{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100487 return container_of(cs, struct sh_cmt_channel, cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000488}
489
490static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
491{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100492 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000493 unsigned long flags, raw;
494 unsigned long value;
495 int has_wrapped;
496
Laurent Pinchart7269f932014-01-27 15:29:19 +0100497 raw_spin_lock_irqsave(&ch->lock, flags);
498 value = ch->total_cycles;
499 raw = sh_cmt_get_counter(ch, &has_wrapped);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000500
501 if (unlikely(has_wrapped))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100502 raw += ch->match_value + 1;
503 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000504
505 return value + raw;
506}
507
508static int sh_cmt_clocksource_enable(struct clocksource *cs)
509{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900510 int ret;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100511 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000512
Laurent Pinchart7269f932014-01-27 15:29:19 +0100513 WARN_ON(ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200514
Laurent Pinchart7269f932014-01-27 15:29:19 +0100515 ch->total_cycles = 0;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000516
Laurent Pinchart7269f932014-01-27 15:29:19 +0100517 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200518 if (!ret) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100519 __clocksource_updatefreq_hz(cs, ch->rate);
520 ch->cs_enabled = true;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200521 }
Magnus Damm3593f5f2011-04-25 22:32:11 +0900522 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000523}
524
525static void sh_cmt_clocksource_disable(struct clocksource *cs)
526{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100527 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200528
Laurent Pinchart7269f932014-01-27 15:29:19 +0100529 WARN_ON(!ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200530
Laurent Pinchart7269f932014-01-27 15:29:19 +0100531 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
532 ch->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000533}
534
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200535static void sh_cmt_clocksource_suspend(struct clocksource *cs)
536{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100537 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200538
Laurent Pinchart7269f932014-01-27 15:29:19 +0100539 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
540 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200541}
542
Magnus Dammc8162882010-02-02 14:41:40 -0800543static void sh_cmt_clocksource_resume(struct clocksource *cs)
544{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100545 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200546
Laurent Pinchart7269f932014-01-27 15:29:19 +0100547 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
548 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800549}
550
Laurent Pinchart7269f932014-01-27 15:29:19 +0100551static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100552 const char *name, unsigned long rating)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000553{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100554 struct clocksource *cs = &ch->cs;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000555
556 cs->name = name;
557 cs->rating = rating;
558 cs->read = sh_cmt_clocksource_read;
559 cs->enable = sh_cmt_clocksource_enable;
560 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200561 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800562 cs->resume = sh_cmt_clocksource_resume;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000563 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
564 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900565
Laurent Pinchart7269f932014-01-27 15:29:19 +0100566 dev_info(&ch->cmt->pdev->dev, "used as clock source\n");
Paul Mundtf4d7c352010-06-02 17:10:44 +0900567
Magnus Damm3593f5f2011-04-25 22:32:11 +0900568 /* Register with dummy 1 Hz value, gets updated in ->enable() */
569 clocksource_register_hz(cs, 1);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000570 return 0;
571}
572
Laurent Pinchart7269f932014-01-27 15:29:19 +0100573static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000574{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100575 return container_of(ced, struct sh_cmt_channel, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000576}
577
Laurent Pinchart7269f932014-01-27 15:29:19 +0100578static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000579{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100580 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000581
Laurent Pinchart7269f932014-01-27 15:29:19 +0100582 sh_cmt_start(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000583
584 /* TODO: calculate good shift from rate and counter bit width */
585
586 ced->shift = 32;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100587 ced->mult = div_sc(ch->rate, NSEC_PER_SEC, ced->shift);
588 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000589 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
590
591 if (periodic)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100592 sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000593 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100594 sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000595}
596
597static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
598 struct clock_event_device *ced)
599{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100600 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000601
602 /* deal with old setting first */
603 switch (ced->mode) {
604 case CLOCK_EVT_MODE_PERIODIC:
605 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100606 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000607 break;
608 default:
609 break;
610 }
611
612 switch (mode) {
613 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100614 dev_info(&ch->cmt->pdev->dev,
615 "used for periodic clock events\n");
616 sh_cmt_clock_event_start(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000617 break;
618 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100619 dev_info(&ch->cmt->pdev->dev,
620 "used for oneshot clock events\n");
621 sh_cmt_clock_event_start(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000622 break;
623 case CLOCK_EVT_MODE_SHUTDOWN:
624 case CLOCK_EVT_MODE_UNUSED:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100625 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000626 break;
627 default:
628 break;
629 }
630}
631
632static int sh_cmt_clock_event_next(unsigned long delta,
633 struct clock_event_device *ced)
634{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100635 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000636
637 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100638 if (likely(ch->flags & FLAG_IRQCONTEXT))
639 ch->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000640 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100641 sh_cmt_set_next(ch, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000642
643 return 0;
644}
645
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200646static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
647{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100648 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900649
Laurent Pinchart7269f932014-01-27 15:29:19 +0100650 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
651 clk_unprepare(ch->cmt->clk);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200652}
653
654static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
655{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100656 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900657
Laurent Pinchart7269f932014-01-27 15:29:19 +0100658 clk_prepare(ch->cmt->clk);
659 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200660}
661
Laurent Pinchart7269f932014-01-27 15:29:19 +0100662static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100663 const char *name, unsigned long rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000664{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100665 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000666
667 memset(ced, 0, sizeof(*ced));
668
669 ced->name = name;
670 ced->features = CLOCK_EVT_FEAT_PERIODIC;
671 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
672 ced->rating = rating;
673 ced->cpumask = cpumask_of(0);
674 ced->set_next_event = sh_cmt_clock_event_next;
675 ced->set_mode = sh_cmt_clock_event_mode;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200676 ced->suspend = sh_cmt_clock_event_suspend;
677 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000678
Laurent Pinchart7269f932014-01-27 15:29:19 +0100679 dev_info(&ch->cmt->pdev->dev, "used for clock events\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000680 clockevents_register_device(ced);
681}
682
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100683static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
Paul Mundtd1fcc0a2009-05-03 18:05:42 +0900684 unsigned long clockevent_rating,
685 unsigned long clocksource_rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000686{
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000687 if (clockevent_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100688 sh_cmt_register_clockevent(ch, name, clockevent_rating);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000689
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000690 if (clocksource_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100691 sh_cmt_register_clocksource(ch, name, clocksource_rating);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000692
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000693 return 0;
694}
695
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100696static int sh_cmt_setup_channel(struct sh_cmt_channel *ch,
697 struct sh_cmt_device *cmt)
698{
699 struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
700 int irq;
701 int ret;
702
703 memset(ch, 0, sizeof(*ch));
704 ch->cmt = cmt;
705
706 irq = platform_get_irq(cmt->pdev, 0);
707 if (irq < 0) {
708 dev_err(&cmt->pdev->dev, "failed to get irq\n");
709 return irq;
710 }
711
712 if (cmt->width == (sizeof(ch->max_match_value) * 8))
713 ch->max_match_value = ~0;
714 else
715 ch->max_match_value = (1 << cmt->width) - 1;
716
717 ch->match_value = ch->max_match_value;
718 raw_spin_lock_init(&ch->lock);
719
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100720 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100721 cfg->clockevent_rating,
722 cfg->clocksource_rating);
723 if (ret) {
724 dev_err(&cmt->pdev->dev, "registration failed\n");
725 return ret;
726 }
727 ch->cs_enabled = false;
728
729 ret = request_irq(irq, sh_cmt_interrupt,
730 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
731 dev_name(&cmt->pdev->dev), ch);
732 if (ret) {
733 dev_err(&cmt->pdev->dev, "failed to request irq %d\n", irq);
734 return ret;
735 }
736
737 return 0;
738}
739
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100740static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000741{
Paul Mundt46a12f72009-05-03 17:57:17 +0900742 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900743 struct resource *res, *res2;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100744 int ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000745 ret = -ENXIO;
746
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100747 memset(cmt, 0, sizeof(*cmt));
748 cmt->pdev = pdev;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000749
750 if (!cfg) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100751 dev_err(&cmt->pdev->dev, "missing platform data\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000752 goto err0;
753 }
754
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100755 res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000756 if (!res) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100757 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000758 goto err0;
759 }
760
Magnus Damm8874c5e2013-06-17 15:40:52 +0900761 /* optional resource for the shared timer start/stop register */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100762 res2 = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 1);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900763
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000764 /* map memory, let mapbase point to our channel */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100765 cmt->mapbase = ioremap_nocache(res->start, resource_size(res));
766 if (cmt->mapbase == NULL) {
767 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000768 goto err0;
769 }
770
Magnus Damm8874c5e2013-06-17 15:40:52 +0900771 /* map second resource for CMSTR */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100772 cmt->mapbase_str = ioremap_nocache(res2 ? res2->start :
773 res->start - cfg->channel_offset,
774 res2 ? resource_size(res2) : 2);
775 if (cmt->mapbase_str == NULL) {
776 dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
Magnus Damm8874c5e2013-06-17 15:40:52 +0900777 goto err1;
778 }
779
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000780 /* get hold of clock */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100781 cmt->clk = clk_get(&cmt->pdev->dev, "cmt_fck");
782 if (IS_ERR(cmt->clk)) {
783 dev_err(&cmt->pdev->dev, "cannot get clock\n");
784 ret = PTR_ERR(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900785 goto err2;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000786 }
787
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100788 ret = clk_prepare(cmt->clk);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900789 if (ret < 0)
790 goto err3;
791
Magnus Damm8874c5e2013-06-17 15:40:52 +0900792 if (res2 && (resource_size(res2) == 4)) {
793 /* assume both CMSTR and CMCSR to be 32-bit */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100794 cmt->read_control = sh_cmt_read32;
795 cmt->write_control = sh_cmt_write32;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900796 } else {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100797 cmt->read_control = sh_cmt_read16;
798 cmt->write_control = sh_cmt_write16;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900799 }
Magnus Dammcccd7042012-12-14 14:54:28 +0900800
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000801 if (resource_size(res) == 6) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100802 cmt->width = 16;
803 cmt->read_count = sh_cmt_read16;
804 cmt->write_count = sh_cmt_write16;
805 cmt->overflow_bit = 0x80;
806 cmt->clear_bits = ~0x80;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000807 } else {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100808 cmt->width = 32;
809 cmt->read_count = sh_cmt_read32;
810 cmt->write_count = sh_cmt_write32;
811 cmt->overflow_bit = 0x8000;
812 cmt->clear_bits = ~0xc000;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000813 }
814
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100815 ret = sh_cmt_setup_channel(&cmt->channel, cmt);
816 if (ret < 0)
Laurent Pinchart57dee992013-12-14 15:07:32 +0900817 goto err4;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900818
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100819 platform_set_drvdata(pdev, cmt);
Magnus Dammadccc692012-12-14 14:53:51 +0900820
Paul Mundtda64c2a2010-02-25 16:37:46 +0900821 return 0;
Laurent Pinchart57dee992013-12-14 15:07:32 +0900822err4:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100823 clk_unprepare(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900824err3:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100825 clk_put(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900826err2:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100827 iounmap(cmt->mapbase_str);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900828err1:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100829 iounmap(cmt->mapbase);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900830err0:
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000831 return ret;
832}
833
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800834static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000835{
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100836 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200837 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000838 int ret;
839
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200840 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200841 pm_runtime_set_active(&pdev->dev);
842 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200843 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +0100844
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100845 if (cmt) {
Paul Mundt214a6072010-03-10 16:26:25 +0900846 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200847 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +0000848 }
849
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100850 cmt = kmalloc(sizeof(*cmt), GFP_KERNEL);
851 if (cmt == NULL) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000852 dev_err(&pdev->dev, "failed to allocate driver data\n");
853 return -ENOMEM;
854 }
855
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100856 ret = sh_cmt_setup(cmt, pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000857 if (ret) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100858 kfree(cmt);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200859 pm_runtime_idle(&pdev->dev);
860 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000861 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200862 if (is_early_platform_device(pdev))
863 return 0;
864
865 out:
866 if (cfg->clockevent_rating || cfg->clocksource_rating)
867 pm_runtime_irq_safe(&pdev->dev);
868 else
869 pm_runtime_idle(&pdev->dev);
870
871 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000872}
873
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800874static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000875{
876 return -EBUSY; /* cannot unregister clockevent and clocksource */
877}
878
879static struct platform_driver sh_cmt_device_driver = {
880 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800881 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000882 .driver = {
883 .name = "sh_cmt",
884 }
885};
886
887static int __init sh_cmt_init(void)
888{
889 return platform_driver_register(&sh_cmt_device_driver);
890}
891
892static void __exit sh_cmt_exit(void)
893{
894 platform_driver_unregister(&sh_cmt_device_driver);
895}
896
Magnus Damme475eed2009-04-15 10:50:04 +0000897early_platform_init("earlytimer", &sh_cmt_device_driver);
Simon Hormane903a032013-03-05 15:40:42 +0900898subsys_initcall(sh_cmt_init);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000899module_exit(sh_cmt_exit);
900
901MODULE_AUTHOR("Magnus Damm");
902MODULE_DESCRIPTION("SuperH CMT Timer Driver");
903MODULE_LICENSE("GPL v2");