Skip to content

Commit

Permalink
xtensa: ccount clocksource
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Weiner authored and Chris Zankel committed Apr 3, 2009
1 parent 4476c96 commit fcc8f0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 73 deletions.
3 changes: 3 additions & 0 deletions arch/xtensa/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ config HZ
int
default 100

config GENERIC_TIME
def_bool y

source "init/Kconfig"
source "kernel/Kconfig.freezer"

Expand Down
103 changes: 30 additions & 73 deletions arch/xtensa/kernel/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <linux/errno.h>
#include <linux/time.h>
#include <linux/clocksource.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/init.h>
Expand All @@ -29,6 +30,26 @@ unsigned long ccount_per_jiffy; /* per 1/HZ */
unsigned long nsec_per_ccount; /* nsec per ccount increment */
#endif

static cycle_t ccount_read(void)
{
return (cycle_t)get_ccount();
}

static struct clocksource ccount_clocksource = {
.name = "ccount",
.rating = 200,
.read = ccount_read,
.mask = CLOCKSOURCE_MASK(32),
/*
* With a shift of 22 the lower limit of the cpu clock is
* 1MHz, where NSEC_PER_CCOUNT is 1000 or a bit less than
* 2^10: Since we have 32 bits and the multiplicator can
* already take up as much as 10 bits, this leaves us with
* remaining upper 22 bits.
*/
.shift = 22,
};

static irqreturn_t timer_interrupt(int irq, void *dev_id);
static struct irqaction timer_irqaction = {
.handler = timer_interrupt,
Expand All @@ -38,92 +59,29 @@ static struct irqaction timer_irqaction = {

void __init time_init(void)
{
/* The platform must provide a function to calibrate the processor
* speed for the CALIBRATE.
*/
xtime.tv_nsec = 0;
xtime.tv_sec = read_persistent_clock();

set_normalized_timespec(&wall_to_monotonic,
-xtime.tv_sec, -xtime.tv_nsec);

#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
printk("Calibrating CPU frequency ");
platform_calibrate_ccount();
printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
(int)(ccount_per_jiffy/(10000/HZ))%100);
#endif

xtime.tv_nsec = 0;
xtime.tv_sec = read_persistent_clock();

set_normalized_timespec(&wall_to_monotonic,
-xtime.tv_sec, -xtime.tv_nsec);
ccount_clocksource.mult =
clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
ccount_clocksource.shift);
clocksource_register(&ccount_clocksource);

/* Initialize the linux timer interrupt. */

setup_irq(LINUX_TIMER_INT, &timer_irqaction);
set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
}


int do_settimeofday(struct timespec *tv)
{
time_t wtm_sec, sec = tv->tv_sec;
long wtm_nsec, nsec = tv->tv_nsec;
unsigned long delta;

if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
return -EINVAL;

write_seqlock_irq(&xtime_lock);

/* This is revolting. We need to set "xtime" correctly. However, the
* value in this location is the value at the most recent update of
* wall time. Discover what correction gettimeofday() would have
* made, and then undo it!
*/

delta = CCOUNT_PER_JIFFY;
delta += get_ccount() - get_linux_timer();
nsec -= delta * NSEC_PER_CCOUNT;

wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);

set_normalized_timespec(&xtime, sec, nsec);
set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);

ntp_clear();
write_sequnlock_irq(&xtime_lock);
return 0;
}

EXPORT_SYMBOL(do_settimeofday);


void do_gettimeofday(struct timeval *tv)
{
unsigned long flags;
unsigned long volatile sec, usec, delta, seq;

do {
seq = read_seqbegin_irqsave(&xtime_lock, flags);

sec = xtime.tv_sec;
usec = (xtime.tv_nsec / NSEC_PER_USEC);

delta = get_linux_timer() - get_ccount();

} while (read_seqretry_irqrestore(&xtime_lock, seq, flags));

usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
* (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;

for (; usec >= 1000000; sec++, usec -= 1000000)
;

tv->tv_sec = sec;
tv->tv_usec = usec;
}

EXPORT_SYMBOL(do_gettimeofday);

/*
* The timer interrupt is called HZ times per second.
*/
Expand Down Expand Up @@ -177,4 +135,3 @@ void __cpuinit calibrate_delay(void)
(loops_per_jiffy/(10000/HZ)) % 100);
}
#endif

0 comments on commit fcc8f0f

Please sign in to comment.