timer-ti-dm.c 27.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6
/*
 * linux/arch/arm/plat-omap/dmtimer.c
 *
 * OMAP Dual-Mode Timers
 *
7
 * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com/
8 9 10 11 12
 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
 * Thara Gopinath <thara@ti.com>
 *
 * dmtimer adaptation to platform_driver.
 *
13
 * Copyright (C) 2005 Nokia Corporation
14 15
 * OMAP2 support by Juha Yrjola
 * API improvements and OMAP2 clock framework support by Timo Teras
16
 *
17 18
 * Copyright (C) 2009 Texas Instruments
 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
19 20
 */

21
#include <linux/clk.h>
22
#include <linux/clk-provider.h>
23
#include <linux/cpu_pm.h>
24
#include <linux/module.h>
25
#include <linux/io.h>
26
#include <linux/device.h>
27
#include <linux/err.h>
28
#include <linux/pm_runtime.h>
29 30
#include <linux/of.h>
#include <linux/of_device.h>
31 32
#include <linux/platform_device.h>
#include <linux/platform_data/dmtimer-omap.h>
33

34
#include <clocksource/timer-ti-dm.h>
35

36 37 38 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 86 87 88 89 90 91 92 93 94 95 96 97
/*
 * timer errata flags
 *
 * Errata i103/i767 impacts all OMAP3/4/5 devices including AM33xx. This
 * errata prevents us from using posted mode on these devices, unless the
 * timer counter register is never read. For more details please refer to
 * the OMAP3/4/5 errata documents.
 */
#define OMAP_TIMER_ERRATA_I103_I767			0x80000000

/* posted mode types */
#define OMAP_TIMER_NONPOSTED			0x00
#define OMAP_TIMER_POSTED			0x01

/* register offsets with the write pending bit encoded */
#define	WPSHIFT					16

#define OMAP_TIMER_WAKEUP_EN_REG		(_OMAP_TIMER_WAKEUP_EN_OFFSET \
							| (WP_NONE << WPSHIFT))

#define OMAP_TIMER_CTRL_REG			(_OMAP_TIMER_CTRL_OFFSET \
							| (WP_TCLR << WPSHIFT))

#define OMAP_TIMER_COUNTER_REG			(_OMAP_TIMER_COUNTER_OFFSET \
							| (WP_TCRR << WPSHIFT))

#define OMAP_TIMER_LOAD_REG			(_OMAP_TIMER_LOAD_OFFSET \
							| (WP_TLDR << WPSHIFT))

#define OMAP_TIMER_TRIGGER_REG			(_OMAP_TIMER_TRIGGER_OFFSET \
							| (WP_TTGR << WPSHIFT))

#define OMAP_TIMER_WRITE_PEND_REG		(_OMAP_TIMER_WRITE_PEND_OFFSET \
							| (WP_NONE << WPSHIFT))

#define OMAP_TIMER_MATCH_REG			(_OMAP_TIMER_MATCH_OFFSET \
							| (WP_TMAR << WPSHIFT))

#define OMAP_TIMER_CAPTURE_REG			(_OMAP_TIMER_CAPTURE_OFFSET \
							| (WP_NONE << WPSHIFT))

#define OMAP_TIMER_IF_CTRL_REG			(_OMAP_TIMER_IF_CTRL_OFFSET \
							| (WP_NONE << WPSHIFT))

#define OMAP_TIMER_CAPTURE2_REG			(_OMAP_TIMER_CAPTURE2_OFFSET \
							| (WP_NONE << WPSHIFT))

#define OMAP_TIMER_TICK_POS_REG			(_OMAP_TIMER_TICK_POS_OFFSET \
							| (WP_TPIR << WPSHIFT))

#define OMAP_TIMER_TICK_NEG_REG			(_OMAP_TIMER_TICK_NEG_OFFSET \
							| (WP_TNIR << WPSHIFT))

#define OMAP_TIMER_TICK_COUNT_REG		(_OMAP_TIMER_TICK_COUNT_OFFSET \
							| (WP_TCVR << WPSHIFT))

#define OMAP_TIMER_TICK_INT_MASK_SET_REG				\
		(_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))

#define OMAP_TIMER_TICK_INT_MASK_COUNT_REG				\
		(_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))

98
static u32 omap_reserved_systimers;
99
static LIST_HEAD(omap_timer_list);
100
static DEFINE_SPINLOCK(dm_timer_lock);
101

102 103 104 105 106 107 108
enum {
	REQUEST_ANY = 0,
	REQUEST_BY_ID,
	REQUEST_BY_CAP,
	REQUEST_BY_NODE,
};

