sh_cmt.c 25.2 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#define SH_CMT16_CMCSR_CMF		(1 << 7)
#define SH_CMT16_CMCSR_CMIE		(1 << 6)
#define SH_CMT16_CMCSR_CKS8		(0 << 0)
#define SH_CMT16_CMCSR_CKS32		(1 << 0)
#define SH_CMT16_CMCSR_CKS128		(2 << 0)
#define SH_CMT16_CMCSR_CKS512		(3 << 0)
#define SH_CMT16_CMCSR_CKS_MASK		(3 << 0)

#define SH_CMT32_CMCSR_CMF		(1 << 15)
#define SH_CMT32_CMCSR_OVF		(1 << 14)
#define SH_CMT32_CMCSR_WRFLG		(1 << 13)
#define SH_CMT32_CMCSR_STTF		(1 << 12)
#define SH_CMT32_CMCSR_STPF		(1 << 11)
#define SH_CMT32_CMCSR_SSIE		(1 << 10)
#define SH_CMT32_CMCSR_CMS		(1 << 9)
#define SH_CMT32_CMCSR_CMM		(1 << 8)
#define SH_CMT32_CMCSR_CMTOUT_IE	(1 << 7)
#define SH_CMT32_CMCSR_CMR_NONE		(0 << 4)
#define SH_CMT32_CMCSR_CMR_DMA		(1 << 4)
#define SH_CMT32_CMCSR_CMR_IRQ		(2 << 4)
#define SH_CMT32_CMCSR_CMR_MASK		(3 << 4)
#define SH_CMT32_CMCSR_DBGIVD		(1 << 3)
#define SH_CMT32_CMCSR_CKS_RCLK8	(4 << 0)
#define SH_CMT32_CMCSR_CKS_RCLK32	(5 << 0)
#define SH_CMT32_CMCSR_CKS_RCLK128	(6 << 0)
#define SH_CMT32_CMCSR_CKS_RCLK1	(7 << 0)
#define SH_CMT32_CMCSR_CKS_MASK		(7 << 0)

145
static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
146 147 148 149
{
	return ioread16(base + (offs << 1));
}

150 151 152 153 154 155 156
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)
157 158 159
{
	iowrite16(value, base + (offs << 1));
}
M
Magnus Damm 已提交
160

161 162 163 164 165 166
static void sh_cmt_write32(void __iomem *base, unsigned long offs,
			   unsigned long value)
{
	iowrite32(value, base + (offs << 2));
}

167 168 169 170
static const struct sh_cmt_info sh_cmt_info[] = {
	[SH_CMT_16BIT] = {
		.model = SH_CMT_16BIT,
		.width = 16,
171 172
		.overflow_bit = SH_CMT16_CMCSR_CMF,
		.clear_bits = ~SH_CMT16_CMCSR_CMF,
173 174 175 176 177 178 179 180
		.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,
181 182
		.overflow_bit = SH_CMT32_CMCSR_CMF,
		.clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
183 184 185 186 187 188 189 190
		.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,
191 192
		.overflow_bit = SH_CMT32_CMCSR_CMF,
		.clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
193 194 195 196 197 198 199 200
		.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,
201 202
		.overflow_bit = SH_CMT32_CMCSR_CMF,
		.clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
203 204 205 206 207 208 209 210
		.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,
211 212
		.overflow_bit = SH_CMT32_CMCSR_CMF,
		.clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
213 214 215 216 217 218 219
		.read_control = sh_cmt_read32,
		.write_control = sh_cmt_write32,
		.read_count = sh_cmt_read32,
		.write_count = sh_cmt_write32,
	},
};

M
Magnus Damm 已提交
220 221 222 223
#define CMCSR 0 /* channel register */
#define CMCNT 1 /* channel register */
#define CMCOR 2 /* channel register */

224
static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
225
{
226
	return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
227 228
}

229
static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
230
{
231
	return ch->cmt->info->read_control(ch->base, CMCSR);
232 233
}

