📄 time.c
字号:
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * arch/sh64/kernel/time.c * * Copyright (C) 2000, 2001 Paolo Alberelli * Copyright (C) 2003, 2004 Paul Mundt * Copyright (C) 2003 Richard Curnow * * Original TMU/RTC code taken from sh version. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka * Some code taken from i386 version. * Copyright (C) 1991, 1992, 1995 Linus Torvalds */#include <linux/config.h>#include <linux/errno.h>#include <linux/rwsem.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/param.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/time.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/profile.h>#include <linux/smp.h>#include <asm/registers.h> /* required by inline __asm__ stmt. */#include <asm/processor.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/delay.h>#include <linux/timex.h>#include <linux/irq.h>#include <asm/hardware.h>#define TMU_TOCR_INIT 0x00#define TMU0_TCR_INIT 0x0020#define TMU_TSTR_INIT 1#define TMU_TSTR_OFF 0/* RCR1 Bits */#define RCR1_CF 0x80 /* Carry Flag */#define RCR1_CIE 0x10 /* Carry Interrupt Enable */#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */#define RCR1_AF 0x01 /* Alarm Flag *//* RCR2 Bits */#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */#define RCR2_RTCEN 0x08 /* ENable RTC */#define RCR2_ADJ 0x04 /* ADJustment (30-second) */#define RCR2_RESET 0x02 /* Reset bit */#define RCR2_START 0x01 /* Start bit *//* Clock, Power and Reset Controller */#define CPRC_BLOCK_OFF 0x01010000#define CPRC_BASE PHYS_PERIPHERAL_BLOCK + CPRC_BLOCK_OFF#define FRQCR (cprc_base+0x0)#define WTCSR (cprc_base+0x0018)#define STBCR (cprc_base+0x0030)/* Time Management Unit */#define TMU_BLOCK_OFF 0x01020000#define TMU_BASE PHYS_PERIPHERAL_BLOCK + TMU_BLOCK_OFF#define TMU0_BASE tmu_base + 0x8 + (0xc * 0x0)#define TMU1_BASE tmu_base + 0x8 + (0xc * 0x1)#define TMU2_BASE tmu_base + 0x8 + (0xc * 0x2)#define TMU_TOCR tmu_base+0x0 /* Byte access */#define TMU_TSTR tmu_base+0x4 /* Byte access */#define TMU0_TCOR TMU0_BASE+0x0 /* Long access */#define TMU0_TCNT TMU0_BASE+0x4 /* Long access */#define TMU0_TCR TMU0_BASE+0x8 /* Word access *//* Real Time Clock */#define RTC_BLOCK_OFF 0x01040000#define RTC_BASE PHYS_PERIPHERAL_BLOCK + RTC_BLOCK_OFF#define R64CNT rtc_base+0x00#define RSECCNT rtc_base+0x04#define RMINCNT rtc_base+0x08#define RHRCNT rtc_base+0x0c#define RWKCNT rtc_base+0x10#define RDAYCNT rtc_base+0x14#define RMONCNT rtc_base+0x18#define RYRCNT rtc_base+0x1c /* 16bit */#define RSECAR rtc_base+0x20#define RMINAR rtc_base+0x24#define RHRAR rtc_base+0x28#define RWKAR rtc_base+0x2c#define RDAYAR rtc_base+0x30#define RMONAR rtc_base+0x34#define RCR1 rtc_base+0x38#define RCR2 rtc_base+0x3c#ifndef BCD_TO_BIN#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)#endif#ifndef BIN_TO_BCD#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)#endif#define TICK_SIZE (tick_nsec / 1000)extern unsigned long wall_jiffies;static unsigned long tmu_base, rtc_base;unsigned long cprc_base;/* Variables to allow interpolation of time of day to resolution better than a * jiffy. *//* This is effectively protected by xtime_lock */static unsigned long ctc_last_interrupt;static unsigned long long usecs_per_jiffy = 1000000/HZ; /* Approximation */#define CTC_JIFFY_SCALE_SHIFT 40/* 2**CTC_JIFFY_SCALE_SHIFT / ctc_ticks_per_jiffy */static unsigned long long scaled_recip_ctc_ticks_per_jiffy;/* Estimate number of microseconds that have elapsed since the last timer tick, by scaling the delta that has occured in the CTC register. WARNING WARNING WARNING : This algorithm relies on the CTC decrementing at the CPU clock rate. If the CPU sleeps, the CTC stops counting. Bear this in mind if enabling SLEEP_WORKS in process.c. In that case, this algorithm probably needs to use TMU.TCNT0 instead. This will work even if the CPU is sleeping, though will be coarser. FIXME : What if usecs_per_tick is moving around too much, e.g. if an adjtime is running or if the freq or tick arguments of adjtimex are modified after we have calibrated the scaling factor? This will result in either a jump at the end of a tick period, or a wrap backwards at the start of the next one, if the application is reading the time of day often enough. I think we ought to do better than this. For this reason, usecs_per_jiffy is left separated out in the calculation below. This allows some future hook into the adjtime-related stuff in kernel/timer.c to remove this hazard.*/static unsigned long usecs_since_tick(void){ unsigned long long current_ctc; long ctc_ticks_since_interrupt; unsigned long long ull_ctc_ticks_since_interrupt; unsigned long result; unsigned long long mul1_out; unsigned long long mul1_out_high; unsigned long long mul2_out_low, mul2_out_high; /* Read CTC register */ asm ("getcon cr62, %0" : "=r" (current_ctc)); /* Note, the CTC counts down on each CPU clock, not up. Note(2), use long type to get correct wraparound arithmetic when the counter crosses zero. */ ctc_ticks_since_interrupt = (long) ctc_last_interrupt - (long) current_ctc; ull_ctc_ticks_since_interrupt = (unsigned long long) ctc_ticks_since_interrupt; /* Inline assembly to do 32x32x32->64 multiplier */ asm volatile ("mulu.l %1, %2, %0" : "=r" (mul1_out) : "r" (ull_ctc_ticks_since_interrupt), "r" (usecs_per_jiffy)); mul1_out_high = mul1_out >> 32; asm volatile ("mulu.l %1, %2, %0" : "=r" (mul2_out_low) : "r" (mul1_out), "r" (scaled_recip_ctc_ticks_per_jiffy));#if 1 asm volatile ("mulu.l %1, %2, %0" : "=r" (mul2_out_high) : "r" (mul1_out_high), "r" (scaled_recip_ctc_ticks_per_jiffy));#endif result = (unsigned long) (((mul2_out_high << 32) + mul2_out_low) >> CTC_JIFFY_SCALE_SHIFT); return result;}void do_gettimeofday(struct timeval *tv){ unsigned long flags; unsigned long seq; unsigned long usec, sec; do { seq = read_seqbegin_irqsave(&xtime_lock, flags); usec = usecs_since_tick(); { unsigned long lost = jiffies - wall_jiffies; if (lost) usec += lost * (1000000 / HZ); } sec = xtime.tv_sec; usec += xtime.tv_nsec / 1000; } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); while (usec >= 1000000) { usec -= 1000000; sec++; } tv->tv_sec = sec; tv->tv_usec = usec;}int do_settimeofday(struct timespec *tv){ time_t wtm_sec, sec = tv->tv_sec; long wtm_nsec, nsec = tv->tv_nsec; if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) return -EINVAL; write_seqlock_irq(&xtime_lock); /* * This is revolting. We need to set "xtime" correctly. However, the * value in this location is the value at the most recent update of * wall time. Discover what correction gettimeofday() would have * made, and then undo it! */ nsec -= 1000 * (usecs_since_tick() + (jiffies - wall_jiffies) * (1000000 / HZ)); wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); set_normalized_timespec(&xtime, sec, nsec); set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); ntp_clear(); write_sequnlock_irq(&xtime_lock); clock_was_set(); return 0;}EXPORT_SYMBOL(do_settimeofday);static int set_rtc_time(unsigned long nowtime){ int retval = 0; int real_seconds, real_minutes, cmos_minutes; ctrl_outb(RCR2_RESET, RCR2); /* Reset pre-scaler & stop RTC */ cmos_minutes = ctrl_inb(RMINCNT); BCD_TO_BIN(cmos_minutes); /* * since we're only adjusting minutes and seconds, * don't interfere with hour overflow. This avoids * messing with unknown time zones but requires your * RTC not to be off by more than 15 minutes */ real_seconds = nowtime % 60; real_minutes = nowtime / 60; if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) real_minutes += 30; /* correct for half hour time zone */ real_minutes %= 60; if (abs(real_minutes - cmos_minutes) < 30) { BIN_TO_BCD(real_seconds); BIN_TO_BCD(real_minutes); ctrl_outb(real_seconds, RSECCNT); ctrl_outb(real_minutes, RMINCNT); } else { printk(KERN_WARNING "set_rtc_time: can't update from %d to %d\n", cmos_minutes, real_minutes); retval = -1; } ctrl_outb(RCR2_RTCEN|RCR2_START, RCR2); /* Start RTC */ return retval;}/* last time the RTC clock got updated */static long last_rtc_update = 0;/* * timer_interrupt() needs to keep up the real-time clock, * as well as call the "do_timer()" routine every clocktick */static inline void do_timer_interrupt(int irq, struct pt_regs *regs){ unsigned long long current_ctc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -