blob: 5b955000626d9f79fc3abe126dcc117c0076bc1e [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>
Mark Salter730c1fa2010-10-27 17:28:57 +010020#include <linux/clocksource.h>
21#include <linux/clockchips.h>
David Howellsb920de12008-02-08 04:19:31 -080022#include <asm/irq.h>
23#include <asm/div64.h>
24#include <asm/processor.h>
25#include <asm/intctl-regs.h>
26#include <asm/rtc.h>
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010027#include "internal.h"
David Howellsb920de12008-02-08 04:19:31 -080028
29static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
30 * interrupt occurred */
31
David Howells08ec3c22008-09-24 17:48:31 +010032static unsigned long sched_clock_multiplier;
33
David Howellsb920de12008-02-08 04:19:31 -080034/*
35 * scheduler clock - returns current time in nanosec units.
36 */
37unsigned long long sched_clock(void)
38{
39 union {
David Howells08ec3c22008-09-24 17:48:31 +010040 unsigned long long ll;
41 unsigned l[2];
42 } tsc64, result;
Nicolas Pitreb8da46d2010-12-20 00:29:32 -050043 unsigned long tmp;
David Howells08ec3c22008-09-24 17:48:31 +010044 unsigned product[3]; /* 96-bit intermediate value */
David Howellsb920de12008-02-08 04:19:31 -080045
David Howells3b950de2010-10-27 17:28:35 +010046 /* cnt32_to_63() is not safe with preemption */
47 preempt_disable();
48
Nicolas Pitreb8da46d2010-12-20 00:29:32 -050049 /* expand the tsc to 64-bits.
David Howells08ec3c22008-09-24 17:48:31 +010050 * - sched_clock() must be called once a minute or better or the
51 * following will go horribly wrong - see cnt32_to_63()
52 */
Nicolas Pitreb8da46d2010-12-20 00:29:32 -050053 tsc64.ll = cnt32_to_63(get_cycles()) & 0x7fffffffffffffffULL;
David Howells08ec3c22008-09-24 17:48:31 +010054
David Howells3b950de2010-10-27 17:28:35 +010055 preempt_enable();
56
David Howells08ec3c22008-09-24 17:48:31 +010057 /* scale the 64-bit TSC value to a nanosecond value via a 96-bit
58 * intermediate
59 */
60 asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */
61 "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */
62 "add %3,%1 \n"
63 "addc 0,%2 \n" /* result in %2:%1:%0 */
64 : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)
65 : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)
David Howellsb920de12008-02-08 04:19:31 -080066 : "cc");
67
David Howells08ec3c22008-09-24 17:48:31 +010068 result.l[0] = product[1] << 16 | product[0] >> 16;
69 result.l[1] = product[2] << 16 | product[1] >> 16;
David Howellsb920de12008-02-08 04:19:31 -080070
David Howells08ec3c22008-09-24 17:48:31 +010071 return result.ll;
72}
73
74/*
75 * initialise the scheduler clock
76 */
77static void __init mn10300_sched_clock_init(void)
78{
79 sched_clock_multiplier =
80 __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
David Howellsb920de12008-02-08 04:19:31 -080081}
82
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010083/**
84 * local_timer_interrupt - Local timer interrupt handler
85 *
86 * Handle local timer interrupts for this CPU. They may have been propagated
87 * to this CPU from the CPU that actually gets them by way of an IPI.
88 */
89irqreturn_t local_timer_interrupt(void)
90{
91 profile_tick(CPU_PROFILING);
92 update_process_times(user_mode(get_irq_regs()));
93 return IRQ_HANDLED;
94}
95
Mark Salter730c1fa2010-10-27 17:28:57 +010096#ifndef CONFIG_GENERIC_TIME
David Howellsb920de12008-02-08 04:19:31 -080097/*
98 * advance the kernel's time keeping clocks (xtime and jiffies)
99 * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
100 * there's a need to update
101 */
102static irqreturn_t timer_interrupt(int irq, void *dev_id)
103{
104 unsigned tsc, elapse;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100105 irqreturn_t ret;
David Howellsb920de12008-02-08 04:19:31 -0800106
David Howellsb920de12008-02-08 04:19:31 -0800107 while (tsc = get_cycles(),
Mark Salter730c1fa2010-10-27 17:28:57 +0100108 elapse = tsc - mn10300_last_tsc, /* time elapsed since last
David Howellsb920de12008-02-08 04:19:31 -0800109 * tick */
110 elapse > MN10300_TSC_PER_HZ
111 ) {
Mark Salter730c1fa2010-10-27 17:28:57 +0100112 mn10300_last_tsc += MN10300_TSC_PER_HZ;
David Howellsb920de12008-02-08 04:19:31 -0800113
114 /* advance the kernel's time tracking system */
Torben Hohn22b7fcd2011-01-27 16:00:12 +0100115 xtime_update(1);
David Howellsb920de12008-02-08 04:19:31 -0800116 }
117
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100118 ret = local_timer_interrupt();
119#ifdef CONFIG_SMP
120 send_IPI_allbutself(LOCAL_TIMER_IPI);
121#endif
122 return ret;
David Howellsb920de12008-02-08 04:19:31 -0800123}
124
Mark Salter730c1fa2010-10-27 17:28:57 +0100125static struct irqaction timer_irq = {
126 .handler = timer_interrupt,
127 .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
128 .name = "timer",
129};
130#endif /* CONFIG_GENERIC_TIME */
131
132#ifdef CONFIG_CSRC_MN10300
133void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
134{
135 u64 temp;
136 u32 shift;
137
138 /* Find a shift value */
139 for (shift = 32; shift > 0; shift--) {
140 temp = (u64) NSEC_PER_SEC << shift;
141 do_div(temp, clock);
142 if ((temp >> 32) == 0)
143 break;
144 }
145 cs->shift = shift;
146 cs->mult = (u32) temp;
147}
148#endif
149
150#if CONFIG_CEVT_MN10300
151void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
152 unsigned int clock)
153{
154 u64 temp;
155 u32 shift;
156
157 /* Find a shift value */
158 for (shift = 32; shift > 0; shift--) {
159 temp = (u64) clock << shift;
160 do_div(temp, NSEC_PER_SEC);
161 if ((temp >> 32) == 0)
162 break;
163 }
164 cd->shift = shift;
165 cd->mult = (u32) temp;
166}
167#endif
168
David Howellsb920de12008-02-08 04:19:31 -0800169/*
170 * initialise the various timers used by the main part of the kernel
171 */
172void __init time_init(void)
173{
174 /* we need the prescalar running to be able to use IOCLK/8
175 * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
176 * - IOCLK runs at Fosc rate (crystal speed)
177 */
178 TMPSCNT |= TMPSCNT_ENABLE;
179
Mark Salter730c1fa2010-10-27 17:28:57 +0100180#ifdef CONFIG_GENERIC_TIME
181 init_clocksource();
182#else
David Howellsb920de12008-02-08 04:19:31 -0800183 startup_timestamp_counter();
Mark Salter730c1fa2010-10-27 17:28:57 +0100184#endif
David Howellsb920de12008-02-08 04:19:31 -0800185
186 printk(KERN_INFO
187 "timestamp counter I/O clock running at %lu.%02lu"
188 " (calibrated against RTC)\n",
189 MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
190
Mark Salter730c1fa2010-10-27 17:28:57 +0100191 mn10300_last_tsc = read_timestamp_counter();
David Howellsb920de12008-02-08 04:19:31 -0800192
Mark Salter730c1fa2010-10-27 17:28:57 +0100193#ifdef CONFIG_GENERIC_CLOCKEVENTS
194 init_clockevents();
195#else
196 reload_jiffies_counter(MN10300_JC_PER_HZ - 1);
197 setup_jiffies_interrupt(TMJCIRQ, &timer_irq, CONFIG_TIMER_IRQ_LEVEL);
198#endif
David Howellsb920de12008-02-08 04:19:31 -0800199
200#ifdef CONFIG_MN10300_WD_TIMER
201 /* start the watchdog timer */
202 watchdog_go();
203#endif
David Howells08ec3c22008-09-24 17:48:31 +0100204
205 mn10300_sched_clock_init();
David Howellsb920de12008-02-08 04:19:31 -0800206}