1
2
3
4
5
6
7
8
9
10
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>
19#include <linux/cnt32_to_63.h>
20#include <linux/clocksource.h>
21#include <linux/clockchips.h>
22#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>
27#include "internal.h"
28
29static unsigned long mn10300_last_tsc;
30
31
32static unsigned long sched_clock_multiplier;
33
34
35
36
37unsigned long long sched_clock(void)
38{
39 union {
40 unsigned long long ll;
41 unsigned l[2];
42 } tsc64, result;
43 unsigned long tmp;
44 unsigned product[3];
45
46
47 preempt_disable();
48
49
50
51
52
53 tsc64.ll = cnt32_to_63(get_cycles()) & 0x7fffffffffffffffULL;
54
55 preempt_enable();
56
57
58
59
60 asm("mulu %2,%0,%3,%0 \n"
61 "mulu %2,%1,%2,%1 \n"
62 "add %3,%1 \n"
63 "addc 0,%2 \n"
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)
66 : "cc");
67
68 result.l[0] = product[1] << 16 | product[0] >> 16;
69 result.l[1] = product[2] << 16 | product[1] >> 16;
70
71 return result.ll;
72}
73
74
75
76
77static void __init mn10300_sched_clock_init(void)
78{
79 sched_clock_multiplier =
80 __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
81}
82
83
84
85
86
87
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
96#ifndef CONFIG_GENERIC_TIME
97
98
99
100
101
102static irqreturn_t timer_interrupt(int irq, void *dev_id)
103{
104 unsigned tsc, elapse;
105 irqreturn_t ret;
106
107 write_seqlock(&xtime_lock);
108
109 while (tsc = get_cycles(),
110 elapse = tsc - mn10300_last_tsc,
111
112 elapse > MN10300_TSC_PER_HZ
113 ) {
114 mn10300_last_tsc += MN10300_TSC_PER_HZ;
115
116
117 do_timer(1);
118 }
119
120 write_sequnlock(&xtime_lock);
121
122 ret = local_timer_interrupt();
123#ifdef CONFIG_SMP
124 send_IPI_allbutself(LOCAL_TIMER_IPI);
125#endif
126 return ret;
127}
128
129static struct irqaction timer_irq = {
130 .handler = timer_interrupt,
131 .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
132 .name = "timer",
133};
134#endif
135
136#ifdef CONFIG_CSRC_MN10300
137void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
138{
139 u64 temp;
140 u32 shift;
141
142
143 for (shift = 32; shift > 0; shift--) {
144 temp = (u64) NSEC_PER_SEC << shift;
145 do_div(temp, clock);
146 if ((temp >> 32) == 0)
147 break;
148 }
149 cs->shift = shift;
150 cs->mult = (u32) temp;
151}
152#endif
153
154#if CONFIG_CEVT_MN10300
155void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
156 unsigned int clock)
157{
158 u64 temp;
159 u32 shift;
160
161
162 for (shift = 32; shift > 0; shift--) {
163 temp = (u64) clock << shift;
164 do_div(temp, NSEC_PER_SEC);
165 if ((temp >> 32) == 0)
166 break;
167 }
168 cd->shift = shift;
169 cd->mult = (u32) temp;
170}
171#endif
172
173
174
175
176void __init time_init(void)
177{
178
179
180
181
182 TMPSCNT |= TMPSCNT_ENABLE;
183
184#ifdef CONFIG_GENERIC_TIME
185 init_clocksource();
186#else
187 startup_timestamp_counter();
188#endif
189
190 printk(KERN_INFO
191 "timestamp counter I/O clock running at %lu.%02lu"
192 " (calibrated against RTC)\n",
193 MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
194
195 mn10300_last_tsc = read_timestamp_counter();
196
197#ifdef CONFIG_GENERIC_CLOCKEVENTS
198 init_clockevents();
199#else
200 reload_jiffies_counter(MN10300_JC_PER_HZ - 1);
201 setup_jiffies_interrupt(TMJCIRQ, &timer_irq, CONFIG_TIMER_IRQ_LEVEL);
202#endif
203
204#ifdef CONFIG_MN10300_WD_TIMER
205
206 watchdog_go();
207#endif
208
209 mn10300_sched_clock_init();
210}
211