Commit | Line | Data |
---|---|---|
5f97f7f9 | 1 | /* |
7760989e | 2 | * Copyright (C) 2004-2007 Atmel Corporation |
5f97f7f9 | 3 | * |
5f97f7f9 HS |
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 | */ | |
5f97f7f9 | 8 | #include <linux/clk.h> |
e723ff66 | 9 | #include <linux/clockchips.h> |
9dbef285 | 10 | #include <linux/init.h> |
5f97f7f9 HS |
11 | #include <linux/interrupt.h> |
12 | #include <linux/irq.h> | |
9dbef285 HS |
13 | #include <linux/kernel.h> |
14 | #include <linux/time.h> | |
01426478 | 15 | #include <linux/cpu.h> |
5f97f7f9 | 16 | |
5f97f7f9 | 17 | #include <asm/sysreg.h> |
5f97f7f9 | 18 | |
3663b736 | 19 | #include <mach/pm.h> |
7760989e | 20 | |
7760989e | 21 | |
8e19608e | 22 | static cycle_t read_cycle_count(struct clocksource *cs) |
5f97f7f9 HS |
23 | { |
24 | return (cycle_t)sysreg_read(COUNT); | |
25 | } | |
26 | ||
62c6df62 DB |
27 | /* |
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. | |
e723ff66 | 31 | * Their duration also changes if cpufreq changes the CPU clock rate. |
62c6df62 DB |
32 | * So we rate the clocksource using COUNT as very low quality. |
33 | */ | |
e723ff66 DB |
34 | static struct clocksource counter = { |
35 | .name = "avr32_counter", | |
62c6df62 | 36 | .rating = 50, |
5f97f7f9 HS |
37 | .read = read_cycle_count, |
38 | .mask = CLOCKSOURCE_MASK(32), | |
2693506c | 39 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
5f97f7f9 HS |
40 | }; |
41 | ||
e723ff66 | 42 | static irqreturn_t timer_interrupt(int irq, void *dev_id) |
5f97f7f9 | 43 | { |
e723ff66 | 44 | struct clock_event_device *evdev = dev_id; |
7760989e | 45 | |
56d3eef2 VN |
46 | if (unlikely(!(intc_get_pending(0) & 1))) |
47 | return IRQ_NONE; | |
48 | ||
e723ff66 DB |
49 | /* |
50 | * Disable the interrupt until the clockevent subsystem | |
51 | * reprograms it. | |
52 | */ | |
53 | sysreg_write(COMPARE, 0); | |
7760989e | 54 | |
e723ff66 DB |
55 | evdev->event_handler(evdev); |
56 | return IRQ_HANDLED; | |
57 | } | |
7760989e | 58 | |
e723ff66 DB |
59 | static struct irqaction timer_irqaction = { |
60 | .handler = timer_interrupt, | |
56d3eef2 | 61 | /* Oprofile uses the same irq as the timer, so allow it to be shared */ |
7155530d | 62 | .flags = IRQF_TIMER | IRQF_SHARED, |
e723ff66 DB |
63 | .name = "avr32_comparator", |
64 | }; | |
7760989e | 65 | |
e723ff66 DB |
66 | static int comparator_next_event(unsigned long delta, |
67 | struct clock_event_device *evdev) | |
68 | { | |
69 | unsigned long flags; | |
7760989e | 70 | |
e723ff66 | 71 | raw_local_irq_save(flags); |
7760989e | 72 | |
e723ff66 DB |
73 | /* The time to read COUNT then update COMPARE must be less |
74 | * than the min_delta_ns value for this clockevent source. | |
75 | */ | |
76 | sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1); | |
5f97f7f9 | 77 | |
e723ff66 | 78 | raw_local_irq_restore(flags); |
7760989e HCE |
79 | |
80 | return 0; | |
5f97f7f9 HS |
81 | } |
82 | ||
e723ff66 DB |
83 | static void comparator_mode(enum clock_event_mode mode, |
84 | struct clock_event_device *evdev) | |
5f97f7f9 | 85 | { |
e723ff66 DB |
86 | switch (mode) { |
87 | case CLOCK_EVT_MODE_ONESHOT: | |
88 | pr_debug("%s: start\n", evdev->name); | |
89 | /* FALLTHROUGH */ | |
90 | case CLOCK_EVT_MODE_RESUME: | |
01426478 TG |
91 | /* |
92 | * If we're using the COUNT and COMPARE registers we | |
93 | * need to force idle poll. | |
94 | */ | |
95 | cpu_idle_poll_ctrl(true); | |
e723ff66 DB |
96 | break; |
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); | |
1b0135b5 GJ |
101 | if (evdev->mode == CLOCK_EVT_MODE_ONESHOT || |
102 | evdev->mode == CLOCK_EVT_MODE_RESUME) { | |
103 | /* | |
104 | * Only disable idle poll if we have forced that | |
105 | * in a previous call. | |
106 | */ | |
107 | cpu_idle_poll_ctrl(false); | |
108 | } | |
e723ff66 DB |
109 | break; |
110 | default: | |
111 | BUG(); | |
112 | } | |
5f97f7f9 HS |
113 | } |
114 | ||
e723ff66 DB |
115 | static struct clock_event_device comparator = { |
116 | .name = "avr32_comparator", | |
117 | .features = CLOCK_EVT_FEAT_ONESHOT, | |
118 | .shift = 16, | |
119 | .rating = 50, | |
e723ff66 DB |
120 | .set_next_event = comparator_next_event, |
121 | .set_mode = comparator_mode, | |
122 | }; | |
5f97f7f9 | 123 | |
e2032a4a JS |
124 | void read_persistent_clock(struct timespec *ts) |
125 | { | |
e9ddbc07 | 126 | ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0); |
e2032a4a JS |
127 | ts->tv_nsec = 0; |
128 | } | |
129 | ||
5f97f7f9 HS |
130 | void __init time_init(void) |
131 | { | |
e723ff66 | 132 | unsigned long counter_hz; |
5f97f7f9 HS |
133 | int ret; |
134 | ||
e723ff66 DB |
135 | /* figure rate for counter */ |
136 | counter_hz = clk_get_rate(boot_cpu_data.clk); | |
1e2de47c | 137 | ret = clocksource_register_hz(&counter, counter_hz); |
5f97f7f9 | 138 | if (ret) |
7760989e | 139 | pr_debug("timer: could not register clocksource: %d\n", ret); |
5f97f7f9 | 140 | |
e723ff66 DB |
141 | /* setup COMPARE clockevent */ |
142 | comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift); | |
143 | comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator); | |
144 | comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1; | |
320ab2b0 | 145 | comparator.cpumask = cpumask_of(0); |
e723ff66 DB |
146 | |
147 | sysreg_write(COMPARE, 0); | |
148 | timer_irqaction.dev_id = &comparator; | |
149 | ||
150 | ret = setup_irq(0, &timer_irqaction); | |
151 | if (ret) | |
152 | pr_debug("timer: could not request IRQ 0: %d\n", ret); | |
153 | else { | |
154 | clockevents_register_device(&comparator); | |
155 | ||
156 | pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name, | |
157 | ((counter_hz + 500) / 1000) / 1000, | |
158 | ((counter_hz + 500) / 1000) % 1000); | |
7760989e | 159 | } |
5f97f7f9 | 160 | } |