Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/mach-pxa/time.c |
| 3 | * |
| 4 | * Author: Nicolas Pitre |
| 5 | * Created: Jun 15, 2001 |
| 6 | * Copyright: MontaVista Software Inc. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/time.h> |
| 19 | #include <linux/signal.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/sched.h> |
| 22 | |
| 23 | #include <asm/system.h> |
| 24 | #include <asm/hardware.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/leds.h> |
| 27 | #include <asm/irq.h> |
| 28 | #include <asm/mach/irq.h> |
| 29 | #include <asm/mach/time.h> |
| 30 | #include <asm/arch/pxa-regs.h> |
| 31 | |
| 32 | |
| 33 | static inline unsigned long pxa_get_rtc_time(void) |
| 34 | { |
| 35 | return RCNR; |
| 36 | } |
| 37 | |
| 38 | static int pxa_set_rtc(void) |
| 39 | { |
| 40 | unsigned long current_time = xtime.tv_sec; |
| 41 | |
| 42 | if (RTSR & RTSR_ALE) { |
| 43 | /* make sure not to forward the clock over an alarm */ |
| 44 | unsigned long alarm = RTAR; |
| 45 | if (current_time >= alarm && alarm >= RCNR) |
| 46 | return -ERESTARTSYS; |
| 47 | } |
| 48 | RCNR = current_time; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* IRQs are disabled before entering here from do_gettimeofday() */ |
| 53 | static unsigned long pxa_gettimeoffset (void) |
| 54 | { |
| 55 | long ticks_to_match, elapsed, usec; |
| 56 | |
| 57 | /* Get ticks before next timer match */ |
| 58 | ticks_to_match = OSMR0 - OSCR; |
| 59 | |
| 60 | /* We need elapsed ticks since last match */ |
| 61 | elapsed = LATCH - ticks_to_match; |
| 62 | |
| 63 | /* don't get fooled by the workaround in pxa_timer_interrupt() */ |
| 64 | if (elapsed <= 0) |
| 65 | return 0; |
| 66 | |
| 67 | /* Now convert them to usec */ |
| 68 | usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH; |
| 69 | |
| 70 | return usec; |
| 71 | } |
| 72 | |
Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +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 | pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
| 80 | { |
| 81 | int next_match; |
| 82 | |
| 83 | write_seqlock(&xtime_lock); |
| 84 | |
Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* Loop until we get ahead of the free running timer. |
| 93 | * This ensures an exact clock tick count and time accuracy. |
Nicolas Pitre | 20e9126 | 2005-09-01 12:48:47 +0100 | [diff] [blame] | 94 | * Since IRQs are disabled at this point, coherence between |
| 95 | * lost_ticks(updated in do_timer()) and the match reg value is |
| 96 | * ensured, hence we can use do_gettimeofday() from interrupt |
| 97 | * handlers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * |
| 99 | * HACK ALERT: it seems that the PXA timer regs aren't updated right |
| 100 | * away in all cases when a write occurs. We therefore compare with |
| 101 | * 8 instead of 0 in the while() condition below to avoid missing a |
| 102 | * match if OSCR has already reached the next OSMR value. |
| 103 | * Experience has shown that up to 6 ticks are needed to work around |
| 104 | * this problem, but let's use 8 to be conservative. Note that this |
| 105 | * affect things only when the timer IRQ has been delayed by nearly |
| 106 | * exactly one tick period which should be a pretty rare event. |
| 107 | */ |
| 108 | do { |
| 109 | timer_tick(regs); |
| 110 | OSSR = OSSR_M0; /* Clear match on timer 0 */ |
| 111 | next_match = (OSMR0 += LATCH); |
| 112 | } while( (signed long)(next_match - OSCR) <= 8 ); |
| 113 | |
| 114 | write_sequnlock(&xtime_lock); |
| 115 | |
| 116 | return IRQ_HANDLED; |
| 117 | } |
| 118 | |
| 119 | static struct irqaction pxa_timer_irq = { |
| 120 | .name = "PXA Timer Tick", |
Russell King | 09b8b5f | 2005-06-26 17:06:36 +0100 | [diff] [blame] | 121 | .flags = SA_INTERRUPT | SA_TIMER, |
| 122 | .handler = pxa_timer_interrupt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static void __init pxa_timer_init(void) |
| 126 | { |
| 127 | struct timespec tv; |
| 128 | |
| 129 | set_rtc = pxa_set_rtc; |
| 130 | |
| 131 | tv.tv_nsec = 0; |
| 132 | tv.tv_sec = pxa_get_rtc_time(); |
| 133 | do_settimeofday(&tv); |
| 134 | |
Nicolas Pitre | 5285eb5 | 2005-11-08 22:43:06 +0000 | [diff] [blame^] | 135 | OIER = 0; /* disable any timer interrupts */ |
| 136 | OSCR = LATCH*2; /* push OSCR out of the way */ |
| 137 | OSMR0 = LATCH; /* set initial match */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | OSSR = 0xf; /* clear status on all timers */ |
| 139 | setup_irq(IRQ_OST0, &pxa_timer_irq); |
Nicolas Pitre | 5285eb5 | 2005-11-08 22:43:06 +0000 | [diff] [blame^] | 140 | OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */ |
| 141 | OSCR = 0; /* initialize free-running timer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 144 | #ifdef CONFIG_NO_IDLE_HZ |
| 145 | static int pxa_dyn_tick_enable_disable(void) |
| 146 | { |
| 147 | /* nothing to do */ |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void pxa_dyn_tick_reprogram(unsigned long ticks) |
| 152 | { |
| 153 | if (ticks > 1) { |
| 154 | initial_match = OSMR0; |
| 155 | OSMR0 = initial_match + ticks * LATCH; |
| 156 | match_posponed = 1; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static irqreturn_t |
| 161 | pxa_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs) |
| 162 | { |
| 163 | if (match_posponed) { |
| 164 | match_posponed = 0; |
| 165 | OSMR0 = initial_match; |
| 166 | if ( (signed long)(initial_match - OSCR) <= 8 ) |
| 167 | return pxa_timer_interrupt(irq, dev_id, regs); |
| 168 | } |
| 169 | return IRQ_NONE; |
| 170 | } |
| 171 | |
| 172 | static struct dyn_tick_timer pxa_dyn_tick = { |
| 173 | .enable = pxa_dyn_tick_enable_disable, |
| 174 | .disable = pxa_dyn_tick_enable_disable, |
| 175 | .reprogram = pxa_dyn_tick_reprogram, |
| 176 | .handler = pxa_dyn_tick_handler, |
| 177 | }; |
| 178 | #endif |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | #ifdef CONFIG_PM |
| 181 | static unsigned long osmr[4], oier; |
| 182 | |
| 183 | static void pxa_timer_suspend(void) |
| 184 | { |
| 185 | osmr[0] = OSMR0; |
| 186 | osmr[1] = OSMR1; |
| 187 | osmr[2] = OSMR2; |
| 188 | osmr[3] = OSMR3; |
| 189 | oier = OIER; |
| 190 | } |
| 191 | |
| 192 | static void pxa_timer_resume(void) |
| 193 | { |
| 194 | OSMR0 = osmr[0]; |
| 195 | OSMR1 = osmr[1]; |
| 196 | OSMR2 = osmr[2]; |
| 197 | OSMR3 = osmr[3]; |
| 198 | OIER = oier; |
| 199 | |
| 200 | /* |
| 201 | * OSMR0 is the system timer: make sure OSCR is sufficiently behind |
| 202 | */ |
| 203 | OSCR = OSMR0 - LATCH; |
| 204 | } |
| 205 | #else |
| 206 | #define pxa_timer_suspend NULL |
| 207 | #define pxa_timer_resume NULL |
| 208 | #endif |
| 209 | |
| 210 | struct sys_timer pxa_timer = { |
| 211 | .init = pxa_timer_init, |
| 212 | .suspend = pxa_timer_suspend, |
| 213 | .resume = pxa_timer_resume, |
| 214 | .offset = pxa_gettimeoffset, |
Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 215 | #ifdef CONFIG_NO_IDLE_HZ |
| 216 | .dyn_tick = &pxa_dyn_tick, |
| 217 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | }; |