blob: ec340955e852e7f7af97434314bc231d7aae808c [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.
Magnus Damm9570ef22009-05-01 06:51:00 +000014 */
15
Magnus Damm9570ef22009-05-01 06:51:00 +000016#include <linux/clk.h>
Magnus Damm9570ef22009-05-01 06:51:00 +000017#include <linux/clockchips.h>
Laurent Pinchart13931f82014-02-12 16:56:44 +010018#include <linux/clocksource.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/ioport.h>
25#include <linux/irq.h>
Paul Gortmaker7deeab52011-07-03 13:36:22 -040026#include <linux/module.h>
Laurent Pinchart13931f82014-02-12 16:56:44 +010027#include <linux/platform_device.h>
Rafael J. Wysocki2ee619f2012-03-13 22:40:00 +010028#include <linux/pm_domain.h>
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +020029#include <linux/pm_runtime.h>
Laurent Pinchart13931f82014-02-12 16:56:44 +010030#include <linux/sh_timer.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
Magnus Damm9570ef22009-05-01 06:51:00 +000033
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010034enum sh_tmu_model {
35 SH_TMU_LEGACY,
36 SH_TMU,
37 SH_TMU_SH3,
38};
39
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010040struct sh_tmu_device;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010041
42struct sh_tmu_channel {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010043 struct sh_tmu_device *tmu;
Laurent Pinchartfe68eb82014-01-27 22:04:17 +010044 unsigned int index;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010045
Laurent Pinchartde693462014-01-27 22:04:17 +010046 void __iomem *base;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +010047 int irq;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010048
Magnus Damm9570ef22009-05-01 06:51:00 +000049 unsigned long rate;
50 unsigned long periodic;
51 struct clock_event_device ced;
52 struct clocksource cs;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +020053 bool cs_enabled;
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +020054 unsigned int enable_count;
Magnus Damm9570ef22009-05-01 06:51:00 +000055};
56
Laurent Pinchart0a72aa32014-01-27 22:04:17 +010057struct sh_tmu_device {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010058 struct platform_device *pdev;
59
60 void __iomem *mapbase;
61 struct clk *clk;
62
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010063 enum sh_tmu_model model;
64
Laurent Pincharta5de49f2014-01-27 22:04:17 +010065 struct sh_tmu_channel *channels;
66 unsigned int num_channels;
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010067
68 bool has_clockevent;
69 bool has_clocksource;
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010070};
71
Paul Mundtc2225a52012-05-25 13:39:09 +090072static DEFINE_RAW_SPINLOCK(sh_tmu_lock);
Magnus Damm9570ef22009-05-01 06:51:00 +000073
74#define TSTR -1 /* shared register */
75#define TCOR 0 /* channel register */
76#define TCNT 1 /* channel register */
77#define TCR 2 /* channel register */
78
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +010079#define TCR_UNF (1 << 8)
80#define TCR_UNIE (1 << 5)
81#define TCR_TPSC_CLK4 (0 << 0)
82#define TCR_TPSC_CLK16 (1 << 0)
83#define TCR_TPSC_CLK64 (2 << 0)
84#define TCR_TPSC_CLK256 (3 << 0)
85#define TCR_TPSC_CLK1024 (4 << 0)
86#define TCR_TPSC_MASK (7 << 0)
87
Laurent Pinchartde2d12c2014-01-27 15:29:19 +010088static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr)
Magnus Damm9570ef22009-05-01 06:51:00 +000089{
Magnus Damm9570ef22009-05-01 06:51:00 +000090 unsigned long offs;
91
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +010092 if (reg_nr == TSTR) {
93 switch (ch->tmu->model) {
94 case SH_TMU_LEGACY:
95 return ioread8(ch->tmu->mapbase);
96 case SH_TMU_SH3:
97 return ioread8(ch->tmu->mapbase + 2);
98 case SH_TMU:
99 return ioread8(ch->tmu->mapbase + 4);
100 }
101 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000102
103 offs = reg_nr << 2;
104
105 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +0100106 return ioread16(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000107 else
Laurent Pinchartde693462014-01-27 22:04:17 +0100108 return ioread32(ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000109}
110
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100111static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr,
Magnus Damm9570ef22009-05-01 06:51:00 +0000112 unsigned long value)
113{
Magnus Damm9570ef22009-05-01 06:51:00 +0000114 unsigned long offs;
115
116 if (reg_nr == TSTR) {
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100117 switch (ch->tmu->model) {
118 case SH_TMU_LEGACY:
119 return iowrite8(value, ch->tmu->mapbase);
120 case SH_TMU_SH3:
121 return iowrite8(value, ch->tmu->mapbase + 2);
122 case SH_TMU:
123 return iowrite8(value, ch->tmu->mapbase + 4);
124 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000125 }
126
127 offs = reg_nr << 2;
128
129 if (reg_nr == TCR)
Laurent Pinchartde693462014-01-27 22:04:17 +0100130 iowrite16(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000131 else
Laurent Pinchartde693462014-01-27 22:04:17 +0100132 iowrite32(value, ch->base + offs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000133}
134
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100135static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start)
Magnus Damm9570ef22009-05-01 06:51:00 +0000136{
Magnus Damm9570ef22009-05-01 06:51:00 +0000137 unsigned long flags, value;
138
139 /* start stop register shared by multiple timer channels */
Paul Mundtc2225a52012-05-25 13:39:09 +0900140 raw_spin_lock_irqsave(&sh_tmu_lock, flags);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100141 value = sh_tmu_read(ch, TSTR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000142
143 if (start)
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100144 value |= 1 << ch->index;
Magnus Damm9570ef22009-05-01 06:51:00 +0000145 else
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100146 value &= ~(1 << ch->index);
Magnus Damm9570ef22009-05-01 06:51:00 +0000147
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100148 sh_tmu_write(ch, TSTR, value);
Paul Mundtc2225a52012-05-25 13:39:09 +0900149 raw_spin_unlock_irqrestore(&sh_tmu_lock, flags);
Magnus Damm9570ef22009-05-01 06:51:00 +0000150}
151
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100152static int __sh_tmu_enable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000153{
Magnus Damm9570ef22009-05-01 06:51:00 +0000154 int ret;
155
Paul Mundtd4905ce2011-05-31 15:23:20 +0900156 /* enable clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100157 ret = clk_enable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000158 if (ret) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100159 dev_err(&ch->tmu->pdev->dev, "ch%u: cannot enable clock\n",
160 ch->index);
Magnus Damm9570ef22009-05-01 06:51:00 +0000161 return ret;
162 }
163
164 /* make sure channel is disabled */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100165 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000166
167 /* maximum timeout */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100168 sh_tmu_write(ch, TCOR, 0xffffffff);
169 sh_tmu_write(ch, TCNT, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000170
171 /* configure channel to parent clock / 4, irq off */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100172 ch->rate = clk_get_rate(ch->tmu->clk) / 4;
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100173 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000174
175 /* enable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100176 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000177
178 return 0;
179}
180
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100181static int sh_tmu_enable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200182{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100183 if (ch->enable_count++ > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200184 return 0;
185
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100186 pm_runtime_get_sync(&ch->tmu->pdev->dev);
187 dev_pm_syscore_device(&ch->tmu->pdev->dev, true);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200188
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100189 return __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200190}
191
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100192static void __sh_tmu_disable(struct sh_tmu_channel *ch)
Magnus Damm9570ef22009-05-01 06:51:00 +0000193{
194 /* disable channel */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100195 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000196
Magnus Dammbe890a12009-06-17 05:04:04 +0000197 /* disable interrupts in TMU block */
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100198 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Dammbe890a12009-06-17 05:04:04 +0000199
Paul Mundtd4905ce2011-05-31 15:23:20 +0900200 /* stop clock */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100201 clk_disable(ch->tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000202}
203
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100204static void sh_tmu_disable(struct sh_tmu_channel *ch)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200205{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100206 if (WARN_ON(ch->enable_count == 0))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200207 return;
208
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100209 if (--ch->enable_count > 0)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200210 return;
211
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100212 __sh_tmu_disable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200213
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100214 dev_pm_syscore_device(&ch->tmu->pdev->dev, false);
215 pm_runtime_put(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200216}
217
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100218static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta,
Magnus Damm9570ef22009-05-01 06:51:00 +0000219 int periodic)
220{
221 /* stop timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100222 sh_tmu_start_stop_ch(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000223
224 /* acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100225 sh_tmu_read(ch, TCR);
Magnus Damm9570ef22009-05-01 06:51:00 +0000226
227 /* enable interrupt */
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100228 sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000229
230 /* reload delta value in case of periodic timer */
231 if (periodic)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100232 sh_tmu_write(ch, TCOR, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000233 else
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100234 sh_tmu_write(ch, TCOR, 0xffffffff);
Magnus Damm9570ef22009-05-01 06:51:00 +0000235
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100236 sh_tmu_write(ch, TCNT, delta);
Magnus Damm9570ef22009-05-01 06:51:00 +0000237
238 /* start timer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100239 sh_tmu_start_stop_ch(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000240}
241
242static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id)
243{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100244 struct sh_tmu_channel *ch = dev_id;
Magnus Damm9570ef22009-05-01 06:51:00 +0000245
246 /* disable or acknowledge interrupt */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100247 if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT)
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100248 sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000249 else
Laurent Pinchart5cfe2d12014-01-29 00:33:08 +0100250 sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
Magnus Damm9570ef22009-05-01 06:51:00 +0000251
252 /* notify clockevent layer */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100253 ch->ced.event_handler(&ch->ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000254 return IRQ_HANDLED;
255}
256
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100257static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs)
Magnus Damm9570ef22009-05-01 06:51:00 +0000258{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100259 return container_of(cs, struct sh_tmu_channel, cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000260}
261
262static cycle_t sh_tmu_clocksource_read(struct clocksource *cs)
263{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100264 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm9570ef22009-05-01 06:51:00 +0000265
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100266 return sh_tmu_read(ch, TCNT) ^ 0xffffffff;
Magnus Damm9570ef22009-05-01 06:51:00 +0000267}
268
269static int sh_tmu_clocksource_enable(struct clocksource *cs)
270{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100271 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Magnus Damm0aeac452011-04-25 22:38:37 +0900272 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000273
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100274 if (WARN_ON(ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200275 return 0;
276
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100277 ret = sh_tmu_enable(ch);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200278 if (!ret) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100279 __clocksource_updatefreq_hz(cs, ch->rate);
280 ch->cs_enabled = true;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200281 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200282
Magnus Damm0aeac452011-04-25 22:38:37 +0900283 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000284}
285
286static void sh_tmu_clocksource_disable(struct clocksource *cs)
287{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100288 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200289
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100290 if (WARN_ON(!ch->cs_enabled))
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200291 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200292
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100293 sh_tmu_disable(ch);
294 ch->cs_enabled = false;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200295}
296
297static void sh_tmu_clocksource_suspend(struct clocksource *cs)
298{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100299 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200300
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100301 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200302 return;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200303
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100304 if (--ch->enable_count == 0) {
305 __sh_tmu_disable(ch);
306 pm_genpd_syscore_poweroff(&ch->tmu->pdev->dev);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200307 }
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200308}
309
310static void sh_tmu_clocksource_resume(struct clocksource *cs)
311{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100312 struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200313
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100314 if (!ch->cs_enabled)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200315 return;
316
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100317 if (ch->enable_count++ == 0) {
318 pm_genpd_syscore_poweron(&ch->tmu->pdev->dev);
319 __sh_tmu_enable(ch);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200320 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000321}
322
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100323static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100324 const char *name)
Magnus Damm9570ef22009-05-01 06:51:00 +0000325{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100326 struct clocksource *cs = &ch->cs;
Magnus Damm9570ef22009-05-01 06:51:00 +0000327
328 cs->name = name;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100329 cs->rating = 200;
Magnus Damm9570ef22009-05-01 06:51:00 +0000330 cs->read = sh_tmu_clocksource_read;
331 cs->enable = sh_tmu_clocksource_enable;
332 cs->disable = sh_tmu_clocksource_disable;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200333 cs->suspend = sh_tmu_clocksource_suspend;
334 cs->resume = sh_tmu_clocksource_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000335 cs->mask = CLOCKSOURCE_MASK(32);
336 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Aurelien Jarno66f49122010-05-31 21:45:48 +0000337
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100338 dev_info(&ch->tmu->pdev->dev, "ch%u: used as clock source\n",
339 ch->index);
Magnus Damm0aeac452011-04-25 22:38:37 +0900340
341 /* Register with dummy 1 Hz value, gets updated in ->enable() */
342 clocksource_register_hz(cs, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000343 return 0;
344}
345
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100346static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced)
Magnus Damm9570ef22009-05-01 06:51:00 +0000347{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100348 return container_of(ced, struct sh_tmu_channel, ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000349}
350
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100351static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic)
Magnus Damm9570ef22009-05-01 06:51:00 +0000352{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100353 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000354
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100355 sh_tmu_enable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000356
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100357 clockevents_config(ced, ch->rate);
Magnus Damm9570ef22009-05-01 06:51:00 +0000358
359 if (periodic) {
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100360 ch->periodic = (ch->rate + HZ/2) / HZ;
361 sh_tmu_set_next(ch, ch->periodic, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000362 }
363}
364
365static void sh_tmu_clock_event_mode(enum clock_event_mode mode,
366 struct clock_event_device *ced)
367{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100368 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000369 int disabled = 0;
370
371 /* deal with old setting first */
372 switch (ced->mode) {
373 case CLOCK_EVT_MODE_PERIODIC:
374 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100375 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000376 disabled = 1;
377 break;
378 default:
379 break;
380 }
381
382 switch (mode) {
383 case CLOCK_EVT_MODE_PERIODIC:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100384 dev_info(&ch->tmu->pdev->dev,
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100385 "ch%u: used for periodic clock events\n", ch->index);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100386 sh_tmu_clock_event_start(ch, 1);
Magnus Damm9570ef22009-05-01 06:51:00 +0000387 break;
388 case CLOCK_EVT_MODE_ONESHOT:
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100389 dev_info(&ch->tmu->pdev->dev,
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100390 "ch%u: used for oneshot clock events\n", ch->index);
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100391 sh_tmu_clock_event_start(ch, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000392 break;
393 case CLOCK_EVT_MODE_UNUSED:
394 if (!disabled)
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100395 sh_tmu_disable(ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000396 break;
397 case CLOCK_EVT_MODE_SHUTDOWN:
398 default:
399 break;
400 }
401}
402
403static int sh_tmu_clock_event_next(unsigned long delta,
404 struct clock_event_device *ced)
405{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100406 struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
Magnus Damm9570ef22009-05-01 06:51:00 +0000407
408 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
409
410 /* program new delta value */
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100411 sh_tmu_set_next(ch, delta, 0);
Magnus Damm9570ef22009-05-01 06:51:00 +0000412 return 0;
413}
414
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200415static void sh_tmu_clock_event_suspend(struct clock_event_device *ced)
416{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100417 pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200418}
419
420static void sh_tmu_clock_event_resume(struct clock_event_device *ced)
421{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100422 pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200423}
424
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100425static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100426 const char *name)
Magnus Damm9570ef22009-05-01 06:51:00 +0000427{
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100428 struct clock_event_device *ced = &ch->ced;
Magnus Damm9570ef22009-05-01 06:51:00 +0000429 int ret;
430
Magnus Damm9570ef22009-05-01 06:51:00 +0000431 ced->name = name;
432 ced->features = CLOCK_EVT_FEAT_PERIODIC;
433 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100434 ced->rating = 200;
Magnus Damm9570ef22009-05-01 06:51:00 +0000435 ced->cpumask = cpumask_of(0);
436 ced->set_next_event = sh_tmu_clock_event_next;
437 ced->set_mode = sh_tmu_clock_event_mode;
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200438 ced->suspend = sh_tmu_clock_event_suspend;
439 ced->resume = sh_tmu_clock_event_resume;
Magnus Damm9570ef22009-05-01 06:51:00 +0000440
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100441 dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n",
442 ch->index);
Paul Mundt39774072012-06-11 17:10:16 +0900443
444 clockevents_config_and_register(ced, 1, 0x300, 0xffffffff);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900445
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100446 ret = request_irq(ch->irq, sh_tmu_interrupt,
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100447 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
Laurent Pinchartde2d12c2014-01-27 15:29:19 +0100448 dev_name(&ch->tmu->pdev->dev), ch);
Magnus Damm9570ef22009-05-01 06:51:00 +0000449 if (ret) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100450 dev_err(&ch->tmu->pdev->dev, "ch%u: failed to request irq %d\n",
451 ch->index, ch->irq);
Magnus Damm9570ef22009-05-01 06:51:00 +0000452 return;
453 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000454}
455
Laurent Pinchart84876d02014-02-17 16:04:16 +0100456static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name,
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100457 bool clockevent, bool clocksource)
Magnus Damm9570ef22009-05-01 06:51:00 +0000458{
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100459 if (clockevent) {
460 ch->tmu->has_clockevent = true;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100461 sh_tmu_register_clockevent(ch, name);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100462 } else if (clocksource) {
463 ch->tmu->has_clocksource = true;
Laurent Pinchartf1010ed2014-02-19 17:00:31 +0100464 sh_tmu_register_clocksource(ch, name);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100465 }
Magnus Damm9570ef22009-05-01 06:51:00 +0000466
467 return 0;
468}
469
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100470static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index,
471 bool clockevent, bool clocksource,
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100472 struct sh_tmu_device *tmu)
473{
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100474 /* Skip unused channels. */
475 if (!clockevent && !clocksource)
476 return 0;
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100477
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100478 ch->tmu = tmu;
479
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100480 if (tmu->model == SH_TMU_LEGACY) {
481 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100482
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100483 /*
484 * The SH3 variant (SH770x, SH7705, SH7710 and SH7720) maps
485 * channel registers blocks at base + 2 + 12 * index, while all
486 * other variants map them at base + 4 + 12 * index. We can
487 * compute the index by just dividing by 12, the 2 bytes or 4
488 * bytes offset being hidden by the integer division.
489 */
490 ch->index = cfg->channel_offset / 12;
491 ch->base = tmu->mapbase + cfg->channel_offset;
492 } else {
493 ch->index = index;
494
495 if (tmu->model == SH_TMU_SH3)
496 ch->base = tmu->mapbase + 4 + ch->index * 12;
497 else
498 ch->base = tmu->mapbase + 8 + ch->index * 12;
499 }
500
Laurent Pinchartc54697a2014-05-16 14:44:23 +0200501 ch->irq = platform_get_irq(tmu->pdev, index);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100502 if (ch->irq < 0) {
Laurent Pinchartfe68eb82014-01-27 22:04:17 +0100503 dev_err(&tmu->pdev->dev, "ch%u: failed to get irq\n",
504 ch->index);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100505 return ch->irq;
506 }
507
508 ch->cs_enabled = false;
509 ch->enable_count = 0;
510
Laurent Pinchart84876d02014-02-17 16:04:16 +0100511 return sh_tmu_register(ch, dev_name(&tmu->pdev->dev),
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100512 clockevent, clocksource);
513}
514
515static int sh_tmu_map_memory(struct sh_tmu_device *tmu)
516{
517 struct resource *res;
518
519 res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0);
520 if (!res) {
521 dev_err(&tmu->pdev->dev, "failed to get I/O memory\n");
522 return -ENXIO;
523 }
524
525 tmu->mapbase = ioremap_nocache(res->start, resource_size(res));
526 if (tmu->mapbase == NULL)
527 return -ENXIO;
528
529 /*
530 * In legacy platform device configuration (with one device per channel)
531 * the resource points to the channel base address.
532 */
533 if (tmu->model == SH_TMU_LEGACY) {
534 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
535 tmu->mapbase -= cfg->channel_offset;
536 }
537
538 return 0;
539}
540
541static void sh_tmu_unmap_memory(struct sh_tmu_device *tmu)
542{
543 if (tmu->model == SH_TMU_LEGACY) {
544 struct sh_timer_config *cfg = tmu->pdev->dev.platform_data;
545 tmu->mapbase += cfg->channel_offset;
546 }
547
548 iounmap(tmu->mapbase);
Laurent Pincharta94ddaa2014-01-27 22:04:17 +0100549}
550
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100551static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000552{
Paul Mundt46a12f72009-05-03 17:57:17 +0900553 struct sh_timer_config *cfg = pdev->dev.platform_data;
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100554 const struct platform_device_id *id = pdev->id_entry;
555 unsigned int i;
Laurent Pinchart1c56cf62014-02-17 11:27:49 +0100556 int ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000557
558 if (!cfg) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100559 dev_err(&tmu->pdev->dev, "missing platform data\n");
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100560 return -ENXIO;
Magnus Damm9570ef22009-05-01 06:51:00 +0000561 }
562
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100563 tmu->pdev = pdev;
564 tmu->model = id->driver_data;
Magnus Damm9570ef22009-05-01 06:51:00 +0000565
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100566 /* Get hold of clock. */
Laurent Pincharta27d9222014-02-14 00:35:18 +0100567 tmu->clk = clk_get(&tmu->pdev->dev,
568 tmu->model == SH_TMU_LEGACY ? "tmu_fck" : "fck");
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100569 if (IS_ERR(tmu->clk)) {
570 dev_err(&tmu->pdev->dev, "cannot get clock\n");
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100571 return PTR_ERR(tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000572 }
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100573
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100574 ret = clk_prepare(tmu->clk);
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100575 if (ret < 0)
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100576 goto err_clk_put;
Laurent Pinchart1c09eb32013-11-08 11:08:00 +0100577
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100578 /* Map the memory resource. */
579 ret = sh_tmu_map_memory(tmu);
580 if (ret < 0) {
581 dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n");
582 goto err_clk_unprepare;
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100583 }
584
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100585 /* Allocate and setup the channels. */
586 if (tmu->model == SH_TMU_LEGACY)
587 tmu->num_channels = 1;
588 else
589 tmu->num_channels = hweight8(cfg->channels_mask);
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100590
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100591 tmu->channels = kzalloc(sizeof(*tmu->channels) * tmu->num_channels,
592 GFP_KERNEL);
593 if (tmu->channels == NULL) {
594 ret = -ENOMEM;
595 goto err_unmap;
596 }
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100597
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100598 if (tmu->model == SH_TMU_LEGACY) {
599 ret = sh_tmu_channel_setup(&tmu->channels[0], 0,
600 cfg->clockevent_rating != 0,
601 cfg->clocksource_rating != 0, tmu);
602 if (ret < 0)
603 goto err_unmap;
604 } else {
605 /*
606 * Use the first channel as a clock event device and the second
607 * channel as a clock source.
608 */
609 for (i = 0; i < tmu->num_channels; ++i) {
610 ret = sh_tmu_channel_setup(&tmu->channels[i], i,
611 i == 0, i == 1, tmu);
612 if (ret < 0)
613 goto err_unmap;
614 }
615 }
616
617 platform_set_drvdata(pdev, tmu);
Laurent Pinchart394a4482013-11-08 11:07:59 +0100618
619 return 0;
620
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100621err_unmap:
Laurent Pincharta5de49f2014-01-27 22:04:17 +0100622 kfree(tmu->channels);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100623 sh_tmu_unmap_memory(tmu);
624err_clk_unprepare:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100625 clk_unprepare(tmu->clk);
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100626err_clk_put:
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100627 clk_put(tmu->clk);
Magnus Damm9570ef22009-05-01 06:51:00 +0000628 return ret;
629}
630
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800631static int sh_tmu_probe(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000632{
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100633 struct sh_tmu_device *tmu = platform_get_drvdata(pdev);
Magnus Damm9570ef22009-05-01 06:51:00 +0000634 int ret;
635
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200636 if (!is_early_platform_device(pdev)) {
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200637 pm_runtime_set_active(&pdev->dev);
638 pm_runtime_enable(&pdev->dev);
Rafael J. Wysockieaa49a82012-08-06 01:41:20 +0200639 }
Rafael J. Wysocki2ee619f2012-03-13 22:40:00 +0100640
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100641 if (tmu) {
Paul Mundt214a6072010-03-10 16:26:25 +0900642 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200643 goto out;
Magnus Damm9570ef22009-05-01 06:51:00 +0000644 }
645
Laurent Pinchart3b77a832014-01-27 22:04:17 +0100646 tmu = kzalloc(sizeof(*tmu), GFP_KERNEL);
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100647 if (tmu == NULL) {
Magnus Damm9570ef22009-05-01 06:51:00 +0000648 dev_err(&pdev->dev, "failed to allocate driver data\n");
649 return -ENOMEM;
650 }
651
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100652 ret = sh_tmu_setup(tmu, pdev);
Magnus Damm9570ef22009-05-01 06:51:00 +0000653 if (ret) {
Laurent Pinchart0a72aa32014-01-27 22:04:17 +0100654 kfree(tmu);
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200655 pm_runtime_idle(&pdev->dev);
656 return ret;
Magnus Damm9570ef22009-05-01 06:51:00 +0000657 }
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200658 if (is_early_platform_device(pdev))
659 return 0;
660
661 out:
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100662 if (tmu->has_clockevent || tmu->has_clocksource)
Rafael J. Wysocki61a53bf2012-08-06 01:48:17 +0200663 pm_runtime_irq_safe(&pdev->dev);
664 else
665 pm_runtime_idle(&pdev->dev);
666
667 return 0;
Magnus Damm9570ef22009-05-01 06:51:00 +0000668}
669
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800670static int sh_tmu_remove(struct platform_device *pdev)
Magnus Damm9570ef22009-05-01 06:51:00 +0000671{
672 return -EBUSY; /* cannot unregister clockevent and clocksource */
673}
674
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100675static const struct platform_device_id sh_tmu_id_table[] = {
676 { "sh_tmu", SH_TMU_LEGACY },
677 { "sh-tmu", SH_TMU },
678 { "sh-tmu-sh3", SH_TMU_SH3 },
679 { }
680};
681MODULE_DEVICE_TABLE(platform, sh_tmu_id_table);
682
Magnus Damm9570ef22009-05-01 06:51:00 +0000683static struct platform_driver sh_tmu_device_driver = {
684 .probe = sh_tmu_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800685 .remove = sh_tmu_remove,
Magnus Damm9570ef22009-05-01 06:51:00 +0000686 .driver = {
687 .name = "sh_tmu",
Laurent Pinchart8c7f21e2014-01-28 12:36:48 +0100688 },
689 .id_table = sh_tmu_id_table,
Magnus Damm9570ef22009-05-01 06:51:00 +0000690};
691
692static int __init sh_tmu_init(void)
693{
694 return platform_driver_register(&sh_tmu_device_driver);
695}
696
697static void __exit sh_tmu_exit(void)
698{
699 platform_driver_unregister(&sh_tmu_device_driver);
700}
701
702early_platform_init("earlytimer", &sh_tmu_device_driver);
Simon Hormanb9773c32013-03-05 15:40:42 +0900703subsys_initcall(sh_tmu_init);
Magnus Damm9570ef22009-05-01 06:51:00 +0000704module_exit(sh_tmu_exit);
705
706MODULE_AUTHOR("Magnus Damm");
707MODULE_DESCRIPTION("SuperH TMU Timer Driver");
708MODULE_LICENSE("GPL v2");