2 * Copyright (C) 2004-2006 Atmel Corporation
4 * Based on MIPS implementation arch/mips/kernel/time.c
5 * Copyright 2001 MontaVista Software Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/clocksource.h>
14 #include <linux/time.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/profile.h>
22 #include <linux/sysdev.h>
24 #include <asm/div64.h>
25 #include <asm/sysreg.h>
27 #include <asm/sections.h>
29 static cycle_t read_cycle_count(void)
31 return (cycle_t)sysreg_read(COUNT);
34 static struct clocksource clocksource_avr32 = {
37 .read = read_cycle_count,
38 .mask = CLOCKSOURCE_MASK(32),
44 * By default we provide the null RTC ops
46 static unsigned long null_rtc_get_time(void)
48 return mktime(2004, 1, 1, 0, 0, 0);
51 static int null_rtc_set_time(unsigned long sec)
56 static unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
57 static int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
59 /* how many counter cycles in a jiffy? */
60 static unsigned long cycles_per_jiffy;
62 /* cycle counter value at the previous timer interrupt */
63 static unsigned int timerhi, timerlo;
65 /* the count value for the next timer interrupt */
66 static unsigned int expirelo;
68 static void avr32_timer_ack(void)
72 /* Ack this timer interrupt and set the next one */
73 expirelo += cycles_per_jiffy;
75 printk(KERN_DEBUG "expirelo == 0\n");
76 sysreg_write(COMPARE, expirelo + 1);
78 sysreg_write(COMPARE, expirelo);
81 /* Check to see if we have missed any timer interrupts */
82 count = sysreg_read(COUNT);
83 if ((count - expirelo) < 0x7fffffff) {
84 expirelo = count + cycles_per_jiffy;
85 sysreg_write(COMPARE, expirelo);
89 static unsigned int avr32_hpt_read(void)
91 return sysreg_read(COUNT);
95 * Taken from MIPS c0_hpt_timer_init().
97 * Why is it so complicated, and what is "count"? My assumption is
98 * that `count' specifies the "reference cycle", i.e. the cycle since
99 * reset that should mean "zero". The reason COUNT is written twice is
100 * probably to make sure we don't get any timer interrupts while we
101 * are messing with the counter.
103 static void avr32_hpt_init(unsigned int count)
105 count = sysreg_read(COUNT) - count;
106 expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
107 sysreg_write(COUNT, expirelo - cycles_per_jiffy);
108 sysreg_write(COMPARE, expirelo);
109 sysreg_write(COUNT, count);
113 * local_timer_interrupt() does profiling and process accounting on a
116 * In UP mode, it is invoked from the (global) timer_interrupt.
118 static void local_timer_interrupt(int irq, void *dev_id)
121 profile_tick(CPU_PROFILING);
122 update_process_times(user_mode(get_irq_regs()));
126 timer_interrupt(int irq, void *dev_id)
130 /* ack timer interrupt and try to set next interrupt */
131 count = avr32_hpt_read();
134 /* Update timerhi/timerlo for intra-jiffy calibration */
135 timerhi += count < timerlo; /* Wrap around */
139 * Call the generic timer interrupt handler
141 write_seqlock(&xtime_lock);
143 write_sequnlock(&xtime_lock);
146 * In UP mode, we call local_timer_interrupt() to do profiling
147 * and process accounting.
149 * SMP is not supported yet.
151 local_timer_interrupt(irq, dev_id);
156 static struct irqaction timer_irqaction = {
157 .handler = timer_interrupt,
158 .flags = IRQF_DISABLED,
162 void __init time_init(void)
164 unsigned long mult, shift, count_hz;
167 xtime.tv_sec = rtc_get_time();
170 set_normalized_timespec(&wall_to_monotonic,
171 -xtime.tv_sec, -xtime.tv_nsec);
173 printk("Before time_init: count=%08lx, compare=%08lx\n",
174 (unsigned long)sysreg_read(COUNT),
175 (unsigned long)sysreg_read(COMPARE));
177 count_hz = clk_get_rate(boot_cpu_data.clk);
178 shift = clocksource_avr32.shift;
179 mult = clocksource_hz2mult(count_hz, shift);
180 clocksource_avr32.mult = mult;
182 printk("Cycle counter: mult=%lu, shift=%lu\n", mult, shift);
192 cycles_per_jiffy = tmp;
195 /* This sets up the high precision timer for the first interrupt. */
196 avr32_hpt_init(avr32_hpt_read());
198 printk("After time_init: count=%08lx, compare=%08lx\n",
199 (unsigned long)sysreg_read(COUNT),
200 (unsigned long)sysreg_read(COMPARE));
202 ret = clocksource_register(&clocksource_avr32);
205 "timer: could not register clocksource: %d\n", ret);
207 ret = setup_irq(0, &timer_irqaction);
209 printk("timer: could not request IRQ 0: %d\n", ret);
212 static struct sysdev_class timer_class = {
213 set_kset_name("timer"),
216 static struct sys_device timer_device = {
221 static int __init init_timer_sysfs(void)
223 int err = sysdev_class_register(&timer_class);
225 err = sysdev_register(&timer_device);
229 device_initcall(init_timer_sysfs);