blob: 172b4c588467ff2a51fbcc5993a4507d39e83d91 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Mike Frysinger1b047d82008-11-18 17:48:22 +08002 * arch/blackfin/kernel/time.c
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Mike Frysinger1b047d82008-11-18 17:48:22 +08004 * This file contains the Blackfin-specific time handling details.
5 * Most of the stuff is located in the machine specific files.
Bryan Wu1394f032007-05-06 14:50:22 -07006 *
Mike Frysinger1b047d82008-11-18 17:48:22 +08007 * Copyright 2004-2008 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07009 */
10
11#include <linux/module.h>
12#include <linux/profile.h>
13#include <linux/interrupt.h>
14#include <linux/time.h>
15#include <linux/irq.h>
Graf Yang8f658732008-11-18 17:48:22 +080016#include <linux/delay.h>
Bryan Wu1394f032007-05-06 14:50:22 -070017
18#include <asm/blackfin.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080019#include <asm/time.h>
Graf Yang8f658732008-11-18 17:48:22 +080020#include <asm/gptimers.h>
Bryan Wu1394f032007-05-06 14:50:22 -070021
22/* This is an NTP setting */
23#define TICK_SIZE (tick_nsec / 1000)
24
Bryan Wu1394f032007-05-06 14:50:22 -070025static struct irqaction bfin_timer_irq = {
Mike Frysinger1b047d82008-11-18 17:48:22 +080026 .name = "Blackfin Timer Tick",
Graf Yang8f658732008-11-18 17:48:22 +080027#ifdef CONFIG_IRQ_PER_CPU
Mike Frysinger1b047d82008-11-18 17:48:22 +080028 .flags = IRQF_DISABLED | IRQF_PERCPU,
Graf Yang8f658732008-11-18 17:48:22 +080029#else
Bryan Wu1394f032007-05-06 14:50:22 -070030 .flags = IRQF_DISABLED
Graf Yang8f658732008-11-18 17:48:22 +080031#endif
Bryan Wu1394f032007-05-06 14:50:22 -070032};
33
Yi Li6a01f232009-01-07 23:14:39 +080034#if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
Mike Frysingera1ee74c2009-01-07 23:14:38 +080035void __init setup_system_timer0(void)
Graf Yang8f658732008-11-18 17:48:22 +080036{
37 /* Power down the core timer, just to play safe. */
38 bfin_write_TCNTL(0);
39
40 disable_gptimers(TIMER0bit);
41 set_gptimer_status(0, TIMER_STATUS_TRUN0);
42 while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
43 udelay(10);
44
45 set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
46 set_gptimer_period(TIMER0_id, get_sclk() / HZ);
47 set_gptimer_pwidth(TIMER0_id, 1);
48 SSYNC();
49 enable_gptimers(TIMER0bit);
50}
Mike Frysinger1b047d82008-11-18 17:48:22 +080051#else
Mike Frysingera1ee74c2009-01-07 23:14:38 +080052void __init setup_core_timer(void)
Mike Frysinger1b047d82008-11-18 17:48:22 +080053{
54 u32 tcount;
55
56 /* power up the timer, but don't enable it just yet */
57 bfin_write_TCNTL(1);
58 CSYNC();
59
60 /* the TSCALE prescaler counter */
61 bfin_write_TSCALE(TIME_SCALE - 1);
62
63 tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
64 bfin_write_TPERIOD(tcount);
65 bfin_write_TCOUNT(tcount);
66
67 /* now enable the timer */
68 CSYNC();
69
70 bfin_write_TCNTL(7);
71}
Graf Yang8f658732008-11-18 17:48:22 +080072#endif
73
Mike Frysingera1ee74c2009-01-07 23:14:38 +080074static void __init
Graf Yang8f658732008-11-18 17:48:22 +080075time_sched_init(irqreturn_t(*timer_routine) (int, void *))
76{
Yi Li6a01f232009-01-07 23:14:39 +080077#if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
Graf Yang8f658732008-11-18 17:48:22 +080078 setup_system_timer0();
Mike Frysinger1b047d82008-11-18 17:48:22 +080079 bfin_timer_irq.handler = timer_routine;
Graf Yang8f658732008-11-18 17:48:22 +080080 setup_irq(IRQ_TIMER0, &bfin_timer_irq);
81#else
Mike Frysinger1b047d82008-11-18 17:48:22 +080082 setup_core_timer();
83 bfin_timer_irq.handler = timer_routine;
Bryan Wu1394f032007-05-06 14:50:22 -070084 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
Graf Yang8f658732008-11-18 17:48:22 +080085#endif
Bryan Wu1394f032007-05-06 14:50:22 -070086}
87
88/*
89 * Should return useconds since last timer tick
90 */
Mike Frysinger1b047d82008-11-18 17:48:22 +080091#ifndef CONFIG_GENERIC_TIME
Bryan Wu1394f032007-05-06 14:50:22 -070092static unsigned long gettimeoffset(void)
93{
94 unsigned long offset;
95 unsigned long clocks_per_jiffy;
96
Yi Li6a01f232009-01-07 23:14:39 +080097#if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
Mike Frysinger1b047d82008-11-18 17:48:22 +080098 clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
99 offset = bfin_read_TIMER0_COUNTER() / \
Graf Yang8f658732008-11-18 17:48:22 +0800100 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
101
102 if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
103 offset += (USEC_PER_SEC / HZ);
104#else
Bryan Wu1394f032007-05-06 14:50:22 -0700105 clocks_per_jiffy = bfin_read_TPERIOD();
Graf Yang8f658732008-11-18 17:48:22 +0800106 offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
Mike Frysinger1b047d82008-11-18 17:48:22 +0800107 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
Bryan Wu1394f032007-05-06 14:50:22 -0700108
109 /* Check if we just wrapped the counters and maybe missed a tick */
110 if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
Graf Yang8f658732008-11-18 17:48:22 +0800111 && (offset < (100000 / HZ / 2)))
Bryan Wu1394f032007-05-06 14:50:22 -0700112 offset += (USEC_PER_SEC / HZ);
Graf Yang8f658732008-11-18 17:48:22 +0800113#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700114 return offset;
115}
Mike Frysinger1b047d82008-11-18 17:48:22 +0800116#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700117
118static inline int set_rtc_mmss(unsigned long nowtime)
119{
120 return 0;
121}
122
123/*
124 * timer_interrupt() needs to keep up the real-time clock,
125 * as well as call the "do_timer()" routine every clocktick
126 */
127#ifdef CONFIG_CORE_TIMER_IRQ_L1
Mike Frysinger1b047d82008-11-18 17:48:22 +0800128__attribute__((l1_text))
Bryan Wu1394f032007-05-06 14:50:22 -0700129#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700130irqreturn_t timer_interrupt(int irq, void *dummy)
131{
132 /* last time the cmos clock got updated */
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800133 static long last_rtc_update;
Bryan Wu1394f032007-05-06 14:50:22 -0700134
135 write_seqlock(&xtime_lock);
Yi Li6a01f232009-01-07 23:14:39 +0800136#if defined(CONFIG_TICK_SOURCE_SYSTMR0) && !defined(CONFIG_IPIPE)
137/* FIXME: Here TIMIL0 is not set when IPIPE enabled, why? */
Graf Yang8f658732008-11-18 17:48:22 +0800138 if (get_gptimer_status(0) & TIMER_STATUS_TIMIL0) {
139#endif
140 do_timer(1);
Bryan Wu1394f032007-05-06 14:50:22 -0700141
Graf Yang8f658732008-11-18 17:48:22 +0800142 /*
143 * If we have an externally synchronized Linux clock, then update
144 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
145 * called as close as possible to 500 ms before the new second starts.
146 */
Graf Yang8f658732008-11-18 17:48:22 +0800147 if (ntp_synced() &&
148 xtime.tv_sec > last_rtc_update + 660 &&
149 (xtime.tv_nsec / NSEC_PER_USEC) >=
150 500000 - ((unsigned)TICK_SIZE) / 2
151 && (xtime.tv_nsec / NSEC_PER_USEC) <=
152 500000 + ((unsigned)TICK_SIZE) / 2) {
153 if (set_rtc_mmss(xtime.tv_sec) == 0)
154 last_rtc_update = xtime.tv_sec;
155 else
156 /* Do it again in 60s. */
157 last_rtc_update = xtime.tv_sec - 600;
158 }
Yi Li6a01f232009-01-07 23:14:39 +0800159#if defined(CONFIG_TICK_SOURCE_SYSTMR0) && !defined(CONFIG_IPIPE)
Graf Yang8f658732008-11-18 17:48:22 +0800160 set_gptimer_status(0, TIMER_STATUS_TIMIL0);
Bryan Wu1394f032007-05-06 14:50:22 -0700161 }
Graf Yang8f658732008-11-18 17:48:22 +0800162#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700163 write_sequnlock(&xtime_lock);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100164
Yi Li6a01f232009-01-07 23:14:39 +0800165#ifdef CONFIG_IPIPE
166 update_root_process_times(get_irq_regs());
167#else
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100168 update_process_times(user_mode(get_irq_regs()));
Yi Li6a01f232009-01-07 23:14:39 +0800169#endif
Graf Yang8f658732008-11-18 17:48:22 +0800170 profile_tick(CPU_PROFILING);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100171
Bryan Wu1394f032007-05-06 14:50:22 -0700172 return IRQ_HANDLED;
173}
174
175void __init time_init(void)
176{
177 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
178
179#ifdef CONFIG_RTC_DRV_BFIN
180 /* [#2663] hack to filter junk RTC values that would cause
181 * userspace to have to deal with time values greater than
182 * 2^31 seconds (which uClibc cannot cope with yet)
183 */
184 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
185 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
186 bfin_write_RTC_STAT(0);
187 }
188#endif
189
190 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
191 xtime.tv_sec = secs_since_1970;
192 xtime.tv_nsec = 0;
193
194 wall_to_monotonic.tv_sec = -xtime.tv_sec;
195
196 time_sched_init(timer_interrupt);
197}
198
199#ifndef CONFIG_GENERIC_TIME
200void do_gettimeofday(struct timeval *tv)
201{
202 unsigned long flags;
203 unsigned long seq;
204 unsigned long usec, sec;
205
206 do {
207 seq = read_seqbegin_irqsave(&xtime_lock, flags);
208 usec = gettimeoffset();
209 sec = xtime.tv_sec;
210 usec += (xtime.tv_nsec / NSEC_PER_USEC);
211 }
212 while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
213
214 while (usec >= USEC_PER_SEC) {
215 usec -= USEC_PER_SEC;
216 sec++;
217 }
218
219 tv->tv_sec = sec;
220 tv->tv_usec = usec;
221}
222EXPORT_SYMBOL(do_gettimeofday);
223
224int do_settimeofday(struct timespec *tv)
225{
226 time_t wtm_sec, sec = tv->tv_sec;
227 long wtm_nsec, nsec = tv->tv_nsec;
228
229 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
230 return -EINVAL;
231
232 write_seqlock_irq(&xtime_lock);
233 /*
234 * This is revolting. We need to set the xtime.tv_usec
235 * correctly. However, the value in this location is
236 * is value at the last tick.
237 * Discover what correction gettimeofday
238 * would have done, and then undo it!
239 */
240 nsec -= (gettimeoffset() * NSEC_PER_USEC);
241
242 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
243 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
244
245 set_normalized_timespec(&xtime, sec, nsec);
246 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
247
248 ntp_clear();
249
250 write_sequnlock_irq(&xtime_lock);
251 clock_was_set();
252
253 return 0;
254}
255EXPORT_SYMBOL(do_settimeofday);
256#endif /* !CONFIG_GENERIC_TIME */
257
258/*
259 * Scheduler clock - returns current time in nanosec units.
260 */
261unsigned long long sched_clock(void)
262{
263 return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
264}