1 /* $NetBSD: clock.c,v 1.2 2012/11/01 14:46:26 isaki Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Tetsuya Isaki. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <lib/libsa/stand.h> 30 #include <lib/libsa/net.h> 31 #include "iocs.h" 32 #include "libx68k.h" 33 #include "consio.h" /* XXX: for MFP_TIMERC */ 34 35 /* x68k's RTC is defunct 2079, so there is no y2100 problem. */ 36 #define LEAPYEAR(y) (((y) % 4) == 0) 37 #define SECDAY (24 * 60 * 60) 38 39 int rtc_offset; 40 41 const int yday[] = { 42 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 43 }; 44 45 satime_t 46 getsecs(void) 47 { 48 int val; 49 int sec, min, hour, day, mon, year; 50 int days, y; 51 52 /* Get date & time via IOCS */ 53 val = IOCS_DATEBIN(IOCS_BINDATEGET()); 54 year = ((val & 0x0fff0000) >> 16) + 1980; 55 mon = ((val & 0x0000ff00) >> 8); 56 day = (val & 0x000000ff); 57 58 val = IOCS_TIMEBIN(IOCS_TIMEGET()); 59 hour = ((val & 0x00ff0000) >> 16); 60 min = ((val & 0x0000ff00) >> 8); 61 sec = (val & 0x000000ff); 62 63 /* simple sanity checks */ 64 if (mon < 1 || mon > 12 || day < 1 || day > 31) 65 return 0; 66 if (hour > 23 || min > 59 || sec > 59) 67 return 0; 68 69 days = 0; 70 for (y = 1970; y < year; y++) 71 days += 365 + LEAPYEAR(y); 72 days += yday[mon - 1] + day - 1; 73 if (LEAPYEAR(y) && mon > 2) 74 days++; 75 76 /* now we have days since Jan 1, 1970. the rest is easy... */ 77 return (days * SECDAY) + (hour * 3600) + (min * 60) + sec 78 + (rtc_offset * 60); 79 } 80 81 void 82 delay(int us) 83 { 84 int end; 85 86 /* sanity check */ 87 if (us < 1) 88 return; 89 90 /* 91 * assume IPLROM initializes MFP Timer-C as following: 92 * - free run down count 93 * - 1/200 presclaer (50us with 4MHz clock) 94 * 95 * Note we can't change MFP_TCDR reload value (200) 96 * because awaitkey_1sec() in consio.c assumes that value. 97 */ 98 99 /* handle >5ms delays first */ 100 for (; us > 5000; us -= 5000) { 101 MFP_TIMERC = 200; 102 while (MFP_TIMERC >= 100) 103 continue; 104 } 105 106 /* count rest fractions */ 107 end = 200 - (us / 50); 108 MFP_TIMERC = 200; 109 while (MFP_TIMERC >= end) 110 continue; 111 } 112