2 * Copyright (c) 2003-2009 RMI Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 * RTC chip sitting on the I2C bus.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/clock.h>
43 #include <sys/resource.h>
46 #include <mips/include/bus.h>
47 #include <mips/include/cpu.h>
48 #include <mips/include/cpufunc.h>
49 #include <mips/include/frame.h>
50 #include <mips/include/resource.h>
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
55 #include "iicbus_if.h"
58 #define DS1374_RTC_COUNTER 0 /* counter (bytes 0-3) */
60 struct ds1374u_softc {
66 ds1374u_probe(device_t dev)
68 device_set_desc(dev, "DS1374U-33 RTC");
73 ds1374u_attach(device_t dev)
75 struct ds1374u_softc *sc = device_get_softc(dev);
78 printf("ds1374u_attach device_get_softc failed\n");
82 sc->sc_addr = iicbus_get_addr(dev);
84 clock_register(dev, 1000);
89 ds1374u_settime(device_t dev, struct timespec *ts)
91 /* NB: register pointer precedes actual data */
92 uint8_t data[5] = { DS1374_RTC_COUNTER };
93 struct ds1374u_softc *sc = device_get_softc(dev);
94 struct iic_msg msgs[1] = {
95 { sc->sc_addr, IIC_M_WR, 5, data },
98 data[1] = (ts->tv_sec >> 0) & 0xff;
99 data[2] = (ts->tv_sec >> 8) & 0xff;
100 data[3] = (ts->tv_sec >> 16) & 0xff;
101 data[4] = (ts->tv_sec >> 24) & 0xff;
103 return iicbus_transfer(dev, msgs, 1);
107 ds1374u_gettime(device_t dev, struct timespec *ts)
109 struct ds1374u_softc *sc = device_get_softc(dev);
110 uint8_t addr[1] = { DS1374_RTC_COUNTER };
112 struct iic_msg msgs[2] = {
113 { sc->sc_addr, IIC_M_WR, 1, addr },
114 { sc->sc_addr, IIC_M_RD, 4, secs },
118 error = iicbus_transfer(dev, msgs, 2);
120 /* counter has seconds since epoch */
121 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
122 | (secs[1] << 8) | (secs[0] << 0);
128 static device_method_t ds1374u_methods[] = {
129 DEVMETHOD(device_probe, ds1374u_probe),
130 DEVMETHOD(device_attach, ds1374u_attach),
132 DEVMETHOD(clock_gettime, ds1374u_gettime),
133 DEVMETHOD(clock_settime, ds1374u_settime),
138 static driver_t ds1374u_driver = {
141 sizeof(struct ds1374u_softc),
143 static devclass_t ds1374u_devclass;
145 DRIVER_MODULE(ds1374u, iicbus, ds1374u_driver, ds1374u_devclass, 0, 0);
146 MODULE_VERSION(ds1374u, 1);
147 MODULE_DEPEND(ds1374u, iicbus, 1, 1, 1);