155342Snyan/*-
255342Snyan * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
355342Snyan * All rights reserved.
453207Snyan *
555342Snyan * Redistribution and use in source and binary forms, with or without
655342Snyan * modification, are permitted provided that the following conditions
755342Snyan * are met:
855342Snyan * 1. Redistributions of source code must retain the above copyright
955342Snyan *    notice, this list of conditions and the following disclaimer.
1055342Snyan * 2. Redistributions in binary form must reproduce the above copyright
1155342Snyan *    notice, this list of conditions and the following disclaimer in the
1255342Snyan *    documentation and/or other materials provided with the distribution.
1355342Snyan *
1455342Snyan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1555342Snyan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1655342Snyan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1755342Snyan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1855342Snyan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1955342Snyan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2055342Snyan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2155342Snyan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2255342Snyan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2355342Snyan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2455342Snyan * SUCH DAMAGE.
2543561Skato */
2643561Skato
27119880Sobrien#include <sys/cdefs.h>
28119880Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/boot/pc98/libpc98/time.c 190126 2009-03-20 05:21:29Z nyan $");
29119880Sobrien
3043561Skato#include <stand.h>
3143561Skato#include <btxv86.h>
3243561Skato#include <machine/cpufunc.h>
3368358Snyan#include "bootstrap.h"
3468358Snyan#include "libi386.h"
3543561Skato
36190126Snyanstatic int	bios_seconds(void);
37190126Snyan
3843561Skato/*
39190126Snyan * Return the BIOS time-of-day value.
4043561Skato *
4143561Skato * XXX uses undocumented BCD support from libstand.
4243561Skato */
43190126Snyanstatic int
44190126Snyanbios_seconds(void)
4543561Skato{
4668358Snyan    int			hr, minute, sec;
4753207Snyan    unsigned char	bios_time[6];
48190126Snyan
4943561Skato    v86.ctl = 0;
50190126Snyan    v86.addr = 0x1c;		/* int 0x1c, function 0 */
5143561Skato    v86.eax = 0x0000;
5243561Skato    v86.es  = VTOPSEG(bios_time);
5343561Skato    v86.ebx = VTOPOFF(bios_time);
5443561Skato    v86int();
5543561Skato
5643561Skato    hr = bcd2bin(bios_time[3]);
5768358Snyan    minute = bcd2bin(bios_time[4]);
5843561Skato    sec = bcd2bin(bios_time[5]);
5943561Skato
60190126Snyan    return (hr * 3600 + minute * 60 + sec);
61190126Snyan}
62190126Snyan
63190126Snyan/*
64190126Snyan * Return the time in seconds since the beginning of the day.
65190126Snyan */
66190126Snyantime_t
67190126Snyantime(time_t *t)
68190126Snyan{
69190126Snyan    static time_t lasttime;
70190126Snyan    time_t now;
71190126Snyan
72190126Snyan    now = bios_seconds();
73190126Snyan
7443561Skato    if (now < lasttime)
7543561Skato	now += 24 * 3600;
7643561Skato    lasttime = now;
7743561Skato
7843561Skato    if (t != NULL)
7943561Skato	*t = now;
8043561Skato    return(now);
8143561Skato}
8243561Skato
8343561Skato/*
8443561Skato * Use the BIOS Wait function to pause for (period) microseconds.
8543561Skato *
8643561Skato * Resolution of this function is variable, but typically around
8743561Skato * 1ms.
8843561Skato */
8943561Skatovoid
9043561Skatodelay(int period)
9143561Skato{
9243561Skato    int i;
93146011Snyan
9443561Skato    period = (period + 500) / 1000;
9543561Skato    for( ; period != 0 ; period--)
9643561Skato	for(i=800;i != 0; i--)
9743561Skato	    outb(0x5f,0);       /* wait 600ns */
9843561Skato}
99