dmtimer.c 18.1 KB
Newer Older
1 2 3 4 5 6
/*
 * linux/arch/arm/plat-omap/dmtimer.c
 *
 * OMAP Dual-Mode Timers
 *
 * Copyright (C) 2005 Nokia Corporation
7 8
 * OMAP2 support by Juha Yrjola
 * API improvements and OMAP2 clock framework support by Timo Teras
9
 *
10 11 12
 * Copyright (C) 2009 Texas Instruments
 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
 *
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * 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, or (at your
 * option) any later version.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * 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.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
33 34 35 36 37
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/clk.h>
#include <linux/delay.h>
38
#include <linux/io.h>
39
#include <linux/module.h>
40
#include <mach/hardware.h>
41
#include <plat/dmtimer.h>
42
#include <mach/irqs.h>
43

44
static int dm_timer_count;
45

46
#ifdef CONFIG_ARCH_OMAP1
47
static struct omap_dm_timer omap1_dm_timers[] = {
48 49 50 51 52 53
	{ .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
	{ .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
	{ .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
	{ .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
	{ .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
	{ .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
54 55
	{ .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
	{ .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
56
};
57

58
static const int omap1_dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
59

60
#else
61
#define omap1_dm_timers			NULL
62 63
#define omap1_dm_timer_count		0
#endif	/* CONFIG_ARCH_OMAP1 */
64

