r2025.c revision 1.2.8.3 1 1.2.8.3 yamt /* $NetBSD: r2025.c,v 1.2.8.3 2006/12/30 20:48:00 yamt Exp $ */
2 1.2.8.2 yamt
3 1.2.8.2 yamt /*-
4 1.2.8.2 yamt * Copyright (c) 2006 Shigeyuki Fukushima.
5 1.2.8.2 yamt * All rights reserved.
6 1.2.8.2 yamt *
7 1.2.8.2 yamt * Written by Shigeyuki Fukushima.
8 1.2.8.2 yamt *
9 1.2.8.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.2.8.2 yamt * modification, are permitted provided that the following conditions
11 1.2.8.2 yamt * are met:
12 1.2.8.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.2.8.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.2.8.2 yamt * 2. Redistributions in binary form must reproduce the above
15 1.2.8.2 yamt * copyright notice, this list of conditions and the following
16 1.2.8.2 yamt * disclaimer in the documentation and/or other materials provided
17 1.2.8.2 yamt * with the distribution.
18 1.2.8.2 yamt * 3. The name of the author may not be used to endorse or promote
19 1.2.8.2 yamt * products derived from this software without specific prior
20 1.2.8.2 yamt * written permission.
21 1.2.8.2 yamt *
22 1.2.8.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 1.2.8.2 yamt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.2.8.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.8.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 1.2.8.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2.8.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 1.2.8.2 yamt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.2.8.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.2.8.2 yamt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 1.2.8.2 yamt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 1.2.8.2 yamt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.2.8.2 yamt */
34 1.2.8.2 yamt
35 1.2.8.2 yamt #include <sys/cdefs.h>
36 1.2.8.3 yamt __KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.2.8.3 2006/12/30 20:48:00 yamt Exp $");
37 1.2.8.2 yamt
38 1.2.8.2 yamt #include <sys/param.h>
39 1.2.8.2 yamt #include <sys/systm.h>
40 1.2.8.2 yamt #include <sys/device.h>
41 1.2.8.2 yamt #include <sys/kernel.h>
42 1.2.8.2 yamt #include <sys/fcntl.h>
43 1.2.8.2 yamt #include <sys/uio.h>
44 1.2.8.2 yamt #include <sys/conf.h>
45 1.2.8.2 yamt #include <sys/event.h>
46 1.2.8.2 yamt
47 1.2.8.2 yamt #include <dev/clock_subr.h>
48 1.2.8.2 yamt
49 1.2.8.2 yamt #include <dev/i2c/i2cvar.h>
50 1.2.8.2 yamt #include <dev/i2c/r2025reg.h>
51 1.2.8.2 yamt
52 1.2.8.2 yamt struct r2025rtc_softc {
53 1.2.8.2 yamt struct device sc_dev;
54 1.2.8.2 yamt i2c_tag_t sc_tag;
55 1.2.8.2 yamt int sc_address;
56 1.2.8.2 yamt int sc_open;
57 1.2.8.2 yamt struct todr_chip_handle sc_todr;
58 1.2.8.2 yamt };
59 1.2.8.2 yamt
60 1.2.8.2 yamt static void r2025rtc_attach(struct device *, struct device *, void *);
61 1.2.8.2 yamt static int r2025rtc_match(struct device *, struct cfdata *, void *);
62 1.2.8.2 yamt
63 1.2.8.2 yamt CFATTACH_DECL(r2025rtc, sizeof(struct r2025rtc_softc),
64 1.2.8.2 yamt r2025rtc_match, r2025rtc_attach, NULL, NULL);
65 1.2.8.2 yamt
66 1.2.8.2 yamt static int r2025rtc_gettime(struct todr_chip_handle *,
67 1.2.8.2 yamt volatile struct timeval *);
68 1.2.8.2 yamt static int r2025rtc_settime(struct todr_chip_handle *,
69 1.2.8.2 yamt volatile struct timeval *);
70 1.2.8.2 yamt static int r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int);
71 1.2.8.2 yamt static int r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int);
72 1.2.8.2 yamt
73 1.2.8.2 yamt
74 1.2.8.2 yamt static int
75 1.2.8.2 yamt r2025rtc_match(struct device *parent, struct cfdata *cf, void *arg)
76 1.2.8.2 yamt {
77 1.2.8.2 yamt struct i2c_attach_args *ia = arg;
78 1.2.8.2 yamt
79 1.2.8.2 yamt /* match only R2025 RTC devices */
80 1.2.8.2 yamt if (ia->ia_addr == R2025_ADDR)
81 1.2.8.2 yamt return 1;
82 1.2.8.2 yamt
83 1.2.8.2 yamt return 0;
84 1.2.8.2 yamt }
85 1.2.8.2 yamt
86 1.2.8.2 yamt static void
87 1.2.8.2 yamt r2025rtc_attach(struct device *parent, struct device *self, void *arg)
88 1.2.8.2 yamt {
89 1.2.8.2 yamt struct r2025rtc_softc *sc = device_private(self);
90 1.2.8.2 yamt struct i2c_attach_args *ia = arg;
91 1.2.8.2 yamt
92 1.2.8.2 yamt aprint_normal(": RICOH R2025S/D Real-time Clock\n");
93 1.2.8.2 yamt
94 1.2.8.2 yamt sc->sc_tag = ia->ia_tag;
95 1.2.8.2 yamt sc->sc_address = ia->ia_addr;
96 1.2.8.2 yamt sc->sc_open = 0;
97 1.2.8.2 yamt sc->sc_todr.cookie = sc;
98 1.2.8.2 yamt sc->sc_todr.todr_gettime = r2025rtc_gettime;
99 1.2.8.2 yamt sc->sc_todr.todr_settime = r2025rtc_settime;
100 1.2.8.2 yamt sc->sc_todr.todr_setwen = NULL;
101 1.2.8.2 yamt
102 1.2.8.2 yamt todr_attach(&sc->sc_todr);
103 1.2.8.2 yamt }
104 1.2.8.2 yamt
105 1.2.8.2 yamt static int
106 1.2.8.2 yamt r2025rtc_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv)
107 1.2.8.2 yamt {
108 1.2.8.2 yamt struct r2025rtc_softc *sc = ch->cookie;
109 1.2.8.2 yamt struct clock_ymdhms dt;
110 1.2.8.2 yamt uint8_t rctrl;
111 1.2.8.2 yamt uint8_t bcd[R2025_CLK_SIZE];
112 1.2.8.2 yamt int hour;
113 1.2.8.2 yamt
114 1.2.8.2 yamt memset(&dt, 0, sizeof(dt));
115 1.2.8.2 yamt
116 1.2.8.2 yamt if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
117 1.2.8.2 yamt printf("%s: r2025rtc_gettime: failed to read registers.\n",
118 1.2.8.2 yamt sc->sc_dev.dv_xname);
119 1.2.8.2 yamt return -1;
120 1.2.8.2 yamt }
121 1.2.8.2 yamt
122 1.2.8.2 yamt if (r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0], R2025_CLK_SIZE)
123 1.2.8.2 yamt != 0) {
124 1.2.8.2 yamt printf("%s: r2025rtc_gettime: failed to read registers.\n",
125 1.2.8.2 yamt sc->sc_dev.dv_xname);
126 1.2.8.2 yamt return -1;
127 1.2.8.2 yamt }
128 1.2.8.2 yamt
129 1.2.8.2 yamt dt.dt_sec = FROMBCD(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK);
130 1.2.8.2 yamt dt.dt_min = FROMBCD(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK);
131 1.2.8.2 yamt hour = FROMBCD(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK);
132 1.2.8.2 yamt if (rctrl & R2025_REG_CTRL1_H1224) {
133 1.2.8.2 yamt dt.dt_hour = hour;
134 1.2.8.2 yamt } else {
135 1.2.8.2 yamt if (hour == 12) {
136 1.2.8.2 yamt dt.dt_hour = 0;
137 1.2.8.2 yamt } else if (hour == 32) {
138 1.2.8.2 yamt dt.dt_hour = 12;
139 1.2.8.2 yamt } else if (hour > 13) {
140 1.2.8.2 yamt dt.dt_hour = (hour - 8);
141 1.2.8.2 yamt } else { /* (hour < 12) */
142 1.2.8.2 yamt dt.dt_hour = hour;
143 1.2.8.2 yamt }
144 1.2.8.2 yamt }
145 1.2.8.2 yamt dt.dt_wday = FROMBCD(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK);
146 1.2.8.2 yamt dt.dt_day = FROMBCD(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK);
147 1.2.8.2 yamt dt.dt_mon = FROMBCD(bcd[R2025_REG_MON] & R2025_REG_MON_MASK);
148 1.2.8.2 yamt dt.dt_year = FROMBCD(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK)
149 1.2.8.2 yamt + ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900);
150 1.2.8.2 yamt
151 1.2.8.2 yamt tv->tv_sec = clock_ymdhms_to_secs(&dt);
152 1.2.8.2 yamt tv->tv_usec = 0;
153 1.2.8.2 yamt
154 1.2.8.2 yamt return 0;
155 1.2.8.2 yamt }
156 1.2.8.2 yamt
157 1.2.8.2 yamt static int
158 1.2.8.2 yamt r2025rtc_settime(struct todr_chip_handle *ch, volatile struct timeval *tv)
159 1.2.8.2 yamt {
160 1.2.8.2 yamt struct r2025rtc_softc *sc = ch->cookie;
161 1.2.8.2 yamt struct clock_ymdhms dt;
162 1.2.8.2 yamt uint8_t rctrl;
163 1.2.8.2 yamt uint8_t bcd[R2025_CLK_SIZE];
164 1.2.8.2 yamt
165 1.2.8.2 yamt clock_secs_to_ymdhms(tv->tv_sec, &dt);
166 1.2.8.2 yamt
167 1.2.8.2 yamt /* Y3K problem */
168 1.2.8.2 yamt if (dt.dt_year >= 3000) {
169 1.2.8.2 yamt printf("%s: r2025rtc_settime: "
170 1.2.8.2 yamt "RTC does not support year 3000 or over.\n",
171 1.2.8.2 yamt sc->sc_dev.dv_xname);
172 1.2.8.2 yamt return -1;
173 1.2.8.2 yamt }
174 1.2.8.2 yamt
175 1.2.8.2 yamt if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
176 1.2.8.2 yamt printf("%s: r2025rtc_settime: failed to read register.\n",
177 1.2.8.2 yamt sc->sc_dev.dv_xname);
178 1.2.8.2 yamt return -1;
179 1.2.8.2 yamt }
180 1.2.8.2 yamt rctrl |= R2025_REG_CTRL1_H1224;
181 1.2.8.2 yamt
182 1.2.8.2 yamt /* setup registers 0x00-0x06 (7 byte) */
183 1.2.8.2 yamt bcd[R2025_REG_SEC] = TOBCD(dt.dt_sec) & R2025_REG_SEC_MASK;
184 1.2.8.2 yamt bcd[R2025_REG_MIN] = TOBCD(dt.dt_min) & R2025_REG_MIN_MASK;
185 1.2.8.2 yamt bcd[R2025_REG_HOUR] = TOBCD(dt.dt_hour) & R2025_REG_HOUR_MASK;
186 1.2.8.2 yamt bcd[R2025_REG_WDAY] = TOBCD(dt.dt_wday) & R2025_REG_WDAY_MASK;
187 1.2.8.2 yamt bcd[R2025_REG_DAY] = TOBCD(dt.dt_day) & R2025_REG_DAY_MASK;
188 1.2.8.2 yamt bcd[R2025_REG_MON] = (TOBCD(dt.dt_mon) & R2025_REG_MON_MASK)
189 1.2.8.2 yamt | ((dt.dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0);
190 1.2.8.2 yamt bcd[R2025_REG_YEAR] = TOBCD(dt.dt_year % 100) & R2025_REG_YEAR_MASK;
191 1.2.8.2 yamt
192 1.2.8.2 yamt /* Write RTC register */
193 1.2.8.2 yamt if (r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
194 1.2.8.2 yamt printf("%s: r2025rtc_settime: failed to write registers.\n",
195 1.2.8.2 yamt sc->sc_dev.dv_xname);
196 1.2.8.2 yamt return -1;
197 1.2.8.2 yamt }
198 1.2.8.2 yamt if (r2025rtc_reg_write(sc, R2025_REG_SEC, bcd, R2025_CLK_SIZE) != 0) {
199 1.2.8.2 yamt printf("%s: r2025rtc_settime: failed to write registers.\n",
200 1.2.8.2 yamt sc->sc_dev.dv_xname);
201 1.2.8.2 yamt return -1;
202 1.2.8.2 yamt }
203 1.2.8.2 yamt
204 1.2.8.2 yamt return 0;
205 1.2.8.2 yamt }
206 1.2.8.2 yamt
207 1.2.8.2 yamt static int
208 1.2.8.2 yamt r2025rtc_reg_write(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
209 1.2.8.2 yamt {
210 1.2.8.2 yamt int i;
211 1.2.8.2 yamt uint8_t buf[1];
212 1.2.8.2 yamt uint8_t cmdbuf[1];
213 1.2.8.2 yamt
214 1.2.8.2 yamt if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
215 1.2.8.2 yamt printf("%s: r2025rtc_clock_write: failed to acquire I2C bus\n",
216 1.2.8.2 yamt sc->sc_dev.dv_xname);
217 1.2.8.2 yamt return -1;
218 1.2.8.2 yamt }
219 1.2.8.2 yamt
220 1.2.8.2 yamt for (i = 0 ; i < len ; i++) {
221 1.2.8.2 yamt cmdbuf[0] = (((reg + i) << 4) & 0xf0);
222 1.2.8.2 yamt buf[0] = val[i];
223 1.2.8.2 yamt if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
224 1.2.8.2 yamt cmdbuf, 1, buf, 1, I2C_F_POLL)) {
225 1.2.8.2 yamt iic_release_bus(sc->sc_tag, I2C_F_POLL);
226 1.2.8.2 yamt printf("%s: r2025rtc_reg_write: "
227 1.2.8.2 yamt "failed to write registers\n",
228 1.2.8.2 yamt sc->sc_dev.dv_xname);
229 1.2.8.2 yamt return -1;
230 1.2.8.2 yamt }
231 1.2.8.2 yamt }
232 1.2.8.2 yamt
233 1.2.8.2 yamt iic_release_bus(sc->sc_tag, I2C_F_POLL);
234 1.2.8.2 yamt
235 1.2.8.2 yamt return 0;
236 1.2.8.2 yamt }
237 1.2.8.2 yamt
238 1.2.8.2 yamt static int
239 1.2.8.2 yamt r2025rtc_reg_read(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
240 1.2.8.2 yamt {
241 1.2.8.2 yamt int i;
242 1.2.8.2 yamt uint8_t buf[1];
243 1.2.8.2 yamt uint8_t cmdbuf[1];
244 1.2.8.2 yamt
245 1.2.8.2 yamt if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
246 1.2.8.2 yamt printf("%s: r2025rtc_clock_read: failed to acquire I2C bus\n",
247 1.2.8.2 yamt sc->sc_dev.dv_xname);
248 1.2.8.2 yamt return -1;
249 1.2.8.2 yamt }
250 1.2.8.2 yamt
251 1.2.8.2 yamt for (i = 0 ; i < len ; i++) {
252 1.2.8.2 yamt cmdbuf[0] = (((reg + i) << 4) & 0xf0);
253 1.2.8.2 yamt buf[0] = 0;
254 1.2.8.2 yamt if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
255 1.2.8.2 yamt cmdbuf, 1, buf, 1, I2C_F_POLL)) {
256 1.2.8.2 yamt iic_release_bus(sc->sc_tag, I2C_F_POLL);
257 1.2.8.2 yamt printf("%s: r2025rtc_reg_read: "
258 1.2.8.2 yamt "failed to write registers\n",
259 1.2.8.2 yamt sc->sc_dev.dv_xname);
260 1.2.8.2 yamt return -1;
261 1.2.8.2 yamt }
262 1.2.8.2 yamt
263 1.2.8.2 yamt *(val + i) = buf[0];
264 1.2.8.2 yamt }
265 1.2.8.2 yamt
266 1.2.8.2 yamt iic_release_bus(sc->sc_tag, I2C_F_POLL);
267 1.2.8.2 yamt
268 1.2.8.2 yamt return 0;
269 1.2.8.2 yamt }
270