234
static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
235
{
236
	return ch->cmt->info->read_count(ch->base, CMCNT);
M
Magnus Damm 已提交
237 238
}

239
static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
240 241
				      unsigned long value)
{
242
	ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
243 244
}

245
static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
246 247
				      unsigned long value)
{
248
	ch->cmt->info->write_control(ch->base, CMCSR, value);
249 250
}

251
static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
252 253
				      unsigned long value)
{
254
	ch->cmt->info->write_count(ch->base, CMCNT, value);
255 256
}

257
static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
258 259
				      unsigned long value)
{
260
	ch->cmt->info->write_count(ch->base, CMCOR, value);
261 262
}

263
static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
264 265 266
					int *has_wrapped)
{
	unsigned long v1, v2, v3;
267 268
	int o1, o2;

269
	o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
M
Magnus Damm 已提交
270 271 272

	/* Make sure the timer value is stable. Stolen from acpi_pm.c */
	do {
273
		o2 = o1;
274 275 276
		v1 = sh_cmt_read_cmcnt(ch);
		v2 = sh_cmt_read_cmcnt(ch);
		v3 = sh_cmt_read_cmcnt(ch);
277
		o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
278 279
	} while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
			  || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
M
Magnus Damm 已提交
280

281
	*has_wrapped = o1;
M
Magnus Damm 已提交
282 283 284
	return v2;
}

285
static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
M
Magnus Damm 已提交
286

287
static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
M
Magnus Damm 已提交
288
{
289
	struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
M
Magnus Damm 已提交
290 291 292
	unsigned long flags, value;

	/* start stop register shared by multiple timer channels */
293
	raw_spin_lock_irqsave(&sh_cmt_lock, flags);
294
	value = sh_cmt_read_cmstr(ch);
M
Magnus Damm 已提交
295 296 297 298 299 300

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

301
	sh_cmt_write_cmstr(ch, value);
302
	raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
M
Magnus Damm 已提交
303 304
}

305
static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
M
Magnus Damm 已提交
306
{
307
	int k, ret;
M
Magnus Damm 已提交
308

309 310
	pm_runtime_get_sync(&ch->cmt->pdev->dev);
	dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
311

312
	/* enable clock */
313
	ret = clk_enable(ch->cmt->clk);
M
Magnus Damm 已提交
314
	if (ret) {
315 316
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
			ch->index);
317
		goto err0;
M
Magnus Damm 已提交
318 319 320
	}

	/* make sure channel is disabled */
321
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
322 323

	/* configure channel, periodic mode and maximum timeout */
324
	if (ch->cmt->info->width == 16) {
325
		*rate = clk_get_rate(ch->cmt->clk) / 512;
326 327
		sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
				   SH_CMT16_CMCSR_CKS512);
M
Magnus Damm 已提交
328
	} else {
329
		*rate = clk_get_rate(ch->cmt->clk) / 8;
330 331 332 333
		sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
				   SH_CMT32_CMCSR_CMTOUT_IE |
				   SH_CMT32_CMCSR_CMR_IRQ |
				   SH_CMT32_CMCSR_CKS_RCLK8);
M
Magnus Damm 已提交
334
	}
M
Magnus Damm 已提交
335

336 337
	sh_cmt_write_cmcor(ch, 0xffffffff);
	sh_cmt_write_cmcnt(ch, 0);
M
Magnus Damm 已提交
338

339 340 341 342 343 344 345 346 347 348 349 350
	/*
	 * 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++) {
351
		if (!sh_cmt_read_cmcnt(ch))
352 353 354 355
			break;
		udelay(1);
	}

356
	if (sh_cmt_read_cmcnt(ch)) {
357 358
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
			ch->index);
359 360 361 362
		ret = -ETIMEDOUT;
		goto err1;
	}

M
Magnus Damm 已提交
363
	/* enable channel */
364
	sh_cmt_start_stop_ch(ch, 1);
