xref: /f-stack/freebsd/sys/clock.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * This code is derived from software contributed to The NetBSD Foundation
8a9643ea8Slogwang  * by Gordon W. Ross
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a9643ea8Slogwang  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a9643ea8Slogwang  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a9643ea8Slogwang  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a9643ea8Slogwang  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a9643ea8Slogwang  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a9643ea8Slogwang  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a9643ea8Slogwang  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a9643ea8Slogwang  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a9643ea8Slogwang  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a9643ea8Slogwang  * POSSIBILITY OF SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  *	$NetBSD: clock_subr.h,v 1.7 2000/10/03 13:41:07 tsutsui Exp $
32a9643ea8Slogwang  *
33a9643ea8Slogwang  *
34a9643ea8Slogwang  * This file is the central clearing-house for calendrical issues.
35a9643ea8Slogwang  *
36a9643ea8Slogwang  * In general the kernel does not know about minutes, hours, days, timezones,
37a9643ea8Slogwang  * daylight savings time, leap-years and such.  All that is theoretically a
38a9643ea8Slogwang  * matter for userland only.
39a9643ea8Slogwang  *
40a9643ea8Slogwang  * Parts of kernel code does however care: badly designed filesystems store
41a9643ea8Slogwang  * timestamps in local time and RTC chips sometimes track time in a local
42a9643ea8Slogwang  * timezone instead of UTC and so on.
43a9643ea8Slogwang  *
44a9643ea8Slogwang  * All that code should go here for service.
45a9643ea8Slogwang  *
46a9643ea8Slogwang  * $FreeBSD$
47a9643ea8Slogwang  */
48a9643ea8Slogwang 
49a9643ea8Slogwang #ifndef _SYS_CLOCK_H_
50a9643ea8Slogwang #define _SYS_CLOCK_H_
51a9643ea8Slogwang 
52a9643ea8Slogwang #ifdef _KERNEL		/* No user serviceable parts */
53a9643ea8Slogwang 
54a9643ea8Slogwang int utc_offset(void);
55a9643ea8Slogwang 
56a9643ea8Slogwang /*
57*22ce4affSfengbojiang  * Structure to hold the values typically reported by time-of-day clocks,
58*22ce4affSfengbojiang  * expressed as binary integers (see below for a BCD version).  This can be
59*22ce4affSfengbojiang  * passed to the conversion functions to be converted to/from a struct timespec.
60*22ce4affSfengbojiang  *
61*22ce4affSfengbojiang  * On input, the year is interpreted as follows:
62*22ce4affSfengbojiang  *       0 -   69 = 2000 - 2069
63*22ce4affSfengbojiang  *      70 -   99 = 1970 - 1999
64*22ce4affSfengbojiang  *     100 -  199 = 2000 - 2099 (Supports hardware "century bit".)
65*22ce4affSfengbojiang  *     200 - 1969 = Invalid.
66*22ce4affSfengbojiang  *    1970 - 9999 = Full 4-digit century+year.
67*22ce4affSfengbojiang  *
68*22ce4affSfengbojiang  * The dow field is ignored (not even validated) on input, but is always
69*22ce4affSfengbojiang  * populated with day-of-week on output.
70*22ce4affSfengbojiang  *
71*22ce4affSfengbojiang  * clock_ct_to_ts() returns EINVAL if any values are out of range.  The year
72*22ce4affSfengbojiang  * field will always be 4-digit on output.
73a9643ea8Slogwang  */
74a9643ea8Slogwang struct clocktime {
75a9643ea8Slogwang 	int	year;			/* year (4 digit year) */
76a9643ea8Slogwang 	int	mon;			/* month (1 - 12) */
77a9643ea8Slogwang 	int	day;			/* day (1 - 31) */
78a9643ea8Slogwang 	int	hour;			/* hour (0 - 23) */
79a9643ea8Slogwang 	int	min;			/* minute (0 - 59) */
80a9643ea8Slogwang 	int	sec;			/* second (0 - 59) */
81a9643ea8Slogwang 	int	dow;			/* day of week (0 - 6; 0 = Sunday) */
82a9643ea8Slogwang 	long	nsec;			/* nano seconds */
83a9643ea8Slogwang };
84a9643ea8Slogwang 
85*22ce4affSfengbojiang int clock_ct_to_ts(const struct clocktime *, struct timespec *);
86*22ce4affSfengbojiang void clock_ts_to_ct(const struct timespec *, struct clocktime *);
87*22ce4affSfengbojiang 
88*22ce4affSfengbojiang /*
89*22ce4affSfengbojiang  * Structure to hold the values typically reported by time-of-day clocks,
90*22ce4affSfengbojiang  * expressed as BCD.  This can be passed to the conversion functions to be
91*22ce4affSfengbojiang  * converted to/from a struct timespec.
92*22ce4affSfengbojiang  *
93*22ce4affSfengbojiang  * The clock_bcd_to_ts() function interprets the values in the year through sec
94*22ce4affSfengbojiang  * fields as BCD numbers, and returns EINVAL if any BCD values are out of range.
95*22ce4affSfengbojiang  * After conversion to binary, the values are passed to clock_ct_to_ts() and
96*22ce4affSfengbojiang  * undergo further validation as described above.  Year may be 2 or 4-digit BCD,
97*22ce4affSfengbojiang  * interpreted as described above.  The nsec field is binary.  If the ampm arg
98*22ce4affSfengbojiang  * is true, the incoming hour and ispm values are interpreted as 12-hour am/pm
99*22ce4affSfengbojiang  * representation of the hour, otherwise hour is interpreted as 24-hour and ispm
100*22ce4affSfengbojiang  * is ignored.
101*22ce4affSfengbojiang  *
102*22ce4affSfengbojiang  * The clock_ts_to_bcd() function converts the timespec to BCD values stored
103*22ce4affSfengbojiang  * into year through sec.  The value in year will be 4-digit BCD (e.g.,
104*22ce4affSfengbojiang  * 0x2017). The mon through sec values will be 2-digit BCD.  The nsec field will
105*22ce4affSfengbojiang  * be binary, and the range of dow makes its binary and BCD values identical.
106*22ce4affSfengbojiang  * If the ampm arg is true, the hour and ispm fields are set to the 12-hour
107*22ce4affSfengbojiang  * time plus a pm flag, otherwise the hour is set to 24-hour time and ispm is
108*22ce4affSfengbojiang  * set to false.
109*22ce4affSfengbojiang  */
110*22ce4affSfengbojiang struct bcd_clocktime {
111*22ce4affSfengbojiang 	uint16_t year;			/* year (2 or 4 digit year) */
112*22ce4affSfengbojiang 	uint8_t  mon;			/* month (1 - 12) */
113*22ce4affSfengbojiang 	uint8_t  day;			/* day (1 - 31) */
114*22ce4affSfengbojiang 	uint8_t  hour;			/* hour (0 - 23 or 1 - 12) */
115*22ce4affSfengbojiang 	uint8_t  min;			/* minute (0 - 59) */
116*22ce4affSfengbojiang 	uint8_t  sec;			/* second (0 - 59) */
117*22ce4affSfengbojiang 	uint8_t  dow;			/* day of week (0 - 6; 0 = Sunday) */
118*22ce4affSfengbojiang 	long     nsec;			/* nanoseconds */
119*22ce4affSfengbojiang 	bool     ispm;			/* true if hour represents pm time */
120*22ce4affSfengbojiang };
121*22ce4affSfengbojiang 
122*22ce4affSfengbojiang int clock_bcd_to_ts(const struct bcd_clocktime *, struct timespec *, bool ampm);
123*22ce4affSfengbojiang void clock_ts_to_bcd(const struct timespec *, struct bcd_clocktime *, bool ampm);
124*22ce4affSfengbojiang 
125*22ce4affSfengbojiang /*
126*22ce4affSfengbojiang  * Time-of-day clock functions and flags.  These functions might sleep.
127*22ce4affSfengbojiang  *
128*22ce4affSfengbojiang  * clock_register and clock_unregister() do what they say.  Upon return from
129*22ce4affSfengbojiang  * unregister, the clock's methods are not running and will not be called again.
130*22ce4affSfengbojiang  *
131*22ce4affSfengbojiang  * clock_schedule() requests that a registered clock's clock_settime() calls
132*22ce4affSfengbojiang  * happen at the given offset into the second.  The default is 0, meaning no
133*22ce4affSfengbojiang  * specific scheduling.  To schedule the call as soon after top-of-second as
134*22ce4affSfengbojiang  * possible, specify 1.  Each clock has its own schedule, but taskqueue_thread
135*22ce4affSfengbojiang  * is shared by many tasks; the timing of the call is not guaranteed.
136*22ce4affSfengbojiang  *
137*22ce4affSfengbojiang  * Flags:
138*22ce4affSfengbojiang  *
139*22ce4affSfengbojiang  *  CLOCKF_SETTIME_NO_TS
140*22ce4affSfengbojiang  *    Do not pass a timespec to clock_settime(), the driver obtains its own time
141*22ce4affSfengbojiang  *    and applies its own adjustments (this flag implies CLOCKF_SETTIME_NO_ADJ).
142*22ce4affSfengbojiang  *
143*22ce4affSfengbojiang  *  CLOCKF_SETTIME_NO_ADJ
144*22ce4affSfengbojiang  *    Do not apply utc offset and resolution/accuracy adjustments to the value
145*22ce4affSfengbojiang  *    passed to clock_settime(), the driver applies them itself.
146*22ce4affSfengbojiang  *
147*22ce4affSfengbojiang  *  CLOCKF_GETTIME_NO_ADJ
148*22ce4affSfengbojiang  *    Do not apply utc offset and resolution/accuracy adjustments to the value
149*22ce4affSfengbojiang  *    returned from clock_gettime(), the driver has already applied them.
150*22ce4affSfengbojiang  */
151*22ce4affSfengbojiang 
152*22ce4affSfengbojiang #define	CLOCKF_SETTIME_NO_TS	0x00000001
153*22ce4affSfengbojiang #define	CLOCKF_SETTIME_NO_ADJ	0x00000002
154*22ce4affSfengbojiang #define	CLOCKF_GETTIME_NO_ADJ	0x00000004
155*22ce4affSfengbojiang 
156*22ce4affSfengbojiang void clock_register(device_t _clockdev, long _resolution_us);
157*22ce4affSfengbojiang void clock_register_flags(device_t _clockdev, long _resolution_us, int _flags);
158*22ce4affSfengbojiang void clock_schedule(device_t clockdev, u_int _offsetns);
159*22ce4affSfengbojiang void clock_unregister(device_t _clockdev);
160a9643ea8Slogwang 
161a9643ea8Slogwang /*
162a9643ea8Slogwang  * BCD to decimal and decimal to BCD.
163a9643ea8Slogwang  */
164a9643ea8Slogwang #define	FROMBCD(x)	bcd2bin(x)
165a9643ea8Slogwang #define	TOBCD(x)	bin2bcd(x)
166a9643ea8Slogwang 
167a9643ea8Slogwang /* Some handy constants. */
168a9643ea8Slogwang #define SECDAY		(24 * 60 * 60)
169a9643ea8Slogwang #define SECYR		(SECDAY * 365)
170a9643ea8Slogwang 
171a9643ea8Slogwang /* Traditional POSIX base year */
172a9643ea8Slogwang #define	POSIX_BASE_YEAR	1970
173a9643ea8Slogwang 
174*22ce4affSfengbojiang void timespec2fattime(const struct timespec *tsp, int utc, u_int16_t *ddp,
175*22ce4affSfengbojiang     u_int16_t *dtp, u_int8_t *dhp);
176*22ce4affSfengbojiang void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc,
177*22ce4affSfengbojiang     struct timespec *tsp);
178*22ce4affSfengbojiang 
179*22ce4affSfengbojiang /*
180*22ce4affSfengbojiang  * Print a [bcd_]clocktime or timespec, optionally with fractional seconds.  The
181*22ce4affSfengbojiang  * nsdig argument can range from 0-9, and specifies how many decimal digits to
182*22ce4affSfengbojiang  * display for fractional seconds.
183*22ce4affSfengbojiang  */
184*22ce4affSfengbojiang void clock_print_bcd(const struct bcd_clocktime *bct, int nsdig);
185*22ce4affSfengbojiang void clock_print_ct(const struct clocktime *ct, int nsdig);
186*22ce4affSfengbojiang void clock_print_ts(const struct timespec  *ts, int nsdig);
187*22ce4affSfengbojiang 
188*22ce4affSfengbojiang /*
189*22ce4affSfengbojiang  * Debugging helpers for RTC clock drivers.  Print a [bcd_]clocktime or
190*22ce4affSfengbojiang  * timespec, only if rtc clock debugging has been enabled.  The rw argument is
191*22ce4affSfengbojiang  * one of CLOCK_DBG_READ or CLOCK_DBG_WRITE.
192*22ce4affSfengbojiang  */
193*22ce4affSfengbojiang #define	CLOCK_DBG_READ	0x01
194*22ce4affSfengbojiang #define	CLOCK_DBG_WRITE	0x02
195*22ce4affSfengbojiang void clock_dbgprint_bcd(device_t dev, int rw, const struct bcd_clocktime *bct);
196*22ce4affSfengbojiang void clock_dbgprint_ct(device_t dev, int rw, const struct clocktime *ct);
197*22ce4affSfengbojiang void clock_dbgprint_err(device_t dev, int rw, int err);
198*22ce4affSfengbojiang void clock_dbgprint_ts(device_t dev, int rw, const struct timespec *ts);
199a9643ea8Slogwang 
200a9643ea8Slogwang #endif /* _KERNEL */
201a9643ea8Slogwang 
202a9643ea8Slogwang #endif /* !_SYS_CLOCK_H_ */
203