xref: /netbsd-src/sys/dev/i2c/m41st84.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: m41st84.c,v 1.10 2007/12/11 12:09:22 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: m41st84.c,v 1.10 2007/12/11 12:09:22 lukem Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/uio.h>
47 #include <sys/conf.h>
48 #include <sys/event.h>
49 
50 #include <dev/clock_subr.h>
51 
52 #include <dev/i2c/i2cvar.h>
53 #include <dev/i2c/m41st84reg.h>
54 
55 struct strtc_softc {
56 	struct device sc_dev;
57 	i2c_tag_t sc_tag;
58 	int sc_address;
59 	int sc_open;
60 	struct todr_chip_handle sc_todr;
61 };
62 
63 static void	strtc_attach(struct device *, struct device *, void *);
64 static int	strtc_match(struct device *, struct cfdata *, void *);
65 
66 CFATTACH_DECL(strtc, sizeof(struct strtc_softc),
67     strtc_match, strtc_attach, NULL, NULL);
68 extern struct cfdriver strtc_cd;
69 
70 dev_type_open(strtc_open);
71 dev_type_close(strtc_close);
72 dev_type_read(strtc_read);
73 dev_type_write(strtc_write);
74 
75 const struct cdevsw strtc_cdevsw = {
76 	strtc_open, strtc_close, strtc_read, strtc_write, noioctl,
77 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
78 };
79 
80 static int strtc_clock_read(struct strtc_softc *, struct clock_ymdhms *);
81 static int strtc_clock_write(struct strtc_softc *, struct clock_ymdhms *);
82 static int strtc_gettime(struct todr_chip_handle *, volatile struct timeval *);
83 static int strtc_settime(struct todr_chip_handle *, volatile struct timeval *);
84 
85 static int
86 strtc_match(struct device *parent, struct cfdata *cf, void *arg)
87 {
88 	struct i2c_attach_args *ia = arg;
89 
90 	if (ia->ia_addr == M41ST84_ADDR)
91 		return (1);
92 
93 	return (0);
94 }
95 
96 static void
97 strtc_attach(struct device *parent, struct device *self, void *arg)
98 {
99 	struct strtc_softc *sc = device_private(self);
100 	struct i2c_attach_args *ia = arg;
101 
102 	aprint_naive(": Real-time Clock/NVRAM\n");
103 	aprint_normal(": M41ST84 Real-time Clock/NVRAM\n");
104 
105 	sc->sc_tag = ia->ia_tag;
106 	sc->sc_address = ia->ia_addr;
107 	sc->sc_open = 0;
108 	sc->sc_todr.cookie = sc;
109 	sc->sc_todr.todr_gettime = strtc_gettime;
110 	sc->sc_todr.todr_settime = strtc_settime;
111 	sc->sc_todr.todr_setwen = NULL;
112 
113 	todr_attach(&sc->sc_todr);
114 }
115 
116 /*ARGSUSED*/
117 int
118 strtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
119 {
120 	struct strtc_softc *sc;
121 
122 	if ((sc = device_lookup(&strtc_cd, minor(dev))) == NULL)
123 		return (ENXIO);
124 
125 	/* XXX: Locking */
126 
127 	if (sc->sc_open)
128 		return (EBUSY);
129 
130 	sc->sc_open = 1;
131 	return (0);
132 }
133 
134 /*ARGSUSED*/
135 int
136 strtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
137 {
138 	struct strtc_softc *sc;
139 
140 	if ((sc = device_lookup(&strtc_cd, minor(dev))) == NULL)
141 		return (ENXIO);
142 
143 	sc->sc_open = 0;
144 	return (0);
145 }
146 
147 /*ARGSUSED*/
148 int
149 strtc_read(dev_t dev, struct uio *uio, int flags)
150 {
151 	struct strtc_softc *sc;
152 	u_int8_t ch, cmdbuf[1];
153 	int a, error;
154 
155 	if ((sc = device_lookup(&strtc_cd, minor(dev))) == NULL)
156 		return (ENXIO);
157 
158 	if (uio->uio_offset >= M41ST84_USER_RAM_SIZE)
159 		return (EINVAL);
160 
161 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
162 		return (error);
163 
164 	while (uio->uio_resid && uio->uio_offset < M41ST84_USER_RAM_SIZE) {
165 		a = (int)uio->uio_offset;
166 		cmdbuf[0] = a + M41ST84_USER_RAM;
167 		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
168 				      sc->sc_address, cmdbuf, 1,
169 				      &ch, 1, 0)) != 0) {
170 			iic_release_bus(sc->sc_tag, 0);
171 			printf("%s: strtc_read: read failed at 0x%x\n",
172 			    sc->sc_dev.dv_xname, a);
173 			return (error);
174 		}
175 		if ((error = uiomove(&ch, 1, uio)) != 0) {
176 			iic_release_bus(sc->sc_tag, 0);
177 			return (error);
178 		}
179 	}
180 
181 	iic_release_bus(sc->sc_tag, 0);
182 
183 	return (0);
184 }
185 
186 /*ARGSUSED*/
187 int
188 strtc_write(dev_t dev, struct uio *uio, int flags)
189 {
190 	struct strtc_softc *sc;
191 	u_int8_t cmdbuf[2];
192 	int a, error;
193 
194 	if ((sc = device_lookup(&strtc_cd, minor(dev))) == NULL)
195 		return (ENXIO);
196 
197 	if (uio->uio_offset >= M41ST84_USER_RAM_SIZE)
198 		return (EINVAL);
199 
200 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
201 		return (error);
202 
203 	while (uio->uio_resid && uio->uio_offset < M41ST84_USER_RAM_SIZE) {
204 		a = (int)uio->uio_offset;
205 		cmdbuf[0] = a + M41ST84_USER_RAM;
206 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
207 			break;
208 
209 		if ((error = iic_exec(sc->sc_tag,
210 		    uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
211 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
212 			printf("%s: strtc_write: write failed at 0x%x\n",
213 			    sc->sc_dev.dv_xname, a);
214 			break;
215 		}
216 	}
217 
218 	iic_release_bus(sc->sc_tag, 0);
219 
220 	return (error);
221 }
222 
223 static int
224 strtc_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv)
225 {
226 	struct strtc_softc *sc = ch->cookie;
227 	struct clock_ymdhms dt, check;
228 	int retries;
229 
230 	memset(&dt, 0, sizeof(dt));
231 	memset(&check, 0, sizeof(check));
232 
233 	/*
234 	 * Since we don't support Burst Read, we have to read the clock twice
235 	 * until we get two consecutive identical results.
236 	 */
237 	retries = 5;
238 	do {
239 		strtc_clock_read(sc, &dt);
240 		strtc_clock_read(sc, &check);
241 	} while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries);
242 
243 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
244 	tv->tv_usec = 0;
245 
246 	return (0);
247 }
248 
249 static int
250 strtc_settime(struct todr_chip_handle *ch, volatile struct timeval *tv)
251 {
252 	struct strtc_softc *sc = ch->cookie;
253 	struct clock_ymdhms dt;
254 
255 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
256 
257 	if (strtc_clock_write(sc, &dt) == 0)
258 		return (-1);
259 
260 	return (0);
261 }
262 
263 static int
264 strtc_clock_read(struct strtc_softc *sc, struct clock_ymdhms *dt)
265 {
266 	u_int8_t bcd[M41ST84_REG_DATE_BYTES], cmdbuf[1];
267 	int i;
268 
269 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
270 		printf("%s: strtc_clock_read: failed to acquire I2C bus\n",
271 		    sc->sc_dev.dv_xname);
272 		return (0);
273 	}
274 
275 	/*
276 	 * Check for the HT bit -- if set, then clock lost power & stopped
277 	 * If that happened, then clear the bit so that the clock will have
278 	 * a chance to run again.
279 	 */
280 	cmdbuf[0] = M41ST84_REG_AL_HOUR;
281 	if (iic_exec(sc->sc_tag, I2C_OP_READ, sc->sc_address,
282 		     cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
283 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
284 		printf("%s: strtc_clock_read: failed to read HT\n",
285 		    sc->sc_dev.dv_xname);
286 		return (0);
287 	}
288 	if (cmdbuf[1] & M41ST84_AL_HOUR_HT) {
289 		cmdbuf[1] &= ~M41ST84_AL_HOUR_HT;
290 		if (iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
291 			     cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
292 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
293 			printf("%s: strtc_clock_read: failed to reset HT\n",
294 			    sc->sc_dev.dv_xname);
295 			return (0);
296 		}
297 	}
298 
299 	/* Read each RTC register in order. */
300 	for (i = M41ST84_REG_CSEC; i < M41ST84_REG_DATE_BYTES; i++) {
301 		cmdbuf[0] = i;
302 
303 		if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
304 			     sc->sc_address, cmdbuf, 1,
305 			     &bcd[i], 1, I2C_F_POLL)) {
306 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
307 			printf("%s: strtc_clock_read: failed to read rtc "
308 			    "at 0x%x\n", sc->sc_dev.dv_xname, i);
309 			return (0);
310 		}
311 	}
312 
313 	/* Done with I2C */
314 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
315 
316 	/*
317 	 * Convert the M41ST84's register values into something useable
318 	 */
319 	dt->dt_sec = FROMBCD(bcd[M41ST84_REG_SEC] & M41ST84_SEC_MASK);
320 	dt->dt_min = FROMBCD(bcd[M41ST84_REG_MIN] & M41ST84_MIN_MASK);
321 	dt->dt_hour = FROMBCD(bcd[M41ST84_REG_CENHR] & M41ST84_HOUR_MASK);
322 	dt->dt_day = FROMBCD(bcd[M41ST84_REG_DATE] & M41ST84_DATE_MASK);
323 	dt->dt_mon = FROMBCD(bcd[M41ST84_REG_MONTH] & M41ST84_MONTH_MASK);
324 
325 	/* XXX: Should be an MD way to specify EPOCH used by BIOS/Firmware */
326 	dt->dt_year = FROMBCD(bcd[M41ST84_REG_YEAR]) + POSIX_BASE_YEAR;
327 
328 	return (1);
329 }
330 
331 static int
332 strtc_clock_write(struct strtc_softc *sc, struct clock_ymdhms *dt)
333 {
334 	uint8_t bcd[M41ST84_REG_DATE_BYTES], cmdbuf[2];
335 	int i;
336 
337 	/*
338 	 * Convert our time representation into something the M41ST84
339 	 * can understand.
340 	 */
341 	bcd[M41ST84_REG_CSEC] = TOBCD(0);	/* must always write as 0 */
342 	bcd[M41ST84_REG_SEC] = TOBCD(dt->dt_sec);
343 	bcd[M41ST84_REG_MIN] = TOBCD(dt->dt_min);
344 	bcd[M41ST84_REG_CENHR] = TOBCD(dt->dt_hour);
345 	bcd[M41ST84_REG_DATE] = TOBCD(dt->dt_day);
346 	bcd[M41ST84_REG_DAY] = TOBCD(dt->dt_wday);
347 	bcd[M41ST84_REG_MONTH] = TOBCD(dt->dt_mon);
348 	bcd[M41ST84_REG_YEAR] = TOBCD((dt->dt_year - POSIX_BASE_YEAR) % 100);
349 
350 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
351 		printf("%s: strtc_clock_write: failed to acquire I2C bus\n",
352 		    sc->sc_dev.dv_xname);
353 		return (0);
354 	}
355 
356 	/* Stop the clock */
357 	cmdbuf[0] = M41ST84_REG_SEC;
358 	cmdbuf[1] = M41ST84_SEC_ST;
359 
360 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
361 		     cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
362 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
363 		printf("%s: strtc_clock_write: failed to Hold Clock\n",
364 		    sc->sc_dev.dv_xname);
365 		return (0);
366 	}
367 
368 	/*
369 	 * Check for the HT bit -- if set, then clock lost power & stopped
370 	 * If that happened, then clear the bit so that the clock will have
371 	 * a chance to run again.
372 	 */
373 	cmdbuf[0] = M41ST84_REG_AL_HOUR;
374 	if (iic_exec(sc->sc_tag, I2C_OP_READ, sc->sc_address,
375 		     cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
376 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
377 		printf("%s: strtc_clock_write: failed to read HT\n",
378 		    sc->sc_dev.dv_xname);
379 		return (0);
380 	}
381 	if (cmdbuf[1] & M41ST84_AL_HOUR_HT) {
382 		cmdbuf[1] &= ~M41ST84_AL_HOUR_HT;
383 		if (iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
384 			     cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
385 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
386 			printf("%s: strtc_clock_write: failed to reset HT\n",
387 			    sc->sc_dev.dv_xname);
388 			return (0);
389 		}
390 	}
391 
392 	/*
393 	 * Write registers in reverse order. The last write (to the Seconds
394 	 * register) will undo the Clock Hold, above.
395 	 */
396 	for (i = M41ST84_REG_DATE_BYTES - 1; i >= 0; i--) {
397 		cmdbuf[0] = i;
398 		if (iic_exec(sc->sc_tag,
399 			     i ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
400 			     sc->sc_address, cmdbuf, 1, &bcd[i], 1,
401 			     I2C_F_POLL)) {
402 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
403 			printf("%s: strtc_clock_write: failed to write rtc "
404 			    " at 0x%x\n", sc->sc_dev.dv_xname, i);
405 			/* XXX: Clock Hold is likely still asserted! */
406 			return (0);
407 		}
408 	}
409 
410 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
411 
412 	return (1);
413 }
414