sh_cmt.c 23.8 KB
Newer Older
M
Magnus Damm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * SuperH Timer Support - CMT
 *
 *  Copyright (C) 2008 Magnus Damm
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/irq.h>
#include <linux/err.h>
29
#include <linux/delay.h>
M
Magnus Damm 已提交
30 31
#include <linux/clocksource.h>
#include <linux/clockchips.h>
32
#include <linux/sh_timer.h>
33
#include <linux/slab.h>
34
#include <linux/module.h>
35
#include <linux/pm_domain.h>
36
#include <linux/pm_runtime.h>
M
Magnus Damm 已提交
37

38
struct sh_cmt_device;
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * The CMT comes in 5 different identified flavours, depending not only on the
 * SoC but also on the particular instance. The following table lists the main
 * characteristics of those flavours.
 *
 *			16B	32B	32B-F	48B	48B-2
 * -----------------------------------------------------------------------------
 * Channels		2	1/4	1	6	2/8
 * Control Width	16	16	16	16	32
 * Counter Width	16	32	32	32/48	32/48
 * Shared Start/Stop	Y	Y	Y	Y	N
 *
 * The 48-bit gen2 version has a per-channel start/stop register located in the
 * channel registers block. All other versions have a shared start/stop register
 * located in the global space.
 *
 * Note that CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
 * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
 */

enum sh_cmt_model {
	SH_CMT_16BIT,
	SH_CMT_32BIT,
	SH_CMT_32BIT_FAST,
	SH_CMT_48BIT,
	SH_CMT_48BIT_GEN2,
};

struct sh_cmt_info {
	enum sh_cmt_model model;

	unsigned long width; /* 16 or 32 bit version of hardware block */
	unsigned long overflow_bit;
	unsigned long clear_bits;

	/* callbacks for CMSTR and CMCSR access */
	unsigned long (*read_control)(void __iomem *base, unsigned long offs);
	void (*write_control)(void __iomem *base, unsigned long offs,
			      unsigned long value);

	/* callbacks for CMCNT and CMCOR access */
	unsigned long (*read_count)(void __iomem *base, unsigned long offs);
	void (*write_count)(void __iomem *base, unsigned long offs,
			    unsigned long value);
};

86
struct sh_cmt_channel {
87
	struct sh_cmt_device *cmt;
88
	unsigned int index;
M
Magnus Damm 已提交
89

90 91
	void __iomem *base;

M
Magnus Damm 已提交
92 93 94 95 96
	unsigned long flags;
	unsigned long match_value;
	unsigned long next_match_value;
	unsigned long max_match_value;
	unsigned long rate;
97
	raw_spinlock_t lock;
M
Magnus Damm 已提交
98
	struct clock_event_device ced;
99
	struct clocksource cs;
M
Magnus Damm 已提交
100
	unsigned long total_cycles;
101
	bool cs_enabled;
102 103
};

104
struct sh_cmt_device {
105 106
	struct platform_device *pdev;

107 108
	const struct sh_cmt_info *info;

109
	void __iomem *mapbase_ch;
110 111 112
	void __iomem *mapbase;
	struct clk *clk;

113 114
	struct sh_cmt_channel *channels;
	unsigned int num_channels;
M
Magnus Damm 已提交
115 116
};

117
static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
118 119 120 121
{
	return ioread16(base + (offs << 1));
}

122 123 124 125 126 127 128
static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
{
	return ioread32(base + (offs << 2));
}

static void sh_cmt_write16(void __iomem *base, unsigned long offs,
			   unsigned long value)
129 130 131
{
	iowrite16(value, base + (offs << 1));
}
M
Magnus Damm 已提交
132

