blob: e5069fe6861ee97a96a9481f0b3700def7cd7f40 [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",
Bryan Wu1394f032007-05-06 14:50:22 -070027 .flags = IRQF_DISABLED
28};
29
Graf Yang9b9bfde2009-05-27 09:58:35 +000030#if defined(CONFIG_IPIPE)
Mike Frysingera1ee74c2009-01-07 23:14:38 +080031void __init setup_system_timer0(void)
Graf Yang8f658732008-11-18 17:48:22 +080032{
33 /* Power down the core timer, just to play safe. */
34 bfin_write_TCNTL(0);
35
36 disable_gptimers(TIMER0bit);
37 set_gptimer_status(0, TIMER_STATUS_TRUN0);
38 while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
39 udelay(10);
40
41 set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
42 set_gptimer_period(TIMER0_id, get_sclk() / HZ);
43 set_gptimer_pwidth(TIMER0_id, 1);
44 SSYNC();
45 enable_gptimers(TIMER0bit);
46}
Mike Frysinger1b047d82008-11-18 17:48:22 +080047#else
Mike Frysingera1ee74c2009-01-07 23:14:38 +080048void __init setup_core_timer(void)
Mike Frysinger1b047d82008-11-18 17:48:22 +080049{
50 u32 tcount;
51
52 /* power up the timer, but don't enable it just yet */
53 bfin_write_TCNTL(1);
54 CSYNC();
55
56 /* the TSCALE prescaler counter */
57 bfin_write_TSCALE(TIME_SCALE - 1);
58
59 tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
60 bfin_write_TPERIOD(tcount);
61 bfin_write_TCOUNT(tcount);
62
63 /* now enable the timer */
64 CSYNC();
65
66 bfin_write_TCNTL(7);
67}
Graf Yang8f658732008-11-18 17:48:22 +080068#endif
69
Mike Frysingera1ee74c2009-01-07 23:14:38 +080070static void __init
Graf Yang8f658732008-11-18 17:48:22 +080071time_sched_init(irqreturn_t(*timer_routine) (int, void *))
72{
Graf Yang9b9bfde2009-05-27 09:58:35 +000073#if defined(CONFIG_IPIPE)
Graf Yang8f658732008-11-18 17:48:22 +080074 setup_system_timer0();
Mike Frysinger1b047d82008-11-18 17:48:22 +080075 bfin_timer_irq.handler = timer_routine;
Graf Yang8f658732008-11-18 17:48:22 +080076 setup_irq(IRQ_TIMER0, &bfin_timer_irq);
77#else
Mike Frysinger1b047d82008-11-18 17:48:22 +080078 setup_core_timer();
79 bfin_timer_irq.handler = timer_routine;
Bryan Wu1394f032007-05-06 14:50:22 -070080 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
Graf Yang8f658732008-11-18 17:48:22 +080081#endif
Bryan Wu1394f032007-05-06 14:50:22 -070082}
83
john stultz10f03f12009-09-15 21:17:19 -070084#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Bryan Wu1394f032007-05-06 14:50:22 -070085/*
86 * Should return useconds since last timer tick
87 */
john stultz10f03f12009-09-15 21:17:19 -070088u32 arch_gettimeoffset(void)
Bryan Wu1394f032007-05-06 14:50:22 -070089{
90 unsigned long offset;
91 unsigned long clocks_per_jiffy;
92
Graf Yang9b9bfde2009-05-27 09:58:35 +000093#if defined(CONFIG_IPIPE)
Mike Frysinger1b047d82008-11-18 17:48:22 +080094 clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
95 offset = bfin_read_TIMER0_COUNTER() / \
Graf Yang8f658732008-11-18 17:48:22 +080096 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
97
98 if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
99 offset += (USEC_PER_SEC / HZ);
100#else
Bryan Wu1394f032007-05-06 14:50:22 -0700101 clocks_per_jiffy = bfin_read_TPERIOD();
Graf Yang8f658732008-11-18 17:48:22 +0800102 offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
Mike Frysinger1b047d82008-11-18 17:48:22 +0800103 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
Bryan Wu1394f032007-05-06 14:50:22 -0700104
105 /* Check if we just wrapped the counters and maybe missed a tick */
106 if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
Graf Yang8f658732008-11-18 17:48:22 +0800107 && (offset < (100000 / HZ / 2)))
Bryan Wu1394f032007-05-06 14:50:22 -0700108 offset += (USEC_PER_SEC / HZ);
Graf Yang8f658732008-11-18 17:48:22 +0800109#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700110 return offset;
111}
Mike Frysinger1b047d82008-11-18 17:48:22 +0800112#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700113
114static inline int set_rtc_mmss(unsigned long nowtime)
115{
116 return 0;
117}
118
119/*
120 * timer_interrupt() needs to keep up the real-time clock,
121 * as well as call the "do_timer()" routine every clocktick
122 */
123#ifdef CONFIG_CORE_TIMER_IRQ_L1
Mike Frysinger1b047d82008-11-18 17:48:22 +0800124__attribute__((l1_text))
Bryan Wu1394f032007-05-06 14:50:22 -0700125#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700126irqreturn_t timer_interrupt(int irq, void *dummy)
127{
128 /* last time the cmos clock got updated */
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800129 static long last_rtc_update;
Bryan Wu1394f032007-05-06 14:50:22 -0700130
131 write_seqlock(&xtime_lock);
Graf Yang9b9bfde2009-05-27 09:58:35 +0000132 do_timer(1);
Bryan Wu1394f032007-05-06 14:50:22 -0700133
Graf Yang9b9bfde2009-05-27 09:58:35 +0000134 /*
135 * If we have an externally synchronized Linux clock, then update
136 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
137 * called as close as possible to 500 ms before the new second starts.
138 */
139 if (ntp_synced() &&
140 xtime.tv_sec > last_rtc_update + 660 &&
141 (xtime.tv_nsec / NSEC_PER_USEC) >=
142 500000 - ((unsigned)TICK_SIZE) / 2
143 && (xtime.tv_nsec / NSEC_PER_USEC) <=
144 500000 + ((unsigned)TICK_SIZE) / 2) {
145 if (set_rtc_mmss(xtime.tv_sec) == 0)
146 last_rtc_update = xtime.tv_sec;
147 else
148 /* Do it again in 60s. */
149 last_rtc_update = xtime.tv_sec - 600;
Bryan Wu1394f032007-05-06 14:50:22 -0700150 }
151 write_sequnlock(&xtime_lock);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100152
Yi Li6a01f232009-01-07 23:14:39 +0800153#ifdef CONFIG_IPIPE
154 update_root_process_times(get_irq_regs());
155#else
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100156 update_process_times(user_mode(get_irq_regs()));
Yi Li6a01f232009-01-07 23:14:39 +0800157#endif
Graf Yang8f658732008-11-18 17:48:22 +0800158 profile_tick(CPU_PROFILING);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100159
Bryan Wu1394f032007-05-06 14:50:22 -0700160 return IRQ_HANDLED;
161}
162
163void __init time_init(void)
164{
165 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
166
167#ifdef CONFIG_RTC_DRV_BFIN
168 /* [#2663] hack to filter junk RTC values that would cause
169 * userspace to have to deal with time values greater than
170 * 2^31 seconds (which uClibc cannot cope with yet)
171 */
172 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
173 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
174 bfin_write_RTC_STAT(0);
175 }
176#endif
177
178 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
179 xtime.tv_sec = secs_since_1970;
180 xtime.tv_nsec = 0;
181
182 wall_to_monotonic.tv_sec = -xtime.tv_sec;
183
184 time_sched_init(timer_interrupt);
185}
186
Bryan Wu1394f032007-05-06 14:50:22 -0700187/*
188 * Scheduler clock - returns current time in nanosec units.
189 */
190unsigned long long sched_clock(void)
191{
192 return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
193}