1 /* $OpenBSD: getsecs.c,v 1.3 2013/11/12 13:56:23 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 = (u_int8_t)(data); } while (0) 41 42 time_t 43 getsecs(void) 44 { 45 u_int t; 46 47 if (machtype == LUNA_88K) { 48 volatile uint32_t *mclock = 49 (volatile uint32_t *)(NVRAM_ADDR + MK_NVRAM_SPACE); 50 mclock[MK_CSR] |= MK_CSR_READ << 24; 51 t = bcdtobin(mclock[MK_SEC] >> 24); 52 t += bcdtobin(mclock[MK_MIN] >> 24) * 60; 53 t += bcdtobin(mclock[MK_HOUR] >> 24) * 60 * 60; 54 mclock[MK_CSR] &= ~(MK_CSR_READ << 24); 55 } else { 56 volatile uint8_t *chiptime = (volatile uint8_t *)NVRAM_ADDR; 57 volatile u_int8_t *chipdata = chiptime + 1; 58 59 uint8_t c; 60 61 /* specify 24hr and BCD mode */ 62 _DS_GET(DS_REGB, c); 63 c |= DS_REGB_24HR; 64 c &= ~DS_REGB_BINARY; 65 _DS_SET(DS_REGB, c); 66 67 /* update in progress; spin loop */ 68 for (;;) { 69 *chiptime = DS_REGA; 70 if ((*chipdata & DS_REGA_UIP) == 0) 71 break; 72 } 73 74 *chiptime = DS_SEC; 75 t = bcdtobin(*chipdata); 76 *chiptime = DS_MIN; 77 t += bcdtobin(*chipdata) * 60; 78 *chiptime = DS_HOUR; 79 t += bcdtobin(*chipdata) * 60 * 60; 80 } 81 82 return (time_t)t; 83 } 84