blob: fb0ce75772256568bc2195ba08cbfb9a652def94 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* time.c: FRV arch-specific time handling
2 *
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
6 *
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.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/param.h>
18#include <linux/string.h>
19#include <linux/interrupt.h>
20#include <linux/profile.h>
21#include <linux/irq.h>
22#include <linux/mm.h>
23
24#include <asm/io.h>
25#include <asm/timer-regs.h>
26#include <asm/mb-regs.h>
27#include <asm/mb86943a.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <linux/timex.h>
30
31#define TICK_SIZE (tick_nsec / 1000)
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033unsigned long __nongprelbss __clkin_clock_speed_HZ;
34unsigned long __nongprelbss __ext_bus_clock_speed_HZ;
35unsigned long __nongprelbss __res_bus_clock_speed_HZ;
36unsigned long __nongprelbss __sdram_clock_speed_HZ;
37unsigned long __nongprelbss __core_bus_clock_speed_HZ;
38unsigned long __nongprelbss __core_clock_speed_HZ;
39unsigned long __nongprelbss __dsu_clock_speed_HZ;
40unsigned long __nongprelbss __serial_clock_speed_HZ;
41unsigned long __delay_loops_MHz;
42
David Howells7d12e782006-10-05 14:55:46 +010043static irqreturn_t timer_interrupt(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static struct irqaction timer_irq = {
Thomas Gleixnera2e170e2007-10-16 01:26:29 -070046 .handler = timer_interrupt,
47 .flags = IRQF_DISABLED,
Thomas Gleixnera2e170e2007-10-16 01:26:29 -070048 .name = "timer",
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
51static inline int set_rtc_mmss(unsigned long nowtime)
52{
53 return -1;
54}
55
56/*
57 * timer_interrupt() needs to keep up the real-time clock,
58 * as well as call the "do_timer()" routine every clocktick
59 */
David Howells7d12e782006-10-05 14:55:46 +010060static irqreturn_t timer_interrupt(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 /* last time the cmos clock got updated */
63 static long last_rtc_update = 0;
64
Peter Zijlstraaa02cd22008-02-13 21:33:16 +010065 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /*
67 * Here we are in the timer irq handler. We just have irqs locally
68 * disabled but we don't know if the timer_bh is running on the other
Simon Arlott761a7e32007-10-20 01:09:42 +020069 * CPU. We need to avoid to SMP race with it. NOTE: we don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * the irq version of write_lock because as just said we have irq
71 * locally disabled. -arca
72 */
73 write_seqlock(&xtime_lock);
74
Atsushi Nemoto3171a032006-09-29 02:00:32 -070075 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /*
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.
81 */
john stultzb149ee22005-09-06 15:17:46 -070082 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 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
86 ) {
87 if (set_rtc_mmss(xtime.tv_sec) == 0)
88 last_rtc_update = xtime.tv_sec;
89 else
90 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
91 }
92
93#ifdef CONFIG_HEARTBEAT
94 static unsigned short n;
95 n++;
96 __set_LEDS(n);
97#endif /* CONFIG_HEARTBEAT */
98
99 write_sequnlock(&xtime_lock);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100100
101 update_process_times(user_mode(get_irq_regs()));
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return IRQ_HANDLED;
104}
105
106void time_divisor_init(void)
107{
108 unsigned short base, pre, prediv;
109
110 /* set the scheduling timer going */
111 pre = 1;
112 prediv = 4;
113 base = __res_bus_clock_speed_HZ / pre / HZ / (1 << prediv);
114
115 __set_TPRV(pre);
116 __set_TxCKSL_DATA(0, prediv);
117 __set_TCTR(TCTR_SC_CTR0 | TCTR_RL_RW_LH8 | TCTR_MODE_2);
118 __set_TCSR_DATA(0, base & 0xff);
119 __set_TCSR_DATA(0, base >> 8);
120}
121
122void time_init(void)
123{
124 unsigned int year, mon, day, hour, min, sec;
125
126 extern void arch_gettod(int *year, int *mon, int *day, int *hour, int *min, int *sec);
127
128 /* FIX by dqg : Set to zero for platforms that don't have tod */
129 /* without this time is undefined and can overflow time_t, causing */
Simon Arlott761a7e32007-10-20 01:09:42 +0200130 /* very strange errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 year = 1980;
132 mon = day = 1;
133 hour = min = sec = 0;
134 arch_gettod (&year, &mon, &day, &hour, &min, &sec);
135
136 if ((year += 1900) < 1970)
137 year += 100;
138 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
139 xtime.tv_nsec = 0;
140
141 /* install scheduling interrupt handler */
142 setup_irq(IRQ_CPU_TIMER0, &timer_irq);
143
144 time_divisor_init();
145}
146
147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * Scheduler clock - returns current time in nanosec units.
149 */
150unsigned long long sched_clock(void)
151{
152 return jiffies_64 * (1000000000 / HZ);
153}