Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-sa1100/time.c |
| 3 | * |
| 4 | * Copyright (C) 1998 Deborah Wallach. |
| 5 | * Twiddles (C) 1999 Hugo Fiennes <hugo@empeg.com> |
| 6 | * |
| 7 | * 2000/03/29 (C) Nicolas Pitre <nico@cam.org> |
| 8 | * Rewritten: big cleanup, much simpler, better HZ accuracy. |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/interrupt.h> |
Thomas Gleixner | 119c641 | 2006-07-01 22:32:38 +0100 | [diff] [blame] | 14 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/timex.h> |
| 16 | #include <linux/signal.h> |
Russell King | d142b6e7 | 2007-11-12 21:55:12 +0000 | [diff] [blame^] | 17 | #include <linux/clocksource.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/mach/time.h> |
| 20 | #include <asm/hardware.h> |
| 21 | |
| 22 | #define RTC_DEF_DIVIDER (32768 - 1) |
| 23 | #define RTC_DEF_TRIM 0 |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static int sa1100_set_rtc(void) |
| 26 | { |
| 27 | unsigned long current_time = xtime.tv_sec; |
| 28 | |
| 29 | if (RTSR & RTSR_ALE) { |
| 30 | /* make sure not to forward the clock over an alarm */ |
| 31 | unsigned long alarm = RTAR; |
| 32 | if (current_time >= alarm && alarm >= RCNR) |
| 33 | return -ERESTARTSYS; |
| 34 | } |
| 35 | RCNR = current_time; |
| 36 | return 0; |
| 37 | } |
| 38 | |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 39 | #ifdef CONFIG_NO_IDLE_HZ |
| 40 | static unsigned long initial_match; |
| 41 | static int match_posponed; |
| 42 | #endif |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static irqreturn_t |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 45 | sa1100_timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | unsigned int next_match; |
| 48 | |
| 49 | write_seqlock(&xtime_lock); |
| 50 | |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 51 | #ifdef CONFIG_NO_IDLE_HZ |
| 52 | if (match_posponed) { |
| 53 | match_posponed = 0; |
| 54 | OSMR0 = initial_match; |
| 55 | } |
| 56 | #endif |
| 57 | |
Nicolas Pitre | 20e9126 | 2005-09-01 12:48:47 +0100 | [diff] [blame] | 58 | /* |
| 59 | * Loop until we get ahead of the free running timer. |
| 60 | * This ensures an exact clock tick count and time accuracy. |
| 61 | * Since IRQs are disabled at this point, coherence between |
| 62 | * lost_ticks(updated in do_timer()) and the match reg value is |
| 63 | * ensured, hence we can use do_gettimeofday() from interrupt |
| 64 | * handlers. |
| 65 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | do { |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 67 | timer_tick(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | OSSR = OSSR_M0; /* Clear match on timer 0 */ |
| 69 | next_match = (OSMR0 += LATCH); |
| 70 | } while ((signed long)(next_match - OSCR) <= 0); |
| 71 | |
| 72 | write_sequnlock(&xtime_lock); |
| 73 | |
| 74 | return IRQ_HANDLED; |
| 75 | } |
| 76 | |
| 77 | static struct irqaction sa1100_timer_irq = { |
| 78 | .name = "SA11xx Timer Tick", |
Bernhard Walle | b30faba | 2007-05-08 00:35:39 -0700 | [diff] [blame] | 79 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
Russell King | 09b8b5f | 2005-06-26 17:06:36 +0100 | [diff] [blame] | 80 | .handler = sa1100_timer_interrupt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Russell King | d142b6e7 | 2007-11-12 21:55:12 +0000 | [diff] [blame^] | 83 | static cycle_t sa1100_read_oscr(void) |
| 84 | { |
| 85 | return OSCR; |
| 86 | } |
| 87 | |
| 88 | static struct clocksource cksrc_sa1100_oscr = { |
| 89 | .name = "oscr", |
| 90 | .rating = 200, |
| 91 | .read = sa1100_read_oscr, |
| 92 | .mask = CLOCKSOURCE_MASK(32), |
| 93 | .shift = 20, |
| 94 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 95 | }; |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | static void __init sa1100_timer_init(void) |
| 98 | { |
Nicolas Pitre | bf46878 | 2006-11-20 22:17:09 +0100 | [diff] [blame] | 99 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | set_rtc = sa1100_set_rtc; |
| 102 | |
Nicolas Pitre | 5285eb57 | 2005-11-08 22:43:06 +0000 | [diff] [blame] | 103 | OIER = 0; /* disable any timer interrupts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | OSSR = 0xf; /* clear status on all timers */ |
| 105 | setup_irq(IRQ_OST0, &sa1100_timer_irq); |
Nicolas Pitre | bf46878 | 2006-11-20 22:17:09 +0100 | [diff] [blame] | 106 | local_irq_save(flags); |
Nicolas Pitre | 5285eb57 | 2005-11-08 22:43:06 +0000 | [diff] [blame] | 107 | OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */ |
Nicolas Pitre | bf46878 | 2006-11-20 22:17:09 +0100 | [diff] [blame] | 108 | OSMR0 = OSCR + LATCH; /* set initial match */ |
| 109 | local_irq_restore(flags); |
Russell King | d142b6e7 | 2007-11-12 21:55:12 +0000 | [diff] [blame^] | 110 | |
| 111 | cksrc_sa1100_oscr.mult = |
| 112 | clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_sa1100_oscr.shift); |
| 113 | |
| 114 | clocksource_register(&cksrc_sa1100_oscr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 117 | #ifdef CONFIG_NO_IDLE_HZ |
| 118 | static int sa1100_dyn_tick_enable_disable(void) |
| 119 | { |
| 120 | /* nothing to do */ |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static void sa1100_dyn_tick_reprogram(unsigned long ticks) |
| 125 | { |
| 126 | if (ticks > 1) { |
| 127 | initial_match = OSMR0; |
| 128 | OSMR0 = initial_match + ticks * LATCH; |
| 129 | match_posponed = 1; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static irqreturn_t |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 134 | sa1100_dyn_tick_handler(int irq, void *dev_id) |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 135 | { |
| 136 | if (match_posponed) { |
| 137 | match_posponed = 0; |
| 138 | OSMR0 = initial_match; |
| 139 | if ((signed long)(initial_match - OSCR) <= 0) |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 140 | return sa1100_timer_interrupt(irq, dev_id); |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 141 | } |
| 142 | return IRQ_NONE; |
| 143 | } |
| 144 | |
| 145 | static struct dyn_tick_timer sa1100_dyn_tick = { |
| 146 | .enable = sa1100_dyn_tick_enable_disable, |
| 147 | .disable = sa1100_dyn_tick_enable_disable, |
| 148 | .reprogram = sa1100_dyn_tick_reprogram, |
| 149 | .handler = sa1100_dyn_tick_handler, |
| 150 | }; |
| 151 | #endif |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #ifdef CONFIG_PM |
| 154 | unsigned long osmr[4], oier; |
| 155 | |
| 156 | static void sa1100_timer_suspend(void) |
| 157 | { |
| 158 | osmr[0] = OSMR0; |
| 159 | osmr[1] = OSMR1; |
| 160 | osmr[2] = OSMR2; |
| 161 | osmr[3] = OSMR3; |
| 162 | oier = OIER; |
| 163 | } |
| 164 | |
| 165 | static void sa1100_timer_resume(void) |
| 166 | { |
| 167 | OSSR = 0x0f; |
| 168 | OSMR0 = osmr[0]; |
| 169 | OSMR1 = osmr[1]; |
| 170 | OSMR2 = osmr[2]; |
| 171 | OSMR3 = osmr[3]; |
| 172 | OIER = oier; |
| 173 | |
| 174 | /* |
| 175 | * OSMR0 is the system timer: make sure OSCR is sufficiently behind |
| 176 | */ |
| 177 | OSCR = OSMR0 - LATCH; |
| 178 | } |
| 179 | #else |
| 180 | #define sa1100_timer_suspend NULL |
| 181 | #define sa1100_timer_resume NULL |
| 182 | #endif |
| 183 | |
| 184 | struct sys_timer sa1100_timer = { |
| 185 | .init = sa1100_timer_init, |
| 186 | .suspend = sa1100_timer_suspend, |
| 187 | .resume = sa1100_timer_resume, |
Nicolas Pitre | 569d2c3 | 2005-09-01 12:48:48 +0100 | [diff] [blame] | 188 | #ifdef CONFIG_NO_IDLE_HZ |
| 189 | .dyn_tick = &sa1100_dyn_tick, |
| 190 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | }; |