[timer] Formalise the timer API

We now have two implementations for the timer API: one using the
time-of-day counter at 40:70 and one using RDTSC.  Both make use of
timer2_udelay().
This commit is contained in:
Michael Brown
2008-10-12 19:56:52 +01:00
parent e6f276ece3
commit 16f1e35775
22 changed files with 386 additions and 284 deletions
+87
View File
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file
*
* RDTSC timer
*
*/
#include <assert.h>
#include <gpxe/timer.h>
#include <gpxe/timer2.h>
/**
* Number of TSC ticks per microsecond
*
* This is calibrated on the first use of the timer.
*/
static unsigned long rdtsc_ticks_per_usec;
/**
* Delay for a fixed number of microseconds
*
* @v usecs Number of microseconds for which to delay
*/
static void rdtsc_udelay ( unsigned long usecs ) {
unsigned long start;
unsigned long elapsed;
/* Sanity guard, since we may divide by this */
if ( ! usecs )
usecs = 1;
start = currticks();
if ( rdtsc_ticks_per_usec ) {
/* Already calibrated; busy-wait until done */
do {
elapsed = ( currticks() - start );
} while ( elapsed < ( usecs * rdtsc_ticks_per_usec ) );
} else {
/* Not yet calibrated; use timer2 and calibrate
* based on result.
*/
timer2_udelay ( usecs );
elapsed = ( currticks() - start );
rdtsc_ticks_per_usec = ( elapsed / usecs );
DBG ( "RDTSC timer calibrated: %ld ticks in %ld usecs "
"(%ld MHz)\n", elapsed, usecs,
( rdtsc_ticks_per_usec << TSC_SHIFT ) );
}
}
/**
* Get number of ticks per second
*
* @ret ticks_per_sec Number of ticks per second
*/
static unsigned long rdtsc_ticks_per_sec ( void ) {
/* Calibrate timer, if not already done */
if ( ! rdtsc_ticks_per_usec )
udelay ( 1 );
/* Sanity check */
assert ( rdtsc_ticks_per_usec != 0 );
return ( rdtsc_ticks_per_usec * 1000 * 1000 );
}
PROVIDE_TIMER ( rdtsc, udelay, rdtsc_udelay );
PROVIDE_TIMER_INLINE ( rdtsc, currticks );
PROVIDE_TIMER ( rdtsc, ticks_per_sec, rdtsc_ticks_per_sec );
@@ -12,12 +12,11 @@
*/
#include <stddef.h>
#include <bits/timer2.h>
#include <gpxe/timer.h>
#include <gpxe/timer2.h>
#include <gpxe/io.h>
/* Timers tick over at this rate */
#define TIMER2_TICK_RATE 1193180U
#define TIMER2_TICKS_PER_SEC 1193180U
/* Parallel Peripheral Controller Port B */
#define PPC_PORTB 0x61
@@ -52,8 +51,7 @@
#define BINARY_COUNT 0x00
#define BCD_COUNT 0x01
static void load_timer2(unsigned int ticks)
{
static void load_timer2 ( unsigned int ticks ) {
/*
* Now let's take care of PPC channel 2
*
@@ -75,15 +73,13 @@ static void load_timer2(unsigned int ticks)
outb(ticks >> 8, TIMER2_PORT);
}
static int timer2_running(void)
{
static int timer2_running ( void ) {
return ((inb(PPC_PORTB) & PPCB_T2OUT) == 0);
}
void i386_timer2_udelay(unsigned int usecs)
{
load_timer2((usecs * TIMER2_TICK_RATE)/USECS_IN_SEC);
while (timer2_running())
;
void timer2_udelay ( unsigned long usecs ) {
load_timer2 ( ( usecs * TIMER2_TICKS_PER_SEC ) / ( 1000 * 1000 ) );
while (timer2_running()) {
/* Do nothing */
}
}