sh_cmt.c 22.3 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

struct sh_cmt_channel {
41
	struct sh_cmt_device *cmt;
42
	unsigned int index;
M
Magnus Damm 已提交
43

44 45
	void __iomem *base;

M
Magnus Damm 已提交
46 47 48 49 50
	unsigned long flags;
	unsigned long match_value;
	unsigned long next_match_value;
	unsigned long max_match_value;
	unsigned long rate;
51
	raw_spinlock_t lock;
M
Magnus Damm 已提交
52
	struct clock_event_device ced;
53
	struct clocksource cs;
M
Magnus Damm 已提交
54
	unsigned long total_cycles;
55
	bool cs_enabled;
56 57
};

58
struct sh_cmt_device {
59 60
	struct platform_device *pdev;

61
	void __iomem *mapbase_ch;
62 63 64 65 66 67 68 69
	void __iomem *mapbase;
	struct clk *clk;

	struct sh_cmt_channel channel;

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

71 72 73 74 75
	/* 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);

76 77 78 79
	/* 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);
M
Magnus Damm 已提交
80 81
};

82 83 84 85 86 87 88 89 90 91 92 93 94
/* Examples of supported CMT timer register layouts and I/O access widths:
 *
 * "16-bit counter and 16-bit control" as found on sh7263:
 * CMSTR 0xfffec000 16-bit
 * CMCSR 0xfffec002 16-bit
 * CMCNT 0xfffec004 16-bit
 * CMCOR 0xfffec006 16-bit
 *
 * "32-bit counter and 16-bit control" as found on sh7372, sh73a0, r8a7740:
 * CMSTR 0xffca0000 16-bit
 * CMCSR 0xffca0060 16-bit
 * CMCNT 0xffca0064 32-bit
 * CMCOR 0xffca0068 32-bit
95 96 97 98 99 100
 *
 * "32-bit counter and 32-bit control" as found on r8a73a4 and r8a7790:
 * CMSTR 0xffca0500 32-bit
 * CMCSR 0xffca0510 32-bit
 * CMCNT 0xffca0514 32-bit
 * CMCOR 0xffca0518 32-bit
101 102
 */

103
static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
104 105 106 107
{
	return ioread16(base + (offs << 1));
}

108 109 110 111 112 113 114
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)
115 116 117
{
	iowrite16(value, base + (offs << 1));
}
M
Magnus Damm 已提交
118

119 120 121 122 123 124
static void sh_cmt_write32(void __iomem *base, unsigned long offs,
			   unsigned long value)
{
	iowrite32(value, base + (offs << 2));
}

M
Magnus Damm 已提交
125 126 127 128
#define CMCSR 0 /* channel register */
#define CMCNT 1 /* channel register */
#define CMCOR 2 /* channel register */

129
static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
130
{
131
	return ch->cmt->read_control(ch->cmt->mapbase, 0);
132 133
}

134
static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
135
{
136
	return ch->cmt->read_control(ch->base, CMCSR);
137 138
}

139
static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
140
{
141
	return ch->cmt->read_count(ch->base, CMCNT);
M
Magnus Damm 已提交
142 143
}

144
static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
145 146
				      unsigned long value)
{
147
	ch->cmt->write_control(ch->cmt->mapbase, 0, value);
148 149
}

150
static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
151 152
				      unsigned long value)
{
153
	ch->cmt->write_control(ch->base, CMCSR, value);
154 155
}

156
static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
157 158
				      unsigned long value)
{
159
	ch->cmt->write_count(ch->base, CMCNT, value);
160 161
}

162
static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
163 164
				      unsigned long value)
{
165
	ch->cmt->write_count(ch->base, CMCOR, value);
166 167
}

