Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
Hans-Christian Egtvedt | 7760989 | 2007-03-12 18:15:16 +0100 | [diff] [blame] | 2 | * Copyright (C) 2004-2007 Atmel Corporation |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 3 | * |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 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 | */ |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 8 | #include <linux/clk.h> |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 9 | #include <linux/clockchips.h> |
Haavard Skinnemoen | 9dbef28 | 2008-05-28 13:07:40 +0200 | [diff] [blame] | 10 | #include <linux/init.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/irq.h> |
Haavard Skinnemoen | 9dbef28 | 2008-05-28 13:07:40 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/time.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 15 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 16 | #include <asm/sysreg.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 17 | |
Haavard Skinnemoen | 3663b73 | 2008-08-05 13:57:38 +0200 | [diff] [blame] | 18 | #include <mach/pm.h> |
Hans-Christian Egtvedt | 7760989 | 2007-03-12 18:15:16 +0100 | [diff] [blame] | 19 | |
Hans-Christian Egtvedt | 7760989 | 2007-03-12 18:15:16 +0100 | [diff] [blame] | 20 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 21 | static cycle_t read_cycle_count(struct clocksource *cs) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 22 | { |
| 23 | return (cycle_t)sysreg_read(COUNT); |
| 24 | } |
| 25 | |
David Brownell | 62c6df6 | 2008-02-12 14:45:49 -0800 | [diff] [blame] | 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. |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 30 | * Their duration also changes if cpufreq changes the CPU clock rate. |
David Brownell | 62c6df6 | 2008-02-12 14:45:49 -0800 | [diff] [blame] | 31 | * So we rate the clocksource using COUNT as very low quality. |
| 32 | */ |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 33 | static struct clocksource counter = { |
| 34 | .name = "avr32_counter", |
David Brownell | 62c6df6 | 2008-02-12 14:45:49 -0800 | [diff] [blame] | 35 | .rating = 50, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 36 | .read = read_cycle_count, |
| 37 | .mask = CLOCKSOURCE_MASK(32), |
Thomas Gleixner | 2693506c4 | 2007-02-16 01:27:38 -0800 | [diff] [blame] | 38 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 41 | static irqreturn_t timer_interrupt(int irq, void *dev_id) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 42 | { |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 43 | struct clock_event_device *evdev = dev_id; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 44 | |
Voss, Nikolaus | 56d3eef | 2008-07-18 14:44:48 +0200 | [diff] [blame] | 45 | if (unlikely(!(intc_get_pending(0) & 1))) |
| 46 | return IRQ_NONE; |
| 47 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 48 | /* |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 49 | * Disable the interrupt until the clockevent subsystem |
| 50 | * reprograms it. |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 51 | */ |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 52 | sysreg_write(COMPARE, 0); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 53 | |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 54 | evdev->event_handler(evdev); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 55 | return IRQ_HANDLED; |
| 56 | } |
| 57 | |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 58 | static struct irqaction timer_irqaction = { |
| 59 | .handler = timer_interrupt, |
Voss, Nikolaus | 56d3eef | 2008-07-18 14:44:48 +0200 | [diff] [blame] | 60 | /* Oprofile uses the same irq as the timer, so allow it to be shared */ |
| 61 | .flags = IRQF_TIMER | IRQF_DISABLED | IRQF_SHARED, |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 62 | .name = "avr32_comparator", |
| 63 | }; |
| 64 | |
| 65 | static int comparator_next_event(unsigned long delta, |
| 66 | struct clock_event_device *evdev) |
| 67 | { |
| 68 | unsigned long flags; |
| 69 | |
| 70 | raw_local_irq_save(flags); |
| 71 | |
| 72 | /* The time to read COUNT then update COMPARE must be less |
| 73 | * than the min_delta_ns value for this clockevent source. |
| 74 | */ |
| 75 | sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1); |
| 76 | |
| 77 | raw_local_irq_restore(flags); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void comparator_mode(enum clock_event_mode mode, |
| 83 | struct clock_event_device *evdev) |
| 84 | { |
| 85 | switch (mode) { |
| 86 | case CLOCK_EVT_MODE_ONESHOT: |
| 87 | pr_debug("%s: start\n", evdev->name); |
| 88 | /* FALLTHROUGH */ |
| 89 | case CLOCK_EVT_MODE_RESUME: |
| 90 | cpu_disable_idle_sleep(); |
| 91 | break; |
| 92 | case CLOCK_EVT_MODE_UNUSED: |
| 93 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 94 | sysreg_write(COMPARE, 0); |
| 95 | pr_debug("%s: stop\n", evdev->name); |
| 96 | cpu_enable_idle_sleep(); |
| 97 | break; |
| 98 | default: |
| 99 | BUG(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static struct clock_event_device comparator = { |
| 104 | .name = "avr32_comparator", |
| 105 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 106 | .shift = 16, |
| 107 | .rating = 50, |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 108 | .set_next_event = comparator_next_event, |
| 109 | .set_mode = comparator_mode, |
| 110 | }; |
| 111 | |
John Stultz | e2032a4 | 2010-03-03 19:57:21 -0800 | [diff] [blame] | 112 | void read_persistent_clock(struct timespec *ts) |
| 113 | { |
Peter Huewe | e9ddbc0 | 2010-04-27 15:23:01 +0200 | [diff] [blame] | 114 | ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0); |
John Stultz | e2032a4 | 2010-03-03 19:57:21 -0800 | [diff] [blame] | 115 | ts->tv_nsec = 0; |
| 116 | } |
| 117 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 118 | void __init time_init(void) |
| 119 | { |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 120 | unsigned long counter_hz; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 121 | int ret; |
| 122 | |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 123 | /* figure rate for counter */ |
| 124 | counter_hz = clk_get_rate(boot_cpu_data.clk); |
John Stultz | 1e2de47 | 2010-11-01 13:12:27 -0700 | [diff] [blame] | 125 | ret = clocksource_register_hz(&counter, counter_hz); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 126 | if (ret) |
Hans-Christian Egtvedt | 7760989 | 2007-03-12 18:15:16 +0100 | [diff] [blame] | 127 | pr_debug("timer: could not register clocksource: %d\n", ret); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 128 | |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 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; |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 133 | comparator.cpumask = cpumask_of(0); |
David Brownell | e723ff6 | 2008-02-14 11:24:02 -0800 | [diff] [blame] | 134 | |
| 135 | sysreg_write(COMPARE, 0); |
| 136 | timer_irqaction.dev_id = &comparator; |
| 137 | |
| 138 | ret = setup_irq(0, &timer_irqaction); |
| 139 | if (ret) |
| 140 | pr_debug("timer: could not request IRQ 0: %d\n", ret); |
| 141 | else { |
| 142 | clockevents_register_device(&comparator); |
| 143 | |
| 144 | pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name, |
| 145 | ((counter_hz + 500) / 1000) / 1000, |
| 146 | ((counter_hz + 500) / 1000) % 1000); |
Hans-Christian Egtvedt | 7760989 | 2007-03-12 18:15:16 +0100 | [diff] [blame] | 147 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 148 | } |