1 /*-
2 * Copyright (c) 2006 Sam Leffler. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 /*
28 * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
29 */
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>
35 #include <sys/time.h>
36 #include <sys/bus.h>
37 #include <sys/resource.h>
38 #include <sys/rman.h>
39
40 #include <dev/iicbus/iiconf.h>
41
42 #include "iicbus_if.h"
43 #include "clock_if.h"
44
45 #define IIC_M_WR 0 /* write operation */
46
47 #define DS1672_ADDR 0xd0 /* slave address */
48
49 #define DS1672_COUNTER 0 /* counter (bytes 0-3) */
50 #define DS1672_CTRL 4 /* control (1 byte) */
51 #define DS1672_TRICKLE 5 /* trickle charger (1 byte) */
52
53 #define DS1672_CTRL_EOSC (1 << 7) /* Stop/start flag. */
54
55 #define NANOSEC 1000000000
56
57 #define MAX_IIC_DATA_SIZE 4
58
59 struct ds1672_softc {
60 device_t sc_dev;
61 };
62
63 static int
64 ds1672_probe(device_t dev)
65 {
66 /* XXX really probe? */
67 device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
68 return (BUS_PROBE_NOWILDCARD);
69 }
70
71 static int
72 ds1672_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
73 {
74 struct iic_msg msgs[2] = {
75 { DS1672_ADDR, IIC_M_WR, 1, &addr },
76 { DS1672_ADDR, IIC_M_RD, size, data }
77 };
78
79 return (iicbus_transfer(dev, msgs, 2));
80 }
81
82 static int
83 ds1672_write(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
84 {
85 uint8_t buffer[MAX_IIC_DATA_SIZE + 1];
86 struct iic_msg msgs[1] = {
87 { DS1672_ADDR, IIC_M_WR, size + 1, buffer },
88 };
89
90 if (size > MAX_IIC_DATA_SIZE)
91 return (ENOMEM);
92 /* NB: register pointer precedes actual data */
93 buffer[0] = addr;
94 memcpy(buffer + 1, data, size);
95 return (iicbus_transfer(dev, msgs, 1));
96 }
97
98 static int
99 ds1672_init(device_t dev)
100 {
101 uint8_t ctrl;
102 int error;
103
104 error = ds1672_read(dev, DS1672_CTRL, &ctrl, 1);
105 if (error)
106 return (error);
107
108 /*
109 * Check if oscialltor is not runned.
110 */
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);
116 }
117 return (error);
118 }
119
120 static int
121 ds1672_attach(device_t dev)
122 {
123 struct ds1672_softc *sc = device_get_softc(dev);
124 int error;
125
126 sc->sc_dev = dev;
127 error = ds1672_init(dev);
128 if (error)
129 return (error);
130 clock_register(dev, 1000);
131 return (0);
132 }
133
134 static int
135 ds1672_gettime(device_t dev, struct timespec *ts)
136 {
137 uint8_t secs[4];
138 int error;
139
140 error = ds1672_read(dev, DS1672_COUNTER, secs, 4);
141 if (error == 0) {
142 /* counter has seconds since epoch */
143 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
144 | (secs[1] << 8) | (secs[0] << 0);
145 ts->tv_nsec = NANOSEC / 2;
146 }
147 return (error);
148 }
149
150 static int
151 ds1672_settime(device_t dev, struct timespec *ts)
152 {
153 uint8_t data[4];
154
155 data[0] = (ts->tv_sec >> 0) & 0xff;
156 data[1] = (ts->tv_sec >> 8) & 0xff;
157 data[2] = (ts->tv_sec >> 16) & 0xff;
158 data[3] = (ts->tv_sec >> 24) & 0xff;
159
160 return (ds1672_write(dev, DS1672_COUNTER, data, 4));
161 }
162
163 static device_method_t ds1672_methods[] = {
164 DEVMETHOD(device_probe, ds1672_probe),
165 DEVMETHOD(device_attach, ds1672_attach),
166
167 DEVMETHOD(clock_gettime, ds1672_gettime),
168 DEVMETHOD(clock_settime, ds1672_settime),
169
170 {0, 0},
171 };
172
173 static driver_t ds1672_driver = {
174 "ds1672_rtc",
175 ds1672_methods,
176 sizeof(struct ds1672_softc),
177 };
178 static devclass_t ds1672_devclass;
179
180 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
181 MODULE_VERSION(ds1672, 1);
182 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);
Cache object: 4b1ebba720d09bbb105a4c3ab14d3a4c
|