2 * Copyright (C) 2004-2007 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>
23 #include <linux/err.h>
25 #include <asm/div64.h>
26 #include <asm/sysreg.h>
28 #include <asm/sections.h>
30 /* how many counter cycles in a jiffy? */
31 static u32 cycles_per_jiffy
;
33 /* the count value for the next timer interrupt */
36 cycle_t __weak
read_cycle_count(void)
38 return (cycle_t
)sysreg_read(COUNT
);
41 struct clocksource __weak clocksource_avr32
= {
44 .read
= read_cycle_count
,
45 .mask
= CLOCKSOURCE_MASK(32),
47 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
50 irqreturn_t __weak
timer_interrupt(int irq
, void *dev_id
);
52 struct irqaction timer_irqaction
= {
53 .handler
= timer_interrupt
,
54 .flags
= IRQF_DISABLED
,
59 * By default we provide the null RTC ops
61 static unsigned long null_rtc_get_time(void)
63 return mktime(2007, 1, 1, 0, 0, 0);
66 static int null_rtc_set_time(unsigned long sec
)
71 static unsigned long (*rtc_get_time
)(void) = null_rtc_get_time
;
72 static int (*rtc_set_time
)(unsigned long) = null_rtc_set_time
;
74 static void avr32_timer_ack(void)
78 /* Ack this timer interrupt and set the next one */
79 expirelo
+= cycles_per_jiffy
;
80 /* setting COMPARE to 0 stops the COUNT-COMPARE */
82 sysreg_write(COMPARE
, expirelo
+ 1);
84 sysreg_write(COMPARE
, expirelo
);
87 /* Check to see if we have missed any timer interrupts */
88 count
= sysreg_read(COUNT
);
89 if ((count
- expirelo
) < 0x7fffffff) {
90 expirelo
= count
+ cycles_per_jiffy
;
91 sysreg_write(COMPARE
, expirelo
);
95 int __weak
avr32_hpt_init(void)
98 unsigned long mult
, shift
, count_hz
;
100 count_hz
= clk_get_rate(boot_cpu_data
.clk
);
101 shift
= clocksource_avr32
.shift
;
102 mult
= clocksource_hz2mult(count_hz
, shift
);
103 clocksource_avr32
.mult
= mult
;
113 cycles_per_jiffy
= tmp
;
116 ret
= setup_irq(0, &timer_irqaction
);
118 pr_debug("timer: could not request IRQ 0: %d\n", ret
);
122 printk(KERN_INFO
"timer: AT32AP COUNT-COMPARE at irq 0, "
124 ((count_hz
+ 500) / 1000) / 1000,
125 ((count_hz
+ 500) / 1000) % 1000);
131 * Taken from MIPS c0_hpt_timer_init().
133 * The reason COUNT is written twice is probably to make sure we don't get any
134 * timer interrupts while we are messing with the counter.
136 int __weak
avr32_hpt_start(void)
138 u32 count
= sysreg_read(COUNT
);
139 expirelo
= (count
/ cycles_per_jiffy
+ 1) * cycles_per_jiffy
;
140 sysreg_write(COUNT
, expirelo
- cycles_per_jiffy
);
141 sysreg_write(COMPARE
, expirelo
);
142 sysreg_write(COUNT
, count
);
148 * local_timer_interrupt() does profiling and process accounting on a
151 * In UP mode, it is invoked from the (global) timer_interrupt.
153 void local_timer_interrupt(int irq
, void *dev_id
)
156 profile_tick(CPU_PROFILING
);
157 update_process_times(user_mode(get_irq_regs()));
160 irqreturn_t __weak
timer_interrupt(int irq
, void *dev_id
)
162 /* ack timer interrupt and try to set next interrupt */
166 * Call the generic timer interrupt handler
168 write_seqlock(&xtime_lock
);
170 write_sequnlock(&xtime_lock
);
173 * In UP mode, we call local_timer_interrupt() to do profiling
174 * and process accounting.
176 * SMP is not supported yet.
178 local_timer_interrupt(irq
, dev_id
);
183 void __init
time_init(void)
188 * Make sure we don't get any COMPARE interrupts before we can
191 sysreg_write(COMPARE
, 0);
193 xtime
.tv_sec
= rtc_get_time();
196 set_normalized_timespec(&wall_to_monotonic
,
197 -xtime
.tv_sec
, -xtime
.tv_nsec
);
199 ret
= avr32_hpt_init();
201 pr_debug("timer: failed setup: %d\n", ret
);
205 ret
= clocksource_register(&clocksource_avr32
);
207 pr_debug("timer: could not register clocksource: %d\n", ret
);
209 ret
= avr32_hpt_start();
211 pr_debug("timer: failed starting: %d\n", ret
);
216 static struct sysdev_class timer_class
= {
217 set_kset_name("timer"),
220 static struct sys_device timer_device
= {
225 static int __init
init_timer_sysfs(void)
227 int err
= sysdev_class_register(&timer_class
);
229 err
= sysdev_register(&timer_device
);
233 device_initcall(init_timer_sysfs
);