109 110 111 112 113 114 115 116 117 118
/**
 * dmtimer_read - read timer registers in posted and non-posted mode
 * @timer:	timer pointer over which read operation to perform
 * @reg:	lowest byte holds the register offset
 *
 * 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 dmtimer_read(struct omap_dm_timer *timer, u32 reg)
119
{
120 121 122 123 124 125 126 127
	u16 wp, offset;

	wp = reg >> WPSHIFT;
	offset = reg & 0xff;

	/* Wait for a possible write pending bit in posted mode */
	if (wp && timer->posted)
		while (readl_relaxed(timer->pend) & wp)
128 129
			cpu_relax();

130
	return readl_relaxed(timer->func_base + offset);
131 132
}

133 134 135 136 137 138 139 140 141 142 143
/**
 * dmtimer_write - write timer registers in posted and non-posted mode
 * @timer:      timer pointer over which write operation is to perform
 * @reg:        lowest byte holds the register offset
 * @value:      data to write into the register
 *
 * 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 inline void dmtimer_write(struct omap_dm_timer *timer, u32 reg, u32 val)
144
{
145 146 147 148 149 150 151 152
	u16 wp, offset;

	wp = reg >> WPSHIFT;
	offset = reg & 0xff;

	/* Wait for a possible write pending bit in posted mode */
	if (wp && timer->posted)
		while (readl_relaxed(timer->pend) & wp)
153 154
			cpu_relax();

155
	writel_relaxed(val, timer->func_base + offset);
156 157 158 159 160 161 162 163 164 165
}

static inline void __omap_dm_timer_init_regs(struct omap_dm_timer *timer)
{
	u32 tidr;

	/* Assume v1 ip if bits [31:16] are zero */
	tidr = readl_relaxed(timer->io_base);
	if (!(tidr >> 16)) {
		timer->revision = 1;
166 167 168
		timer->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
		timer->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
		timer->irq_dis = OMAP_TIMER_V1_INT_EN_OFFSET;
169 170 171 172
		timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
		timer->func_base = timer->io_base;
	} else {
		timer->revision = 2;
173 174 175
		timer->irq_stat = OMAP_TIMER_V2_IRQSTATUS - OMAP_TIMER_V2_FUNC_OFFSET;
		timer->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET - OMAP_TIMER_V2_FUNC_OFFSET;
		timer->irq_dis = OMAP_TIMER_V2_IRQENABLE_CLR - OMAP_TIMER_V2_FUNC_OFFSET;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		timer->pend = timer->io_base +
			_OMAP_TIMER_WRITE_PEND_OFFSET +
				OMAP_TIMER_V2_FUNC_OFFSET;
		timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
	}
}

/*
 * __omap_dm_timer_enable_posted - enables write posted mode
 * @timer:      pointer to timer instance handle
 *
 * Enables the write posted mode for the timer. When posted mode is enabled
 * writes to certain timer registers are immediately acknowledged by the
 * internal bus and hence prevents stalling the CPU waiting for the write to
 * complete. Enabling this feature can improve performance for writing to the
 * timer registers.
 */
static inline void __omap_dm_timer_enable_posted(struct omap_dm_timer *timer)
{
	if (timer->posted)
		return;

	if (timer->errata & OMAP_TIMER_ERRATA_I103_I767) {
		timer->posted = OMAP_TIMER_NONPOSTED;
200
		dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0);
201 202 203
		return;
	}

204
	dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, OMAP_TIMER_CTRL_POSTED);
205 206 207 208 209
	timer->context.tsicr = OMAP_TIMER_CTRL_POSTED;
	timer->posted = OMAP_TIMER_POSTED;
}

static inline void __omap_dm_timer_stop(struct omap_dm_timer *timer,
210
					unsigned long rate)
211 212 213
{
	u32 l;

214
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
215 216
	if (l & OMAP_TIMER_CTRL_ST) {
		l &= ~0x1;
217
		dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
218 219
#ifdef CONFIG_ARCH_OMAP2PLUS
		/* Readback to make sure write has completed */
220
		dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
221 222 223 224 225 226 227 228 229
		/*
		 * Wait for functional clock period x 3.5 to make sure that
		 * timer is stopped
		 */
		udelay(3500000 / rate + 1);
#endif
	}

	/* Ack possibly pending interrupt */
230
	dmtimer_write(timer, timer->irq_stat, OMAP_TIMER_INT_OVERFLOW);
231 232 233 234 235
}

static inline void __omap_dm_timer_int_enable(struct omap_dm_timer *timer,
						unsigned int value)
{
236
	dmtimer_write(timer, timer->irq_ena, value);
237
	dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
238 239 240
}

