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