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