iPXE
bios_timer.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * BIOS timer
00029  *
00030  */
00031 
00032 #include <ipxe/timer.h>
00033 #include <realmode.h>
00034 #include <bios.h>
00035 #include <ipxe/pit8254.h>
00036 
00037 /** Number of ticks per day
00038  *
00039  * This seems to be the normative value, as used by e.g. SeaBIOS to
00040  * decide when to set the midnight rollover flag.
00041  */
00042 #define BIOS_TICKS_PER_DAY 0x1800b0
00043 
00044 /** Number of ticks per BIOS tick */
00045 #define TICKS_PER_BIOS_TICK \
00046         ( ( TICKS_PER_SEC * 60 * 60 * 24 ) / BIOS_TICKS_PER_DAY )
00047 
00048 /**
00049  * Get current system time in ticks
00050  *
00051  * @ret ticks           Current time, in ticks
00052  *
00053  * Use direct memory access to BIOS variables, longword 0040:006C
00054  * (ticks today) and byte 0040:0070 (midnight crossover flag) instead
00055  * of calling timeofday BIOS interrupt.
00056  */
00057 static unsigned long bios_currticks ( void ) {
00058         static uint32_t offset;
00059         uint32_t ticks;
00060         uint8_t midnight;
00061 
00062         /* Re-enable interrupts so that the timer interrupt can occur */
00063         __asm__ __volatile__ ( "sti\n\t"
00064                                "nop\n\t"
00065                                "nop\n\t"
00066                                "cli\n\t" );
00067 
00068         /* Read current BIOS time of day */
00069         get_real ( ticks, BDA_SEG, BDA_TICKS );
00070         get_real ( midnight, BDA_SEG, BDA_MIDNIGHT );
00071 
00072         /* Handle midnight rollover */
00073         if ( midnight ) {
00074                 midnight = 0;
00075                 put_real ( midnight, BDA_SEG, BDA_MIDNIGHT );
00076                 offset += BIOS_TICKS_PER_DAY;
00077         }
00078         ticks += offset;
00079 
00080         /* Convert to timer ticks */
00081         return ( ticks * TICKS_PER_BIOS_TICK );
00082 }
00083 
00084 /** BIOS timer */
00085 struct timer bios_timer __timer ( TIMER_NORMAL ) = {
00086         .name = "bios",
00087         .currticks = bios_currticks,
00088         .udelay = pit8254_udelay,
00089 };