133 134 135 136 137 138
static void sh_cmt_write32(void __iomem *base, unsigned long offs,
			   unsigned long value)
{
	iowrite32(value, base + (offs << 2));
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static const struct sh_cmt_info sh_cmt_info[] = {
	[SH_CMT_16BIT] = {
		.model = SH_CMT_16BIT,
		.width = 16,
		.overflow_bit = 0x80,
		.clear_bits = ~0x80,
		.read_control = sh_cmt_read16,
		.write_control = sh_cmt_write16,
		.read_count = sh_cmt_read16,
		.write_count = sh_cmt_write16,
	},
	[SH_CMT_32BIT] = {
		.model = SH_CMT_32BIT,
		.width = 32,
		.overflow_bit = 0x8000,
		.clear_bits = ~0xc000,
		.read_control = sh_cmt_read16,
		.write_control = sh_cmt_write16,
		.read_count = sh_cmt_read32,
		.write_count = sh_cmt_write32,
	},
	[SH_CMT_32BIT_FAST] = {
		.model = SH_CMT_32BIT_FAST,
		.width = 32,
		.overflow_bit = 0x8000,
		.clear_bits = ~0xc000,
		.read_control = sh_cmt_read16,
		.write_control = sh_cmt_write16,
		.read_count = sh_cmt_read32,
		.write_count = sh_cmt_write32,
	},
	[SH_CMT_48BIT] = {
		.model = SH_CMT_48BIT,
		.width = 32,
		.overflow_bit = 0x8000,
		.clear_bits = ~0xc000,
		.read_control = sh_cmt_read32,
		.write_control = sh_cmt_write32,
		.read_count = sh_cmt_read32,
		.write_count = sh_cmt_write32,
	},
	[SH_CMT_48BIT_GEN2] = {
		.model = SH_CMT_48BIT_GEN2,
		.width = 32,
		.overflow_bit = 0x8000,
		.clear_bits = ~0xc000,
		.read_control = sh_cmt_read32,
		.write_control = sh_cmt_write32,
		.read_count = sh_cmt_read32,
		.write_count = sh_cmt_write32,
	},
};

M
Magnus Damm 已提交
192 193 194 195
#define CMCSR 0 /* channel register */
#define CMCNT 1 /* channel register */
#define CMCOR 2 /* channel register */

196
static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
197
{
198
	return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
199 200
}

201
static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
202
{
203
	return ch->cmt->info->read_control(ch->base, CMCSR);
204 205
}

206
static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
207
{
208
	return ch->cmt->info->read_count(ch->base, CMCNT);
M
Magnus Damm 已提交
209 210
}

211
static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
212 213
				      unsigned long value)
{
214
	ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
215 216
}

217
static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
218 219
				      unsigned long value)
{
220
	ch->cmt->info->write_control(ch->base, CMCSR, value);
221 222
}

223
static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
224 225
				      unsigned long value)
{
226
	ch->cmt->info->write_count(ch->base, CMCNT, value);
227 228
}

229
static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
230 231
				      unsigned long value)
{
232
	ch->cmt->info->write_count(ch->base, CMCOR, value);
233 234
}

235
static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
236 237 238
					int *has_wrapped)
{
	unsigned long v1, v2, v3;
239 240
	int o1, o2;

241
	o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
M
Magnus Damm 已提交
242 243 244

	/* Make sure the timer value is stable. Stolen from acpi_pm.c */
	do {
245
		o2 = o1;
246 247 248
		v1 = sh_cmt_read_cmcnt(ch);
		v2 = sh_cmt_read_cmcnt(ch);
		v3 = sh_cmt_read_cmcnt(ch);
249
		o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
250 251
	} while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
			  || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
M
Magnus Damm 已提交
252

253
	*has_wrapped = o1;
M
Magnus Damm 已提交
254 255 256
	return v2;
}

257
static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
M
Magnus Damm 已提交
258

259
static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
M
Magnus Damm 已提交
260
{
261
	struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
M
Magnus Damm 已提交
262 263 264
	unsigned long flags, value;

	/* start stop register shared by multiple timer channels */
265
	raw_spin_lock_irqsave(&sh_cmt_lock, flags);
266
	value = sh_cmt_read_cmstr(ch);
M
Magnus Damm 已提交
267 268 269 270 271 272

	if (start)
		value |= 1 << cfg->timer_bit;
	else
		value &= ~(1 << cfg->timer_bit);

273
	sh_cmt_write_cmstr(ch, value);
274
	raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
M
Magnus Damm 已提交
275 276
}

277
static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
M
Magnus Damm 已提交
278
{
279
	int k, ret;
M
Magnus Damm 已提交
280

281 282
	pm_runtime_get_sync(&ch->cmt->pdev->dev);
	dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
283

284
	/* enable clock */
285
	ret = clk_enable(ch->cmt->clk);
M
Magnus Damm 已提交
286
	if (ret) {
287 288
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
			ch->index);
289
		goto err0;
M
Magnus Damm 已提交
290 291 292
	}

	/* make sure channel is disabled */
293
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
294 295

	/* configure channel, periodic mode and maximum timeout */
296
	if (ch->cmt->info->width == 16) {
297 298
		*rate = clk_get_rate(ch->cmt->clk) / 512;
		sh_cmt_write_cmcsr(ch, 0x43);
M
Magnus Damm 已提交
299
	} else {
300 301
		*rate = clk_get_rate(ch->cmt->clk) / 8;
		sh_cmt_write_cmcsr(ch, 0x01a4);
M
Magnus Damm 已提交
302
	}
M
Magnus Damm 已提交
303

304 305
	sh_cmt_write_cmcor(ch, 0xffffffff);
	sh_cmt_write_cmcnt(ch, 0);
M
Magnus Damm 已提交
306

307 308 309 310 311 312 313 314 315 316 317 318
	/*
	 * According to the sh73a0 user's manual, as CMCNT can be operated
	 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
	 * modifying CMCNT register; two RCLK cycles are necessary before
	 * this register is either read or any modification of the value
	 * it holds is reflected in the LSI's actual operation.
	 *
	 * While at it, we're supposed to clear out the CMCNT as of this
	 * moment, so make sure it's processed properly here.  This will
	 * take RCLKx2 at maximum.
	 */
	for (k = 0; k < 100; k++) {
319
		if (!sh_cmt_read_cmcnt(ch))
320 321 322 323
			break;
		udelay(1);
	}

324
	if (sh_cmt_read_cmcnt(ch)) {
325 326
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
			ch->index);
327 328 329 330
		ret = -ETIMEDOUT;
		goto err1;
	}

M
Magnus Damm 已提交
331
	/* enable channel */
332
	sh_cmt_start_stop_ch(ch, 1);
M
Magnus Damm 已提交
333
	return 0;
334 335
 err1:
	/* stop clock */
336
	clk_disable(ch->cmt->clk);
337 338 339

 err0:
	return ret;
M
Magnus Damm 已提交
340 341
}

342
static void sh_cmt_disable(struct sh_cmt_channel *ch)
M
Magnus Damm 已提交
343 344
{
	/* disable channel */
345
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
346

347
	/* disable interrupts in CMT block */
348
	sh_cmt_write_cmcsr(ch, 0);
349

350
	/* stop clock */
351
	clk_disable(ch->cmt->clk);
352

353 354
	dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
	pm_runtime_put(&ch->cmt->pdev->dev);
M
Magnus Damm 已提交
355 356 357 358 359 360 361 362 363
}

/* private flags */
#define FLAG_CLOCKEVENT (1 << 0)
#define FLAG_CLOCKSOURCE (1 << 1)
#define FLAG_REPROGRAM (1 << 2)
#define FLAG_SKIPEVENT (1 << 3)
#define FLAG_IRQCONTEXT (1 << 4)