static inline unsigned int
241
__omap_dm_timer_read_counter(struct omap_dm_timer *timer)
242
{
243
	return dmtimer_read(timer, OMAP_TIMER_COUNTER_REG);
244 245 246 247 248
}

static inline void __omap_dm_timer_write_status(struct omap_dm_timer *timer,
						unsigned int value)
{
249
	dmtimer_write(timer, timer->irq_stat, value);
250 251
}

252 253
static void omap_timer_restore_context(struct omap_dm_timer *timer)
{
254 255 256 257 258 259 260
	dmtimer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, timer->context.ocp_cfg);

	dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, timer->context.twer);
	dmtimer_write(timer, OMAP_TIMER_COUNTER_REG, timer->context.tcrr);
	dmtimer_write(timer, OMAP_TIMER_LOAD_REG, timer->context.tldr);
	dmtimer_write(timer, OMAP_TIMER_MATCH_REG, timer->context.tmar);
	dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, timer->context.tsicr);
261
	dmtimer_write(timer, timer->irq_ena, timer->context.tier);
262
	dmtimer_write(timer, OMAP_TIMER_CTRL_REG, timer->context.tclr);
263 264
}

265 266
static void omap_timer_save_context(struct omap_dm_timer *timer)
{
267 268 269 270 271 272
	timer->context.ocp_cfg = dmtimer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET);

	timer->context.tclr = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
	timer->context.twer = dmtimer_read(timer, OMAP_TIMER_WAKEUP_EN_REG);
	timer->context.tldr = dmtimer_read(timer, OMAP_TIMER_LOAD_REG);
	timer->context.tmar = dmtimer_read(timer, OMAP_TIMER_MATCH_REG);
273
	timer->context.tier = dmtimer_read(timer, timer->irq_ena);
274
	timer->context.tsicr = dmtimer_read(timer, OMAP_TIMER_IF_CTRL_REG);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

static int omap_timer_context_notifier(struct notifier_block *nb,
				       unsigned long cmd, void *v)
{
	struct omap_dm_timer *timer;

	timer = container_of(nb, struct omap_dm_timer, nb);

	switch (cmd) {
	case CPU_CLUSTER_PM_ENTER:
		if ((timer->capability & OMAP_TIMER_ALWON) ||
		    !atomic_read(&timer->enabled))
			break;
		omap_timer_save_context(timer);
		break;
291 292
	case CPU_CLUSTER_PM_ENTER_FAILED:	/* No need to restore context */
		break;
293 294 295 296 297 298 299 300 301 302 303
	case CPU_CLUSTER_PM_EXIT:
		if ((timer->capability & OMAP_TIMER_ALWON) ||
		    !atomic_read(&timer->enabled))
			break;
		omap_timer_restore_context(timer);
		break;
	}

	return NOTIFY_OK;
}

304
static int omap_dm_timer_reset(struct omap_dm_timer *timer)
305
{
306
	u32 l, timeout = 100000;
307

308 309
	if (timer->revision != 1)
		return -EINVAL;
310

311
	dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
312 313

	do {
314
		l = dmtimer_read(timer, OMAP_TIMER_V1_SYS_STAT_OFFSET);
315 316 317 318 319
	} while (!l && timeout--);

	if (!timeout) {
		dev_err(&timer->pdev->dev, "Timer failed to reset\n");
		return -ETIMEDOUT;
320
	}
321

322
	/* Configure timer for smart-idle mode */
323
	l = dmtimer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET);
324
	l |= 0x2 << 0x3;
325
	dmtimer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l);
326 327 328 329

	timer->posted = 0;

	return 0;
330 331
}

332 333 334
static int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
{
	int ret;
335
	const char *parent_name;
336 337 338
	struct clk *parent;
	struct dmtimer_platform_data *pdata;

339
	if (unlikely(!timer) || IS_ERR(timer->fclk))
340 341
		return -EINVAL;

342 343 344 345 346 347 348 349 350 351 352
	switch (source) {
	case OMAP_TIMER_SRC_SYS_CLK:
		parent_name = "timer_sys_ck";
		break;
	case OMAP_TIMER_SRC_32_KHZ:
		parent_name = "timer_32k_ck";
		break;
	case OMAP_TIMER_SRC_EXT_CLK:
		parent_name = "timer_ext_ck";
		break;
	default:
353
		return -EINVAL;
354 355 356
	}

	pdata = timer->pdev->dev.platform_data;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

	/*
	 * FIXME: Used for OMAP1 devices only because they do not currently
	 * use the clock framework to set the parent clock. To be removed
	 * once OMAP1 migrated to using clock framework for dmtimers
	 */
	if (pdata && pdata->set_timer_src)
		return pdata->set_timer_src(timer->pdev, source);

#if defined(CONFIG_COMMON_CLK)
	/* Check if the clock has configurable parents */
	if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
		return 0;
#endif

	parent = clk_get(&timer->pdev->dev, parent_name);
	if (IS_ERR(parent)) {
		pr_err("%s: %s not found\n", __func__, parent_name);
		return -EINVAL;
	}

	ret = clk_set_parent(timer->fclk, parent);
	if (ret < 0)
		pr_err("%s: failed to set %s as parent\n", __func__,
			parent_name);

	clk_put(parent);

	return ret;
}

