1*27322462Sthorpej /* $NetBSD: clock_subr.h,v 1.30 2020/01/01 23:07:38 thorpej Exp $ */
2d8b59a12Sgwr
3d8b59a12Sgwr /*-
4a1e56ffaSthorpej * Copyright (c) 1996, 2020 The NetBSD Foundation, Inc.
5d8b59a12Sgwr * All rights reserved.
6d8b59a12Sgwr *
7d8b59a12Sgwr * This code is derived from software contributed to The NetBSD Foundation
8d8b59a12Sgwr * by Gordon W. Ross
9d8b59a12Sgwr *
10d8b59a12Sgwr * Redistribution and use in source and binary forms, with or without
11d8b59a12Sgwr * modification, are permitted provided that the following conditions
12d8b59a12Sgwr * are met:
13d8b59a12Sgwr * 1. Redistributions of source code must retain the above copyright
14d8b59a12Sgwr * notice, this list of conditions and the following disclaimer.
15d8b59a12Sgwr * 2. Redistributions in binary form must reproduce the above copyright
16d8b59a12Sgwr * notice, this list of conditions and the following disclaimer in the
17d8b59a12Sgwr * documentation and/or other materials provided with the distribution.
18d8b59a12Sgwr *
19d8b59a12Sgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d8b59a12Sgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d8b59a12Sgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d8b59a12Sgwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d8b59a12Sgwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d8b59a12Sgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d8b59a12Sgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d8b59a12Sgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d8b59a12Sgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d8b59a12Sgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d8b59a12Sgwr * POSSIBILITY OF SUCH DAMAGE.
30d8b59a12Sgwr */
31d8b59a12Sgwr
325d09a845Smatt #ifndef _DEV_CLOCK_SUBR_H_
335d09a845Smatt #define _DEV_CLOCK_SUBR_H_
345d09a845Smatt
35a95736d4Schristos #include <sys/clock.h>
36*27322462Sthorpej #include <sys/stdbool.h>
37a95736d4Schristos
38d8b59a12Sgwr /*
39d8b59a12Sgwr * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
40d8b59a12Sgwr */
41d8b59a12Sgwr struct clock_ymdhms {
425621a8b9Smartin uint64_t dt_year;
43a95736d4Schristos uint8_t dt_mon;
44a95736d4Schristos uint8_t dt_day;
45a95736d4Schristos uint8_t dt_wday; /* Day of week */
46a95736d4Schristos uint8_t dt_hour;
47a95736d4Schristos uint8_t dt_min;
48a95736d4Schristos uint8_t dt_sec;
49d8b59a12Sgwr };
50d8b59a12Sgwr
5118db93c7Sperry time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
525621a8b9Smartin int clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
53d8b59a12Sgwr
54d8b59a12Sgwr /*
5531930d4dSkleink * BCD to binary and binary to BCD.
56d8b59a12Sgwr */
5787fd18f8Schristos static __inline unsigned int
bcdtobin(unsigned int bcd)580600c9daSchristos bcdtobin(unsigned int bcd)
590600c9daSchristos {
600600c9daSchristos return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f);
610600c9daSchristos }
620600c9daSchristos
6387fd18f8Schristos static __inline unsigned int
bintobcd(unsigned int bin)640600c9daSchristos bintobcd(unsigned int bin)
650600c9daSchristos {
660600c9daSchristos return (((bin / 10) << 4) & 0xf0) | (bin % 10);
670600c9daSchristos }
68d8b59a12Sgwr
69969b4bc5Spk /*
70969b4bc5Spk * Interface to time-of-day clock devices.
71969b4bc5Spk *
72969b4bc5Spk * todr_gettime: convert time-of-day clock into a `struct timeval'
73969b4bc5Spk * todr_settime: set time-of-day clock from a `struct timeval'
74969b4bc5Spk *
75969b4bc5Spk * (this is probably not so useful:)
76969b4bc5Spk * todr_setwen: provide a machine-dependent TOD clock write-enable callback
77969b4bc5Spk * function which takes one boolean argument:
78969b4bc5Spk * 1 to enable writes; 0 to disable writes.
79969b4bc5Spk */
80c68b2c78Schristos struct timeval;
81969b4bc5Spk struct todr_chip_handle {
82969b4bc5Spk void *cookie; /* Device specific data */
830d01e040Seeh void *bus_cookie; /* Bus specific data */
8406522e0fSgdamore time_t base_time; /* Base time (e.g. rootfs time) */
85969b4bc5Spk
86428585a7Stsutsui int (*todr_gettime)(struct todr_chip_handle *, struct timeval *);
87428585a7Stsutsui int (*todr_settime)(struct todr_chip_handle *, struct timeval *);
88942bfe3eSgdamore int (*todr_gettime_ymdhms)(struct todr_chip_handle *,
89942bfe3eSgdamore struct clock_ymdhms *);
90942bfe3eSgdamore int (*todr_settime_ymdhms)(struct todr_chip_handle *,
91942bfe3eSgdamore struct clock_ymdhms *);
920d01e040Seeh int (*todr_setwen)(struct todr_chip_handle *, int);
9306522e0fSgdamore
94969b4bc5Spk };
95969b4bc5Spk typedef struct todr_chip_handle *todr_chip_handle_t;
96969b4bc5Spk
97a1e56ffaSthorpej void todr_init(void);
9896ea5b9aSthorpej void todr_attach(todr_chip_handle_t);
99a1e56ffaSthorpej void todr_lock(void);
100a1e56ffaSthorpej void todr_unlock(void);
101a1e56ffaSthorpej bool todr_lock_owned(void);
102a1e56ffaSthorpej
103a1e56ffaSthorpej void todr_set_systime(time_t base);
104a1e56ffaSthorpej void todr_save_systime(void);
10591b8f572Sthorpej
1065d09a845Smatt #endif /* _DEV_CLOCK_SUBR_H_ */
107