364
static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
365 366 367
					      int absolute)
{
	unsigned long new_match;
368
	unsigned long value = ch->next_match_value;
M
Magnus Damm 已提交
369 370 371 372
	unsigned long delay = 0;
	unsigned long now = 0;
	int has_wrapped;

373 374
	now = sh_cmt_get_counter(ch, &has_wrapped);
	ch->flags |= FLAG_REPROGRAM; /* force reprogram */
M
Magnus Damm 已提交
375 376 377 378 379 380

	if (has_wrapped) {
		/* we're competing with the interrupt handler.
		 *  -> let the interrupt handler reprogram the timer.
		 *  -> interrupt number two handles the event.
		 */
381
		ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
382 383 384 385 386 387 388 389 390 391 392
		return;
	}

	if (absolute)
		now = 0;

	do {
		/* reprogram the timer hardware,
		 * but don't save the new match value yet.
		 */
		new_match = now + value + delay;
393 394
		if (new_match > ch->max_match_value)
			new_match = ch->max_match_value;
M
Magnus Damm 已提交
395

396
		sh_cmt_write_cmcor(ch, new_match);
M
Magnus Damm 已提交
397

398 399
		now = sh_cmt_get_counter(ch, &has_wrapped);
		if (has_wrapped && (new_match > ch->match_value)) {
M
Magnus Damm 已提交
400 401 402 403 404 405
			/* we are changing to a greater match value,
			 * so this wrap must be caused by the counter
			 * matching the old value.
			 * -> first interrupt reprograms the timer.
			 * -> interrupt number two handles the event.
			 */
406
			ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
407 408 409 410 411 412 413 414 415 416
			break;
		}

		if (has_wrapped) {
			/* we are changing to a smaller match value,
			 * so the wrap must be caused by the counter
			 * matching the new value.
			 * -> save programmed match value.
			 * -> let isr handle the event.
			 */
417
			ch->match_value = new_match;
M
Magnus Damm 已提交
418 419 420 421 422 423 424 425 426 427
			break;
		}

		/* be safe: verify hardware settings */
		if (now < new_match) {
			/* timer value is below match value, all good.
			 * this makes sure we won't miss any match events.
			 * -> save programmed match value.
			 * -> let isr handle the event.
			 */
428
			ch->match_value = new_match;
M
Magnus Damm 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
			break;
		}

		/* the counter has reached a value greater
		 * than our new match value. and since the
		 * has_wrapped flag isn't set we must have
		 * programmed a too close event.
		 * -> increase delay and retry.
		 */
		if (delay)
			delay <<= 1;
		else
			delay = 1;

		if (!delay)
444 445
			dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
				 ch->index);
M
Magnus Damm 已提交
446 447 448 449

	} while (delay);
}

450
static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
M
Magnus Damm 已提交
451
{
452
	if (delta > ch->max_match_value)
453 454
		dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
			 ch->index);
M
Magnus Damm 已提交
455

456 457
	ch->next_match_value = delta;
	sh_cmt_clock_event_program_verify(ch, 0);
458 459
}

460
static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
461 462 463
{
	unsigned long flags;

464 465 466
	raw_spin_lock_irqsave(&ch->lock, flags);
	__sh_cmt_set_next(ch, delta);
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
467 468 469 470
}

static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
{
471
	struct sh_cmt_channel *ch = dev_id;
M
Magnus Damm 已提交
472 473

	/* clear flags */
474 475
	sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
			   ch->cmt->info->clear_bits);
M
Magnus Damm 已提交
476 477 478 479 480

	/* update clock source counter to begin with if enabled
	 * the wrap flag should be cleared by the timer specific
	 * isr before we end up here.
	 */
481 482
	if (ch->flags & FLAG_CLOCKSOURCE)
		ch->total_cycles += ch->match_value + 1;
M
Magnus Damm 已提交
483

484 485
	if (!(ch->flags & FLAG_REPROGRAM))
		ch->next_match_value = ch->max_match_value;
M
Magnus Damm 已提交
486