static void omap_dm_timer_enable(struct omap_dm_timer *timer)
{
390 391 392 393 394 395
	struct device *dev = &timer->pdev->dev;
	int rc;

	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		dev_err(dev, "could not enable timer\n");
396 397 398 399
}

static void omap_dm_timer_disable(struct omap_dm_timer *timer)
{
400 401 402
	struct device *dev = &timer->pdev->dev;

	pm_runtime_put_sync(dev);
403 404
}

405
static int omap_dm_timer_prepare(struct omap_dm_timer *timer)
406
{
407
	struct device *dev = &timer->pdev->dev;
408 409
	int rc;

Jon Hunter's avatar
Jon Hunter committed
410 411 412 413 414 415
	/*
	 * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
	 * do not call clk_get() for these devices.
	 */
	if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
		timer->fclk = clk_get(&timer->pdev->dev, "fck");
416
		if (WARN_ON_ONCE(IS_ERR(timer->fclk))) {
Jon Hunter's avatar
Jon Hunter committed
417 418 419
			dev_err(&timer->pdev->dev, ": No fclk handle.\n");
			return -EINVAL;
		}
420 421
	}

422 423 424
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;
425

426 427 428
	if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
		rc = omap_dm_timer_reset(timer);
		if (rc) {
429
			pm_runtime_put_sync(dev);
430 431 432
			return rc;
		}
	}
433

434
	__omap_dm_timer_enable_posted(timer);
435
	pm_runtime_put_sync(dev);
436

437
	return 0;
438 439
}

440 441 442 443 444
static inline u32 omap_dm_timer_reserved_systimer(int id)
{
	return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
}

445
static struct omap_dm_timer *_omap_dm_timer_request(int req_type, void *data)
446
{
447
	struct omap_dm_timer *timer = NULL, *t;
448
	struct device_node *np = NULL;
449
	unsigned long flags;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	u32 cap = 0;
	int id = 0;

	switch (req_type) {
	case REQUEST_BY_ID:
		id = *(int *)data;
		break;
	case REQUEST_BY_CAP:
		cap = *(u32 *)data;
		break;
	case REQUEST_BY_NODE:
		np = (struct device_node *)data;
		break;
	default:
		/* REQUEST_ANY */
		break;
	}
467 468

	spin_lock_irqsave(&dm_timer_lock, flags);
469 470
	list_for_each_entry(t, &omap_timer_list, node) {
		if (t->reserved)
471 472
			continue;

473 474 475 476 477 478 479 480 481 482 483 484
		switch (req_type) {
		case REQUEST_BY_ID:
			if (id == t->pdev->id) {
				timer = t;
				timer->reserved = 1;
				goto found;
			}
			break;
		case REQUEST_BY_CAP:
			if (cap == (t->capability & cap)) {
				/*
				 * If timer is not NULL, we have already found
485 486
				 * one timer. But it was not an exact match
				 * because it had more capabilities than what
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
				 * was required. Therefore, unreserve the last
				 * timer found and see if this one is a better
				 * match.
				 */
				if (timer)
					timer->reserved = 0;
				timer = t;
				timer->reserved = 1;

				/* Exit loop early if we find an exact match */
				if (t->capability == cap)
					goto found;
			}
			break;
		case REQUEST_BY_NODE:
			if (np == t->pdev->dev.of_node) {
				timer = t;
				timer->reserved = 1;
				goto found;
			}
			break;
		default:
			/* REQUEST_ANY */
			timer = t;
			timer->reserved = 1;
			goto found;
		}
514
	}
515
found:
516
	spin_unlock_irqrestore(&dm_timer_lock, flags);
517

518 519 520
	if (timer && omap_dm_timer_prepare(timer)) {
		timer->reserved = 0;
		timer = NULL;
521
	}
522

523 524
	if (!timer)
		pr_debug("%s: timer request failed!\n", __func__);
Timo Teras's avatar
Timo Teras committed
525

526 527
	return timer;
}
528

