blob: 879b8c2ae5564ff0405b432d5633dce4860e664f [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
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010040/*
41 * The CMT comes in 5 different identified flavours, depending not only on the
42 * SoC but also on the particular instance. The following table lists the main
43 * characteristics of those flavours.
44 *
45 * 16B 32B 32B-F 48B 48B-2
46 * -----------------------------------------------------------------------------
47 * Channels 2 1/4 1 6 2/8
48 * Control Width 16 16 16 16 32
49 * Counter Width 16 32 32 32/48 32/48
50 * Shared Start/Stop Y Y Y Y N
51 *
52 * The 48-bit gen2 version has a per-channel start/stop register located in the
53 * channel registers block. All other versions have a shared start/stop register
54 * located in the global space.
55 *
56 * Note that CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
57 * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
58 */
59
60enum sh_cmt_model {
61 SH_CMT_16BIT,
62 SH_CMT_32BIT,
63 SH_CMT_32BIT_FAST,
64 SH_CMT_48BIT,
65 SH_CMT_48BIT_GEN2,
66};
67
68struct sh_cmt_info {
69 enum sh_cmt_model model;
70
71 unsigned long width; /* 16 or 32 bit version of hardware block */
72 unsigned long overflow_bit;
73 unsigned long clear_bits;
74
75 /* callbacks for CMSTR and CMCSR access */
76 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
77 void (*write_control)(void __iomem *base, unsigned long offs,
78 unsigned long value);
79
80 /* callbacks for CMCNT and CMCOR access */
81 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
82 void (*write_count)(void __iomem *base, unsigned long offs,
83 unsigned long value);
84};
85
Laurent Pinchart7269f932014-01-27 15:29:19 +010086struct sh_cmt_channel {
Laurent Pinchart2653caf2014-01-27 22:04:17 +010087 struct sh_cmt_device *cmt;
Laurent Pinchart740a9512014-01-27 22:04:17 +010088 unsigned int index;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000089
Laurent Pinchartc924d2d2014-01-27 22:04:17 +010090 void __iomem *base;
91
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000092 unsigned long flags;
93 unsigned long match_value;
94 unsigned long next_match_value;
95 unsigned long max_match_value;
96 unsigned long rate;
Paul Mundt7d0c3992012-05-25 13:36:43 +090097 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000098 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +000099 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000100 unsigned long total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200101 bool cs_enabled;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100102};
103
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100104struct sh_cmt_device {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100105 struct platform_device *pdev;
106
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100107 const struct sh_cmt_info *info;
108
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100109 void __iomem *mapbase_ch;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100110 void __iomem *mapbase;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100111 struct clk *clk;
112
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100113 struct sh_cmt_channel *channels;
114 unsigned int num_channels;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000115};
116
Magnus Damma6a912c2012-12-14 14:54:19 +0900117static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +0900118{
119 return ioread16(base + (offs << 1));
120}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000121
Magnus Damma6a912c2012-12-14 14:54:19 +0900122static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
123{
124 return ioread32(base + (offs << 2));
125}
126
127static void sh_cmt_write16(void __iomem *base, unsigned long offs,
128 unsigned long value)
Magnus Damm587acb32012-12-14 14:54:10 +0900129{
130 iowrite16(value, base + (offs << 1));
131}
132
Magnus Damma6a912c2012-12-14 14:54:19 +0900133static void sh_cmt_write32(void __iomem *base, unsigned long offs,
134 unsigned long value)
135{
136 iowrite32(value, base + (offs << 2));
137}
138
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100139static const struct sh_cmt_info sh_cmt_info[] = {
140 [SH_CMT_16BIT] = {
141 .model = SH_CMT_16BIT,
142 .width = 16,
143 .overflow_bit = 0x80,
144 .clear_bits = ~0x80,
145 .read_control = sh_cmt_read16,
146 .write_control = sh_cmt_write16,
147 .read_count = sh_cmt_read16,
148 .write_count = sh_cmt_write16,
149 },
150 [SH_CMT_32BIT] = {
151 .model = SH_CMT_32BIT,
152 .width = 32,
153 .overflow_bit = 0x8000,
154 .clear_bits = ~0xc000,
155 .read_control = sh_cmt_read16,
156 .write_control = sh_cmt_write16,
157 .read_count = sh_cmt_read32,
158 .write_count = sh_cmt_write32,
159 },
160 [SH_CMT_32BIT_FAST] = {
161 .model = SH_CMT_32BIT_FAST,
162 .width = 32,
163 .overflow_bit = 0x8000,
164 .clear_bits = ~0xc000,
165 .read_control = sh_cmt_read16,
166 .write_control = sh_cmt_write16,
167 .read_count = sh_cmt_read32,
168 .write_count = sh_cmt_write32,
169 },
170 [SH_CMT_48BIT] = {
171 .model = SH_CMT_48BIT,
172 .width = 32,
173 .overflow_bit = 0x8000,
174 .clear_bits = ~0xc000,
175 .read_control = sh_cmt_read32,
176 .write_control = sh_cmt_write32,
177 .read_count = sh_cmt_read32,
178 .write_count = sh_cmt_write32,
179 },
180 [SH_CMT_48BIT_GEN2] = {
181 .model = SH_CMT_48BIT_GEN2,
182 .width = 32,
183 .overflow_bit = 0x8000,
184 .clear_bits = ~0xc000,
185 .read_control = sh_cmt_read32,
186 .write_control = sh_cmt_write32,
187 .read_count = sh_cmt_read32,
188 .write_count = sh_cmt_write32,
189 },
190};
191
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000192#define CMCSR 0 /* channel register */
193#define CMCNT 1 /* channel register */
194#define CMCOR 2 /* channel register */
195
Laurent Pinchart7269f932014-01-27 15:29:19 +0100196static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900197{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100198 return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
Magnus Damm1b56b962012-12-14 14:54:00 +0900199}
200
Laurent Pinchart7269f932014-01-27 15:29:19 +0100201static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900202{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100203 return ch->cmt->info->read_control(ch->base, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900204}
205
Laurent Pinchart7269f932014-01-27 15:29:19 +0100206static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900207{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100208 return ch->cmt->info->read_count(ch->base, CMCNT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000209}
210
Laurent Pinchart7269f932014-01-27 15:29:19 +0100211static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900212 unsigned long value)
213{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100214 ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900215}
216
Laurent Pinchart7269f932014-01-27 15:29:19 +0100217static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900218 unsigned long value)
219{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100220 ch->cmt->info->write_control(ch->base, CMCSR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900221}
222
Laurent Pinchart7269f932014-01-27 15:29:19 +0100223static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900224 unsigned long value)
225{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100226 ch->cmt->info->write_count(ch->base, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900227}
228
Laurent Pinchart7269f932014-01-27 15:29:19 +0100229static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900230 unsigned long value)
231{
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100232 ch->cmt->info->write_count(ch->base, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900233}
234
Laurent Pinchart7269f932014-01-27 15:29:19 +0100235static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000236 int *has_wrapped)
237{
238 unsigned long v1, v2, v3;
Magnus Damm5b644c72009-04-28 08:17:54 +0000239 int o1, o2;
240
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100241 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000242
243 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
244 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000245 o2 = o1;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100246 v1 = sh_cmt_read_cmcnt(ch);
247 v2 = sh_cmt_read_cmcnt(ch);
248 v3 = sh_cmt_read_cmcnt(ch);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100249 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000250 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
251 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000252
Magnus Damm5b644c72009-04-28 08:17:54 +0000253 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000254 return v2;
255}
256
Magnus Damm587acb32012-12-14 14:54:10 +0900257static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000258
Laurent Pinchart7269f932014-01-27 15:29:19 +0100259static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000260{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100261 struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000262 unsigned long flags, value;
263
264 /* start stop register shared by multiple timer channels */
Paul Mundt7d0c3992012-05-25 13:36:43 +0900265 raw_spin_lock_irqsave(&sh_cmt_lock, flags);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100266 value = sh_cmt_read_cmstr(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000267
268 if (start)
269 value |= 1 << cfg->timer_bit;
270 else
271 value &= ~(1 << cfg->timer_bit);
272
Laurent Pinchart7269f932014-01-27 15:29:19 +0100273 sh_cmt_write_cmstr(ch, value);
Paul Mundt7d0c3992012-05-25 13:36:43 +0900274 raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000275}
276
Laurent Pinchart7269f932014-01-27 15:29:19 +0100277static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000278{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000279 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000280
Laurent Pinchart7269f932014-01-27 15:29:19 +0100281 pm_runtime_get_sync(&ch->cmt->pdev->dev);
282 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200283
Paul Mundt9436b4a2011-05-31 15:26:42 +0900284 /* enable clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100285 ret = clk_enable(ch->cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000286 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100287 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
288 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000289 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000290 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000291
292 /* make sure channel is disabled */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100293 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000294
295 /* configure channel, periodic mode and maximum timeout */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100296 if (ch->cmt->info->width == 16) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100297 *rate = clk_get_rate(ch->cmt->clk) / 512;
298 sh_cmt_write_cmcsr(ch, 0x43);
Magnus Damm3014f472009-04-29 14:50:37 +0000299 } else {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100300 *rate = clk_get_rate(ch->cmt->clk) / 8;
301 sh_cmt_write_cmcsr(ch, 0x01a4);
Magnus Damm3014f472009-04-29 14:50:37 +0000302 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000303
Laurent Pinchart7269f932014-01-27 15:29:19 +0100304 sh_cmt_write_cmcor(ch, 0xffffffff);
305 sh_cmt_write_cmcnt(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000306
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000307 /*
308 * According to the sh73a0 user's manual, as CMCNT can be operated
309 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
310 * modifying CMCNT register; two RCLK cycles are necessary before
311 * this register is either read or any modification of the value
312 * it holds is reflected in the LSI's actual operation.
313 *
314 * While at it, we're supposed to clear out the CMCNT as of this
315 * moment, so make sure it's processed properly here. This will
316 * take RCLKx2 at maximum.
317 */
318 for (k = 0; k < 100; k++) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100319 if (!sh_cmt_read_cmcnt(ch))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000320 break;
321 udelay(1);
322 }
323
Laurent Pinchart7269f932014-01-27 15:29:19 +0100324 if (sh_cmt_read_cmcnt(ch)) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100325 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
326 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000327 ret = -ETIMEDOUT;
328 goto err1;
329 }
330
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000331 /* enable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100332 sh_cmt_start_stop_ch(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000333 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000334 err1:
335 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100336 clk_disable(ch->cmt->clk);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000337
338 err0:
339 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000340}
341
Laurent Pinchart7269f932014-01-27 15:29:19 +0100342static void sh_cmt_disable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000343{
344 /* disable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100345 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000346
Magnus Dammbe890a12009-06-17 05:04:04 +0000347 /* disable interrupts in CMT block */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100348 sh_cmt_write_cmcsr(ch, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000349
Paul Mundt9436b4a2011-05-31 15:26:42 +0900350 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100351 clk_disable(ch->cmt->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200352
Laurent Pinchart7269f932014-01-27 15:29:19 +0100353 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
354 pm_runtime_put(&ch->cmt->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000355}
356
357/* private flags */
358#define FLAG_CLOCKEVENT (1 << 0)
359#define FLAG_CLOCKSOURCE (1 << 1)
360#define FLAG_REPROGRAM (1 << 2)
361#define FLAG_SKIPEVENT (1 << 3)
362#define FLAG_IRQCONTEXT (1 << 4)
363
Laurent Pinchart7269f932014-01-27 15:29:19 +0100364static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000365 int absolute)
366{
367 unsigned long new_match;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100368 unsigned long value = ch->next_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000369 unsigned long delay = 0;
370 unsigned long now = 0;
371 int has_wrapped;
372
Laurent Pinchart7269f932014-01-27 15:29:19 +0100373 now = sh_cmt_get_counter(ch, &has_wrapped);
374 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000375
376 if (has_wrapped) {
377 /* we're competing with the interrupt handler.
378 * -> let the interrupt handler reprogram the timer.
379 * -> interrupt number two handles the event.
380 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100381 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000382 return;
383 }
384
385 if (absolute)
386 now = 0;
387
388 do {
389 /* reprogram the timer hardware,
390 * but don't save the new match value yet.
391 */
392 new_match = now + value + delay;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100393 if (new_match > ch->max_match_value)
394 new_match = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000395
Laurent Pinchart7269f932014-01-27 15:29:19 +0100396 sh_cmt_write_cmcor(ch, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000397
Laurent Pinchart7269f932014-01-27 15:29:19 +0100398 now = sh_cmt_get_counter(ch, &has_wrapped);
399 if (has_wrapped && (new_match > ch->match_value)) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000400 /* we are changing to a greater match value,
401 * so this wrap must be caused by the counter
402 * matching the old value.
403 * -> first interrupt reprograms the timer.
404 * -> interrupt number two handles the event.
405 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100406 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000407 break;
408 }
409
410 if (has_wrapped) {
411 /* we are changing to a smaller match value,
412 * so the wrap must be caused by the counter
413 * matching the new value.
414 * -> save programmed match value.
415 * -> let isr handle the event.
416 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100417 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000418 break;
419 }
420
421 /* be safe: verify hardware settings */
422 if (now < new_match) {
423 /* timer value is below match value, all good.
424 * this makes sure we won't miss any match events.
425 * -> save programmed match value.
426 * -> let isr handle the event.
427 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100428 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000429 break;
430 }
431
432 /* the counter has reached a value greater
433 * than our new match value. and since the
434 * has_wrapped flag isn't set we must have
435 * programmed a too close event.
436 * -> increase delay and retry.
437 */
438 if (delay)
439 delay <<= 1;
440 else
441 delay = 1;
442
443 if (!delay)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100444 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
445 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000446
447 } while (delay);
448}
449
Laurent Pinchart7269f932014-01-27 15:29:19 +0100450static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Takashi YOSHII65ada542010-12-17 07:25:09 +0000451{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100452 if (delta > ch->max_match_value)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100453 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
454 ch->index);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000455
Laurent Pinchart7269f932014-01-27 15:29:19 +0100456 ch->next_match_value = delta;
457 sh_cmt_clock_event_program_verify(ch, 0);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000458}
459
Laurent Pinchart7269f932014-01-27 15:29:19 +0100460static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000461{
462 unsigned long flags;
463
Laurent Pinchart7269f932014-01-27 15:29:19 +0100464 raw_spin_lock_irqsave(&ch->lock, flags);
465 __sh_cmt_set_next(ch, delta);
466 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000467}
468
469static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
470{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100471 struct sh_cmt_channel *ch = dev_id;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000472
473 /* clear flags */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100474 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
475 ch->cmt->info->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000476
477 /* update clock source counter to begin with if enabled
478 * the wrap flag should be cleared by the timer specific
479 * isr before we end up here.
480 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100481 if (ch->flags & FLAG_CLOCKSOURCE)
482 ch->total_cycles += ch->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000483
Laurent Pinchart7269f932014-01-27 15:29:19 +0100484 if (!(ch->flags & FLAG_REPROGRAM))
485 ch->next_match_value = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000486
Laurent Pinchart7269f932014-01-27 15:29:19 +0100487 ch->flags |= FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000488
Laurent Pinchart7269f932014-01-27 15:29:19 +0100489 if (ch->flags & FLAG_CLOCKEVENT) {
490 if (!(ch->flags & FLAG_SKIPEVENT)) {
491 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
492 ch->next_match_value = ch->max_match_value;
493 ch->flags |= FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000494 }
495
Laurent Pinchart7269f932014-01-27 15:29:19 +0100496 ch->ced.event_handler(&ch->ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000497 }
498 }
499
Laurent Pinchart7269f932014-01-27 15:29:19 +0100500 ch->flags &= ~FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000501
Laurent Pinchart7269f932014-01-27 15:29:19 +0100502 if (ch->flags & FLAG_REPROGRAM) {
503 ch->flags &= ~FLAG_REPROGRAM;
504 sh_cmt_clock_event_program_verify(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000505
Laurent Pinchart7269f932014-01-27 15:29:19 +0100506 if (ch->flags & FLAG_CLOCKEVENT)
507 if ((ch->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
508 || (ch->match_value == ch->next_match_value))
509 ch->flags &= ~FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000510 }
511
Laurent Pinchart7269f932014-01-27 15:29:19 +0100512 ch->flags &= ~FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000513
514 return IRQ_HANDLED;
515}
516
Laurent Pinchart7269f932014-01-27 15:29:19 +0100517static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000518{
519 int ret = 0;
520 unsigned long flags;
521
Laurent Pinchart7269f932014-01-27 15:29:19 +0100522 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000523
Laurent Pinchart7269f932014-01-27 15:29:19 +0100524 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
525 ret = sh_cmt_enable(ch, &ch->rate);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000526
527 if (ret)
528 goto out;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100529 ch->flags |= flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000530
531 /* setup timeout if no clockevent */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100532 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
533 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000534 out:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100535 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000536
537 return ret;
538}
539
Laurent Pinchart7269f932014-01-27 15:29:19 +0100540static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000541{
542 unsigned long flags;
543 unsigned long f;
544
Laurent Pinchart7269f932014-01-27 15:29:19 +0100545 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000546
Laurent Pinchart7269f932014-01-27 15:29:19 +0100547 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
548 ch->flags &= ~flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000549
Laurent Pinchart7269f932014-01-27 15:29:19 +0100550 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
551 sh_cmt_disable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000552
553 /* adjust the timeout to maximum if only clocksource left */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100554 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
555 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000556
Laurent Pinchart7269f932014-01-27 15:29:19 +0100557 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000558}
559
Laurent Pinchart7269f932014-01-27 15:29:19 +0100560static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000561{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100562 return container_of(cs, struct sh_cmt_channel, cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000563}
564
565static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
566{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100567 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000568 unsigned long flags, raw;
569 unsigned long value;
570 int has_wrapped;
571
Laurent Pinchart7269f932014-01-27 15:29:19 +0100572 raw_spin_lock_irqsave(&ch->lock, flags);
573 value = ch->total_cycles;
574 raw = sh_cmt_get_counter(ch, &has_wrapped);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000575
576 if (unlikely(has_wrapped))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100577 raw += ch->match_value + 1;
578 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000579
580 return value + raw;
581}
582
583static int sh_cmt_clocksource_enable(struct clocksource *cs)
584{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900585 int ret;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100586 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000587
Laurent Pinchart7269f932014-01-27 15:29:19 +0100588 WARN_ON(ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200589
Laurent Pinchart7269f932014-01-27 15:29:19 +0100590 ch->total_cycles = 0;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000591
Laurent Pinchart7269f932014-01-27 15:29:19 +0100592 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200593 if (!ret) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100594 __clocksource_updatefreq_hz(cs, ch->rate);
595 ch->cs_enabled = true;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200596 }
Magnus Damm3593f5f2011-04-25 22:32:11 +0900597 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000598}
599
600static void sh_cmt_clocksource_disable(struct clocksource *cs)
601{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100602 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200603
Laurent Pinchart7269f932014-01-27 15:29:19 +0100604 WARN_ON(!ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200605
Laurent Pinchart7269f932014-01-27 15:29:19 +0100606 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
607 ch->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000608}
609
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200610static void sh_cmt_clocksource_suspend(struct clocksource *cs)
611{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100612 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200613
Laurent Pinchart7269f932014-01-27 15:29:19 +0100614 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
615 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200616}
617
Magnus Dammc8162882010-02-02 14:41:40 -0800618static void sh_cmt_clocksource_resume(struct clocksource *cs)
619{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100620 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200621
Laurent Pinchart7269f932014-01-27 15:29:19 +0100622 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
623 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800624}
625
Laurent Pinchart7269f932014-01-27 15:29:19 +0100626static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100627 const char *name, unsigned long rating)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000628{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100629 struct clocksource *cs = &ch->cs;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000630
631 cs->name = name;
632 cs->rating = rating;
633 cs->read = sh_cmt_clocksource_read;
634 cs->enable = sh_cmt_clocksource_enable;
635 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200636 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800637 cs->resume = sh_cmt_clocksource_resume;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000638 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
639 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900640
Laurent Pinchart740a9512014-01-27 22:04:17 +0100641 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
642 ch->index);
Paul Mundtf4d7c352010-06-02 17:10:44 +0900643
Magnus Damm3593f5f2011-04-25 22:32:11 +0900644 /* Register with dummy 1 Hz value, gets updated in ->enable() */
645 clocksource_register_hz(cs, 1);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000646 return 0;
647}
648
Laurent Pinchart7269f932014-01-27 15:29:19 +0100649static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000650{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100651 return container_of(ced, struct sh_cmt_channel, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000652}
653
Laurent Pinchart7269f932014-01-27 15:29:19 +0100654static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000655{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100656 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000657
Laurent Pinchart7269f932014-01-27 15:29:19 +0100658 sh_cmt_start(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000659
660 /* TODO: calculate good shift from rate and counter bit width */
661
662 ced->shift = 32;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100663 ced->mult = div_sc(ch->rate, NSEC_PER_SEC, ced->shift);
664 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000665 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
666
667 if (periodic)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100668 sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000669 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100670 sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000671}
672
673static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
674 struct clock_event_device *ced)
675{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100676 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000677
678 /* deal with old setting first */
679 switch (ced->mode) {
680 case CLOCK_EVT_MODE_PERIODIC:
681 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100682 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000683 break;
684 default:
685 break;
686 }
687
688 switch (mode) {
689 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100690 dev_info(&ch->cmt->pdev->dev,
Laurent Pinchart740a9512014-01-27 22:04:17 +0100691 "ch%u: used for periodic clock events\n", ch->index);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100692 sh_cmt_clock_event_start(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000693 break;
694 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100695 dev_info(&ch->cmt->pdev->dev,
Laurent Pinchart740a9512014-01-27 22:04:17 +0100696 "ch%u: used for oneshot clock events\n", ch->index);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100697 sh_cmt_clock_event_start(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000698 break;
699 case CLOCK_EVT_MODE_SHUTDOWN:
700 case CLOCK_EVT_MODE_UNUSED:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100701 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000702 break;
703 default:
704 break;
705 }
706}
707
708static int sh_cmt_clock_event_next(unsigned long delta,
709 struct clock_event_device *ced)
710{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100711 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000712
713 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100714 if (likely(ch->flags & FLAG_IRQCONTEXT))
715 ch->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000716 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100717 sh_cmt_set_next(ch, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000718
719 return 0;
720}
721
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200722static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
723{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100724 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900725
Laurent Pinchart7269f932014-01-27 15:29:19 +0100726 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
727 clk_unprepare(ch->cmt->clk);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200728}
729
730static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
731{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100732 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900733
Laurent Pinchart7269f932014-01-27 15:29:19 +0100734 clk_prepare(ch->cmt->clk);
735 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200736}
737
Laurent Pinchart7269f932014-01-27 15:29:19 +0100738static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100739 const char *name, unsigned long rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000740{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100741 struct clock_event_device *ced = &ch->ced;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000742
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000743 ced->name = name;
744 ced->features = CLOCK_EVT_FEAT_PERIODIC;
745 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
746 ced->rating = rating;
747 ced->cpumask = cpumask_of(0);
748 ced->set_next_event = sh_cmt_clock_event_next;
749 ced->set_mode = sh_cmt_clock_event_mode;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200750 ced->suspend = sh_cmt_clock_event_suspend;
751 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000752
Laurent Pinchart740a9512014-01-27 22:04:17 +0100753 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
754 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000755 clockevents_register_device(ced);
756}
757
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100758static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
Paul Mundtd1fcc0a2009-05-03 18:05:42 +0900759 unsigned long clockevent_rating,
760 unsigned long clocksource_rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000761{
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000762 if (clockevent_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100763 sh_cmt_register_clockevent(ch, name, clockevent_rating);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000764
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000765 if (clocksource_rating)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100766 sh_cmt_register_clocksource(ch, name, clocksource_rating);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000767
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000768 return 0;
769}
770
Laurent Pinchart740a9512014-01-27 22:04:17 +0100771static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100772 struct sh_cmt_device *cmt)
773{
774 struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
775 int irq;
776 int ret;
777
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100778 ch->cmt = cmt;
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100779 ch->base = cmt->mapbase_ch;
Laurent Pinchart740a9512014-01-27 22:04:17 +0100780 ch->index = index;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100781
782 irq = platform_get_irq(cmt->pdev, 0);
783 if (irq < 0) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100784 dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n",
785 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100786 return irq;
787 }
788
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100789 if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100790 ch->max_match_value = ~0;
791 else
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100792 ch->max_match_value = (1 << cmt->info->width) - 1;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100793
794 ch->match_value = ch->max_match_value;
795 raw_spin_lock_init(&ch->lock);
796
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100797 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100798 cfg->clockevent_rating,
799 cfg->clocksource_rating);
800 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100801 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
802 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100803 return ret;
804 }
805 ch->cs_enabled = false;
806
807 ret = request_irq(irq, sh_cmt_interrupt,
808 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
809 dev_name(&cmt->pdev->dev), ch);
810 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100811 dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n",
812 ch->index, irq);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100813 return ret;
814 }
815
816 return 0;
817}
818
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100819static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000820{
Paul Mundt46a12f72009-05-03 17:57:17 +0900821 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm8874c5e2013-06-17 15:40:52 +0900822 struct resource *res, *res2;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100823 int ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000824 ret = -ENXIO;
825
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100826 cmt->pdev = pdev;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000827
828 if (!cfg) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100829 dev_err(&cmt->pdev->dev, "missing platform data\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000830 goto err0;
831 }
832
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100833 res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000834 if (!res) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100835 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000836 goto err0;
837 }
838
Magnus Damm8874c5e2013-06-17 15:40:52 +0900839 /* optional resource for the shared timer start/stop register */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100840 res2 = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 1);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900841
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100842 /* map memory, let mapbase_ch point to our channel */
843 cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res));
844 if (cmt->mapbase_ch == NULL) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100845 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000846 goto err0;
847 }
848
Magnus Damm8874c5e2013-06-17 15:40:52 +0900849 /* map second resource for CMSTR */
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100850 cmt->mapbase = ioremap_nocache(res2 ? res2->start :
851 res->start - cfg->channel_offset,
852 res2 ? resource_size(res2) : 2);
853 if (cmt->mapbase == NULL) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100854 dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
Magnus Damm8874c5e2013-06-17 15:40:52 +0900855 goto err1;
856 }
857
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000858 /* get hold of clock */
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100859 cmt->clk = clk_get(&cmt->pdev->dev, "cmt_fck");
860 if (IS_ERR(cmt->clk)) {
861 dev_err(&cmt->pdev->dev, "cannot get clock\n");
862 ret = PTR_ERR(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900863 goto err2;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000864 }
865
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100866 ret = clk_prepare(cmt->clk);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900867 if (ret < 0)
868 goto err3;
869
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100870 /* identify the model based on the resources */
871 if (resource_size(res) == 6)
872 cmt->info = &sh_cmt_info[SH_CMT_16BIT];
873 else if (res2 && (resource_size(res2) == 4))
874 cmt->info = &sh_cmt_info[SH_CMT_48BIT_GEN2];
875 else
876 cmt->info = &sh_cmt_info[SH_CMT_32BIT];
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000877
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100878 cmt->channels = kzalloc(sizeof(*cmt->channels), GFP_KERNEL);
879 if (cmt->channels == NULL) {
880 ret = -ENOMEM;
881 goto err4;
882 }
883
884 cmt->num_channels = 1;
885
886 ret = sh_cmt_setup_channel(&cmt->channels[0], cfg->timer_bit, cmt);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100887 if (ret < 0)
Laurent Pinchart57dee992013-12-14 15:07:32 +0900888 goto err4;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900889
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100890 platform_set_drvdata(pdev, cmt);
Magnus Dammadccc692012-12-14 14:53:51 +0900891
Paul Mundtda64c2a2010-02-25 16:37:46 +0900892 return 0;
Laurent Pinchart57dee992013-12-14 15:07:32 +0900893err4:
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100894 kfree(cmt->channels);
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100895 clk_unprepare(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900896err3:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100897 clk_put(cmt->clk);
Magnus Damm8874c5e2013-06-17 15:40:52 +0900898err2:
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100899 iounmap(cmt->mapbase);
Laurent Pinchart36f1ac92014-01-27 22:04:17 +0100900err1:
901 iounmap(cmt->mapbase_ch);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900902err0:
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000903 return ret;
904}
905
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800906static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000907{
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100908 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200909 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000910 int ret;
911
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200912 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200913 pm_runtime_set_active(&pdev->dev);
914 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200915 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +0100916
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100917 if (cmt) {
Paul Mundt214a6072010-03-10 16:26:25 +0900918 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200919 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +0000920 }
921
Laurent Pinchartb262bc72014-01-27 22:04:17 +0100922 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100923 if (cmt == NULL) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000924 dev_err(&pdev->dev, "failed to allocate driver data\n");
925 return -ENOMEM;
926 }
927
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100928 ret = sh_cmt_setup(cmt, pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000929 if (ret) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100930 kfree(cmt);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200931 pm_runtime_idle(&pdev->dev);
932 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000933 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200934 if (is_early_platform_device(pdev))
935 return 0;
936
937 out:
938 if (cfg->clockevent_rating || cfg->clocksource_rating)
939 pm_runtime_irq_safe(&pdev->dev);
940 else
941 pm_runtime_idle(&pdev->dev);
942
943 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000944}
945
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800946static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000947{
948 return -EBUSY; /* cannot unregister clockevent and clocksource */
949}
950
951static struct platform_driver sh_cmt_device_driver = {
952 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800953 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000954 .driver = {
955 .name = "sh_cmt",
956 }
957};
958
959static int __init sh_cmt_init(void)
960{
961 return platform_driver_register(&sh_cmt_device_driver);
962}
963
964static void __exit sh_cmt_exit(void)
965{
966 platform_driver_unregister(&sh_cmt_device_driver);
967}
968
Magnus Damme475eed2009-04-15 10:50:04 +0000969early_platform_init("earlytimer", &sh_cmt_device_driver);
Simon Hormane903a032013-03-05 15:40:42 +0900970subsys_initcall(sh_cmt_init);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000971module_exit(sh_cmt_exit);
972
973MODULE_AUTHOR("Magnus Damm");
974MODULE_DESCRIPTION("SuperH CMT Timer Driver");
975MODULE_LICENSE("GPL v2");