168
static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
169 170 171
					int *has_wrapped)
{
	unsigned long v1, v2, v3;
172 173
	int o1, o2;

174
	o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
M
Magnus Damm 已提交
175 176 177

	/* Make sure the timer value is stable. Stolen from acpi_pm.c */
	do {
178
		o2 = o1;
179 180 181 182
		v1 = sh_cmt_read_cmcnt(ch);
		v2 = sh_cmt_read_cmcnt(ch);
		v3 = sh_cmt_read_cmcnt(ch);
		o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->overflow_bit;
183 184
	} while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
			  || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
M
Magnus Damm 已提交
185

186
	*has_wrapped = o1;
M
Magnus Damm 已提交
187 188 189
	return v2;
}

190
static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
M
Magnus Damm 已提交
191

192
static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
M
Magnus Damm 已提交
193
{
194
	struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data;
M
Magnus Damm 已提交
195 196 197
	unsigned long flags, value;

	/* start stop register shared by multiple timer channels */
198
	raw_spin_lock_irqsave(&sh_cmt_lock, flags);
199
	value = sh_cmt_read_cmstr(ch);
M
Magnus Damm 已提交
200 201 202 203 204 205

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

206
	sh_cmt_write_cmstr(ch, value);
207
	raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
M
Magnus Damm 已提交
208 209
}

210
static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate)
M
Magnus Damm 已提交
211
{
212
	int k, ret;
M
Magnus Damm 已提交
213

214 215
	pm_runtime_get_sync(&ch->cmt->pdev->dev);
	dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
216

217
	/* enable clock */
218
	ret = clk_enable(ch->cmt->clk);
M
Magnus Damm 已提交
219
	if (ret) {
220 221
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
			ch->index);
222
		goto err0;
M
Magnus Damm 已提交
223 224 225
	}

	/* make sure channel is disabled */
226
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
227 228

	/* configure channel, periodic mode and maximum timeout */
229 230 231
	if (ch->cmt->width == 16) {
		*rate = clk_get_rate(ch->cmt->clk) / 512;
		sh_cmt_write_cmcsr(ch, 0x43);
M
Magnus Damm 已提交
232
	} else {
233 234
		*rate = clk_get_rate(ch->cmt->clk) / 8;
		sh_cmt_write_cmcsr(ch, 0x01a4);
M
Magnus Damm 已提交
235
	}
M
Magnus Damm 已提交
236

237 238
	sh_cmt_write_cmcor(ch, 0xffffffff);
	sh_cmt_write_cmcnt(ch, 0);
M
Magnus Damm 已提交
239

240 241 242 243 244 245 246 247 248 249 250 251
	/*
	 * 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++) {
252
		if (!sh_cmt_read_cmcnt(ch))
253 254 255 256
			break;
		udelay(1);
	}

257
	if (sh_cmt_read_cmcnt(ch)) {
258 259
		dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
			ch->index);
260 261 262 263
		ret = -ETIMEDOUT;
		goto err1;
	}

M
Magnus Damm 已提交
264
	/* enable channel */
265
	sh_cmt_start_stop_ch(ch, 1);
M
Magnus Damm 已提交
266
	return 0;
267 268
 err1:
	/* stop clock */
269
	clk_disable(ch->cmt->clk);
270 271 272

 err0:
	return ret;
M
Magnus Damm 已提交
273 274
}

275
static void sh_cmt_disable(struct sh_cmt_channel *ch)
M
Magnus Damm 已提交
276 277
{
	/* disable channel */
278
	sh_cmt_start_stop_ch(ch, 0);
M
Magnus Damm 已提交
279

280
	/* disable interrupts in CMT block */
281
	sh_cmt_write_cmcsr(ch, 0);
282

283
	/* stop clock */
284
	clk_disable(ch->cmt->clk);
285

286 287
	dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
	pm_runtime_put(&ch->cmt->pdev->dev);
M
Magnus Damm 已提交
288 289 290 291 292 293 294 295 296
}

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

