| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/arch/arm26/kernel/time.c | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds | 
 | 5 |  *  Modifications for ARM (C) 1994-2001 Russell King | 
 | 6 |  *  Mods for ARM26 (C) 2003 Ian Molton | 
 | 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 |  *  This file contains the ARM-specific time handling details: | 
 | 13 |  *  reading the RTC at bootup, etc... | 
 | 14 |  * | 
 | 15 |  *  1994-07-02  Alan Modra | 
 | 16 |  *              fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime | 
 | 17 |  *  1998-12-20  Updated NTP code according to technical memorandum Jan '96 | 
 | 18 |  *              "A Kernel Model for Precision Timekeeping" by Dave Mills | 
 | 19 |  */ | 
 | 20 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> | 
 | 22 | #include <linux/kernel.h> | 
 | 23 | #include <linux/interrupt.h> | 
 | 24 | #include <linux/time.h> | 
 | 25 | #include <linux/init.h> | 
 | 26 | #include <linux/smp.h> | 
 | 27 | #include <linux/timex.h> | 
 | 28 | #include <linux/errno.h> | 
 | 29 | #include <linux/profile.h> | 
 | 30 |  | 
 | 31 | #include <asm/hardware.h> | 
 | 32 | #include <asm/io.h> | 
 | 33 | #include <asm/irq.h> | 
 | 34 | #include <asm/ioc.h> | 
 | 35 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* this needs a better home */ | 
 | 37 | DEFINE_SPINLOCK(rtc_lock); | 
 | 38 |  | 
 | 39 | /* change this if you have some constant time drift */ | 
 | 40 | #define USECS_PER_JIFFY	(1000000/HZ) | 
 | 41 |  | 
 | 42 | static int dummy_set_rtc(void) | 
 | 43 | { | 
 | 44 | 	return 0; | 
 | 45 | } | 
 | 46 |  | 
 | 47 | /* | 
 | 48 |  * hook for setting the RTC's idea of the current time. | 
 | 49 |  */ | 
 | 50 | int (*set_rtc)(void) = dummy_set_rtc; | 
 | 51 |  | 
 | 52 | /* | 
 | 53 |  * Get time offset based on IOCs timer. | 
 | 54 |  * FIXME - if this is called with interrutps off, why the shennanigans | 
 | 55 |  * below ? | 
 | 56 |  */ | 
 | 57 | static unsigned long gettimeoffset(void) | 
 | 58 | { | 
 | 59 |         unsigned int count1, count2, status; | 
 | 60 |         long offset; | 
 | 61 |  | 
 | 62 |         ioc_writeb (0, IOC_T0LATCH); | 
 | 63 |         barrier (); | 
 | 64 |         count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8); | 
 | 65 |         barrier (); | 
 | 66 |         status = ioc_readb(IOC_IRQREQA); | 
 | 67 |         barrier (); | 
 | 68 |         ioc_writeb (0, IOC_T0LATCH); | 
 | 69 |         barrier (); | 
 | 70 |         count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8); | 
 | 71 |  | 
 | 72 |         offset = count2; | 
 | 73 |         if (count2 < count1) { | 
 | 74 |                 /* | 
 | 75 |                  * We have not had an interrupt between reading count1 | 
 | 76 |                  * and count2. | 
 | 77 |                  */ | 
 | 78 |                 if (status & (1 << 5)) | 
 | 79 |                         offset -= LATCH; | 
 | 80 |         } else if (count2 > count1) { | 
 | 81 |                 /* | 
 | 82 |                  * We have just had another interrupt between reading | 
 | 83 |                  * count1 and count2. | 
 | 84 |                  */ | 
 | 85 |                 offset -= LATCH; | 
 | 86 |         } | 
 | 87 |  | 
 | 88 |         offset = (LATCH - offset) * (tick_nsec / 1000); | 
 | 89 |         return (offset + LATCH/2) / LATCH; | 
 | 90 | } | 
 | 91 |  | 
 | 92 | /* | 
 | 93 |  * Scheduler clock - returns current time in nanosec units. | 
 | 94 |  */ | 
 | 95 | unsigned long long sched_clock(void) | 
 | 96 | { | 
 | 97 | 	return (unsigned long long)jiffies * (1000000000 / HZ); | 
 | 98 | } | 
 | 99 |  | 
 | 100 | static unsigned long next_rtc_update; | 
 | 101 |  | 
 | 102 | /* | 
 | 103 |  * If we have an externally synchronized linux clock, then update | 
 | 104 |  * CMOS clock accordingly every ~11 minutes.  set_rtc() has to be | 
 | 105 |  * called as close as possible to 500 ms before the new second | 
 | 106 |  * starts. | 
 | 107 |  */ | 
 | 108 | static inline void do_set_rtc(void) | 
 | 109 | { | 
| john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 110 | 	if (!ntp_synced() || set_rtc == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 		return; | 
 | 112 |  | 
 | 113 | //FIXME - timespec.tv_sec is a time_t not unsigned long | 
 | 114 | 	if (next_rtc_update && | 
 | 115 | 	    time_before((unsigned long)xtime.tv_sec, next_rtc_update)) | 
 | 116 | 		return; | 
 | 117 |  | 
 | 118 | 	if (xtime.tv_nsec < 500000000 - ((unsigned) tick_nsec >> 1) && | 
 | 119 | 	    xtime.tv_nsec >= 500000000 + ((unsigned) tick_nsec >> 1)) | 
 | 120 | 		return; | 
 | 121 |  | 
 | 122 | 	if (set_rtc()) | 
 | 123 | 		/* | 
 | 124 | 		 * rtc update failed.  Try again in 60s | 
 | 125 | 		 */ | 
 | 126 | 		next_rtc_update = xtime.tv_sec + 60; | 
 | 127 | 	else | 
 | 128 | 		next_rtc_update = xtime.tv_sec + 660; | 
 | 129 | } | 
 | 130 |  | 
 | 131 | #define do_leds() | 
 | 132 |  | 
 | 133 | void do_gettimeofday(struct timeval *tv) | 
 | 134 | { | 
 | 135 | 	unsigned long flags; | 
 | 136 | 	unsigned long seq; | 
| Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 137 | 	unsigned long usec, sec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
 | 139 | 	do { | 
 | 140 | 		seq = read_seqbegin_irqsave(&xtime_lock, flags); | 
 | 141 | 		usec = gettimeoffset(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | 		sec = xtime.tv_sec; | 
 | 143 | 		usec += xtime.tv_nsec / 1000; | 
 | 144 | 	} while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); | 
 | 145 |  | 
 | 146 | 	/* usec may have gone up a lot: be safe */ | 
 | 147 | 	while (usec >= 1000000) { | 
 | 148 | 		usec -= 1000000; | 
 | 149 | 		sec++; | 
 | 150 | 	} | 
 | 151 |  | 
 | 152 | 	tv->tv_sec = sec; | 
 | 153 | 	tv->tv_usec = usec; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | EXPORT_SYMBOL(do_gettimeofday); | 
 | 157 |  | 
 | 158 | int do_settimeofday(struct timespec *tv) | 
 | 159 | { | 
 | 160 | 	if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | 
 | 161 | 		return -EINVAL; | 
 | 162 |  | 
 | 163 | 	write_seqlock_irq(&xtime_lock); | 
 | 164 | 	/* | 
 | 165 | 	 * This is revolting. We need to set "xtime" correctly. However, the | 
 | 166 | 	 * value in this location is the value at the most recent update of | 
 | 167 | 	 * wall time.  Discover what correction gettimeofday() would have | 
 | 168 | 	 * done, and then undo it! | 
 | 169 | 	 */ | 
| Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 170 | 	tv->tv_nsec -= 1000 * gettimeoffset(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 |  | 
 | 172 | 	while (tv->tv_nsec < 0) { | 
 | 173 | 		tv->tv_nsec += NSEC_PER_SEC; | 
 | 174 | 		tv->tv_sec--; | 
 | 175 | 	} | 
 | 176 |  | 
 | 177 | 	xtime.tv_sec = tv->tv_sec; | 
 | 178 | 	xtime.tv_nsec = tv->tv_nsec; | 
| john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 179 | 	ntp_clear(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | 	write_sequnlock_irq(&xtime_lock); | 
 | 181 | 	clock_was_set(); | 
 | 182 | 	return 0; | 
 | 183 | } | 
 | 184 |  | 
 | 185 | EXPORT_SYMBOL(do_settimeofday); | 
 | 186 |  | 
 | 187 | static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) | 
 | 188 | { | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 189 |         do_timer(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | #ifndef CONFIG_SMP | 
 | 191 | 	update_process_times(user_mode(regs)); | 
 | 192 | #endif | 
 | 193 |         do_set_rtc(); //FIME - EVERY timer IRQ? | 
 | 194 |         profile_tick(CPU_PROFILING, regs); | 
 | 195 | 	return IRQ_HANDLED; //FIXME - is this right? | 
 | 196 | } | 
 | 197 |  | 
 | 198 | static struct irqaction timer_irq = { | 
 | 199 | 	.name	= "timer", | 
| Thomas Gleixner | af0f4d3 | 2006-07-01 19:29:12 -0700 | [diff] [blame] | 200 | 	.flags	= IRQF_DISABLED, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | 	.handler = timer_interrupt, | 
 | 202 | }; | 
 | 203 |  | 
 | 204 | extern void ioctime_init(void); | 
 | 205 |  | 
 | 206 | /* | 
 | 207 |  * Set up timer interrupt. | 
 | 208 |  */ | 
 | 209 | void __init time_init(void) | 
 | 210 | { | 
 | 211 | 	ioc_writeb(LATCH & 255, IOC_T0LTCHL); | 
 | 212 |         ioc_writeb(LATCH >> 8, IOC_T0LTCHH); | 
 | 213 |         ioc_writeb(0, IOC_T0GO); | 
 | 214 |  | 
 | 215 |  | 
 | 216 |         setup_irq(IRQ_TIMER, &timer_irq); | 
 | 217 | } | 
 | 218 |  |