487
	ch->flags |= FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
488

489 490 491 492 493
	if (ch->flags & FLAG_CLOCKEVENT) {
		if (!(ch->flags & FLAG_SKIPEVENT)) {
			if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
				ch->next_match_value = ch->max_match_value;
				ch->flags |= FLAG_REPROGRAM;
M
Magnus Damm 已提交
494 495
			}

496
			ch->ced.event_handler(&ch->ced);
M
Magnus Damm 已提交
497 498 499
		}
	}

500
	ch->flags &= ~FLAG_SKIPEVENT;
M
Magnus Damm 已提交
501

502 503 504
	if (ch->flags & FLAG_REPROGRAM) {
		ch->flags &= ~FLAG_REPROGRAM;
		sh_cmt_clock_event_program_verify(ch, 1);
M
Magnus Damm 已提交
505

506 507 508 509
		if (ch->flags & FLAG_CLOCKEVENT)
			if ((ch->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
			    || (ch->match_value == ch->next_match_value))
				ch->flags &= ~FLAG_REPROGRAM;
M
Magnus Damm 已提交
510 511
	}

512
	ch->flags &= ~FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
513 514 515 516

	return IRQ_HANDLED;
}

517
static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
518 519 520 521
{
	int ret = 0;
	unsigned long flags;

522
	raw_spin_lock_irqsave(&ch->lock, flags);
M
Magnus Damm 已提交
523

524 525
	if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		ret = sh_cmt_enable(ch, &ch->rate);
M
Magnus Damm 已提交
526 527 528

	if (ret)
		goto out;
529
	ch->flags |= flag;
M
Magnus Damm 已提交
530 531

	/* setup timeout if no clockevent */
532 533
	if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
		__sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
534
 out:
535
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
536 537 538 539

	return ret;
}

540
static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
541 542 543 544
{
	unsigned long flags;
	unsigned long f;

545
	raw_spin_lock_irqsave(&ch->lock, flags);
M
Magnus Damm 已提交
546

547 548
	f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
	ch->flags &= ~flag;
M
Magnus Damm 已提交
549

550 551
	if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		sh_cmt_disable(ch);
M
Magnus Damm 已提交
552 553

	/* adjust the timeout to maximum if only clocksource left */
554 555
	if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
		__sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
556

557
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
558 559
}

560
static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
561
{
562
	return container_of(cs, struct sh_cmt_channel, cs);
563 564 565 566
}

static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
{
567
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
568 569 570 571
	unsigned long flags, raw;
	unsigned long value;
	int has_wrapped;

572 573 574
	raw_spin_lock_irqsave(&ch->lock, flags);
	value = ch->total_cycles;
	raw = sh_cmt_get_counter(ch, &has_wrapped);
575 576

	if (unlikely(has_wrapped))
577 578
		raw += ch->match_value + 1;
	raw_spin_unlock_irqrestore(&ch->lock, flags);
579 580 581 582 583 584

	return value + raw;
}

static int sh_cmt_clocksource_enable(struct clocksource *cs)
{
585
	int ret;
586
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
587

588
	WARN_ON(ch->cs_enabled);
589

590
	ch->total_cycles = 0;
591

592
	ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
593
	if (!ret) {
594 595
		__clocksource_updatefreq_hz(cs, ch->rate);
		ch->cs_enabled = true;
596
	}
597
	return ret;
598 599 600 601
}

static void sh_cmt_clocksource_disable(struct clocksource *cs)
{
602
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
603

604
	WARN_ON(!ch->cs_enabled);
605

606 607
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	ch->cs_enabled = false;
608 609
}

610 611
static void sh_cmt_clocksource_suspend(struct clocksource *cs)
{
612
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
613

614 615
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
616 617
}

618 619
static void sh_cmt_clocksource_resume(struct clocksource *cs)
{
620
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
621

622 623
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
	sh_cmt_start(ch, FLAG_CLOCKSOURCE);
624 625
}

