xref: /openbmc/linux/arch/xtensa/kernel/time.c (revision 8fdff1dc)
1 /*
2  * arch/xtensa/kernel/time.c
3  *
4  * Timer and clock support.
5  *
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
8  * for more details.
9  *
10  * Copyright (C) 2005 Tensilica Inc.
11  *
12  * Chris Zankel <chris@zankel.net>
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 #include <linux/irqdomain.h>
26 
27 #include <asm/timex.h>
28 #include <asm/platform.h>
29 
30 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
31 unsigned long ccount_per_jiffy;		/* per 1/HZ */
32 unsigned long nsec_per_ccount;		/* nsec per ccount increment */
33 #endif
34 
35 static cycle_t ccount_read(struct clocksource *cs)
36 {
37 	return (cycle_t)get_ccount();
38 }
39 
40 static struct clocksource ccount_clocksource = {
41 	.name = "ccount",
42 	.rating = 200,
43 	.read = ccount_read,
44 	.mask = CLOCKSOURCE_MASK(32),
45 };
46 
47 static irqreturn_t timer_interrupt(int irq, void *dev_id);
48 static struct irqaction timer_irqaction = {
49 	.handler =	timer_interrupt,
50 	.flags =	IRQF_DISABLED,
51 	.name =		"timer",
52 };
53 
54 void __init time_init(void)
55 {
56 	unsigned int irq;
57 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
58 	printk("Calibrating CPU frequency ");
59 	platform_calibrate_ccount();
60 	printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
61 			(int)(ccount_per_jiffy/(10000/HZ))%100);
62 #endif
63 	clocksource_register_hz(&ccount_clocksource, CCOUNT_PER_JIFFY * HZ);
64 
65 	/* Initialize the linux timer interrupt. */
66 
67 	irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
68 	setup_irq(irq, &timer_irqaction);
69 	set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
70 }
71 
72 /*
73  * The timer interrupt is called HZ times per second.
74  */
75 
76 irqreturn_t timer_interrupt (int irq, void *dev_id)
77 {
78 
79 	unsigned long next;
80 
81 	next = get_linux_timer();
82 
83 again:
84 	while ((signed long)(get_ccount() - next) > 0) {
85 
86 		profile_tick(CPU_PROFILING);
87 #ifndef CONFIG_SMP
88 		update_process_times(user_mode(get_irq_regs()));
89 #endif
90 
91 		xtime_update(1); /* Linux handler in kernel/time/timekeeping */
92 
93 		/* Note that writing CCOMPARE clears the interrupt. */
94 
95 		next += CCOUNT_PER_JIFFY;
96 		set_linux_timer(next);
97 	}
98 
99 	/* Allow platform to do something useful (Wdog). */
100 
101 	platform_heartbeat();
102 
103 	/* Make sure we didn't miss any tick... */
104 
105 	if ((signed long)(get_ccount() - next) > 0)
106 		goto again;
107 
108 	return IRQ_HANDLED;
109 }
110 
111 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
112 void __cpuinit calibrate_delay(void)
113 {
114 	loops_per_jiffy = CCOUNT_PER_JIFFY;
115 	printk("Calibrating delay loop (skipped)... "
116 	       "%lu.%02lu BogoMIPS preset\n",
117 	       loops_per_jiffy/(1000000/HZ),
118 	       (loops_per_jiffy/(10000/HZ)) % 100);
119 }
120 #endif
121