1 /* $OpenBSD: getsecs.c,v 1.5 2023/02/23 13:28:38 aoyama Exp $ */ 2 /* $NetBSD: getsecs.c,v 1.1 2013/01/13 14:10:55 tsutsui Exp $ */ 3 4 /*- 5 * Copyright (c) 2004 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by UCHIYAMA Yasushi. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <luna88k/stand/boot/samachdep.h> 34 #include <machine/board.h> 35 #include <luna88k/dev/timekeeper.h> 36 37 #define _DS_GET(off, data) \ 38 do { *chiptime = (off); (data) = (*chipdata); } while (0) 39 #define _DS_SET(off, data) \ 40 do { *chiptime = (off); *chipdata = (uint8_t)(data); } while (0) 41 42 /* 43 * Convert a single byte between (unsigned) packed bcd and binary. 44 * Public domain. 45 */ 46 unsigned int 47 bcdtobin(unsigned int bcd) 48 { 49 50 return (((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f)); 51 } 52 53 typedef struct { 54 uint Year; 55 uint Month; 56 uint Day; 57 uint Hour; 58 uint Minute; 59 uint Second; 60 } rtc_time; 61 62 #define MK_YEAR0 1970 /* year offset of MK */ 63 #define DS_YEAR0 1990 /* year offset of DS */ 64 65 static void 66 mk_gettime(rtc_time *t) { 67 volatile uint32_t *mclock = 68 (volatile uint32_t *)(NVRAM_ADDR + MK_NVRAM_SPACE); 69 mclock[MK_CSR] |= MK_CSR_READ << 24; 70 t->Second = bcdtobin(mclock[MK_SEC] >> 24); 71 t->Minute = bcdtobin(mclock[MK_MIN] >> 24); 72 t->Hour = bcdtobin(mclock[MK_HOUR] >> 24); 73 t->Day = bcdtobin(mclock[MK_DOM] >> 24); 74 t->Month = bcdtobin(mclock[MK_MONTH] >> 24); 75 t->Year = bcdtobin(mclock[MK_YEAR] >> 24); 76 mclock[MK_CSR] &= ~(MK_CSR_READ << 24); 77 78 /* UniOS-Mach doesn't set the correct BCD year after Y2K */ 79 if (t->Year > 100) t->Year -= (MK_YEAR0 % 100); 80 81 t->Year += MK_YEAR0; 82 83 return; 84 } 85 86 static void 87 ds_gettime(rtc_time *t) { 88 volatile uint8_t *chiptime = (volatile uint8_t *)NVRAM_ADDR; 89 volatile uint8_t *chipdata = chiptime + 1; 90 91 uint8_t c; 92 93 /* specify 24hr and BCD mode */ 94 _DS_GET(DS_REGB, c); 95 c |= DS_REGB_24HR; 96 c &= ~DS_REGB_BINARY; 97 _DS_SET(DS_REGB, c); 98 99 /* update in progress; spin loop */ 100 for (;;) { 101 *chiptime = DS_REGA; 102 if ((*chipdata & DS_REGA_UIP) == 0) 103 break; 104 } 105 106 *chiptime = DS_SEC; 107 t->Second = bcdtobin(*chipdata); 108 *chiptime = DS_MIN; 109 t->Minute = bcdtobin(*chipdata); 110 *chiptime = DS_HOUR; 111 t->Hour = bcdtobin(*chipdata); 112 *chiptime = DS_DOM; 113 t->Day = bcdtobin(*chipdata); 114 *chiptime = DS_MONTH; 115 t->Month = bcdtobin(*chipdata); 116 *chiptime = DS_YEAR; 117 t->Year = bcdtobin(*chipdata); 118 119 /* UniOS-Mach doesn't set the correct BCD year after Y2K */ 120 if (t->Year > 100) t->Year -= (DS_YEAR0 % 100); 121 122 t->Year += DS_YEAR0; 123 124 return; 125 } 126 127 time_t 128 getsecs(void) 129 { 130 rtc_time t; 131 time_t r = 0; 132 int y = 0; 133 const int daytab[][14] = { 134 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }, 135 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 136 }; 137 #define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0)) 138 139 if (machtype == LUNA_88K) { 140 mk_gettime(&t); 141 } else { 142 ds_gettime(&t); 143 } 144 145 /* Calc days from UNIX epoch */ 146 r = (t.Year - 1970) * 365; 147 for (y = 1970; y < t.Year; y++) { 148 if (isleap(y)) 149 r++; 150 } 151 r += daytab[isleap(t.Year)? 1 : 0][t.Month] + t.Day; 152 153 /* Calc secs */ 154 r *= 60 * 60 * 24; 155 r += ((t.Hour * 60) + t.Minute) * 60 + t.Second; 156 157 return (r); 158 } 159