297
static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
M
Magnus Damm 已提交
298 299 300
					      int absolute)
{
	unsigned long new_match;
301
	unsigned long value = ch->next_match_value;
M
Magnus Damm 已提交
302 303 304 305
	unsigned long delay = 0;
	unsigned long now = 0;
	int has_wrapped;

306 307
	now = sh_cmt_get_counter(ch, &has_wrapped);
	ch->flags |= FLAG_REPROGRAM; /* force reprogram */
M
Magnus Damm 已提交
308 309 310 311 312 313

	if (has_wrapped) {
		/* we're competing with the interrupt handler.
		 *  -> let the interrupt handler reprogram the timer.
		 *  -> interrupt number two handles the event.
		 */
314
		ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
315 316 317 318 319 320 321 322 323 324 325
		return;
	}

	if (absolute)
		now = 0;

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

329
		sh_cmt_write_cmcor(ch, new_match);
M
Magnus Damm 已提交
330

331 332
		now = sh_cmt_get_counter(ch, &has_wrapped);
		if (has_wrapped && (new_match > ch->match_value)) {
M
Magnus Damm 已提交
333 334 335 336 337 338
			/* 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.
			 */
339
			ch->flags |= FLAG_SKIPEVENT;
M
Magnus Damm 已提交
340 341 342 343 344 345 346 347 348 349
			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.
			 */
350
			ch->match_value = new_match;
M
Magnus Damm 已提交
351 352 353 354 355 356 357 358 359 360
			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.
			 */
361
			ch->match_value = new_match;
M
Magnus Damm 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
			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)
377 378
			dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
				 ch->index);
M
Magnus Damm 已提交
379 380 381 382

	} while (delay);
}

383
static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
M
Magnus Damm 已提交
384
{
385
	if (delta > ch->max_match_value)
386 387
		dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
			 ch->index);
M
Magnus Damm 已提交
388

389 390
	ch->next_match_value = delta;
	sh_cmt_clock_event_program_verify(ch, 0);
391 392
}

393
static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
394 395 396
{
	unsigned long flags;

397 398 399
	raw_spin_lock_irqsave(&ch->lock, flags);
	__sh_cmt_set_next(ch, delta);
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
400 401 402 403
}

static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
{
404
	struct sh_cmt_channel *ch = dev_id;
M
Magnus Damm 已提交
405 406

	/* clear flags */
407
	sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) & ch->cmt->clear_bits);
M
Magnus Damm 已提交
408 409 410 411 412

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

416 417
	if (!(ch->flags & FLAG_REPROGRAM))
		ch->next_match_value = ch->max_match_value;
M
Magnus Damm 已提交
418

419
	ch->flags |= FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
420

421 422 423 424 425
	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 已提交
426 427
			}

428
			ch->ced.event_handler(&ch->ced);
M
Magnus Damm 已提交
429 430 431
		}
	}

432
	ch->flags &= ~FLAG_SKIPEVENT;
M
Magnus Damm 已提交
433

434 435 436
	if (ch->flags & FLAG_REPROGRAM) {
		ch->flags &= ~FLAG_REPROGRAM;
		sh_cmt_clock_event_program_verify(ch, 1);
M
Magnus Damm 已提交
437

438 439 440 441
		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 已提交
442 443
	}

444
	ch->flags &= ~FLAG_IRQCONTEXT;
M
Magnus Damm 已提交
445 446 447 448

	return IRQ_HANDLED;
}

449
static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
450 451 452 453
{
	int ret = 0;
	unsigned long flags;

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

456 457
	if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		ret = sh_cmt_enable(ch, &ch->rate);
M
Magnus Damm 已提交
458 459 460

	if (ret)
		goto out;
461
	ch->flags |= flag;
M
Magnus Damm 已提交
462 463

	/* setup timeout if no clockevent */
464 465
	if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
		__sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
466
 out:
467
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
468 469 470 471

	return ret;
}

472
static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
M
Magnus Damm 已提交
473 474 475 476
{
	unsigned long flags;
	unsigned long f;

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

479 480
	f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
	ch->flags &= ~flag;
M
Magnus Damm 已提交
481

482 483
	if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
		sh_cmt_disable(ch);
M
Magnus Damm 已提交
484 485

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

489
	raw_spin_unlock_irqrestore(&ch->lock, flags);
M
Magnus Damm 已提交
490 491
}

