time.c 4.0 KB
Newer Older
H
Haavard Skinnemoen 已提交
1
/*
2
 * Copyright (C) 2004-2007 Atmel Corporation
H
Haavard Skinnemoen 已提交
3 4 5 6 7 8
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/clk.h>
9
#include <linux/clockchips.h>
10
#include <linux/init.h>
H
Haavard Skinnemoen 已提交
11 12
#include <linux/interrupt.h>
#include <linux/irq.h>
13 14
#include <linux/kernel.h>
#include <linux/time.h>
T
Thomas Gleixner 已提交
15
#include <linux/cpu.h>
H
Haavard Skinnemoen 已提交
16 17 18

#include <asm/sysreg.h>

19
#include <mach/pm.h>
20

21
static bool disable_cpu_idle_poll;
22

23
static u64 read_cycle_count(struct clocksource *cs)
H
Haavard Skinnemoen 已提交
24
{
25
	return (u64)sysreg_read(COUNT);
H
Haavard Skinnemoen 已提交
26 27
}

28 29 30 31
/*
 * The architectural cycle count registers are a fine clocksource unless
 * the system idle loop use sleep states like "idle":  the CPU cycles
 * measured by COUNT (and COMPARE) don't happen during sleep states.
32
 * Their duration also changes if cpufreq changes the CPU clock rate.
33 34
 * So we rate the clocksource using COUNT as very low quality.
 */
35 36
static struct clocksource counter = {
	.name		= "avr32_counter",
37
	.rating		= 50,
H
Haavard Skinnemoen 已提交
38 39
	.read		= read_cycle_count,
	.mask		= CLOCKSOURCE_MASK(32),
40
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
H
Haavard Skinnemoen 已提交
41 42
};

43
static irqreturn_t timer_interrupt(int irq, void *dev_id)
H
Haavard Skinnemoen 已提交
44
{
45
	struct clock_event_device *evdev = dev_id;
46

47 48 49
	if (unlikely(!(intc_get_pending(0) & 1)))
		return IRQ_NONE;

50 51 52 53 54
	/*
	 * Disable the interrupt until the clockevent subsystem
	 * reprograms it.
	 */
	sysreg_write(COMPARE, 0);
55

56 57 58
	evdev->event_handler(evdev);
	return IRQ_HANDLED;
}
59

60 61
static struct irqaction timer_irqaction = {
	.handler	= timer_interrupt,
62
	/* Oprofile uses the same irq as the timer, so allow it to be shared */
63
	.flags		= IRQF_TIMER | IRQF_SHARED,
64 65
	.name		= "avr32_comparator",
};
66

67 68 69 70
static int comparator_next_event(unsigned long delta,
		struct clock_event_device *evdev)
{
	unsigned long	flags;
71

72
	raw_local_irq_save(flags);
73

74 75 76 77
	/* The time to read COUNT then update COMPARE must be less
	 * than the min_delta_ns value for this clockevent source.
	 */
	sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1);
H
Haavard Skinnemoen 已提交
78

79
	raw_local_irq_restore(flags);
80 81

	return 0;
H
Haavard Skinnemoen 已提交
82 83
}

84
static int comparator_shutdown(struct clock_event_device *evdev)
H
Haavard Skinnemoen 已提交
85
{
86 87 88 89 90
	pr_debug("%s: %s\n", __func__, evdev->name);
	sysreg_write(COMPARE, 0);

	if (disable_cpu_idle_poll) {
		disable_cpu_idle_poll = false;
T
Thomas Gleixner 已提交
91
		/*
92 93
		 * Only disable idle poll if we have forced that
		 * in a previous call.
T
Thomas Gleixner 已提交
94
		 */
95
		cpu_idle_poll_ctrl(false);
96
	}
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	return 0;
}

static int comparator_set_oneshot(struct clock_event_device *evdev)
{
	pr_debug("%s: %s\n", __func__, evdev->name);

	disable_cpu_idle_poll = true;
	/*
	 * If we're using the COUNT and COMPARE registers we
	 * need to force idle poll.
	 */
	cpu_idle_poll_ctrl(true);

	return 0;
H
Haavard Skinnemoen 已提交
112 113
}

114
static struct clock_event_device comparator = {
115 116 117 118 119 120 121 122
	.name			= "avr32_comparator",
	.features		= CLOCK_EVT_FEAT_ONESHOT,
	.shift			= 16,
	.rating			= 50,
	.set_next_event		= comparator_next_event,
	.set_state_shutdown	= comparator_shutdown,
	.set_state_oneshot	= comparator_set_oneshot,
	.tick_resume		= comparator_set_oneshot,
123
};
H
Haavard Skinnemoen 已提交
124

125 126
void read_persistent_clock(struct timespec *ts)
{
127
	ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0);
128 129 130
	ts->tv_nsec = 0;
}

H
Haavard Skinnemoen 已提交
131 132
void __init time_init(void)
{
133
	unsigned long counter_hz;
H
Haavard Skinnemoen 已提交
134 135
	int ret;

136 137
	/* figure rate for counter */
	counter_hz = clk_get_rate(boot_cpu_data.clk);
138
	ret = clocksource_register_hz(&counter, counter_hz);
H
Haavard Skinnemoen 已提交
139
	if (ret)
140
		pr_debug("timer: could not register clocksource: %d\n", ret);
H
Haavard Skinnemoen 已提交
141

142 143 144 145
	/* setup COMPARE clockevent */
	comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift);
	comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator);
	comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1;
146
	comparator.cpumask = cpumask_of(0);
147 148 149 150 151 152 153 154 155 156 157 158 159

	sysreg_write(COMPARE, 0);
	timer_irqaction.dev_id = &comparator;

	ret = setup_irq(0, &timer_irqaction);
	if (ret)
		pr_debug("timer: could not request IRQ 0: %d\n", ret);
	else {
		clockevents_register_device(&comparator);

		pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
				((counter_hz + 500) / 1000) / 1000,
				((counter_hz + 500) / 1000) % 1000);
160
	}
H
Haavard Skinnemoen 已提交
161
}