Merge branch 'sched/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[pandora-kernel.git] / arch / avr32 / kernel / time.c
1 /*
2  * Copyright (C) 2004-2007 Atmel Corporation
3  *
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.
7  */
8 #include <linux/clk.h>
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
16 #include <asm/sysreg.h>
17
18 #include <asm/arch/pm.h>
19
20
21 static cycle_t read_cycle_count(void)
22 {
23         return (cycle_t)sysreg_read(COUNT);
24 }
25
26 /*
27  * The architectural cycle count registers are a fine clocksource unless
28  * the system idle loop use sleep states like "idle":  the CPU cycles
29  * measured by COUNT (and COMPARE) don't happen during sleep states.
30  * Their duration also changes if cpufreq changes the CPU clock rate.
31  * So we rate the clocksource using COUNT as very low quality.
32  */
33 static struct clocksource counter = {
34         .name           = "avr32_counter",
35         .rating         = 50,
36         .read           = read_cycle_count,
37         .mask           = CLOCKSOURCE_MASK(32),
38         .shift          = 16,
39         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
40 };
41
42 static irqreturn_t timer_interrupt(int irq, void *dev_id)
43 {
44         struct clock_event_device *evdev = dev_id;
45
46         /*
47          * Disable the interrupt until the clockevent subsystem
48          * reprograms it.
49          */
50         sysreg_write(COMPARE, 0);
51
52         evdev->event_handler(evdev);
53         return IRQ_HANDLED;
54 }
55
56 static struct irqaction timer_irqaction = {
57         .handler        = timer_interrupt,
58         .flags          = IRQF_TIMER | IRQF_DISABLED,
59         .name           = "avr32_comparator",
60 };
61
62 static int comparator_next_event(unsigned long delta,
63                 struct clock_event_device *evdev)
64 {
65         unsigned long   flags;
66
67         raw_local_irq_save(flags);
68
69         /* The time to read COUNT then update COMPARE must be less
70          * than the min_delta_ns value for this clockevent source.
71          */
72         sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1);
73
74         raw_local_irq_restore(flags);
75
76         return 0;
77 }
78
79 static void comparator_mode(enum clock_event_mode mode,
80                 struct clock_event_device *evdev)
81 {
82         switch (mode) {
83         case CLOCK_EVT_MODE_ONESHOT:
84                 pr_debug("%s: start\n", evdev->name);
85                 /* FALLTHROUGH */
86         case CLOCK_EVT_MODE_RESUME:
87                 cpu_disable_idle_sleep();
88                 break;
89         case CLOCK_EVT_MODE_UNUSED:
90         case CLOCK_EVT_MODE_SHUTDOWN:
91                 sysreg_write(COMPARE, 0);
92                 pr_debug("%s: stop\n", evdev->name);
93                 cpu_enable_idle_sleep();
94                 break;
95         default:
96                 BUG();
97         }
98 }
99
100 static struct clock_event_device comparator = {
101         .name           = "avr32_comparator",
102         .features       = CLOCK_EVT_FEAT_ONESHOT,
103         .shift          = 16,
104         .rating         = 50,
105         .cpumask        = CPU_MASK_CPU0,
106         .set_next_event = comparator_next_event,
107         .set_mode       = comparator_mode,
108 };
109
110 void __init time_init(void)
111 {
112         unsigned long counter_hz;
113         int ret;
114
115         xtime.tv_sec = mktime(2007, 1, 1, 0, 0, 0);
116         xtime.tv_nsec = 0;
117
118         set_normalized_timespec(&wall_to_monotonic,
119                                 -xtime.tv_sec, -xtime.tv_nsec);
120
121         /* figure rate for counter */
122         counter_hz = clk_get_rate(boot_cpu_data.clk);
123         counter.mult = clocksource_hz2mult(counter_hz, counter.shift);
124
125         ret = clocksource_register(&counter);
126         if (ret)
127                 pr_debug("timer: could not register clocksource: %d\n", ret);
128
129         /* setup COMPARE clockevent */
130         comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift);
131         comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator);
132         comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1;
133
134         sysreg_write(COMPARE, 0);
135         timer_irqaction.dev_id = &comparator;
136
137         ret = setup_irq(0, &timer_irqaction);
138         if (ret)
139                 pr_debug("timer: could not request IRQ 0: %d\n", ret);
140         else {
141                 clockevents_register_device(&comparator);
142
143                 pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
144                                 ((counter_hz + 500) / 1000) / 1000,
145                                 ((counter_hz + 500) / 1000) % 1000);
146         }
147 }