626
static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
627
				       const char *name, unsigned long rating)
628
{
629
	struct clocksource *cs = &ch->cs;
630 631 632 633 634 635

	cs->name = name;
	cs->rating = rating;
	cs->read = sh_cmt_clocksource_read;
	cs->enable = sh_cmt_clocksource_enable;
	cs->disable = sh_cmt_clocksource_disable;
636
	cs->suspend = sh_cmt_clocksource_suspend;
637
	cs->resume = sh_cmt_clocksource_resume;
638 639
	cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
	cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
640

641 642
	dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
		 ch->index);
643

644 645
	/* Register with dummy 1 Hz value, gets updated in ->enable() */
	clocksource_register_hz(cs, 1);
646 647 648
	return 0;
}

649
static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
M
Magnus Damm 已提交
650
{
651
	return container_of(ced, struct sh_cmt_channel, ced);
M
Magnus Damm 已提交
652 653
}

654
static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
M
Magnus Damm 已提交
655
{
656
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
657

658
	sh_cmt_start(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
659 660 661 662

	/* TODO: calculate good shift from rate and counter bit width */

	ced->shift = 32;
663 664
	ced->mult = div_sc(ch->rate, NSEC_PER_SEC, ced->shift);
	ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
M
Magnus Damm 已提交
665 666 667
	ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);

	if (periodic)
668
		sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
M
Magnus Damm 已提交
669
	else
670
		sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
671 672 673 674 675
}

static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
				    struct clock_event_device *ced)
{
676
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
M
Magnus Damm 已提交
677 678 679 680 681

	/* deal with old setting first */
	switch (ced->mode) {
	case CLOCK_EVT_MODE_PERIODIC:
	case CLOCK_EVT_MODE_ONESHOT:
682
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
683 684 685 686 687 688 689
		break;
	default:
		break;
	}

	switch (mode) {
	case CLOCK_EVT_MODE_PERIODIC:
690
		dev_info(&ch->cmt->pdev->dev,
691
			 "ch%u: used for periodic clock events\n", ch->index);
692
		sh_cmt_clock_event_start(ch, 1);
M
Magnus Damm 已提交
693 694
		break;
	case CLOCK_EVT_MODE_ONESHOT:
695
		dev_info(&ch->cmt->pdev->dev,
696
			 "ch%u: used for oneshot clock events\n", ch->index);
697
		sh_cmt_clock_event_start(ch, 0);
M
Magnus Damm 已提交
698 699 700
		break;
	case CLOCK_EVT_MODE_SHUTDOWN:
	case CLOCK_EVT_MODE_UNUSED:
701
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
702 703 704 705 706 707 708 709 710
		break;
	default:
		break;
	}
}

static int sh_cmt_clock_event_next(unsigned long delta,
				   struct clock_event_device *ced)
{
711
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
M
Magnus Damm 已提交
712 713

	BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
714 715
	if (likely(ch->flags & FLAG_IRQCONTEXT))
		ch->next_match_value = delta - 1;
M
Magnus Damm 已提交
716
	else
717
		sh_cmt_set_next(ch, delta - 1);
M
Magnus Damm 已提交
718 719 720 721

	return 0;
}

722 723
static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
{
724
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
725

726 727
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
	clk_unprepare(ch->cmt->clk);
728 729 730 731
}

static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
{
732
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
733

734 735
	clk_prepare(ch->cmt->clk);
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
736 737
}

738
static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
739
				       const char *name, unsigned long rating)
M
Magnus Damm 已提交
740
{
741
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
742 743 744 745 746 747 748 749

	ced->name = name;
	ced->features = CLOCK_EVT_FEAT_PERIODIC;
	ced->features |= CLOCK_EVT_FEAT_ONESHOT;
	ced->rating = rating;
	ced->cpumask = cpumask_of(0);
	ced->set_next_event = sh_cmt_clock_event_next;
	ced->set_mode = sh_cmt_clock_event_mode;
750 751
	ced->suspend = sh_cmt_clock_event_suspend;
	ced->resume = sh_cmt_clock_event_resume;
M
Magnus Damm 已提交
752

753 754
	dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
		 ch->index);