492
static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
493
{
494
	return container_of(cs, struct sh_cmt_channel, cs);
495 496 497 498
}

static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
{
499
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
500 501 502 503
	unsigned long flags, raw;
	unsigned long value;
	int has_wrapped;

504 505 506
	raw_spin_lock_irqsave(&ch->lock, flags);
	value = ch->total_cycles;
	raw = sh_cmt_get_counter(ch, &has_wrapped);
507 508

	if (unlikely(has_wrapped))
509 510
		raw += ch->match_value + 1;
	raw_spin_unlock_irqrestore(&ch->lock, flags);
511 512 513 514 515 516

	return value + raw;
}

static int sh_cmt_clocksource_enable(struct clocksource *cs)
{
517
	int ret;
518
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
519

520
	WARN_ON(ch->cs_enabled);
521

522
	ch->total_cycles = 0;
523

524
	ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
525
	if (!ret) {
526 527
		__clocksource_updatefreq_hz(cs, ch->rate);
		ch->cs_enabled = true;
528
	}
529
	return ret;
530 531 532 533
}

static void sh_cmt_clocksource_disable(struct clocksource *cs)
{
534
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
535

536
	WARN_ON(!ch->cs_enabled);
537

538 539
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	ch->cs_enabled = false;
540 541
}

542 543
static void sh_cmt_clocksource_suspend(struct clocksource *cs)
{
544
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
545

546 547
	sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
548 549
}

550 551
static void sh_cmt_clocksource_resume(struct clocksource *cs)
{
552
	struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
553

554 555
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
	sh_cmt_start(ch, FLAG_CLOCKSOURCE);
556 557
}

558
static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
559
				       const char *name, unsigned long rating)
560
{
561
	struct clocksource *cs = &ch->cs;
562 563 564 565 566 567

	cs->name = name;
	cs->rating = rating;
	cs->read = sh_cmt_clocksource_read;
	cs->enable = sh_cmt_clocksource_enable;
	cs->disable = sh_cmt_clocksource_disable;
568
	cs->suspend = sh_cmt_clocksource_suspend;
569
	cs->resume = sh_cmt_clocksource_resume;
570 571
	cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
	cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
572

573 574
	dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
		 ch->index);
575

576 577
	/* Register with dummy 1 Hz value, gets updated in ->enable() */
	clocksource_register_hz(cs, 1);
578 579 580
	return 0;
}

581
static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
M
Magnus Damm 已提交
582
{
583
	return container_of(ced, struct sh_cmt_channel, ced);
M
Magnus Damm 已提交
584 585
}

586
static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
M
Magnus Damm 已提交
587
{
588
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
589

590
	sh_cmt_start(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
591 592 593 594

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

	ced->shift = 32;
595 596
	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 已提交
597 598 599
	ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);

	if (periodic)
600
		sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1);
M
Magnus Damm 已提交
601
	else
602
		sh_cmt_set_next(ch, ch->max_match_value);
M
Magnus Damm 已提交
603 604 605 606 607
}

static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
				    struct clock_event_device *ced)
{
608
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
M
Magnus Damm 已提交
609 610 611 612 613

	/* deal with old setting first */
	switch (ced->mode) {
	case CLOCK_EVT_MODE_PERIODIC:
	case CLOCK_EVT_MODE_ONESHOT:
614
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
615 616 617 618 619 620 621
		break;
	default:
		break;
	}

	switch (mode) {
	case CLOCK_EVT_MODE_PERIODIC:
622
		dev_info(&ch->cmt->pdev->dev,
623
			 "ch%u: used for periodic clock events\n", ch->index);
624
		sh_cmt_clock_event_start(ch, 1);
M
Magnus Damm 已提交
625 626
		break;
	case CLOCK_EVT_MODE_ONESHOT:
627
		dev_info(&ch->cmt->pdev->dev,
628
			 "ch%u: used for oneshot clock events\n", ch->index);
629
		sh_cmt_clock_event_start(ch, 0);
M
Magnus Damm 已提交
630 631 632
		break;
	case CLOCK_EVT_MODE_SHUTDOWN:
	case CLOCK_EVT_MODE_UNUSED:
633
		sh_cmt_stop(ch, FLAG_CLOCKEVENT);
M
Magnus Damm 已提交
634 635 636 637 638 639 640 641 642
		break;
	default:
		break;
	}
}

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

	BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
