clock.c revision 1.5
11.5Schristos/* $NetBSD: clock.c,v 1.5 2014/11/21 01:18:39 christos Exp $ */ 21.1Stsutsui 31.1Stsutsui/* 41.1Stsutsui * Copyright (c) 2003 Tetsuya Isaki. All rights reserved. 51.1Stsutsui * 61.1Stsutsui * Redistribution and use in source and binary forms, with or without 71.1Stsutsui * modification, are permitted provided that the following conditions 81.1Stsutsui * are met: 91.1Stsutsui * 1. Redistributions of source code must retain the above copyright 101.1Stsutsui * notice, this list of conditions and the following disclaimer. 111.1Stsutsui * 2. Redistributions in binary form must reproduce the above copyright 121.1Stsutsui * notice, this list of conditions and the following disclaimer in the 131.1Stsutsui * documentation and/or other materials provided with the distribution. 141.1Stsutsui * 151.1Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 161.1Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171.1Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181.1Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191.1Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 201.1Stsutsui * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 211.1Stsutsui * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 221.1Stsutsui * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 231.1Stsutsui * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 241.1Stsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 251.1Stsutsui * SUCH DAMAGE. 261.1Stsutsui */ 271.1Stsutsui 281.1Stsutsui#include <sys/types.h> 291.4Schristos#include <sys/clock.h> 301.1Stsutsui#include <lib/libsa/stand.h> 311.1Stsutsui#include <lib/libsa/net.h> 321.1Stsutsui#include "iocs.h" 331.1Stsutsui#include "libx68k.h" 341.1Stsutsui#include "consio.h" /* XXX: for MFP_TIMERC */ 351.1Stsutsui 361.1Stsutsui/* x68k's RTC is defunct 2079, so there is no y2100 problem. */ 371.1Stsutsui 381.1Stsutsuiint rtc_offset; 391.1Stsutsui 401.1Stsutsuiconst int yday[] = { 411.1Stsutsui 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 421.1Stsutsui}; 431.1Stsutsui 441.1Stsutsuisatime_t 451.1Stsutsuigetsecs(void) 461.1Stsutsui{ 471.1Stsutsui int val; 481.1Stsutsui int sec, min, hour, day, mon, year; 491.1Stsutsui int days, y; 501.1Stsutsui 511.1Stsutsui /* Get date & time via IOCS */ 521.1Stsutsui val = IOCS_DATEBIN(IOCS_BINDATEGET()); 531.1Stsutsui year = ((val & 0x0fff0000) >> 16) + 1980; 541.1Stsutsui mon = ((val & 0x0000ff00) >> 8); 551.1Stsutsui day = (val & 0x000000ff); 561.1Stsutsui 571.1Stsutsui val = IOCS_TIMEBIN(IOCS_TIMEGET()); 581.1Stsutsui hour = ((val & 0x00ff0000) >> 16); 591.1Stsutsui min = ((val & 0x0000ff00) >> 8); 601.1Stsutsui sec = (val & 0x000000ff); 611.1Stsutsui 621.1Stsutsui /* simple sanity checks */ 631.1Stsutsui if (mon < 1 || mon > 12 || day < 1 || day > 31) 641.1Stsutsui return 0; 651.1Stsutsui if (hour > 23 || min > 59 || sec > 59) 661.1Stsutsui return 0; 671.1Stsutsui 681.1Stsutsui days = 0; 691.1Stsutsui for (y = 1970; y < year; y++) 701.3Schristos days += days_per_year(y); 711.1Stsutsui days += yday[mon - 1] + day - 1; 721.3Schristos if (is_leap_year(y) && mon > 2) 731.1Stsutsui days++; 741.1Stsutsui 751.1Stsutsui /* now we have days since Jan 1, 1970. the rest is easy... */ 761.5Schristos return (days * SECS_PER_DAY) + (hour * SECS_PER_HOUR) 771.5Schristos + (min * SECS_PER_MINUTE) + sec + (rtc_offset * 60); 781.1Stsutsui} 791.1Stsutsui 801.1Stsutsuivoid 811.1Stsutsuidelay(int us) 821.1Stsutsui{ 831.1Stsutsui int end; 841.1Stsutsui 851.1Stsutsui /* sanity check */ 861.1Stsutsui if (us < 1) 871.1Stsutsui return; 881.1Stsutsui 891.1Stsutsui /* 901.1Stsutsui * assume IPLROM initializes MFP Timer-C as following: 911.1Stsutsui * - free run down count 921.1Stsutsui * - 1/200 presclaer (50us with 4MHz clock) 931.1Stsutsui * 941.1Stsutsui * Note we can't change MFP_TCDR reload value (200) 951.1Stsutsui * because awaitkey_1sec() in consio.c assumes that value. 961.1Stsutsui */ 971.1Stsutsui 981.1Stsutsui /* handle >5ms delays first */ 991.1Stsutsui for (; us > 5000; us -= 5000) { 1001.1Stsutsui MFP_TIMERC = 200; 1011.1Stsutsui while (MFP_TIMERC >= 100) 1021.1Stsutsui continue; 1031.1Stsutsui } 1041.1Stsutsui 1051.1Stsutsui /* count rest fractions */ 1061.1Stsutsui end = 200 - (us / 50); 1071.1Stsutsui MFP_TIMERC = 200; 1081.1Stsutsui while (MFP_TIMERC >= end) 1091.1Stsutsui continue; 1101.1Stsutsui} 111