blob: cf07797dbcf3c7f0d8a1f26b189b11c2cde3540a [file] [log] [blame]
Magnus Damm9570ef22009-05-01 06:51:00 +00001/*
2 * SuperH Timer Support - TMU
3 *
4 * Copyright (C) 2009 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>
21#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/delay.h>
26#include <linux/io.h>
27#include <linux/clk.h>
28#include <linux/irq.h>
29#include <linux/err.h>
30#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. Wysocki2ee619f2012-03-13 22:40:00 +010035#include <linux/pm_domain.h>
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +020036#include <linux/pm_runtime.h>
Magnus Damm9570ef22009-05-01 06:51:00 +000037
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010038enum sh_tmu_model {
39 SH_TMU_LEGACY,
40 SH_TMU,
41 SH_TMU_SH3,
42};
43
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010044struct sh_tmu_device;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010045
46struct sh_tmu_channel {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010047 struct sh_tmu_device *tmu;
Laurent Pinchartfe68eb82014-01-27 22:04:17 +010048 unsigned int index;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010049
Laurent Pinchartde693462014-01-27 22:04:17 +010050 void __iomem *base;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +010051 int irq;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010052
Magnus Damm9570ef22009-05-01 06:51:00 +000053 unsigned long rate;
54 unsigned long periodic;
55 struct clock_event_device ced;
56 struct clocksource cs;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +020057 bool cs_enabled;
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +020058 unsigned int enable_count;
Magnus Damm9570ef22009-05-01 06:51:00 +000059};
60
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010061struct sh_tmu_device {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010062 struct platform_device *pdev;
63
64 void __iomem *mapbase;
65 struct clk *clk;
66
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010067 enum sh_tmu_model model;
68
Laurent Pincharta5de49f2014-01-27 22:04:17 +010069 struct sh_tmu_channel *channels;
70 unsigned int num_channels;
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010071
72 bool has_clockevent;
73 bool has_clocksource;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010074};
75
Paul Mundtc2225a52012-05-25 13:39:09 +090076static DEFINE_RAW_SPINLOCK(sh_tmu_lock);
Magnus Damm9570ef22009-05-01 06:51:00 +000077
78#define TSTR -1 /* shared register */
79#define TCOR 0 /* channel register */
80#define TCNT 1 /* channel register */
81#define TCR 2 /* channel register */
82
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +010083#define TCR_UNF (1 << 8)
84#define TCR_UNIE (1 << 5)
85#define TCR_TPSC_CLK4 (0 << 0)
86#define TCR_TPSC_CLK16 (1 << 0)
87#define TCR_TPSC_CLK64 (2 << 0)
88#define TCR_TPSC_CLK256 (3 << 0)
89#define TCR_TPSC_CLK1024 (4 << 0)
90#define TCR_TPSC_MASK (7 << 0)
91
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010092static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr)
Magnus Damm9570ef22009-05-01 06:51:00 +000093{
Magnus Damm9570ef22009-05-01 06:51:00 +000094 unsigned long offs;
95
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010096 if (reg_nr == TSTR) {
97 switch (ch->tmu->model) {
98 case SH_TMU_LEGACY:
99 return ioread8(ch->tmu->mapbase);
100 case SH_TMU_SH3:
101 return ioread8(ch->tmu->mapbase + 2);
102 case SH_TMU:
103 return ioread8(ch->tmu->mapbase + 4);
104 }
105 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000106
107 offs = reg_nr << 2;
108
109 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +0100110 return ioread16(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000111 else
Laurent Pinchartde693462014-01-27 22:04:17 +0100112 return ioread32(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000113}
114
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100115static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr,
Magnus Damm9570ef22009-05-01 06:51:00 +0000116 unsigned long value)
117{
Magnus Damm9570ef22009-05-01 06:51:00 +0000118 unsigned long offs;
119
120 if (reg_nr == TSTR) {
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100121 switch (ch->tmu->model) {
122 case SH_TMU_LEGACY:
123 return iowrite8(value, ch->tmu->mapbase);
124 case SH_TMU_SH3:
125 return iowrite8(value, ch->tmu->mapbase + 2);
126 case SH_TMU:
127 return iowrite8(value, ch->tmu->mapbase + 4);
128 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000129 }
130
131 offs = reg_nr << 2;
132
133 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +0100134 iowrite16(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000135 else
Laurent Pinchartde693462014-01-27 22:04:17 +0100136 iowrite32(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000137}
138
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100139static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start)
Magnus Damm9570ef22009-05-01 06:51:00 +0000140{
Magnus Damm9570ef22009-05-01 06:51:00 +0000141 unsigned long flags, value;
142
143 /* start stop register shared by multiple timer channels */
Paul Mundtc2225a52012-05-25 13:39:09 +0900144 raw_spin_lock_irqsave(&sh_tmu_lock, flags);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100145 value = sh_tmu_read(ch, TSTR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000146
147 if (start)
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100148 value |= 1 << ch->index;
Magnus Damm9570ef22009-05-01 06:51:00 +0000149 else
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100150 value &= ~(1 << ch->index);
Magnus Damm9570ef22009-05-01 06:51:00 +0000151
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100152 sh_tmu_write(ch, TSTR, value);
Paul Mundtc2225a52012-05-25 13:39:09 +0900153 raw_spin_unlock_irqrestore(&sh_tmu_lock, flags);
Magnus Damm9570ef22009-05-01 06:51:00 +0000154}
155
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100156static int __sh_tmu_enable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000157{
Magnus Damm9570ef22009-05-01 06:51:00 +0000158 int ret;
159
Paul Mundtd4905ce2011-05-31 15:23:20 +0900160 /* enable clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100161 ret = clk_enable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000162 if (ret) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100163 dev_err(&ch->tmu->pdev->dev, "ch%u: cannot enable clock\n",
164 ch->index);
Magnus Damm9570ef22009-05-01 06:51:00 +0000165 return ret;
166 }
167
168 /* make sure channel is disabled */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100169 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000170
171 /* maximum timeout */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100172 sh_tmu_write(ch, TCOR, 0xffffffff);
173 sh_tmu_write(ch, TCNT, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000174
175 /* configure channel to parent clock / 4, irq off */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100176 ch->rate = clk_get_rate(ch->tmu->clk) / 4;
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100177 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000178
179 /* enable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100180 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000181
182 return 0;
183}
184
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100185static int sh_tmu_enable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200186{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100187 if (ch->enable_count++ > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200188 return 0;
189
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100190 pm_runtime_get_sync(&ch->tmu->pdev->dev);
191 dev_pm_syscore_device(&ch->tmu->pdev->dev, true);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200192
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100193 return __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200194}
195
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100196static void __sh_tmu_disable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000197{
198 /* disable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100199 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000200
Magnus Dammbe890a12009-06-17 05:04:04 +0000201 /* disable interrupts in TMU block */
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100202 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Dammbe890a12009-06-17 05:04:04 +0000203
Paul Mundtd4905ce2011-05-31 15:23:20 +0900204 /* stop clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100205 clk_disable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000206}
207
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100208static void sh_tmu_disable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200209{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100210 if (WARN_ON(ch->enable_count == 0))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200211 return;
212
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100213 if (--ch->enable_count > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200214 return;
215
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100216 __sh_tmu_disable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200217
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100218 dev_pm_syscore_device(&ch->tmu->pdev->dev, false);
219 pm_runtime_put(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200220}
221
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100222static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta,
Magnus Damm9570ef22009-05-01 06:51:00 +0000223 int periodic)
224{
225 /* stop timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100226 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000227
228 /* acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100229 sh_tmu_read(ch, TCR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000230
231 /* enable interrupt */
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100232 sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000233
234 /* reload delta value in case of periodic timer */
235 if (periodic)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100236 sh_tmu_write(ch, TCOR, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000237 else
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100238 sh_tmu_write(ch, TCOR, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000239
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100240 sh_tmu_write(ch, TCNT, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000241
242 /* start timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100243 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000244}
245
246static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id)
247{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100248 struct sh_tmu_channel *ch = dev_id;
Magnus Damm9570ef22009-05-01 06:51:00 +0000249
250 /* disable or acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100251 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT)
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100252 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000253 else
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100254 sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000255
256 /* notify clockevent layer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100257 ch->ced.event_handler(&ch->ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000258 return IRQ_HANDLED;
259}
260
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100261static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs)
Magnus Damm9570ef22009-05-01 06:51:00 +0000262{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100263 return container_of(cs, struct sh_tmu_channel, cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000264}
265
266static cycle_t sh_tmu_clocksource_read(struct clocksource *cs)
267{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100268 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000269
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100270 return sh_tmu_read(ch, TCNT) ^ 0xffffffff;
Magnus Damm9570ef22009-05-01 06:51:00 +0000271}
272
273static int sh_tmu_clocksource_enable(struct clocksource *cs)
274{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100275 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm0aeac452011-04-25 22:38:37 +0900276 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000277
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100278 if (WARN_ON(ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200279 return 0;
280
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100281 ret = sh_tmu_enable(ch);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200282 if (!ret) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100283 __clocksource_updatefreq_hz(cs, ch->rate);
284 ch->cs_enabled = true;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200285 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200286
Magnus Damm0aeac452011-04-25 22:38:37 +0900287 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000288}
289
290static void sh_tmu_clocksource_disable(struct clocksource *cs)
291{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100292 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200293
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100294 if (WARN_ON(!ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200295 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200296
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100297 sh_tmu_disable(ch);
298 ch->cs_enabled = false;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200299}
300
301static void sh_tmu_clocksource_suspend(struct clocksource *cs)
302{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100303 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200304
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100305 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200306 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200307
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100308 if (--ch->enable_count == 0) {
309 __sh_tmu_disable(ch);
310 pm_genpd_syscore_poweroff(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200311 }
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200312}
313
314static void sh_tmu_clocksource_resume(struct clocksource *cs)
315{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100316 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200317
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100318 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200319 return;
320
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100321 if (ch->enable_count++ == 0) {
322 pm_genpd_syscore_poweron(&ch->tmu->pdev->dev);
323 __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200324 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000325}
326
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100327static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100328 const char *name)
Magnus Damm9570ef22009-05-01 06:51:00 +0000329{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100330 struct clocksource *cs = &ch->cs;
Magnus Damm9570ef22009-05-01 06:51:00 +0000331
332 cs->name = name;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100333 cs->rating = 200;
Magnus Damm9570ef22009-05-01 06:51:00 +0000334 cs->read = sh_tmu_clocksource_read;
335 cs->enable = sh_tmu_clocksource_enable;
336 cs->disable = sh_tmu_clocksource_disable;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200337 cs->suspend = sh_tmu_clocksource_suspend;
338 cs->resume = sh_tmu_clocksource_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000339 cs->mask = CLOCKSOURCE_MASK(32);
340 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Aurelien Jarno66f49122010-05-31 21:45:48 +0000341
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100342 dev_info(&ch->tmu->pdev->dev, "ch%u: used as clock source\n",
343 ch->index);
Magnus Damm0aeac452011-04-25 22:38:37 +0900344
345 /* Register with dummy 1 Hz value, gets updated in ->enable() */
346 clocksource_register_hz(cs, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000347 return 0;
348}
349
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100350static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced)
Magnus Damm9570ef22009-05-01 06:51:00 +0000351{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100352 return container_of(ced, struct sh_tmu_channel, ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000353}
354
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100355static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic)
Magnus Damm9570ef22009-05-01 06:51:00 +0000356{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100357 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000358
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100359 sh_tmu_enable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000360
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100361 clockevents_config(ced, ch->rate);
Magnus Damm9570ef22009-05-01 06:51:00 +0000362
363 if (periodic) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100364 ch->periodic = (ch->rate + HZ/2) / HZ;
365 sh_tmu_set_next(ch, ch->periodic, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000366 }
367}
368
369static void sh_tmu_clock_event_mode(enum clock_event_mode mode,
370 struct clock_event_device *ced)
371{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100372 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000373 int disabled = 0;
374
375 /* deal with old setting first */
376 switch (ced->mode) {
377 case CLOCK_EVT_MODE_PERIODIC:
378 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100379 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000380 disabled = 1;
381 break;
382 default:
383 break;
384 }
385
386 switch (mode) {
387 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100388 dev_info(&ch->tmu->pdev->dev,
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100389 "ch%u: used for periodic clock events\n", ch->index);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100390 sh_tmu_clock_event_start(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000391 break;
392 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100393 dev_info(&ch->tmu->pdev->dev,
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100394 "ch%u: used for oneshot clock events\n", ch->index);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100395 sh_tmu_clock_event_start(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000396 break;
397 case CLOCK_EVT_MODE_UNUSED:
398 if (!disabled)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100399 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000400 break;
401 case CLOCK_EVT_MODE_SHUTDOWN:
402 default:
403 break;
404 }
405}
406
407static int sh_tmu_clock_event_next(unsigned long delta,
408 struct clock_event_device *ced)
409{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100410 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000411
412 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
413
414 /* program new delta value */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100415 sh_tmu_set_next(ch, delta, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000416 return 0;
417}
418
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200419static void sh_tmu_clock_event_suspend(struct clock_event_device *ced)
420{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100421 pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200422}
423
424static void sh_tmu_clock_event_resume(struct clock_event_device *ced)
425{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100426 pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200427}
428
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100429static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100430 const char *name)
Magnus Damm9570ef22009-05-01 06:51:00 +0000431{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100432 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000433 int ret;
434
Magnus Damm9570ef22009-05-01 06:51:00 +0000435 ced->name = name;
436 ced->features = CLOCK_EVT_FEAT_PERIODIC;
437 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100438 ced->rating = 200;
Magnus Damm9570ef22009-05-01 06:51:00 +0000439 ced->cpumask = cpumask_of(0);
440 ced->set_next_event = sh_tmu_clock_event_next;
441 ced->set_mode = sh_tmu_clock_event_mode;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200442 ced->suspend = sh_tmu_clock_event_suspend;
443 ced->resume = sh_tmu_clock_event_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000444
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100445 dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n",
446 ch->index);
Paul Mundt39774072012-06-11 17:10:16 +0900447
448 clockevents_config_and_register(ced, 1, 0x300, 0xffffffff);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900449
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100450 ret = request_irq(ch->irq, sh_tmu_interrupt,
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100451 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100452 dev_name(&ch->tmu->pdev->dev), ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000453 if (ret) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100454 dev_err(&ch->tmu->pdev->dev, "ch%u: failed to request irq %d\n",
455 ch->index, ch->irq);
Magnus Damm9570ef22009-05-01 06:51:00 +0000456 return;
457 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000458}
459
Laurent Pinchart84876d02014-02-17 16:04:16 +0100460static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100461 bool clockevent, bool clocksource)
Magnus Damm9570ef22009-05-01 06:51:00 +0000462{
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100463 if (clockevent) {
464 ch->tmu->has_clockevent = true;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100465 sh_tmu_register_clockevent(ch, name);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100466 } else if (clocksource) {
467 ch->tmu->has_clocksource = true;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100468 sh_tmu_register_clocksource(ch, name);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100469 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000470
471 return 0;
472}
473
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100474static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index,
475 bool clockevent, bool clocksource,
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100476 struct sh_tmu_device *tmu)
477{
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100478 /* Skip unused channels. */
479 if (!clockevent && !clocksource)
480 return 0;
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100481
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100482 ch->tmu = tmu;
483
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100484 if (tmu->model == SH_TMU_LEGACY) {
485 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100486
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100487 /*
488 * The SH3 variant (SH770x, SH7705, SH7710 and SH7720) maps
489 * channel registers blocks at base + 2 + 12 * index, while all
490 * other variants map them at base + 4 + 12 * index. We can
491 * compute the index by just dividing by 12, the 2 bytes or 4
492 * bytes offset being hidden by the integer division.
493 */
494 ch->index = cfg->channel_offset / 12;
495 ch->base = tmu->mapbase + cfg->channel_offset;
496 } else {
497 ch->index = index;
498
499 if (tmu->model == SH_TMU_SH3)
500 ch->base = tmu->mapbase + 4 + ch->index * 12;
501 else
502 ch->base = tmu->mapbase + 8 + ch->index * 12;
503 }
504
505 ch->irq = platform_get_irq(tmu->pdev, ch->index);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100506 if (ch->irq < 0) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100507 dev_err(&tmu->pdev->dev, "ch%u: failed to get irq\n",
508 ch->index);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100509 return ch->irq;
510 }
511
512 ch->cs_enabled = false;
513 ch->enable_count = 0;
514
Laurent Pinchart84876d02014-02-17 16:04:16 +0100515 return sh_tmu_register(ch, dev_name(&tmu->pdev->dev),
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100516 clockevent, clocksource);
517}
518
519static int sh_tmu_map_memory(struct sh_tmu_device *tmu)
520{
521 struct resource *res;
522
523 res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0);
524 if (!res) {
525 dev_err(&tmu->pdev->dev, "failed to get I/O memory\n");
526 return -ENXIO;
527 }
528
529 tmu->mapbase = ioremap_nocache(res->start, resource_size(res));
530 if (tmu->mapbase == NULL)
531 return -ENXIO;
532
533 /*
534 * In legacy platform device configuration (with one device per channel)
535 * the resource points to the channel base address.
536 */
537 if (tmu->model == SH_TMU_LEGACY) {
538 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
539 tmu->mapbase -= cfg->channel_offset;
540 }
541
542 return 0;
543}
544
545static void sh_tmu_unmap_memory(struct sh_tmu_device *tmu)
546{
547 if (tmu->model == SH_TMU_LEGACY) {
548 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
549 tmu->mapbase += cfg->channel_offset;
550 }
551
552 iounmap(tmu->mapbase);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100553}
554
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100555static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000556{
Paul Mundt46a12f72009-05-03 17:57:17 +0900557 struct sh_timer_config *cfg = pdev->dev.platform_data;
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100558 const struct platform_device_id *id = pdev->id_entry;
559 unsigned int i;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100560 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000561
562 if (!cfg) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100563 dev_err(&tmu->pdev->dev, "missing platform data\n");
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100564 return -ENXIO;
Magnus Damm9570ef22009-05-01 06:51:00 +0000565 }
566
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100567 tmu->pdev = pdev;
568 tmu->model = id->driver_data;
Magnus Damm9570ef22009-05-01 06:51:00 +0000569
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100570 /* Get hold of clock. */
Laurent Pincharta27d9222014-02-14 00:35:18 +0100571 tmu->clk = clk_get(&tmu->pdev->dev,
572 tmu->model == SH_TMU_LEGACY ? "tmu_fck" : "fck");
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100573 if (IS_ERR(tmu->clk)) {
574 dev_err(&tmu->pdev->dev, "cannot get clock\n");
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100575 return PTR_ERR(tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000576 }
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100577
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100578 ret = clk_prepare(tmu->clk);
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100579 if (ret < 0)
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100580 goto err_clk_put;
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100581
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100582 /* Map the memory resource. */
583 ret = sh_tmu_map_memory(tmu);
584 if (ret < 0) {
585 dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n");
586 goto err_clk_unprepare;
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100587 }
588
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100589 /* Allocate and setup the channels. */
590 if (tmu->model == SH_TMU_LEGACY)
591 tmu->num_channels = 1;
592 else
593 tmu->num_channels = hweight8(cfg->channels_mask);
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100594
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100595 tmu->channels = kzalloc(sizeof(*tmu->channels) * tmu->num_channels,
596 GFP_KERNEL);
597 if (tmu->channels == NULL) {
598 ret = -ENOMEM;
599 goto err_unmap;
600 }
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100601
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100602 if (tmu->model == SH_TMU_LEGACY) {
603 ret = sh_tmu_channel_setup(&tmu->channels[0], 0,
604 cfg->clockevent_rating != 0,
605 cfg->clocksource_rating != 0, tmu);
606 if (ret < 0)
607 goto err_unmap;
608 } else {
609 /*
610 * Use the first channel as a clock event device and the second
611 * channel as a clock source.
612 */
613 for (i = 0; i < tmu->num_channels; ++i) {
614 ret = sh_tmu_channel_setup(&tmu->channels[i], i,
615 i == 0, i == 1, tmu);
616 if (ret < 0)
617 goto err_unmap;
618 }
619 }
620
621 platform_set_drvdata(pdev, tmu);
Laurent Pinchart394a4482013-11-08 11:07:59 +0100622
623 return 0;
624
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100625err_unmap:
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100626 kfree(tmu->channels);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100627 sh_tmu_unmap_memory(tmu);
628err_clk_unprepare:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100629 clk_unprepare(tmu->clk);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100630err_clk_put:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100631 clk_put(tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000632 return ret;
633}
634
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800635static int sh_tmu_probe(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000636{
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100637 struct sh_tmu_device *tmu = platform_get_drvdata(pdev);
Magnus Damm9570ef22009-05-01 06:51:00 +0000638 int ret;
639
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200640 if (!is_early_platform_device(pdev)) {
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200641 pm_runtime_set_active(&pdev->dev);
642 pm_runtime_enable(&pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200643 }
Rafael J. Wysocki2ee619f2012-03-13 22:40:00 +0100644
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100645 if (tmu) {
Paul Mundt214a6072010-03-10 16:26:25 +0900646 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200647 goto out;
Magnus Damm9570ef22009-05-01 06:51:00 +0000648 }
649
Laurent Pinchart3b77a832014-01-27 22:04:17 +0100650 tmu = kzalloc(sizeof(*tmu), GFP_KERNEL);
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100651 if (tmu == NULL) {
Magnus Damm9570ef22009-05-01 06:51:00 +0000652 dev_err(&pdev->dev, "failed to allocate driver data\n");
653 return -ENOMEM;
654 }
655
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100656 ret = sh_tmu_setup(tmu, pdev);
Magnus Damm9570ef22009-05-01 06:51:00 +0000657 if (ret) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100658 kfree(tmu);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200659 pm_runtime_idle(&pdev->dev);
660 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000661 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200662 if (is_early_platform_device(pdev))
663 return 0;
664
665 out:
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100666 if (tmu->has_clockevent || tmu->has_clocksource)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200667 pm_runtime_irq_safe(&pdev->dev);
668 else
669 pm_runtime_idle(&pdev->dev);
670
671 return 0;
Magnus Damm9570ef22009-05-01 06:51:00 +0000672}
673
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800674static int sh_tmu_remove(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000675{
676 return -EBUSY; /* cannot unregister clockevent and clocksource */
677}
678
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100679static const struct platform_device_id sh_tmu_id_table[] = {
680 { "sh_tmu", SH_TMU_LEGACY },
681 { "sh-tmu", SH_TMU },
682 { "sh-tmu-sh3", SH_TMU_SH3 },
683 { }
684};
685MODULE_DEVICE_TABLE(platform, sh_tmu_id_table);
686
Magnus Damm9570ef22009-05-01 06:51:00 +0000687static struct platform_driver sh_tmu_device_driver = {
688 .probe = sh_tmu_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800689 .remove = sh_tmu_remove,
Magnus Damm9570ef22009-05-01 06:51:00 +0000690 .driver = {
691 .name = "sh_tmu",
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100692 },
693 .id_table = sh_tmu_id_table,
Magnus Damm9570ef22009-05-01 06:51:00 +0000694};
695
696static int __init sh_tmu_init(void)
697{
698 return platform_driver_register(&sh_tmu_device_driver);
699}
700
701static void __exit sh_tmu_exit(void)
702{
703 platform_driver_unregister(&sh_tmu_device_driver);
704}
705
706early_platform_init("earlytimer", &sh_tmu_device_driver);
Simon Hormanb9773c32013-03-05 15:40:42 +0900707subsys_initcall(sh_tmu_init);
Magnus Damm9570ef22009-05-01 06:51:00 +0000708module_exit(sh_tmu_exit);
709
710MODULE_AUTHOR("Magnus Damm");
711MODULE_DESCRIPTION("SuperH TMU Timer Driver");
712MODULE_LICENSE("GPL v2");