blob: 395caf01b9099cc42c2b2e3b0148765df3de0ed9 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* MN10300 Low level time management
2 *
David Howells08ec3c22008-09-24 17:48:31 +01003 * Copyright (C) 2007-2008 Red Hat, Inc. All Rights Reserved.
David Howellsb920de12008-02-08 04:19:31 -08004 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/i386/kernel/time.c
6 *
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.
11 */
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>
David Howells08ec3c22008-09-24 17:48:31 +010019#include <linux/cnt32_to_63.h>
David Howellsb920de12008-02-08 04:19:31 -080020#include <asm/irq.h>
21#include <asm/div64.h>
22#include <asm/processor.h>
23#include <asm/intctl-regs.h>
24#include <asm/rtc.h>
25
26#ifdef CONFIG_MN10300_RTC
27unsigned long mn10300_ioclk; /* system I/O clock frequency */
28unsigned long mn10300_iobclk; /* system I/O clock frequency */
29unsigned long mn10300_tsc_per_HZ; /* number of ioclks per jiffy */
30#endif /* CONFIG_MN10300_RTC */
31
32static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
33 * interrupt occurred */
34
35static irqreturn_t timer_interrupt(int irq, void *dev_id);
36
37static struct irqaction timer_irq = {
38 .handler = timer_interrupt,
39 .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
David Howellsb920de12008-02-08 04:19:31 -080040 .name = "timer",
41};
42
David Howells08ec3c22008-09-24 17:48:31 +010043static unsigned long sched_clock_multiplier;
44
David Howellsb920de12008-02-08 04:19:31 -080045/*
46 * scheduler clock - returns current time in nanosec units.
47 */
48unsigned long long sched_clock(void)
49{
50 union {
David Howells08ec3c22008-09-24 17:48:31 +010051 unsigned long long ll;
52 unsigned l[2];
53 } tsc64, result;
54 unsigned long tsc, tmp;
55 unsigned product[3]; /* 96-bit intermediate value */
David Howellsb920de12008-02-08 04:19:31 -080056
David Howells08ec3c22008-09-24 17:48:31 +010057 /* read the TSC value
58 */
59 tsc = 0 - get_cycles(); /* get_cycles() counts down */
David Howellsb920de12008-02-08 04:19:31 -080060
David Howells08ec3c22008-09-24 17:48:31 +010061 /* expand to 64-bits.
62 * - sched_clock() must be called once a minute or better or the
63 * following will go horribly wrong - see cnt32_to_63()
64 */
65 tsc64.ll = cnt32_to_63(tsc) & 0x7fffffffffffffffULL;
66
67 /* scale the 64-bit TSC value to a nanosecond value via a 96-bit
68 * intermediate
69 */
70 asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */
71 "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */
72 "add %3,%1 \n"
73 "addc 0,%2 \n" /* result in %2:%1:%0 */
74 : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)
75 : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)
David Howellsb920de12008-02-08 04:19:31 -080076 : "cc");
77
David Howells08ec3c22008-09-24 17:48:31 +010078 result.l[0] = product[1] << 16 | product[0] >> 16;
79 result.l[1] = product[2] << 16 | product[1] >> 16;
David Howellsb920de12008-02-08 04:19:31 -080080
David Howells08ec3c22008-09-24 17:48:31 +010081 return result.ll;
82}
83
84/*
85 * initialise the scheduler clock
86 */
87static void __init mn10300_sched_clock_init(void)
88{
89 sched_clock_multiplier =
90 __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
David Howellsb920de12008-02-08 04:19:31 -080091}
92
93/*
94 * advance the kernel's time keeping clocks (xtime and jiffies)
95 * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
96 * there's a need to update
97 */
98static irqreturn_t timer_interrupt(int irq, void *dev_id)
99{
100 unsigned tsc, elapse;
101
102 write_seqlock(&xtime_lock);
103
104 while (tsc = get_cycles(),
105 elapse = mn10300_last_tsc - tsc, /* time elapsed since last
106 * tick */
107 elapse > MN10300_TSC_PER_HZ
108 ) {
109 mn10300_last_tsc -= MN10300_TSC_PER_HZ;
110
111 /* advance the kernel's time tracking system */
112 profile_tick(CPU_PROFILING);
113 do_timer(1);
David Howellsb920de12008-02-08 04:19:31 -0800114 check_rtc_time();
115 }
116
117 write_sequnlock(&xtime_lock);
David Howells2b79aac2008-02-19 18:58:49 +0000118
119 update_process_times(user_mode(get_irq_regs()));
120
David Howellsb920de12008-02-08 04:19:31 -0800121 return IRQ_HANDLED;
122}
123
124/*
125 * initialise the various timers used by the main part of the kernel
126 */
127void __init time_init(void)
128{
129 /* we need the prescalar running to be able to use IOCLK/8
130 * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
131 * - IOCLK runs at Fosc rate (crystal speed)
132 */
133 TMPSCNT |= TMPSCNT_ENABLE;
134
135 startup_timestamp_counter();
136
137 printk(KERN_INFO
138 "timestamp counter I/O clock running at %lu.%02lu"
139 " (calibrated against RTC)\n",
140 MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
141
142 xtime.tv_sec = get_initial_rtc_time();
143 xtime.tv_nsec = 0;
144
145 mn10300_last_tsc = TMTSCBC;
146
147 /* use timer 0 & 1 cascaded to tick at as close to HZ as possible */
148 setup_irq(TMJCIRQ, &timer_irq);
149
150 set_intr_level(TMJCIRQ, TMJCICR_LEVEL);
151
152 startup_jiffies_counter();
153
154#ifdef CONFIG_MN10300_WD_TIMER
155 /* start the watchdog timer */
156 watchdog_go();
157#endif
David Howells08ec3c22008-09-24 17:48:31 +0100158
159 mn10300_sched_clock_init();
David Howellsb920de12008-02-08 04:19:31 -0800160}