Home | History | Annotate | Line # | Download | only in i2c
m41t00.c revision 1.10
      1 /*	$NetBSD: m41t00.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: m41t00.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/proc.h>
     49 #include <sys/event.h>
     50 
     51 #include <sys/bus.h>
     52 
     53 #include <dev/clock_subr.h>
     54 
     55 #include <dev/i2c/i2cvar.h>
     56 #include <dev/i2c/m41t00reg.h>
     57 
     58 struct m41t00_softc {
     59 	struct device sc_dev;
     60 	i2c_tag_t sc_tag;
     61 	int sc_address;
     62 	int sc_open;
     63 	struct todr_chip_handle sc_todr;
     64 };
     65 
     66 static int  m41t00_match(struct device *, struct cfdata *, void *);
     67 static void m41t00_attach(struct device *, struct device *, void *);
     68 
     69 CFATTACH_DECL(m41trtc, sizeof(struct m41t00_softc),
     70 	m41t00_match, m41t00_attach, NULL, NULL);
     71 extern struct cfdriver m41trtc_cd;
     72 
     73 dev_type_open(m41t00_open);
     74 dev_type_close(m41t00_close);
     75 dev_type_read(m41t00_read);
     76 dev_type_write(m41t00_write);
     77 
     78 const struct cdevsw m41t00_cdevsw = {
     79 	m41t00_open, m41t00_close, m41t00_read, m41t00_write, noioctl,
     80 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
     81 };
     82 
     83 static int m41t00_clock_read(struct m41t00_softc *, struct clock_ymdhms *);
     84 static int m41t00_clock_write(struct m41t00_softc *, struct clock_ymdhms *);
     85 static int m41t00_gettime(struct todr_chip_handle *, volatile struct timeval *);
     86 static int m41t00_settime(struct todr_chip_handle *, volatile struct timeval *);
     87 
     88 int
     89 m41t00_match(struct device *parent, struct cfdata *cf, void *aux)
     90 {
     91 	struct i2c_attach_args *ia = aux;
     92 
     93 	if (ia->ia_addr == M41T00_ADDR) {
     94 		return 1;
     95 	}
     96 
     97 	return 0;
     98 }
     99 
    100 void
    101 m41t00_attach(struct device *parent, struct device *self, void *aux)
    102 {
    103 	struct m41t00_softc *sc = device_private(self);
    104 	struct i2c_attach_args *ia = aux;
    105 
    106 	sc->sc_tag = ia->ia_tag;
    107 	sc->sc_address = ia->ia_addr;
    108 
    109 	aprint_naive(": Real-time Clock\n");
    110 	aprint_normal(": M41T00 Real-time Clock\n");
    111 
    112 	sc->sc_open = 0;
    113 	sc->sc_todr.cookie = sc;
    114 	sc->sc_todr.todr_gettime = m41t00_gettime;
    115 	sc->sc_todr.todr_settime = m41t00_settime;
    116 	sc->sc_todr.todr_setwen = NULL;
    117 
    118 	todr_attach(&sc->sc_todr);
    119 }
    120 
    121 /*ARGSUSED*/
    122 int
    123 m41t00_open(dev_t dev, int flag, int fmt, struct lwp *l)
    124 {
    125 	struct m41t00_softc *sc;
    126 
    127 	if ((sc = device_lookup(&m41trtc_cd, minor(dev))) == NULL)
    128 		return ENXIO;
    129 
    130 	/* XXX: Locking */
    131 
    132 	if (sc->sc_open)
    133 		return EBUSY;
    134 
    135 	sc->sc_open = 1;
    136 	return 0;
    137 }
    138 
    139 /*ARGSUSED*/
    140 int
    141 m41t00_close(dev_t dev, int flag, int fmt, struct lwp *l)
    142 {
    143 	struct m41t00_softc *sc;
    144 
    145 	if ((sc = device_lookup(&m41trtc_cd, minor(dev))) == NULL)
    146 		return ENXIO;
    147 
    148 	sc->sc_open = 0;
    149 	return 0;
    150 }
    151 
    152 /*ARGSUSED*/
    153 int
    154 m41t00_read(dev_t dev, struct uio *uio, int flags)
    155 {
    156 	struct m41t00_softc *sc;
    157 	u_int8_t ch, cmdbuf[1];
    158 	int a, error;
    159 
    160 	if ((sc = device_lookup(&m41trtc_cd, minor(dev))) == NULL)
    161 		return (ENXIO);
    162 
    163 	if (uio->uio_offset >= M41T00_NBYTES)
    164 		return (EINVAL);
    165 
    166 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    167 		return (error);
    168 
    169 	while (uio->uio_resid && uio->uio_offset < M41T00_NBYTES) {
    170 		a = (int)uio->uio_offset;
    171 		cmdbuf[0] = a;
    172 		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    173 				      sc->sc_address, cmdbuf, 1,
    174 				      &ch, 1, 0)) != 0) {
    175 			iic_release_bus(sc->sc_tag, 0);
    176 			printf("%s: m41t00_read: read failed at 0x%x\n",
    177 			    sc->sc_dev.dv_xname, a);
    178 			return (error);
    179 		}
    180 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    181 			iic_release_bus(sc->sc_tag, 0);
    182 			return (error);
    183 		}
    184 	}
    185 
    186 	iic_release_bus(sc->sc_tag, 0);
    187 
    188 	return (0);
    189 }
    190 
    191 /*ARGSUSED*/
    192 int
    193 m41t00_write(dev_t dev, struct uio *uio, int flags)
    194 {
    195 	struct m41t00_softc *sc;
    196 	u_int8_t cmdbuf[2];
    197 	int a, error;
    198 
    199 	if ((sc = device_lookup(&m41trtc_cd, minor(dev))) == NULL)
    200 		return (ENXIO);
    201 
    202 	if (uio->uio_offset >= M41T00_NBYTES)
    203 		return (EINVAL);
    204 
    205 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    206 		return (error);
    207 
    208 	while (uio->uio_resid && uio->uio_offset < M41T00_NBYTES) {
    209 		a = (int)uio->uio_offset;
    210 
    211 		cmdbuf[0] = a;
    212 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
    213 			break;
    214 
    215 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    216 				      sc->sc_address,
    217 				      cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
    218 			printf("%s: m41t00_write: write failed at 0x%x\n",
    219 			    sc->sc_dev.dv_xname, a);
    220 			break;
    221 		}
    222 	}
    223 
    224 	iic_release_bus(sc->sc_tag, 0);
    225 
    226 	return (error);
    227 }
    228 
    229 static int
    230 m41t00_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv)
    231 {
    232 	struct m41t00_softc *sc = ch->cookie;
    233 	struct clock_ymdhms dt;
    234 
    235 	if (m41t00_clock_read(sc, &dt) == 0)
    236 		return (-1);
    237 
    238 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    239 	tv->tv_usec = 0;
    240 
    241 	return (0);
    242 }
    243 
    244 static int
    245 m41t00_settime(struct todr_chip_handle *ch, volatile struct timeval *tv)
    246 {
    247 	struct m41t00_softc *sc = ch->cookie;
    248 	struct clock_ymdhms dt;
    249 
    250 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    251 
    252 	if (m41t00_clock_write(sc, &dt) == 0)
    253 		return (-1);
    254 
    255 	return (0);
    256 }
    257 
    258 static int m41t00_rtc_offset[] = {
    259 	M41T00_SEC,
    260 	M41T00_MIN,
    261 	M41T00_CENHR,
    262 	M41T00_DAY,
    263 	M41T00_DATE,
    264 	M41T00_MONTH,
    265 	M41T00_YEAR,
    266 };
    267 
    268 static int
    269 m41t00_clock_read(struct m41t00_softc *sc, struct clock_ymdhms *dt)
    270 {
    271 	u_int8_t bcd[M41T00_NBYTES], cmdbuf[1];
    272 	int i, n;
    273 
    274 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    275 		printf("%s: m41t00_clock_read: failed to acquire I2C bus\n",
    276 		    sc->sc_dev.dv_xname);
    277 		return (0);
    278 	}
    279 
    280 	/* Read each timekeeping register in order. */
    281 	n = sizeof(m41t00_rtc_offset) / sizeof(m41t00_rtc_offset[0]);
    282 	for (i = 0; i < n ; i++) {
    283 		cmdbuf[0] = m41t00_rtc_offset[i];
    284 
    285 		if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    286 			     sc->sc_address, cmdbuf, 1,
    287 			     &bcd[i], 1, I2C_F_POLL)) {
    288 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    289 			printf("%s: m41t00_clock_read: failed to read rtc "
    290 			    "at 0x%x\n", sc->sc_dev.dv_xname,
    291 			    m41t00_rtc_offset[i]);
    292 			return (0);
    293 		}
    294 	}
    295 
    296 	/* Done with I2C */
    297 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    298 
    299 	/*
    300 	 * Convert the M41T00's register values into something useable
    301 	 */
    302 	dt->dt_sec = FROMBCD(bcd[M41T00_SEC] & M41T00_SEC_MASK);
    303 	dt->dt_min = FROMBCD(bcd[M41T00_MIN] & M41T00_MIN_MASK);
    304 	dt->dt_hour = FROMBCD(bcd[M41T00_CENHR] & M41T00_HOUR_MASK);
    305 	dt->dt_day = FROMBCD(bcd[M41T00_DATE] & M41T00_DATE_MASK);
    306 	dt->dt_wday = FROMBCD(bcd[M41T00_DAY] & M41T00_DAY_MASK);
    307 	dt->dt_mon = FROMBCD(bcd[M41T00_MONTH] & M41T00_MONTH_MASK);
    308 	dt->dt_year = FROMBCD(bcd[M41T00_YEAR] & M41T00_YEAR_MASK);
    309 
    310 	/*
    311 	 * Since the m41t00 just stores 00-99, and this is 2003 as I write
    312 	 * this comment, use 2000 as a base year
    313 	 */
    314 	dt->dt_year += 2000;
    315 
    316 	return (1);
    317 }
    318 
    319 static int
    320 m41t00_clock_write(struct m41t00_softc *sc, struct clock_ymdhms *dt)
    321 {
    322 	uint8_t bcd[M41T00_DATE_BYTES], cmdbuf[2];
    323 	uint8_t init_seconds, final_seconds;
    324 	int i;
    325 
    326 	/*
    327 	 * Convert our time representation into something the MAX6900
    328 	 * can understand.
    329 	 */
    330 	bcd[M41T00_SEC] = TOBCD(dt->dt_sec);
    331 	bcd[M41T00_MIN] = TOBCD(dt->dt_min);
    332 	bcd[M41T00_CENHR] = TOBCD(dt->dt_hour);
    333 	bcd[M41T00_DATE] = TOBCD(dt->dt_day);
    334 	bcd[M41T00_DAY] = TOBCD(dt->dt_wday);
    335 	bcd[M41T00_MONTH] = TOBCD(dt->dt_mon);
    336 	bcd[M41T00_YEAR] = TOBCD(dt->dt_year % 100);
    337 
    338 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    339 		printf("%s: m41t00_clock_write: failed to acquire I2C bus\n",
    340 		    sc->sc_dev.dv_xname);
    341 		return (0);
    342 	}
    343 
    344 	/*
    345 	 * The MAX6900 RTC manual recommends ensuring "atomicity" of
    346 	 * a non-burst write by:
    347 	 *
    348 	 *	- writing SECONDS
    349 	 *	- reading back SECONDS, remembering it as "initial seconds"
    350 	 *	- write the remaing RTC registers
    351 	 *	- read back SECONDS as "final seconds"
    352 	 *	- if "initial seconds" == 59, ensure "final seconds" == 59
    353 	 *	- else, ensure "final seconds" is no more than one second
    354 	 *	  beyond "initial seconds".
    355 	 *
    356 	 * This sounds reasonable for the M41T00, too.
    357 	 */
    358  again:
    359 	cmdbuf[0] = M41T00_SEC;
    360 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    361 		     cmdbuf, 1, &bcd[M41T00_SEC], 1, I2C_F_POLL)) {
    362 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    363 		printf("%s: m41t00_clock_write: failed to write SECONDS\n",
    364 		    sc->sc_dev.dv_xname);
    365 		return (0);
    366 	}
    367 
    368 	cmdbuf[0] = M41T00_SEC;
    369 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    370 		     cmdbuf, 1, &init_seconds, 1, I2C_F_POLL)) {
    371 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    372 		printf("%s: m41t00_clock_write: failed to read "
    373 		    "INITIAL SECONDS\n", sc->sc_dev.dv_xname);
    374 		return (0);
    375 	}
    376 	init_seconds = FROMBCD(init_seconds & M41T00_SEC_MASK);
    377 
    378 	for (i = 1; i < M41T00_DATE_BYTES; i++) {
    379 		cmdbuf[0] = m41t00_rtc_offset[i];
    380 		if (iic_exec(sc->sc_tag,
    381 			     I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    382 			     cmdbuf, 1, &bcd[i], 1, I2C_F_POLL)) {
    383 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    384 			printf("%s: m41t00_clock_write: failed to write rtc "
    385 			    " at 0x%x\n", sc->sc_dev.dv_xname,
    386 			    m41t00_rtc_offset[i]);
    387 			return (0);
    388 		}
    389 	}
    390 
    391 	cmdbuf[0] = M41T00_SEC;
    392 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    393 		     cmdbuf, 1, &final_seconds, 1, I2C_F_POLL)) {
    394 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    395 		printf("%s: m41t00_clock_write: failed to read "
    396 		    "FINAL SECONDS\n", sc->sc_dev.dv_xname);
    397 		return (0);
    398 	}
    399 	final_seconds = FROMBCD(final_seconds & M41T00_SEC_MASK);
    400 
    401 	if ((init_seconds != final_seconds) &&
    402 	    (((init_seconds + 1) % 60) != final_seconds)) {
    403 #if 1
    404 		printf("%s: m41t00_clock_write: init %d, final %d, try again\n",
    405 		    sc->sc_dev.dv_xname, init_seconds, final_seconds);
    406 #endif
    407 		goto again;
    408 	}
    409 
    410 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    411 
    412 	return (1);
    413 }
    414