Skip to content

Commit fcc8f0f

Browse files
hnazczankel
authored andcommitted
xtensa: ccount clocksource
Switch to GENERIC_TIME by using the ccount register as a clock source. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Signed-off-by: Chris Zankel <chris@zankel.net>
1 parent 4476c96 commit fcc8f0f

2 files changed

Lines changed: 33 additions & 73 deletions

File tree

‎arch/xtensa/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ config HZ
4848
int
4949
default 100
5050

51+
config GENERIC_TIME
52+
def_bool y
53+
5154
source "init/Kconfig"
5255
source "kernel/Kconfig.freezer"
5356

‎arch/xtensa/kernel/time.c‎

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <linux/errno.h>
1616
#include <linux/time.h>
17+
#include <linux/clocksource.h>
1718
#include <linux/interrupt.h>
1819
#include <linux/module.h>
1920
#include <linux/init.h>
@@ -29,6 +30,26 @@ unsigned long ccount_per_jiffy; /* per 1/HZ */
2930
unsigned long nsec_per_ccount; /* nsec per ccount increment */
3031
#endif
3132

33+
static cycle_t ccount_read(void)
34+
{
35+
return (cycle_t)get_ccount();
36+
}
37+
38+
static struct clocksource ccount_clocksource = {
39+
.name = "ccount",
40+
.rating = 200,
41+
.read = ccount_read,
42+
.mask = CLOCKSOURCE_MASK(32),
43+
/*
44+
* With a shift of 22 the lower limit of the cpu clock is
45+
* 1MHz, where NSEC_PER_CCOUNT is 1000 or a bit less than
46+
* 2^10: Since we have 32 bits and the multiplicator can
47+
* already take up as much as 10 bits, this leaves us with
48+
* remaining upper 22 bits.
49+
*/
50+
.shift = 22,
51+
};
52+
3253
static irqreturn_t timer_interrupt(int irq, void *dev_id);
3354
static struct irqaction timer_irqaction = {
3455
.handler = timer_interrupt,
@@ -38,92 +59,29 @@ static struct irqaction timer_irqaction = {
3859

3960
void __init time_init(void)
4061
{
41-
/* The platform must provide a function to calibrate the processor
42-
* speed for the CALIBRATE.
43-
*/
62+
xtime.tv_nsec = 0;
63+
xtime.tv_sec = read_persistent_clock();
64+
65+
set_normalized_timespec(&wall_to_monotonic,
66+
-xtime.tv_sec, -xtime.tv_nsec);
4467

4568
#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
4669
printk("Calibrating CPU frequency ");
4770
platform_calibrate_ccount();
4871
printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
4972
(int)(ccount_per_jiffy/(10000/HZ))%100);
5073
#endif
51-
52-
xtime.tv_nsec = 0;
53-
xtime.tv_sec = read_persistent_clock();
54-
55-
set_normalized_timespec(&wall_to_monotonic,
56-
-xtime.tv_sec, -xtime.tv_nsec);
74+
ccount_clocksource.mult =
75+
clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
76+
ccount_clocksource.shift);
77+
clocksource_register(&ccount_clocksource);
5778

5879
/* Initialize the linux timer interrupt. */
5980

6081
setup_irq(LINUX_TIMER_INT, &timer_irqaction);
6182
set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
6283
}
6384

64-
65-
int do_settimeofday(struct timespec *tv)
66-
{
67-
time_t wtm_sec, sec = tv->tv_sec;
68-
long wtm_nsec, nsec = tv->tv_nsec;
69-
unsigned long delta;
70-
71-
if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
72-
return -EINVAL;
73-
74-
write_seqlock_irq(&xtime_lock);
75-
76-
/* This is revolting. We need to set "xtime" correctly. However, the
77-
* value in this location is the value at the most recent update of
78-
* wall time. Discover what correction gettimeofday() would have
79-
* made, and then undo it!
80-
*/
81-
82-
delta = CCOUNT_PER_JIFFY;
83-
delta += get_ccount() - get_linux_timer();
84-
nsec -= delta * NSEC_PER_CCOUNT;
85-
86-
wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
87-
wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
88-
89-
set_normalized_timespec(&xtime, sec, nsec);
90-
set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
91-
92-
ntp_clear();
93-
write_sequnlock_irq(&xtime_lock);
94-
return 0;
95-
}
96-
97-
EXPORT_SYMBOL(do_settimeofday);
98-
99-
100-
void do_gettimeofday(struct timeval *tv)
101-
{
102-
unsigned long flags;
103-
unsigned long volatile sec, usec, delta, seq;
104-
105-
do {
106-
seq = read_seqbegin_irqsave(&xtime_lock, flags);
107-
108-
sec = xtime.tv_sec;
109-
usec = (xtime.tv_nsec / NSEC_PER_USEC);
110-
111-
delta = get_linux_timer() - get_ccount();
112-
113-
} while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
114-
115-
usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
116-
* (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;
117-
118-
for (; usec >= 1000000; sec++, usec -= 1000000)
119-
;
120-
121-
tv->tv_sec = sec;
122-
tv->tv_usec = usec;
123-
}
124-
125-
EXPORT_SYMBOL(do_gettimeofday);
126-
12785
/*
12886
* The timer interrupt is called HZ times per second.
12987
*/
@@ -177,4 +135,3 @@ void __cpuinit calibrate_delay(void)
177135
(loops_per_jiffy/(10000/HZ)) % 100);
178136
}
179137
#endif
180-

0 commit comments

Comments
 (0)