M
Magnus Damm 已提交
365
	return 0;
366 367
 err1:
	/* stop clock */
368
	clk_disable(ch->cmt->clk);
369 370 371

 err0:
	return ret;
M
Magnus Damm 已提交
372 373
}

374
static void sh_cmt_disable(struct sh_cmt_channel *ch)
M
Magnus Damm 已提交
375 376
{
	/* disable channel */
377
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
378

379
	/* disable interrupts in CMT block */
380
	sh_cmt_write_cmcsr(ch, 0);
381

382
	/* stop clock */
383
	clk_disable(ch->cmt->clk);
384

385 386
	dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
	pm_runtime_put(&ch->cmt->pdev->dev);
M
Magnus Damm 已提交
387 388 389 390 391 392 393 394 395
}

/* 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)

396
static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
397 398 399
					      int absolute)
{
	unsigned long new_match;
400
	unsigned long value = ch->next_match_value;
M
Magnus Damm 已提交
401 402 403 404
	unsigned long delay = 0;
	unsigned long now = 0;
	int has_wrapped;

405 406
	now = sh_cmt_get_counter(ch, &has_wrapped);
	ch->flags |= FLAG_REPROGRAM; /* force reprogram */
M
Magnus Damm 已提交
407 408 409 410 411 412

	if (has_wrapped) {
		/* we're competing with the interrupt handler.
		 *  -> let the interrupt handler reprogram the timer.
		 *  -> interrupt number two handles the event.
		 */
413
		ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
414 415 416 417 418 419 420 421 422 423 424
		return;
	}

	if (absolute)
		now = 0;

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

428
		sh_cmt_write_cmcor(ch, new_match);
M
Magnus Damm 已提交
429

430 431
		now = sh_cmt_get_counter(ch, &has_wrapped);
		if (has_wrapped && (new_match > ch->match_value)) {
M
Magnus Damm 已提交
432 433 434 435 436 437
			/* 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.
			 */
438
			ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
439 440 441 442 443 444 445 446 447 448
			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.
			 */
449
			ch->match_value = new_match;
M
Magnus Damm 已提交
450 451 452 453 454 455 456 457 458 459
			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.
			 */
460
			ch->match_value = new_match;
M
Magnus Damm 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
			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)
476 477
			dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
				 ch->index);
M
Magnus Damm 已提交
478 479 480 481

	} while (delay);
}

482
static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
M
Magnus Damm 已提交
483
{
484
	if (delta > ch->max_match_value)
485 486
		dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
			 ch->index);
M
Magnus Damm 已提交
487

488 489
	ch->next_match_value = delta;
	sh_cmt_clock_event_program_verify(ch, 0);
490 491
}

492
static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
493 494 495
{
	unsigned long flags;

496 497 498
	raw_spin_lock_irqsave(&ch->lock, flags);
	__sh_cmt_set_next(ch, delta);
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
499 500 501 502
}

static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
{
503
	struct sh_cmt_channel *ch = dev_id;
M
Magnus Damm 已提交
504 505

	/* clear flags */
506 507
	sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
			   ch->cmt->info->clear_bits);
M
Magnus Damm 已提交
508 509 510 511 512

	/* 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.
	 */
513 514
	if (ch->flags & FLAG_CLOCKSOURCE)
		ch->total_cycles += ch->match_value + 1;
M
Magnus Damm 已提交
515

516 517
	if (!(ch->flags & FLAG_REPROGRAM))
		ch->next_match_value = ch->max_match_value;
M
Magnus Damm 已提交
518

519
	ch->flags |= FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
520

521 522 523 524 525
	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 已提交
526 527
			}

528
			ch->ced.event_handler(&ch->ced);
M
Magnus Damm 已提交
529 530 531
		}
	}

532
	ch->flags &= ~FLAG_SKIPEVENT;
M
Magnus Damm 已提交
533

