2 * Copyright (C) 2004-2007 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/clockchips.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/time.h>
15 #include <linux/cpu.h>
17 #include <asm/sysreg.h>
22 static cycle_t
read_cycle_count(struct clocksource
*cs
)
24 return (cycle_t
)sysreg_read(COUNT
);
28 * The architectural cycle count registers are a fine clocksource unless
29 * the system idle loop use sleep states like "idle": the CPU cycles
30 * measured by COUNT (and COMPARE) don't happen during sleep states.
31 * Their duration also changes if cpufreq changes the CPU clock rate.
32 * So we rate the clocksource using COUNT as very low quality.
34 static struct clocksource counter
= {
35 .name
= "avr32_counter",
37 .read
= read_cycle_count
,
38 .mask
= CLOCKSOURCE_MASK(32),
39 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
42 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
44 struct clock_event_device
*evdev
= dev_id
;
46 if (unlikely(!(intc_get_pending(0) & 1)))
50 * Disable the interrupt until the clockevent subsystem
53 sysreg_write(COMPARE
, 0);
55 evdev
->event_handler(evdev
);
59 static struct irqaction timer_irqaction
= {
60 .handler
= timer_interrupt
,
61 /* Oprofile uses the same irq as the timer, so allow it to be shared */
62 .flags
= IRQF_TIMER
| IRQF_DISABLED
| IRQF_SHARED
,
63 .name
= "avr32_comparator",
66 static int comparator_next_event(unsigned long delta
,
67 struct clock_event_device
*evdev
)
71 raw_local_irq_save(flags
);
73 /* The time to read COUNT then update COMPARE must be less
74 * than the min_delta_ns value for this clockevent source.
76 sysreg_write(COMPARE
, (sysreg_read(COUNT
) + delta
) ? : 1);
78 raw_local_irq_restore(flags
);
83 static void comparator_mode(enum clock_event_mode mode
,
84 struct clock_event_device
*evdev
)
87 case CLOCK_EVT_MODE_ONESHOT
:
88 pr_debug("%s: start\n", evdev
->name
);
90 case CLOCK_EVT_MODE_RESUME
:
92 * If we're using the COUNT and COMPARE registers we
93 * need to force idle poll.
95 cpu_idle_poll_ctrl(true);
97 case CLOCK_EVT_MODE_UNUSED
:
98 case CLOCK_EVT_MODE_SHUTDOWN
:
99 sysreg_write(COMPARE
, 0);
100 pr_debug("%s: stop\n", evdev
->name
);
101 cpu_idle_poll_ctrl(false);
108 static struct clock_event_device comparator
= {
109 .name
= "avr32_comparator",
110 .features
= CLOCK_EVT_FEAT_ONESHOT
,
113 .set_next_event
= comparator_next_event
,
114 .set_mode
= comparator_mode
,
117 void read_persistent_clock(struct timespec
*ts
)
119 ts
->tv_sec
= mktime(2007, 1, 1, 0, 0, 0);
123 void __init
time_init(void)
125 unsigned long counter_hz
;
128 /* figure rate for counter */
129 counter_hz
= clk_get_rate(boot_cpu_data
.clk
);
130 ret
= clocksource_register_hz(&counter
, counter_hz
);
132 pr_debug("timer: could not register clocksource: %d\n", ret
);
134 /* setup COMPARE clockevent */
135 comparator
.mult
= div_sc(counter_hz
, NSEC_PER_SEC
, comparator
.shift
);
136 comparator
.max_delta_ns
= clockevent_delta2ns((u32
)~0, &comparator
);
137 comparator
.min_delta_ns
= clockevent_delta2ns(50, &comparator
) + 1;
138 comparator
.cpumask
= cpumask_of(0);
140 sysreg_write(COMPARE
, 0);
141 timer_irqaction
.dev_id
= &comparator
;
143 ret
= setup_irq(0, &timer_irqaction
);
145 pr_debug("timer: could not request IRQ 0: %d\n", ret
);
147 clockevents_register_device(&comparator
);
149 pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator
.name
,
150 ((counter_hz
+ 500) / 1000) / 1000,
151 ((counter_hz
+ 500) / 1000) % 1000);