1/* $NetBSD: clock.c,v 1.6 2024/01/07 07:58:34 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 <sys/clock.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 38int rtc_offset; 39 40const int yday[] = { 41 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 42}; 43 44satime_t 45getsecs(void) 46{ 47 int val; 48 int sec, min, hour, day, mon, year; 49 int days, y; 50 51 /* Get date & time via IOCS */ 52 val = IOCS_DATEBIN(IOCS_BINDATEGET()); 53 year = ((val & 0x0fff0000) >> 16) + 1980; 54 mon = ((val & 0x0000ff00) >> 8); 55 day = (val & 0x000000ff); 56 57 val = IOCS_TIMEBIN(IOCS_TIMEGET()); 58 hour = ((val & 0x00ff0000) >> 16); 59 min = ((val & 0x0000ff00) >> 8); 60 sec = (val & 0x000000ff); 61 62 /* simple sanity checks */ 63 if (mon < 1 || mon > 12 || day < 1 || day > 31) 64 return 0; 65 if (hour > 23 || min > 59 || sec > 59) 66 return 0; 67 68 days = 0; 69 for (y = 1970; y < year; y++) 70 days += days_per_year(y); 71 days += yday[mon - 1] + day - 1; 72 if (is_leap_year(y) && mon > 2) 73 days++; 74 75 /* now we have days since Jan 1, 1970. the rest is easy... */ 76 return (days * SECS_PER_DAY) + (hour * SECS_PER_HOUR) 77 + (min * SECS_PER_MINUTE) + sec + (rtc_offset * 60); 78} 79 80void 81delay(int us) 82{ 83 int end; 84 85 /* sanity check */ 86 if (us < 1) 87 return; 88 89 /* 90 * assume IPLROM initializes MFP Timer-C as following: 91 * - free run down count 92 * - 1/200 presclaer (50us with 4MHz clock) 93 * 94 * Note we can't change MFP_TCDR reload value (200) 95 * because awaitkey_1sec() in consio.c assumes that value. 96 */ 97 98 /* handle >5ms delays first */ 99 for (; us > 5000; us -= 5000) { 100 MFP_TIMERC = 200; 101 while (MFP_TIMERC >= 100) 102 continue; 103 } 104 105 /* count rest fractions */ 106 end = 200 - (us / 50); 107 MFP_TIMERC = 200; 108 while (MFP_TIMERC >= end) 109 continue; 110} 111