blob: 869a1c6ffeee944a64608d996877a78ee3499245 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +01002 * Copyright (C) 2004-2007 Atmel Corporation
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07003 *
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07004 * 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 */
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07008#include <linux/clk.h>
David Brownelle723ff62008-02-14 11:24:02 -08009#include <linux/clockchips.h>
Haavard Skinnemoen9dbef282008-05-28 13:07:40 +020010#include <linux/init.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070011#include <linux/interrupt.h>
12#include <linux/irq.h>
Haavard Skinnemoen9dbef282008-05-28 13:07:40 +020013#include <linux/kernel.h>
14#include <linux/time.h>
Thomas Gleixner01426472013-03-21 22:49:40 +010015#include <linux/cpu.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070016
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070017#include <asm/sysreg.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070018
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020019#include <mach/pm.h>
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +010020
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +010021
Magnus Damm8e196082009-04-21 12:24:00 -070022static cycle_t read_cycle_count(struct clocksource *cs)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070023{
24 return (cycle_t)sysreg_read(COUNT);
25}
26
David Brownell62c6df62008-02-12 14:45:49 -080027/*
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.
David Brownelle723ff62008-02-14 11:24:02 -080031 * Their duration also changes if cpufreq changes the CPU clock rate.
David Brownell62c6df62008-02-12 14:45:49 -080032 * So we rate the clocksource using COUNT as very low quality.
33 */
David Brownelle723ff62008-02-14 11:24:02 -080034static struct clocksource counter = {
35 .name = "avr32_counter",
David Brownell62c6df62008-02-12 14:45:49 -080036 .rating = 50,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070037 .read = read_cycle_count,
38 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner26935062007-02-16 01:27:38 -080039 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070040};
41
David Brownelle723ff62008-02-14 11:24:02 -080042static irqreturn_t timer_interrupt(int irq, void *dev_id)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070043{
David Brownelle723ff62008-02-14 11:24:02 -080044 struct clock_event_device *evdev = dev_id;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070045
Voss, Nikolaus56d3eef2008-07-18 14:44:48 +020046 if (unlikely(!(intc_get_pending(0) & 1)))
47 return IRQ_NONE;
48
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070049 /*
David Brownelle723ff62008-02-14 11:24:02 -080050 * Disable the interrupt until the clockevent subsystem
51 * reprograms it.
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070052 */
David Brownelle723ff62008-02-14 11:24:02 -080053 sysreg_write(COMPARE, 0);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070054
David Brownelle723ff62008-02-14 11:24:02 -080055 evdev->event_handler(evdev);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070056 return IRQ_HANDLED;
57}
58
David Brownelle723ff62008-02-14 11:24:02 -080059static struct irqaction timer_irqaction = {
60 .handler = timer_interrupt,
Voss, Nikolaus56d3eef2008-07-18 14:44:48 +020061 /* Oprofile uses the same irq as the timer, so allow it to be shared */
62 .flags = IRQF_TIMER | IRQF_DISABLED | IRQF_SHARED,
David Brownelle723ff62008-02-14 11:24:02 -080063 .name = "avr32_comparator",
64};
65
66static int comparator_next_event(unsigned long delta,
67 struct clock_event_device *evdev)
68{
69 unsigned long flags;
70
71 raw_local_irq_save(flags);
72
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);
77
78 raw_local_irq_restore(flags);
79
80 return 0;
81}
82
83static void comparator_mode(enum clock_event_mode mode,
84 struct clock_event_device *evdev)
85{
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:
Thomas Gleixner01426472013-03-21 22:49:40 +010091 /*
92 * If we're using the COUNT and COMPARE registers we
93 * need to force idle poll.
94 */
95 cpu_idle_poll_ctrl(true);
David Brownelle723ff62008-02-14 11:24:02 -080096 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);
Thomas Gleixner01426472013-03-21 22:49:40 +0100101 cpu_idle_poll_ctrl(false);
David Brownelle723ff62008-02-14 11:24:02 -0800102 break;
103 default:
104 BUG();
105 }
106}
107
108static struct clock_event_device comparator = {
109 .name = "avr32_comparator",
110 .features = CLOCK_EVT_FEAT_ONESHOT,
111 .shift = 16,
112 .rating = 50,
David Brownelle723ff62008-02-14 11:24:02 -0800113 .set_next_event = comparator_next_event,
114 .set_mode = comparator_mode,
115};
116
John Stultze2032a42010-03-03 19:57:21 -0800117void read_persistent_clock(struct timespec *ts)
118{
Peter Huewee9ddbc02010-04-27 15:23:01 +0200119 ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0);
John Stultze2032a42010-03-03 19:57:21 -0800120 ts->tv_nsec = 0;
121}
122
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700123void __init time_init(void)
124{
David Brownelle723ff62008-02-14 11:24:02 -0800125 unsigned long counter_hz;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700126 int ret;
127
David Brownelle723ff62008-02-14 11:24:02 -0800128 /* figure rate for counter */
129 counter_hz = clk_get_rate(boot_cpu_data.clk);
John Stultz1e2de472010-11-01 13:12:27 -0700130 ret = clocksource_register_hz(&counter, counter_hz);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700131 if (ret)
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100132 pr_debug("timer: could not register clocksource: %d\n", ret);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700133
David Brownelle723ff62008-02-14 11:24:02 -0800134 /* 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;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030138 comparator.cpumask = cpumask_of(0);
David Brownelle723ff62008-02-14 11:24:02 -0800139
140 sysreg_write(COMPARE, 0);
141 timer_irqaction.dev_id = &comparator;
142
143 ret = setup_irq(0, &timer_irqaction);
144 if (ret)
145 pr_debug("timer: could not request IRQ 0: %d\n", ret);
146 else {
147 clockevents_register_device(&comparator);
148
149 pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
150 ((counter_hz + 500) / 1000) / 1000,
151 ((counter_hz + 500) / 1000) % 1000);
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100152 }
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700153}