646 647
	if (likely(ch->flags & FLAG_IRQCONTEXT))
		ch->next_match_value = delta - 1;
M
Magnus Damm 已提交
648
	else
649
		sh_cmt_set_next(ch, delta - 1);
M
Magnus Damm 已提交
650 651 652 653

	return 0;
}

654 655
static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
{
656
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
657

658 659
	pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
	clk_unprepare(ch->cmt->clk);
660 661 662 663
}

static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
{
664
	struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
665

666 667
	clk_prepare(ch->cmt->clk);
	pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
668 669
}

670
static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
671
				       const char *name, unsigned long rating)
M
Magnus Damm 已提交
672
{
673
	struct clock_event_device *ced = &ch->ced;
M
Magnus Damm 已提交
674 675 676 677 678 679 680 681 682 683

	memset(ced, 0, sizeof(*ced));

	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;
684 685
	ced->suspend = sh_cmt_clock_event_suspend;
	ced->resume = sh_cmt_clock_event_resume;
M
Magnus Damm 已提交
686

687 688
	dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
		 ch->index);
M
Magnus Damm 已提交
689 690 691
	clockevents_register_device(ced);
}

692
static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
693 694
			   unsigned long clockevent_rating,
			   unsigned long clocksource_rating)
M
Magnus Damm 已提交
695 696
{
	if (clockevent_rating)
697
		sh_cmt_register_clockevent(ch, name, clockevent_rating);
M
Magnus Damm 已提交
698

699
	if (clocksource_rating)
700
		sh_cmt_register_clocksource(ch, name, clocksource_rating);
701

M
Magnus Damm 已提交
702 703 704
	return 0;
}

705
static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
706 707 708 709 710 711 712 713
				struct sh_cmt_device *cmt)
{
	struct sh_timer_config *cfg = cmt->pdev->dev.platform_data;
	int irq;
	int ret;

	memset(ch, 0, sizeof(*ch));
	ch->cmt = cmt;
714
	ch->base = cmt->mapbase_ch;
715
	ch->index = index;
716 717 718

	irq = platform_get_irq(cmt->pdev, 0);
	if (irq < 0) {
719 720
		dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n",
			ch->index);
721 722 723 724 725 726 727 728 729 730 731
		return irq;
	}

	if (cmt->width == (sizeof(ch->max_match_value) * 8))
		ch->max_match_value = ~0;
	else
		ch->max_match_value = (1 << cmt->width) - 1;

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

732
	ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
733 734 735
			      cfg->clockevent_rating,
			      cfg->clocksource_rating);
	if (ret) {
736 737
		dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
			ch->index);
738 739 740 741 742 743 744 745
		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) {
746 747
		dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n",
			ch->index, irq);
748 749 750 751 752 753
		return ret;
	}

	return 0;
}

754
static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
M
Magnus Damm 已提交
755
{
756
	struct sh_timer_config *cfg = pdev->dev.platform_data;
757
	struct resource *res, *res2;
758
	int ret;
M
Magnus Damm 已提交
759 760
	ret = -ENXIO;

761 762
	memset(cmt, 0, sizeof(*cmt));
	cmt->pdev = pdev;
M
Magnus Damm 已提交
763 764

	if (!cfg) {
765
		dev_err(&cmt->pdev->dev, "missing platform data\n");
M
Magnus Damm 已提交
766 767 768
		goto err0;
	}

769
	res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
M
Magnus Damm 已提交
770
	if (!res) {
771
		dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
M
Magnus Damm 已提交
772 773 774
		goto err0;
	}

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

778 779 780
	/* map memory, let mapbase_ch point to our channel */
	cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res));
	if (cmt->mapbase_ch == NULL) {
781
		dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
M
Magnus Damm 已提交
782 783 784
		goto err0;
	}