M
Magnus Damm 已提交
755 756 757
	clockevents_register_device(ced);
}

758
static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
759 760
			   unsigned long clockevent_rating,
			   unsigned long clocksource_rating)
M
Magnus Damm 已提交
761 762
{
	if (clockevent_rating)
763
		sh_cmt_register_clockevent(ch, name, clockevent_rating);
M
Magnus Damm 已提交
764

765
	if (clocksource_rating)
766
		sh_cmt_register_clocksource(ch, name, clocksource_rating);
767

M
Magnus Damm 已提交
768 769 770
	return 0;
}

771
static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
772 773 774 775 776 777 778
				struct sh_cmt_device *cmt)
{
	struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
	int irq;
	int ret;

	ch->cmt = cmt;
779
	ch->base = cmt->mapbase_ch;
780
	ch->index = index;
781 782 783

	irq = platform_get_irq(cmt->pdev, 0);
	if (irq < 0) {
784 785
		dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n",
			ch->index);
786 787 788
		return irq;
	}

789
	if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
790 791
		ch->max_match_value = ~0;
	else
792
		ch->max_match_value = (1 << cmt->info->width) - 1;
793 794 795 796

	ch->match_value = ch->max_match_value;
	raw_spin_lock_init(&ch->lock);

797
	ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
798 799 800
			      cfg->clockevent_rating,
			      cfg->clocksource_rating);
	if (ret) {
801 802
		dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
			ch->index);
803 804 805 806 807 808 809 810
		return ret;
	}
	ch->cs_enabled = false;

	ret = request_irq(irq, sh_cmt_interrupt,
			  IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
			  dev_name(&cmt->pdev->dev), ch);
	if (ret) {
811 812
		dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n",
			ch->index, irq);
813 814 815 816 817 818
		return ret;
	}

	return 0;
}

819
static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
M
Magnus Damm 已提交
820
{
821
	struct sh_timer_config *cfg = pdev->dev.platform_data;
822
	struct resource *res, *res2;
823
	int ret;
M
Magnus Damm 已提交
824 825
	ret = -ENXIO;

826
	cmt->pdev = pdev;
M
Magnus Damm 已提交
827 828

	if (!cfg) {
829
		dev_err(&cmt->pdev->dev, "missing platform data\n");
M
Magnus Damm 已提交
830 831 832
		goto err0;
	}

833
	res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
M
Magnus Damm 已提交
834
	if (!res) {
835
		dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
M
Magnus Damm 已提交
836 837 838
		goto err0;
	}

839
	/* optional resource for the shared timer start/stop register */
840
	res2 = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 1);
841

842 843 844
	/* map memory, let mapbase_ch point to our channel */
	cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res));
	if (cmt->mapbase_ch == NULL) {
845
		dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
M
Magnus Damm 已提交
846 847 848
		goto err0;
	}

849
	/* map second resource for CMSTR */
850 851 852 853
	cmt->mapbase = ioremap_nocache(res2 ? res2->start :
				       res->start - cfg->channel_offset,
				       res2 ? resource_size(res2) : 2);
	if (cmt->mapbase == NULL) {
854
		dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
855 856 857
		goto err1;
	}

M
Magnus Damm 已提交
858
	/* get hold of clock */
859 860 861 862
	cmt->clk = clk_get(&cmt->pdev->dev, "cmt_fck");
	if (IS_ERR(cmt->clk)) {
		dev_err(&cmt->pdev->dev, "cannot get clock\n");
		ret = PTR_ERR(cmt->clk);
863
		goto err2;
M
Magnus Damm 已提交
864 865
	}

