mirror of
https://github.com/ipxe/ipxe
synced 2025-12-29 11:03:15 +03:00
Introduce the new timer subsystem.
Timer subsystem initialization code in core/timer.c Split the BIOS and RTDSC timer drivers from i386_timer.c Split arch/i386/firmware/pcbios/bios.c into the RTSDC timer driver and arch/i386/core/nap.c Split the headers properly: include/unistd.h - delay functions to be used by the gPXE core and drivers. include/gpxe/timer.h - the fimer subsystem interface to be used by the timer drivers and currticks() to be used by the code gPXE subsystems. include/latch.h - removed include/timer.h - scheduled for removal. Some driver are using currticks, which is only for core subsystems. Signed-off-by: Alexey Zaytsev <alexey.zaytsev@gmail.com>
This commit is contained in:
@@ -1,18 +1,58 @@
|
||||
/* A couple of routines to implement a low-overhead timer for drivers */
|
||||
|
||||
/*
|
||||
/*
|
||||
* arch/i386/core/i386_timer.c
|
||||
*
|
||||
* Use the "System Timer 2" to implement the udelay callback in
|
||||
* the BIOS timer driver. Also used to calibrate the clock rate
|
||||
* in the RTDSC timer driver.
|
||||
*
|
||||
* 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, or (at
|
||||
* your option) any later version.
|
||||
*/
|
||||
|
||||
#include "timer.h"
|
||||
#include "latch.h"
|
||||
#include <io.h>
|
||||
#include <gpxe/init.h>
|
||||
#include <stddef.h>
|
||||
#include <bits/timer2.h>
|
||||
#include <gpxe/timer.h>
|
||||
#include <io.h>
|
||||
|
||||
void __load_timer2(unsigned int ticks)
|
||||
/* Timers tick over at this rate */
|
||||
#define TIMER2_TICK_RATE 1193180U
|
||||
|
||||
/* Parallel Peripheral Controller Port B */
|
||||
#define PPC_PORTB 0x61
|
||||
|
||||
/* Meaning of the port bits */
|
||||
#define PPCB_T2OUT 0x20 /* Bit 5 */
|
||||
#define PPCB_SPKR 0x02 /* Bit 1 */
|
||||
#define PPCB_T2GATE 0x01 /* Bit 0 */
|
||||
|
||||
/* Ports for the 8254 timer chip */
|
||||
#define TIMER2_PORT 0x42
|
||||
#define TIMER_MODE_PORT 0x43
|
||||
|
||||
/* Meaning of the mode bits */
|
||||
#define TIMER0_SEL 0x00
|
||||
#define TIMER1_SEL 0x40
|
||||
#define TIMER2_SEL 0x80
|
||||
#define READBACK_SEL 0xC0
|
||||
|
||||
#define LATCH_COUNT 0x00
|
||||
#define LOBYTE_ACCESS 0x10
|
||||
#define HIBYTE_ACCESS 0x20
|
||||
#define WORD_ACCESS 0x30
|
||||
|
||||
#define MODE0 0x00
|
||||
#define MODE1 0x02
|
||||
#define MODE2 0x04
|
||||
#define MODE3 0x06
|
||||
#define MODE4 0x08
|
||||
#define MODE5 0x0A
|
||||
|
||||
#define BINARY_COUNT 0x00
|
||||
#define BCD_COUNT 0x01
|
||||
|
||||
static void load_timer2(unsigned int ticks)
|
||||
{
|
||||
/*
|
||||
* Now let's take care of PPC channel 2
|
||||
@@ -35,162 +75,15 @@ 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);
|
||||
}
|
||||
|
||||
#if !defined(CONFIG_TSC_CURRTICKS)
|
||||
static void setup_timers(void)
|
||||
void i386_timer2_udelay(unsigned int usecs)
|
||||
{
|
||||
return;
|
||||
load_timer2((usecs * TIMER2_TICK_RATE)/USECS_IN_SEC);
|
||||
while (timer2_running())
|
||||
;
|
||||
}
|
||||
|
||||
void load_timer2(unsigned int ticks)
|
||||
{
|
||||
return __load_timer2(ticks);
|
||||
}
|
||||
|
||||
int timer2_running(void)
|
||||
{
|
||||
return __timer2_running();
|
||||
}
|
||||
|
||||
void ndelay(unsigned int nsecs)
|
||||
{
|
||||
waiton_timer2((nsecs * CLOCK_TICK_RATE)/1000000000);
|
||||
}
|
||||
void udelay(unsigned int usecs)
|
||||
{
|
||||
waiton_timer2((usecs * TICKS_PER_MS)/1000);
|
||||
}
|
||||
#endif /* !defined(CONFIG_TSC_CURRTICKS) */
|
||||
|
||||
#if defined(CONFIG_TSC_CURRTICKS)
|
||||
|
||||
#define rdtsc(low,high) \
|
||||
__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
|
||||
|
||||
#define rdtscll(val) \
|
||||
__asm__ __volatile__ ("rdtsc" : "=A" (val))
|
||||
|
||||
|
||||
/* Number of clock ticks to time with the rtc */
|
||||
#define LATCH 0xFF
|
||||
|
||||
#define LATCHES_PER_SEC ((CLOCK_TICK_RATE + (LATCH/2))/LATCH)
|
||||
#define TICKS_PER_LATCH ((LATCHES_PER_SEC + (TICKS_PER_SEC/2))/TICKS_PER_SEC)
|
||||
|
||||
static void sleep_latch(void)
|
||||
{
|
||||
__load_timer2(LATCH);
|
||||
while(__timer2_running());
|
||||
}
|
||||
|
||||
/* ------ Calibrate the TSC -------
|
||||
* Time how long it takes to excute a loop that runs in known time.
|
||||
* And find the convertion needed to get to CLOCK_TICK_RATE
|
||||
*/
|
||||
|
||||
|
||||
static unsigned long long calibrate_tsc(void)
|
||||
{
|
||||
unsigned long startlow, starthigh;
|
||||
unsigned long endlow, endhigh;
|
||||
|
||||
rdtsc(startlow,starthigh);
|
||||
sleep_latch();
|
||||
rdtsc(endlow,endhigh);
|
||||
|
||||
/* 64-bit subtract - gcc just messes up with long longs */
|
||||
__asm__("subl %2,%0\n\t"
|
||||
"sbbl %3,%1"
|
||||
:"=a" (endlow), "=d" (endhigh)
|
||||
:"g" (startlow), "g" (starthigh),
|
||||
"0" (endlow), "1" (endhigh));
|
||||
|
||||
/* Error: ECPUTOOFAST */
|
||||
if (endhigh)
|
||||
goto bad_ctc;
|
||||
|
||||
endlow *= TICKS_PER_LATCH;
|
||||
return endlow;
|
||||
|
||||
/*
|
||||
* The CTC wasn't reliable: we got a hit on the very first read,
|
||||
* or the CPU was so fast/slow that the quotient wouldn't fit in
|
||||
* 32 bits..
|
||||
*/
|
||||
bad_ctc:
|
||||
printf("bad_ctc\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned long clocks_per_tick;
|
||||
static void setup_timers(void)
|
||||
{
|
||||
if (!clocks_per_tick) {
|
||||
clocks_per_tick = calibrate_tsc();
|
||||
/* Display the CPU Mhz to easily test if the calibration was bad */
|
||||
printf("CPU %ld Mhz\n", (clocks_per_tick/1000 * TICKS_PER_SEC)/1000);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long currticks(void)
|
||||
{
|
||||
unsigned long clocks_high, clocks_low;
|
||||
unsigned long currticks;
|
||||
/* Read the Time Stamp Counter */
|
||||
rdtsc(clocks_low, clocks_high);
|
||||
|
||||
/* currticks = clocks / clocks_per_tick; */
|
||||
__asm__("divl %1"
|
||||
:"=a" (currticks)
|
||||
:"r" (clocks_per_tick), "0" (clocks_low), "d" (clocks_high));
|
||||
|
||||
|
||||
return currticks;
|
||||
}
|
||||
|
||||
static unsigned long long timer_timeout;
|
||||
static int __timer_running(void)
|
||||
{
|
||||
unsigned long long now;
|
||||
rdtscll(now);
|
||||
return now < timer_timeout;
|
||||
}
|
||||
|
||||
void udelay(unsigned int usecs)
|
||||
{
|
||||
unsigned long long now;
|
||||
rdtscll(now);
|
||||
timer_timeout = now + usecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000));
|
||||
while(__timer_running());
|
||||
}
|
||||
void ndelay(unsigned int nsecs)
|
||||
{
|
||||
unsigned long long now;
|
||||
rdtscll(now);
|
||||
timer_timeout = now + nsecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000*1000));
|
||||
while(__timer_running());
|
||||
}
|
||||
|
||||
void load_timer2(unsigned int timer2_ticks)
|
||||
{
|
||||
unsigned long long now;
|
||||
unsigned long clocks;
|
||||
rdtscll(now);
|
||||
clocks = timer2_ticks * ((clocks_per_tick * TICKS_PER_SEC)/CLOCK_TICK_RATE);
|
||||
timer_timeout = now + clocks;
|
||||
}
|
||||
|
||||
int timer2_running(void)
|
||||
{
|
||||
return __timer_running();
|
||||
}
|
||||
|
||||
#endif /* RTC_CURRTICKS */
|
||||
|
||||
struct init_fn timer_init_fn __init_fn ( INIT_NORMAL ) = {
|
||||
.initialise = setup_timers,
|
||||
};
|
||||
|
||||
12
src/arch/i386/core/nap.c
Normal file
12
src/arch/i386/core/nap.c
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
#include <realmode.h>
|
||||
#include <bios.h>
|
||||
|
||||
/**************************************************************************
|
||||
* Save power by halting the CPU until the next interrupt
|
||||
**************************************************************************/
|
||||
void cpu_nap ( void ) {
|
||||
__asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
|
||||
"hlt\n\t"
|
||||
"cli\n\t" ) : : );
|
||||
}
|
||||
Reference in New Issue
Block a user