| 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> | 
 | 14 | #include <linux/init.h> | 
 | 15 | #include <linux/delay.h> | 
 | 16 | #include <linux/interrupt.h> | 
 | 17 | #include <linux/time.h> | 
 | 18 | #include <linux/signal.h> | 
 | 19 | #include <linux/errno.h> | 
 | 20 | #include <linux/sched.h> | 
| Sascha Hauer | c80204e | 2006-12-12 09:21:50 +0100 | [diff] [blame] | 21 | #include <linux/clocksource.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 53 | static unsigned long initial_match; | 
 | 54 | static int match_posponed; | 
 | 55 | #endif | 
 | 56 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static irqreturn_t | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 58 | pxa_timer_interrupt(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { | 
 | 60 | 	int next_match; | 
 | 61 |  | 
 | 62 | 	write_seqlock(&xtime_lock); | 
 | 63 |  | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 64 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 65 | 	if (match_posponed) { | 
 | 66 | 		match_posponed = 0; | 
 | 67 | 		OSMR0 = initial_match; | 
 | 68 | 	} | 
 | 69 | #endif | 
 | 70 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | 	/* Loop until we get ahead of the free running timer. | 
 | 72 | 	 * This ensures an exact clock tick count and time accuracy. | 
| Nicolas Pitre | 20e9126 | 2005-09-01 12:48:47 +0100 | [diff] [blame] | 73 | 	 * Since IRQs are disabled at this point, coherence between | 
 | 74 | 	 * lost_ticks(updated in do_timer()) and the match reg value is | 
 | 75 | 	 * ensured, hence we can use do_gettimeofday() from interrupt | 
 | 76 | 	 * handlers. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | 	 * | 
 | 78 | 	 * HACK ALERT: it seems that the PXA timer regs aren't updated right | 
 | 79 | 	 * away in all cases when a write occurs.  We therefore compare with | 
 | 80 | 	 * 8 instead of 0 in the while() condition below to avoid missing a | 
 | 81 | 	 * match if OSCR has already reached the next OSMR value. | 
 | 82 | 	 * Experience has shown that up to 6 ticks are needed to work around | 
 | 83 | 	 * this problem, but let's use 8 to be conservative.  Note that this | 
 | 84 | 	 * affect things only when the timer IRQ has been delayed by nearly | 
 | 85 | 	 * exactly one tick period which should be a pretty rare event. | 
 | 86 | 	 */ | 
 | 87 | 	do { | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 88 | 		timer_tick(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | 		OSSR = OSSR_M0;  /* Clear match on timer 0 */ | 
 | 90 | 		next_match = (OSMR0 += LATCH); | 
 | 91 | 	} while( (signed long)(next_match - OSCR) <= 8 ); | 
 | 92 |  | 
 | 93 | 	write_sequnlock(&xtime_lock); | 
 | 94 |  | 
 | 95 | 	return IRQ_HANDLED; | 
 | 96 | } | 
 | 97 |  | 
 | 98 | static struct irqaction pxa_timer_irq = { | 
 | 99 | 	.name		= "PXA Timer Tick", | 
| Thomas Gleixner | 52e405e | 2006-07-03 02:20:05 +0200 | [diff] [blame] | 100 | 	.flags		= IRQF_DISABLED | IRQF_TIMER, | 
| Russell King | 09b8b5f | 2005-06-26 17:06:36 +0100 | [diff] [blame] | 101 | 	.handler	= pxa_timer_interrupt, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; | 
 | 103 |  | 
| Nicolas Pitre | 35108fb | 2006-12-22 18:36:30 +0100 | [diff] [blame] | 104 | static cycle_t pxa_get_cycles(void) | 
| Sascha Hauer | c80204e | 2006-12-12 09:21:50 +0100 | [diff] [blame] | 105 | { | 
 | 106 | 	return OSCR; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | static struct clocksource clocksource_pxa = { | 
 | 110 | 	.name           = "pxa_timer", | 
 | 111 | 	.rating         = 200, | 
 | 112 | 	.read           = pxa_get_cycles, | 
 | 113 | 	.mask           = CLOCKSOURCE_MASK(32), | 
 | 114 | 	.shift          = 20, | 
 | 115 | 	.is_continuous  = 1, | 
 | 116 | }; | 
 | 117 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | static void __init pxa_timer_init(void) | 
 | 119 | { | 
 | 120 | 	struct timespec tv; | 
| Nicolas Pitre | 46ec0ce | 2006-11-20 22:19:29 +0100 | [diff] [blame] | 121 | 	unsigned long flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 |  | 
 | 123 | 	set_rtc = pxa_set_rtc; | 
 | 124 |  | 
 | 125 | 	tv.tv_nsec = 0; | 
 | 126 | 	tv.tv_sec = pxa_get_rtc_time(); | 
 | 127 | 	do_settimeofday(&tv); | 
 | 128 |  | 
| Nicolas Pitre | 5285eb5 | 2005-11-08 22:43:06 +0000 | [diff] [blame] | 129 | 	OIER = 0;		/* disable any timer interrupts */ | 
| 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, &pxa_timer_irq); | 
| Nicolas Pitre | 46ec0ce | 2006-11-20 22:19:29 +0100 | [diff] [blame] | 132 | 	local_irq_save(flags); | 
| Nicolas Pitre | 5285eb5 | 2005-11-08 22:43:06 +0000 | [diff] [blame] | 133 | 	OIER = OIER_E0;		/* enable match on timer 0 to cause interrupts */ | 
| Nicolas Pitre | 46ec0ce | 2006-11-20 22:19:29 +0100 | [diff] [blame] | 134 | 	OSMR0 = OSCR + LATCH;	/* set initial match */ | 
 | 135 | 	local_irq_restore(flags); | 
| Sascha Hauer | c80204e | 2006-12-12 09:21:50 +0100 | [diff] [blame] | 136 |  | 
| Nicolas Pitre | 35108fb | 2006-12-22 18:36:30 +0100 | [diff] [blame] | 137 | 	/* | 
 | 138 | 	 * OSCR runs continuously on PXA and is not written to, | 
 | 139 | 	 * so we can use it as clock source directly. | 
| Sascha Hauer | c80204e | 2006-12-12 09:21:50 +0100 | [diff] [blame] | 140 | 	 */ | 
 | 141 | 	clocksource_pxa.mult = | 
 | 142 | 		clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift); | 
 | 143 | 	clocksource_register(&clocksource_pxa); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } | 
 | 145 |  | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 146 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 147 | static int pxa_dyn_tick_enable_disable(void) | 
 | 148 | { | 
 | 149 | 	/* nothing to do */ | 
 | 150 | 	return 0; | 
 | 151 | } | 
 | 152 |  | 
 | 153 | static void pxa_dyn_tick_reprogram(unsigned long ticks) | 
 | 154 | { | 
 | 155 | 	if (ticks > 1) { | 
 | 156 | 		initial_match = OSMR0; | 
 | 157 | 		OSMR0 = initial_match + ticks * LATCH; | 
 | 158 | 		match_posponed = 1; | 
 | 159 | 	} | 
 | 160 | } | 
 | 161 |  | 
 | 162 | static irqreturn_t | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 163 | pxa_dyn_tick_handler(int irq, void *dev_id) | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 164 | { | 
 | 165 | 	if (match_posponed) { | 
 | 166 | 		match_posponed = 0; | 
 | 167 | 		OSMR0 = initial_match; | 
 | 168 | 		if ( (signed long)(initial_match - OSCR) <= 8 ) | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 169 | 			return pxa_timer_interrupt(irq, dev_id); | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 170 | 	} | 
 | 171 | 	return IRQ_NONE; | 
 | 172 | } | 
 | 173 |  | 
 | 174 | static struct dyn_tick_timer pxa_dyn_tick = { | 
 | 175 | 	.enable		= pxa_dyn_tick_enable_disable, | 
 | 176 | 	.disable	= pxa_dyn_tick_enable_disable, | 
 | 177 | 	.reprogram	= pxa_dyn_tick_reprogram, | 
 | 178 | 	.handler	= pxa_dyn_tick_handler, | 
 | 179 | }; | 
 | 180 | #endif | 
 | 181 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | #ifdef CONFIG_PM | 
 | 183 | static unsigned long osmr[4], oier; | 
 | 184 |  | 
 | 185 | static void pxa_timer_suspend(void) | 
 | 186 | { | 
 | 187 | 	osmr[0] = OSMR0; | 
 | 188 | 	osmr[1] = OSMR1; | 
 | 189 | 	osmr[2] = OSMR2; | 
 | 190 | 	osmr[3] = OSMR3; | 
 | 191 | 	oier = OIER; | 
 | 192 | } | 
 | 193 |  | 
 | 194 | static void pxa_timer_resume(void) | 
 | 195 | { | 
 | 196 | 	OSMR0 = osmr[0]; | 
 | 197 | 	OSMR1 = osmr[1]; | 
 | 198 | 	OSMR2 = osmr[2]; | 
 | 199 | 	OSMR3 = osmr[3]; | 
 | 200 | 	OIER = oier; | 
 | 201 |  | 
 | 202 | 	/* | 
 | 203 | 	 * OSMR0 is the system timer: make sure OSCR is sufficiently behind | 
 | 204 | 	 */ | 
 | 205 | 	OSCR = OSMR0 - LATCH; | 
 | 206 | } | 
 | 207 | #else | 
 | 208 | #define pxa_timer_suspend NULL | 
 | 209 | #define pxa_timer_resume NULL | 
 | 210 | #endif | 
 | 211 |  | 
 | 212 | struct sys_timer pxa_timer = { | 
 | 213 | 	.init		= pxa_timer_init, | 
 | 214 | 	.suspend	= pxa_timer_suspend, | 
 | 215 | 	.resume		= pxa_timer_resume, | 
| Nicolas Pitre | 5c53ff0 | 2005-09-01 12:48:40 +0100 | [diff] [blame] | 216 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 217 | 	.dyn_tick	= &pxa_dyn_tick, | 
 | 218 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }; |