785
	/* map second resource for CMSTR */
786 787 788 789
	cmt->mapbase = ioremap_nocache(res2 ? res2->start :
				       res->start - cfg->channel_offset,
				       res2 ? resource_size(res2) : 2);
	if (cmt->mapbase == NULL) {
790
		dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n");
791 792 793
		goto err1;
	}

M
Magnus Damm 已提交
794
	/* get hold of clock */
795 796 797 798
	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);
799
		goto err2;
M
Magnus Damm 已提交
800 801
	}

802
	ret = clk_prepare(cmt->clk);
803 804 805
	if (ret < 0)
		goto err3;

806 807
	if (res2 && (resource_size(res2) == 4)) {
		/* assume both CMSTR and CMCSR to be 32-bit */
808 809
		cmt->read_control = sh_cmt_read32;
		cmt->write_control = sh_cmt_write32;
810
	} else {
811 812
		cmt->read_control = sh_cmt_read16;
		cmt->write_control = sh_cmt_write16;
813
	}
814

M
Magnus Damm 已提交
815
	if (resource_size(res) == 6) {
816 817 818 819 820
		cmt->width = 16;
		cmt->read_count = sh_cmt_read16;
		cmt->write_count = sh_cmt_write16;
		cmt->overflow_bit = 0x80;
		cmt->clear_bits = ~0x80;
M
Magnus Damm 已提交
821
	} else {
822 823 824 825 826
		cmt->width = 32;
		cmt->read_count = sh_cmt_read32;
		cmt->write_count = sh_cmt_write32;
		cmt->overflow_bit = 0x8000;
		cmt->clear_bits = ~0xc000;
M
Magnus Damm 已提交
827 828
	}

829
	ret = sh_cmt_setup_channel(&cmt->channel, cfg->timer_bit, cmt);
830
	if (ret < 0)
831
		goto err4;
832

833
	platform_set_drvdata(pdev, cmt);
834

835
	return 0;
836
err4:
837
	clk_unprepare(cmt->clk);
838
err3:
839
	clk_put(cmt->clk);
840
err2:
841
	iounmap(cmt->mapbase);
842 843
err1:
	iounmap(cmt->mapbase_ch);
844
err0:
M
Magnus Damm 已提交
845 846 847
	return ret;
}

848
static int sh_cmt_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
849
{
850
	struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
851
	struct sh_timer_config *cfg = pdev->dev.platform_data;
M
Magnus Damm 已提交
852 853
	int ret;

854
	if (!is_early_platform_device(pdev)) {
855 856
		pm_runtime_set_active(&pdev->dev);
		pm_runtime_enable(&pdev->dev);
857
	}
858

859
	if (cmt) {
860
		dev_info(&pdev->dev, "kept as earlytimer\n");
861
		goto out;
862 863
	}

864 865
	cmt = kmalloc(sizeof(*cmt), GFP_KERNEL);
	if (cmt == NULL) {
M
Magnus Damm 已提交
866 867 868 869
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		return -ENOMEM;
	}

870
	ret = sh_cmt_setup(cmt, pdev);
M
Magnus Damm 已提交
871
	if (ret) {
872
		kfree(cmt);
873 874
		pm_runtime_idle(&pdev->dev);
		return ret;
M
Magnus Damm 已提交
875
	}
876 877 878 879 880 881 882 883 884 885
	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 已提交
886 887
}

888
static int sh_cmt_remove(struct platform_device *pdev)
M
Magnus Damm 已提交
889 890 891 892 893 894
{
	return -EBUSY; /* cannot unregister clockevent and clocksource */
}

static struct platform_driver sh_cmt_device_driver = {
	.probe		= sh_cmt_probe,
895
	.remove		= sh_cmt_remove,
M
Magnus Damm 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
	.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);
}

911
early_platform_init("earlytimer", &sh_cmt_device_driver);
912
subsys_initcall(sh_cmt_init);
M
Magnus Damm 已提交
913 914 915 916 917
module_exit(sh_cmt_exit);

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