65
#ifdef CONFIG_ARCH_OMAP2
66
static struct omap_dm_timer omap2_dm_timers[] = {
67 68 69 70 71 72 73 74 75 76 77 78
	{ .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
	{ .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
	{ .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
	{ .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
	{ .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
	{ .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
	{ .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
	{ .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
	{ .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
	{ .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
	{ .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
	{ .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
79 80
};

81
static const char *omap2_dm_source_names[] __initdata = {
T
Timo Teras 已提交
82 83
	"sys_ck",
	"func_32k_ck",
84 85
	"alt_ck",
	NULL
T
Timo Teras 已提交
86 87
};

88
static struct clk *omap2_dm_source_clocks[3];
89
static const int omap2_dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
90

91
#else
92
#define omap2_dm_timers			NULL
93
#define omap2_dm_timer_count		0
94 95
#define omap2_dm_source_names		NULL
#define omap2_dm_source_clocks		NULL
96
#endif	/* CONFIG_ARCH_OMAP2 */
97

98
#ifdef CONFIG_ARCH_OMAP3
99 100 101 102 103 104 105 106 107 108 109 110
static struct omap_dm_timer omap3_dm_timers[] = {
	{ .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
	{ .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
	{ .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
	{ .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
	{ .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
	{ .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
	{ .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
	{ .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
	{ .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
	{ .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
	{ .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
P
Paul Walmsley 已提交
111
	{ .phys_base = 0x48304000, .irq = INT_34XX_GPT12_IRQ },
112 113 114 115 116 117 118 119
};

static const char *omap3_dm_source_names[] __initdata = {
	"sys_ck",
	"omap_32k_fck",
	NULL
};

120
static struct clk *omap3_dm_source_clocks[2];
121
static const int omap3_dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
122

123
#else
124
#define omap3_dm_timers			NULL
125
#define omap3_dm_timer_count		0
126 127
#define omap3_dm_source_names		NULL
#define omap3_dm_source_clocks		NULL
128
#endif	/* CONFIG_ARCH_OMAP3 */
129

130
#ifdef CONFIG_ARCH_OMAP4
131
static struct omap_dm_timer omap4_dm_timers[] = {
132 133 134 135 136 137 138 139 140 141 142 143
	{ .phys_base = 0x4a318000, .irq = OMAP44XX_IRQ_GPT1 },
	{ .phys_base = 0x48032000, .irq = OMAP44XX_IRQ_GPT2 },
	{ .phys_base = 0x48034000, .irq = OMAP44XX_IRQ_GPT3 },
	{ .phys_base = 0x48036000, .irq = OMAP44XX_IRQ_GPT4 },
	{ .phys_base = 0x40138000, .irq = OMAP44XX_IRQ_GPT5 },
	{ .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT6 },
	{ .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT7 },
	{ .phys_base = 0x4013e000, .irq = OMAP44XX_IRQ_GPT8 },
	{ .phys_base = 0x4803e000, .irq = OMAP44XX_IRQ_GPT9 },
	{ .phys_base = 0x48086000, .irq = OMAP44XX_IRQ_GPT10 },
	{ .phys_base = 0x48088000, .irq = OMAP44XX_IRQ_GPT11 },
	{ .phys_base = 0x4a320000, .irq = OMAP44XX_IRQ_GPT12 },
144 145
};
static const char *omap4_dm_source_names[] __initdata = {
146 147
	"sys_clkin_ck",
	"sys_32k_ck",
148 149 150
	NULL
};
static struct clk *omap4_dm_source_clocks[2];
151
static const int omap4_dm_timer_count = ARRAY_SIZE(omap4_dm_timers);
152

153
#else
154 155 156 157 158
#define omap4_dm_timers			NULL
#define omap4_dm_timer_count		0
#define omap4_dm_source_names		NULL
#define omap4_dm_source_clocks		NULL
#endif	/* CONFIG_ARCH_OMAP4 */
159

160
static struct omap_dm_timer *dm_timers;
161
static const char **dm_source_names;
162 163
static struct clk **dm_source_clocks;

164 165
static spinlock_t dm_timer_lock;

166 167 168 169 170 171
/*
 * Reads timer registers in posted and non-posted mode. The posted mode bit
 * is encoded in reg. Note that in posted mode write pending bit must be
 * checked. Otherwise a read of a non completed write will produce an error.
 */
static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
172
{
173 174 175 176 177
	if (timer->posted)
		while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
				& (reg >> WPSHIFT))
			cpu_relax();
	return readl(timer->io_base + (reg & 0xff));
178
}
179

180 181 182 183 184 185 186 187
/*
 * Writes timer registers in posted and non-posted mode. The posted mode bit
 * is encoded in reg. Note that in posted mode the write pending bit must be
 * checked. Otherwise a write on a register which has a pending write will be
 * lost.
 */
static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
						u32 value)
188
{
189 190 191 192 193
	if (timer->posted)
		while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
				& (reg >> WPSHIFT))
			cpu_relax();
	writel(value, timer->io_base + (reg & 0xff));
194 195
}

196
static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
197
{
198 199 200 201 202 203 204 205 206 207
	int c;

	c = 0;
	while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
		c++;
		if (c > 100000) {
			printk(KERN_ERR "Timer failed to reset\n");
			return;
		}
	}
208 209
}

210 211 212 213
static void omap_dm_timer_reset(struct omap_dm_timer *timer)
{
	u32 l;

214
	if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
215 216 217
		omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
		omap_dm_timer_wait_for_reset(timer);
	}
218
	omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
219 220

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
221 222 223
	l |= 0x02 << 3;  /* Set to smart-idle mode */
	l |= 0x2 << 8;   /* Set clock activity to perserve f-clock on idle */

T
Tero Kristo 已提交
224 225 226 227
	/* Enable autoidle on OMAP2 / OMAP3 */
	if (cpu_is_omap24xx() || cpu_is_omap34xx())
		l |= 0x1 << 0;

228
	/*
229
	 * Enable wake-up on OMAP2 CPUs.
230
	 */
231
	if (cpu_class_is_omap2())
232
		l |= 1 << 2;
233
	omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
234 235 236 237 238

	/* Match hardware reset default of posted mode */
	omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
			OMAP_TIMER_CTRL_POSTED);
	timer->posted = 1;
239 240
}

T
Timo Teras 已提交
241
static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
242
{
243
	omap_dm_timer_enable(timer);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	omap_dm_timer_reset(timer);
}

struct omap_dm_timer *omap_dm_timer_request(void)
{
	struct omap_dm_timer *timer = NULL;
	unsigned long flags;
	int i;

	spin_lock_irqsave(&dm_timer_lock, flags);
	for (i = 0; i < dm_timer_count; i++) {
		if (dm_timers[i].reserved)
			continue;

		timer = &dm_timers[i];
T
Timo Teras 已提交
259
		timer->reserved = 1;
260 261 262 263
		break;
	}
	spin_unlock_irqrestore(&dm_timer_lock, flags);

T
Timo Teras 已提交
264 265 266
	if (timer != NULL)
		omap_dm_timer_prepare(timer);

267 268
	return timer;
}
269
EXPORT_SYMBOL_GPL(omap_dm_timer_request);
270 271

struct omap_dm_timer *omap_dm_timer_request_specific(int id)
272 273
{
	struct omap_dm_timer *timer;
274
	unsigned long flags;
275

276 277 278 279
	spin_lock_irqsave(&dm_timer_lock, flags);
	if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
		spin_unlock_irqrestore(&dm_timer_lock, flags);
		printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
280
		       __FILE__, __LINE__, __func__, id);
281 282 283
		dump_stack();
		return NULL;
	}
284

285
	timer = &dm_timers[id-1];
T
Timo Teras 已提交
286
	timer->reserved = 1;
287 288
	spin_unlock_irqrestore(&dm_timer_lock, flags);

T
Timo Teras 已提交
289 290
	omap_dm_timer_prepare(timer);

291
	return timer;
292
}
293
EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
294

295 296
void omap_dm_timer_free(struct omap_dm_timer *timer)
{
297
	omap_dm_timer_enable(timer);
298
	omap_dm_timer_reset(timer);
299
	omap_dm_timer_disable(timer);
300

301 302 303
	WARN_ON(!timer->reserved);
	timer->reserved = 0;
}
304
EXPORT_SYMBOL_GPL(omap_dm_timer_free);
305

306 307 308 309 310
void omap_dm_timer_enable(struct omap_dm_timer *timer)
{
	if (timer->enabled)
		return;

311 312 313 314 315 316
#ifdef CONFIG_ARCH_OMAP2PLUS
	if (cpu_class_is_omap2()) {
		clk_enable(timer->fclk);
		clk_enable(timer->iclk);
	}
#endif
317 318 319

	timer->enabled = 1;
}
320
EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
321 322 323 324 325 326

void omap_dm_timer_disable(struct omap_dm_timer *timer)
{
	if (!timer->enabled)
		return;

327 328 329 330 331 332
#ifdef CONFIG_ARCH_OMAP2PLUS
	if (cpu_class_is_omap2()) {
		clk_disable(timer->iclk);
		clk_disable(timer->fclk);
	}
#endif
333 334 335

	timer->enabled = 0;
}
336
EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
337

338 339 340 341
int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
{
	return timer->irq;
}
342
EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
343 344 345

#if defined(CONFIG_ARCH_OMAP1)

346 347 348 349 350 351
/**
 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
 * @inputmask: current value of idlect mask
 */
__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
{
352
	int i;
353 354 355 356 357 358

	/* If ARMXOR cannot be idled this function call is unnecessary */
	if (!(inputmask & (1 << 1)))
		return inputmask;

	/* If any active timer is using ARMXOR return modified mask */
359 360 361
	for (i = 0; i < dm_timer_count; i++) {
		u32 l;

362
		l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
363 364
		if (l & OMAP_TIMER_CTRL_ST) {
			if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
365 366 367 368
				inputmask &= ~(1 << 1);
			else
				inputmask &= ~(1 << 2);
		}
369
	}
370 371 372

	return inputmask;
}
373
EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
374

375
#else
376

377
struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
378
{
379
	return timer->fclk;
380
}
381
EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
382

383 384 385
__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
{
	BUG();
386 387

	return 0;
388
}
389
EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
390

391
#endif
392

393
void omap_dm_timer_trigger(struct omap_dm_timer *timer)
394
{
395
	omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
396
}
397
EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
398

399 400 401
void omap_dm_timer_start(struct omap_dm_timer *timer)
{
	u32 l;
402

403 404 405 406 407 408
	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
	if (!(l & OMAP_TIMER_CTRL_ST)) {
		l |= OMAP_TIMER_CTRL_ST;
		omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
	}
}
409
EXPORT_SYMBOL_GPL(omap_dm_timer_start);
410

411
void omap_dm_timer_stop(struct omap_dm_timer *timer)
412
{
413
	u32 l;
414

415 416 417 418
	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
	if (l & OMAP_TIMER_CTRL_ST) {
		l &= ~0x1;
		omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
419
#ifdef CONFIG_ARCH_OMAP2PLUS
420 421 422 423 424 425 426 427
		/* Readback to make sure write has completed */
		omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
		 /*
		  * Wait for functional clock period x 3.5 to make sure that
		  * timer is stopped
		  */
		udelay(3500000 / clk_get_rate(timer->fclk) + 1);
#endif
428
	}
429 430 431
	/* Ack possibly pending interrupt */
	omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG,
			OMAP_TIMER_INT_OVERFLOW);
432
}
433
EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
434

435
#ifdef CONFIG_ARCH_OMAP1
436

437
int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
438
{
439 440
	int n = (timer - dm_timers) << 1;
	u32 l;
441

442 443 444
	l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
	l |= source << n;
	omap_writel(l, MOD_CONF_CTRL_1);
445 446

	return 0;
447
}
448
EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
449

450
#else
451

452
int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
453
{
454 455
	int ret = -EINVAL;

456
	if (source < 0 || source >= 3)
457
		return -EINVAL;
458 459

	clk_disable(timer->fclk);
460
	ret = clk_set_parent(timer->fclk, dm_source_clocks[source]);
461 462
	clk_enable(timer->fclk);

463 464 465 466
	/*
	 * When the functional clock disappears, too quick writes seem
	 * to cause an abort. XXX Is this still necessary?
	 */
467
	__delay(300000);
468 469

	return ret;
470
}
471
EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
472

473
#endif
474

475 476
void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
			    unsigned int load)
477 478
{
	u32 l;
479

480
	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
481 482 483 484
	if (autoreload)
		l |= OMAP_TIMER_CTRL_AR;
	else
		l &= ~OMAP_TIMER_CTRL_AR;
485
	omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
486
	omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
487

488
	omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
489
}
490
EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
491

492 493 494 495 496 497 498
/* Optimized set_load which removes costly spin wait in timer_start */
void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
                            unsigned int load)
{
	u32 l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
499
	if (autoreload) {
500
		l |= OMAP_TIMER_CTRL_AR;
501 502
		omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
	} else {
503
		l &= ~OMAP_TIMER_CTRL_AR;
504
	}
505 506 507 508 509
	l |= OMAP_TIMER_CTRL_ST;

	omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
	omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
}
510
EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
511

512 513
void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
			     unsigned int match)
514 515 516 517
{
	u32 l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
T
Timo Teras 已提交
518
	if (enable)
519 520 521
		l |= OMAP_TIMER_CTRL_CE;
	else
		l &= ~OMAP_TIMER_CTRL_CE;
522
	omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
523
	omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
524
}
525
EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
526

527 528
void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
			   int toggle, int trigger)
529 530 531 532
{
	u32 l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
533 534 535 536 537 538 539
	l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
	       OMAP_TIMER_CTRL_PT | (0x03 << 10));
	if (def_on)
		l |= OMAP_TIMER_CTRL_SCPWM;
	if (toggle)
		l |= OMAP_TIMER_CTRL_PT;
	l |= trigger << 10;
540 541
	omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
}
542
EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
543

544
void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
545 546 547 548
{
	u32 l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
549 550 551 552 553
	l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
	if (prescaler >= 0x00 && prescaler <= 0x07) {
		l |= OMAP_TIMER_CTRL_PRE;
		l |= prescaler << 2;
	}
554 555
	omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
}
556
EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
557

558 559
void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
				  unsigned int value)
560
{
561
	omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
562
	omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
563
}
564
EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
565

566
unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
567
{
568 569 570 571 572
	unsigned int l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);

	return l;
573
}
574
EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
575

576
void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
577
{
578
	omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
579
}
580
EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
581

582
unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
583
{
584 585 586 587 588
	unsigned int l;

	l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);

	return l;
589
}
590
EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
591

T
Timo Teras 已提交
592 593
void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
{
594
	omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
T
Timo Teras 已提交
595
}
596
EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
T
Timo Teras 已提交
597

598
int omap_dm_timers_active(void)
599
{
600
	int i;
601

602 603
	for (i = 0; i < dm_timer_count; i++) {
		struct omap_dm_timer *timer;
604

605
		timer = &dm_timers[i];
606 607 608 609

		if (!timer->enabled)
			continue;

610
		if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
611
		    OMAP_TIMER_CTRL_ST) {
612
			return 1;
613
		}
614 615 616
	}
	return 0;
}
617
EXPORT_SYMBOL_GPL(omap_dm_timers_active);
618

619
int __init omap_dm_timer_init(void)
620 621
{
	struct omap_dm_timer *timer;
T
Tony Lindgren 已提交
622
	int i, map_size = SZ_8K;	/* Module 4KB + L4 4KB except on omap1 */
623

624
	if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
625
		return -ENODEV;
626 627

	spin_lock_init(&dm_timer_lock);
628

T
Tony Lindgren 已提交
629
	if (cpu_class_is_omap1()) {
630
		dm_timers = omap1_dm_timers;
631
		dm_timer_count = omap1_dm_timer_count;
T
Tony Lindgren 已提交
632 633
		map_size = SZ_2K;
	} else if (cpu_is_omap24xx()) {
634
		dm_timers = omap2_dm_timers;
635
		dm_timer_count = omap2_dm_timer_count;
636 637
		dm_source_names = omap2_dm_source_names;
		dm_source_clocks = omap2_dm_source_clocks;
638 639
	} else if (cpu_is_omap34xx()) {
		dm_timers = omap3_dm_timers;
640
		dm_timer_count = omap3_dm_timer_count;
641 642
		dm_source_names = omap3_dm_source_names;
		dm_source_clocks = omap3_dm_source_clocks;
643 644
	} else if (cpu_is_omap44xx()) {
		dm_timers = omap4_dm_timers;
645
		dm_timer_count = omap4_dm_timer_count;
646 647
		dm_source_names = omap4_dm_source_names;
		dm_source_clocks = omap4_dm_source_clocks;
T
Timo Teras 已提交
648
	}
649 650 651 652 653

	if (cpu_class_is_omap2())
		for (i = 0; dm_source_names[i] != NULL; i++)
			dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);

654 655
	if (cpu_is_omap243x())
		dm_timers[0].phys_base = 0x49018000;
T
Timo Teras 已提交
656

657 658
	for (i = 0; i < dm_timer_count; i++) {
		timer = &dm_timers[i];
T
Tony Lindgren 已提交
659 660 661 662 663

		/* Static mapping, never released */
		timer->io_base = ioremap(timer->phys_base, map_size);
		BUG_ON(!timer->io_base);

664
#ifdef CONFIG_ARCH_OMAP2PLUS
665 666 667 668 669 670 671
		if (cpu_class_is_omap2()) {
			char clk_name[16];
			sprintf(clk_name, "gpt%d_ick", i + 1);
			timer->iclk = clk_get(NULL, clk_name);
			sprintf(clk_name, "gpt%d_fck", i + 1);
			timer->fclk = clk_get(NULL, clk_name);
		}
672
#endif
673 674 675 676
	}

	return 0;
}