headers: remove sched.h from interrupt.h
[safe/jmp/linux-2.6] / arch / blackfin / kernel / time.c
index 5bd64e3..bd3b53d 100644 (file)
@@ -1,31 +1,11 @@
 /*
- * File:         arch/blackfin/kernel/time.c
- * Based on:     none - original work
- * Author:
+ * arch/blackfin/kernel/time.c
  *
- * Created:
- * Description:  This file contains the bfin-specific time handling details.
- *               Most of the stuff is located in the machine specific files.
+ * This file contains the Blackfin-specific time handling details.
+ * Most of the stuff is located in the machine specific files.
  *
- * Modified:
- *               Copyright 2004-2006 Analog Devices Inc.
- *
- * Bugs:         Enter bugs at http://blackfin.uclinux.org/
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see the file COPYING, or write
- * to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * Copyright 2004-2008 Analog Devices Inc.
+ * Licensed under the GPL-2 or later.
  */
 
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/time.h>
 #include <linux/irq.h>
+#include <linux/delay.h>
+#include <linux/sched.h>
 
 #include <asm/blackfin.h>
+#include <asm/time.h>
+#include <asm/gptimers.h>
 
 /* This is an NTP setting */
 #define        TICK_SIZE (tick_nsec / 1000)
 
-static void time_sched_init(irqreturn_t(*timer_routine)
-                       (int, void *));
-static unsigned long gettimeoffset(void);
-
 static struct irqaction bfin_timer_irq = {
-       .name = "BFIN Timer Tick",
+       .name = "Blackfin Timer Tick",
        .flags = IRQF_DISABLED
 };
 
-/*
- * The way that the Blackfin core timer works is:
- *  - CCLK is divided by a programmable 8-bit pre-scaler (TSCALE)
- *  - Every time TSCALE ticks, a 32bit is counted down (TCOUNT)
- *
- * If you take the fastest clock (1ns, or 1GHz to make the math work easier)
- *    10ms is 10,000,000 clock ticks, which fits easy into a 32-bit counter
- *    (32 bit counter is 4,294,967,296ns or 4.2 seconds) so, we don't need
- *    to use TSCALE, and program it to zero (which is pass CCLK through).
- *    If you feel like using it, try to keep HZ * TIMESCALE to some
- *    value that divides easy (like power of 2).
- */
-
-#define TIME_SCALE 1
-
-static void
-time_sched_init(irqreturn_t(*timer_routine) (int, void *))
+#if defined(CONFIG_IPIPE)
+void __init setup_system_timer0(void)
+{
+       /* Power down the core timer, just to play safe. */
+       bfin_write_TCNTL(0);
+
+       disable_gptimers(TIMER0bit);
+       set_gptimer_status(0, TIMER_STATUS_TRUN0);
+       while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
+               udelay(10);
+
+       set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
+       set_gptimer_period(TIMER0_id, get_sclk() / HZ);
+       set_gptimer_pwidth(TIMER0_id, 1);
+       SSYNC();
+       enable_gptimers(TIMER0bit);
+}
+#else
+void __init setup_core_timer(void)
 {
        u32 tcount;
 
@@ -72,10 +54,8 @@ time_sched_init(irqreturn_t(*timer_routine) (int, void *))
        bfin_write_TCNTL(1);
        CSYNC();
 
-       /*
-        * the TSCALE prescaler counter.
-        */
-       bfin_write_TSCALE((TIME_SCALE - 1));
+       /* the TSCALE prescaler counter */
+       bfin_write_TSCALE(TIME_SCALE - 1);
 
        tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
        bfin_write_TPERIOD(tcount);
@@ -85,35 +65,52 @@ time_sched_init(irqreturn_t(*timer_routine) (int, void *))
        CSYNC();
 
        bfin_write_TCNTL(7);
+}
+#endif
 
-       bfin_timer_irq.handler = (irq_handler_t)timer_routine;
-       /* call setup_irq instead of request_irq because request_irq calls
-        * kmalloc which has not been initialized yet
-        */
+static void __init
+time_sched_init(irqreturn_t(*timer_routine) (int, void *))
+{
+#if defined(CONFIG_IPIPE)
+       setup_system_timer0();
+       bfin_timer_irq.handler = timer_routine;
+       setup_irq(IRQ_TIMER0, &bfin_timer_irq);
+#else
+       setup_core_timer();
+       bfin_timer_irq.handler = timer_routine;
        setup_irq(IRQ_CORETMR, &bfin_timer_irq);
+#endif
 }
 
