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 * Scheduler clock - returns current time in nanosec units.
115 unsigned long long sched_clock(void)
117 /* There must be better ways...? */
118 return (unsigned long long)jiffies * (1000000000 / HZ);
122 * local_timer_interrupt() does profiling and process accounting on a
125 * In UP mode, it is invoked from the (global) timer_interrupt.
127 static void local_timer_interrupt(int irq, void *dev_id)
130 profile_tick(CPU_PROFILING);
131 update_process_times(user_mode(get_irq_regs()));
135 timer_interrupt(int irq, void *dev_id)
139 /* ack timer interrupt and try to set next interrupt */
140 count = avr32_hpt_read();
143 /* Update timerhi/timerlo for intra-jiffy calibration */
144 timerhi += count < timerlo; /* Wrap around */
148 * Call the generic timer interrupt handler
150 write_seqlock(&xtime_lock);
152 write_sequnlock(&xtime_lock);
155 * In UP mode, we call local_timer_interrupt() to do profiling
156 * and process accounting.
158 * SMP is not supported yet.
160 local_timer_interrupt(irq, dev_id);
165 static struct irqaction timer_irqaction = {
166 .handler = timer_interrupt,
167 .flags = IRQF_DISABLED,
171 void __init time_init(void)
173 unsigned long mult, shift, count_hz;
176 xtime.tv_sec = rtc_get_time();
179 set_normalized_timespec(&wall_to_monotonic,
180 -xtime.tv_sec, -xtime.tv_nsec);
182 printk("Before time_init: count=%08lx, compare=%08lx\n",
183 (unsigned long)sysreg_read(COUNT),
184 (unsigned long)sysreg_read(COMPARE));
186 count_hz = clk_get_rate(boot_cpu_data.clk);
187 shift = clocksource_avr32.shift;
188 mult = clocksource_hz2mult(count_hz, shift);
189 clocksource_avr32.mult = mult;
191 printk("Cycle counter: mult=%lu, shift=%lu\n", mult, shift);
201 cycles_per_jiffy = tmp;
204 /* This sets up the high precision timer for the first interrupt. */
205 avr32_hpt_init(avr32_hpt_read());
207 printk("After time_init: count=%08lx, compare=%08lx\n",
208 (unsigned long)sysreg_read(COUNT),
209 (unsigned long)sysreg_read(COMPARE));
211 ret = clocksource_register(&clocksource_avr32);
214 "timer: could not register clocksource: %d\n", ret);
216 ret = setup_irq(0, &timer_irqaction);
218 printk("timer: could not request IRQ 0: %d\n", ret);
221 static struct sysdev_class timer_class = {
222 set_kset_name("timer"),
225 static struct sys_device timer_device = {
230 static int __init init_timer_sysfs(void)
232 int err = sysdev_class_register(&timer_class);
234 err = sysdev_register(&timer_device);
238 device_initcall(init_timer_sysfs);