529
static struct omap_dm_timer *omap_dm_timer_request(void)
530 531 532
{
	return _omap_dm_timer_request(REQUEST_ANY, NULL);
}
533

534
static struct omap_dm_timer *omap_dm_timer_request_specific(int id)
535
{
536 537
	/* Requesting timer by ID is not supported when device tree is used */
	if (of_have_populated_dt()) {
538
		pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
539 540 541 542
			__func__);
		return NULL;
	}

543
	return _omap_dm_timer_request(REQUEST_BY_ID, &id);
544 545
}

546 547 548 549 550 551 552
/**
 * omap_dm_timer_request_by_node - Request a timer by device-tree node
 * @np:		Pointer to device-tree timer node
 *
 * Request a timer based upon a device node pointer. Returns pointer to
 * timer handle on success and a NULL pointer on failure.
 */
553
static struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
554 555
{
	if (!np)
556 557
		return NULL;

558
	return _omap_dm_timer_request(REQUEST_BY_NODE, np);
559 560
}

561
static int omap_dm_timer_free(struct omap_dm_timer *timer)
562
{
563 564 565
	if (unlikely(!timer))
		return -EINVAL;

566
	clk_put(timer->fclk);
567

568 569
	WARN_ON(!timer->reserved);
	timer->reserved = 0;
570
	return 0;
571 572 573 574
}

int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
{
575 576 577
	if (timer)
		return timer->irq;
	return -EINVAL;
578 579 580
}

#if defined(CONFIG_ARCH_OMAP1)
581
#include <linux/soc/ti/omap1-io.h>
582 583 584 585 586 587

static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
{
	return NULL;
}

588 589 590 591 592 593
/**
 * 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)
{
594 595 596
	int i = 0;
	struct omap_dm_timer *timer = NULL;
	unsigned long flags;
597 598 599 600 601 602

	/* 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 */
603 604
	spin_lock_irqsave(&dm_timer_lock, flags);
	list_for_each_entry(timer, &omap_timer_list, node) {
605 606
		u32 l;

607
		l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
608 609
		if (l & OMAP_TIMER_CTRL_ST) {
			if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
610 611 612 613
				inputmask &= ~(1 << 1);
			else
				inputmask &= ~(1 << 2);
		}
614
		i++;
615
	}
616
	spin_unlock_irqrestore(&dm_timer_lock, flags);
617 618 619 620

	return inputmask;
}

621
#else
622

623
static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
624
{
625
	if (timer && !IS_ERR(timer->fclk))
626 627
		return timer->fclk;
	return NULL;
628
}
629

630 631 632
__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
{
	BUG();
633 634

	return 0;
635 636
}

637
#endif
638

639
static int omap_dm_timer_start(struct omap_dm_timer *timer)
640
{
641 642
	struct device *dev = &timer->pdev->dev;
	int rc;
643
	u32 l;
644

645 646 647
	if (unlikely(!timer))
		return -EINVAL;

648 649 650
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;
651

652
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
653 654
	if (!(l & OMAP_TIMER_CTRL_ST)) {
		l |= OMAP_TIMER_CTRL_ST;
655
		dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
656
	}
657

658
	return 0;
659
}
660

661
static int omap_dm_timer_stop(struct omap_dm_timer *timer)
662
{
663
	struct device *dev = &timer->pdev->dev;
664
	unsigned long rate = 0;
665

666 667 668
	if (unlikely(!timer))
		return -EINVAL;

669
	if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
670
		rate = clk_get_rate(timer->fclk);
671

672
	__omap_dm_timer_stop(timer, rate);
673

674 675
	pm_runtime_put_sync(dev);

676
	return 0;
677 678
}

679
static int omap_dm_timer_set_load(struct omap_dm_timer *timer,
680
				  unsigned int load)
681
{
682 683 684
	struct device *dev;
	int rc;

685 686 687
	if (unlikely(!timer))
		return -EINVAL;

688 689 690 691 692
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

693
	dmtimer_write(timer, OMAP_TIMER_LOAD_REG, load);
694

695 696
	pm_runtime_put_sync(dev);

697
	return 0;
698 699
}

700 701
static int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
				   unsigned int match)
