1 /* $NetBSD: clock_subr.h,v 1.33 2025/09/09 14:52:10 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _DEV_CLOCK_SUBR_H_ 33 #define _DEV_CLOCK_SUBR_H_ 34 35 #include <sys/clock.h> 36 #include <sys/device_if.h> 37 #include <sys/stdbool.h> 38 39 /* 40 * "POSIX time" to/from "YY/MM/DD/hh/mm/ss" 41 */ 42 struct clock_ymdhms { 43 uint64_t dt_year; 44 uint8_t dt_mon; 45 uint8_t dt_day; 46 uint8_t dt_wday; /* Day of week */ 47 uint8_t dt_hour; 48 uint8_t dt_min; 49 uint8_t dt_sec; 50 }; 51 52 time_t clock_ymdhms_to_secs(struct clock_ymdhms *); 53 int clock_secs_to_ymdhms(time_t, struct clock_ymdhms *); 54 55 /* 56 * BCD to binary and binary to BCD. 57 */ 58 static __inline unsigned int 59 bcdtobin(unsigned int bcd) 60 { 61 return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f); 62 } 63 64 static __inline unsigned int 65 bintobcd(unsigned int bin) 66 { 67 return (((bin / 10) << 4) & 0xf0) | (bin % 10); 68 } 69 70 /* 71 * Interface to time-of-day clock devices. 72 * 73 * todr_gettime: convert time-of-day clock into a `struct timeval' 74 * todr_settime: set time-of-day clock from a `struct timeval' 75 * 76 * (this is probably not so useful:) 77 * todr_setwen: provide a machine-dependent TOD clock write-enable callback 78 * function which takes one boolean argument: 79 * 1 to enable writes; 0 to disable writes. 80 */ 81 struct timeval; 82 struct todr_chip_handle { 83 device_t todr_dev; /* Device backing the TOD clock */ 84 void *todr_devaux; /* Auxillary data (maybe) needed by device */ 85 time_t todr_base_time; /* Base time (e.g. rootfs time) */ 86 87 int (*todr_gettime)(struct todr_chip_handle *, struct timeval *); 88 int (*todr_settime)(struct todr_chip_handle *, struct timeval *); 89 int (*todr_gettime_ymdhms)(struct todr_chip_handle *, 90 struct clock_ymdhms *); 91 int (*todr_settime_ymdhms)(struct todr_chip_handle *, 92 struct clock_ymdhms *); 93 int (*todr_setwen)(struct todr_chip_handle *, int); 94 95 }; 96 typedef struct todr_chip_handle *todr_chip_handle_t; 97 98 void todr_init(void); 99 void todr_attach(todr_chip_handle_t); 100 void todr_lock(void); 101 void todr_unlock(void); 102 bool todr_lock_owned(void); 103 104 void todr_set_systime(time_t base); 105 void todr_save_systime(void); 106 107 #endif /* _DEV_CLOCK_SUBR_H_ */ 108