1 /* time.c: FRV arch-specific time handling
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/time.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/config.h> /* CONFIG_HEARTBEAT */
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/param.h>
19 #include <linux/string.h>
20 #include <linux/interrupt.h>
21 #include <linux/profile.h>
22 #include <linux/irq.h>
26 #include <asm/timer-regs.h>
27 #include <asm/mb-regs.h>
28 #include <asm/mb86943a.h>
30 #include <linux/timex.h>
32 #define TICK_SIZE (tick_nsec / 1000)
34 unsigned long __nongprelbss __clkin_clock_speed_HZ;
35 unsigned long __nongprelbss __ext_bus_clock_speed_HZ;
36 unsigned long __nongprelbss __res_bus_clock_speed_HZ;
37 unsigned long __nongprelbss __sdram_clock_speed_HZ;
38 unsigned long __nongprelbss __core_bus_clock_speed_HZ;
39 unsigned long __nongprelbss __core_clock_speed_HZ;
40 unsigned long __nongprelbss __dsu_clock_speed_HZ;
41 unsigned long __nongprelbss __serial_clock_speed_HZ;
42 unsigned long __delay_loops_MHz;
44 static irqreturn_t timer_interrupt(int irq, void *dummy, struct pt_regs *regs);
46 static struct irqaction timer_irq = {
47 timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, "timer", NULL, NULL
50 static inline int set_rtc_mmss(unsigned long nowtime)
56 * timer_interrupt() needs to keep up the real-time clock,
57 * as well as call the "do_timer()" routine every clocktick
59 static irqreturn_t timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
61 /* last time the cmos clock got updated */
62 static long last_rtc_update = 0;
65 * Here we are in the timer irq handler. We just have irqs locally
66 * disabled but we don't know if the timer_bh is running on the other
67 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
68 * the irq version of write_lock because as just said we have irq
69 * locally disabled. -arca
71 write_seqlock(&xtime_lock);
74 update_process_times(user_mode(regs));
75 profile_tick(CPU_PROFILING, regs);
78 * If we have an externally synchronized Linux clock, then update
79 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
80 * called as close as possible to 500 ms before the new second starts.
83 xtime.tv_sec > last_rtc_update + 660 &&
84 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
85 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2
87 if (set_rtc_mmss(xtime.tv_sec) == 0)
88 last_rtc_update = xtime.tv_sec;
90 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
93 #ifdef CONFIG_HEARTBEAT
94 static unsigned short n;
97 #endif /* CONFIG_HEARTBEAT */
99 write_sequnlock(&xtime_lock);
103 void time_divisor_init(void)
105 unsigned short base, pre, prediv;
107 /* set the scheduling timer going */
110 base = __res_bus_clock_speed_HZ / pre / HZ / (1 << prediv);
113 __set_TxCKSL_DATA(0, prediv);
114 __set_TCTR(TCTR_SC_CTR0 | TCTR_RL_RW_LH8 | TCTR_MODE_2);
115 __set_TCSR_DATA(0, base & 0xff);
116 __set_TCSR_DATA(0, base >> 8);
121 unsigned int year, mon, day, hour, min, sec;
123 extern void arch_gettod(int *year, int *mon, int *day, int *hour, int *min, int *sec);
125 /* FIX by dqg : Set to zero for platforms that don't have tod */
126 /* without this time is undefined and can overflow time_t, causing */
127 /* very stange errors */
130 hour = min = sec = 0;
131 arch_gettod (&year, &mon, &day, &hour, &min, &sec);
133 if ((year += 1900) < 1970)
135 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
138 /* install scheduling interrupt handler */
139 setup_irq(IRQ_CPU_TIMER0, &timer_irq);
145 * Scheduler clock - returns current time in nanosec units.
147 unsigned long long sched_clock(void)
149 return jiffies_64 * (1000000000 / HZ);