534 535 536
	if (ch->flags & FLAG_REPROGRAM) {
		ch->flags &= ~FLAG_REPROGRAM;
		sh_cmt_clock_event_program_verify(ch, 1);
M
Magnus Damm 已提交
537

538 539 540 541
		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 已提交
542 543
	}

544
	ch->flags &= ~FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
545 546 547 548

	return IRQ_HANDLED;
}

549
static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
550 551 552 553
{
	int ret = 0;
	unsigned long flags;

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

556 557
	if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		ret = sh_cmt_enable(ch, &ch->rate);
M
Magnus Damm 已提交
558 559 560

	if (ret)
		goto out;
561
	ch->flags |= flag;
M
Magnus Damm 已提交
562 563

	/* setup timeout if no clockevent */
564 565
	if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
		__sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
566
 out:
567
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
568 569 570 571

	return ret;
}

572
static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
573 574 575 576
{
	unsigned long flags;
	unsigned long f;

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

579 580
	f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
	ch->flags &= ~flag;
M
Magnus Damm 已提交
581

582 583
	if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		sh_cmt_disable(ch);
M
Magnus Damm 已提交
584 585

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

589
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
590 591
}

592
static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
593
{
594
	return container_of(cs, struct sh_cmt_channel, cs);
595 596 597 598
}

static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
{
599
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
600 601 602 603
	unsigned long flags, raw;
	unsigned long value;
	int has_wrapped;

604 605 606
	raw_spin_lock_irqsave(&ch->lock, flags);
	value = ch->total_cycles;
	raw = sh_cmt_get_counter(ch, &has_wrapped);
607 608

	if (unlikely(has_wrapped))
609 610
		raw += ch->match_value + 1;
	raw_spin_unlock_irqrestore(&ch->lock, flags);
611 612 613 614 615 616

	return value + raw;
}

static int sh_cmt_clocksource_enable(struct clocksource *cs)
{
617
	int ret;
618
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
619

620
	WARN_ON(ch->cs_enabled);
621

622
	ch->total_cycles = 0;
623

624
	ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
625
	if (!ret) {
626 627
		__clocksource_updatefreq_hz(cs, ch->rate);
		ch->cs_enabled = true;
628
	}
629
	return ret;
630 631 632 633
}

static void sh_cmt_clocksource_disable(struct clocksource *cs)
{
634
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
635

636
	WARN_ON(!ch->cs_enabled);
637

638 639
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	ch->cs_enabled = false;
640 641
}

642 643
static void sh_cmt_clocksource_suspend(struct clocksource *cs)
{
644
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
645

646 647
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
648 649
}

650 651
static void sh_cmt_clocksource_resume(struct clocksource *cs)
{
652
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
653

654 655
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
	sh_cmt_start(ch, FLAG_CLOCKSOURCE);
656 657
}

658
static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
659
				       const char *name, unsigned long rating)
660
{
661
	struct clocksource *cs = &ch->cs;
662 663 664 665 666 667

	cs->name = name;
	cs->rating = rating;
	cs->read = sh_cmt_clocksource_read;
	cs->enable = sh_cmt_clocksource_enable;
	cs->disable = sh_cmt_clocksource_disable;
668
	cs->suspend = sh_cmt_clocksource_suspend;
669
	cs->resume = sh_cmt_clocksource_resume;
670 671
	cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
	cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
672

673 674
	dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
		 ch->index);
675

676 677
	/* Register with dummy 1 Hz value, gets updated in ->enable() */
	clocksource_register_hz(cs, 1);
678 679 680
	return 0;
}

681
static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
M
Magnus Damm 已提交
682
{
683
	return container_of(ced, struct sh_cmt_channel, ced);
M
Magnus Damm 已提交
684 685
}

686
static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
M
Magnus Damm 已提交
687
{
688
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
689

690
	sh_cmt_start(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
691 692 693 694

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

	ced->shift = 32;
695 696
	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 已提交
697 698 699
	ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);

	if (periodic)
700
		sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
M
Magnus Damm 已提交
701
	else
702
		sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
703 704 705 706 707
}

static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
				    struct clock_event_device *ced)
{
708
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
M
Magnus Damm 已提交
709 710 711 712 713

	/* deal with old setting first */
	switch (ced->mode) {
	case CLOCK_EVT_MODE_PERIODIC:
	case CLOCK_EVT_MODE_ONESHOT:
714
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
715 716 717 718 719 720 721
		break;
	default:
		break;
	}

	switch (mode) {
	case CLOCK_EVT_MODE_PERIODIC:
722
		dev_info(&ch->cmt->pdev->dev,
723
			 "ch%u: used for periodic clock events\n", ch->index);
724
		sh_cmt_clock_event_start(ch, 1);
M
Magnus Damm 已提交
725 726
		break;
	case CLOCK_EVT_MODE_ONESHOT:
727
		dev_info(&ch->cmt->pdev->dev,
728
			 "ch%u: used for oneshot clock events\n", ch->index);
729
		sh_cmt_clock_event_start(ch, 0);
M
Magnus Damm 已提交
730 731 732
		break;
	case CLOCK_EVT_MODE_SHUTDOWN:
	case CLOCK_EVT_MODE_UNUSED:
733
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
734 735 736 737 738 739 740 741 742
		break;
	default:
		break;
	}
}

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

	BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
746 747
	if (likely(ch->flags & FLAG_IRQCONTEXT))
		ch->next_match_value = delta - 1;
M
Magnus Damm 已提交
748
	else
749
		sh_cmt_set_next(ch, delta - 1);
M
Magnus Damm 已提交
750 751 752 753

	return 0;
}

754 755
static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
{
756
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
757

758 759
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
	clk_unprepare(ch->cmt->clk);
760 761 762 763
}

static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
{
764
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
765

766 767
	clk_prepare(ch->cmt->clk);
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
768 769
}

770
static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
771
				       const char *name)
M
Magnus Damm 已提交
772
{
773
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
774 775 776 777

	ced->name = name;
	ced->features = CLOCK_EVT_FEAT_PERIODIC;
	ced->features |= CLOCK_EVT_FEAT_ONESHOT;
778
	ced->rating = 125;
779
	ced->cpumask = cpu_possible_mask;
M
Magnus Damm 已提交
780 781
	ced->set_next_event = sh_cmt_clock_event_next;
	ced->set_mode = sh_cmt_clock_event_mode;
782 783
	ced->suspend = sh_cmt_clock_event_suspend;
	ced->resume = sh_cmt_clock_event_resume;
M
Magnus Damm 已提交
784

785 786
	dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
		 ch->index);
M
Magnus Damm 已提交
787 788 789
	clockevents_register_device(ced);
}

790
static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
791
			   bool clockevent, unsigned long clocksource_rating)
M
Magnus Damm 已提交
792
{
793 794
	if (clockevent)
		sh_cmt_register_clockevent(ch, name);
M
Magnus Damm 已提交
795

796
	if (clocksource_rating)
797
		sh_cmt_register_clocksource(ch, name, clocksource_rating);
798

M
Magnus Damm 已提交
799 800 801
	return 0;
}

802
static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
803 804 805 806 807 808 809
				struct sh_cmt_device *cmt)
{
	struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
	int irq;
	int ret;

	ch->cmt = cmt;
810
	ch->base = cmt->mapbase_ch;
811
	ch->index = index;
812 813 814

	irq = platform_get_irq(cmt->pdev, 0);
	if (irq < 0) {
815 816
		dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n",
			ch->index);
817 818 819
		return irq;
	}

820
	if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
821 822
		ch->max_match_value = ~0;
	else
823
		ch->max_match_value = (1 << cmt->info->width) - 1;
824 825 826 827

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

828
	ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
829
			      cfg->clockevent_rating != 0,
830 831
			      cfg->clocksource_rating);
	if (ret) {
832 833
		dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
			ch->index);
834 835 836 837 838 839 840 841
		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) {
842 843
		dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n",
			ch->index, irq);
