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