702
{
703 704
	struct device *dev;
	int rc;
705 706
	u32 l;

707 708 709
	if (unlikely(!timer))
		return -EINVAL;

710 711 712 713 714
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

715
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
Timo Teras's avatar
Timo Teras committed
716
	if (enable)
717 718 719
		l |= OMAP_TIMER_CTRL_CE;
	else
		l &= ~OMAP_TIMER_CTRL_CE;
720 721
	dmtimer_write(timer, OMAP_TIMER_MATCH_REG, match);
	dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
722

723 724
	pm_runtime_put_sync(dev);

725
	return 0;
726 727
}

728
static int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
729
				 int toggle, int trigger, int autoreload)
730
{
731 732
	struct device *dev;
	int rc;
733 734
	u32 l;

735 736 737
	if (unlikely(!timer))
		return -EINVAL;

738 739 740 741 742
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

743
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
744
	l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
745
	       OMAP_TIMER_CTRL_PT | (0x03 << 10) | OMAP_TIMER_CTRL_AR);
746 747 748 749 750
	if (def_on)
		l |= OMAP_TIMER_CTRL_SCPWM;
	if (toggle)
		l |= OMAP_TIMER_CTRL_PT;
	l |= trigger << 10;
751 752
	if (autoreload)
		l |= OMAP_TIMER_CTRL_AR;
753
	dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
754

755 756
	pm_runtime_put_sync(dev);

757
	return 0;
758 759
}

760 761
static int omap_dm_timer_get_pwm_status(struct omap_dm_timer *timer)
{
762 763
	struct device *dev;
	int rc;
764 765 766 767 768
	u32 l;

	if (unlikely(!timer))
		return -EINVAL;

769 770 771 772 773
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

774
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
775 776

	pm_runtime_put_sync(dev);
777 778 779 780

	return l;
}

781 782
static int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer,
					int prescaler)
783
{
784 785
	struct device *dev;
	int rc;
786 787
	u32 l;

788
	if (unlikely(!timer) || prescaler < -1 || prescaler > 7)
789 790
		return -EINVAL;

791 792 793 794 795
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

796
	l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
797
	l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
798
	if (prescaler >= 0) {
799 800 801
		l |= OMAP_TIMER_CTRL_PRE;
		l |= prescaler << 2;
	}
802
	dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
803

804 805
	pm_runtime_put_sync(dev);

806
	return 0;
807 808
}

809 810
static int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
					unsigned int value)
811
{
812 813 814
	struct device *dev;
	int rc;

815 816 817
	if (unlikely(!timer))
		return -EINVAL;

818 819 820 821 822
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;

823
	__omap_dm_timer_int_enable(timer, value);
824

825 826
	pm_runtime_put_sync(dev);

827
	return 0;
828 829
}

830 831 832 833 834 835 836
/**
 * omap_dm_timer_set_int_disable - disable timer interrupts
 * @timer:	pointer to timer handle
 * @mask:	bit mask of interrupts to be disabled
 *
 * Disables the specified timer interrupts for a timer.
 */
