blob: f94db327ac7c861586852df2a0c7668301cad4fe [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;
Laurent Pinchart740a9512014-01-27 22:04:17 +010042 unsigned int index;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000043
Laurent Pinchartc924d2d2014-01-27 22:04:17 +010044 void __iomem *base;
45
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000046 unsigned long flags;
47 unsigned long match_value;
48 unsigned long next_match_value;
49 unsigned long max_match_value;
50 unsigned long rate;
Paul Mundt7d0c3992012-05-25 13:36:43 +090051 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000052 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +000053 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000054 unsigned long total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +020055 bool cs_enabled;
Laurent Pinchart7269f932014-01-27 15:29:19 +010056};
57
Laurent Pinchart2653caf2014-01-27 22:04:17 +010058struct sh_cmt_device {
Laurent Pinchart7269f932014-01-27 15:29:19 +010059 struct platform_device *pdev;
60
Laurent Pinchart36f1ac92014-01-27 22:04:17 +010061 void __iomem *mapbase_ch;
Laurent Pinchart7269f932014-01-27 15:29:19 +010062 void __iomem *mapbase;
Laurent Pinchart7269f932014-01-27 15:29:19 +010063 struct clk *clk;
64
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +010065 struct sh_cmt_channel *channels;
66 unsigned int num_channels;
Laurent Pinchart7269f932014-01-27 15:29:19 +010067
68 unsigned long width; /* 16 or 32 bit version of hardware block */
69 unsigned long overflow_bit;
70 unsigned long clear_bits;
Magnus Damma6a912c2012-12-14 14:54:19 +090071
Magnus Dammcccd7042012-12-14 14:54:28 +090072 /* callbacks for CMSTR and CMCSR access */
73 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
74 void (*write_control)(void __iomem *base, unsigned long offs,
75 unsigned long value);
76
Magnus Damma6a912c2012-12-14 14:54:19 +090077 /* callbacks for CMCNT and CMCOR access */
78 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
79 void (*write_count)(void __iomem *base, unsigned long offs,
80 unsigned long value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000081};
82
Magnus Damm118aee42012-12-14 14:54:37 +090083/* Examples of supported CMT timer register layouts and I/O access widths:
84 *
85 * "16-bit counter and 16-bit control" as found on sh7263:
86 * CMSTR 0xfffec000 16-bit
87 * CMCSR 0xfffec002 16-bit
88 * CMCNT 0xfffec004 16-bit
89 * CMCOR 0xfffec006 16-bit
90 *
91 * "32-bit counter and 16-bit control" as found on sh7372, sh73a0, r8a7740:
92 * CMSTR 0xffca0000 16-bit
93 * CMCSR 0xffca0060 16-bit
94 * CMCNT 0xffca0064 32-bit
95 * CMCOR 0xffca0068 32-bit
Magnus Damm8874c5e2013-06-17 15:40:52 +090096 *
97 * "32-bit counter and 32-bit control" as found on r8a73a4 and r8a7790:
98 * CMSTR 0xffca0500 32-bit
99 * CMCSR 0xffca0510 32-bit
100 * CMCNT 0xffca0514 32-bit
101 * CMCOR 0xffca0518 32-bit
Magnus Damm118aee42012-12-14 14:54:37 +0900102 */
103
Magnus Damma6a912c2012-12-14 14:54:19 +0900104static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +0900105{
106 return ioread16(base + (offs << 1));
107}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000108
Magnus Damma6a912c2012-12-14 14:54:19 +0900109static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
110{
111 return ioread32(base + (offs << 2));
112}
113
114static void sh_cmt_write16(void __iomem *base, unsigned long offs,
115 unsigned long value)
Magnus Damm587acb32012-12-14 14:54:10 +0900116{
117 iowrite16(value, base + (offs << 1));
118}
119
Magnus Damma6a912c2012-12-14 14:54:19 +0900120static void sh_cmt_write32(void __iomem *base, unsigned long offs,
121 unsigned long value)
122{
123 iowrite32(value, base + (offs << 2));
124}
125
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000126#define CMCSR 0 /* channel register */
127#define CMCNT 1 /* channel register */
128#define CMCOR 2 /* channel register */
129
Laurent Pinchart7269f932014-01-27 15:29:19 +0100130static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900131{
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100132 return ch->cmt->read_control(ch->cmt->mapbase, 0);
Magnus Damm1b56b962012-12-14 14:54:00 +0900133}
134
Laurent Pinchart7269f932014-01-27 15:29:19 +0100135static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900136{
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100137 return ch->cmt->read_control(ch->base, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900138}
139
Laurent Pinchart7269f932014-01-27 15:29:19 +0100140static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900141{
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100142 return ch->cmt->read_count(ch->base, CMCNT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000143}
144
Laurent Pinchart7269f932014-01-27 15:29:19 +0100145static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900146 unsigned long value)
147{
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100148 ch->cmt->write_control(ch->cmt->mapbase, 0, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900149}
150
Laurent Pinchart7269f932014-01-27 15:29:19 +0100151static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900152 unsigned long value)
153{
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100154 ch->cmt->write_control(ch->base, CMCSR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900155}
156
Laurent Pinchart7269f932014-01-27 15:29:19 +0100157static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900158 unsigned long value)
159{
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100160 ch->cmt->write_count(ch->base, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900161}
162
Laurent Pinchart7269f932014-01-27 15:29:19 +0100163static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900164 unsigned long value)
165{
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100166 ch->cmt->write_count(ch->base, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900167}
168
Laurent Pinchart7269f932014-01-27 15:29:19 +0100169static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000170 int *has_wrapped)
171{
172 unsigned long v1, v2, v3;
Magnus Damm5b644c72009-04-28 08:17:54 +0000173 int o1, o2;
174
Laurent Pinchart7269f932014-01-27 15:29:19 +0100175 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000176
177 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
178 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000179 o2 = o1;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100180 v1 = sh_cmt_read_cmcnt(ch);
181 v2 = sh_cmt_read_cmcnt(ch);
182 v3 = sh_cmt_read_cmcnt(ch);
183 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000184 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
185 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000186
Magnus Damm5b644c72009-04-28 08:17:54 +0000187 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000188 return v2;
189}
190
Magnus Damm587acb32012-12-14 14:54:10 +0900191static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000192
Laurent Pinchart7269f932014-01-27 15:29:19 +0100193static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000194{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100195 struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000196 unsigned long flags, value;
197
198 /* start stop register shared by multiple timer channels */
Paul Mundt7d0c3992012-05-25 13:36:43 +0900199 raw_spin_lock_irqsave(&sh_cmt_lock, flags);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100200 value = sh_cmt_read_cmstr(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000201
202 if (start)
203 value |= 1 << cfg->timer_bit;
204 else
205 value &= ~(1 << cfg->timer_bit);
206
Laurent Pinchart7269f932014-01-27 15:29:19 +0100207 sh_cmt_write_cmstr(ch, value);
Paul Mundt7d0c3992012-05-25 13:36:43 +0900208 raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000209}
210
Laurent Pinchart7269f932014-01-27 15:29:19 +0100211static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000212{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000213 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000214
Laurent Pinchart7269f932014-01-27 15:29:19 +0100215 pm_runtime_get_sync(&ch->cmt->pdev->dev);
216 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200217
Paul Mundt9436b4a2011-05-31 15:26:42 +0900218 /* enable clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100219 ret = clk_enable(ch->cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000220 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100221 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
222 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000223 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000224 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000225
226 /* make sure channel is disabled */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100227 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000228
229 /* configure channel, periodic mode and maximum timeout */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100230 if (ch->cmt->width == 16) {
231 *rate = clk_get_rate(ch->cmt->clk) / 512;
232 sh_cmt_write_cmcsr(ch, 0x43);
Magnus Damm3014f472009-04-29 14:50:37 +0000233 } else {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100234 *rate = clk_get_rate(ch->cmt->clk) / 8;
235 sh_cmt_write_cmcsr(ch, 0x01a4);
Magnus Damm3014f472009-04-29 14:50:37 +0000236 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000237
Laurent Pinchart7269f932014-01-27 15:29:19 +0100238 sh_cmt_write_cmcor(ch, 0xffffffff);
239 sh_cmt_write_cmcnt(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000240
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000241 /*
242 * According to the sh73a0 user's manual, as CMCNT can be operated
243 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
244 * modifying CMCNT register; two RCLK cycles are necessary before
245 * this register is either read or any modification of the value
246 * it holds is reflected in the LSI's actual operation.
247 *
248 * While at it, we're supposed to clear out the CMCNT as of this
249 * moment, so make sure it's processed properly here. This will
250 * take RCLKx2 at maximum.
251 */
252 for (k = 0; k < 100; k++) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100253 if (!sh_cmt_read_cmcnt(ch))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000254 break;
255 udelay(1);
256 }
257
Laurent Pinchart7269f932014-01-27 15:29:19 +0100258 if (sh_cmt_read_cmcnt(ch)) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100259 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
260 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000261 ret = -ETIMEDOUT;
262 goto err1;
263 }
264
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000265 /* enable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100266 sh_cmt_start_stop_ch(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000267 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000268 err1:
269 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100270 clk_disable(ch->cmt->clk);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000271
272 err0:
273 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000274}
275
Laurent Pinchart7269f932014-01-27 15:29:19 +0100276static void sh_cmt_disable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000277{
278 /* disable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100279 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000280
Magnus Dammbe890a12009-06-17 05:04:04 +0000281 /* disable interrupts in CMT block */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100282 sh_cmt_write_cmcsr(ch, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000283
Paul Mundt9436b4a2011-05-31 15:26:42 +0900284 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100285 clk_disable(ch->cmt->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200286
Laurent Pinchart7269f932014-01-27 15:29:19 +0100287 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
288 pm_runtime_put(&ch->cmt->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000289}
290
291/* private flags */
292#define FLAG_CLOCKEVENT (1 << 0)
293#define FLAG_CLOCKSOURCE (1 << 1)
294#define FLAG_REPROGRAM (1 << 2)
295#define FLAG_SKIPEVENT (1 << 3)
296#define FLAG_IRQCONTEXT (1 << 4)
297
Laurent Pinchart7269f932014-01-27 15:29:19 +0100298static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000299 int absolute)
300{
301 unsigned long new_match;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100302 unsigned long value = ch->next_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000303 unsigned long delay = 0;
304 unsigned long now = 0;
305 int has_wrapped;
306
Laurent Pinchart7269f932014-01-27 15:29:19 +0100307 now = sh_cmt_get_counter(ch, &has_wrapped);
308 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000309
310 if (has_wrapped) {
311 /* we're competing with the interrupt handler.
312 * -> let the interrupt handler reprogram the timer.
313 * -> interrupt number two handles the event.
314 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100315 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000316 return;
317 }
318
319 if (absolute)
320 now = 0;
321
322 do {
323 /* reprogram the timer hardware,
324 * but don't save the new match value yet.
325 */
326 new_match = now + value + delay;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100327 if (new_match > ch->max_match_value)
328 new_match = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000329
Laurent Pinchart7269f932014-01-27 15:29:19 +0100330 sh_cmt_write_cmcor(ch, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000331
Laurent Pinchart7269f932014-01-27 15:29:19 +0100332 now = sh_cmt_get_counter(ch, &has_wrapped);
333 if (has_wrapped && (new_match > ch->match_value)) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000334 /* we are changing to a greater match value,
335 * so this wrap must be caused by the counter
336 * matching the old value.
337 * -> first interrupt reprograms the timer.
338 * -> interrupt number two handles the event.
339 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100340 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000341 break;
342 }
343
344 if (has_wrapped) {
345 /* we are changing to a smaller match value,
346 * so the wrap must be caused by the counter
347 * matching the new value.
348 * -> save programmed match value.
349 * -> let isr handle the event.
350 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100351 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000352 break;
353 }
354
355 /* be safe: verify hardware settings */
356 if (now < new_match) {
357 /* timer value is below match value, all good.
358 * this makes sure we won't miss any match events.
359 * -> save programmed match value.
360 * -> let isr handle the event.
361 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100362 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000363 break;
364 }
365
366 /* the counter has reached a value greater
367 * than our new match value. and since the
368 * has_wrapped flag isn't set we must have
369 * programmed a too close event.
370 * -> increase delay and retry.
371 */
372 if (delay)
373 delay <<= 1;
374 else
375 delay = 1;
376
377 if (!delay)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100378 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
379 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000380
381 } while (delay);
382}
383
Laurent Pinchart7269f932014-01-27 15:29:19 +0100384static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Takashi YOSHII65ada542010-12-17 07:25:09 +0000385{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100386 if (delta > ch->max_match_value)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100387 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
388 ch->index);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000389
Laurent Pinchart7269f932014-01-27 15:29:19 +0100390 ch->next_match_value = delta;
391 sh_cmt_clock_event_program_verify(ch, 0);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000392}
393
Laurent Pinchart7269f932014-01-27 15:29:19 +0100394static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000395{
396 unsigned long flags;
397
Laurent Pinchart7269f932014-01-27 15:29:19 +0100398 raw_spin_lock_irqsave(&ch->lock, flags);
399 __sh_cmt_set_next(ch, delta);
400 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000401}
402
403static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
404{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100405 struct sh_cmt_channel *ch = dev_id;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000406
407 /* clear flags */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100408 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) & ch->cmt->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000409
410 /* update clock source counter to begin with if enabled
411 * the wrap flag should be cleared by the timer specific
412 * isr before we end up here.
413 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100414 if (ch->flags & FLAG_CLOCKSOURCE)
415 ch->total_cycles += ch->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000416
Laurent Pinchart7269f932014-01-27 15:29:19 +0100417 if (!(ch->flags & FLAG_REPROGRAM))
418 ch->next_match_value = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000419
Laurent Pinchart7269f932014-01-27 15:29:19 +0100420 ch->flags |= FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000421
Laurent Pinchart7269f932014-01-27 15:29:19 +0100422 if (ch->flags & FLAG_CLOCKEVENT) {
423 if (!(ch->flags & FLAG_SKIPEVENT)) {
424 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
425 ch->next_match_value = ch->max_match_value;
426 ch->flags |= FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000427 }
428
Laurent Pinchart7269f932014-01-27 15:29:19 +0100429 ch->ced.event_handler(&ch->ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000430 }
431 }
432
Laurent Pinchart7269f932014-01-27 15:29:19 +0100433 ch->flags &= ~FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000434
Laurent Pinchart7269f932014-01-27 15:29:19 +0100435 if (ch->flags & FLAG_REPROGRAM) {
436 ch->flags &= ~FLAG_REPROGRAM;
437 sh_cmt_clock_event_program_verify(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000438
Laurent Pinchart7269f932014-01-27 15:29:19 +0100439 if (ch->flags & FLAG_CLOCKEVENT)
440 if ((ch->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
441 || (ch->match_value == ch->next_match_value))
442 ch->flags &= ~FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000443 }
444
Laurent Pinchart7269f932014-01-27 15:29:19 +0100445 ch->flags &= ~FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000446
447 return IRQ_HANDLED;
448}
449
Laurent Pinchart7269f932014-01-27 15:29:19 +0100450static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000451{
452 int ret = 0;
453 unsigned long flags;
454
Laurent Pinchart7269f932014-01-27 15:29:19 +0100455 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000456
Laurent Pinchart7269f932014-01-27 15:29:19 +0100457 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
458 ret = sh_cmt_enable(ch, &ch->rate);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000459
460 if (ret)
461 goto out;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100462 ch->flags |= flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000463
464 /* setup timeout if no clockevent */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100465 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
466 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000467 out:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100468 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000469
470 return ret;
471}
472
Laurent Pinchart7269f932014-01-27 15:29:19 +0100473static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000474{
475 unsigned long flags;
476 unsigned long f;
477
Laurent Pinchart7269f932014-01-27 15:29:19 +0100478 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000479
Laurent Pinchart7269f932014-01-27 15:29:19 +0100480 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
481 ch->flags &= ~flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000482
Laurent Pinchart7269f932014-01-27 15:29:19 +0100483 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
484 sh_cmt_disable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000485
486 /* adjust the timeout to maximum if only clocksource left */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100487 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
488 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000489
Laurent Pinchart7269f932014-01-27 15:29:19 +0100490 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000491}
492
Laurent Pinchart7269f932014-01-27 15:29:19 +0100493static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000494{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100495 return container_of(cs, struct sh_cmt_channel, cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000496}
497
498static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
499{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100500 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000501 unsigned long flags, raw;
502 unsigned long value;
503 int has_wrapped;
504
Laurent Pinchart7269f932014-01-27 15:29:19 +0100505 raw_spin_lock_irqsave(&ch->lock, flags);
506 value = ch->total_cycles;
507 raw = sh_cmt_get_counter(ch, &has_wrapped);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000508
509 if (unlikely(has_wrapped))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100510 raw += ch->match_value + 1;
511 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000512
513 return value + raw;
514}
515
516static int sh_cmt_clocksource_enable(struct clocksource *cs)
517{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900518 int ret;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100519 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000520
Laurent Pinchart7269f932014-01-27 15:29:19 +0100521 WARN_ON(ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200522
Laurent Pinchart7269f932014-01-27 15:29:19 +0100523 ch->total_cycles = 0;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000524
Laurent Pinchart7269f932014-01-27 15:29:19 +0100525 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200526 if (!ret) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100527 __clocksource_updatefreq_hz(cs, ch->rate);
528 ch->cs_enabled = true;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200529 }
Magnus Damm3593f5f2011-04-25 22:32:11 +0900530 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000531}
532
533static void sh_cmt_clocksource_disable(struct clocksource *cs)
534{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100535 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200536
Laurent Pinchart7269f932014-01-27 15:29:19 +0100537 WARN_ON(!ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200538
Laurent Pinchart7269f932014-01-27 15:29:19 +0100539 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
540 ch->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000541}
542
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200543static void sh_cmt_clocksource_suspend(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 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
548 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200549}
550
Magnus Dammc8162882010-02-02 14:41:40 -0800551static void sh_cmt_clocksource_resume(struct clocksource *cs)
552{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100553 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200554
Laurent Pinchart7269f932014-01-27 15:29:19 +0100555 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
556 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800557}
558
Laurent Pinchart7269f932014-01-27 15:29:19 +0100559static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100560 const char *name, unsigned long rating)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000561{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100562 struct clocksource *cs = &ch->cs;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000563
564 cs->name = name;
565 cs->rating = rating;
566 cs->read = sh_cmt_clocksource_read;
567 cs->enable = sh_cmt_clocksource_enable;
568 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200569 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800570 cs->resume = sh_cmt_clocksource_resume;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000571 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
572 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900573
Laurent Pinchart740a9512014-01-27 22:04:17 +0100574 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
575 ch->index);
Paul Mundtf4d7c352010-06-02 17:10:44 +0900576
Magnus Damm3593f5f2011-04-25 22:32:11 +0900577 /* Register with dummy 1 Hz value, gets updated in ->enable() */
578 clocksource_register_hz(cs, 1);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000579 return 0;
580}
581
Laurent Pinchart7269f932014-01-27 15:29:19 +0100582static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000583{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100584 return container_of(ced, struct sh_cmt_channel, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000585}
586
Laurent Pinchart7269f932014-01-27 15:29:19 +0100587static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000588{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100589 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000590
Laurent Pinchart7269f932014-01-27 15:29:19 +0100591 sh_cmt_start(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000592
593 /* TODO: calculate good shift from rate and counter bit width */
594
595 ced->shift = 32;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100596 ced->mult = div_sc(ch->rate, NSEC_PER_SEC, ced->shift);
597 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000598 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
599
600 if (periodic)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100601 sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000602 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100603 sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000604}
605
606static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
607 struct clock_event_device *ced)
608{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100609 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000610
611 /* deal with old setting first */
612 switch (ced->mode) {
613 case CLOCK_EVT_MODE_PERIODIC:
614 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100615 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000616 break;
617 default:
618 break;
619 }
620
621 switch (mode) {
622 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100623 dev_info(&ch->cmt->pdev->dev,
Laurent Pinchart740a9512014-01-27 22:04:17 +0100624 "ch%u: used for periodic clock events\n", ch->index);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100625 sh_cmt_clock_event_start(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000626 break;
627 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100628 dev_info(&ch->cmt->pdev->dev,
Laurent Pinchart740a9512014-01-27 22:04:17 +0100629 "ch%u: used for oneshot clock events\n", ch->index);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100630 sh_cmt_clock_event_start(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000631 break;
632 case CLOCK_EVT_MODE_SHUTDOWN:
633 case CLOCK_EVT_MODE_UNUSED:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100634 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000635 break;
636 default:
637 break;
638 }
639}
640
641static int sh_cmt_clock_event_next(unsigned long delta,
642 struct clock_event_device *ced)
643{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100644 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000645
646 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100647 if (likely(ch->flags & FLAG_IRQCONTEXT))
648 ch->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000649 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100650 sh_cmt_set_next(ch, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000651
652 return 0;
653}
654
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200655static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
656{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100657 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900658
Laurent Pinchart7269f932014-01-27 15:29:19 +0100659 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
660 clk_unprepare(ch->cmt->clk);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200661}
662
663static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
664{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100665 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900666
Laurent Pinchart7269f932014-01-27 15:29:19 +0100667 clk_prepare(ch->cmt->clk);
668 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200669}
670
Laurent Pinchart7269f932014-01-27 15:29:19 +0100671static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100672 const char *name, unsigned long rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000673{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100674 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000675
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000676 ced->name = name;
677 ced->features = CLOCK_EVT_FEAT_PERIODIC;
678 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
679 ced->rating = rating;
680 ced->cpumask = cpumask_of(0);
681 ced->set_next_event = sh_cmt_clock_event_next;
682 ced->set_mode = sh_cmt_clock_event_mode;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200683 ced->suspend = sh_cmt_clock_event_suspend;
684 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000685
Laurent Pinchart740a9512014-01-27 22:04:17 +0100686 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
687 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000688 clockevents_register_device(ced);
689}
690
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100691static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
Paul Mundtd1fcc0a2009-05-03 18:05:42 +0900692 unsigned long clockevent_rating,
693 unsigned long clocksource_rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000694{
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000695 if (clockevent_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100696 sh_cmt_register_clockevent(ch, name, clockevent_rating);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000697
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000698 if (clocksource_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100699 sh_cmt_register_clocksource(ch, name, clocksource_rating);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000700
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000701 return 0;
702}
703
Laurent Pinchart740a9512014-01-27 22:04:17 +0100704static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100705 struct sh_cmt_device *cmt)
706{
707 struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
708 int irq;
709 int ret;
710
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100711 ch->cmt = cmt;
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100712 ch->base = cmt->mapbase_ch;
Laurent Pinchart740a9512014-01-27 22:04:17 +0100713 ch->index = index;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100714
715 irq = platform_get_irq(cmt->pdev, 0);
716 if (irq < 0) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100717 dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n",
718 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100719 return irq;
720 }
721
722 if (cmt->width == (sizeof(ch->max_match_value) * 8))
723 ch->max_match_value = ~0;
724 else
725 ch->max_match_value = (1 << cmt->width) - 1;
726
727 ch->match_value = ch->max_match_value;
728 raw_spin_lock_init(&ch->lock);
729
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100730 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100731 cfg->clockevent_rating,
732 cfg->clocksource_rating);
733 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100734 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
735 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100736 return ret;
737 }
738 ch->cs_enabled = false;
739
740 ret = request_irq(irq, sh_cmt_interrupt,
741 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
742 dev_name(&cmt->pdev->dev), ch);
743 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100744 dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n",
745 ch->index, irq);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100746 return ret;
747 }
748
749 return 0;
750}
751
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100752static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000753{
Paul Mundt46a12f72009-05-03 17:57:17 +0900754 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900755 struct resource *res, *res2;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100756 int ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000757 ret = -ENXIO;
758
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100759 cmt->pdev = pdev;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000760
761 if (!cfg) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100762 dev_err(&cmt->pdev->dev, "missing platform data\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000763 goto err0;
764 }
765
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100766 res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000767 if (!res) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100768 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000769 goto err0;
770 }
771
Magnus Damm8874c5e2013-06-17 15:40:52 +0900772 /* optional resource for the shared timer start/stop register */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100773 res2 = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 1);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900774
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100775 /* map memory, let mapbase_ch point to our channel */
776 cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res));
777 if (cmt->mapbase_ch == NULL) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100778 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000779 goto err0;
780 }
781
Magnus Damm8874c5e2013-06-17 15:40:52 +0900782 /* map second resource for CMSTR */
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100783 cmt->mapbase = ioremap_nocache(res2 ? res2->start :
784 res->start - cfg->channel_offset,
785 res2 ? resource_size(res2) : 2);
786 if (cmt->mapbase == NULL) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100787 dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
Magnus Damm8874c5e2013-06-17 15:40:52 +0900788 goto err1;
789 }
790
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000791 /* get hold of clock */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100792 cmt->clk = clk_get(&cmt->pdev->dev, "cmt_fck");
793 if (IS_ERR(cmt->clk)) {
794 dev_err(&cmt->pdev->dev, "cannot get clock\n");
795 ret = PTR_ERR(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900796 goto err2;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000797 }
798
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100799 ret = clk_prepare(cmt->clk);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900800 if (ret < 0)
801 goto err3;
802
Magnus Damm8874c5e2013-06-17 15:40:52 +0900803 if (res2 && (resource_size(res2) == 4)) {
804 /* assume both CMSTR and CMCSR to be 32-bit */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100805 cmt->read_control = sh_cmt_read32;
806 cmt->write_control = sh_cmt_write32;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900807 } else {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100808 cmt->read_control = sh_cmt_read16;
809 cmt->write_control = sh_cmt_write16;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900810 }
Magnus Dammcccd7042012-12-14 14:54:28 +0900811
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000812 if (resource_size(res) == 6) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100813 cmt->width = 16;
814 cmt->read_count = sh_cmt_read16;
815 cmt->write_count = sh_cmt_write16;
816 cmt->overflow_bit = 0x80;
817 cmt->clear_bits = ~0x80;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000818 } else {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100819 cmt->width = 32;
820 cmt->read_count = sh_cmt_read32;
821 cmt->write_count = sh_cmt_write32;
822 cmt->overflow_bit = 0x8000;
823 cmt->clear_bits = ~0xc000;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000824 }
825
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100826 cmt->channels = kzalloc(sizeof(*cmt->channels), GFP_KERNEL);
827 if (cmt->channels == NULL) {
828 ret = -ENOMEM;
829 goto err4;
830 }
831
832 cmt->num_channels = 1;
833
834 ret = sh_cmt_setup_channel(&cmt->channels[0], cfg->timer_bit, cmt);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100835 if (ret < 0)
Laurent Pinchart57dee992013-12-14 15:07:32 +0900836 goto err4;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900837
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100838 platform_set_drvdata(pdev, cmt);
Magnus Dammadccc692012-12-14 14:53:51 +0900839
Paul Mundtda64c2a2010-02-25 16:37:46 +0900840 return 0;
Laurent Pinchart57dee992013-12-14 15:07:32 +0900841err4:
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100842 kfree(cmt->channels);
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100843 clk_unprepare(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900844err3:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100845 clk_put(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900846err2:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100847 iounmap(cmt->mapbase);
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100848err1:
849 iounmap(cmt->mapbase_ch);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900850err0:
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000851 return ret;
852}
853
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800854static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000855{
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100856 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200857 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000858 int ret;
859
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200860 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200861 pm_runtime_set_active(&pdev->dev);
862 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200863 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +0100864
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100865 if (cmt) {
Paul Mundt214a6072010-03-10 16:26:25 +0900866 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200867 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +0000868 }
869
Laurent Pinchartb262bc72014-01-27 22:04:17 +0100870 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100871 if (cmt == NULL) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000872 dev_err(&pdev->dev, "failed to allocate driver data\n");
873 return -ENOMEM;
874 }
875
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100876 ret = sh_cmt_setup(cmt, pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000877 if (ret) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100878 kfree(cmt);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200879 pm_runtime_idle(&pdev->dev);
880 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000881 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200882 if (is_early_platform_device(pdev))
883 return 0;
884
885 out:
886 if (cfg->clockevent_rating || cfg->clocksource_rating)
887 pm_runtime_irq_safe(&pdev->dev);
888 else
889 pm_runtime_idle(&pdev->dev);
890
891 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000892}
893
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800894static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000895{
896 return -EBUSY; /* cannot unregister clockevent and clocksource */
897}
898
899static struct platform_driver sh_cmt_device_driver = {
900 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800901 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000902 .driver = {
903 .name = "sh_cmt",
904 }
905};
906
907static int __init sh_cmt_init(void)
908{
909 return platform_driver_register(&sh_cmt_device_driver);
910}
911
912static void __exit sh_cmt_exit(void)
913{
914 platform_driver_unregister(&sh_cmt_device_driver);
915}
916
Magnus Damme475eed2009-04-15 10:50:04 +0000917early_platform_init("earlytimer", &sh_cmt_device_driver);
Simon Hormane903a032013-03-05 15:40:42 +0900918subsys_initcall(sh_cmt_init);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000919module_exit(sh_cmt_exit);
920
921MODULE_AUTHOR("Magnus Damm");
922MODULE_DESCRIPTION("SuperH CMT Timer Driver");
923MODULE_LICENSE("GPL v2");