844 845 846 847 848 849
		return ret;
	}

	return 0;
}

850
static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
M
Magnus Damm 已提交
851
{
852
	struct sh_timer_config *cfg = pdev->dev.platform_data;
853
	struct resource *res, *res2;
854
	int ret;
M
Magnus Damm 已提交
855 856
	ret = -ENXIO;

857
	cmt->pdev = pdev;
M
Magnus Damm 已提交
858 859

	if (!cfg) {
860
		dev_err(&cmt->pdev->dev, "missing platform data\n");
M
Magnus Damm 已提交
861 862 863
		goto err0;
	}

864
	res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
M
Magnus Damm 已提交
865
	if (!res) {
866
		dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
M
Magnus Damm 已提交
867 868 869
		goto err0;
	}

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

873 874 875
	/* map memory, let mapbase_ch point to our channel */
	cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res));
	if (cmt->mapbase_ch == NULL) {
876
		dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
M
Magnus Damm 已提交
877 878 879
		goto err0;
	}

880
	/* map second resource for CMSTR */
881 882 883 884
	cmt->mapbase = ioremap_nocache(res2 ? res2->start :
				       res->start - cfg->channel_offset,
				       res2 ? resource_size(res2) : 2);
	if (cmt->mapbase == NULL) {
885
		dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
886 887 888
		goto err1;
	}

M
Magnus Damm 已提交
889
	/* get hold of clock */
890 891 892 893
	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);
894
		goto err2;
M
Magnus Damm 已提交
895 896
	}

897
	ret = clk_prepare(cmt->clk);
898 899 900
	if (ret < 0)
		goto err3;

901 902 903 904 905 906 907
	/* 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 已提交
908

909 910 911 912 913 914 915 916 917
	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);
918
	if (ret < 0)
919
		goto err4;
920

921
	platform_set_drvdata(pdev, cmt);
922

923
	return 0;
924
err4:
925
	kfree(cmt->channels);
926
	clk_unprepare(cmt->clk);
927
err3:
928
	clk_put(cmt->clk);
929
err2:
930
	iounmap(cmt->mapbase);
931 932
err1:
	iounmap(cmt->mapbase_ch);
933
err0:
M
Magnus Damm 已提交
934 935 936
	return ret;
}

937
static int sh_cmt_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
938
{
939
	struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
940
	struct sh_timer_config *cfg = pdev->dev.platform_data;
M
Magnus Damm 已提交
941 942
	int ret;

943
	if (!is_early_platform_device(pdev)) {
944 945
		pm_runtime_set_active(&pdev->dev);
		pm_runtime_enable(&pdev->dev);
946
	}
947

948
	if (cmt) {
949
		dev_info(&pdev->dev, "kept as earlytimer\n");
950
		goto out;
951 952
	}

953
	cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
954
	if (cmt == NULL) {
M
Magnus Damm 已提交
955 956 957 958
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		return -ENOMEM;
	}

959
	ret = sh_cmt_setup(cmt, pdev);
M
Magnus Damm 已提交
960
	if (ret) {
961
		kfree(cmt);
962 963
		pm_runtime_idle(&pdev->dev);
		return ret;
M
Magnus Damm 已提交
964
	}
965 966 967 968 969 970 971 972 973 974
	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 已提交
975 976
}

977
static int sh_cmt_remove(struct platform_device *pdev)
M
Magnus Damm 已提交
978 979 980 981 982 983
{
	return -EBUSY; /* cannot unregister clockevent and clocksource */
}

static struct platform_driver sh_cmt_device_driver = {
	.probe		= sh_cmt_probe,
984
	.remove		= sh_cmt_remove,
M
Magnus Damm 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	.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);
}

1000
early_platform_init("earlytimer", &sh_cmt_device_driver);
1001
subsys_initcall(sh_cmt_init);
M
Magnus Damm 已提交
1002 1003 1004 1005 1006
module_exit(sh_cmt_exit);

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