837
static int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask)
838
{
839
	struct device *dev;
840
	u32 l = mask;
841
	int rc;
842 843 844 845

	if (unlikely(!timer))
		return -EINVAL;

846 847 848 849
	dev = &timer->pdev->dev;
	rc = pm_runtime_resume_and_get(dev);
	if (rc)
		return rc;
850 851

	if (timer->revision == 1)
852
		l = dmtimer_read(timer, timer->irq_ena) & ~mask;
853

854
	dmtimer_write(timer, timer->irq_dis, l);
855
	l = dmtimer_read(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
856
	dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
857

858 859
	pm_runtime_put_sync(dev);

860 861 862
	return 0;
}

863
static unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
864
{
865 866
	unsigned int l;

867
	if (unlikely(!timer || !atomic_read(&timer->enabled))) {
868
		pr_err("%s: timer not available or enabled.\n", __func__);
869 870 871
		return 0;
	}

872
	l = dmtimer_read(timer, timer->irq_stat);
873 874

	return l;
875 876
}

877
static int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
878
{
879
	if (unlikely(!timer || !atomic_read(&timer->enabled)))
880 881
		return -EINVAL;

882
	__omap_dm_timer_write_status(timer, value);
883

884
	return 0;
885 886
}

887
static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
888
{
889
	if (unlikely(!timer || !atomic_read(&timer->enabled))) {
890
		pr_err("%s: timer not iavailable or enabled.\n", __func__);
891 892 893
		return 0;
	}

894
	return __omap_dm_timer_read_counter(timer);
895 896
}

897
static int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
Timo Teras's avatar
Timo Teras committed
898
{
899
	if (unlikely(!timer || !atomic_read(&timer->enabled))) {
900 901
		pr_err("%s: timer not available or enabled.\n", __func__);
		return -EINVAL;
902 903
	}

904
	dmtimer_write(timer, OMAP_TIMER_COUNTER_REG, value);
905 906 907

	/* Save the context */
	timer->context.tcrr = value;
908
	return 0;
Timo Teras's avatar
Timo Teras committed
909 910
}

911 912 913 914 915 916
static int __maybe_unused omap_dm_timer_runtime_suspend(struct device *dev)
{
	struct omap_dm_timer *timer = dev_get_drvdata(dev);

	atomic_set(&timer->enabled, 0);

917 918 919 920 921
	if (timer->capability & OMAP_TIMER_ALWON || !timer->func_base)
		return 0;

	omap_timer_save_context(timer);

922 923 924 925 926 927 928
	return 0;
}

static int __maybe_unused omap_dm_timer_runtime_resume(struct device *dev)
{
	struct omap_dm_timer *timer = dev_get_drvdata(dev);

929 930 931
	if (!(timer->capability & OMAP_TIMER_ALWON) && timer->func_base)
		omap_timer_restore_context(timer);

932 933 934 935 936 937 938 939 940 941
	atomic_set(&timer->enabled, 1);

	return 0;
}

static const struct dev_pm_ops omap_dm_timer_pm_ops = {
	SET_RUNTIME_PM_OPS(omap_dm_timer_runtime_suspend,
			   omap_dm_timer_runtime_resume, NULL)
};

942 943
static const struct of_device_id omap_timer_match[];

944 945 946 947 948 949 950
/**
 * omap_dm_timer_probe - probe function called for every registered device
 * @pdev:	pointer to current timer platform device
 *
 * Called by driver framework at the end of device registration for all
 * timer devices.
 */
951
static int omap_dm_timer_probe(struct platform_device *pdev)
952 953 954
{
	unsigned long flags;
	struct omap_dm_timer *timer;
955
	struct device *dev = &pdev->dev;
956
	const struct dmtimer_platform_data *pdata;
957
	int ret;
958

959 960 961 962 963
	pdata = of_device_get_match_data(dev);
	if (!pdata)
		pdata = dev_get_platdata(dev);
	else
		dev->platform_data = (void *)pdata;
964

965
	if (!pdata) {
966
		dev_err(dev, "%s: no platform data.\n", __func__);
967 968 969
		return -ENODEV;
	}

970
	timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
971
	if (!timer)
972
		return  -ENOMEM;
973

974 975 976 977
	timer->irq = platform_get_irq(pdev, 0);
	if (timer->irq < 0)
		return timer->irq;

978
	timer->fclk = ERR_PTR(-ENODEV);
979
	timer->io_base = devm_platform_ioremap_resource(pdev, 0);
980 981
	if (IS_ERR(timer->io_base))
		return PTR_ERR(timer->io_base);
982

983 984
	platform_set_drvdata(pdev, timer);

985 986 987 988 989 990 991 992 993 994 995 996 997
	if (dev->of_node) {
		if (of_find_property(dev->of_node, "ti,timer-alwon", NULL))
			timer->capability |= OMAP_TIMER_ALWON;
		if (of_find_property(dev->of_node, "ti,timer-dsp", NULL))
			timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
		if (of_find_property(dev->of_node, "ti,timer-pwm", NULL))
			timer->capability |= OMAP_TIMER_HAS_PWM;
		if (of_find_property(dev->of_node, "ti,timer-secure", NULL))
			timer->capability |= OMAP_TIMER_SECURE;
	} else {
		timer->id = pdev->id;
		timer->capability = pdata->timer_capability;
		timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
998 999 1000 1001 1002
	}

	if (!(timer->capability & OMAP_TIMER_ALWON)) {
		timer->nb.notifier_call = omap_timer_context_notifier;
		cpu_pm_register_notifier(&timer->nb);
1003 1004
	}

1005
	timer->errata = pdata->timer_errata;
1006

1007 1008
	timer->pdev = pdev;

1009
	pm_runtime_enable(dev);
1010

1011
	if (!timer->reserved) {
1012 1013
		ret = pm_runtime_resume_and_get(dev);
		if (ret) {
1014 1015
			dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
				__func__);
1016
			goto err_disable;
1017
		}
1018
		__omap_dm_timer_init_regs(timer);
1019
		pm_runtime_put(dev);
1020 1021
	}

1022 1023 1024 1025 1026
	/* add the timer element to the list */
	spin_lock_irqsave(&dm_timer_lock, flags);
	list_add_tail(&timer->node, &omap_timer_list);
	spin_unlock_irqrestore(&dm_timer_lock, flags);

1027
	dev_dbg(dev, "Device Probed.\n");
1028 1029

	return 0;
1030

1031
err_disable:
1032 1033
	pm_runtime_disable(dev);
	return ret;
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
}

