1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/errno.h>
16#include <linux/sched.h>
17#include <linux/time.h>
18#include <linux/clocksource.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/irq.h>
23#include <linux/profile.h>
24#include <linux/delay.h>
25
26#include <asm/timex.h>
27#include <asm/platform.h>
28
29#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
30unsigned long ccount_per_jiffy;
31unsigned long nsec_per_ccount;
32#endif
33
34static cycle_t ccount_read(void)
35{
36 return (cycle_t)get_ccount();
37}
38
39static struct clocksource ccount_clocksource = {
40 .name = "ccount",
41 .rating = 200,
42 .read = ccount_read,
43 .mask = CLOCKSOURCE_MASK(32),
44
45
46
47
48
49
50
51 .shift = 22,
52};
53
54static irqreturn_t timer_interrupt(int irq, void *dev_id);
55static struct irqaction timer_irqaction = {
56 .handler = timer_interrupt,
57 .flags = IRQF_DISABLED,
58 .name = "timer",
59};
60
61void __init time_init(void)
62{
63
64 read_persistent_clock(&xtime);
65 set_normalized_timespec(&wall_to_monotonic,
66 -xtime.tv_sec, -xtime.tv_nsec);
67
68#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
69 printk("Calibrating CPU frequency ");
70 platform_calibrate_ccount();
71 printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
72 (int)(ccount_per_jiffy/(10000/HZ))%100);
73#endif
74 ccount_clocksource.mult =
75 clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
76 ccount_clocksource.shift);
77 clocksource_register(&ccount_clocksource);
78
79
80
81 setup_irq(LINUX_TIMER_INT, &timer_irqaction);
82 set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
83}
84
85
86
87
88
89irqreturn_t timer_interrupt (int irq, void *dev_id)
90{
91
92 unsigned long next;
93
94 next = get_linux_timer();
95
96again:
97 while ((signed long)(get_ccount() - next) > 0) {
98
99 profile_tick(CPU_PROFILING);
100#ifndef CONFIG_SMP
101 update_process_times(user_mode(get_irq_regs()));
102#endif
103
104 write_seqlock(&xtime_lock);
105
106 do_timer(1);
107
108
109
110 next += CCOUNT_PER_JIFFY;
111 set_linux_timer(next);
112
113 write_sequnlock(&xtime_lock);
114 }
115
116
117
118 platform_heartbeat();
119
120
121
122 if ((signed long)(get_ccount() - next) > 0)
123 goto again;
124
125 return IRQ_HANDLED;
126}
127
128#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
129void __cpuinit calibrate_delay(void)
130{
131 loops_per_jiffy = CCOUNT_PER_JIFFY;
132 printk("Calibrating delay loop (skipped)... "
133 "%lu.%02lu BogoMIPS preset\n",
134 loops_per_jiffy/(1000000/HZ),
135 (loops_per_jiffy/(10000/HZ)) % 100);
136}
137#endif
138