[Blackfin] arch: Functional power management support: Add support for cpu frequency...
[linux-2.6/mini2440.git] / arch / blackfin / kernel / time.c
blobeb23523204547194c1de575478554510a7665356
1 /*
2 * File: arch/blackfin/kernel/time.c
3 * Based on: none - original work
4 * Author:
6 * Created:
7 * Description: This file contains the bfin-specific time handling details.
8 * Most of the stuff is located in the machine specific files.
9 * FIXME: (This file is subject for removal)
11 * Modified:
12 * Copyright 2004-2008 Analog Devices Inc.
14 * Bugs: Enter bugs at http://blackfin.uclinux.org/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see the file COPYING, or write
28 * to the Free Software Foundation, Inc.,
29 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <linux/module.h>
33 #include <linux/profile.h>
34 #include <linux/interrupt.h>
35 #include <linux/time.h>
36 #include <linux/irq.h>
38 #include <asm/blackfin.h>
39 #include <asm/time.h>
41 /* This is an NTP setting */
42 #define TICK_SIZE (tick_nsec / 1000)
44 static void time_sched_init(irq_handler_t timer_routine);
45 static unsigned long gettimeoffset(void);
47 static struct irqaction bfin_timer_irq = {
48 .name = "BFIN Timer Tick",
49 .flags = IRQF_DISABLED
52 static void
53 time_sched_init(irq_handler_t timer_routine)
55 u32 tcount;
57 /* power up the timer, but don't enable it just yet */
58 bfin_write_TCNTL(1);
59 CSYNC();
62 * the TSCALE prescaler counter.
64 bfin_write_TSCALE((TIME_SCALE - 1));
66 tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
67 bfin_write_TPERIOD(tcount);
68 bfin_write_TCOUNT(tcount);
70 /* now enable the timer */
71 CSYNC();
73 bfin_write_TCNTL(7);
75 bfin_timer_irq.handler = (irq_handler_t)timer_routine;
76 /* call setup_irq instead of request_irq because request_irq calls
77 * kmalloc which has not been initialized yet
79 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
83 * Should return useconds since last timer tick
85 static unsigned long gettimeoffset(void)
87 unsigned long offset;
88 unsigned long clocks_per_jiffy;
90 clocks_per_jiffy = bfin_read_TPERIOD();
91 offset =
92 (clocks_per_jiffy -
93 bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) /
94 USEC_PER_SEC);
96 /* Check if we just wrapped the counters and maybe missed a tick */
97 if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
98 && (offset < (100000 / HZ / 2)))
99 offset += (USEC_PER_SEC / HZ);
101 return offset;
104 static inline int set_rtc_mmss(unsigned long nowtime)
106 return 0;
110 * timer_interrupt() needs to keep up the real-time clock,
111 * as well as call the "do_timer()" routine every clocktick
113 #ifdef CONFIG_CORE_TIMER_IRQ_L1
114 irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text));
115 #endif
117 irqreturn_t timer_interrupt(int irq, void *dummy)
119 /* last time the cmos clock got updated */
120 static long last_rtc_update;
122 write_seqlock(&xtime_lock);
124 do_timer(1);
126 profile_tick(CPU_PROFILING);
129 * If we have an externally synchronized Linux clock, then update
130 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
131 * called as close as possible to 500 ms before the new second starts.
134 if (ntp_synced() &&
135 xtime.tv_sec > last_rtc_update + 660 &&
136 (xtime.tv_nsec / NSEC_PER_USEC) >=
137 500000 - ((unsigned)TICK_SIZE) / 2
138 && (xtime.tv_nsec / NSEC_PER_USEC) <=
139 500000 + ((unsigned)TICK_SIZE) / 2) {
140 if (set_rtc_mmss(xtime.tv_sec) == 0)
141 last_rtc_update = xtime.tv_sec;
142 else
143 /* Do it again in 60s. */
144 last_rtc_update = xtime.tv_sec - 600;
146 write_sequnlock(&xtime_lock);
148 #ifndef CONFIG_SMP
149 update_process_times(user_mode(get_irq_regs()));
150 #endif
152 return IRQ_HANDLED;
155 void __init time_init(void)
157 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
159 #ifdef CONFIG_RTC_DRV_BFIN
160 /* [#2663] hack to filter junk RTC values that would cause
161 * userspace to have to deal with time values greater than
162 * 2^31 seconds (which uClibc cannot cope with yet)
164 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
165 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
166 bfin_write_RTC_STAT(0);
168 #endif
170 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
171 xtime.tv_sec = secs_since_1970;
172 xtime.tv_nsec = 0;
174 wall_to_monotonic.tv_sec = -xtime.tv_sec;
176 time_sched_init(timer_interrupt);
179 #ifndef CONFIG_GENERIC_TIME
180 void do_gettimeofday(struct timeval *tv)
182 unsigned long flags;
183 unsigned long seq;
184 unsigned long usec, sec;
186 do {
187 seq = read_seqbegin_irqsave(&xtime_lock, flags);
188 usec = gettimeoffset();
189 sec = xtime.tv_sec;
190 usec += (xtime.tv_nsec / NSEC_PER_USEC);
192 while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
194 while (usec >= USEC_PER_SEC) {
195 usec -= USEC_PER_SEC;
196 sec++;
199 tv->tv_sec = sec;
200 tv->tv_usec = usec;
202 EXPORT_SYMBOL(do_gettimeofday);
204 int do_settimeofday(struct timespec *tv)
206 time_t wtm_sec, sec = tv->tv_sec;
207 long wtm_nsec, nsec = tv->tv_nsec;
209 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
210 return -EINVAL;
212 write_seqlock_irq(&xtime_lock);
214 * This is revolting. We need to set the xtime.tv_usec
215 * correctly. However, the value in this location is
216 * is value at the last tick.
217 * Discover what correction gettimeofday
218 * would have done, and then undo it!
220 nsec -= (gettimeoffset() * NSEC_PER_USEC);
222 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
223 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
225 set_normalized_timespec(&xtime, sec, nsec);
226 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
228 ntp_clear();
230 write_sequnlock_irq(&xtime_lock);
231 clock_was_set();
233 return 0;
235 EXPORT_SYMBOL(do_settimeofday);
236 #endif /* !CONFIG_GENERIC_TIME */
239 * Scheduler clock - returns current time in nanosec units.
241 unsigned long long sched_clock(void)
243 return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);