x1226.c revision 1.22 1 /* $NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2003 Shigeyuki Fukushima.
5 * All rights reserved.
6 *
7 * Written by Shigeyuki Fukushima for the NetBSD Project.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Shigeyuki Fukushima.
21 * 4. The name of Shigeyuki Fukushima may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY SHIGEYUKI FUKUSHIMA ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIGEYUKI FUKUSHIMA
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/uio.h>
47 #include <sys/conf.h>
48 #include <sys/event.h>
49
50 #include <dev/clock_subr.h>
51
52 #include <dev/i2c/i2cvar.h>
53 #include <dev/i2c/x1226reg.h>
54
55 #include "ioconf.h"
56
57 struct xrtc_softc {
58 device_t sc_dev;
59 i2c_tag_t sc_tag;
60 int sc_address;
61 int sc_open;
62 struct todr_chip_handle sc_todr;
63 };
64
65 static void xrtc_attach(device_t, device_t, void *);
66 static int xrtc_match(device_t, cfdata_t, void *);
67
68 CFATTACH_DECL_NEW(xrtc, sizeof(struct xrtc_softc),
69 xrtc_match, xrtc_attach, NULL, NULL);
70
71 dev_type_open(xrtc_open);
72 dev_type_close(xrtc_close);
73 dev_type_read(xrtc_read);
74 dev_type_write(xrtc_write);
75
76 const struct cdevsw xrtc_cdevsw = {
77 .d_open = xrtc_open,
78 .d_close = xrtc_close,
79 .d_read = xrtc_read,
80 .d_write = xrtc_write,
81 .d_ioctl = noioctl,
82 .d_stop = nostop,
83 .d_tty = notty,
84 .d_poll = nopoll,
85 .d_mmap = nommap,
86 .d_kqfilter = nokqfilter,
87 .d_discard = nodiscard,
88 .d_flag = D_OTHER
89 };
90
91 static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *);
92 static int xrtc_clock_write(struct xrtc_softc *, struct clock_ymdhms *);
93 static int xrtc_gettime(struct todr_chip_handle *, struct timeval *);
94 static int xrtc_settime(struct todr_chip_handle *, struct timeval *);
95
96 /*
97 * xrtc_match()
98 */
99 static int
100 xrtc_match(device_t parent, cfdata_t cf, void *arg)
101 {
102 struct i2c_attach_args *ia = arg;
103
104 /* match only this RTC devices */
105 if (ia->ia_addr == X1226_ADDR)
106 return (I2C_MATCH_ADDRESS_ONLY);
107
108 return (0);
109 }
110
111 /*
112 * xrtc_attach()
113 */
114 static void
115 xrtc_attach(device_t parent, device_t self, void *arg)
116 {
117 struct xrtc_softc *sc = device_private(self);
118 struct i2c_attach_args *ia = arg;
119
120 aprint_naive(": Real-time Clock/NVRAM\n");
121 aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n");
122
123 sc->sc_tag = ia->ia_tag;
124 sc->sc_address = ia->ia_addr;
125 sc->sc_dev = self;
126 sc->sc_open = 0;
127 sc->sc_todr.cookie = sc;
128 sc->sc_todr.todr_gettime = xrtc_gettime;
129 sc->sc_todr.todr_settime = xrtc_settime;
130 sc->sc_todr.todr_setwen = NULL;
131
132 todr_attach(&sc->sc_todr);
133 }
134
135
136 /*ARGSUSED*/
137 int
138 xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
139 {
140 struct xrtc_softc *sc;
141
142 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
143 return (ENXIO);
144
145 /* XXX: Locking */
146
147 if (sc->sc_open)
148 return (EBUSY);
149
150 sc->sc_open = 1;
151 return (0);
152 }
153
154 /*ARGSUSED*/
155 int
156 xrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
157 {
158 struct xrtc_softc *sc;
159
160 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
161 return (ENXIO);
162
163 sc->sc_open = 0;
164 return (0);
165 }
166
167 /*ARGSUSED*/
168 int
169 xrtc_read(dev_t dev, struct uio *uio, int flags)
170 {
171 struct xrtc_softc *sc;
172 u_int8_t ch, cmdbuf[2];
173 int addr, error;
174
175 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
176 return (ENXIO);
177
178 if (uio->uio_offset >= X1226_NVRAM_SIZE)
179 return (EINVAL);
180
181 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
182 return (error);
183
184 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
185 addr = (int)uio->uio_offset + X1226_NVRAM_START;
186 cmdbuf[0] = (addr >> 8) & 0xff;
187 cmdbuf[1] = addr & 0xff;
188 if ((error = iic_exec(sc->sc_tag,
189 I2C_OP_READ_WITH_STOP,
190 sc->sc_address, cmdbuf, 2, &ch, 1, 0)) != 0) {
191 iic_release_bus(sc->sc_tag, 0);
192 aprint_error_dev(sc->sc_dev,
193 "xrtc_read: read failed at 0x%x\n",
194 (int)uio->uio_offset);
195 return (error);
196 }
197 if ((error = uiomove(&ch, 1, uio)) != 0) {
198 iic_release_bus(sc->sc_tag, 0);
199 return (error);
200 }
201 }
202
203 iic_release_bus(sc->sc_tag, 0);
204
205 return (0);
206 }
207
208 /*ARGSUSED*/
209 int
210 xrtc_write(dev_t dev, struct uio *uio, int flags)
211 {
212 struct xrtc_softc *sc;
213 u_int8_t cmdbuf[3];
214 int addr, error;
215
216 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
217 return (ENXIO);
218
219 if (uio->uio_offset >= X1226_NVRAM_SIZE)
220 return (EINVAL);
221
222 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
223 return (error);
224
225 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
226 addr = (int)uio->uio_offset + X1226_NVRAM_START;
227 cmdbuf[0] = (addr >> 8) & 0xff;
228 cmdbuf[1] = addr & 0xff;
229 if ((error = uiomove(&cmdbuf[2], 1, uio)) != 0) {
230 break;
231 }
232 if ((error = iic_exec(sc->sc_tag,
233 uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
234 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
235 iic_release_bus(sc->sc_tag, 0);
236 aprint_error_dev(sc->sc_dev,
237 "xrtc_write: write failed at 0x%x\n",
238 (int)uio->uio_offset);
239 return (error);
240 }
241 }
242
243 iic_release_bus(sc->sc_tag, 0);
244
245 return (0);
246 }
247
248
249 static int
250 xrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
251 {
252 struct xrtc_softc *sc = ch->cookie;
253 struct clock_ymdhms dt, check;
254 int retries;
255 int error;
256
257 memset(&dt, 0, sizeof(dt));
258 memset(&check, 0, sizeof(check));
259
260 retries = 5;
261 do {
262 if ((error = xrtc_clock_read(sc, &dt)) == 0)
263 error = xrtc_clock_read(sc, &check);
264 if (error)
265 return error;
266 } while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries);
267
268 tv->tv_sec = clock_ymdhms_to_secs(&dt);
269 tv->tv_usec = 0;
270
271 return (0);
272 }
273
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)
287 {
288 int i = 0;
289 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2];
290 int error;
291
292 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
293 aprint_error_dev(sc->sc_dev,
294 "xrtc_clock_read: failed to acquire I2C bus\n");
295 return (error);
296 }
297
298 /* Read each RTC register in order */
299 for (i = 0 ; i < X1226_REG_RTC_SIZE ; i++) {
300 int addr = i + X1226_REG_RTC_BASE;
301 cmdbuf[0] = (addr >> 8) & 0xff;
302 cmdbuf[1] = addr & 0xff;
303
304 if ((error = iic_exec(sc->sc_tag,
305 I2C_OP_READ_WITH_STOP,
306 sc->sc_address, cmdbuf, 2,
307 &bcd[i], 1, 0)) != 0) {
308 iic_release_bus(sc->sc_tag, 0);
309 aprint_error_dev(sc->sc_dev,
310 "xrtc_clock_read: failed to read rtc "
311 "at 0x%x\n", i);
312 return (error);
313 }
314 }
315
316 /* Done with I2C */
317 iic_release_bus(sc->sc_tag, 0);
318
319 /*
320 * Convert the X1226's register bcd values
321 */
322 dt->dt_sec = bcdtobin(bcd[X1226_REG_SC - X1226_REG_RTC_BASE]
323 & X1226_REG_SC_MASK);
324 dt->dt_min = bcdtobin(bcd[X1226_REG_MN - X1226_REG_RTC_BASE]
325 & X1226_REG_MN_MASK);
326 if (!(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_24H)) {
327 dt->dt_hour = bcdtobin(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
328 & X1226_REG_HR12_MASK);
329 if (bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_12HPM) {
330 dt->dt_hour += 12;
331 }
332 } else {
333 dt->dt_hour = bcdtobin(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
334 & X1226_REG_HR24_MASK);
335 }
336 dt->dt_wday = bcdtobin(bcd[X1226_REG_DW - X1226_REG_RTC_BASE]
337 & X1226_REG_DT_MASK);
338 dt->dt_day = bcdtobin(bcd[X1226_REG_DT - X1226_REG_RTC_BASE]
339 & X1226_REG_DT_MASK);
340 dt->dt_mon = bcdtobin(bcd[X1226_REG_MO - X1226_REG_RTC_BASE]
341 & X1226_REG_MO_MASK);
342 dt->dt_year = bcdtobin(bcd[X1226_REG_YR - X1226_REG_RTC_BASE]
343 & X1226_REG_YR_MASK);
344 dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE]
345 & X1226_REG_Y2K_MASK) * 100;
346
347 return (0);
348 }
349
350 static int
351 xrtc_clock_write(struct xrtc_softc *sc, struct clock_ymdhms *dt)
352 {
353 int i = 0, addr;
354 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3];
355 int error, error2;
356
357 /*
358 * Convert our time to bcd values
359 */
360 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);
362 bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_hour)
363 | X1226_FLAG_HR_24H;
364 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);
366 bcd[X1226_REG_MO - X1226_REG_RTC_BASE] = bintobcd(dt->dt_mon);
367 bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year % 100);
368 bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year / 100);
369
370 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
371 aprint_error_dev(sc->sc_dev,
372 "xrtc_clock_write: failed to acquire I2C bus\n");
373 return (error);
374 }
375
376 /* Unlock register: Write Enable Latch */
377 addr = X1226_REG_SR;
378 cmdbuf[0] = ((addr >> 8) & 0xff);
379 cmdbuf[1] = (addr & 0xff);
380 cmdbuf[2] = X1226_FLAG_SR_WEL;
381 if ((error = iic_exec(sc->sc_tag,
382 I2C_OP_WRITE_WITH_STOP,
383 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
384 iic_release_bus(sc->sc_tag, 0);
385 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
386 "failed to write-unlock status register(WEL=1)\n");
387 return (error);
388 }
389
390 /* Unlock register: Register Write Enable Latch */
391 addr = X1226_REG_SR;
392 cmdbuf[0] = ((addr >> 8) & 0xff);
393 cmdbuf[1] = (addr & 0xff);
394 cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL;
395 if ((error = iic_exec(sc->sc_tag,
396 I2C_OP_WRITE_WITH_STOP,
397 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
398 iic_release_bus(sc->sc_tag, 0);
399 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
400 "failed to write-unlock status register(RWEL=1)\n");
401 return (error);
402 }
403
404 /* Write each RTC register in reverse order */
405 for (i = (X1226_REG_RTC_SIZE - 1) ; i >= 0; i--) {
406 addr = i + X1226_REG_RTC_BASE;
407 cmdbuf[0] = ((addr >> 8) & 0xff);
408 cmdbuf[1] = (addr & 0xff);
409 if ((error = iic_exec(sc->sc_tag,
410 I2C_OP_WRITE_WITH_STOP,
411 sc->sc_address, cmdbuf, 2,
412 &bcd[i], 1, 0)) != 0) {
413
414 aprint_error_dev(sc->sc_dev,
415 "xrtc_clock_write: failed to write rtc at 0x%x\n",
416 i);
417
418 goto write_lock_rtc;
419 }
420 }
421
422 write_lock_rtc:
423 /* Lock register: WEL/RWEL off */
424 addr = X1226_REG_SR;
425 cmdbuf[0] = ((addr >> 8) & 0xff);
426 cmdbuf[1] = (addr & 0xff);
427 cmdbuf[2] = 0;
428 if ((error2 = iic_exec(sc->sc_tag,
429 I2C_OP_WRITE_WITH_STOP,
430 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
431 iic_release_bus(sc->sc_tag, 0);
432 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
433 "failed to write-lock status register\n");
434 return (error ? error : error2);
435 }
436
437 iic_release_bus(sc->sc_tag, 0);
438 return (error);
439 }
440