866
	ret = clk_prepare(cmt->clk);
867 868 869
	if (ret < 0)
		goto err3;

870 871 872 873 874 875 876
	/* identify the model based on the resources */
	if (resource_size(res) == 6)
		cmt->info = &sh_cmt_info[SH_CMT_16BIT];
	else if (res2 && (resource_size(res2) == 4))
		cmt->info = &sh_cmt_info[SH_CMT_48BIT_GEN2];
	else
		cmt->info = &sh_cmt_info[SH_CMT_32BIT];
M
Magnus Damm 已提交
877

878 879 880 881 882 883 884 885 886
	cmt->channels = kzalloc(sizeof(*cmt->channels), GFP_KERNEL);
	if (cmt->channels == NULL) {
		ret = -ENOMEM;
		goto err4;
	}

	cmt->num_channels = 1;

	ret = sh_cmt_setup_channel(&cmt->channels[0], cfg->timer_bit, cmt);
887
	if (ret < 0)
888
		goto err4;
889

890
	platform_set_drvdata(pdev, cmt);
891

892
	return 0;
893
err4:
894
	kfree(cmt->channels);
895
	clk_unprepare(cmt->clk);
896
err3:
897
	clk_put(cmt->clk);
898
err2:
899
	iounmap(cmt->mapbase);
900 901
err1:
	iounmap(cmt->mapbase_ch);
902
err0:
M
Magnus Damm 已提交
903 904 905
	return ret;
}

906
static int sh_cmt_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
907
{
908
	struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
909
	struct sh_timer_config *cfg = pdev->dev.platform_data;
M
Magnus Damm 已提交
910 911
	int ret;

912
	if (!is_early_platform_device(pdev)) {
913 914
		pm_runtime_set_active(&pdev->dev);
		pm_runtime_enable(&pdev->dev);
915
	}
916

917
	if (cmt) {
918
		dev_info(&pdev->dev, "kept as earlytimer\n");
919
		goto out;
920 921
	}

922
	cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
923
	if (cmt == NULL) {
M
Magnus Damm 已提交
924 925 926 927
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		return -ENOMEM;
	}

928
	ret = sh_cmt_setup(cmt, pdev);
M
Magnus Damm 已提交
929
	if (ret) {
930
		kfree(cmt);
931 932
		pm_runtime_idle(&pdev->dev);
		return ret;
M
Magnus Damm 已提交
933
	}
934 935 936 937 938 939 940 941 942 943
	if (is_early_platform_device(pdev))
		return 0;

 out:
	if (cfg->clockevent_rating || cfg->clocksource_rating)
		pm_runtime_irq_safe(&pdev->dev);
	else
		pm_runtime_idle(&pdev->dev);

	return 0;
M
Magnus Damm 已提交
944 945
}

946
static int sh_cmt_remove(struct platform_device *pdev)
M
Magnus Damm 已提交
947 948 949 950 951 952
{
	return -EBUSY; /* cannot unregister clockevent and clocksource */
}

static struct platform_driver sh_cmt_device_driver = {
	.probe		= sh_cmt_probe,
953
	.remove		= sh_cmt_remove,
M
Magnus Damm 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	.driver		= {
		.name	= "sh_cmt",
	}
};

static int __init sh_cmt_init(void)
{
	return platform_driver_register(&sh_cmt_device_driver);
}

static void __exit sh_cmt_exit(void)
{
	platform_driver_unregister(&sh_cmt_device_driver);
}

969
early_platform_init("earlytimer", &sh_cmt_device_driver);
970
subsys_initcall(sh_cmt_init);
M
Magnus Damm 已提交
971 972 973 974 975
module_exit(sh_cmt_exit);

MODULE_AUTHOR("Magnus Damm");
MODULE_DESCRIPTION("SuperH CMT Timer Driver");
MODULE_LICENSE("GPL v2");