xref: /netbsd/sys/dev/clock_subr.h (revision ea4fcec5)
1*ea4fcec5Sthorpej /*	$NetBSD: clock_subr.h,v 1.30 2020/01/01 23:07:38 thorpej Exp $	*/
2d8b59a12Sgwr 
3d8b59a12Sgwr /*-
40d5cc02cSthorpej  * 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 
359b0ac32cSchristos #include <sys/clock.h>
36*ea4fcec5Sthorpej #include <sys/stdbool.h>
379b0ac32cSchristos 
38d8b59a12Sgwr /*
39d8b59a12Sgwr  * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
40d8b59a12Sgwr  */
41d8b59a12Sgwr struct clock_ymdhms {
42ef7a6950Smartin 	uint64_t dt_year;
439b0ac32cSchristos 	uint8_t dt_mon;
449b0ac32cSchristos 	uint8_t dt_day;
459b0ac32cSchristos 	uint8_t dt_wday;	/* Day of week */
469b0ac32cSchristos 	uint8_t dt_hour;
479b0ac32cSchristos 	uint8_t dt_min;
489b0ac32cSchristos 	uint8_t dt_sec;
49d8b59a12Sgwr };
50d8b59a12Sgwr 
5118db93c7Sperry time_t	clock_ymdhms_to_secs(struct clock_ymdhms *);
52ef7a6950Smartin int	clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
53d8b59a12Sgwr 
54d8b59a12Sgwr /*
5531930d4dSkleink  * BCD to binary and binary to BCD.
56d8b59a12Sgwr  */
5730296cb2Schristos static __inline unsigned int
bcdtobin(unsigned int bcd)58494cf20cSchristos bcdtobin(unsigned int bcd)
59494cf20cSchristos {
60494cf20cSchristos         return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f);
61494cf20cSchristos }
62494cf20cSchristos 
6330296cb2Schristos static __inline unsigned int
bintobcd(unsigned int bin)64494cf20cSchristos bintobcd(unsigned int bin)
65494cf20cSchristos {
66494cf20cSchristos 	return (((bin / 10) << 4) & 0xf0) | (bin % 10);
67494cf20cSchristos }
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  */
8066303a5bSchristos 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 
970d5cc02cSthorpej void	todr_init(void);
9896ea5b9aSthorpej void	todr_attach(todr_chip_handle_t);
990d5cc02cSthorpej void	todr_lock(void);
1000d5cc02cSthorpej void	todr_unlock(void);
1010d5cc02cSthorpej bool	todr_lock_owned(void);
1020d5cc02cSthorpej 
1030d5cc02cSthorpej void	todr_set_systime(time_t base);
1040d5cc02cSthorpej void	todr_save_systime(void);
10591b8f572Sthorpej 
1065d09a845Smatt #endif /* _DEV_CLOCK_SUBR_H_ */
107