time.c 4.6 KB
Newer Older
H
Haavard Skinnemoen 已提交
1
/*
2
 * Copyright (C) 2004-2007 Atmel Corporation
H
Haavard Skinnemoen 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Based on MIPS implementation arch/mips/kernel/time.c
 *   Copyright 2001 MontaVista Software Inc.
 *
 * 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>
#include <linux/clocksource.h>
#include <linux/time.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel_stat.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/profile.h>
#include <linux/sysdev.h>
23
#include <linux/err.h>
H
Haavard Skinnemoen 已提交
24 25 26 27 28 29

#include <asm/div64.h>
#include <asm/sysreg.h>
#include <asm/io.h>
#include <asm/sections.h>

30 31 32 33 34 35 36
/* how many counter cycles in a jiffy? */
static u32 cycles_per_jiffy;

/* the count value for the next timer interrupt */
static u32 expirelo;

cycle_t __weak read_cycle_count(void)
H
Haavard Skinnemoen 已提交
37 38 39 40
{
	return (cycle_t)sysreg_read(COUNT);
}

41 42 43 44 45 46
/*
 * 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.
 * So we rate the clocksource using COUNT as very low quality.
 */
47
struct clocksource __weak clocksource_avr32 = {
H
Haavard Skinnemoen 已提交
48
	.name		= "avr32",
49
	.rating		= 50,
H
Haavard Skinnemoen 已提交
50 51 52
	.read		= read_cycle_count,
	.mask		= CLOCKSOURCE_MASK(32),
	.shift		= 16,
53
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
H
Haavard Skinnemoen 已提交
54 55
};

56 57 58 59 60 61 62 63
irqreturn_t __weak timer_interrupt(int irq, void *dev_id);

struct irqaction timer_irqaction = {
	.handler	= timer_interrupt,
	.flags		= IRQF_DISABLED,
	.name		= "timer",
};

H
Haavard Skinnemoen 已提交
64 65
static void avr32_timer_ack(void)
{
66
	u32 count;
H
Haavard Skinnemoen 已提交
67 68 69

	/* Ack this timer interrupt and set the next one */
	expirelo += cycles_per_jiffy;
70
	/* setting COMPARE to 0 stops the COUNT-COMPARE */
H
Haavard Skinnemoen 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
	if (expirelo == 0) {
		sysreg_write(COMPARE, expirelo + 1);
	} else {
		sysreg_write(COMPARE, expirelo);
	}

	/* Check to see if we have missed any timer interrupts */
	count = sysreg_read(COUNT);
	if ((count - expirelo) < 0x7fffffff) {
		expirelo = count + cycles_per_jiffy;
		sysreg_write(COMPARE, expirelo);
	}
}

85
int __weak avr32_hpt_init(void)
H
Haavard Skinnemoen 已提交
86
{
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	int ret;
	unsigned long mult, shift, count_hz;

	count_hz = clk_get_rate(boot_cpu_data.clk);
	shift = clocksource_avr32.shift;
	mult = clocksource_hz2mult(count_hz, shift);
	clocksource_avr32.mult = mult;

	{
		u64 tmp;

		tmp = TICK_NSEC;
		tmp <<= shift;
		tmp += mult / 2;
		do_div(tmp, mult);

		cycles_per_jiffy = tmp;
	}

	ret = setup_irq(0, &timer_irqaction);
	if (ret) {
		pr_debug("timer: could not request IRQ 0: %d\n", ret);
		return -ENODEV;
	}

	printk(KERN_INFO "timer: AT32AP COUNT-COMPARE at irq 0, "
			"%lu.%03lu MHz\n",
			((count_hz + 500) / 1000) / 1000,
			((count_hz + 500) / 1000) % 1000);

	return 0;
H
Haavard Skinnemoen 已提交
118 119 120 121 122
}

/*
 * Taken from MIPS c0_hpt_timer_init().
 *
123 124
 * The reason COUNT is written twice is probably to make sure we don't get any
 * timer interrupts while we are messing with the counter.
H
Haavard Skinnemoen 已提交
125
 */
126
int __weak avr32_hpt_start(void)
H
Haavard Skinnemoen 已提交
127
{
128
	u32 count = sysreg_read(COUNT);
H
Haavard Skinnemoen 已提交
129 130 131 132
	expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
	sysreg_write(COUNT, expirelo - cycles_per_jiffy);
	sysreg_write(COMPARE, expirelo);
	sysreg_write(COUNT, count);
133 134

	return 0;
H
Haavard Skinnemoen 已提交
135 136 137 138 139 140 141 142
}

/*
 * local_timer_interrupt() does profiling and process accounting on a
 * per-CPU basis.
 *
 * In UP mode, it is invoked from the (global) timer_interrupt.
 */
143
void local_timer_interrupt(int irq, void *dev_id)
H
Haavard Skinnemoen 已提交
144 145
{
	if (current->pid)
146 147
		profile_tick(CPU_PROFILING);
	update_process_times(user_mode(get_irq_regs()));
H
Haavard Skinnemoen 已提交
148 149
}

150
irqreturn_t __weak timer_interrupt(int irq, void *dev_id)
H
Haavard Skinnemoen 已提交
151 152 153 154 155 156 157 158
{
	/* ack timer interrupt and try to set next interrupt */
	avr32_timer_ack();

	/*
	 * Call the generic timer interrupt handler
	 */
	write_seqlock(&xtime_lock);
159
	do_timer(1);
H
Haavard Skinnemoen 已提交
160 161 162 163 164 165 166 167
	write_sequnlock(&xtime_lock);

	/*
	 * In UP mode, we call local_timer_interrupt() to do profiling
	 * and process accounting.
	 *
	 * SMP is not supported yet.
	 */
168
	local_timer_interrupt(irq, dev_id);
H
Haavard Skinnemoen 已提交
169 170 171 172 173 174 175 176

	return IRQ_HANDLED;
}

void __init time_init(void)
{
	int ret;

177 178 179 180 181 182
	/*
	 * Make sure we don't get any COMPARE interrupts before we can
	 * handle them.
	 */
	sysreg_write(COMPARE, 0);

183
	xtime.tv_sec = mktime(2007, 1, 1, 0, 0, 0);
H
Haavard Skinnemoen 已提交
184 185 186 187 188
	xtime.tv_nsec = 0;

	set_normalized_timespec(&wall_to_monotonic,
				-xtime.tv_sec, -xtime.tv_nsec);

189 190 191 192
	ret = avr32_hpt_init();
	if (ret) {
		pr_debug("timer: failed setup: %d\n", ret);
		return;
H
Haavard Skinnemoen 已提交
193 194 195 196
	}

	ret = clocksource_register(&clocksource_avr32);
	if (ret)
197
		pr_debug("timer: could not register clocksource: %d\n", ret);
H
Haavard Skinnemoen 已提交
198

199 200 201 202 203
	ret = avr32_hpt_start();
	if (ret) {
		pr_debug("timer: failed starting: %d\n", ret);
		return;
	}
H
Haavard Skinnemoen 已提交
204
}