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