Use todr_gettime_ymdhms / todr_settime_ymdhms.diff -r1.9 -r1.10 src/sys/dev/i2c/r2025.c
(thorpej)
--- src/sys/dev/i2c/r2025.c 2020/01/02 17:03:05 1.9
+++ src/sys/dev/i2c/r2025.c 2020/01/02 19:00:34 1.10
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: r2025.c,v 1.9 2020/01/02 17:03:05 thorpej Exp $ */ | 1 | /* $NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2006 Shigeyuki Fukushima. | 4 | * Copyright (c) 2006 Shigeyuki Fukushima. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * Written by Shigeyuki Fukushima. | 7 | * Written by Shigeyuki Fukushima. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above | 14 | * 2. Redistributions in binary form must reproduce the above | |
@@ -23,27 +23,27 @@ | @@ -23,27 +23,27 @@ | |||
23 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 23 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
26 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 26 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
28 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 28 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | 33 | */ | |
34 | 34 | |||
35 | #include <sys/cdefs.h> | 35 | #include <sys/cdefs.h> | |
36 | __KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.9 2020/01/02 17:03:05 thorpej Exp $"); | 36 | __KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $"); | |
37 | 37 | |||
38 | #include <sys/param.h> | 38 | #include <sys/param.h> | |
39 | #include <sys/systm.h> | 39 | #include <sys/systm.h> | |
40 | #include <sys/device.h> | 40 | #include <sys/device.h> | |
41 | #include <sys/kernel.h> | 41 | #include <sys/kernel.h> | |
42 | #include <sys/fcntl.h> | 42 | #include <sys/fcntl.h> | |
43 | #include <sys/uio.h> | 43 | #include <sys/uio.h> | |
44 | #include <sys/conf.h> | 44 | #include <sys/conf.h> | |
45 | #include <sys/event.h> | 45 | #include <sys/event.h> | |
46 | 46 | |||
47 | #include <dev/clock_subr.h> | 47 | #include <dev/clock_subr.h> | |
48 | 48 | |||
49 | #include <dev/i2c/i2cvar.h> | 49 | #include <dev/i2c/i2cvar.h> | |
@@ -53,28 +53,30 @@ struct r2025rtc_softc { | @@ -53,28 +53,30 @@ struct r2025rtc_softc { | |||
53 | device_t sc_dev; | 53 | device_t sc_dev; | |
54 | i2c_tag_t sc_tag; | 54 | i2c_tag_t sc_tag; | |
55 | int sc_address; | 55 | int sc_address; | |
56 | int sc_open; | 56 | int sc_open; | |
57 | struct todr_chip_handle sc_todr; | 57 | struct todr_chip_handle sc_todr; | |
58 | }; | 58 | }; | |
59 | 59 | |||
60 | static void r2025rtc_attach(device_t, device_t, void *); | 60 | static void r2025rtc_attach(device_t, device_t, void *); | |
61 | static int r2025rtc_match(device_t, cfdata_t, void *); | 61 | static int r2025rtc_match(device_t, cfdata_t, void *); | |
62 | 62 | |||
63 | CFATTACH_DECL_NEW(r2025rtc, sizeof(struct r2025rtc_softc), | 63 | CFATTACH_DECL_NEW(r2025rtc, sizeof(struct r2025rtc_softc), | |
64 | r2025rtc_match, r2025rtc_attach, NULL, NULL); | 64 | r2025rtc_match, r2025rtc_attach, NULL, NULL); | |
65 | 65 | |||
66 | static int r2025rtc_gettime(struct todr_chip_handle *, struct timeval *); | 66 | static int r2025rtc_gettime_ymdhms(struct todr_chip_handle *, | |
67 | static int r2025rtc_settime(struct todr_chip_handle *, struct timeval *); | 67 | struct clock_ymdhms *); | |
68 | static int r2025rtc_settime_ymdhms(struct todr_chip_handle *, | |||
69 | struct clock_ymdhms *); | |||
68 | static int r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int); | 70 | static int r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int); | |
69 | static int r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int); | 71 | static int r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int); | |
70 | 72 | |||
71 | 73 | |||
72 | static int | 74 | static int | |
73 | r2025rtc_match(device_t parent, cfdata_t cf, void *arg) | 75 | r2025rtc_match(device_t parent, cfdata_t cf, void *arg) | |
74 | { | 76 | { | |
75 | struct i2c_attach_args *ia = arg; | 77 | struct i2c_attach_args *ia = arg; | |
76 | 78 | |||
77 | /* match only R2025 RTC devices */ | 79 | /* match only R2025 RTC devices */ | |
78 | if (ia->ia_addr == R2025_ADDR) | 80 | if (ia->ia_addr == R2025_ADDR) | |
79 | return I2C_MATCH_ADDRESS_ONLY; | 81 | return I2C_MATCH_ADDRESS_ONLY; | |
80 | 82 | |||
@@ -84,121 +86,116 @@ r2025rtc_match(device_t parent, cfdata_t | @@ -84,121 +86,116 @@ r2025rtc_match(device_t parent, cfdata_t | |||
84 | static void | 86 | static void | |
85 | r2025rtc_attach(device_t parent, device_t self, void *arg) | 87 | r2025rtc_attach(device_t parent, device_t self, void *arg) | |
86 | { | 88 | { | |
87 | struct r2025rtc_softc *sc = device_private(self); | 89 | struct r2025rtc_softc *sc = device_private(self); | |
88 | struct i2c_attach_args *ia = arg; | 90 | struct i2c_attach_args *ia = arg; | |
89 | 91 | |||
90 | aprint_normal(": RICOH R2025S/D Real-time Clock\n"); | 92 | aprint_normal(": RICOH R2025S/D Real-time Clock\n"); | |
91 | 93 | |||
92 | sc->sc_tag = ia->ia_tag; | 94 | sc->sc_tag = ia->ia_tag; | |
93 | sc->sc_address = ia->ia_addr; | 95 | sc->sc_address = ia->ia_addr; | |
94 | sc->sc_dev = self; | 96 | sc->sc_dev = self; | |
95 | sc->sc_open = 0; | 97 | sc->sc_open = 0; | |
96 | sc->sc_todr.cookie = sc; | 98 | sc->sc_todr.cookie = sc; | |
97 | sc->sc_todr.todr_gettime = r2025rtc_gettime; | 99 | sc->sc_todr.todr_gettime = NULL; | |
98 | sc->sc_todr.todr_settime = r2025rtc_settime; | 100 | sc->sc_todr.todr_settime = NULL; | |
101 | sc->sc_todr.todr_gettime_ymdhms = r2025rtc_gettime_ymdhms; | |||
102 | sc->sc_todr.todr_settime_ymdhms = r2025rtc_settime_ymdhms; | |||
99 | sc->sc_todr.todr_setwen = NULL; | 103 | sc->sc_todr.todr_setwen = NULL; | |
100 | 104 | |||
101 | todr_attach(&sc->sc_todr); | 105 | todr_attach(&sc->sc_todr); | |
102 | } | 106 | } | |
103 | 107 | |||
104 | static int | 108 | static int | |
105 | r2025rtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) | 109 | r2025rtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
106 | { | 110 | { | |
107 | struct r2025rtc_softc *sc = ch->cookie; | 111 | struct r2025rtc_softc *sc = ch->cookie; | |
108 | struct clock_ymdhms dt; | |||
109 | uint8_t rctrl; | 112 | uint8_t rctrl; | |
110 | uint8_t bcd[R2025_CLK_SIZE]; | 113 | uint8_t bcd[R2025_CLK_SIZE]; | |
111 | int hour; | 114 | int hour; | |
112 | int error; | 115 | int error; | |
113 | 116 | |||
114 | memset(&dt, 0, sizeof(dt)); | 117 | memset(dt, 0, sizeof(*dt)); | |
115 | 118 | |||
116 | if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | 119 | if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | |
117 | aprint_error_dev(sc->sc_dev, | 120 | aprint_error_dev(sc->sc_dev, | |
118 | "r2025rtc_gettime: failed to read registers.\n"); | 121 | "r2025rtc_gettime: failed to read registers.\n"); | |
119 | return error; | 122 | return error; | |
120 | } | 123 | } | |
121 | 124 | |||
122 | if ((error = r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0], | 125 | if ((error = r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0], | |
123 | R2025_CLK_SIZE)) != 0) { | 126 | R2025_CLK_SIZE)) != 0) { | |
124 | aprint_error_dev(sc->sc_dev, | 127 | aprint_error_dev(sc->sc_dev, | |
125 | "r2025rtc_gettime: failed to read registers.\n"); | 128 | "r2025rtc_gettime: failed to read registers.\n"); | |
126 | return error; | 129 | return error; | |
127 | } | 130 | } | |
128 | 131 | |||
129 | dt.dt_sec = bcdtobin(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK); | 132 | dt->dt_sec = bcdtobin(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK); | |
130 | dt.dt_min = bcdtobin(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK); | 133 | dt->dt_min = bcdtobin(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK); | |
131 | hour = bcdtobin(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK); | 134 | hour = bcdtobin(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK); | |
132 | if (rctrl & R2025_REG_CTRL1_H1224) { | 135 | if (rctrl & R2025_REG_CTRL1_H1224) { | |
133 | dt.dt_hour = hour; | 136 | dt->dt_hour = hour; | |
134 | } else { | 137 | } else { | |
135 | if (hour == 12) { | 138 | if (hour == 12) { | |
136 | dt.dt_hour = 0; | 139 | dt->dt_hour = 0; | |
137 | } else if (hour == 32) { | 140 | } else if (hour == 32) { | |
138 | dt.dt_hour = 12; | 141 | dt->dt_hour = 12; | |
139 | } else if (hour > 13) { | 142 | } else if (hour > 13) { | |
140 | dt.dt_hour = (hour - 8); | 143 | dt->dt_hour = (hour - 8); | |
141 | } else { /* (hour < 12) */ | 144 | } else { /* (hour < 12) */ | |
142 | dt.dt_hour = hour; | 145 | dt->dt_hour = hour; | |
143 | } | 146 | } | |
144 | } | 147 | } | |
145 | dt.dt_wday = bcdtobin(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK); | 148 | dt->dt_wday = bcdtobin(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK); | |
146 | dt.dt_day = bcdtobin(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK); | 149 | dt->dt_day = bcdtobin(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK); | |
147 | dt.dt_mon = bcdtobin(bcd[R2025_REG_MON] & R2025_REG_MON_MASK); | 150 | dt->dt_mon = bcdtobin(bcd[R2025_REG_MON] & R2025_REG_MON_MASK); | |
148 | dt.dt_year = bcdtobin(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK) | 151 | dt->dt_year = bcdtobin(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK) | |
149 | + ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900); | 152 | + ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900); | |
150 | 153 | |||
151 | tv->tv_sec = clock_ymdhms_to_secs(&dt); | |||
152 | tv->tv_usec = 0; | |||
153 | ||||
154 | return 0; | 154 | return 0; | |
155 | } | 155 | } | |
156 | 156 | |||
157 | static int | 157 | static int | |
158 | r2025rtc_settime(struct todr_chip_handle *ch, struct timeval *tv) | 158 | r2025rtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
159 | { | 159 | { | |
160 | struct r2025rtc_softc *sc = ch->cookie; | 160 | struct r2025rtc_softc *sc = ch->cookie; | |
161 | struct clock_ymdhms dt; | |||
162 | uint8_t rctrl; | 161 | uint8_t rctrl; | |
163 | uint8_t bcd[R2025_CLK_SIZE]; | 162 | uint8_t bcd[R2025_CLK_SIZE]; | |
164 | int error; | 163 | int error; | |
165 | 164 | |||
166 | clock_secs_to_ymdhms(tv->tv_sec, &dt); | |||
167 | ||||
168 | /* Y3K problem */ | 165 | /* Y3K problem */ | |
169 | if (dt.dt_year >= 3000) { | 166 | if (dt->dt_year >= 3000) { | |
170 | aprint_error_dev(sc->sc_dev, | 167 | aprint_error_dev(sc->sc_dev, | |
171 | "r2025rtc_settime: " | 168 | "r2025rtc_settime: " | |
172 | "RTC does not support year 3000 or over.\n"); | 169 | "RTC does not support year 3000 or over.\n"); | |
173 | return EINVAL; | 170 | return EINVAL; | |
174 | } | 171 | } | |
175 | 172 | |||
176 | if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | 173 | if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | |
177 | aprint_error_dev(sc->sc_dev, | 174 | aprint_error_dev(sc->sc_dev, | |
178 | "r2025rtc_settime: failed to read register.\n"); | 175 | "r2025rtc_settime: failed to read register.\n"); | |
179 | return error; | 176 | return error; | |
180 | } | 177 | } | |
181 | rctrl |= R2025_REG_CTRL1_H1224; | 178 | rctrl |= R2025_REG_CTRL1_H1224; | |
182 | 179 | |||
183 | /* setup registers 0x00-0x06 (7 byte) */ | 180 | /* setup registers 0x00-0x06 (7 byte) */ | |
184 | bcd[R2025_REG_SEC] = bintobcd(dt.dt_sec) & R2025_REG_SEC_MASK; | 181 | bcd[R2025_REG_SEC] = bintobcd(dt->dt_sec) & R2025_REG_SEC_MASK; | |
185 | bcd[R2025_REG_MIN] = bintobcd(dt.dt_min) & R2025_REG_MIN_MASK; | 182 | bcd[R2025_REG_MIN] = bintobcd(dt->dt_min) & R2025_REG_MIN_MASK; | |
186 | bcd[R2025_REG_HOUR] = bintobcd(dt.dt_hour) & R2025_REG_HOUR_MASK; | 183 | bcd[R2025_REG_HOUR] = bintobcd(dt->dt_hour) & R2025_REG_HOUR_MASK; | |
187 | bcd[R2025_REG_WDAY] = bintobcd(dt.dt_wday) & R2025_REG_WDAY_MASK; | 184 | bcd[R2025_REG_WDAY] = bintobcd(dt->dt_wday) & R2025_REG_WDAY_MASK; | |
188 | bcd[R2025_REG_DAY] = bintobcd(dt.dt_day) & R2025_REG_DAY_MASK; | 185 | bcd[R2025_REG_DAY] = bintobcd(dt->dt_day) & R2025_REG_DAY_MASK; | |
189 | bcd[R2025_REG_MON] = (bintobcd(dt.dt_mon) & R2025_REG_MON_MASK) | 186 | bcd[R2025_REG_MON] = (bintobcd(dt->dt_mon) & R2025_REG_MON_MASK) | |
190 | | ((dt.dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0); | 187 | | ((dt->dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0); | |
191 | bcd[R2025_REG_YEAR] = bintobcd(dt.dt_year % 100) & R2025_REG_YEAR_MASK; | 188 | bcd[R2025_REG_YEAR] = bintobcd(dt->dt_year % 100) & R2025_REG_YEAR_MASK; | |
192 | 189 | |||
193 | /* Write RTC register */ | 190 | /* Write RTC register */ | |
194 | if ((error = r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | 191 | if ((error = r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) { | |
195 | aprint_error_dev(sc->sc_dev, | 192 | aprint_error_dev(sc->sc_dev, | |
196 | "r2025rtc_settime: failed to write registers.\n"); | 193 | "r2025rtc_settime: failed to write registers.\n"); | |
197 | return error; | 194 | return error; | |
198 | } | 195 | } | |
199 | if ((error = r2025rtc_reg_write(sc, R2025_REG_SEC, bcd, | 196 | if ((error = r2025rtc_reg_write(sc, R2025_REG_SEC, bcd, | |
200 | R2025_CLK_SIZE)) != 0) { | 197 | R2025_CLK_SIZE)) != 0) { | |
201 | aprint_error_dev(sc->sc_dev, | 198 | aprint_error_dev(sc->sc_dev, | |
202 | "r2025rtc_settime: failed to write registers.\n"); | 199 | "r2025rtc_settime: failed to write registers.\n"); | |
203 | return error; | 200 | return error; | |
204 | } | 201 | } |
--- src/sys/dev/i2c/s390.c 2020/01/02 17:26:37 1.5
+++ src/sys/dev/i2c/s390.c 2020/01/02 19:00:34 1.6
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: s390.c,v 1.5 2020/01/02 17:26:37 thorpej Exp $ */ | 1 | /* $NetBSD: s390.c,v 1.6 2020/01/02 19:00:34 thorpej Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2011 Frank Wille. | 4 | * Copyright (c) 2011 Frank Wille. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * Written by Frank Wille for The NetBSD Project. | 7 | * Written by Frank Wille for The NetBSD Project. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -19,55 +19,55 @@ | @@ -19,55 +19,55 @@ | |||
19 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | 19 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
20 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 20 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | 28 | * POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | 29 | */ | |
30 | 30 | |||
31 | #include <sys/cdefs.h> | 31 | #include <sys/cdefs.h> | |
32 | __KERNEL_RCSID(0, "$NetBSD: s390.c,v 1.5 2020/01/02 17:26:37 thorpej Exp $"); | 32 | __KERNEL_RCSID(0, "$NetBSD: s390.c,v 1.6 2020/01/02 19:00:34 thorpej Exp $"); | |
33 | 33 | |||
34 | #include <sys/param.h> | 34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | 35 | #include <sys/systm.h> | |
36 | #include <sys/device.h> | 36 | #include <sys/device.h> | |
37 | #include <sys/conf.h> | 37 | #include <sys/conf.h> | |
38 | 38 | |||
39 | #include <dev/clock_subr.h> | 39 | #include <dev/clock_subr.h> | |
40 | 40 | |||
41 | #include <dev/i2c/i2cvar.h> | 41 | #include <dev/i2c/i2cvar.h> | |
42 | #include <dev/i2c/s390reg.h> | 42 | #include <dev/i2c/s390reg.h> | |
43 | 43 | |||
44 | struct s390rtc_softc { | 44 | struct s390rtc_softc { | |
45 | device_t sc_dev; | 45 | device_t sc_dev; | |
46 | i2c_tag_t sc_tag; | 46 | i2c_tag_t sc_tag; | |
47 | i2c_addr_t sc_addr; | 47 | i2c_addr_t sc_addr; | |
48 | struct todr_chip_handle sc_todr; | 48 | struct todr_chip_handle sc_todr; | |
49 | }; | 49 | }; | |
50 | 50 | |||
51 | static int s390rtc_match(device_t, cfdata_t, void *); | 51 | static int s390rtc_match(device_t, cfdata_t, void *); | |
52 | static void s390rtc_attach(device_t, device_t, void *); | 52 | static void s390rtc_attach(device_t, device_t, void *); | |
53 | 53 | |||
54 | CFATTACH_DECL_NEW(s390rtc, sizeof(struct s390rtc_softc), | 54 | CFATTACH_DECL_NEW(s390rtc, sizeof(struct s390rtc_softc), | |
55 | s390rtc_match, s390rtc_attach, NULL, NULL); | 55 | s390rtc_match, s390rtc_attach, NULL, NULL); | |
56 | 56 | |||
57 | static int s390rtc_gettime(struct todr_chip_handle *, struct timeval *); | 57 | static int s390rtc_gettime_ymdhms(struct todr_chip_handle *, | |
58 | static int s390rtc_settime(struct todr_chip_handle *, struct timeval *); | 58 | struct clock_ymdhms *); | |
59 | static int s390rtc_clock_read(struct s390rtc_softc *, struct clock_ymdhms *); | 59 | static int s390rtc_settime_ymdhms(struct todr_chip_handle *, | |
60 | static int s390rtc_clock_write(struct s390rtc_softc *, struct clock_ymdhms *); | 60 | struct clock_ymdhms *); | |
61 | static int s390rtc_read(struct s390rtc_softc *, int, uint8_t *, size_t); | 61 | static int s390rtc_read(struct s390rtc_softc *, int, uint8_t *, size_t); | |
62 | static int s390rtc_write(struct s390rtc_softc *, int, uint8_t *, size_t); | 62 | static int s390rtc_write(struct s390rtc_softc *, int, uint8_t *, size_t); | |
63 | static uint8_t bitreverse(uint8_t); | 63 | static uint8_t bitreverse(uint8_t); | |
64 | 64 | |||
65 | static int | 65 | static int | |
66 | s390rtc_match(device_t parent, cfdata_t cf, void *arg) | 66 | s390rtc_match(device_t parent, cfdata_t cf, void *arg) | |
67 | { | 67 | { | |
68 | struct i2c_attach_args *ia = arg; | 68 | struct i2c_attach_args *ia = arg; | |
69 | int match_result; | 69 | int match_result; | |
70 | 70 | |||
71 | if (iic_use_direct_match(ia, cf, NULL, &match_result)) | 71 | if (iic_use_direct_match(ia, cf, NULL, &match_result)) | |
72 | return match_result; | 72 | return match_result; | |
73 | 73 | |||
@@ -101,91 +101,62 @@ s390rtc_attach(device_t parent, device_t | @@ -101,91 +101,62 @@ s390rtc_attach(device_t parent, device_t | |||
101 | return; | 101 | return; | |
102 | } | 102 | } | |
103 | 103 | |||
104 | /* Disable the test mode, when enabled. */ | 104 | /* Disable the test mode, when enabled. */ | |
105 | if (s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg))) | 105 | if (s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg))) | |
106 | return; | 106 | return; | |
107 | if ((reg[0] & S390_ST2_TEST)) { | 107 | if ((reg[0] & S390_ST2_TEST)) { | |
108 | reg[0] &= ~S390_ST2_TEST; | 108 | reg[0] &= ~S390_ST2_TEST; | |
109 | if (s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg))) | 109 | if (s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg))) | |
110 | return; | 110 | return; | |
111 | } | 111 | } | |
112 | 112 | |||
113 | sc->sc_todr.cookie = sc; | 113 | sc->sc_todr.cookie = sc; | |
114 | sc->sc_todr.todr_gettime = s390rtc_gettime; | 114 | sc->sc_todr.todr_gettime = NULL; | |
115 | sc->sc_todr.todr_settime = s390rtc_settime; | 115 | sc->sc_todr.todr_settime = NULL; | |
116 | sc->sc_todr.todr_gettime_ymdhms = s390rtc_gettime_ymdhms; | |||
117 | sc->sc_todr.todr_settime_ymdhms = s390rtc_settime_ymdhms; | |||
116 | sc->sc_todr.todr_setwen = NULL; | 118 | sc->sc_todr.todr_setwen = NULL; | |
117 | todr_attach(&sc->sc_todr); | 119 | todr_attach(&sc->sc_todr); | |
118 | } | 120 | } | |
119 | 121 | |||
120 | static int | 122 | static int | |
121 | s390rtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) | 123 | s390rtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
122 | { | 124 | { | |
123 | struct s390rtc_softc *sc = ch->cookie; | 125 | struct s390rtc_softc *sc = ch->cookie; | |
124 | struct clock_ymdhms dt; | |||
125 | int error; | |||
126 | ||||
127 | memset(&dt, 0, sizeof(dt)); | |||
128 | ||||
129 | if ((error = s390rtc_clock_read(sc, &dt)) != 0) | |||
130 | return error; | |||
131 | ||||
132 | tv->tv_sec = clock_ymdhms_to_secs(&dt); | |||
133 | tv->tv_usec = 0; | |||
134 | ||||
135 | return 0; | |||
136 | } | |||
137 | ||||
138 | static int | |||
139 | s390rtc_settime(struct todr_chip_handle *ch, struct timeval *tv) | |||
140 | { | |||
141 | struct s390rtc_softc *sc = ch->cookie; | |||
142 | struct clock_ymdhms dt; | |||
143 | int error; | |||
144 | ||||
145 | clock_secs_to_ymdhms(tv->tv_sec, &dt); | |||
146 | ||||
147 | if ((error = s390rtc_clock_write(sc, &dt)) != 0) | |||
148 | return error; | |||
149 | ||||
150 | return 0; | |||
151 | } | |||
152 | ||||
153 | static int | |||
154 | s390rtc_clock_read(struct s390rtc_softc *sc, struct clock_ymdhms *dt) | |||
155 | { | |||
156 | uint8_t bcd[S390_RT1_NBYTES]; | 126 | uint8_t bcd[S390_RT1_NBYTES]; | |
157 | int error; | 127 | int error; | |
158 | 128 | |||
159 | if ((error = s390rtc_read(sc, S390_REALTIME1, bcd, | 129 | if ((error = s390rtc_read(sc, S390_REALTIME1, bcd, | |
160 | S390_RT1_NBYTES)) != 0) | 130 | S390_RT1_NBYTES)) != 0) | |
161 | return error; | 131 | return error; | |
162 | 132 | |||
163 | /* | 133 | /* | |
164 | * Convert the register values into something useable. | 134 | * Convert the register values into something useable. | |
165 | */ | 135 | */ | |
166 | dt->dt_sec = bcdtobin(bcd[S390_RT1_SECOND]); | 136 | dt->dt_sec = bcdtobin(bcd[S390_RT1_SECOND]); | |
167 | dt->dt_min = bcdtobin(bcd[S390_RT1_MINUTE]); | 137 | dt->dt_min = bcdtobin(bcd[S390_RT1_MINUTE]); | |
168 | dt->dt_hour = bcdtobin(bcd[S390_RT1_HOUR] & 0x3f); | 138 | dt->dt_hour = bcdtobin(bcd[S390_RT1_HOUR] & 0x3f); | |
169 | dt->dt_day = bcdtobin(bcd[S390_RT1_DAY]); | 139 | dt->dt_day = bcdtobin(bcd[S390_RT1_DAY]); | |
170 | dt->dt_mon = bcdtobin(bcd[S390_RT1_MONTH]); | 140 | dt->dt_mon = bcdtobin(bcd[S390_RT1_MONTH]); | |
171 | dt->dt_year = bcdtobin(bcd[S390_RT1_YEAR]) + 2000; | 141 | dt->dt_year = bcdtobin(bcd[S390_RT1_YEAR]) + 2000; | |
172 | 142 | |||
173 | return 0; | 143 | return 0; | |
174 | } | 144 | } | |
175 | 145 | |||
176 | static int | 146 | static int | |
177 | s390rtc_clock_write(struct s390rtc_softc *sc, struct clock_ymdhms *dt) | 147 | s390rtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
178 | { | 148 | { | |
149 | struct s390rtc_softc *sc = ch->cookie; | |||
179 | uint8_t bcd[S390_RT1_NBYTES]; | 150 | uint8_t bcd[S390_RT1_NBYTES]; | |
180 | 151 | |||
181 | /* | 152 | /* | |
182 | * Convert our time representation into something the S-xx390 | 153 | * Convert our time representation into something the S-xx390 | |
183 | * can understand. | 154 | * can understand. | |
184 | */ | 155 | */ | |
185 | bcd[S390_RT1_SECOND] = bintobcd(dt->dt_sec); | 156 | bcd[S390_RT1_SECOND] = bintobcd(dt->dt_sec); | |
186 | bcd[S390_RT1_MINUTE] = bintobcd(dt->dt_min); | 157 | bcd[S390_RT1_MINUTE] = bintobcd(dt->dt_min); | |
187 | bcd[S390_RT1_HOUR] = bintobcd(dt->dt_hour); | 158 | bcd[S390_RT1_HOUR] = bintobcd(dt->dt_hour); | |
188 | bcd[S390_RT1_DAY] = bintobcd(dt->dt_day); | 159 | bcd[S390_RT1_DAY] = bintobcd(dt->dt_day); | |
189 | bcd[S390_RT1_WDAY] = bintobcd(dt->dt_wday); | 160 | bcd[S390_RT1_WDAY] = bintobcd(dt->dt_wday); | |
190 | bcd[S390_RT1_MONTH] = bintobcd(dt->dt_mon); | 161 | bcd[S390_RT1_MONTH] = bintobcd(dt->dt_mon); | |
191 | bcd[S390_RT1_YEAR] = bintobcd(dt->dt_year % 100); | 162 | bcd[S390_RT1_YEAR] = bintobcd(dt->dt_year % 100); |
--- src/sys/dev/i2c/x1226.c 2020/01/02 17:40:27 1.22
+++ src/sys/dev/i2c/x1226.c 2020/01/02 19:00:34 1.23
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $ */ | 1 | /* $NetBSD: x1226.c,v 1.23 2020/01/02 19:00:34 thorpej Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 2003 Shigeyuki Fukushima. | 4 | * Copyright (c) 2003 Shigeyuki Fukushima. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * Written by Shigeyuki Fukushima for the NetBSD Project. | 7 | * Written by Shigeyuki Fukushima for the NetBSD Project. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -26,27 +26,27 @@ | @@ -26,27 +26,27 @@ | |||
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIGEYUKI FUKUSHIMA | 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIGEYUKI FUKUSHIMA | |
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
35 | * POSSIBILITY OF SUCH DAMAGE. | 35 | * POSSIBILITY OF SUCH DAMAGE. | |
36 | */ | 36 | */ | |
37 | 37 | |||
38 | #include <sys/cdefs.h> | 38 | #include <sys/cdefs.h> | |
39 | __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $"); | 39 | __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.23 2020/01/02 19:00:34 thorpej Exp $"); | |
40 | 40 | |||
41 | #include <sys/param.h> | 41 | #include <sys/param.h> | |
42 | #include <sys/systm.h> | 42 | #include <sys/systm.h> | |
43 | #include <sys/device.h> | 43 | #include <sys/device.h> | |
44 | #include <sys/kernel.h> | 44 | #include <sys/kernel.h> | |
45 | #include <sys/fcntl.h> | 45 | #include <sys/fcntl.h> | |
46 | #include <sys/uio.h> | 46 | #include <sys/uio.h> | |
47 | #include <sys/conf.h> | 47 | #include <sys/conf.h> | |
48 | #include <sys/event.h> | 48 | #include <sys/event.h> | |
49 | 49 | |||
50 | #include <dev/clock_subr.h> | 50 | #include <dev/clock_subr.h> | |
51 | 51 | |||
52 | #include <dev/i2c/i2cvar.h> | 52 | #include <dev/i2c/i2cvar.h> | |
@@ -79,29 +79,30 @@ const struct cdevsw xrtc_cdevsw = { | @@ -79,29 +79,30 @@ const struct cdevsw xrtc_cdevsw = { | |||
79 | .d_read = xrtc_read, | 79 | .d_read = xrtc_read, | |
80 | .d_write = xrtc_write, | 80 | .d_write = xrtc_write, | |
81 | .d_ioctl = noioctl, | 81 | .d_ioctl = noioctl, | |
82 | .d_stop = nostop, | 82 | .d_stop = nostop, | |
83 | .d_tty = notty, | 83 | .d_tty = notty, | |
84 | .d_poll = nopoll, | 84 | .d_poll = nopoll, | |
85 | .d_mmap = nommap, | 85 | .d_mmap = nommap, | |
86 | .d_kqfilter = nokqfilter, | 86 | .d_kqfilter = nokqfilter, | |
87 | .d_discard = nodiscard, | 87 | .d_discard = nodiscard, | |
88 | .d_flag = D_OTHER | 88 | .d_flag = D_OTHER | |
89 | }; | 89 | }; | |
90 | 90 | |||
91 | static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *); | 91 | static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *); | |
92 | static int xrtc_clock_write(struct xrtc_softc *, struct clock_ymdhms *); | 92 | static int xrtc_gettime_ymdhms(struct todr_chip_handle *, | |
93 | static int xrtc_gettime(struct todr_chip_handle *, struct timeval *); | 93 | struct clock_ymdhms *); | |
94 | static int xrtc_settime(struct todr_chip_handle *, struct timeval *); | 94 | static int xrtc_settime_ymdhms(struct todr_chip_handle *, | |
95 | struct clock_ymdhms *); | |||
95 | 96 | |||
96 | /* | 97 | /* | |
97 | * xrtc_match() | 98 | * xrtc_match() | |
98 | */ | 99 | */ | |
99 | static int | 100 | static int | |
100 | xrtc_match(device_t parent, cfdata_t cf, void *arg) | 101 | xrtc_match(device_t parent, cfdata_t cf, void *arg) | |
101 | { | 102 | { | |
102 | struct i2c_attach_args *ia = arg; | 103 | struct i2c_attach_args *ia = arg; | |
103 | 104 | |||
104 | /* match only this RTC devices */ | 105 | /* match only this RTC devices */ | |
105 | if (ia->ia_addr == X1226_ADDR) | 106 | if (ia->ia_addr == X1226_ADDR) | |
106 | return (I2C_MATCH_ADDRESS_ONLY); | 107 | return (I2C_MATCH_ADDRESS_ONLY); | |
107 | 108 | |||
@@ -115,28 +116,30 @@ static void | @@ -115,28 +116,30 @@ static void | |||
115 | xrtc_attach(device_t parent, device_t self, void *arg) | 116 | xrtc_attach(device_t parent, device_t self, void *arg) | |
116 | { | 117 | { | |
117 | struct xrtc_softc *sc = device_private(self); | 118 | struct xrtc_softc *sc = device_private(self); | |
118 | struct i2c_attach_args *ia = arg; | 119 | struct i2c_attach_args *ia = arg; | |
119 | 120 | |||
120 | aprint_naive(": Real-time Clock/NVRAM\n"); | 121 | aprint_naive(": Real-time Clock/NVRAM\n"); | |
121 | aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n"); | 122 | aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n"); | |
122 | 123 | |||
123 | sc->sc_tag = ia->ia_tag; | 124 | sc->sc_tag = ia->ia_tag; | |
124 | sc->sc_address = ia->ia_addr; | 125 | sc->sc_address = ia->ia_addr; | |
125 | sc->sc_dev = self; | 126 | sc->sc_dev = self; | |
126 | sc->sc_open = 0; | 127 | sc->sc_open = 0; | |
127 | sc->sc_todr.cookie = sc; | 128 | sc->sc_todr.cookie = sc; | |
128 | sc->sc_todr.todr_gettime = xrtc_gettime; | 129 | sc->sc_todr.todr_gettime = NULL; | |
129 | sc->sc_todr.todr_settime = xrtc_settime; | 130 | sc->sc_todr.todr_settime = NULL; | |
131 | sc->sc_todr.todr_gettime_ymdhms = xrtc_gettime_ymdhms; | |||
132 | sc->sc_todr.todr_settime_ymdhms = xrtc_settime_ymdhms; | |||
130 | sc->sc_todr.todr_setwen = NULL; | 133 | sc->sc_todr.todr_setwen = NULL; | |
131 | 134 | |||
132 | todr_attach(&sc->sc_todr); | 135 | todr_attach(&sc->sc_todr); | |
133 | } | 136 | } | |
134 | 137 | |||
135 | 138 | |||
136 | /*ARGSUSED*/ | 139 | /*ARGSUSED*/ | |
137 | int | 140 | int | |
138 | xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l) | 141 | xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l) | |
139 | { | 142 | { | |
140 | struct xrtc_softc *sc; | 143 | struct xrtc_softc *sc; | |
141 | 144 | |||
142 | if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL) | 145 | if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL) | |
@@ -237,62 +240,48 @@ xrtc_write(dev_t dev, struct uio *uio, i | @@ -237,62 +240,48 @@ xrtc_write(dev_t dev, struct uio *uio, i | |||
237 | "xrtc_write: write failed at 0x%x\n", | 240 | "xrtc_write: write failed at 0x%x\n", | |
238 | (int)uio->uio_offset); | 241 | (int)uio->uio_offset); | |
239 | return (error); | 242 | return (error); | |
240 | } | 243 | } | |
241 | } | 244 | } | |
242 | 245 | |||
243 | iic_release_bus(sc->sc_tag, 0); | 246 | iic_release_bus(sc->sc_tag, 0); | |
244 | 247 | |||
245 | return (0); | 248 | return (0); | |
246 | } | 249 | } | |
247 | 250 | |||
248 | 251 | |||
249 | static int | 252 | static int | |
250 | xrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) | 253 | xrtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
251 | { | 254 | { | |
252 | struct xrtc_softc *sc = ch->cookie; | 255 | struct xrtc_softc *sc = ch->cookie; | |
253 | struct clock_ymdhms dt, check; | 256 | struct clock_ymdhms check; | |
254 | int retries; | 257 | int retries; | |
255 | int error; | 258 | int error; | |
256 | 259 | |||
257 | memset(&dt, 0, sizeof(dt)); | 260 | memset(dt, 0, sizeof(*dt)); | |
258 | memset(&check, 0, sizeof(check)); | 261 | memset(&check, 0, sizeof(check)); | |
259 | 262 | |||
260 | retries = 5; | 263 | retries = 5; | |
261 | do { | 264 | do { | |
262 | if ((error = xrtc_clock_read(sc, &dt)) == 0) | 265 | if ((error = xrtc_clock_read(sc, dt)) == 0) | |
263 | error = xrtc_clock_read(sc, &check); | 266 | error = xrtc_clock_read(sc, &check); | |
264 | if (error) | 267 | if (error) | |
265 | return error; | 268 | return error; | |
266 | } while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries); | 269 | } while (memcmp(dt, &check, sizeof(check)) != 0 && --retries); | |
267 | ||||
268 | tv->tv_sec = clock_ymdhms_to_secs(&dt); | |||
269 | tv->tv_usec = 0; | |||
270 | 270 | |||
271 | return (0); | 271 | return (0); | |
272 | } | 272 | } | |
273 | 273 | |||
274 | static int | 274 | static int | |
275 | xrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) | |||
276 | { | |||
277 | struct xrtc_softc *sc = ch->cookie; | |||
278 | struct clock_ymdhms dt; | |||
279 | ||||
280 | clock_secs_to_ymdhms(tv->tv_sec, &dt); | |||
281 | ||||
282 | return xrtc_clock_write(sc, &dt); | |||
283 | } | |||
284 | ||||
285 | static int | |||
286 | xrtc_clock_read(struct xrtc_softc *sc, struct clock_ymdhms *dt) | 275 | xrtc_clock_read(struct xrtc_softc *sc, struct clock_ymdhms *dt) | |
287 | { | 276 | { | |
288 | int i = 0; | 277 | int i = 0; | |
289 | u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2]; | 278 | u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2]; | |
290 | int error; | 279 | int error; | |
291 | 280 | |||
292 | if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) { | 281 | if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) { | |
293 | aprint_error_dev(sc->sc_dev, | 282 | aprint_error_dev(sc->sc_dev, | |
294 | "xrtc_clock_read: failed to acquire I2C bus\n"); | 283 | "xrtc_clock_read: failed to acquire I2C bus\n"); | |
295 | return (error); | 284 | return (error); | |
296 | } | 285 | } | |
297 | 286 | |||
298 | /* Read each RTC register in order */ | 287 | /* Read each RTC register in order */ | |
@@ -338,28 +327,29 @@ xrtc_clock_read(struct xrtc_softc *sc, s | @@ -338,28 +327,29 @@ xrtc_clock_read(struct xrtc_softc *sc, s | |||
338 | dt->dt_day = bcdtobin(bcd[X1226_REG_DT - X1226_REG_RTC_BASE] | 327 | dt->dt_day = bcdtobin(bcd[X1226_REG_DT - X1226_REG_RTC_BASE] | |
339 | & X1226_REG_DT_MASK); | 328 | & X1226_REG_DT_MASK); | |
340 | dt->dt_mon = bcdtobin(bcd[X1226_REG_MO - X1226_REG_RTC_BASE] | 329 | dt->dt_mon = bcdtobin(bcd[X1226_REG_MO - X1226_REG_RTC_BASE] | |
341 | & X1226_REG_MO_MASK); | 330 | & X1226_REG_MO_MASK); | |
342 | dt->dt_year = bcdtobin(bcd[X1226_REG_YR - X1226_REG_RTC_BASE] | 331 | dt->dt_year = bcdtobin(bcd[X1226_REG_YR - X1226_REG_RTC_BASE] | |
343 | & X1226_REG_YR_MASK); | 332 | & X1226_REG_YR_MASK); | |
344 | dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] | 333 | dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] | |
345 | & X1226_REG_Y2K_MASK) * 100; | 334 | & X1226_REG_Y2K_MASK) * 100; | |
346 | 335 | |||
347 | return (0); | 336 | return (0); | |
348 | } | 337 | } | |
349 | 338 | |||
350 | static int | 339 | static int | |
351 | xrtc_clock_write(struct xrtc_softc *sc, struct clock_ymdhms *dt) | 340 | xrtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt) | |
352 | { | 341 | { | |
342 | struct xrtc_softc *sc = ch->cookie; | |||
353 | int i = 0, addr; | 343 | int i = 0, addr; | |
354 | u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3]; | 344 | u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3]; | |
355 | int error, error2; | 345 | int error, error2; | |
356 | 346 | |||
357 | /* | 347 | /* | |
358 | * Convert our time to bcd values | 348 | * Convert our time to bcd values | |
359 | */ | 349 | */ | |
360 | bcd[X1226_REG_SC - X1226_REG_RTC_BASE] = bintobcd(dt->dt_sec); | 350 | bcd[X1226_REG_SC - X1226_REG_RTC_BASE] = bintobcd(dt->dt_sec); | |
361 | bcd[X1226_REG_MN - X1226_REG_RTC_BASE] = bintobcd(dt->dt_min); | 351 | bcd[X1226_REG_MN - X1226_REG_RTC_BASE] = bintobcd(dt->dt_min); | |
362 | bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_hour) | 352 | bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_hour) | |
363 | | X1226_FLAG_HR_24H; | 353 | | X1226_FLAG_HR_24H; | |
364 | bcd[X1226_REG_DW - X1226_REG_RTC_BASE] = bintobcd(dt->dt_wday); | 354 | bcd[X1226_REG_DW - X1226_REG_RTC_BASE] = bintobcd(dt->dt_wday); | |
365 | bcd[X1226_REG_DT - X1226_REG_RTC_BASE] = bintobcd(dt->dt_day); | 355 | bcd[X1226_REG_DT - X1226_REG_RTC_BASE] = bintobcd(dt->dt_day); |