blob: a464ed868a684b8fbf74de36c1a58e6e0f8d3a60 [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 Pinchart0a72aa32014-01-27 22:04:17 +010038struct sh_tmu_device;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010039
40struct sh_tmu_channel {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010041 struct sh_tmu_device *tmu;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010042
Laurent Pinchartde693462014-01-27 22:04:17 +010043 void __iomem *base;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +010044 int irq;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010045
Magnus Damm9570ef22009-05-01 06:51:00 +000046 unsigned long rate;
47 unsigned long periodic;
48 struct clock_event_device ced;
49 struct clocksource cs;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +020050 bool cs_enabled;
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +020051 unsigned int enable_count;
Magnus Damm9570ef22009-05-01 06:51:00 +000052};
53
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010054struct sh_tmu_device {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010055 struct platform_device *pdev;
56
57 void __iomem *mapbase;
58 struct clk *clk;
59
60 struct sh_tmu_channel channel;
61};
62
Paul Mundtc2225a52012-05-25 13:39:09 +090063static DEFINE_RAW_SPINLOCK(sh_tmu_lock);
Magnus Damm9570ef22009-05-01 06:51:00 +000064
65#define TSTR -1 /* shared register */
66#define TCOR 0 /* channel register */
67#define TCNT 1 /* channel register */
68#define TCR 2 /* channel register */
69
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010070static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr)
Magnus Damm9570ef22009-05-01 06:51:00 +000071{
Magnus Damm9570ef22009-05-01 06:51:00 +000072 unsigned long offs;
73
74 if (reg_nr == TSTR)
Laurent Pinchartde693462014-01-27 22:04:17 +010075 return ioread8(ch->tmu->mapbase);
Magnus Damm9570ef22009-05-01 06:51:00 +000076
77 offs = reg_nr << 2;
78
79 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +010080 return ioread16(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +000081 else
Laurent Pinchartde693462014-01-27 22:04:17 +010082 return ioread32(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +000083}
84
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010085static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr,
Magnus Damm9570ef22009-05-01 06:51:00 +000086 unsigned long value)
87{
Magnus Damm9570ef22009-05-01 06:51:00 +000088 unsigned long offs;
89
90 if (reg_nr == TSTR) {
Laurent Pinchartde693462014-01-27 22:04:17 +010091 iowrite8(value, ch->tmu->mapbase);
Magnus Damm9570ef22009-05-01 06:51:00 +000092 return;
93 }
94
95 offs = reg_nr << 2;
96
97 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +010098 iowrite16(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +000099 else
Laurent Pinchartde693462014-01-27 22:04:17 +0100100 iowrite32(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000101}
102
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100103static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start)
Magnus Damm9570ef22009-05-01 06:51:00 +0000104{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100105 struct sh_timer_config *cfg = ch->tmu->pdev->dev.platform_data;
Magnus Damm9570ef22009-05-01 06:51:00 +0000106 unsigned long flags, value;
107
108 /* start stop register shared by multiple timer channels */
Paul Mundtc2225a52012-05-25 13:39:09 +0900109 raw_spin_lock_irqsave(&sh_tmu_lock, flags);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100110 value = sh_tmu_read(ch, TSTR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000111
112 if (start)
113 value |= 1 << cfg->timer_bit;
114 else
115 value &= ~(1 << cfg->timer_bit);
116
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100117 sh_tmu_write(ch, TSTR, value);
Paul Mundtc2225a52012-05-25 13:39:09 +0900118 raw_spin_unlock_irqrestore(&sh_tmu_lock, flags);
Magnus Damm9570ef22009-05-01 06:51:00 +0000119}
120
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100121static int __sh_tmu_enable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000122{
Magnus Damm9570ef22009-05-01 06:51:00 +0000123 int ret;
124
Paul Mundtd4905ce2011-05-31 15:23:20 +0900125 /* enable clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100126 ret = clk_enable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000127 if (ret) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100128 dev_err(&ch->tmu->pdev->dev, "cannot enable clock\n");
Magnus Damm9570ef22009-05-01 06:51:00 +0000129 return ret;
130 }
131
132 /* make sure channel is disabled */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100133 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000134
135 /* maximum timeout */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100136 sh_tmu_write(ch, TCOR, 0xffffffff);
137 sh_tmu_write(ch, TCNT, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000138
139 /* configure channel to parent clock / 4, irq off */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100140 ch->rate = clk_get_rate(ch->tmu->clk) / 4;
141 sh_tmu_write(ch, TCR, 0x0000);
Magnus Damm9570ef22009-05-01 06:51:00 +0000142
143 /* enable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100144 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000145
146 return 0;
147}
148
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100149static int sh_tmu_enable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200150{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100151 if (ch->enable_count++ > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200152 return 0;
153
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100154 pm_runtime_get_sync(&ch->tmu->pdev->dev);
155 dev_pm_syscore_device(&ch->tmu->pdev->dev, true);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200156
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100157 return __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200158}
159
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100160static void __sh_tmu_disable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000161{
162 /* disable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100163 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000164
Magnus Dammbe890a12009-06-17 05:04:04 +0000165 /* disable interrupts in TMU block */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100166 sh_tmu_write(ch, TCR, 0x0000);
Magnus Dammbe890a12009-06-17 05:04:04 +0000167
Paul Mundtd4905ce2011-05-31 15:23:20 +0900168 /* stop clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100169 clk_disable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000170}
171
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100172static void sh_tmu_disable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200173{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100174 if (WARN_ON(ch->enable_count == 0))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200175 return;
176
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100177 if (--ch->enable_count > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200178 return;
179
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100180 __sh_tmu_disable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200181
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100182 dev_pm_syscore_device(&ch->tmu->pdev->dev, false);
183 pm_runtime_put(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200184}
185
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100186static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta,
Magnus Damm9570ef22009-05-01 06:51:00 +0000187 int periodic)
188{
189 /* stop timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100190 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000191
192 /* acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100193 sh_tmu_read(ch, TCR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000194
195 /* enable interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100196 sh_tmu_write(ch, TCR, 0x0020);
Magnus Damm9570ef22009-05-01 06:51:00 +0000197
198 /* reload delta value in case of periodic timer */
199 if (periodic)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100200 sh_tmu_write(ch, TCOR, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000201 else
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100202 sh_tmu_write(ch, TCOR, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000203
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100204 sh_tmu_write(ch, TCNT, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000205
206 /* start timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100207 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000208}
209
210static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id)
211{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100212 struct sh_tmu_channel *ch = dev_id;
Magnus Damm9570ef22009-05-01 06:51:00 +0000213
214 /* disable or acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100215 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT)
216 sh_tmu_write(ch, TCR, 0x0000);
Magnus Damm9570ef22009-05-01 06:51:00 +0000217 else
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100218 sh_tmu_write(ch, TCR, 0x0020);
Magnus Damm9570ef22009-05-01 06:51:00 +0000219
220 /* notify clockevent layer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100221 ch->ced.event_handler(&ch->ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000222 return IRQ_HANDLED;
223}
224
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100225static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs)
Magnus Damm9570ef22009-05-01 06:51:00 +0000226{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100227 return container_of(cs, struct sh_tmu_channel, cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000228}
229
230static cycle_t sh_tmu_clocksource_read(struct clocksource *cs)
231{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100232 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000233
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100234 return sh_tmu_read(ch, TCNT) ^ 0xffffffff;
Magnus Damm9570ef22009-05-01 06:51:00 +0000235}
236
237static int sh_tmu_clocksource_enable(struct clocksource *cs)
238{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100239 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm0aeac452011-04-25 22:38:37 +0900240 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000241
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100242 if (WARN_ON(ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200243 return 0;
244
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100245 ret = sh_tmu_enable(ch);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200246 if (!ret) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100247 __clocksource_updatefreq_hz(cs, ch->rate);
248 ch->cs_enabled = true;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200249 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200250
Magnus Damm0aeac452011-04-25 22:38:37 +0900251 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000252}
253
254static void sh_tmu_clocksource_disable(struct clocksource *cs)
255{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100256 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200257
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100258 if (WARN_ON(!ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200259 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200260
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100261 sh_tmu_disable(ch);
262 ch->cs_enabled = false;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200263}
264
265static void sh_tmu_clocksource_suspend(struct clocksource *cs)
266{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100267 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200268
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100269 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200270 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200271
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100272 if (--ch->enable_count == 0) {
273 __sh_tmu_disable(ch);
274 pm_genpd_syscore_poweroff(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200275 }
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200276}
277
278static void sh_tmu_clocksource_resume(struct clocksource *cs)
279{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100280 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200281
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100282 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200283 return;
284
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100285 if (ch->enable_count++ == 0) {
286 pm_genpd_syscore_poweron(&ch->tmu->pdev->dev);
287 __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200288 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000289}
290
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100291static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch,
Laurent Pinchart84876d02014-02-17 16:04:16 +0100292 const char *name, unsigned long rating)
Magnus Damm9570ef22009-05-01 06:51:00 +0000293{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100294 struct clocksource *cs = &ch->cs;
Magnus Damm9570ef22009-05-01 06:51:00 +0000295
296 cs->name = name;
297 cs->rating = rating;
298 cs->read = sh_tmu_clocksource_read;
299 cs->enable = sh_tmu_clocksource_enable;
300 cs->disable = sh_tmu_clocksource_disable;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200301 cs->suspend = sh_tmu_clocksource_suspend;
302 cs->resume = sh_tmu_clocksource_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000303 cs->mask = CLOCKSOURCE_MASK(32);
304 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Aurelien Jarno66f49122010-05-31 21:45:48 +0000305
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100306 dev_info(&ch->tmu->pdev->dev, "used as clock source\n");
Magnus Damm0aeac452011-04-25 22:38:37 +0900307
308 /* Register with dummy 1 Hz value, gets updated in ->enable() */
309 clocksource_register_hz(cs, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000310 return 0;
311}
312
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100313static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced)
Magnus Damm9570ef22009-05-01 06:51:00 +0000314{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100315 return container_of(ced, struct sh_tmu_channel, ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000316}
317
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100318static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic)
Magnus Damm9570ef22009-05-01 06:51:00 +0000319{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100320 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000321
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100322 sh_tmu_enable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000323
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100324 clockevents_config(ced, ch->rate);
Magnus Damm9570ef22009-05-01 06:51:00 +0000325
326 if (periodic) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100327 ch->periodic = (ch->rate + HZ/2) / HZ;
328 sh_tmu_set_next(ch, ch->periodic, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000329 }
330}
331
332static void sh_tmu_clock_event_mode(enum clock_event_mode mode,
333 struct clock_event_device *ced)
334{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100335 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000336 int disabled = 0;
337
338 /* deal with old setting first */
339 switch (ced->mode) {
340 case CLOCK_EVT_MODE_PERIODIC:
341 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100342 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000343 disabled = 1;
344 break;
345 default:
346 break;
347 }
348
349 switch (mode) {
350 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100351 dev_info(&ch->tmu->pdev->dev,
352 "used for periodic clock events\n");
353 sh_tmu_clock_event_start(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000354 break;
355 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100356 dev_info(&ch->tmu->pdev->dev,
357 "used for oneshot clock events\n");
358 sh_tmu_clock_event_start(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000359 break;
360 case CLOCK_EVT_MODE_UNUSED:
361 if (!disabled)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100362 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000363 break;
364 case CLOCK_EVT_MODE_SHUTDOWN:
365 default:
366 break;
367 }
368}
369
370static int sh_tmu_clock_event_next(unsigned long delta,
371 struct clock_event_device *ced)
372{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100373 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000374
375 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
376
377 /* program new delta value */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100378 sh_tmu_set_next(ch, delta, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000379 return 0;
380}
381
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200382static void sh_tmu_clock_event_suspend(struct clock_event_device *ced)
383{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100384 pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200385}
386
387static void sh_tmu_clock_event_resume(struct clock_event_device *ced)
388{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100389 pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200390}
391
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100392static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch,
Laurent Pinchart84876d02014-02-17 16:04:16 +0100393 const char *name, unsigned long rating)
Magnus Damm9570ef22009-05-01 06:51:00 +0000394{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100395 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000396 int ret;
397
398 memset(ced, 0, sizeof(*ced));
399
400 ced->name = name;
401 ced->features = CLOCK_EVT_FEAT_PERIODIC;
402 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
403 ced->rating = rating;
404 ced->cpumask = cpumask_of(0);
405 ced->set_next_event = sh_tmu_clock_event_next;
406 ced->set_mode = sh_tmu_clock_event_mode;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200407 ced->suspend = sh_tmu_clock_event_suspend;
408 ced->resume = sh_tmu_clock_event_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000409
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100410 dev_info(&ch->tmu->pdev->dev, "used for clock events\n");
Paul Mundt39774072012-06-11 17:10:16 +0900411
412 clockevents_config_and_register(ced, 1, 0x300, 0xffffffff);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900413
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100414 ret = request_irq(ch->irq, sh_tmu_interrupt,
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100415 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100416 dev_name(&ch->tmu->pdev->dev), ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000417 if (ret) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100418 dev_err(&ch->tmu->pdev->dev, "failed to request irq %d\n",
419 ch->irq);
Magnus Damm9570ef22009-05-01 06:51:00 +0000420 return;
421 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000422}
423
Laurent Pinchart84876d02014-02-17 16:04:16 +0100424static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name,
Magnus Damm9570ef22009-05-01 06:51:00 +0000425 unsigned long clockevent_rating,
426 unsigned long clocksource_rating)
427{
428 if (clockevent_rating)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100429 sh_tmu_register_clockevent(ch, name, clockevent_rating);
Magnus Damm9570ef22009-05-01 06:51:00 +0000430 else if (clocksource_rating)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100431 sh_tmu_register_clocksource(ch, name, clocksource_rating);
Magnus Damm9570ef22009-05-01 06:51:00 +0000432
433 return 0;
434}
435
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100436static int sh_tmu_channel_setup(struct sh_tmu_channel *ch,
437 struct sh_tmu_device *tmu)
438{
439 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
440
441 memset(ch, 0, sizeof(*ch));
442 ch->tmu = tmu;
443
444 ch->irq = platform_get_irq(tmu->pdev, 0);
445 if (ch->irq < 0) {
446 dev_err(&tmu->pdev->dev, "failed to get irq\n");
447 return ch->irq;
448 }
449
450 ch->cs_enabled = false;
451 ch->enable_count = 0;
452
Laurent Pinchart84876d02014-02-17 16:04:16 +0100453 return sh_tmu_register(ch, dev_name(&tmu->pdev->dev),
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100454 cfg->clockevent_rating,
455 cfg->clocksource_rating);
456}
457
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100458static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000459{
Paul Mundt46a12f72009-05-03 17:57:17 +0900460 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm9570ef22009-05-01 06:51:00 +0000461 struct resource *res;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100462 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000463 ret = -ENXIO;
464
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100465 memset(tmu, 0, sizeof(*tmu));
466 tmu->pdev = pdev;
Magnus Damm9570ef22009-05-01 06:51:00 +0000467
468 if (!cfg) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100469 dev_err(&tmu->pdev->dev, "missing platform data\n");
Magnus Damm9570ef22009-05-01 06:51:00 +0000470 goto err0;
471 }
472
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100473 platform_set_drvdata(pdev, tmu);
Magnus Damm9570ef22009-05-01 06:51:00 +0000474
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100475 res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000476 if (!res) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100477 dev_err(&tmu->pdev->dev, "failed to get I/O memory\n");
Magnus Damm9570ef22009-05-01 06:51:00 +0000478 goto err0;
479 }
480
Laurent Pinchartde693462014-01-27 22:04:17 +0100481 /*
482 * Map memory, let channel.base point to our channel and mapbase to the
483 * start/stop shared register.
484 */
485 tmu->channel.base = ioremap_nocache(res->start, resource_size(res));
486 if (tmu->channel.base == NULL) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100487 dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n");
Magnus Damm9570ef22009-05-01 06:51:00 +0000488 goto err0;
489 }
490
Laurent Pinchartde693462014-01-27 22:04:17 +0100491 tmu->mapbase = tmu->channel.base - cfg->channel_offset;
492
Magnus Damm9570ef22009-05-01 06:51:00 +0000493 /* get hold of clock */
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100494 tmu->clk = clk_get(&tmu->pdev->dev, "tmu_fck");
495 if (IS_ERR(tmu->clk)) {
496 dev_err(&tmu->pdev->dev, "cannot get clock\n");
497 ret = PTR_ERR(tmu->clk);
Magnus Damm03ff8582010-10-13 07:36:38 +0000498 goto err1;
Magnus Damm9570ef22009-05-01 06:51:00 +0000499 }
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100500
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100501 ret = clk_prepare(tmu->clk);
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100502 if (ret < 0)
503 goto err2;
504
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100505 ret = sh_tmu_channel_setup(&tmu->channel, tmu);
Laurent Pinchart394a4482013-11-08 11:07:59 +0100506 if (ret < 0)
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100507 goto err3;
Laurent Pinchart394a4482013-11-08 11:07:59 +0100508
509 return 0;
510
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100511 err3:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100512 clk_unprepare(tmu->clk);
Laurent Pinchart394a4482013-11-08 11:07:59 +0100513 err2:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100514 clk_put(tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000515 err1:
Laurent Pinchartde693462014-01-27 22:04:17 +0100516 iounmap(tmu->channel.base);
Magnus Damm9570ef22009-05-01 06:51:00 +0000517 err0:
518 return ret;
519}
520
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800521static int sh_tmu_probe(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000522{
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100523 struct sh_tmu_device *tmu = platform_get_drvdata(pdev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200524 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm9570ef22009-05-01 06:51:00 +0000525 int ret;
526
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200527 if (!is_early_platform_device(pdev)) {
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200528 pm_runtime_set_active(&pdev->dev);
529 pm_runtime_enable(&pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200530 }
Rafael J. Wysocki2ee619f2012-03-13 22:40:00 +0100531
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100532 if (tmu) {
Paul Mundt214a6072010-03-10 16:26:25 +0900533 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200534 goto out;
Magnus Damm9570ef22009-05-01 06:51:00 +0000535 }
536
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100537 tmu = kmalloc(sizeof(*tmu), GFP_KERNEL);
538 if (tmu == NULL) {
Magnus Damm9570ef22009-05-01 06:51:00 +0000539 dev_err(&pdev->dev, "failed to allocate driver data\n");
540 return -ENOMEM;
541 }
542
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100543 ret = sh_tmu_setup(tmu, pdev);
Magnus Damm9570ef22009-05-01 06:51:00 +0000544 if (ret) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100545 kfree(tmu);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200546 pm_runtime_idle(&pdev->dev);
547 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000548 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200549 if (is_early_platform_device(pdev))
550 return 0;
551
552 out:
553 if (cfg->clockevent_rating || cfg->clocksource_rating)
554 pm_runtime_irq_safe(&pdev->dev);
555 else
556 pm_runtime_idle(&pdev->dev);
557
558 return 0;
Magnus Damm9570ef22009-05-01 06:51:00 +0000559}
560
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800561static int sh_tmu_remove(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000562{
563 return -EBUSY; /* cannot unregister clockevent and clocksource */
564}
565
566static struct platform_driver sh_tmu_device_driver = {
567 .probe = sh_tmu_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800568 .remove = sh_tmu_remove,
Magnus Damm9570ef22009-05-01 06:51:00 +0000569 .driver = {
570 .name = "sh_tmu",
571 }
572};
573
574static int __init sh_tmu_init(void)
575{
576 return platform_driver_register(&sh_tmu_device_driver);
577}
578
579static void __exit sh_tmu_exit(void)
580{
581 platform_driver_unregister(&sh_tmu_device_driver);
582}
583
584early_platform_init("earlytimer", &sh_tmu_device_driver);
Simon Hormanb9773c32013-03-05 15:40:42 +0900585subsys_initcall(sh_tmu_init);
Magnus Damm9570ef22009-05-01 06:51:00 +0000586module_exit(sh_tmu_exit);
587
588MODULE_AUTHOR("Magnus Damm");
589MODULE_DESCRIPTION("SuperH TMU Timer Driver");
590MODULE_LICENSE("GPL v2");