2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Sam Leffler. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/clock.h>
39 #include <sys/resource.h>
42 #include <dev/iicbus/iiconf.h>
44 #include "iicbus_if.h"
47 #define IIC_M_WR 0 /* write operation */
49 #define DS1672_ADDR 0xd0 /* slave address */
51 #define DS1672_COUNTER 0 /* counter (bytes 0-3) */
52 #define DS1672_CTRL 4 /* control (1 byte) */
53 #define DS1672_TRICKLE 5 /* trickle charger (1 byte) */
55 #define DS1672_CTRL_EOSC (1 << 7) /* Stop/start flag. */
57 #define MAX_IIC_DATA_SIZE 4
64 ds1672_probe(device_t dev)
66 /* XXX really probe? */
67 device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
68 return (BUS_PROBE_NOWILDCARD);
72 ds1672_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
74 struct iic_msg msgs[2] = {
75 { DS1672_ADDR, IIC_M_WR, 1, &addr },
76 { DS1672_ADDR, IIC_M_RD, size, data }
79 return (iicbus_transfer(dev, msgs, 2));
83 ds1672_write(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
85 uint8_t buffer[MAX_IIC_DATA_SIZE + 1];
86 struct iic_msg msgs[1] = {
87 { DS1672_ADDR, IIC_M_WR, size + 1, buffer },
90 if (size > MAX_IIC_DATA_SIZE)
92 /* NB: register pointer precedes actual data */
94 memcpy(buffer + 1, data, size);
95 return (iicbus_transfer(dev, msgs, 1));
99 ds1672_init(device_t dev)
104 error = ds1672_read(dev, DS1672_CTRL, &ctrl, 1);
109 * Check if oscialltor is not runned.
111 if (ctrl & DS1672_CTRL_EOSC) {
112 device_printf(dev, "RTC oscillator was stopped. Check system"
113 " time and RTC battery.\n");
114 ctrl &= ~DS1672_CTRL_EOSC; /* Start oscillator. */
115 error = ds1672_write(dev, DS1672_CTRL, &ctrl, 1);
121 ds1672_detach(device_t dev)
124 clock_unregister(dev);
129 ds1672_attach(device_t dev)
131 struct ds1672_softc *sc = device_get_softc(dev);
135 error = ds1672_init(dev);
138 clock_register(dev, 1000);
143 ds1672_gettime(device_t dev, struct timespec *ts)
148 error = ds1672_read(dev, DS1672_COUNTER, secs, 4);
150 /* counter has seconds since epoch */
151 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
152 | (secs[1] << 8) | (secs[0] << 0);
155 clock_dbgprint_ts(dev, CLOCK_DBG_READ, ts);
160 ds1672_settime(device_t dev, struct timespec *ts)
164 data[0] = (ts->tv_sec >> 0) & 0xff;
165 data[1] = (ts->tv_sec >> 8) & 0xff;
166 data[2] = (ts->tv_sec >> 16) & 0xff;
167 data[3] = (ts->tv_sec >> 24) & 0xff;
170 clock_dbgprint_ts(dev, CLOCK_DBG_WRITE, ts);
171 return (ds1672_write(dev, DS1672_COUNTER, data, 4));
174 static device_method_t ds1672_methods[] = {
175 DEVMETHOD(device_probe, ds1672_probe),
176 DEVMETHOD(device_attach, ds1672_attach),
177 DEVMETHOD(device_detach, ds1672_detach),
179 DEVMETHOD(clock_gettime, ds1672_gettime),
180 DEVMETHOD(clock_settime, ds1672_settime),
185 static driver_t ds1672_driver = {
188 sizeof(struct ds1672_softc),
190 static devclass_t ds1672_devclass;
192 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
193 MODULE_VERSION(ds1672, 1);
194 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);