1 /* MN10300 Low level time management
3 * Copyright (C) 2007-2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/i386/kernel/time.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/time.h>
16 #include <linux/init.h>
17 #include <linux/smp.h>
18 #include <linux/profile.h>
19 #include <linux/cnt32_to_63.h>
21 #include <asm/div64.h>
22 #include <asm/processor.h>
23 #include <asm/intctl-regs.h>
26 #ifdef CONFIG_MN10300_RTC
27 unsigned long mn10300_ioclk; /* system I/O clock frequency */
28 unsigned long mn10300_iobclk; /* system I/O clock frequency */
29 unsigned long mn10300_tsc_per_HZ; /* number of ioclks per jiffy */
30 #endif /* CONFIG_MN10300_RTC */
32 static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
33 * interrupt occurred */
35 static irqreturn_t timer_interrupt(int irq, void *dev_id);
37 static struct irqaction timer_irq = {
38 .handler = timer_interrupt,
39 .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
40 .mask = CPU_MASK_NONE,
44 static unsigned long sched_clock_multiplier;
47 * scheduler clock - returns current time in nanosec units.
49 unsigned long long sched_clock(void)
52 unsigned long long ll;
55 unsigned long tsc, tmp;
56 unsigned product[3]; /* 96-bit intermediate value */
60 tsc = 0 - get_cycles(); /* get_cycles() counts down */
63 * - sched_clock() must be called once a minute or better or the
64 * following will go horribly wrong - see cnt32_to_63()
66 tsc64.ll = cnt32_to_63(tsc) & 0x7fffffffffffffffULL;
68 /* scale the 64-bit TSC value to a nanosecond value via a 96-bit
71 asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */
72 "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */
74 "addc 0,%2 \n" /* result in %2:%1:%0 */
75 : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)
76 : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)
79 result.l[0] = product[1] << 16 | product[0] >> 16;
80 result.l[1] = product[2] << 16 | product[1] >> 16;
86 * initialise the scheduler clock
88 static void __init mn10300_sched_clock_init(void)
90 sched_clock_multiplier =
91 __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
95 * advance the kernel's time keeping clocks (xtime and jiffies)
96 * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
97 * there's a need to update
99 static irqreturn_t timer_interrupt(int irq, void *dev_id)
101 unsigned tsc, elapse;
103 write_seqlock(&xtime_lock);
105 while (tsc = get_cycles(),
106 elapse = mn10300_last_tsc - tsc, /* time elapsed since last
108 elapse > MN10300_TSC_PER_HZ
110 mn10300_last_tsc -= MN10300_TSC_PER_HZ;
112 /* advance the kernel's time tracking system */
113 profile_tick(CPU_PROFILING);
118 write_sequnlock(&xtime_lock);
120 update_process_times(user_mode(get_irq_regs()));
126 * initialise the various timers used by the main part of the kernel
128 void __init time_init(void)
130 /* we need the prescalar running to be able to use IOCLK/8
131 * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
132 * - IOCLK runs at Fosc rate (crystal speed)
134 TMPSCNT |= TMPSCNT_ENABLE;
136 startup_timestamp_counter();
139 "timestamp counter I/O clock running at %lu.%02lu"
140 " (calibrated against RTC)\n",
141 MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
143 xtime.tv_sec = get_initial_rtc_time();
146 mn10300_last_tsc = TMTSCBC;
148 /* use timer 0 & 1 cascaded to tick at as close to HZ as possible */
149 setup_irq(TMJCIRQ, &timer_irq);
151 set_intr_level(TMJCIRQ, TMJCICR_LEVEL);
153 startup_jiffies_counter();
155 #ifdef CONFIG_MN10300_WD_TIMER
156 /* start the watchdog timer */
160 mn10300_sched_clock_init();