/**
 * omap_dm_timer_remove - cleanup a registered timer device
 * @pdev:	pointer to current timer platform device
 *
 * Called by driver framework whenever a timer device is unregistered.
 * In addition to freeing platform resources it also deletes the timer
 * entry from the local list.
 */
1044
static int omap_dm_timer_remove(struct platform_device *pdev)
1045 1046 1047 1048 1049 1050 1051
{
	struct omap_dm_timer *timer;
	unsigned long flags;
	int ret = -EINVAL;

	spin_lock_irqsave(&dm_timer_lock, flags);
	list_for_each_entry(timer, &omap_timer_list, node)
1052 1053
		if (!strcmp(dev_name(&timer->pdev->dev),
			    dev_name(&pdev->dev))) {
1054 1055
			if (!(timer->capability & OMAP_TIMER_ALWON))
				cpu_pm_unregister_notifier(&timer->nb);
1056 1057 1058 1059 1060 1061
			list_del(&timer->node);
			ret = 0;
			break;
		}
	spin_unlock_irqrestore(&dm_timer_lock, flags);

1062 1063
	pm_runtime_disable(&pdev->dev);

1064 1065 1066
	return ret;
}

1067
static const struct omap_dm_timer_ops dmtimer_ops = {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	.request_by_node = omap_dm_timer_request_by_node,
	.request_specific = omap_dm_timer_request_specific,
	.request = omap_dm_timer_request,
	.set_source = omap_dm_timer_set_source,
	.get_irq = omap_dm_timer_get_irq,
	.set_int_enable = omap_dm_timer_set_int_enable,
	.set_int_disable = omap_dm_timer_set_int_disable,
	.free = omap_dm_timer_free,
	.enable = omap_dm_timer_enable,
	.disable = omap_dm_timer_disable,
	.get_fclk = omap_dm_timer_get_fclk,
	.start = omap_dm_timer_start,
	.stop = omap_dm_timer_stop,
	.set_load = omap_dm_timer_set_load,
	.set_match = omap_dm_timer_set_match,
	.set_pwm = omap_dm_timer_set_pwm,
1084
	.get_pwm_status = omap_dm_timer_get_pwm_status,
1085 1086 1087 1088 1089 1090 1091
	.set_prescaler = omap_dm_timer_set_prescaler,
	.read_counter = omap_dm_timer_read_counter,
	.write_counter = omap_dm_timer_write_counter,
	.read_status = omap_dm_timer_read_status,
	.write_status = omap_dm_timer_write_status,
};

1092 1093
static const struct dmtimer_platform_data omap3plus_pdata = {
	.timer_errata = OMAP_TIMER_ERRATA_I103_I767,
1094
	.timer_ops = &dmtimer_ops,
1095 1096
};

1097 1098 1099 1100
static const struct dmtimer_platform_data am6_pdata = {
	.timer_ops = &dmtimer_ops,
};

1101
static const struct of_device_id omap_timer_match[] = {
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	{
		.compatible = "ti,omap2420-timer",
	},
	{
		.compatible = "ti,omap3430-timer",
		.data = &omap3plus_pdata,
	},
	{
		.compatible = "ti,omap4430-timer",
		.data = &omap3plus_pdata,
	},
	{
		.compatible = "ti,omap5430-timer",
		.data = &omap3plus_pdata,
	},
	{
		.compatible = "ti,am335x-timer",
		.data = &omap3plus_pdata,
	},
	{
		.compatible = "ti,am335x-timer-1ms",
		.data = &omap3plus_pdata,
	},
1125 1126 1127 1128
	{
		.compatible = "ti,dm816-timer",
		.data = &omap3plus_pdata,
	},
1129 1130 1131 1132
	{
		.compatible = "ti,am654-timer",
		.data = &am6_pdata,
	},
1133 1134 1135 1136
	{},
};
MODULE_DEVICE_TABLE(of, omap_timer_match);

1137 1138
static struct platform_driver omap_dm_timer_driver = {
	.probe  = omap_dm_timer_probe,
1139
	.remove = omap_dm_timer_remove,
1140 1141
	.driver = {
		.name   = "omap_timer",
1142
		.of_match_table = of_match_ptr(omap_timer_match),
1143
		.pm = &omap_dm_timer_pm_ops,
1144 1145 1146
	},
};

1147
module_platform_driver(omap_dm_timer_driver);
1148 1149 1150 1151

MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Texas Instruments Inc");