+#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
 /*
  * Should return useconds since last timer tick
  */
-static unsigned long gettimeoffset(void)
+u32 arch_gettimeoffset(void)
 {
        unsigned long offset;
        unsigned long clocks_per_jiffy;
 
+#if defined(CONFIG_IPIPE)
+       clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
+       offset = bfin_read_TIMER0_COUNTER() / \
+               (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
+
+       if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
+               offset += (USEC_PER_SEC / HZ);
+#else
        clocks_per_jiffy = bfin_read_TPERIOD();
-       offset =
-           (clocks_per_jiffy -
-            bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) /
-                                   USEC_PER_SEC);
+       offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
+               (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
 
        /* Check if we just wrapped the counters and maybe missed a tick */
        if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
-           && (offset < (100000 / HZ / 2)))
+               && (offset < (100000 / HZ / 2)))
                offset += (USEC_PER_SEC / HZ);
-
+#endif
        return offset;
 }
+#endif
 
 static inline int set_rtc_mmss(unsigned long nowtime)
 {
@@ -125,29 +122,21 @@ static inline int set_rtc_mmss(unsigned long nowtime)
  * as well as call the "do_timer()" routine every clocktick
  */
 #ifdef CONFIG_CORE_TIMER_IRQ_L1
-irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text));
+__attribute__((l1_text))
 #endif
-
 irqreturn_t timer_interrupt(int irq, void *dummy)
 {
        /* last time the cmos clock got updated */
        static long last_rtc_update;
 
        write_seqlock(&xtime_lock);
-
        do_timer(1);
 
-#ifndef CONFIG_SMP
-       update_process_times(user_mode(get_irq_regs()));
-#endif
-       profile_tick(CPU_PROFILING);
-
        /*
         * If we have an externally synchronized Linux clock, then update
         * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
         * called as close as possible to 500 ms before the new second starts.
         */
-
        if (ntp_synced() &&
            xtime.tv_sec > last_rtc_update + 660 &&
            (xtime.tv_nsec / NSEC_PER_USEC) >=
@@ -161,6 +150,14 @@ irqreturn_t timer_interrupt(int irq, void *dummy)
                        last_rtc_update = xtime.tv_sec - 600;
        }
        write_sequnlock(&xtime_lock);
+
+#ifdef CONFIG_IPIPE
+       update_root_process_times(get_irq_regs());
+#else
+       update_process_times(user_mode(get_irq_regs()));
+#endif
+       profile_tick(CPU_PROFILING);
+
        return IRQ_HANDLED;
 }
 
@@ -188,65 +185,6 @@ void __init time_init(void)
        time_sched_init(timer_interrupt);
 }
 
-#ifndef CONFIG_GENERIC_TIME
-void do_gettimeofday(struct timeval *tv)
-{
-       unsigned long flags;
-       unsigned long seq;
-       unsigned long usec, sec;
-
-       do {
-               seq = read_seqbegin_irqsave(&xtime_lock, flags);
-               usec = gettimeoffset();
-               sec = xtime.tv_sec;
-               usec += (xtime.tv_nsec / NSEC_PER_USEC);
-       }
-       while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
-
-       while (usec >= USEC_PER_SEC) {
-               usec -= USEC_PER_SEC;
-               sec++;
-       }
-
-       tv->tv_sec = sec;
-       tv->tv_usec = usec;
-}
-EXPORT_SYMBOL(do_gettimeofday);
-
-int do_settimeofday(struct timespec *tv)
-{
-       time_t wtm_sec, sec = tv->tv_sec;
-       long wtm_nsec, nsec = tv->tv_nsec;
-
-       if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
-               return -EINVAL;
-
-       write_seqlock_irq(&xtime_lock);
-       /*
-        * This is revolting. We need to set the xtime.tv_usec
-        * correctly. However, the value in this location is
-        * is value at the last tick.
-        * Discover what correction gettimeofday
-        * would have done, and then undo it!
-        */
-       nsec -= (gettimeoffset() * NSEC_PER_USEC);
-
-       wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
-       wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
-
-       set_normalized_timespec(&xtime, sec, nsec);
-       set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
-
-       ntp_clear();
-
-       write_sequnlock_irq(&xtime_lock);
-       clock_was_set();
-
-       return 0;
-}
-EXPORT_SYMBOL(do_settimeofday);
-#endif /* !CONFIG_GENERIC_TIME */
-
 /*
  * Scheduler clock - returns current time in nanosec units.
  */