1 /* $NetBSD: m41t00.c,v 1.15 2008/06/08 03:49:26 tsutsui Exp $ */
4 * Copyright (c) 2003 Wasabi Systems, Inc.
7 * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
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.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: m41t00.c,v 1.15 2008/06/08 03:49:26 tsutsui Exp $");
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>
49 #include <sys/event.h>
53 #include <dev/clock_subr.h>
55 #include <dev/i2c/i2cvar.h>
56 #include <dev/i2c/m41t00reg.h>
63 struct todr_chip_handle sc_todr
;
66 static int m41t00_match(device_t
, cfdata_t
, void *);
67 static void m41t00_attach(device_t
, device_t
, void *);
69 CFATTACH_DECL_NEW(m41trtc
, sizeof(struct m41t00_softc
),
70 m41t00_match
, m41t00_attach
, NULL
, NULL
);
71 extern struct cfdriver m41trtc_cd
;
73 dev_type_open(m41t00_open
);
74 dev_type_close(m41t00_close
);
75 dev_type_read(m41t00_read
);
76 dev_type_write(m41t00_write
);
78 const struct cdevsw m41t00_cdevsw
= {
79 m41t00_open
, m41t00_close
, m41t00_read
, m41t00_write
, noioctl
,
80 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_OTHER
83 static int m41t00_clock_read(struct m41t00_softc
*, struct clock_ymdhms
*);
84 static int m41t00_clock_write(struct m41t00_softc
*, struct clock_ymdhms
*);
85 static int m41t00_gettime(struct todr_chip_handle
*, struct timeval
*);
86 static int m41t00_settime(struct todr_chip_handle
*, struct timeval
*);
89 m41t00_match(device_t parent
, cfdata_t cf
, void *aux
)
91 struct i2c_attach_args
*ia
= aux
;
93 if (ia
->ia_addr
== M41T00_ADDR
) {
101 m41t00_attach(device_t parent
, device_t self
, void *aux
)
103 struct m41t00_softc
*sc
= device_private(self
);
104 struct i2c_attach_args
*ia
= aux
;
106 sc
->sc_tag
= ia
->ia_tag
;
107 sc
->sc_address
= ia
->ia_addr
;
110 aprint_naive(": Real-time Clock\n");
111 aprint_normal(": M41T00 Real-time Clock\n");
114 sc
->sc_todr
.cookie
= sc
;
115 sc
->sc_todr
.todr_gettime
= m41t00_gettime
;
116 sc
->sc_todr
.todr_settime
= m41t00_settime
;
117 sc
->sc_todr
.todr_setwen
= NULL
;
119 todr_attach(&sc
->sc_todr
);
124 m41t00_open(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
126 struct m41t00_softc
*sc
;
128 if ((sc
= device_lookup_private(&m41trtc_cd
, minor(dev
))) == NULL
)
142 m41t00_close(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
144 struct m41t00_softc
*sc
;
146 if ((sc
= device_lookup_private(&m41trtc_cd
, minor(dev
))) == NULL
)
155 m41t00_read(dev_t dev
, struct uio
*uio
, int flags
)
157 struct m41t00_softc
*sc
;
158 u_int8_t ch
, cmdbuf
[1];
161 if ((sc
= device_lookup_private(&m41trtc_cd
, minor(dev
))) == NULL
)
164 if (uio
->uio_offset
>= M41T00_NBYTES
)
167 if ((error
= iic_acquire_bus(sc
->sc_tag
, 0)) != 0)
170 while (uio
->uio_resid
&& uio
->uio_offset
< M41T00_NBYTES
) {
171 a
= (int)uio
->uio_offset
;
173 if ((error
= iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
174 sc
->sc_address
, cmdbuf
, 1,
176 iic_release_bus(sc
->sc_tag
, 0);
177 aprint_error_dev(sc
->sc_dev
,
178 "m41t00_read: read failed at 0x%x\n", a
);
181 if ((error
= uiomove(&ch
, 1, uio
)) != 0) {
182 iic_release_bus(sc
->sc_tag
, 0);
187 iic_release_bus(sc
->sc_tag
, 0);
194 m41t00_write(dev_t dev
, struct uio
*uio
, int flags
)
196 struct m41t00_softc
*sc
;
200 if ((sc
= device_lookup_private(&m41trtc_cd
, minor(dev
))) == NULL
)
203 if (uio
->uio_offset
>= M41T00_NBYTES
)
206 if ((error
= iic_acquire_bus(sc
->sc_tag
, 0)) != 0)
209 while (uio
->uio_resid
&& uio
->uio_offset
< M41T00_NBYTES
) {
210 a
= (int)uio
->uio_offset
;
213 if ((error
= uiomove(&cmdbuf
[1], 1, uio
)) != 0)
216 if ((error
= iic_exec(sc
->sc_tag
, I2C_OP_WRITE_WITH_STOP
,
218 cmdbuf
, 1, &cmdbuf
[1], 1, 0)) != 0) {
219 aprint_error_dev(sc
->sc_dev
,
220 "m41t00_write: write failed at 0x%x\n", a
);
225 iic_release_bus(sc
->sc_tag
, 0);
231 m41t00_gettime(struct todr_chip_handle
*ch
, struct timeval
*tv
)
233 struct m41t00_softc
*sc
= ch
->cookie
;
234 struct clock_ymdhms dt
;
236 if (m41t00_clock_read(sc
, &dt
) == 0)
239 tv
->tv_sec
= clock_ymdhms_to_secs(&dt
);
246 m41t00_settime(struct todr_chip_handle
*ch
, struct timeval
*tv
)
248 struct m41t00_softc
*sc
= ch
->cookie
;
249 struct clock_ymdhms dt
;
251 clock_secs_to_ymdhms(tv
->tv_sec
, &dt
);
253 if (m41t00_clock_write(sc
, &dt
) == 0)
259 static int m41t00_rtc_offset
[] = {
270 m41t00_clock_read(struct m41t00_softc
*sc
, struct clock_ymdhms
*dt
)
272 u_int8_t bcd
[M41T00_NBYTES
], cmdbuf
[1];
275 if (iic_acquire_bus(sc
->sc_tag
, I2C_F_POLL
)) {
276 aprint_error_dev(sc
->sc_dev
,
277 "m41t00_clock_read: failed to acquire I2C bus\n");
281 /* Read each timekeeping register in order. */
282 n
= sizeof(m41t00_rtc_offset
) / sizeof(m41t00_rtc_offset
[0]);
283 for (i
= 0; i
< n
; i
++) {
284 cmdbuf
[0] = m41t00_rtc_offset
[i
];
286 if (iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
287 sc
->sc_address
, cmdbuf
, 1,
288 &bcd
[i
], 1, I2C_F_POLL
)) {
289 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
290 aprint_error_dev(sc
->sc_dev
,
291 "m41t00_clock_read: failed to read rtc "
293 m41t00_rtc_offset
[i
]);
299 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
302 * Convert the M41T00's register values into something useable
304 dt
->dt_sec
= FROMBCD(bcd
[M41T00_SEC
] & M41T00_SEC_MASK
);
305 dt
->dt_min
= FROMBCD(bcd
[M41T00_MIN
] & M41T00_MIN_MASK
);
306 dt
->dt_hour
= FROMBCD(bcd
[M41T00_CENHR
] & M41T00_HOUR_MASK
);
307 dt
->dt_day
= FROMBCD(bcd
[M41T00_DATE
] & M41T00_DATE_MASK
);
308 dt
->dt_wday
= FROMBCD(bcd
[M41T00_DAY
] & M41T00_DAY_MASK
);
309 dt
->dt_mon
= FROMBCD(bcd
[M41T00_MONTH
] & M41T00_MONTH_MASK
);
310 dt
->dt_year
= FROMBCD(bcd
[M41T00_YEAR
] & M41T00_YEAR_MASK
);
313 * Since the m41t00 just stores 00-99, and this is 2003 as I write
314 * this comment, use 2000 as a base year
322 m41t00_clock_write(struct m41t00_softc
*sc
, struct clock_ymdhms
*dt
)
324 uint8_t bcd
[M41T00_DATE_BYTES
], cmdbuf
[2];
325 uint8_t init_seconds
, final_seconds
;
329 * Convert our time representation into something the MAX6900
332 bcd
[M41T00_SEC
] = TOBCD(dt
->dt_sec
);
333 bcd
[M41T00_MIN
] = TOBCD(dt
->dt_min
);
334 bcd
[M41T00_CENHR
] = TOBCD(dt
->dt_hour
);
335 bcd
[M41T00_DATE
] = TOBCD(dt
->dt_day
);
336 bcd
[M41T00_DAY
] = TOBCD(dt
->dt_wday
);
337 bcd
[M41T00_MONTH
] = TOBCD(dt
->dt_mon
);
338 bcd
[M41T00_YEAR
] = TOBCD(dt
->dt_year
% 100);
340 if (iic_acquire_bus(sc
->sc_tag
, I2C_F_POLL
)) {
341 aprint_error_dev(sc
->sc_dev
,
342 "m41t00_clock_write: failed to acquire I2C bus\n");
347 * The MAX6900 RTC manual recommends ensuring "atomicity" of
348 * a non-burst write by:
351 * - reading back SECONDS, remembering it as "initial seconds"
352 * - write the remaing RTC registers
353 * - read back SECONDS as "final seconds"
354 * - if "initial seconds" == 59, ensure "final seconds" == 59
355 * - else, ensure "final seconds" is no more than one second
356 * beyond "initial seconds".
358 * This sounds reasonable for the M41T00, too.
361 cmdbuf
[0] = M41T00_SEC
;
362 if (iic_exec(sc
->sc_tag
, I2C_OP_WRITE_WITH_STOP
, sc
->sc_address
,
363 cmdbuf
, 1, &bcd
[M41T00_SEC
], 1, I2C_F_POLL
)) {
364 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
365 aprint_error_dev(sc
->sc_dev
,
366 "m41t00_clock_write: failed to write SECONDS\n");
370 cmdbuf
[0] = M41T00_SEC
;
371 if (iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
, sc
->sc_address
,
372 cmdbuf
, 1, &init_seconds
, 1, I2C_F_POLL
)) {
373 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
374 aprint_error_dev(sc
->sc_dev
,
375 "m41t00_clock_write: failed to read "
376 "INITIAL SECONDS\n");
379 init_seconds
= FROMBCD(init_seconds
& M41T00_SEC_MASK
);
381 for (i
= 1; i
< M41T00_DATE_BYTES
; i
++) {
382 cmdbuf
[0] = m41t00_rtc_offset
[i
];
383 if (iic_exec(sc
->sc_tag
,
384 I2C_OP_WRITE_WITH_STOP
, sc
->sc_address
,
385 cmdbuf
, 1, &bcd
[i
], 1, I2C_F_POLL
)) {
386 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
387 aprint_error_dev(sc
->sc_dev
,
388 "m41t00_clock_write: failed to write rtc "
390 m41t00_rtc_offset
[i
]);
395 cmdbuf
[0] = M41T00_SEC
;
396 if (iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
, sc
->sc_address
,
397 cmdbuf
, 1, &final_seconds
, 1, I2C_F_POLL
)) {
398 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
399 aprint_error_dev(sc
->sc_dev
,
400 "m41t00_clock_write: failed to read "
404 final_seconds
= FROMBCD(final_seconds
& M41T00_SEC_MASK
);
406 if ((init_seconds
!= final_seconds
) &&
407 (((init_seconds
+ 1) % 60) != final_seconds
)) {
409 printf("%s: m41t00_clock_write: init %d, final %d, try again\n",
410 device_xname(sc
->sc_dev
), init_seconds
, final_seconds
);
415 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);