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