| 1 | /* $NetBSD: clock_subr.h,v 1.25 2014/11/20 16:26:34 christos Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1996 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 | |
| 37 | /* |
| 38 | * "POSIX time" to/from "YY/MM/DD/hh/mm/ss" |
| 39 | */ |
| 40 | struct clock_ymdhms { |
| 41 | uint64_t dt_year; |
| 42 | uint8_t dt_mon; |
| 43 | uint8_t dt_day; |
| 44 | uint8_t dt_wday; /* Day of week */ |
| 45 | uint8_t dt_hour; |
| 46 | uint8_t dt_min; |
| 47 | uint8_t dt_sec; |
| 48 | }; |
| 49 | |
| 50 | time_t clock_ymdhms_to_secs(struct clock_ymdhms *); |
| 51 | int clock_secs_to_ymdhms(time_t, struct clock_ymdhms *); |
| 52 | |
| 53 | /* |
| 54 | * BCD to binary and binary to BCD. |
| 55 | */ |
| 56 | static inline unsigned int |
| 57 | bcdtobin(unsigned int bcd) |
| 58 | { |
| 59 | return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f); |
| 60 | } |
| 61 | |
| 62 | static inline unsigned int |
| 63 | bintobcd(unsigned int bin) |
| 64 | { |
| 65 | return (((bin / 10) << 4) & 0xf0) | (bin % 10); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Interface to time-of-day clock devices. |
| 70 | * |
| 71 | * todr_gettime: convert time-of-day clock into a `struct timeval' |
| 72 | * todr_settime: set time-of-day clock from a `struct timeval' |
| 73 | * |
| 74 | * (this is probably not so useful:) |
| 75 | * todr_setwen: provide a machine-dependent TOD clock write-enable callback |
| 76 | * function which takes one boolean argument: |
| 77 | * 1 to enable writes; 0 to disable writes. |
| 78 | */ |
| 79 | struct timeval; |
| 80 | struct todr_chip_handle { |
| 81 | void *cookie; /* Device specific data */ |
| 82 | void *bus_cookie; /* Bus specific data */ |
| 83 | time_t base_time; /* Base time (e.g. rootfs time) */ |
| 84 | |
| 85 | int (*todr_gettime)(struct todr_chip_handle *, struct timeval *); |
| 86 | int (*todr_settime)(struct todr_chip_handle *, struct timeval *); |
| 87 | int (*todr_gettime_ymdhms)(struct todr_chip_handle *, |
| 88 | struct clock_ymdhms *); |
| 89 | int (*todr_settime_ymdhms)(struct todr_chip_handle *, |
| 90 | struct clock_ymdhms *); |
| 91 | int (*todr_setwen)(struct todr_chip_handle *, int); |
| 92 | |
| 93 | }; |
| 94 | typedef struct todr_chip_handle *todr_chip_handle_t; |
| 95 | |
| 96 | #define todr_wenable(ct, v) if ((ct)->todr_setwen) \ |
| 97 | ((*(ct)->todr_setwen)(ct, v)) |
| 98 | |
| 99 | /* |
| 100 | * Probably these should evolve into internal routines in kern_todr.c. |
| 101 | */ |
| 102 | extern int todr_gettime(todr_chip_handle_t, struct timeval *); |
| 103 | extern int todr_settime(todr_chip_handle_t, struct timeval *); |
| 104 | |
| 105 | /* |
| 106 | * Machine-dependent function that machine-independent RTC drivers can |
| 107 | * use to register their todr_chip_handle_t with inittodr()/resettodr(). |
| 108 | */ |
| 109 | void todr_attach(todr_chip_handle_t); |
| 110 | |
| 111 | #endif /* _DEV_CLOCK_SUBR_H_ */ |
| 112 | |