2 * Copyright (c) 2006 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/clock.h>
37 #include <sys/resource.h>
40 #include <machine/bus.h>
41 #include <machine/cpu.h>
42 #include <machine/cpufunc.h>
43 #include <machine/frame.h>
44 #include <machine/resource.h>
45 #include <machine/intr.h>
47 #include <dev/iicbus/iiconf.h>
49 #include "iicbus_if.h"
52 #define IIC_M_WR 0 /* write operation */
54 #define DS1672_ADDR 0xd0 /* slave address */
56 #define DS1672_COUNTER 0 /* counter (bytes 0-3) */
57 #define DS1672_CTRL 4 /* control (1 byte) */
58 #define DS1672_TRICKLE 5 /* trickle charger (1 byte) */
60 #define NANOSEC 1000000000
67 ds1672_probe(device_t dev)
69 /* XXX really probe? */
70 device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
75 ds1672_attach(device_t dev)
77 struct ds1672_softc *sc = device_get_softc(dev);
81 clock_register(dev, 1000);
86 ds1672_gettime(device_t dev, struct timespec *ts)
88 uint8_t addr[1] = { DS1672_COUNTER };
90 struct iic_msg msgs[2] = {
91 { DS1672_ADDR, IIC_M_WR, 1, addr },
92 { DS1672_ADDR, IIC_M_RD, 4, secs },
96 error = iicbus_transfer(dev, msgs, 2);
98 /* counter has seconds since epoch */
99 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
100 | (secs[1] << 8) | (secs[0] << 0);
101 ts->tv_nsec = NANOSEC / 2;
107 ds1672_settime(device_t dev, struct timespec *ts)
109 /* NB: register pointer precedes actual data */
110 uint8_t data[5] = { DS1672_COUNTER };
111 struct iic_msg msgs[1] = {
112 { DS1672_ADDR, IIC_M_WR, 5, data },
115 data[1] = (ts->tv_sec >> 0) & 0xff;
116 data[2] = (ts->tv_sec >> 8) & 0xff;
117 data[3] = (ts->tv_sec >> 16) & 0xff;
118 data[4] = (ts->tv_sec >> 24) & 0xff;
120 return iicbus_transfer(dev, msgs, 1);
123 static device_method_t ds1672_methods[] = {
124 DEVMETHOD(device_probe, ds1672_probe),
125 DEVMETHOD(device_attach, ds1672_attach),
127 DEVMETHOD(clock_gettime, ds1672_gettime),
128 DEVMETHOD(clock_settime, ds1672_settime),
133 static driver_t ds1672_driver = {
136 sizeof(struct ds1672_softc),
138 static devclass_t ds1672_devclass;
140 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
141 MODULE_VERSION(ds1672, 1);
142 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);