2 * arch/xtensa/kernel/time.c
4 * Timer and clock support.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2005 Tensilica Inc.
12 * Chris Zankel <chris@zankel.net>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/time.h>
18 #include <linux/clocksource.h>
19 #include <linux/clockchips.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/irq.h>
24 #include <linux/profile.h>
25 #include <linux/delay.h>
26 #include <linux/irqdomain.h>
27 #include <linux/sched_clock.h>
29 #include <asm/timex.h>
30 #include <asm/platform.h>
32 unsigned long ccount_freq
; /* ccount Hz */
34 static cycle_t
ccount_read(struct clocksource
*cs
)
36 return (cycle_t
)get_ccount();
39 static u64 notrace
ccount_sched_clock_read(void)
44 static struct clocksource ccount_clocksource
= {
48 .mask
= CLOCKSOURCE_MASK(32),
49 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
52 static int ccount_timer_set_next_event(unsigned long delta
,
53 struct clock_event_device
*dev
);
54 static void ccount_timer_set_mode(enum clock_event_mode mode
,
55 struct clock_event_device
*evt
);
57 struct clock_event_device evt
;
61 static DEFINE_PER_CPU(struct ccount_timer
, ccount_timer
);
63 static int ccount_timer_set_next_event(unsigned long delta
,
64 struct clock_event_device
*dev
)
66 unsigned long flags
, next
;
69 local_irq_save(flags
);
70 next
= get_ccount() + delta
;
71 set_linux_timer(next
);
72 if (next
- get_ccount() > delta
)
74 local_irq_restore(flags
);
79 static void ccount_timer_set_mode(enum clock_event_mode mode
,
80 struct clock_event_device
*evt
)
82 struct ccount_timer
*timer
=
83 container_of(evt
, struct ccount_timer
, evt
);
86 * There is no way to disable the timer interrupt at the device level,
87 * only at the intenable register itself. Since enable_irq/disable_irq
88 * calls are nested, we need to make sure that these calls are
92 case CLOCK_EVT_MODE_SHUTDOWN
:
93 case CLOCK_EVT_MODE_UNUSED
:
94 if (timer
->irq_enabled
) {
95 disable_irq(evt
->irq
);
96 timer
->irq_enabled
= 0;
99 case CLOCK_EVT_MODE_RESUME
:
100 case CLOCK_EVT_MODE_ONESHOT
:
101 if (!timer
->irq_enabled
) {
102 enable_irq(evt
->irq
);
103 timer
->irq_enabled
= 1;
110 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
);
111 static struct irqaction timer_irqaction
= {
112 .handler
= timer_interrupt
,
117 void local_timer_setup(unsigned cpu
)
119 struct ccount_timer
*timer
= &per_cpu(ccount_timer
, cpu
);
120 struct clock_event_device
*clockevent
= &timer
->evt
;
122 timer
->irq_enabled
= 1;
123 clockevent
->name
= timer
->name
;
124 snprintf(timer
->name
, sizeof(timer
->name
), "ccount_clockevent_%u", cpu
);
125 clockevent
->features
= CLOCK_EVT_FEAT_ONESHOT
;
126 clockevent
->rating
= 300;
127 clockevent
->set_next_event
= ccount_timer_set_next_event
;
128 clockevent
->set_mode
= ccount_timer_set_mode
;
129 clockevent
->cpumask
= cpumask_of(cpu
);
130 clockevent
->irq
= irq_create_mapping(NULL
, LINUX_TIMER_INT
);
131 if (WARN(!clockevent
->irq
, "error: can't map timer irq"))
133 clockevents_config_and_register(clockevent
, ccount_freq
,
137 void __init
time_init(void)
139 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
140 printk("Calibrating CPU frequency ");
141 platform_calibrate_ccount();
142 printk("%d.%02d MHz\n", (int)ccount_freq
/1000000,
143 (int)(ccount_freq
/10000)%100);
145 ccount_freq
= CONFIG_XTENSA_CPU_CLOCK
*1000000UL;
147 clocksource_register_hz(&ccount_clocksource
, ccount_freq
);
148 local_timer_setup(0);
149 setup_irq(this_cpu_ptr(&ccount_timer
)->evt
.irq
, &timer_irqaction
);
150 sched_clock_register(ccount_sched_clock_read
, 32, ccount_freq
);
151 clocksource_of_init();
155 * The timer interrupt is called HZ times per second.
158 irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
160 struct clock_event_device
*evt
= &this_cpu_ptr(&ccount_timer
)->evt
;
162 set_linux_timer(get_linux_timer());
163 evt
->event_handler(evt
);
165 /* Allow platform to do something useful (Wdog). */
166 platform_heartbeat();
171 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
172 void calibrate_delay(void)
174 loops_per_jiffy
= ccount_freq
/ HZ
;
175 printk("Calibrating delay loop (skipped)... "
176 "%lu.%02lu BogoMIPS preset\n",
177 loops_per_jiffy
/(1000000/HZ
),
178 (loops_per_jiffy
/(10000/HZ
)) % 100);